chiark / gitweb /
d097c7d43c5073590eaddb11148de7186445bfb9
[elogind.git] / src / libsystemd-dhcp / test-dhcp-client.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright (C) 2013 Intel Corporation. All rights reserved.
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdlib.h>
23 #include <assert.h>
24 #include <errno.h>
25 #include <stdio.h>
26
27 #include "dhcp-protocol.h"
28 #include "sd-dhcp-client.h"
29
30 static void test_request_basic(void)
31 {
32         sd_dhcp_client *client;
33
34         client = sd_dhcp_client_new();
35
36         assert(client);
37
38         assert(sd_dhcp_client_set_request_option(NULL, 0) == -EINVAL);
39         assert(sd_dhcp_client_set_request_address(NULL, NULL) == -EINVAL);
40         assert(sd_dhcp_client_set_index(NULL, 0) == -EINVAL);
41
42         assert(sd_dhcp_client_set_index(client, 15) == 0);
43         assert(sd_dhcp_client_set_index(client, -42) == -EINVAL);
44         assert(sd_dhcp_client_set_index(client, -1) == 0);
45
46         assert(sd_dhcp_client_set_request_option(client,
47                                         DHCP_OPTION_SUBNET_MASK) == -EEXIST);
48         assert(sd_dhcp_client_set_request_option(client,
49                                         DHCP_OPTION_ROUTER) == -EEXIST);
50         assert(sd_dhcp_client_set_request_option(client,
51                                         DHCP_OPTION_HOST_NAME) == -EEXIST);
52         assert(sd_dhcp_client_set_request_option(client,
53                                         DHCP_OPTION_DOMAIN_NAME) == -EEXIST);
54         assert(sd_dhcp_client_set_request_option(client,
55                                         DHCP_OPTION_DOMAIN_NAME_SERVER)
56                         == -EEXIST);
57         assert(sd_dhcp_client_set_request_option(client,
58                                         DHCP_OPTION_NTP_SERVER) == -EEXIST);
59
60         assert(sd_dhcp_client_set_request_option(client,
61                                         DHCP_OPTION_PAD) == -EINVAL);
62         assert(sd_dhcp_client_set_request_option(client,
63                                         DHCP_OPTION_END) == -EINVAL);
64         assert(sd_dhcp_client_set_request_option(client,
65                                         DHCP_OPTION_MESSAGE_TYPE) == -EINVAL);
66         assert(sd_dhcp_client_set_request_option(client,
67                                         DHCP_OPTION_OVERLOAD) == -EINVAL);
68         assert(sd_dhcp_client_set_request_option(client,
69                                         DHCP_OPTION_PARAMETER_REQUEST_LIST)
70                         == -EINVAL);
71
72         assert(sd_dhcp_client_set_request_option(client, 33) == 0);
73         assert(sd_dhcp_client_set_request_option(client, 33) == -EEXIST);
74         assert(sd_dhcp_client_set_request_option(client, 44) == 0);
75 }
76
77 static uint16_t client_checksum(void *buf, int len)
78 {
79         uint32_t sum;
80         uint16_t *check;
81         int i;
82         uint8_t *odd;
83
84         sum = 0;
85         check = buf;
86
87         for (i = 0; i < len / 2 ; i++)
88                 sum += check[i];
89
90         if (len & 0x01) {
91                 odd = buf;
92                 sum += odd[len];
93         }
94
95         return ~((sum & 0xffff) + (sum >> 16));
96 }
97
98 static void test_checksum(void)
99 {
100         uint8_t buf[20] = {
101                 0x45, 0x00, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
102                 0x40, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
103                 0xff, 0xff, 0xff, 0xff
104         };
105
106         uint8_t check[2] = {
107                 0x78, 0xae
108         };
109
110         uint16_t *val = (uint16_t *)check;
111
112         assert(client_checksum(&buf, 20) == *val);
113 }
114
115 int main(int argc, char *argv[])
116 {
117         test_request_basic();
118         test_checksum();
119
120         return 0;
121 }