summaryrefslogtreecommitdiffstats
path: root/src/lib/testutils/portconfig.h
blob: ce1bb39c7b2c8d39d0b351b2621dd89ff3f74870 (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
// Copyright (C) 2011  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.

#ifndef __ISC_TESTUTILS_PORTCONFIG_H
#define __ISC_TESTUTILS_PORTCONFIG_H

#include <gtest/gtest.h>
#include <cc/data.h>
#include <server_common/portconfig.h>

namespace isc {
namespace testutils {
/**
 * \brief Bits of tests for server port configuration.
 *
 * These are bits of tests that can be reused by server classes to check if
 * configuration of the listening addresses work.
 *
 * You can put any of these functions into a TEST_F test and pass the server
 * to it.
 *
 * \todo There's quite a lot of common code in the basic server handling.
 *     We should refactor it, so both Resolver server and Auth server have
 *     a common base class. When this is done, the common parts would be put
 *     there and the tests would be at the base class, not here.
 */
namespace portconfig {

/**
 * \brief Check setting of the listening addresses directly (as a list) works.
 *
 * \param server The server to test against.
 */
template<class Server>
void
listenAddresses(Server& server) {
    using namespace isc::server_common::portconfig;
    // In this test we assume the address list is originally empty.
    EXPECT_TRUE(server.getListenAddresses().empty());

    // Try putting there some addresses
    AddressList addresses;
    addresses.push_back(AddressPair("127.0.0.1", 53210));
    addresses.push_back(AddressPair("::1", 53210));
    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());

    // If we set to an empty list next, the server configuration should
    // become empty, too.
    server.setListenAddresses(addresses);
    EXPECT_TRUE(server.getListenAddresses().empty());
}

/**
 * \brief Check setting of the addresses by config value.
 *
 * This passes an listen_on element to the server's updateConfig function.
 * It tries little bit of switching around. It tries both setting a presumably
 * valid addresses and then setting something that cant be bound, rolling back
 * back to original.
 *
 * \param server The server object to test against.
 */
template<class Server>
void
listenAddressConfig(Server& server) {
    using namespace isc::data;
    // Try putting there some address
    ElementPtr config(Element::fromJSON("{"
                                        "\"listen_on\": ["
                                        "   {"
                                        "       \"address\": \"127.0.0.1\","
                                        "       \"port\": 53210"
                                        "   }"
                                        "]"
                                        "}"));
    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);
    EXPECT_EQ(53210, server.getListenAddresses()[0].second);

    // This address is rejected by the test socket requestor
    config = Element::fromJSON("{"
                               "\"listen_on\": ["
                               "   {"
                               "       \"address\": \"192.0.2.2\","
                               "       \"port\": 53210"
                               "   }"
                               "]"
                               "}");
    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);
    EXPECT_EQ(53210, server.getListenAddresses()[0].second);

}

/**
 * \brief Check that given config is rejected.
 *
 * Try if given config is considered invalid by the server and is rejected.
 * The value is converted from JSON to the data elements and passed to server's
 * updateConfig method. It should not crash, but return a negative answer.
 *
 * It is used internally by invalidListenAddressConfig, but you can use it
 * to test any other invalid configs.
 *
 * \todo It might be better to put it to some other namespace, as this is more
 *     generic. But right now it is used only here, so until something else
 *     needs it, it might as well stay here.
 * \param server The server to test against.
 * \param JSON Config to use.
 * \param name It is used in the output if the test fails.
 */
template<class Server>
void
configRejected(Server& server, const std::string& JSON,
               const std::string& name)
{
    SCOPED_TRACE(name);

    using namespace isc::data;
    ElementPtr config(Element::fromJSON(JSON));
    EXPECT_FALSE(server.updateConfig(config)->
                 equals(*isc::config::createAnswer())) <<
        "Accepted invalid config " << JSON;
}

/**
 * \brief Check some invalid address configs.
 *
 * It tries a series of invalid listen_on configs against the server and checks
 * it is rejected.
 * \param server The server to check against.
 */
template<class Server>
void
invalidListenAddressConfig(Server& server) {
    configRejected(server, "{"
                   "\"listen_on\": \"error\""
                   "}", "Wrong element type");
    configRejected(server, "{"
                   "\"listen_on\": [{}]"
                   "}", "Empty address element");
    configRejected(server, "{"
                   "\"listen_on\": [{"
                   "   \"port\": 1.5,"
                   "   \"address\": \"192.0.2.1\""
                   "}]}", "Float port");
    configRejected(server, "{"
                   "\"listen_on\": [{"
                   "   \"port\": -5,"
                   "   \"address\": \"192.0.2.1\""
                   "}]}", "Negative port");
    configRejected(server, "{"
                   "\"listen_on\": [{"
                   "   \"port\": 1000000,"
                   "   \"address\": \"192.0.2.1\""
                   "}]}", "Huge port");
    configRejected(server, "{"
                   "\"listen_on\": [{"
                   "   \"port\": 53,"
                   "   \"address\": \"bad_address\""
                   "}]}", "Bad address");
}

}
}
}

#endif  // __ISC_TESTUTILS_PORTCONFIG_H