auth_srv_unittest.cc 16.1 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>
18
#include <auth/auth_srv.h>
19
#include <testutils/srv_unittest.h>
20 21

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

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

37
class AuthSrvTest : public SrvTestBase {
38
protected:
39
    AuthSrvTest() : server(true, xfrout) {
40
        server.setXfrinSession(&notify_session);
41
    }
42
    MockXfroutClient xfrout;
43 44 45
    AuthSrv server;
};

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

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

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

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

// 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) {
70
    RESPONSE_TEST;
71 72 73 74
}

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

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

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

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

92 93
TEST_F(AuthSrvTest, AXFRSuccess) {
    EXPECT_FALSE(xfrout.isConnected());
Evan Hunt's avatar
Evan Hunt committed
94 95 96
    UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
                         Name("example.com"), RRClass::IN(), RRType::AXFR());
    createRequestPacket(request_message, IPPROTO_TCP);
97 98
    // 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
99 100
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_FALSE(dnsserv.hasAnswer());
101
    EXPECT_TRUE(xfrout.isConnected());
102 103 104 105 106
}

TEST_F(AuthSrvTest, AXFRConnectFail) {
    EXPECT_FALSE(xfrout.isConnected()); // check prerequisite
    xfrout.disableConnect();
Evan Hunt's avatar
Evan Hunt committed
107 108 109 110 111
    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());
112
    headerCheck(*parse_message, default_qid, Rcode::SERVFAIL(),
113 114 115 116 117 118 119
                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
120 121 122 123
    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);
124
    EXPECT_TRUE(xfrout.isConnected());
125 126

    xfrout.disableSend();
127 128
    parse_message->clear(Message::PARSE);
    response_obuffer->clear();
Evan Hunt's avatar
Evan Hunt committed
129 130 131 132 133
    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());
134
    headerCheck(*parse_message, default_qid, Rcode::SERVFAIL(),
135 136 137 138 139 140 141 142 143 144 145
                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
146 147 148
    UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
                         Name("example.com"), RRClass::IN(), RRType::AXFR());
    createRequestPacket(request_message, IPPROTO_TCP);
149
    EXPECT_THROW(server.processMessage(*io_message, parse_message,
Evan Hunt's avatar
Evan Hunt committed
150
                                       response_obuffer, &dnsserv),
151 152 153 154 155 156 157
                 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();
}

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

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

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

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

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

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

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

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

TEST_F(AuthSrvTest, notifyNonSOAQuestion) {
Evan Hunt's avatar
Evan Hunt committed
234 235
    UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(), default_qid,
                         Name("example.com"), RRClass::IN(), RRType::NS());
236
    request_message.setHeaderFlag(Message::HEADERFLAG_AA);
Evan Hunt's avatar
Evan Hunt committed
237 238 239
    createRequestPacket(request_message, IPPROTO_UDP);
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_TRUE(dnsserv.hasAnswer());
240
    headerCheck(*parse_message, default_qid, Rcode::FORMERR(),
241 242 243 244 245
                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
246 247 248 249 250
    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());
251
    headerCheck(*parse_message, default_qid, Rcode::NOERROR(),
252 253 254 255
                Opcode::NOTIFY().getCode(), QR_FLAG | AA_FLAG, 1, 0, 0, 0);
}

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

267
TEST_F(AuthSrvTest, notifyWithoutSession) {
268
    server.setXfrinSession(NULL);
269

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

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

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

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

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

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

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

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

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

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

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

327
void
328 329 330
updateConfig(AuthSrv* server, const char* const dbfile,
             const bool expect_success)
{
331
    ConstElementPtr config_answer =
332
        server->updateConfig(Element::fromJSON(dbfile));
333 334 335
    EXPECT_EQ(Element::map, config_answer->getType());
    EXPECT_TRUE(config_answer->contains("result"));

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

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

    // 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.
348
    createDataFromFile("examplequery_fromWire.wire");
Evan Hunt's avatar
Evan Hunt committed
349 350
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_TRUE(dnsserv.hasAnswer());
351
    headerCheck(*parse_message, default_qid, Rcode::NOERROR(), opcode.getCode(),
352 353 354 355
                QR_FLAG | AA_FLAG, 1, 1, 1, 0);
}

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

    // 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.
362
    createDataFromFile("badExampleQuery_fromWire.wire");
Evan Hunt's avatar
Evan Hunt committed
363 364
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_TRUE(dnsserv.hasAnswer());
365
    headerCheck(*parse_message, default_qid, Rcode::SERVFAIL(),
366
                opcode.getCode(), QR_FLAG, 1, 0, 0, 0);
367
}
368 369 370 371 372 373 374 375 376

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.
377
    createDataFromFile("examplequery_fromWire.wire");
Evan Hunt's avatar
Evan Hunt committed
378 379
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
    EXPECT_TRUE(dnsserv.hasAnswer());
380
    headerCheck(*parse_message, default_qid, Rcode::NOERROR(), opcode.getCode(),
381 382
                QR_FLAG | AA_FLAG, 1, 1, 1, 0);
}
JINMEI Tatuya's avatar
JINMEI Tatuya committed
383 384 385 386 387 388 389 390 391 392

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());
}
393
}