test-dhcp-server.c revision 6f08fb7b34b4eebf664de83c8bc7b9941658aeb4
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright (C) 2013 Intel Corporation. All rights reserved.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright (C) 2014 Tom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering /* attach to loopback interface */
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart 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));
c73ce96b569e2f10dff64b7dc0bd271972674c2aLennart Poettering assert_se(sd_dhcp_server_start(server) == -EUNATCH);
c73ce96b569e2f10dff64b7dc0bd271972674c2aLennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_any) == -EINVAL);
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_lo) >= 0);
c73ce96b569e2f10dff64b7dc0bd271972674c2aLennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_lo) == -EBUSY);
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering assert_se(sd_dhcp_server_set_lease_pool(server, &address_any, 1) == -EINVAL);
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 0) == -EINVAL);
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 1) >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 1) == -EBUSY);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(sd_dhcp_server_start(server) == -EBUSY);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(sd_dhcp_server_stop(server) >= 0);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(sd_dhcp_server_stop(server) >= 0);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(sd_dhcp_server_start(server) >= 0);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poetteringstatic void test_message_handler(void) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering .message.chaddr = { 'A', 'B', 'C', 'D', 'E', 'F' },
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering .option_type.code = DHCP_OPTION_MESSAGE_TYPE,
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering assert_se(sd_dhcp_server_new(&server, 1) >= 0);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_lo) >= 0);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert_se(sd_dhcp_server_attach_event(server, NULL, 0) >= 0);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert_se(sd_dhcp_server_start(server) >= 0);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 10) >= 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);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart 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);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek 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);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart 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);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek 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;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering test.option_requested_ip.address = htobe32(0x12345678);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_NAK);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart 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);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_client_id.code = DHCP_OPTION_CLIENT_IDENTIFIER;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 30);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poetteringstatic void test_client_id_hash(void) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek uint8_t hash_key[HASH_KEY_SIZE] = {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek '0', '1', '2', '3', '4', '5', '6', '7',
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek a.data = (uint8_t*)strdup("abcd");
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek b.data = (uint8_t*)strdup("abcd");
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek assert_se(client_id_compare_func(&a, &b) == 0);
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek assert_se(client_id_hash_func(&a, hash_key) == client_id_hash_func(&b, hash_key));
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek assert_se(client_id_compare_func(&a, &b) != 0);
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek assert_se(client_id_compare_func(&a, &b) == 0);
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek assert_se(client_id_hash_func(&a, hash_key) == client_id_hash_func(&b, hash_key));
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek assert_se(client_id_compare_func(&a, &b) != 0);
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek assert_se(client_id_compare_func(&a, &b) == 0);
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek assert_se(client_id_hash_func(&a, hash_key) == client_id_hash_func(&b, hash_key));
51323288fc628a5cac50914df915545d685b793eLennart Poettering assert_se(client_id_compare_func(&a, &b) != 0);
log_open();
r = test_basic(e);