test-dhcp-server.c revision 4dc355680460fdc8e0d590d8572dff1b6a257d88
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/>.
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering /* attach to loopback interface */
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering assert_se(sd_dhcp_server_new(&server, 1) >= 0);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_attach_event(server, event, 0) >= 0);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_attach_event(server, event, 0) == -EBUSY);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_get_event(server) == event);
6c03089c32c251d823173bda4d809a9e643219f0Lennart 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);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_ref(server) == server);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(!sd_dhcp_server_unref(server));
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering assert_se(sd_dhcp_server_start(server) == -EUNATCH);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_any) == -EINVAL);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_lo) >= 0);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_lo) == -EBUSY);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_start(server) >= 0);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering assert_se(sd_dhcp_server_start(server) == -EBUSY);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_stop(server) >= 0);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_stop(server) >= 0);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_start(server) >= 0);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poetteringstatic void test_message_handler(void) {
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering .option_type.code = DHCP_OPTION_MESSAGE_TYPE,
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_new(&server, 1) >= 0);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_lo) >= 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)) == DHCP_OFFER);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering /* TODO, shouldn't this fail? */
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering test.option_type.code = DHCP_OPTION_MESSAGE_TYPE;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);