summaryrefslogtreecommitdiff
path: root/tacplus-daemon/test/parserTester.cpp
blob: b0a83629770a45ee210354af609013f3c71b7071 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
	Copyright (c) 2018-2019 AT&T Intellectual Property.

	SPDX-License-Identifier: GPL-2.0-only
*/

#include "CppUTest/TestHarness.h"
extern "C" {
    #include <netinet/ip.h>
    #include "parser.h"
}

TEST_GROUP(Parser)
{
    static void check_server_opts(struct tacplus_options::tacplus_options_server *expect_serv,
                                  struct tacplus_options::tacplus_options_server *conf_serv)
    {
        CHECK(conf_serv != expect_serv);

        UNSIGNED_LONGS_EQUAL(expect_serv->id, conf_serv->id);
        CHECK(sockaddr_addr_equal(expect_serv->addrs->ai_addr,
                                  conf_serv->addrs->ai_addr));
        UNSIGNED_LONGS_EQUAL(get_addrinfo_port(expect_serv->addrs),
                             get_addrinfo_port(conf_serv->addrs));

        if (! expect_serv->src_addrs) {
            POINTERS_EQUAL(NULL, conf_serv->src_addrs);
        }
        else {
            CHECK(sockaddr_addr_equal(expect_serv->src_addrs->ai_addr,
                                      conf_serv->src_addrs->ai_addr));
        }

        if (! expect_serv->src_intf) {
            POINTERS_EQUAL(NULL, conf_serv->src_intf);
        }
        else {
            STRCMP_EQUAL(expect_serv->src_intf, conf_serv->src_intf);
        }

        STRCMP_EQUAL(expect_serv->secret, conf_serv->secret);
        UNSIGNED_LONGS_EQUAL(expect_serv->hold_down, conf_serv->hold_down);
        UNSIGNED_LONGS_EQUAL(expect_serv->timeout, conf_serv->timeout);

#ifdef HAVE_LIBTAC_EVENT
        CHECK(! conf_serv->session);
#else
        LONGS_EQUAL(-1, conf_serv->fd);
#endif
    }

    void test_typical(struct tacplus_options *loaded_opts)
    {
        struct tacplus_options::tacplus_options_server *serv1, *serv2;

        struct tacplus_options::tacplus_options_server expect_serv1 = {
            .id = 0,
            .addrs = tacplus_addrinfo("1.1.1.1", "49"),
            .src_addrs = NULL,
            .src_intf = "eth0",
            .timeout = 3,
            .hold_down = 0,
            .secret = "foo",
        };

        struct tacplus_options::tacplus_options_server expect_serv2 = {
            .id = 1,
            .addrs = tacplus_addrinfo("2:2::2:2", "200"),
            .src_addrs = tacplus_addrinfo("1:1::1:1", ""),
            .src_intf = NULL,
            .timeout = 10,
            .hold_down = 12,
            .secret = "bar",
        };

        CHECK(loaded_opts);

        CHECK(!loaded_opts->broadcast);
        UNSIGNED_LONGS_EQUAL(2, loaded_opts->setupTimeout);
        UNSIGNED_LONGS_EQUAL(16<<2, loaded_opts->dscp);

        UNSIGNED_LONGS_EQUAL(2, loaded_opts->n_servers);
        UNSIGNED_LONGS_EQUAL(0, loaded_opts->curr_server);
        UNSIGNED_LONGS_EQUAL(INVALID_SERVER_ID, loaded_opts->next_server);

        serv1 = (struct tacplus_options::tacplus_options_server *) tacplus_server(loaded_opts, 0);
        check_server_opts(&expect_serv1, serv1);

        serv2 = (struct tacplus_options::tacplus_options_server *) tacplus_server(loaded_opts, 1);
        check_server_opts(&expect_serv2, serv2);
    }

    void test_typical_changed_hold_down(struct tacplus_options *loaded_opts)
    {
        struct tacplus_options::tacplus_options_server *serv1, *serv2;

        struct tacplus_options::tacplus_options_server expect_serv1 = {
            .id = 0,
            .addrs = tacplus_addrinfo("1.1.1.1", "49"),
            .src_addrs = NULL,
            .src_intf = "eth0",
            .timeout = 3,
            .hold_down = 10,
            .secret = "foo",
        };

        struct tacplus_options::tacplus_options_server expect_serv2 = {
            .id = 1,
            .addrs = tacplus_addrinfo("2:2::2:2", "200"),
            .src_addrs = tacplus_addrinfo("1:1::1:1", ""),
            .src_intf = NULL,
            .timeout = 10,
            .hold_down = 5,
            .secret = "bar",
        };

        CHECK(loaded_opts);

        CHECK(!loaded_opts->broadcast);
        UNSIGNED_LONGS_EQUAL(2, loaded_opts->setupTimeout);
        UNSIGNED_LONGS_EQUAL(IPTOS_CLASS_CS6, loaded_opts->dscp);

        UNSIGNED_LONGS_EQUAL(2, loaded_opts->n_servers);
        UNSIGNED_LONGS_EQUAL(0, loaded_opts->curr_server);
        UNSIGNED_LONGS_EQUAL(INVALID_SERVER_ID, loaded_opts->next_server);

        serv1 = (struct tacplus_options::tacplus_options_server *) tacplus_server(loaded_opts, 0);
        check_server_opts(&expect_serv1, serv1);

        serv2 = (struct tacplus_options::tacplus_options_server *) tacplus_server(loaded_opts, 1);
        check_server_opts(&expect_serv2, serv2);
    }
};

