test-dhcp-server.c revision 07630cea1f3a845c09309f197ac7c4f11edd3b62
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen This file is part of systemd.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen Copyright (C) 2013 Intel Corporation. All rights reserved.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen Copyright (C) 2014 Tom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen systemd is free software; you can redistribute it and/or modify it
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen under the terms of the GNU Lesser General Public License as published by
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen the Free Software Foundation; either version 2.1 of the License, or
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen (at your option) any later version.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen systemd is distributed in the hope that it will be useful, but
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen Lesser General Public License for more details.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen You should have received a copy of the GNU Lesser General Public License
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poetteringstatic void test_pool(struct in_addr *address, unsigned size, int ret) {
a5a807e63a50314e190e9166d8a453cd8dd258e3Zbigniew Jędrzejewski-Szmek _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering assert_se(sd_dhcp_server_new(&server, 1) >= 0);
39d8db043b599a7382f94bfc904d5e108af438bdLennart Poettering assert_se(sd_dhcp_server_configure_pool(server, address, 8, 0, size) == ret);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* attach to loopback interface */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_new(&server, 1) >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_attach_event(server, event, 0) >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_attach_event(server, event, 0) == -EBUSY);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_get_event(server) == event);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_detach_event(server) >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(!sd_dhcp_server_get_event(server));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_attach_event(server, NULL, 0) >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_attach_event(server, NULL, 0) == -EBUSY);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_ref(server) == server);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(!sd_dhcp_server_unref(server));
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering assert_se(sd_dhcp_server_start(server) == -EUNATCH);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_configure_pool(server, &address_any, 28, 0, 0) == -EINVAL);
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering assert_se(sd_dhcp_server_configure_pool(server, &address_lo, 38, 0, 0) == -ERANGE);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_configure_pool(server, &address_lo, 8, 0, 0) >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_configure_pool(server, &address_lo, 8, 0, 0) == -EBUSY);
21d73c87b09ec2b8642424bc714ce9af3da4fc40Lennart Poettering assert_se(sd_dhcp_server_start(server) == -EBUSY);
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering assert_se(sd_dhcp_server_stop(server) >= 0);
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering assert_se(sd_dhcp_server_stop(server) >= 0);
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering assert_se(sd_dhcp_server_start(server) >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void test_message_handler(void) {
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .message.chaddr = { 'A', 'B', 'C', 'D', 'E', 'F' },
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .option_type.code = DHCP_OPTION_MESSAGE_TYPE,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_new(&server, 1) >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_configure_pool(server, &address_lo, 8, 0, 0) >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_attach_event(server, NULL, 0) >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_start(server) >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* TODO, shouldn't this fail? */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_type.code = DHCP_OPTION_MESSAGE_TYPE;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_requested_ip.code = DHCP_OPTION_REQUESTED_IP_ADDRESS;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_requested_ip.address = htobe32(0x12345678);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_NAK);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_server_id.code = DHCP_OPTION_SERVER_IDENTIFIER;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_server_id.address = htobe32(INADDR_LOOPBACK);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_server_id.address = htobe32(0x12345678);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_server_id.address = htobe32(INADDR_LOOPBACK);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 4);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_client_id.code = DHCP_OPTION_CLIENT_IDENTIFIER;
cc98b3025eeb89addb76a27390cb2baca4eab8b9Torstein Husebø assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 30);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic uint64_t client_id_hash_helper(DHCPClientId *id, uint8_t key[HASH_KEY_SIZE]) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering siphash24_finalize((uint8_t*)&hash, &state);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void test_client_id_hash(void) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(client_id_compare_func(&a, &b) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(client_id_hash_helper(&a, hash_key) == client_id_hash_helper(&b, hash_key));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(client_id_compare_func(&a, &b) != 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(client_id_compare_func(&a, &b) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(client_id_hash_helper(&a, hash_key) == client_id_hash_helper(&b, hash_key));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(client_id_compare_func(&a, &b) != 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(client_id_compare_func(&a, &b) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(client_id_hash_helper(&a, hash_key) == client_id_hash_helper(&b, hash_key));