b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen This file is part of systemd.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen Copyright (C) 2013 Intel Corporation. All rights reserved.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen Copyright (C) 2014 Tom 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 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 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/>.
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 assert_se(sd_dhcp_server_new(&server, 1) >= 0);
99634696183dfabae20104e58157c69029a11594Tom Gundersen assert_se(sd_dhcp_server_configure_pool(server, address, 8, 0, size) == ret);
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_dhcp_server_unrefp) sd_dhcp_server *server = NULL;
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen /* attach to loopback interface */
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen assert_se(sd_dhcp_server_new(&server, 1) >= 0);
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_attach_event(server, NULL, 0) >= 0);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert_se(sd_dhcp_server_attach_event(server, NULL, 0) == -EBUSY);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert_se(sd_dhcp_server_ref(server) == server);
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen assert_se(sd_dhcp_server_start(server) == -EUNATCH);
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);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen assert_se(sd_dhcp_server_start(server) == -EBUSY);
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_dhcp_server_unrefp) sd_dhcp_server *server = NULL;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen .message.chaddr = { 'A', 'B', 'C', 'D', 'E', 'F' },
22805d9207d5242681e5667ee304572e4abf9b94Beniamino Galvani .option_type.code = SD_DHCP_OPTION_MESSAGE_TYPE,
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);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen /* TODO, shouldn't this fail? */
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
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;
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
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.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.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);
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);
22805d9207d5242681e5667ee304572e4abf9b94Beniamino Galvani test.option_client_id.code = SD_DHCP_OPTION_CLIENT_IDENTIFIER;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_ACK);
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);
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersenstatic uint64_t client_id_hash_helper(DHCPClientId *id, uint8_t key[HASH_KEY_SIZE]) {
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 assert_se(client_id_compare_func(&a, &b) != 0);
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 assert_se(client_id_compare_func(&a, &b) != 0);
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));