chiark / gitweb /
dhcp: Add buffer length and invalid cookie tests for DHCP options
[elogind.git] / src / libsystemd-dhcp / test-dhcp-option.c
1
2 #include <stdio.h>
3 #include <stdbool.h>
4 #include <errno.h>
5 #include <string.h>
6 #include <assert.h>
7
8 #include <util.h>
9
10 #include "dhcp-protocol.h"
11 #include "dhcp-internal.h"
12
13 static void test_invalid_buffer_length(void)
14 {
15         DHCPMessage message;
16
17         assert(dhcp_option_parse(&message, 0, NULL, NULL) == -EINVAL);
18         assert(dhcp_option_parse(&message, sizeof(DHCPMessage), NULL, NULL)
19                == -EINVAL);
20 }
21
22 static void test_cookie(void)
23 {
24         DHCPMessage *message;
25         size_t len = sizeof(DHCPMessage) + 4;
26         uint8_t *opt;
27
28         message = malloc0(len);
29
30         opt = (uint8_t *)(message + 1);
31         opt[0] = 0xff;
32
33         assert(dhcp_option_parse(message, len, NULL, NULL) == -EINVAL);
34
35         opt[0] = 99;
36         opt[1] = 130;
37         opt[2] = 83;
38         opt[3] = 99;
39
40         assert(dhcp_option_parse(message, len, NULL, NULL) == -ENOMSG);
41
42         free(message);
43 }
44
45 int main(int argc, char *argv[])
46 {
47         test_invalid_buffer_length();
48         test_cookie();
49
50         return 0;
51 }