test-dhcp-server.c revision 2f8557eb3649840d76d9eb7d688a3c5c2922d31c
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen This file is part of systemd.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen Copyright (C) 2013 Intel Corporation. All rights reserved.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen Copyright (C) 2014 Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen systemd is free software; you can redistribute it and/or modify it
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen under the terms of the GNU Lesser General Public License as published by
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen the Free Software Foundation; either version 2.1 of the License, or
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen (at your option) any later version.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen systemd is distributed in the hope that it will be useful, but
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen Lesser General Public License for more details.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen You should have received a copy of the GNU Lesser General Public License
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen /* attach to loopback interface */
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_new(&server, 1) >= 0);
a501033335ed402c8f7e86fe41a15531ba69abd7Tom Gundersen assert_se(sd_dhcp_server_attach_event(server, event, 0) >= 0);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert_se(sd_dhcp_server_attach_event(server, event, 0) == -EBUSY);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert_se(sd_dhcp_server_get_event(server) == event);
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen assert_se(sd_dhcp_server_detach_event(server) >= 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_attach_event(server, NULL, 0) >= 0);
97f2d76d4f4dfab8b0629c09926a05a1e5621125Tom Gundersen assert_se(sd_dhcp_server_attach_event(server, NULL, 0) == -EBUSY);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_ref(server) == server);
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen assert_se(sd_dhcp_server_start(server) == -EUNATCH);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_set_address(server, &address_any, 28) == -EINVAL);
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen assert_se(sd_dhcp_server_set_address(server, &address_lo, 38) == -ERANGE);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_set_address(server, &address_lo, 8) >= 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_set_address(server, &address_lo, 8) == -EBUSY);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_set_lease_pool(server, &address_any, 1) == -EINVAL);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 0) == -EINVAL);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 1) >= 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 1) == -EBUSY);
9dc670ea766c711741f462b29572f2e5f8f3f6bcTom Gundersen assert_se(sd_dhcp_server_start(server) == -EBUSY);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersenstatic void test_message_handler(void) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
977085794d2996320e345433403de75f662b0622Tom Gundersen .message.chaddr = { 'A', 'B', 'C', 'D', 'E', 'F' },
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_new(&server, 1) >= 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_set_address(server, &address_lo, 8) >= 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_attach_event(server, NULL, 0) >= 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 10) >= 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen /* TODO, shouldn't this fail? */
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_type.code = DHCP_OPTION_MESSAGE_TYPE;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_requested_ip.code = DHCP_OPTION_REQUESTED_IP_ADDRESS;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_requested_ip.address = htobe32(0x12345678);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_NAK);
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen test.option_server_id.code = DHCP_OPTION_SERVER_IDENTIFIER;
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen test.option_server_id.address = htobe32(INADDR_LOOPBACK);
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_server_id.address = htobe32(0x12345678);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_server_id.address = htobe32(INADDR_LOOPBACK);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 4);
98a375f6d5cac24eb80d6d4e00699851324afdecTom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_client_id.code = DHCP_OPTION_CLIENT_IDENTIFIER;
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 30);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersenstatic void test_client_id_hash(void) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(client_id_compare_func(&a, &b) == 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(client_id_hash_func(&a, hash_key) == client_id_hash_func(&b, hash_key));
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(client_id_compare_func(&a, &b) != 0);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert_se(client_id_compare_func(&a, &b) == 0);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert_se(client_id_hash_func(&a, hash_key) == client_id_hash_func(&b, hash_key));
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert_se(client_id_compare_func(&a, &b) != 0);
97f2d76d4f4dfab8b0629c09926a05a1e5621125Tom Gundersen assert_se(client_id_compare_func(&a, &b) == 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(client_id_hash_func(&a, hash_key) == client_id_hash_func(&b, hash_key));