test-dhcp-server.c revision 6f08fb7b34b4eebf664de83c8bc7b9941658aeb4
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright (C) 2013 Intel Corporation. All rights reserved.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright (C) 2014 Tom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
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
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
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***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <netinet/if_ether.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <assert.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <errno.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "sd-event.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "event-util.h"
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering#include "sd-dhcp-server.h"
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering#include "dhcp-server-internal.h"
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poetteringstatic int test_basic(sd_event *event) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering struct in_addr address_lo = {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering .s_addr = htonl(INADDR_LOOPBACK),
51323288fc628a5cac50914df915545d685b793eLennart Poettering };
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct in_addr address_any = {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering .s_addr = htonl(INADDR_ANY),
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering };
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering int r;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering /* attach to loopback interface */
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering assert_se(sd_dhcp_server_new(&server, 1) >= 0);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering assert_se(server);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
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
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_ref(server) == server);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(!sd_dhcp_server_unref(server));
c73ce96b569e2f10dff64b7dc0bd271972674c2aLennart Poettering
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
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);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_dhcp_server_start(server);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (r == -EPERM)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return EXIT_TEST_SKIP;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(r >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
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 Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering return 0;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering}
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poetteringstatic void test_message_handler(void) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering struct {
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering DHCPMessage message;
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering struct {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering uint8_t code;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering uint8_t length;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering uint8_t type;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } _packed_ option_type;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering uint8_t code;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering uint8_t length;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering be32_t address;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } _packed_ option_requested_ip;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering uint8_t code;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering uint8_t length;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering be32_t address;
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering } _packed_ option_server_id;
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering struct {
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering uint8_t code;
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering uint8_t length;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering uint8_t id[7];
ea917db9e662ae6e6d0ae07e0118b323688c8616Lennart Poettering } _packed_ option_client_id;
ea917db9e662ae6e6d0ae07e0118b323688c8616Lennart Poettering uint8_t end;
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering } _packed_ test = {
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering .message.op = BOOTREQUEST,
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering .message.htype = ARPHRD_ETHER,
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering .message.hlen = ETHER_ADDR_LEN,
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering .message.xid = htobe32(0x12345678),
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering .message.chaddr = { 'A', 'B', 'C', 'D', 'E', 'F' },
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering .option_type.code = DHCP_OPTION_MESSAGE_TYPE,
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering .option_type.length = 1,
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering .option_type.type = DHCP_DISCOVER,
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering .end = DHCP_OPTION_END,
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering };
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering struct in_addr address_lo = {
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering .s_addr = htonl(INADDR_LOOPBACK),
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering };
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering
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
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);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering test.end = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* TODO, shouldn't this fail? */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.end = DHCP_OPTION_END;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_type.code = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_type.length = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_type.type = 0;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_type.code = DHCP_OPTION_MESSAGE_TYPE;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering test.option_type.length = 1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_type.type = DHCP_DISCOVER;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.message.op = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek test.message.op = BOOTREQUEST;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering test.message.htype = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.message.htype = ARPHRD_ETHER;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.message.hlen = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.message.hlen = ETHER_ADDR_LEN;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering test.option_type.type = DHCP_REQUEST;
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.length = 4;
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.length = 4;
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
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
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_client_id.code = DHCP_OPTION_CLIENT_IDENTIFIER;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_client_id.length = 7;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_client_id.id[0] = 0x01;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_client_id.id[1] = 'A';
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_client_id.id[2] = 'B';
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_client_id.id[3] = 'C';
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_client_id.id[4] = 'D';
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_client_id.id[5] = 'E';
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_client_id.id[6] = 'F';
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
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 Poettering}
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poetteringstatic void test_client_id_hash(void) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering DHCPClientId a = {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering .length = 4,
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek }, b = {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek .length = 4,
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek };
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',
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek };
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek a.data = (uint8_t*)strdup("abcd");
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek b.data = (uint8_t*)strdup("abcd");
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek
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 a.length = 3;
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek assert_se(client_id_compare_func(&a, &b) != 0);
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek a.length = 4;
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
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek b.length = 3;
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek assert_se(client_id_compare_func(&a, &b) != 0);
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek b.length = 4;
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
51323288fc628a5cac50914df915545d685b793eLennart Poettering free(b.data);
51323288fc628a5cac50914df915545d685b793eLennart Poettering b.data = (uint8_t*)strdup("abce");
51323288fc628a5cac50914df915545d685b793eLennart Poettering assert_se(client_id_compare_func(&a, &b) != 0);
51323288fc628a5cac50914df915545d685b793eLennart Poettering
51323288fc628a5cac50914df915545d685b793eLennart Poettering free(a.data);
51323288fc628a5cac50914df915545d685b793eLennart Poettering free(b.data);
51323288fc628a5cac50914df915545d685b793eLennart Poettering}
51323288fc628a5cac50914df915545d685b793eLennart Poettering
51323288fc628a5cac50914df915545d685b793eLennart Poetteringint main(int argc, char *argv[]) {
51323288fc628a5cac50914df915545d685b793eLennart Poettering _cleanup_event_unref_ sd_event *e;
51323288fc628a5cac50914df915545d685b793eLennart Poettering int r;
51323288fc628a5cac50914df915545d685b793eLennart Poettering
log_set_max_level(LOG_DEBUG);
log_parse_environment();
log_open();
assert_se(sd_event_new(&e) >= 0);
r = test_basic(e);
if (r != 0)
return r;
test_message_handler();
test_client_id_hash();
return 0;
}