]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/test-sd-dhcp-lease.c
Merge pull request #9017 from keszybz/man-coredump
[thirdparty/systemd.git] / src / libsystemd-network / test-sd-dhcp-lease.c
1 #include <errno.h>
2
3 #include "dhcp-lease-internal.h"
4 #include "macro.h"
5 #include "string-util.h"
6 #include "strv.h"
7
8 /* According to RFC1035 section 4.1.4, a domain name in a message can be either:
9 * - a sequence of labels ending in a zero octet
10 * - a pointer
11 * - a sequence of labels ending with a pointer
12 */
13 static void test_dhcp_lease_parse_search_domains_basic(void) {
14 int r;
15 _cleanup_strv_free_ char **domains = NULL;
16 static const uint8_t optionbuf[] = {
17 0x03, 'F', 'O', 'O', 0x03, 'B', 'A', 'R', 0x00,
18 0x04, 'A', 'B', 'C', 'D', 0x03, 'E', 'F', 'G', 0x00,
19 };
20
21 r = dhcp_lease_parse_search_domains(optionbuf, sizeof(optionbuf), &domains);
22 assert_se(r == 2);
23 assert_se(streq(domains[0], "FOO.BAR"));
24 assert_se(streq(domains[1], "ABCD.EFG"));
25 }
26
27 static void test_dhcp_lease_parse_search_domains_ptr(void) {
28 int r;
29 _cleanup_strv_free_ char **domains = NULL;
30 static const uint8_t optionbuf[] = {
31 0x03, 'F', 'O', 'O', 0x00, 0xC0, 0x00,
32 };
33
34 r = dhcp_lease_parse_search_domains(optionbuf, sizeof(optionbuf), &domains);
35 assert_se(r == 2);
36 assert_se(streq(domains[0], "FOO"));
37 assert_se(streq(domains[1], "FOO"));
38 }
39
40 static void test_dhcp_lease_parse_search_domains_labels_and_ptr(void) {
41 int r;
42 _cleanup_strv_free_ char **domains = NULL;
43 static const uint8_t optionbuf[] = {
44 0x03, 'F', 'O', 'O', 0x03, 'B', 'A', 'R', 0x00,
45 0x03, 'A', 'B', 'C', 0xC0, 0x04,
46 };
47
48 r = dhcp_lease_parse_search_domains(optionbuf, sizeof(optionbuf), &domains);
49 assert_se(r == 2);
50 assert_se(streq(domains[0], "FOO.BAR"));
51 assert_se(streq(domains[1], "ABC.BAR"));
52 }
53
54 /* Tests for exceptions. */
55
56 static void test_dhcp_lease_parse_search_domains_no_data(void) {
57 _cleanup_strv_free_ char **domains = NULL;
58 static const uint8_t optionbuf[3] = {0, 0, 0};
59
60 assert_se(dhcp_lease_parse_search_domains(NULL, 0, &domains) == -ENODATA);
61 assert_se(dhcp_lease_parse_search_domains(optionbuf, 0, &domains) == -ENODATA);
62 }
63
64 static void test_dhcp_lease_parse_search_domains_loops(void) {
65 _cleanup_strv_free_ char **domains = NULL;
66 static const uint8_t optionbuf[] = {
67 0x03, 'F', 'O', 'O', 0x00, 0x03, 'B', 'A', 'R', 0xC0, 0x06,
68 };
69
70 assert_se(dhcp_lease_parse_search_domains(optionbuf, sizeof(optionbuf), &domains) == -EBADMSG);
71 }
72
73 static void test_dhcp_lease_parse_search_domains_wrong_len(void) {
74 _cleanup_strv_free_ char **domains = NULL;
75 static const uint8_t optionbuf[] = {
76 0x03, 'F', 'O', 'O', 0x03, 'B', 'A', 'R', 0x00,
77 0x04, 'A', 'B', 'C', 'D', 0x03, 'E', 'F', 'G', 0x00,
78 };
79
80 assert_se(dhcp_lease_parse_search_domains(optionbuf, sizeof(optionbuf) - 5, &domains) == -EBADMSG);
81 }
82
83 int main(int argc, char *argv[]) {
84 test_dhcp_lease_parse_search_domains_basic();
85 test_dhcp_lease_parse_search_domains_ptr();
86 test_dhcp_lease_parse_search_domains_labels_and_ptr();
87 test_dhcp_lease_parse_search_domains_no_data();
88 test_dhcp_lease_parse_search_domains_loops();
89 test_dhcp_lease_parse_search_domains_wrong_len();
90 }