auth_srv_unittest.cc 16.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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.

// $Id$

17
#include <config.h>
JINMEI Tatuya's avatar
JINMEI Tatuya committed
18
#include <datasrc/memory_datasrc.h>
19
#include <auth/auth_srv.h>
20
#include <testutils/srv_unittest.h>
21 22

using namespace std;
23
using namespace isc::cc;
24
using namespace isc::dns;
25
using namespace isc::data;
26
using namespace isc::xfr;
27
using namespace asiolink;
Evan Hunt's avatar
Evan Hunt committed
28
using isc::UnitTestUtil;
29 30

namespace {
31
const char* const CONFIG_TESTDB =
32
    "{\"database_file\": \"" TEST_DATA_DIR "/example.sqlite3\"}";
33 34
// The following file must be non existent and must be non"creatable" (see
// the sqlite3 test).
35
const char* const BADCONFIG_TESTDB =
36
    "{ \"database_file\": \"" TEST_DATA_DIR "/nodir/notexist\"}";
37

38
class AuthSrvTest : public SrvTestBase {
39
protected:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
40
    AuthSrvTest() : server(true, xfrout), rrclass(RRClass::IN()) {
41
        server.setXfrinSession(&notify_session);
42
    }
43
    MockXfroutClient xfrout;
44
    AuthSrv server;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
45
    const RRClass rrclass;
46 47
};

48 49
// Unsupported requests.  Should result in NOTIMP.
TEST_F(AuthSrvTest, unsupportedRequest) {
50
    UNSUPPORTED_REQUEST_TEST;
51
}
52

53 54
// Simple API check
TEST_F(AuthSrvTest, verbose) {
55
    VERBOSE_TEST;
56 57
}

58 59
// Multiple questions.  Should result in FORMERR.
TEST_F(AuthSrvTest, multiQuestion) {
60
    MULTI_QUESTION_TEST;
61 62
}

63 64 65
// Incoming data doesn't even contain the complete header.  Must be silently
// dropped.
TEST_F(AuthSrvTest, shortMessage) {
66
    SHORT_MESSAGE_TEST;
67 68 69 70 71
}

// Response messages.  Must be silently dropped, whether it's a valid response
// or malformed or could otherwise cause a protocol error.
TEST_F(AuthSrvTest, response) {
72
    RESPONSE_TEST;
73 74 75 76
}

// Query with a broken question
TEST_F(AuthSrvTest, shortQuestion) {
77
    SHORT_QUESTION_TEST;
78
}
79

80 81
// Query with a broken answer section
TEST_F(AuthSrvTest, shortAnswer) {
82
    SHORT_ANSWER_TEST;
83 84
}

85 86
// Query with unsupported version of EDNS.
TEST_F(AuthSrvTest, ednsBadVers) {
87
    EDNS_BADVERS_TEST;
88 89
}

JINMEI Tatuya's avatar
JINMEI Tatuya committed
90
TEST_F(AuthSrvTest, AXFROverUDP) {
91
    AXFR_OVER_UDP_TEST;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
92 93
}

94 95
TEST_F(AuthSrvTest, AXFRSuccess) {
    EXPECT_FALSE(xfrout.isConnected());
Evan Hunt's avatar
Evan Hunt committed
96 97 98
    UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
                         Name("example.com"), RRClass::IN(), RRType::AXFR());
    createRequestPacket(request_message, IPPROTO_TCP);
99 100
    // On success, the AXFR query has been passed to a separate process,
    // so we shouldn't have to respond.
Evan Hunt's avatar
Evan Hunt committed
101 102
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_FALSE(dnsserv.hasAnswer());
103
    EXPECT_TRUE(xfrout.isConnected());
104 105 106 107 108
}

TEST_F(AuthSrvTest, AXFRConnectFail) {
    EXPECT_FALSE(xfrout.isConnected()); // check prerequisite
    xfrout.disableConnect();
Evan Hunt's avatar
Evan Hunt committed
109 110 111 112 113
    UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
                         Name("example.com"), RRClass::IN(), RRType::AXFR());
    createRequestPacket(request_message, IPPROTO_TCP);
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_TRUE(dnsserv.hasAnswer());
114
    headerCheck(*parse_message, default_qid, Rcode::SERVFAIL(),
115 116 117 118 119 120 121
                opcode.getCode(), QR_FLAG, 1, 0, 0, 0);
    EXPECT_FALSE(xfrout.isConnected());
}