TEST(Parser, nonExistentFile)
{
    struct tacplus_options *opts = tacplus_parse_options("config/UNKNOWN");
    POINTERS_EQUAL(NULL, opts);
}

TEST(Parser, typical)
{
    struct tacplus_options *opts = tacplus_parse_options("config/typical");
    test_typical(opts);

    cleanup_tacplus_options(&opts);
    POINTERS_EQUAL(NULL, opts);
}

TEST(Parser, typicalChangedHoldDown)
{
    struct tacplus_options *opts = tacplus_parse_options("config/typical-changed-hold-down");
    test_typical_changed_hold_down(opts);

    cleanup_tacplus_options(&opts);
    POINTERS_EQUAL(NULL, opts);
}

TEST(Parser, reloadTypicalChangedHoldDown)
{
    struct tacplus_options *opts = tacplus_parse_options("config/typical");
    test_typical(opts);

    struct tacplus_options *new_opts = tacplus_parse_reload_options(
                                            "config/typical-changed-hold-down", &opts);
    POINTERS_EQUAL(NULL, opts);
    test_typical_changed_hold_down(new_opts);

    cleanup_tacplus_options(&new_opts);
    POINTERS_EQUAL(NULL, new_opts);
}

TEST(Parser, typicalBroadcast)
{
    struct tacplus_options::tacplus_options_server *serv1, *serv2;

    struct tacplus_options::tacplus_options_server expect_serv1 = {
        .id = 0,
        .addrs = tacplus_addrinfo("1.1.1.2", "149"),
        .src_addrs = tacplus_addrinfo("2.2.2.2", ""),
        .src_intf = NULL,
        .timeout = 3,
        .hold_down = 0,
        .secret = "foobar",
    };

    struct tacplus_options::tacplus_options_server expect_serv2 = {
        .id = 1,
        .addrs = tacplus_addrinfo("2:2::2:2", "85"),
        .src_addrs = NULL,
        .src_intf = "eth3",
        .timeout = 15,
        .hold_down = 3600,
        .secret = "bar",
    };

    struct tacplus_options *opts = tacplus_parse_options("config/typical-broadcast");
    CHECK(opts);

#ifdef HAVE_LIBTAC_EVENT
    CHECK(opts->broadcast);
#else
    CHECK(!opts->broadcast);
#endif

    UNSIGNED_LONGS_EQUAL(4, opts->setupTimeout);
    UNSIGNED_LONGS_EQUAL(IPTOS_CLASS_CS6, opts->dscp);

    UNSIGNED_LONGS_EQUAL(2, opts->n_servers);
    UNSIGNED_LONGS_EQUAL(0, opts->curr_server);
    UNSIGNED_LONGS_EQUAL(INVALID_SERVER_ID, opts->next_server);

    serv1 = (struct tacplus_options::tacplus_options_server *) tacplus_server(opts, 0);
    check_server_opts(&expect_serv1, serv1);

    serv2 = (struct tacplus_options::tacplus_options_server *) tacplus_server(opts, 1);
    check_server_opts(&expect_serv2, serv2);

    cleanup_tacplus_options(&opts);
    POINTERS_EQUAL(NULL, opts);
}