]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/test-sd-dhcp-lease.c
3 #include "dhcp-lease-internal.h"
5 #include "string-util.h"
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
11 * - a sequence of labels ending with a pointer
13 static void test_dhcp_lease_parse_search_domains_basic(void) {
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,
21 r
= dhcp_lease_parse_search_domains(optionbuf
, sizeof(optionbuf
), &domains
);
23 assert_se(streq(domains
[0], "FOO.BAR"));
24 assert_se(streq(domains
[1], "ABCD.EFG"));
27 static void test_dhcp_lease_parse_search_domains_ptr(void) {
29 _cleanup_strv_free_
char **domains
= NULL
;
30 static const uint8_t optionbuf
[] = {
31 0x03, 'F', 'O', 'O', 0x00, 0xC0, 0x00,
34 r
= dhcp_lease_parse_search_domains(optionbuf
, sizeof(optionbuf
), &domains
);
36 assert_se(streq(domains
[0], "FOO"));
37 assert_se(streq(domains
[1], "FOO"));
40 static void test_dhcp_lease_parse_search_domains_labels_and_ptr(void) {
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,
48 r
= dhcp_lease_parse_search_domains(optionbuf
, sizeof(optionbuf
), &domains
);
50 assert_se(streq(domains
[0], "FOO.BAR"));
51 assert_se(streq(domains
[1], "ABC.BAR"));
54 /* Tests for exceptions. */
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};
60 assert_se(dhcp_lease_parse_search_domains(NULL
, 0, &domains
) == -ENODATA
);
61 assert_se(dhcp_lease_parse_search_domains(optionbuf
, 0, &domains
) == -ENODATA
);
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,
70 assert_se(dhcp_lease_parse_search_domains(optionbuf
, sizeof(optionbuf
), &domains
) == -EBADMSG
);
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,
80 assert_se(dhcp_lease_parse_search_domains(optionbuf
, sizeof(optionbuf
) - 5, &domains
) == -EBADMSG
);
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();