TEST_F(AuthSrvTest, AXFRSendFail) {
    // first send a valid query, making the connection with the xfr process
    // open.
Evan Hunt's avatar
Evan Hunt committed
122 123 124 125
    UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
                         Name("example.com"), RRClass::IN(), RRType::AXFR());
    createRequestPacket(request_message, IPPROTO_TCP);
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
126
    EXPECT_TRUE(xfrout.isConnected());
127 128

    xfrout.disableSend();
129 130
    parse_message->clear(Message::PARSE);
    response_obuffer->clear();
Evan Hunt's avatar
Evan Hunt committed
131 132 133 134 135
    UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
                         Name("example.com"), RRClass::IN(), RRType::AXFR());
    createRequestPacket(request_message, IPPROTO_TCP);
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_TRUE(dnsserv.hasAnswer());
136
    headerCheck(*parse_message, default_qid, Rcode::SERVFAIL(),
137 138 139 140 141 142 143 144 145 146 147
                opcode.getCode(), QR_FLAG, 1, 0, 0, 0);

    // The connection should have been closed due to the send failure.
    EXPECT_FALSE(xfrout.isConnected());
}

TEST_F(AuthSrvTest, AXFRDisconnectFail) {
    // In our usage disconnect() shouldn't fail.  So we'll see the exception
    // should it be thrown.
    xfrout.disableSend();
    xfrout.disableDisconnect();
Evan Hunt's avatar
Evan Hunt committed
148 149 150
    UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
                         Name("example.com"), RRClass::IN(), RRType::AXFR());
    createRequestPacket(request_message, IPPROTO_TCP);
151
    EXPECT_THROW(server.processMessage(*io_message, parse_message,
Evan Hunt's avatar
Evan Hunt committed
152
                                       response_obuffer, &dnsserv),
153 154 155 156 157 158 159
                 XfroutError);
    EXPECT_TRUE(xfrout.isConnected());
    // XXX: we need to re-enable disconnect.  otherwise an exception would be
    // thrown via the destructor of the server.
    xfrout.enableDisconnect();
}

160
TEST_F(AuthSrvTest, notify) {
Evan Hunt's avatar
Evan Hunt committed
161 162
    UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(), default_qid,
                         Name("example.com"), RRClass::IN(), RRType::SOA());
163
    request_message.setHeaderFlag(Message::HEADERFLAG_AA);
Evan Hunt's avatar
Evan Hunt committed
164 165 166
    createRequestPacket(request_message, IPPROTO_UDP);
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_TRUE(dnsserv.hasAnswer());
167 168 169

    // An internal command message should have been created and sent to an
    // external module.  Check them.
Evan Hunt's avatar
Evan Hunt committed
170
    EXPECT_EQ("Zonemgr", notify_session.getMessageDest());
171
    EXPECT_EQ("notify",
Evan Hunt's avatar
Evan Hunt committed
172
              notify_session.getSentMessage()->get("command")->get(0)->stringValue());
173
    ConstElementPtr notify_args =
Evan Hunt's avatar
Evan Hunt committed
174
        notify_session.getSentMessage()->get("command")->get(1);
175 176 177
    EXPECT_EQ("example.com.", notify_args->get("zone_name")->stringValue());
    EXPECT_EQ(DEFAULT_REMOTE_ADDRESS,
              notify_args->get("master")->stringValue());
178
    EXPECT_EQ("IN", notify_args->get("zone_class")->stringValue());
179 180

    // On success, the server should return a response to the notify.
181
    headerCheck(*parse_message, default_qid, Rcode::NOERROR(),
182 183
                Opcode::NOTIFY().getCode(), QR_FLAG | AA_FLAG, 1, 0, 0, 0);

184
    // The question must be identical to that of the received notify
185
    ConstQuestionPtr question = *parse_message->beginQuestion();
186 187 188 189 190
    EXPECT_EQ(Name("example.com"), question->getName());
    EXPECT_EQ(RRClass::IN(), question->getClass());
    EXPECT_EQ(RRType::SOA(), question->getType());
}

191 192
TEST_F(AuthSrvTest, notifyForCHClass) {
    // Same as the previous test, but for the CH RRClass.
Evan Hunt's avatar
Evan Hunt committed
193 194
    UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(), default_qid,
                         Name("example.com"), RRClass::CH(), RRType::SOA());
195
    request_message.setHeaderFlag(Message::HEADERFLAG_AA);
Evan Hunt's avatar
Evan Hunt committed
196 197 198
    createRequestPacket(request_message, IPPROTO_UDP);
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_TRUE(dnsserv.hasAnswer());
199 200 201

    // Other conditions should be the same, so simply confirm the RR class is
    // set correctly.
202
    ConstElementPtr notify_args =
Evan Hunt's avatar
Evan Hunt committed
203
        notify_session.getSentMessage()->get("command")->get(1);
204
    EXPECT_EQ("CH", notify_args->get("zone_class")->stringValue());
205 206
}

207 208 209
TEST_F(AuthSrvTest, notifyEmptyQuestion) {
    request_message.clear(Message::RENDER);
    request_message.setOpcode(Opcode::NOTIFY());
210
    request_message.setRcode(Rcode::NOERROR());
211
    request_message.setHeaderFlag(Message::HEADERFLAG_AA);
212 213
    request_message.setQid(default_qid);
    request_message.toWire(request_renderer);
Evan Hunt's avatar
Evan Hunt committed
214 215 216
    createRequestPacket(request_message, IPPROTO_UDP);
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_TRUE(dnsserv.hasAnswer());
217
    headerCheck(*parse_message, default_qid, Rcode::FORMERR(),
218 219 220 221
                Opcode::NOTIFY().getCode(), QR_FLAG, 0, 0, 0, 0);
}

TEST_F(AuthSrvTest, notifyMultiQuestions) {
Evan Hunt's avatar
Evan Hunt committed
222 223
    UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(), default_qid,
                         Name("example.com"), RRClass::IN(), RRType::SOA());
224 225 226
    // add one more SOA question
    request_message.addQuestion(Question(Name("example.com"), RRClass::IN(),
                                         RRType::SOA()));
227
    request_message.setHeaderFlag(Message::HEADERFLAG_AA);
Evan Hunt's avatar
Evan Hunt committed
228 229 230
    createRequestPacket(request_message, IPPROTO_UDP);
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_TRUE(dnsserv.hasAnswer());
231
    headerCheck(*parse_message, default_qid, Rcode::FORMERR(),
232 233 234 235
                Opcode::NOTIFY().getCode(), QR_FLAG, 2, 0, 0, 0);
}

TEST_F(AuthSrvTest, notifyNonSOAQuestion) {
Evan Hunt's avatar
Evan Hunt committed
236 237
    UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(), default_qid,
                         Name("example.com"), RRClass::IN(), RRType::NS());
238
    request_message.setHeaderFlag(Message::HEADERFLAG_AA);
Evan Hunt's avatar
Evan Hunt committed
239 240 241
    createRequestPacket(request_message, IPPROTO_UDP);
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_TRUE(dnsserv.hasAnswer());
242
    headerCheck(*parse_message, default_qid, Rcode::FORMERR(),
243 244 245 246 247
                Opcode::NOTIFY().getCode(), QR_FLAG, 1, 0, 0, 0);
}

TEST_F(AuthSrvTest, notifyWithoutAA) {
    // implicitly leave the AA bit off.  our implementation will accept it.
Evan Hunt's avatar
Evan Hunt committed
248 249 250 251 252
    UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(), default_qid,
                         Name("example.com"), RRClass::IN(), RRType::SOA());
    createRequestPacket(request_message, IPPROTO_UDP);
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_TRUE(dnsserv.hasAnswer());
253
    headerCheck(*parse_message, default_qid, Rcode::NOERROR(),
254 255 256 257
                Opcode::NOTIFY().getCode(), QR_FLAG | AA_FLAG, 1, 0, 0, 0);
}

TEST_F(AuthSrvTest, notifyWithErrorRcode) {
Evan Hunt's avatar
Evan Hunt committed
258 259
    UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(), default_qid,
                         Name("example.com"), RRClass::IN(), RRType::SOA());
260
    request_message.setHeaderFlag(Message::HEADERFLAG_AA);
261
    request_message.setRcode(Rcode::SERVFAIL());
Evan Hunt's avatar
Evan Hunt committed
262 263 264
    createRequestPacket(request_message, IPPROTO_UDP);
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_TRUE(dnsserv.hasAnswer());
265
    headerCheck(*parse_message, default_qid, Rcode::NOERROR(),
266
                Opcode::NOTIFY().getCode(), QR_FLAG | AA_FLAG, 1, 0, 0, 0);
Han Feng's avatar
Han Feng committed
267 268
}

269
TEST_F(AuthSrvTest, notifyWithoutSession) {
270
    server.setXfrinSession(NULL);
271

Evan Hunt's avatar
Evan Hunt committed
272 273
    UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(), default_qid,
                         Name("example.com"), RRClass::IN(), RRType::SOA());
274
    request_message.setHeaderFlag(Message::HEADERFLAG_AA);
Evan Hunt's avatar
Evan Hunt committed
275
    createRequestPacket(request_message, IPPROTO_UDP);
276 277 278

    // we simply ignore the notify and let it be resent if an internal error
    // happens.
Evan Hunt's avatar
Evan Hunt committed
279 280
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_FALSE(dnsserv.hasAnswer());
281 282 283 284 285
}

TEST_F(AuthSrvTest, notifySendFail) {
    notify_session.disableSend();

Evan Hunt's avatar
Evan Hunt committed
286 287
    UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(), default_qid,
                         Name("example.com"), RRClass::IN(), RRType::SOA());
288
    request_message.setHeaderFlag(Message::HEADERFLAG_AA);
Evan Hunt's avatar
Evan Hunt committed
289
    createRequestPacket(request_message, IPPROTO_UDP);
290

Evan Hunt's avatar
Evan Hunt committed
291 292
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_FALSE(dnsserv.hasAnswer());
293 294 295 296 297
}

TEST_F(AuthSrvTest, notifyReceiveFail) {
    notify_session.disableReceive();

Evan Hunt's avatar
Evan Hunt committed
298 299
    UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(), default_qid,
                         Name("example.com"), RRClass::IN(), RRType::SOA());
300
    request_message.setHeaderFlag(Message::HEADERFLAG_AA);
Evan Hunt's avatar
Evan Hunt committed
301 302 303
    createRequestPacket(request_message, IPPROTO_UDP);
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_FALSE(dnsserv.hasAnswer());
304 305 306
}

TEST_F(AuthSrvTest, notifyWithBogusSessionMessage) {
307
    notify_session.setMessage(Element::fromJSON("{\"foo\": 1}"));
308

Evan Hunt's avatar
Evan Hunt committed
309 310
    UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(), default_qid,
                         Name("example.com"), RRClass::IN(), RRType::SOA());
311
    request_message.setHeaderFlag(Message::HEADERFLAG_AA);
Evan Hunt's avatar
Evan Hunt committed
312 313 314
    createRequestPacket(request_message, IPPROTO_UDP);
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_FALSE(dnsserv.hasAnswer());
315 316 317 318
}

TEST_F(AuthSrvTest, notifyWithSessionMessageError) {
    notify_session.setMessage(
319
        Element::fromJSON("{\"result\": [1, \"FAIL\"]}"));
320

Evan Hunt's avatar
Evan Hunt committed
321 322
    UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(), default_qid,
                         Name("example.com"), RRClass::IN(), RRType::SOA());
323
    request_message.setHeaderFlag(Message::HEADERFLAG_AA);
Evan Hunt's avatar
Evan Hunt committed
324 325 326
    createRequestPacket(request_message, IPPROTO_UDP);
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_FALSE(dnsserv.hasAnswer());
327 328
}

329
void
JINMEI Tatuya's avatar
JINMEI Tatuya committed
330
updateConfig(AuthSrv* server, const char* const config_data,
331 332
             const bool expect_success)
{
333
    ConstElementPtr config_answer =
JINMEI Tatuya's avatar
JINMEI Tatuya committed
334
        server->updateConfig(Element::fromJSON(config_data));
335 336 337
    EXPECT_EQ(Element::map, config_answer->getType());
    EXPECT_TRUE(config_answer->contains("result"));

338
    ConstElementPtr result = config_answer->get("result");
339
    EXPECT_EQ(Element::list, result->getType());
340
    EXPECT_EQ(expect_success ? 0 : 1, result->get(0)->intValue());
341 342 343 344
}

// Install a Sqlite3 data source with testing data.
TEST_F(AuthSrvTest, updateConfig) {
345
    updateConfig(&server, CONFIG_TESTDB, true);
346 347 348 349

    // query for existent data in the installed data source.  The resulting
    // response should have the AA flag on, and have an RR in each answer
    // and authority section.
350
    createDataFromFile("examplequery_fromWire.wire");
Evan Hunt's avatar
Evan Hunt committed
351 352
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_TRUE(dnsserv.hasAnswer());
353
    headerCheck(*parse_message, default_qid, Rcode::NOERROR(), opcode.getCode(),
354 355 356 357
                QR_FLAG | AA_FLAG, 1, 1, 1, 0);
}

TEST_F(AuthSrvTest, datasourceFail) {
358
    updateConfig(&server, CONFIG_TESTDB, true);
359 360 361 362 363

    // This query will hit a corrupted entry of the data source (the zoneload
    // tool and the data source itself naively accept it).  This will result
    // in a SERVFAIL response, and the answer and authority sections should
    // be empty.
364
    createDataFromFile("badExampleQuery_fromWire.wire");
Evan Hunt's avatar
Evan Hunt committed
365 366
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_TRUE(dnsserv.hasAnswer());
367
    headerCheck(*parse_message, default_qid, Rcode::SERVFAIL(),
368
                opcode.getCode(), QR_FLAG, 1, 0, 0, 0);
369
}
370 371 372 373 374 375 376 377 378

TEST_F(AuthSrvTest, updateConfigFail) {
    // First, load a valid data source.
    updateConfig(&server, CONFIG_TESTDB, true);

    // Next, try to update it with a non-existent one.  This should fail.
    updateConfig(&server, BADCONFIG_TESTDB, false);

    // The original data source should still exist.
379
    createDataFromFile("examplequery_fromWire.wire");
Evan Hunt's avatar
Evan Hunt committed
380 381
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_TRUE(dnsserv.hasAnswer());
382
    headerCheck(*parse_message, default_qid, Rcode::NOERROR(), opcode.getCode(),
383 384
                QR_FLAG | AA_FLAG, 1, 1, 1, 0);
}
JINMEI Tatuya's avatar
JINMEI Tatuya committed
385

JINMEI Tatuya's avatar
JINMEI Tatuya committed
386 387 388 389 390 391 392 393 394 395 396 397 398
TEST_F(AuthSrvTest, updateWithMemoryDataSrc) {
    // Test configuring memory data source.  Detailed test cases are covered
    // in the configuration tests.  We only check the AuthSrv interface here.

    // By default memory data source isn't enabled
    EXPECT_EQ(AuthSrv::MemoryDataSrcPtr(), server.getMemoryDataSrc(rrclass));
    updateConfig(&server,
                 "{\"datasources\": [{\"type\": \"memory\"}]}", true);
    // after successful configuration, we should have one (with empty zoneset).
    ASSERT_NE(AuthSrv::MemoryDataSrcPtr(), server.getMemoryDataSrc(rrclass));
    EXPECT_EQ(0, server.getMemoryDataSrc(rrclass)->getZoneCount());
}

JINMEI Tatuya's avatar
JINMEI Tatuya committed
399 400 401 402 403 404 405 406 407
TEST_F(AuthSrvTest, cacheSlots) {
    // simple check for the get/set operations
    server.setCacheSlots(10);    // 10 = arbitrary choice
    EXPECT_EQ(10, server.getCacheSlots());

    // 0 is a valid size
    server.setCacheSlots(0);
    EXPECT_EQ(00, server.getCacheSlots());
}
408
}