test-dhcp-server.c revision 4dc355680460fdc8e0d590d8572dff1b6a257d88
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 <netinet/if_ether.h>
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek#include <assert.h>
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek#include <errno.h>
143bfdaf0b890fa7acadf02d1eafacaef1b696bdHolger Hans Peter Freyther
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek#include "sd-event.h"
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering#include "event-util.h"
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek#include "sd-dhcp-server.h"
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering#include "dhcp-server-internal.h"
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poetteringstatic void test_basic(sd_event *event) {
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering struct in_addr address_lo = {
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering .s_addr = htonl(INADDR_LOOPBACK),
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering };
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering struct in_addr address_any = {
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering .s_addr = htonl(INADDR_ANY),
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering };
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering /* attach to loopback interface */
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering assert_se(sd_dhcp_server_new(&server, 1) >= 0);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering assert_se(server);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_attach_event(server, event, 0) >= 0);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_attach_event(server, event, 0) == -EBUSY);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_get_event(server) == event);
6c03089c32c251d823173bda4d809a9e643219f0Lennart 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
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_ref(server) == server);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(!sd_dhcp_server_unref(server));
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering assert_se(sd_dhcp_server_start(server) == -EUNATCH);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_any) == -EINVAL);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_lo) >= 0);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_lo) == -EBUSY);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_start(server) >= 0);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering assert_se(sd_dhcp_server_start(server) == -EBUSY);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_stop(server) >= 0);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_stop(server) >= 0);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(sd_dhcp_server_start(server) >= 0);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering}
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poetteringstatic 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;
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering uint8_t end;
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering } _packed_ test = {
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering .message.op = BOOTREQUEST,
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering .message.htype = ARPHRD_ETHER,
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering .message.hlen = ETHER_ADDR_LEN,
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering .option_type.code = DHCP_OPTION_MESSAGE_TYPE,
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering .option_type.length = 1,
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering .option_type.type = DHCP_DISCOVER,
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering .end = DHCP_OPTION_END,
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering };
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering struct in_addr address_lo = {
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering .s_addr = htonl(INADDR_LOOPBACK),
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering };
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek assert_se(sd_dhcp_server_new(&server, 1) >= 0);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(sd_dhcp_server_set_address(server, &address_lo) >= 0);
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering assert_se(sd_dhcp_server_attach_event(server, NULL, 0) >= 0);
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering assert_se(sd_dhcp_server_start(server) >= 0);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering test.end = 0;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering /* TODO, shouldn't this fail? */
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering test.end = DHCP_OPTION_END;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering test.option_type.code = 0;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering test.option_type.length = 0;
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering test.option_type.type = 0;
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering test.option_type.code = DHCP_OPTION_MESSAGE_TYPE;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering test.option_type.length = 1;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering test.option_type.type = DHCP_DISCOVER;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering test.message.op = 0;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering test.message.op = BOOTREQUEST;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering test.message.htype = 0;
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering test.message.htype = ARPHRD_ETHER;
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering test.message.hlen = 0;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 0);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering test.message.hlen = ETHER_ADDR_LEN;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == DHCP_OFFER);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering}
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringint main(int argc, char *argv[]) {
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering _cleanup_event_unref_ sd_event *e;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering log_set_max_level(LOG_DEBUG);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering log_parse_environment();
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering log_open();
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering assert_se(sd_event_new(&e) >= 0);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering test_basic(e);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering test_message_handler();
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering return 0;
1021b21bc6f8dd522b46116e8598b17f9f93f1b7Lennart Poettering}
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering