auth_srv_unittest.cc 21.9 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
#include <auth/statistics.h>
22

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
        server.setStatisticsSession(&statistics_session);
43
    }
44
    MockSession statistics_session;
45
    MockXfroutClient xfrout;
46
    AuthSrv server;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
47
    const RRClass rrclass;
48 49
};

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

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

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

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

// 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) {
74
    RESPONSE_TEST;
75 76 77 78
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

271
TEST_F(AuthSrvTest, notifyWithoutSession) {
272
    server.setXfrinSession(NULL);
273

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

JINMEI Tatuya's avatar
JINMEI Tatuya committed
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());
Jerry's avatar
Jerry committed
399

400
    // The memory data source is empty, should return REFUSED rcode.
Jerry's avatar
Jerry committed
401
    createDataFromFile("examplequery_fromWire.wire");
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
    server.processMessage(*io_message, parse_message, response_obuffer,
                          &dnsserv);
    EXPECT_TRUE(dnsserv.hasAnswer());
    headerCheck(*parse_message, default_qid, Rcode::REFUSED(),
                opcode.getCode(), QR_FLAG, 1, 0, 0, 0);
}

TEST_F(AuthSrvTest, chQueryWithMemoryDataSrc) {
    // Configure memory data source for class IN
    updateConfig(&server, "{\"datasources\": "
                 "[{\"class\": \"IN\", \"type\": \"memory\"}]}", true);

    // This shouldn't affect the result of class CH query
    UnitTestUtil::createRequestMessage(request_message, Opcode::QUERY(),
                                       default_qid, Name("version.bind"),
                                       RRClass::CH(), RRType::TXT());
    createRequestPacket(request_message, IPPROTO_UDP);
    server.processMessage(*io_message, parse_message, response_obuffer,
                          &dnsserv);
421
    EXPECT_TRUE(dnsserv.hasAnswer());
422 423
    headerCheck(*parse_message, default_qid, Rcode::NOERROR(),
                opcode.getCode(), QR_FLAG | AA_FLAG, 1, 1, 1, 0);
JINMEI Tatuya's avatar
JINMEI Tatuya committed
424 425
}

JINMEI Tatuya's avatar
JINMEI Tatuya committed
426 427 428 429 430 431 432 433 434
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());
}
435

436 437 438
// Submit UDP normal query and check query counter
TEST_F(AuthSrvTest, queryCounterUDPNormal) {
    // The counter should be initialized to 0.
439
    EXPECT_EQ(0, server.getCounter(AuthCounters::COUNTER_UDP_QUERY));
440 441 442 443 444 445 446
    // Create UDP message and process.
    UnitTestUtil::createRequestMessage(request_message, Opcode::QUERY(),
                                       default_qid, Name("example.com"),
                                       RRClass::IN(), RRType::NS());
    createRequestPacket(request_message, IPPROTO_UDP);
    server.processMessage(*io_message, parse_message, response_obuffer,
                          &dnsserv);
447
    // After processing UDP query, the counter should be 1.
448
    EXPECT_EQ(1, server.getCounter(AuthCounters::COUNTER_UDP_QUERY));
449 450
}

451 452 453
// Submit TCP normal query and check query counter
TEST_F(AuthSrvTest, queryCounterTCPNormal) {
    // The counter should be initialized to 0.
454
    EXPECT_EQ(0, server.getCounter(AuthCounters::COUNTER_TCP_QUERY));
455 456 457 458 459 460 461
    // Create TCP message and process.
    UnitTestUtil::createRequestMessage(request_message, Opcode::QUERY(),
                                       default_qid, Name("example.com"),
                                       RRClass::IN(), RRType::NS());
    createRequestPacket(request_message, IPPROTO_TCP);
    server.processMessage(*io_message, parse_message, response_obuffer,
                          &dnsserv);
462
    // After processing TCP query, the counter should be 1.
463
    EXPECT_EQ(1, server.getCounter(AuthCounters::COUNTER_TCP_QUERY));
464 465 466 467 468
}

// Submit TCP AXFR query and check query counter
TEST_F(AuthSrvTest, queryCounterTCPAXFR) {
    // The counter should be initialized to 0.
469
    EXPECT_EQ(0, server.getCounter(AuthCounters::COUNTER_TCP_QUERY));
470 471 472 473 474 475
    UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
                         Name("example.com"), RRClass::IN(), RRType::AXFR());
    createRequestPacket(request_message, IPPROTO_TCP);
    // On success, the AXFR query has been passed to a separate process,
    // so we shouldn't have to respond.
    server.processMessage(*io_message, parse_message, response_obuffer, &dnsserv);
476
    // After processing TCP AXFR query, the counter should be 1.
477
    EXPECT_EQ(1, server.getCounter(AuthCounters::COUNTER_TCP_QUERY));
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
}

// class for queryCounterUnexpected test
// getProtocol() returns IPPROTO_IP
class DummyUnknownSocket : public IOSocket {
public:
    DummyUnknownSocket() {}
    virtual int getNative() const { return (0); }
    virtual int getProtocol() const { return (IPPROTO_IP); }
};

// function for queryCounterUnexpected test
// returns a reference to a static object of DummyUnknownSocket
IOSocket&
getDummyUnknownSocket() {
    static DummyUnknownSocket socket;
    return (socket);
}

497
// Submit unexpected type of query and check it throws isc::Unexpected
498
TEST_F(AuthSrvTest, queryCounterUnexpected) {
499 500 501 502
    // This code isn't exception safe, but we'd rather keep the code
    // simpler and more readable as this is only for tests and if it throws
    // the program would immediately terminate anyway.

503
    // Create UDP query packet.
504 505 506 507
    UnitTestUtil::createRequestMessage(request_message, Opcode::QUERY(),
                                       default_qid, Name("example.com"),
                                       RRClass::IN(), RRType::NS());
    createRequestPacket(request_message, IPPROTO_UDP);
508

509
    // Modify the message.
510 511 512 513 514 515 516 517
    delete io_message;
    endpoint = IOEndpoint::create(IPPROTO_UDP,
                                  IOAddress(DEFAULT_REMOTE_ADDRESS), 5300);
    io_message = new IOMessage(request_renderer.getData(),
                               request_renderer.getLength(),
                               getDummyUnknownSocket(), *endpoint);

    EXPECT_THROW(server.processMessage(*io_message, parse_message,
518
                                       response_obuffer, &dnsserv),
519
                 isc::Unexpected);
520
}
521
}