test-dhcp-server.c revision b6e676ce41508e2aeea22202fc8f234126177f52
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering This file is part of systemd.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering Copyright (C) 2013 Intel Corporation. All rights reserved.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering Copyright (C) 2014 Tom Gundersen
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering systemd is free software; you can redistribute it and/or modify it
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering under the terms of the GNU Lesser General Public License as published by
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering (at your option) any later version.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering systemd is distributed in the hope that it will be useful, but
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering Lesser General Public License for more details.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering You should have received a copy of the GNU Lesser General Public License
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmekstatic int test_basic(sd_event *event) {
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek .s_addr = htonl(INADDR_LOOPBACK),
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering /* attach to loopback interface */
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering assert_se(sd_dhcp_server_new(&server, 1) >= 0);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering assert_se(sd_dhcp_server_attach_event(server, event, 0) >= 0);
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering assert_se(sd_dhcp_server_attach_event(server, event, 0) == -EBUSY);
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering assert_se(sd_dhcp_server_get_event(server) == event);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering assert_se(sd_dhcp_server_detach_event(server) >= 0);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(!sd_dhcp_server_get_event(server));
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_attach_event(server, NULL, 0) >= 0);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_attach_event(server, NULL, 0) == -EBUSY);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_ref(server) == server);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(!sd_dhcp_server_unref(server));
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_start(server) == -EUNATCH);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_set_address(server, &address_any, 28) == -EINVAL);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_lo, 38) == -ERANGE);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_lo, 8) >= 0);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_lo, 8) == -EBUSY);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_set_lease_pool(server, &address_any, 1) == -EINVAL);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 0) == -EINVAL);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 1) >= 0);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 1) == -EBUSY);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_start(server) == -EBUSY);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_stop(server) >= 0);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_stop(server) >= 0);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_start(server) >= 0);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmekstatic void test_message_handler(void) {
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering .message.chaddr = { 'A', 'B', 'C', 'D', 'E', 'F' },
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering .option_type.code = DHCP_OPTION_MESSAGE_TYPE,
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_new(&server, 1) >= 0);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_lo, 8) >= 0);
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering assert_se(sd_dhcp_server_attach_event(server, NULL, 0) >= 0);
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering assert_se(sd_dhcp_server_start(server) >= 0);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 10) >= 0);
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering /* TODO, shouldn't this fail? */
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering test.option_type.code = DHCP_OPTION_MESSAGE_TYPE;
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering test.option_requested_ip.code = DHCP_OPTION_REQUESTED_IP_ADDRESS;
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering test.option_requested_ip.address = htobe32(0x12345678);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_NAK);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering test.option_server_id.code = DHCP_OPTION_SERVER_IDENTIFIER;
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering test.option_server_id.address = htobe32(INADDR_LOOPBACK);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering test.option_server_id.address = htobe32(0x12345678);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering test.option_server_id.address = htobe32(INADDR_LOOPBACK);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 4);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering test.option_client_id.code = DHCP_OPTION_CLIENT_IDENTIFIER;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 30);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poetteringstatic void test_client_id_hash(void) {
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek assert_se(client_id_compare_func(&a, &b) == 0);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering assert_se(client_id_hash_func(&a, hash_key) == client_id_hash_func(&b, hash_key));
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek assert_se(client_id_compare_func(&a, &b) != 0);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering assert_se(client_id_compare_func(&a, &b) == 0);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering assert_se(client_id_hash_func(&a, hash_key) == client_id_hash_func(&b, hash_key));
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering assert_se(client_id_compare_func(&a, &b) != 0);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering assert_se(client_id_compare_func(&a, &b) == 0);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering assert_se(client_id_hash_func(&a, hash_key) == client_id_hash_func(&b, hash_key));