summaryrefslogtreecommitdiffstats
path: root/src/libsystemd-network/test-sd-dhcp-lease.c
blob: 9f13226889961a96659498ac9c460ff11a2628b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <errno.h>

#include "dhcp-lease-internal.h"
#include "macro.h"
#include "string-util.h"
#include "strv.h"

/* According to RFC1035 section 4.1.4, a domain name in a message can be either:
 *      - a sequence of labels ending in a zero octet
 *      - a pointer
 *      - a sequence of labels ending with a pointer
 */
static void test_dhcp_lease_parse_search_domains_basic(void) {
        int r;
        _cleanup_strv_free_ char **domains = NULL;
        static const uint8_t optionbuf[] = {
                0x03, 'F', 'O', 'O', 0x03, 'B', 'A', 'R', 0x00,
                0x04, 'A', 'B', 'C', 'D', 0x03, 'E', 'F', 'G', 0x00,
        };

        r = dhcp_lease_parse_search_domains(optionbuf, sizeof(optionbuf), &domains);
        assert_se(r == 2);
        assert_se(streq(domains[0], "FOO.BAR"));
        assert_se(streq(domains[1], "ABCD.EFG"));
}

static void test_dhcp_lease_parse_search_domains_ptr(void) {
        int r;
        _cleanup_strv_free_ char **domains = NULL;
        static const uint8_t optionbuf[] = {
                0x03, 'F', 'O', 'O', 0x00, 0xC0, 0x00,
        };

        r = dhcp_lease_parse_search_domains(optionbuf, sizeof(optionbuf), &domains);
        assert_se(r == 2);
        assert_se(streq(domains[0], "FOO"));
        assert_se(streq(domains[1], "FOO"));
}

static void test_dhcp_lease_parse_search_domains_labels_and_ptr(void) {
        int r;
        _cleanup_strv_free_ char **domains = NULL;
        static const uint8_t optionbuf[] = {
                0x03, 'F', 'O', 'O', 0x03, 'B', 'A', 'R', 0x00,
                0x03, 'A', 'B', 'C', 0xC0, 0x04,
        };

        r = dhcp_lease_parse_search_domains(optionbuf, sizeof(optionbuf), &domains);
        assert_se(r == 2);
        assert_se(streq(domains[0], "FOO.BAR"));
        assert_se(streq(domains[1], "ABC.BAR"));
}

/* Tests for exceptions. */

static void test_dhcp_lease_parse_search_domains_no_data(void) {
        _cleanup_strv_free_ char **domains = NULL;
        static const uint8_t optionbuf[3] = {0, 0, 0};

        assert_se(dhcp_lease_parse_search_domains(NULL, 0, &domains) == -ENODATA);
        assert_se(dhcp_lease_parse_search_domains(optionbuf, 0, &domains) == -ENODATA);
}

static void test_dhcp_lease_parse_search_domains_loops(void) {
        _cleanup_strv_free_ char **domains = NULL;
        static const uint8_t optionbuf[] = {
                0x03, 'F', 'O', 'O', 0x00, 0x03, 'B', 'A', 'R', 0xC0, 0x06,
        };

        assert_se(dhcp_lease_parse_search_domains(optionbuf, sizeof(optionbuf), &domains) == -EBADMSG);
}

static void test_dhcp_lease_parse_search_domains_wrong_len(void) {
        _cleanup_strv_free_ char **domains = NULL;
        static const uint8_t optionbuf[] = {
                0x03, 'F', 'O', 'O', 0x03, 'B', 'A', 'R', 0x00,
                0x04, 'A', 'B', 'C', 'D', 0x03, 'E', 'F', 'G', 0x00,
        };

        assert_se(dhcp_lease_parse_search_domains(optionbuf, sizeof(optionbuf) - 5, &domains) == -EBADMSG);
}

int main(int argc, char *argv[]) {
        test_dhcp_lease_parse_search_domains_basic();
        test_dhcp_lease_parse_search_domains_ptr();
        test_dhcp_lease_parse_search_domains_labels_and_ptr();
        test_dhcp_lease_parse_search_domains_no_data();
        test_dhcp_lease_parse_search_domains_loops();
        test_dhcp_lease_parse_search_domains_wrong_len();
        return 0;
}