test-dhcp-server.c revision 99634696183dfabae20104e58157c69029a11594
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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) {
99634696183dfabae20104e58157c69029a11594Tom Gundersen _cleanup_dhcp_server_unref_ 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);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen _cleanup_dhcp_server_unref_ 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);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersenstatic void test_message_handler(void) {
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen .message.chaddr = { 'A', 'B', 'C', 'D', 'E', 'F' },
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);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen test.option_type.code = 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);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen test.option_requested_ip.code = 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);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen test.option_server_id.code = 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);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen test.option_client_id.code = 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);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersenstatic void test_client_id_hash(void) {
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen assert_se(client_id_compare_func(&a, &b) == 0);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen assert_se(client_id_hash_func(&a, hash_key) == client_id_hash_func(&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);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen assert_se(client_id_hash_func(&a, hash_key) == client_id_hash_func(&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);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen assert_se(client_id_hash_func(&a, hash_key) == client_id_hash_func(&b, hash_key));