resolver_config_unittest.cc 7.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

15
16
17
18
19
20
21
#include <string>

#include <gtest/gtest.h>

#include <cc/data.h>

#include <asiolink/asiolink.h>
22

23
#include <resolver/resolver.h>
24
25
26
27
28
29
30
31
32

#include <dns/tests/unittest_util.h>
#include <testutils/srv_test.h>

using namespace std;
using namespace isc::data;
using namespace isc::testutils;
using namespace asiolink;
using isc::UnitTestUtil;
33
34

namespace {
35
class ResolverConfig : public ::testing::Test {
36
37
38
    public:
        IOService ios;
        DNSService dnss;
39
40
        Resolver server;
        ResolverConfig() :
41
42
43
44
45
46
47
            dnss(ios, NULL, NULL, NULL)
        {
            server.setDNSService(dnss);
        }
        void invalidTest(const string &JOSN);
};

48
TEST_F(ResolverConfig, forwardAddresses) {
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    // Default value should be fully recursive
    EXPECT_TRUE(server.getForwardAddresses().empty());
    EXPECT_FALSE(server.isForwarding());

    // Try putting there some addresses
    vector<pair<string, uint16_t> > addresses;
    addresses.push_back(pair<string, uint16_t>(DEFAULT_REMOTE_ADDRESS, 53));
    addresses.push_back(pair<string, uint16_t>("::1", 53));
    server.setForwardAddresses(addresses);
    EXPECT_EQ(2, server.getForwardAddresses().size());
    EXPECT_EQ("::1", server.getForwardAddresses()[1].first);
    EXPECT_TRUE(server.isForwarding());

    // Is it independent from what we do with the vector later?
    addresses.clear();
    EXPECT_EQ(2, server.getForwardAddresses().size());

    // Did it return to fully recursive?
    server.setForwardAddresses(addresses);
    EXPECT_TRUE(server.getForwardAddresses().empty());
    EXPECT_FALSE(server.isForwarding());
}

72
TEST_F(ResolverConfig, forwardAddressConfig) {
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
    // Try putting there some address
    ElementPtr config(Element::fromJSON("{"
        "\"forward_addresses\": ["
        "   {"
        "       \"address\": \"192.0.2.1\","
        "       \"port\": 53"
        "   }"
        "]"
        "}"));
    ConstElementPtr result(server.updateConfig(config));
    EXPECT_EQ(result->toWire(), isc::config::createAnswer()->toWire());
    EXPECT_TRUE(server.isForwarding());
    ASSERT_EQ(1, server.getForwardAddresses().size());
    EXPECT_EQ("192.0.2.1", server.getForwardAddresses()[0].first);
    EXPECT_EQ(53, server.getForwardAddresses()[0].second);

    // And then remove all addresses
    config = Element::fromJSON("{"
        "\"forward_addresses\": null"
        "}");
    result = server.updateConfig(config);
    EXPECT_EQ(result->toWire(), isc::config::createAnswer()->toWire());
    EXPECT_FALSE(server.isForwarding());
    EXPECT_EQ(0, server.getForwardAddresses().size());
}

void
100
ResolverConfig::invalidTest(const string &JOSN) {
101
102
103
104
105
    ElementPtr config(Element::fromJSON(JOSN));
    EXPECT_FALSE(server.updateConfig(config)->equals(
        *isc::config::createAnswer())) << "Accepted config " << JOSN << endl;
}

106
TEST_F(ResolverConfig, invalidForwardAddresses) {
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
    // Try torturing it with some invalid inputs
    invalidTest("{"
        "\"forward_addresses\": \"error\""
        "}");
    invalidTest("{"
        "\"forward_addresses\": [{}]"
        "}");
    invalidTest("{"
        "\"forward_addresses\": [{"
        "   \"port\": 1.5,"
        "   \"address\": \"192.0.2.1\""
        "}]}");
    invalidTest("{"
        "\"forward_addresses\": [{"
        "   \"port\": -5,"
        "   \"address\": \"192.0.2.1\""
        "}]}");
    invalidTest("{"
        "\"forward_addresses\": [{"
        "   \"port\": 53,"
        "   \"address\": \"bad_address\""
        "}]}");
}

131
TEST_F(ResolverConfig, listenAddresses) {
132
133
134
135
136
    // Default value should be fully recursive
    EXPECT_TRUE(server.getListenAddresses().empty());

    // Try putting there some addresses
    vector<pair<string, uint16_t> > addresses;
137
138
    addresses.push_back(pair<string, uint16_t>("127.0.0.1", 5321));
    addresses.push_back(pair<string, uint16_t>("::1", 5321));
139
140
141
142
143
144
145
146
147
148
149
150
151
    server.setListenAddresses(addresses);
    EXPECT_EQ(2, server.getListenAddresses().size());
    EXPECT_EQ("::1", server.getListenAddresses()[1].first);

    // Is it independent from what we do with the vector later?
    addresses.clear();
    EXPECT_EQ(2, server.getListenAddresses().size());

    // Did it return to fully recursive?
    server.setListenAddresses(addresses);
    EXPECT_TRUE(server.getListenAddresses().empty());
}

152
TEST_F(ResolverConfig, DISABLED_listenAddressConfig) {
153
154
155
156
157
    // Try putting there some address
    ElementPtr config(Element::fromJSON("{"
        "\"listen_on\": ["
        "   {"
        "       \"address\": \"127.0.0.1\","
158
        "       \"port\": 5321"
159
160
161
162
163
164
165
        "   }"
        "]"
        "}"));
    ConstElementPtr result(server.updateConfig(config));
    EXPECT_EQ(result->toWire(), isc::config::createAnswer()->toWire());
    ASSERT_EQ(1, server.getListenAddresses().size());
    EXPECT_EQ("127.0.0.1", server.getListenAddresses()[0].first);
166
    EXPECT_EQ(5321, server.getListenAddresses()[0].second);
167
168
169
170
171
172
173
174
175
176

    // As this is example address, the machine should not have it on
    // any interface
    // FIXME: This test aborts, because it tries to rollback and
    //     it is impossible, since the sockets are not closed.
    //     Once #388 is solved, enable this test.
    config = Element::fromJSON("{"
        "\"listen_on\": ["
        "   {"
        "       \"address\": \"192.0.2.0\","
177
        "       \"port\": 5321"
178
179
180
181
182
183
184
        "   }"
        "]"
        "}");
    result = server.updateConfig(config);
    EXPECT_FALSE(result->equals(*isc::config::createAnswer()));
    ASSERT_EQ(1, server.getListenAddresses().size());
    EXPECT_EQ("127.0.0.1", server.getListenAddresses()[0].first);
185
    EXPECT_EQ(5321, server.getListenAddresses()[0].second);
186
187
}

188
TEST_F(ResolverConfig, invalidListenAddresses) {
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
    // Try torturing it with some invalid inputs
    invalidTest("{"
        "\"listen_on\": \"error\""
        "}");
    invalidTest("{"
        "\"listen_on\": [{}]"
        "}");
    invalidTest("{"
        "\"listen_on\": [{"
        "   \"port\": 1.5,"
        "   \"address\": \"192.0.2.1\""
        "}]}");
    invalidTest("{"
        "\"listen_on\": [{"
        "   \"port\": -5,"
        "   \"address\": \"192.0.2.1\""
        "}]}");
    invalidTest("{"
        "\"listen_on\": [{"
        "   \"port\": 53,"
        "   \"address\": \"bad_address\""
        "}]}");
}

// Just test it sets and gets the values correctly
214
TEST_F(ResolverConfig, timeouts) {
215
216
217
218
219
220
221
222
    server.setTimeouts(0, 1);
    EXPECT_EQ(0, server.getTimeouts().first);
    EXPECT_EQ(1, server.getTimeouts().second);
    server.setTimeouts();
    EXPECT_EQ(-1, server.getTimeouts().first);
    EXPECT_EQ(0, server.getTimeouts().second);
}

223
TEST_F(ResolverConfig, timeoutsConfig) {
224
225
226
227
228
229
230
231
232
233
    ElementPtr config = Element::fromJSON("{"
            "\"timeout\": 1000,"
            "\"retries\": 3"
            "}");
    ConstElementPtr result(server.updateConfig(config));
    EXPECT_EQ(result->toWire(), isc::config::createAnswer()->toWire());
    EXPECT_EQ(1000, server.getTimeouts().first);
    EXPECT_EQ(3, server.getTimeouts().second);
}

234
TEST_F(ResolverConfig, invalidTimeoutsConfig) {
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
    invalidTest("{"
        "\"timeout\": \"error\""
        "}");
    invalidTest("{"
        "\"timeout\": -2"
        "}");
    invalidTest("{"
        "\"retries\": \"error\""
        "}");
    invalidTest("{"
        "\"retries\": -1"
        "}");
}

}