test-dhcp-server.c revision 07630cea1f3a845c09309f197ac7c4f11edd3b62
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen/***
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen This file is part of systemd.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen Copyright (C) 2013 Intel Corporation. All rights reserved.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen Copyright (C) 2014 Tom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen systemd is free software; you can redistribute it and/or modify it
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen under the terms of the GNU Lesser General Public License as published by
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen the Free Software Foundation; either version 2.1 of the License, or
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen (at your option) any later version.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen systemd is distributed in the hope that it will be useful, but
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen Lesser General Public License for more details.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen You should have received a copy of the GNU Lesser General Public License
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen***/
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <errno.h>
0a6f50c0afdfc434b492493bd9efab20cbee8623Thomas Hindoe Paaboel Andersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "sd-dhcp-server.h"
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen#include "sd-event.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen#include "dhcp-server-internal.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "event-util.h"
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poetteringstatic void test_pool(struct in_addr *address, unsigned size, int ret) {
a5a807e63a50314e190e9166d8a453cd8dd258e3Zbigniew Jędrzejewski-Szmek _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering assert_se(sd_dhcp_server_new(&server, 1) >= 0);
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
39d8db043b599a7382f94bfc904d5e108af438bdLennart Poettering assert_se(sd_dhcp_server_configure_pool(server, address, 8, 0, size) == ret);
39d8db043b599a7382f94bfc904d5e108af438bdLennart Poettering}
39d8db043b599a7382f94bfc904d5e108af438bdLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int test_basic(sd_event *event) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct in_addr address_lo = {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .s_addr = htonl(INADDR_LOOPBACK),
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering };
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct in_addr address_any = {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .s_addr = htonl(INADDR_ANY),
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering };
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* attach to loopback interface */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_new(&server, 1) >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(server);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart 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));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering assert_se(sd_dhcp_server_start(server) == -EUNATCH);
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_configure_pool(server, &address_any, 28, 0, 0) == -EINVAL);
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering assert_se(sd_dhcp_server_configure_pool(server, &address_lo, 38, 0, 0) == -ERANGE);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_configure_pool(server, &address_lo, 8, 0, 0) >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_configure_pool(server, &address_lo, 8, 0, 0) == -EBUSY);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test_pool(&address_any, 1, -EINVAL);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test_pool(&address_lo, 1, 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_dhcp_server_start(server);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r == -EPERM)
21d73c87b09ec2b8642424bc714ce9af3da4fc40Lennart Poettering return EXIT_TEST_SKIP;
21d73c87b09ec2b8642424bc714ce9af3da4fc40Lennart Poettering assert_se(r >= 0);
21d73c87b09ec2b8642424bc714ce9af3da4fc40Lennart Poettering
21d73c87b09ec2b8642424bc714ce9af3da4fc40Lennart Poettering assert_se(sd_dhcp_server_start(server) == -EBUSY);
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering assert_se(sd_dhcp_server_stop(server) >= 0);
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering assert_se(sd_dhcp_server_stop(server) >= 0);
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering assert_se(sd_dhcp_server_start(server) >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void test_message_handler(void) {
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DHCPMessage message;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart 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;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt uint8_t length;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart 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;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } _packed_ option_server_id;
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering struct {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering uint8_t code;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering uint8_t length;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering uint8_t id[7];
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } _packed_ option_client_id;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering uint8_t end;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } _packed_ test = {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .message.op = BOOTREQUEST,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .message.htype = ARPHRD_ETHER,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .message.hlen = ETHER_ADDR_LEN,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .message.xid = htobe32(0x12345678),
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .message.chaddr = { 'A', 'B', 'C', 'D', 'E', 'F' },
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .option_type.code = DHCP_OPTION_MESSAGE_TYPE,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .option_type.length = 1,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .option_type.type = DHCP_DISCOVER,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .end = DHCP_OPTION_END,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering };
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct in_addr address_lo = {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .s_addr = htonl(INADDR_LOOPBACK),
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering };
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_new(&server, 1) >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_configure_pool(server, &address_lo, 8, 0, 0) >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_attach_event(server, NULL, 0) >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_dhcp_server_start(server) >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart 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;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart 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 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);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.message.op = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.message.op = BOOTREQUEST;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart 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;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart 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;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart 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;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_requested_ip.length = 4;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test.option_requested_ip.address = htobe32(0x12345678);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_NAK);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart 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);
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt 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';
cc98b3025eeb89addb76a27390cb2baca4eab8b9Torstein Husebø 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);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic uint64_t client_id_hash_helper(DHCPClientId *id, uint8_t key[HASH_KEY_SIZE]) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct siphash state;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering uint64_t hash;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering siphash24_init(&state, key);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering client_id_hash_func(id, &state);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering siphash24_finalize((uint8_t*)&hash, &state);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return hash;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void test_client_id_hash(void) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DHCPClientId a = {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .length = 4,
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen }, b = {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .length = 4,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering };
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering uint8_t hash_key[HASH_KEY_SIZE] = {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering '0', '1', '2', '3', '4', '5', '6', '7',
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering };
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering a.data = (uint8_t*)strdup("abcd");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering b.data = (uint8_t*)strdup("abcd");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(client_id_compare_func(&a, &b) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(client_id_hash_helper(&a, hash_key) == client_id_hash_helper(&b, hash_key));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering a.length = 3;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(client_id_compare_func(&a, &b) != 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering a.length = 4;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(client_id_compare_func(&a, &b) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(client_id_hash_helper(&a, hash_key) == client_id_hash_helper(&b, hash_key));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering b.length = 3;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(client_id_compare_func(&a, &b) != 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering b.length = 4;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(client_id_compare_func(&a, &b) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(client_id_hash_helper(&a, hash_key) == client_id_hash_helper(&b, hash_key));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering free(b.data);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering b.data = (uint8_t*)strdup("abce");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(client_id_compare_func(&a, &b) != 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering free(a.data);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering free(b.data);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint main(int argc, char *argv[]) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_event_unref_ sd_event *e;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_set_max_level(LOG_DEBUG);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_parse_environment();
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_open();
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(sd_event_new(&e) >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = test_basic(e);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r != 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test_message_handler();
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test_client_id_hash();
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering