b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen/***
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen This file is part of systemd.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen Copyright (C) 2013 Intel Corporation. All rights reserved.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen Copyright (C) 2014 Tom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen systemd is free software; you can redistribute it and/or modify it
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen under the terms of the GNU Lesser General Public License as published by
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen the Free Software Foundation; either version 2.1 of the License, or
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen (at your option) any later version.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen systemd is distributed in the hope that it will be useful, but
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen Lesser General Public License for more details.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen You should have received a copy of the GNU Lesser General Public License
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen***/
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen#include <errno.h>
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "sd-dhcp-server.h"
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen#include "sd-event.h"
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen#include "dhcp-server-internal.h"
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
99634696183dfabae20104e58157c69029a11594Tom Gundersenstatic void test_pool(struct in_addr *address, unsigned size, int ret) {
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_dhcp_server_unrefp) sd_dhcp_server *server = NULL;
99634696183dfabae20104e58157c69029a11594Tom Gundersen
99634696183dfabae20104e58157c69029a11594Tom Gundersen assert_se(sd_dhcp_server_new(&server, 1) >= 0);
99634696183dfabae20104e58157c69029a11594Tom Gundersen
99634696183dfabae20104e58157c69029a11594Tom Gundersen assert_se(sd_dhcp_server_configure_pool(server, address, 8, 0, size) == ret);
99634696183dfabae20104e58157c69029a11594Tom Gundersen}
99634696183dfabae20104e58157c69029a11594Tom Gundersen
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersenstatic int test_basic(sd_event *event) {
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_dhcp_server_unrefp) sd_dhcp_server *server = NULL;
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen struct in_addr address_lo = {
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen .s_addr = htonl(INADDR_LOOPBACK),
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen };
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen struct in_addr address_any = {
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen .s_addr = htonl(INADDR_ANY),
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen };
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen int r;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen /* attach to loopback interface */
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen assert_se(sd_dhcp_server_new(&server, 1) >= 0);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert_se(server);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert_se(sd_dhcp_server_attach_event(server, event, 0) >= 0);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert_se(sd_dhcp_server_attach_event(server, event, 0) == -EBUSY);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert_se(sd_dhcp_server_get_event(server) == event);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert_se(sd_dhcp_server_detach_event(server) >= 0);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert_se(!sd_dhcp_server_get_event(server));
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert_se(sd_dhcp_server_attach_event(server, NULL, 0) >= 0);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert_se(sd_dhcp_server_attach_event(server, NULL, 0) == -EBUSY);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert_se(sd_dhcp_server_ref(server) == server);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert_se(!sd_dhcp_server_unref(server));
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen assert_se(sd_dhcp_server_start(server) == -EUNATCH);
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen
99634696183dfabae20104e58157c69029a11594Tom Gundersen assert_se(sd_dhcp_server_configure_pool(server, &address_any, 28, 0, 0) == -EINVAL);
99634696183dfabae20104e58157c69029a11594Tom Gundersen assert_se(sd_dhcp_server_configure_pool(server, &address_lo, 38, 0, 0) == -ERANGE);
99634696183dfabae20104e58157c69029a11594Tom Gundersen assert_se(sd_dhcp_server_configure_pool(server, &address_lo, 8, 0, 0) >= 0);
99634696183dfabae20104e58157c69029a11594Tom Gundersen assert_se(sd_dhcp_server_configure_pool(server, &address_lo, 8, 0, 0) == -EBUSY);
99634696183dfabae20104e58157c69029a11594Tom Gundersen
99634696183dfabae20104e58157c69029a11594Tom Gundersen test_pool(&address_any, 1, -EINVAL);
99634696183dfabae20104e58157c69029a11594Tom Gundersen test_pool(&address_lo, 1, 0);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen r = sd_dhcp_server_start(server);
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen if (r == -EPERM)
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen return EXIT_TEST_SKIP;
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen assert_se(r >= 0);
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen assert_se(sd_dhcp_server_start(server) == -EBUSY);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen assert_se(sd_dhcp_server_stop(server) >= 0);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen assert_se(sd_dhcp_server_stop(server) >= 0);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen assert_se(sd_dhcp_server_start(server) >= 0);
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen return 0;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen}
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
be077570f779664ed87b50f60608df9fbe258821Tom Gundersenstatic void test_message_handler(void) {
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_dhcp_server_unrefp) sd_dhcp_server *server = NULL;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen struct {
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen DHCPMessage message;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen struct {
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen uint8_t code;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen uint8_t length;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen uint8_t type;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen } _packed_ option_type;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen struct {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen uint8_t code;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen uint8_t length;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen be32_t address;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen } _packed_ option_requested_ip;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen struct {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen uint8_t code;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen uint8_t length;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen be32_t address;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen } _packed_ option_server_id;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen struct {
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen uint8_t code;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen uint8_t length;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen uint8_t id[7];
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen } _packed_ option_client_id;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen uint8_t end;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen } _packed_ test = {
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen .message.op = BOOTREQUEST,
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen .message.htype = ARPHRD_ETHER,
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen .message.hlen = ETHER_ADDR_LEN,
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen .message.xid = htobe32(0x12345678),
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen .message.chaddr = { 'A', 'B', 'C', 'D', 'E', 'F' },
22805d9207d5242681e5667ee304572e4abf9b94Beniamino Galvani .option_type.code = SD_DHCP_OPTION_MESSAGE_TYPE,
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen .option_type.length = 1,
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen .option_type.type = DHCP_DISCOVER,
22805d9207d5242681e5667ee304572e4abf9b94Beniamino Galvani .end = SD_DHCP_OPTION_END,
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen };
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen struct in_addr address_lo = {
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen .s_addr = htonl(INADDR_LOOPBACK),
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen };
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen assert_se(sd_dhcp_server_new(&server, 1) >= 0);
99634696183dfabae20104e58157c69029a11594Tom Gundersen assert_se(sd_dhcp_server_configure_pool(server, &address_lo, 8, 0, 0) >= 0);
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen assert_se(sd_dhcp_server_attach_event(server, NULL, 0) >= 0);
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen assert_se(sd_dhcp_server_start(server) >= 0);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen test.end = 0;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen /* TODO, shouldn't this fail? */
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
22805d9207d5242681e5667ee304572e4abf9b94Beniamino Galvani test.end = SD_DHCP_OPTION_END;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen test.option_type.code = 0;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen test.option_type.length = 0;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen test.option_type.type = 0;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
22805d9207d5242681e5667ee304572e4abf9b94Beniamino Galvani test.option_type.code = SD_DHCP_OPTION_MESSAGE_TYPE;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen test.option_type.length = 1;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen test.option_type.type = DHCP_DISCOVER;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen test.message.op = 0;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen test.message.op = BOOTREQUEST;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen test.message.htype = 0;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen test.message.htype = ARPHRD_ETHER;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen test.message.hlen = 0;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen test.message.hlen = ETHER_ADDR_LEN;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen test.option_type.type = DHCP_REQUEST;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
22805d9207d5242681e5667ee304572e4abf9b94Beniamino Galvani test.option_requested_ip.code = SD_DHCP_OPTION_REQUESTED_IP_ADDRESS;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen test.option_requested_ip.length = 4;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen test.option_requested_ip.address = htobe32(0x12345678);
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_NAK);
22805d9207d5242681e5667ee304572e4abf9b94Beniamino Galvani test.option_server_id.code = SD_DHCP_OPTION_SERVER_IDENTIFIER;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen test.option_server_id.length = 4;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen test.option_server_id.address = htobe32(INADDR_LOOPBACK);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen test.option_server_id.address = htobe32(0x12345678);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen test.option_server_id.address = htobe32(INADDR_LOOPBACK);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 4);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
22805d9207d5242681e5667ee304572e4abf9b94Beniamino Galvani test.option_client_id.code = SD_DHCP_OPTION_CLIENT_IDENTIFIER;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen test.option_client_id.length = 7;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen test.option_client_id.id[0] = 0x01;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen test.option_client_id.id[1] = 'A';
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen test.option_client_id.id[2] = 'B';
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen test.option_client_id.id[3] = 'C';
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen test.option_client_id.id[4] = 'D';
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen test.option_client_id.id[5] = 'E';
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen test.option_client_id.id[6] = 'F';
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 30);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen}
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersenstatic uint64_t client_id_hash_helper(DHCPClientId *id, uint8_t key[HASH_KEY_SIZE]) {
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen struct siphash state;
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen
0cb3c286883b694fc52a18a3b559ff98931641f3Tom Gundersen siphash24_init(&state, key);
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen client_id_hash_func(id, &state);
0cb3c286883b694fc52a18a3b559ff98931641f3Tom Gundersen
933f9caeeb2b3c1b951d330e04beb04226e5a890Daniel Mack return htole64(siphash24_finalize(&state));
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen}
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersenstatic void test_client_id_hash(void) {
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen DHCPClientId a = {
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen .length = 4,
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen }, b = {
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen .length = 4,
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen };
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen uint8_t hash_key[HASH_KEY_SIZE] = {
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen '0', '1', '2', '3', '4', '5', '6', '7',
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen };
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen a.data = (uint8_t*)strdup("abcd");
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen b.data = (uint8_t*)strdup("abcd");
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen assert_se(client_id_compare_func(&a, &b) == 0);
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen assert_se(client_id_hash_helper(&a, hash_key) == client_id_hash_helper(&b, hash_key));
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen a.length = 3;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen assert_se(client_id_compare_func(&a, &b) != 0);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen a.length = 4;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen assert_se(client_id_compare_func(&a, &b) == 0);
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen assert_se(client_id_hash_helper(&a, hash_key) == client_id_hash_helper(&b, hash_key));
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen b.length = 3;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen assert_se(client_id_compare_func(&a, &b) != 0);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen b.length = 4;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen assert_se(client_id_compare_func(&a, &b) == 0);
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen assert_se(client_id_hash_helper(&a, hash_key) == client_id_hash_helper(&b, hash_key));
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen free(b.data);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen b.data = (uint8_t*)strdup("abce");
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen assert_se(client_id_compare_func(&a, &b) != 0);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen free(a.data);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen free(b.data);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen}
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersenint main(int argc, char *argv[]) {
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_event_unrefp) sd_event *e;
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen int r;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen log_set_max_level(LOG_DEBUG);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen log_parse_environment();
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen log_open();
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert_se(sd_event_new(&e) >= 0);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen r = test_basic(e);
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen if (r != 0)
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen return r;
6f08fb7b34b4eebf664de83c8bc7b9941658aeb4Tom Gundersen
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen test_message_handler();
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen test_client_id_hash();
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen return 0;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen}