test-dhcp-server.c revision bd57b45029ff25067704c9538e79f31e71c10045
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek This file is part of systemd.
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek Copyright (C) 2013 Intel Corporation. All rights reserved.
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek Copyright (C) 2014 Tom Gundersen
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek (at your option) any later version.
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek#include "dhcp-server-internal.h"
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmekstatic void test_basic(sd_event *event) {
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmek _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
799a8f39d8eb9ea725e85a598c0f5dbd658c8ba7Zbigniew Jędrzejewski-Szmek /* attach to loopback interface */
50a0b0717563d08c027a16a896bff8d7754eab9eZbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_new(&server, 1) >= 0);
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_attach_event(server, event, 0) >= 0);
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_attach_event(server, event, 0) == -EBUSY);
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_get_event(server) == event);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_detach_event(server) >= 0);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert_se(!sd_dhcp_server_get_event(server));
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_attach_event(server, NULL, 0) >= 0);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_attach_event(server, NULL, 0) == -EBUSY);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_ref(server) == server);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert_se(!sd_dhcp_server_unref(server));
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_start(server) == -EUNATCH);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_set_address(server, &address_any) == -EINVAL);
2cf4172a71860c6e44edd27a3b68047ae062d7fcLennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_lo) >= 0);
2cf4172a71860c6e44edd27a3b68047ae062d7fcLennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_lo) == -EBUSY);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_set_lease_pool(server, &address_any, 1) == -EINVAL);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 0) == -EINVAL);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 1) >= 0);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 1) == -EBUSY);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_start(server) >= 0);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_start(server) == -EBUSY);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_stop(server) >= 0);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_stop(server) >= 0);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_start(server) >= 0);
8847551bcbfa8265bae04f567bb1aadc7b480325Zbigniew Jędrzejewski-Szmekstatic void test_message_handler(void) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek .message.xid = htobe32(0x12345678),
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek .message.chaddr = { 'A', 'B', 'C', 'D', 'E', 'F' },
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek .option_type.code = DHCP_OPTION_MESSAGE_TYPE,
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek .option_type.type = DHCP_DISCOVER,
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_new(&server, 1) >= 0);
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_set_address(server, &address_lo) >= 0);
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt assert_se(sd_dhcp_server_attach_event(server, NULL, 0) >= 0);
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt assert_se(sd_dhcp_server_start(server) >= 0);
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 10) >= 0);
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek /* TODO, shouldn't this fail? */
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek test.option_type.code = DHCP_OPTION_MESSAGE_TYPE;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek test.option_type.type = DHCP_DISCOVER;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering test.option_requested_ip.code = DHCP_OPTION_REQUESTED_IP_ADDRESS;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek test.option_requested_ip.address = htobe32(0x12345678);
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_NAK);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek test.option_server_id.code = DHCP_OPTION_SERVER_IDENTIFIER;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek test.option_server_id.length = 4;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek test.option_server_id.address = htobe32(INADDR_LOOPBACK);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek test.option_server_id.address = htobe32(0x12345678);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek test.option_server_id.address = htobe32(INADDR_LOOPBACK);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 30);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
36d4739a68c3edafe4d145d525a26de4ef0b8e5aZbigniew Jędrzejewski-Szmekint main(int argc, char *argv[]) {