test-dhcp-server.c revision b6e676ce41508e2aeea22202fc8f234126177f52
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering/***
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering This file is part of systemd.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering Copyright (C) 2013 Intel Corporation. All rights reserved.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering Copyright (C) 2014 Tom Gundersen
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering systemd is free software; you can redistribute it and/or modify it
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering under the terms of the GNU Lesser General Public License as published by
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering (at your option) any later version.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering systemd is distributed in the hope that it will be useful, but
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering Lesser General Public License for more details.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering You should have received a copy of the GNU Lesser General Public License
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering***/
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek#include <errno.h>
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek
143bfdaf0b890fa7acadf02d1eafacaef1b696bdHolger Hans Peter Freyther#include "sd-event.h"
6482f6269c87d2249e52e889a63adbdd50f2d691Ronny Chevalier#include "event-util.h"
0b452006de98294d1690f045f6ea2f7f6630ec3bRonny Chevalier
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek#include "sd-dhcp-server.h"
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering#include "dhcp-server-internal.h"
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmekstatic int test_basic(sd_event *event) {
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek struct in_addr address_lo = {
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek .s_addr = htonl(INADDR_LOOPBACK),
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek };
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering struct in_addr address_any = {
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering .s_addr = htonl(INADDR_ANY),
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek };
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering int r;
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering /* attach to loopback interface */
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering assert_se(sd_dhcp_server_new(&server, 1) >= 0);
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering assert_se(server);
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering assert_se(sd_dhcp_server_attach_event(server, event, 0) >= 0);
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering assert_se(sd_dhcp_server_attach_event(server, event, 0) == -EBUSY);
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering assert_se(sd_dhcp_server_get_event(server) == event);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering assert_se(sd_dhcp_server_detach_event(server) >= 0);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(!sd_dhcp_server_get_event(server));
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_attach_event(server, NULL, 0) >= 0);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_attach_event(server, NULL, 0) == -EBUSY);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_ref(server) == server);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(!sd_dhcp_server_unref(server));
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_start(server) == -EUNATCH);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_set_address(server, &address_any, 28) == -EINVAL);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_lo, 38) == -ERANGE);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_lo, 8) >= 0);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_lo, 8) == -EBUSY);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_set_lease_pool(server, &address_any, 1) == -EINVAL);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 0) == -EINVAL);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 1) >= 0);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 1) == -EBUSY);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering r = sd_dhcp_server_start(server);
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering if (r == -EPERM)
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering return EXIT_TEST_SKIP;
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek assert_se(r >= 0);
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_start(server) == -EBUSY);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_stop(server) >= 0);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_stop(server) >= 0);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_start(server) >= 0);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering return 0;
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek}
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmekstatic void test_message_handler(void) {
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering struct {
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering DHCPMessage message;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering struct {
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering uint8_t code;
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering uint8_t length;
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering uint8_t type;
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering } _packed_ option_type;
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering struct {
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering uint8_t code;
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering uint8_t length;
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering be32_t address;
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering } _packed_ option_requested_ip;
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering struct {
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek uint8_t code;
320814811417146cfa1e416f69f1101eed630c36Luke Shumaker uint8_t length;
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering be32_t address;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering } _packed_ option_server_id;
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering struct {
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering uint8_t code;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering uint8_t length;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering uint8_t id[7];
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering } _packed_ option_client_id;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering uint8_t end;
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek } _packed_ test = {
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek .message.op = BOOTREQUEST,
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering .message.htype = ARPHRD_ETHER,
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering .message.hlen = ETHER_ADDR_LEN,
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering .message.xid = htobe32(0x12345678),
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering .message.chaddr = { 'A', 'B', 'C', 'D', 'E', 'F' },
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering .option_type.code = DHCP_OPTION_MESSAGE_TYPE,
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering .option_type.length = 1,
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering .option_type.type = DHCP_DISCOVER,
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering .end = DHCP_OPTION_END,
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering };
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering struct in_addr address_lo = {
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering .s_addr = htonl(INADDR_LOOPBACK),
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering };
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_new(&server, 1) >= 0);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_lo, 8) >= 0);
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering assert_se(sd_dhcp_server_attach_event(server, NULL, 0) >= 0);
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering assert_se(sd_dhcp_server_start(server) >= 0);
cfeaa44a09756a93a881f786678973d9b1e382dbLennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 10) >= 0);
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering test.end = 0;
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering /* TODO, shouldn't this fail? */
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering test.end = DHCP_OPTION_END;
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering test.option_type.code = 0;
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering test.option_type.length = 0;
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering test.option_type.type = 0;
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering test.option_type.code = DHCP_OPTION_MESSAGE_TYPE;
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering test.option_type.length = 1;
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering test.option_type.type = DHCP_DISCOVER;
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
8b0849e9710d721c5d0b775aaf0fd662eefa1449Lennart Poettering
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering test.message.op = 0;
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering test.message.op = BOOTREQUEST;
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering test.message.htype = 0;
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering test.message.htype = ARPHRD_ETHER;
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering test.message.hlen = 0;
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering test.message.hlen = ETHER_ADDR_LEN;
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering test.option_type.type = DHCP_REQUEST;
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering test.option_requested_ip.code = DHCP_OPTION_REQUESTED_IP_ADDRESS;
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering test.option_requested_ip.length = 4;
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering test.option_requested_ip.address = htobe32(0x12345678);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_NAK);
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering test.option_server_id.code = DHCP_OPTION_SERVER_IDENTIFIER;
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering test.option_server_id.length = 4;
329ac4bc5429cd86c4ac76b13e7e2784f3982760Lennart Poettering test.option_server_id.address = htobe32(INADDR_LOOPBACK);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering test.option_server_id.address = htobe32(0x12345678);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering test.option_server_id.address = htobe32(INADDR_LOOPBACK);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 4);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering test.option_client_id.code = DHCP_OPTION_CLIENT_IDENTIFIER;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering test.option_client_id.length = 7;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering test.option_client_id.id[0] = 0x01;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering test.option_client_id.id[1] = 'A';
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering test.option_client_id.id[2] = 'B';
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering test.option_client_id.id[3] = 'C';
fed1e721fd0c81e60c77120539f34e16c2585634Lennart Poettering test.option_client_id.id[4] = 'D';
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering test.option_client_id.id[5] = 'E';
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering test.option_client_id.id[6] = 'F';
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 30);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering}
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poetteringstatic void test_client_id_hash(void) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering DHCPClientId a = {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering .length = 4,
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering }, b = {
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering .length = 4,
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering };
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering uint8_t hash_key[HASH_KEY_SIZE] = {
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering '0', '1', '2', '3', '4', '5', '6', '7',
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering };
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
1021b21bc6f8dd522b46116e8598b17f9f93f1b7Lennart Poettering a.data = (uint8_t*)strdup("abcd");
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering b.data = (uint8_t*)strdup("abcd");
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek assert_se(client_id_compare_func(&a, &b) == 0);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering assert_se(client_id_hash_func(&a, hash_key) == client_id_hash_func(&b, hash_key));
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering a.length = 3;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek assert_se(client_id_compare_func(&a, &b) != 0);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering a.length = 4;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering assert_se(client_id_compare_func(&a, &b) == 0);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering assert_se(client_id_hash_func(&a, hash_key) == client_id_hash_func(&b, hash_key));
1021b21bc6f8dd522b46116e8598b17f9f93f1b7Lennart Poettering
1021b21bc6f8dd522b46116e8598b17f9f93f1b7Lennart Poettering b.length = 3;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering assert_se(client_id_compare_func(&a, &b) != 0);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering b.length = 4;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering assert_se(client_id_compare_func(&a, &b) == 0);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering assert_se(client_id_hash_func(&a, hash_key) == client_id_hash_func(&b, hash_key));
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering free(b.data);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering b.data = (uint8_t*)strdup("abce");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering assert_se(client_id_compare_func(&a, &b) != 0);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering free(a.data);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering free(b.data);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering}
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poetteringint main(int argc, char *argv[]) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering _cleanup_event_unref_ sd_event *e;
a0ab566574303be1ca12cdb334f284cfd407caa5Lennart Poettering int r;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_set_max_level(LOG_DEBUG);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_parse_environment();
e13bb5d2b133f9ae51c0a2d20aa51071c780e9aeKay Sievers log_open();
e13bb5d2b133f9ae51c0a2d20aa51071c780e9aeKay Sievers
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering assert_se(sd_event_new(&e) >= 0);
a0ab566574303be1ca12cdb334f284cfd407caa5Lennart Poettering
a0ab566574303be1ca12cdb334f284cfd407caa5Lennart Poettering r = test_basic(e);
a0ab566574303be1ca12cdb334f284cfd407caa5Lennart Poettering if (r != 0)
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering return r;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering test_message_handler();
185a08745957cbd32e8293daf8c51ab9c995a71eDimitri John Ledkov test_client_id_hash();
185a08745957cbd32e8293daf8c51ab9c995a71eDimitri John Ledkov
185a08745957cbd32e8293daf8c51ab9c995a71eDimitri John Ledkov return 0;
185a08745957cbd32e8293daf8c51ab9c995a71eDimitri John Ledkov}
185a08745957cbd32e8293daf8c51ab9c995a71eDimitri John Ledkov