test-dhcp-server.c revision 2f8557eb3649840d76d9eb7d688a3c5c2922d31c
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen/***
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen This file is part of systemd.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen Copyright (C) 2013 Intel Corporation. All rights reserved.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen Copyright (C) 2014 Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen systemd is free software; you can redistribute it and/or modify it
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen under the terms of the GNU Lesser General Public License as published by
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen the Free Software Foundation; either version 2.1 of the License, or
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen (at your option) any later version.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen systemd is distributed in the hope that it will be useful, but
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen Lesser General Public License for more details.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen You should have received a copy of the GNU Lesser General Public License
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen***/
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen
2a73e0d39a9bec82c3800071e375d27164727e71Tom Gundersen#include <netinet/if_ether.h>
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen#include <assert.h>
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen#include <errno.h>
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen#include "sd-event.h"
a501033335ed402c8f7e86fe41a15531ba69abd7Tom Gundersen#include "event-util.h"
a501033335ed402c8f7e86fe41a15531ba69abd7Tom Gundersen
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen#include "sd-dhcp-server.h"
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen#include "dhcp-server-internal.h"
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersenstatic int test_basic(sd_event *event) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen struct in_addr address_lo = {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen .s_addr = htonl(INADDR_LOOPBACK),
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen };
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersen struct in_addr address_any = {
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen .s_addr = htonl(INADDR_ANY),
3aeb37bc4f32b5edc334f2ac7c5d3c7b0a121328Tom Gundersen };
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen int r;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen /* attach to loopback interface */
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_new(&server, 1) >= 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(server);
a501033335ed402c8f7e86fe41a15531ba69abd7Tom Gundersen
a501033335ed402c8f7e86fe41a15531ba69abd7Tom Gundersen assert_se(sd_dhcp_server_attach_event(server, event, 0) >= 0);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert_se(sd_dhcp_server_attach_event(server, event, 0) == -EBUSY);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert_se(sd_dhcp_server_get_event(server) == event);
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen assert_se(sd_dhcp_server_detach_event(server) >= 0);
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen assert_se(!sd_dhcp_server_get_event(server));
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_attach_event(server, NULL, 0) >= 0);
97f2d76d4f4dfab8b0629c09926a05a1e5621125Tom Gundersen assert_se(sd_dhcp_server_attach_event(server, NULL, 0) == -EBUSY);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_ref(server) == server);
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen assert_se(!sd_dhcp_server_unref(server));
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen assert_se(sd_dhcp_server_start(server) == -EUNATCH);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_set_address(server, &address_any, 28) == -EINVAL);
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen assert_se(sd_dhcp_server_set_address(server, &address_lo, 38) == -ERANGE);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_set_address(server, &address_lo, 8) >= 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_set_address(server, &address_lo, 8) == -EBUSY);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_set_lease_pool(server, &address_any, 1) == -EINVAL);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 0) == -EINVAL);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 1) >= 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 1) == -EBUSY);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen r = sd_dhcp_server_start(server);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
977085794d2996320e345433403de75f662b0622Tom Gundersen if (r == -EPERM)
977085794d2996320e345433403de75f662b0622Tom Gundersen return EXIT_TEST_SKIP;
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert_se(r >= 0);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen
9dc670ea766c711741f462b29572f2e5f8f3f6bcTom Gundersen assert_se(sd_dhcp_server_start(server) == -EBUSY);
9dc670ea766c711741f462b29572f2e5f8f3f6bcTom Gundersen assert_se(sd_dhcp_server_stop(server) >= 0);
9dc670ea766c711741f462b29572f2e5f8f3f6bcTom Gundersen assert_se(sd_dhcp_server_stop(server) >= 0);
c50e28a38ccbdd1e1d1f082dd67bb3e74be4d459Dave Reisner assert_se(sd_dhcp_server_start(server) >= 0);
c50e28a38ccbdd1e1d1f082dd67bb3e74be4d459Dave Reisner
c50e28a38ccbdd1e1d1f082dd67bb3e74be4d459Dave Reisner return 0;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen}
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersenstatic void test_message_handler(void) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen struct {
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen DHCPMessage message;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen struct {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen uint8_t code;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen uint8_t length;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen uint8_t type;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen } _packed_ option_type;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen struct {
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen uint8_t code;
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen uint8_t length;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen be32_t address;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen } _packed_ option_requested_ip;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen struct {
977085794d2996320e345433403de75f662b0622Tom Gundersen uint8_t code;
977085794d2996320e345433403de75f662b0622Tom Gundersen uint8_t length;
977085794d2996320e345433403de75f662b0622Tom Gundersen be32_t address;
977085794d2996320e345433403de75f662b0622Tom Gundersen } _packed_ option_server_id;
977085794d2996320e345433403de75f662b0622Tom Gundersen struct {
977085794d2996320e345433403de75f662b0622Tom Gundersen uint8_t code;
977085794d2996320e345433403de75f662b0622Tom Gundersen uint8_t length;
977085794d2996320e345433403de75f662b0622Tom Gundersen uint8_t id[7];
977085794d2996320e345433403de75f662b0622Tom Gundersen } _packed_ option_client_id;
977085794d2996320e345433403de75f662b0622Tom Gundersen uint8_t end;
977085794d2996320e345433403de75f662b0622Tom Gundersen } _packed_ test = {
977085794d2996320e345433403de75f662b0622Tom Gundersen .message.op = BOOTREQUEST,
977085794d2996320e345433403de75f662b0622Tom Gundersen .message.htype = ARPHRD_ETHER,
977085794d2996320e345433403de75f662b0622Tom Gundersen .message.hlen = ETHER_ADDR_LEN,
977085794d2996320e345433403de75f662b0622Tom Gundersen .message.xid = htobe32(0x12345678),
977085794d2996320e345433403de75f662b0622Tom Gundersen .message.chaddr = { 'A', 'B', 'C', 'D', 'E', 'F' },
977085794d2996320e345433403de75f662b0622Tom Gundersen .option_type.code = DHCP_OPTION_MESSAGE_TYPE,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen .option_type.length = 1,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen .option_type.type = DHCP_DISCOVER,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen .end = DHCP_OPTION_END,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen };
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen struct in_addr address_lo = {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen .s_addr = htonl(INADDR_LOOPBACK),
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen };
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_new(&server, 1) >= 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_set_address(server, &address_lo, 8) >= 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_attach_event(server, NULL, 0) >= 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_start(server) >= 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 10) >= 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.end = 0;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen /* TODO, shouldn't this fail? */
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.end = DHCP_OPTION_END;
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen test.option_type.code = 0;
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen test.option_type.length = 0;
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen test.option_type.type = 0;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_type.code = DHCP_OPTION_MESSAGE_TYPE;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_type.length = 1;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_type.type = DHCP_DISCOVER;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.message.op = 0;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.message.op = BOOTREQUEST;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.message.htype = 0;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.message.htype = ARPHRD_ETHER;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.message.hlen = 0;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.message.hlen = ETHER_ADDR_LEN;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_type.type = DHCP_REQUEST;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_requested_ip.code = DHCP_OPTION_REQUESTED_IP_ADDRESS;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_requested_ip.length = 4;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_requested_ip.address = htobe32(0x12345678);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_NAK);
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen test.option_server_id.code = DHCP_OPTION_SERVER_IDENTIFIER;
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen test.option_server_id.length = 4;
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen test.option_server_id.address = htobe32(INADDR_LOOPBACK);
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_server_id.address = htobe32(0x12345678);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_server_id.address = htobe32(INADDR_LOOPBACK);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 4);
98a375f6d5cac24eb80d6d4e00699851324afdecTom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_client_id.code = DHCP_OPTION_CLIENT_IDENTIFIER;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_client_id.length = 7;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_client_id.id[0] = 0x01;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_client_id.id[1] = 'A';
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_client_id.id[2] = 'B';
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_client_id.id[3] = 'C';
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_client_id.id[4] = 'D';
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_client_id.id[5] = 'E';
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test.option_client_id.id[6] = 'F';
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 30);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen}
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersenstatic void test_client_id_hash(void) {
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen DHCPClientId a = {
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen .length = 4,
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen }, b = {
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen .length = 4,
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen };
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen uint8_t hash_key[HASH_KEY_SIZE] = {
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen '0', '1', '2', '3', '4', '5', '6', '7',
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen };
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen a.data = (uint8_t*)strdup("abcd");
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen b.data = (uint8_t*)strdup("abcd");
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(client_id_compare_func(&a, &b) == 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(client_id_hash_func(&a, hash_key) == client_id_hash_func(&b, hash_key));
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen a.length = 3;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(client_id_compare_func(&a, &b) != 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen a.length = 4;
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert_se(client_id_compare_func(&a, &b) == 0);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert_se(client_id_hash_func(&a, hash_key) == client_id_hash_func(&b, hash_key));
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen b.length = 3;
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert_se(client_id_compare_func(&a, &b) != 0);
97f2d76d4f4dfab8b0629c09926a05a1e5621125Tom Gundersen b.length = 4;
97f2d76d4f4dfab8b0629c09926a05a1e5621125Tom Gundersen assert_se(client_id_compare_func(&a, &b) == 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(client_id_hash_func(&a, hash_key) == client_id_hash_func(&b, hash_key));
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen free(b.data);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen b.data = (uint8_t*)strdup("abce");
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(client_id_compare_func(&a, &b) != 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen free(a.data);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen free(b.data);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen}
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersenint main(int argc, char *argv[]) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen _cleanup_event_unref_ sd_event *e;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen int r;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen log_set_max_level(LOG_DEBUG);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen log_parse_environment();
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen log_open();
97f2d76d4f4dfab8b0629c09926a05a1e5621125Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert_se(sd_event_new(&e) >= 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen r = test_basic(e);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (r != 0)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return r;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
b3e013148603aa670bc2c060ac63d48e54d76fc2Tom Gundersen test_message_handler();
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen test_client_id_hash();
b3e013148603aa670bc2c060ac63d48e54d76fc2Tom Gundersen
b3e013148603aa670bc2c060ac63d48e54d76fc2Tom Gundersen return 0;
b3e013148603aa670bc2c060ac63d48e54d76fc2Tom Gundersen}
b3e013148603aa670bc2c060ac63d48e54d76fc2Tom Gundersen