auth_srv_unittest.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. // $Id$
  15. #include <config.h>
  16. #include <gtest/gtest.h>
  17. #include <dns/buffer.h>
  18. #include <dns/name.h>
  19. #include <dns/message.h>
  20. #include <dns/messagerenderer.h>
  21. #include <dns/opcode.h>
  22. #include <dns/rcode.h>
  23. #include <dns/rrclass.h>
  24. #include <dns/rrtype.h>
  25. #include <cc/data.h>
  26. #include <cc/session.h>
  27. #include <xfr/xfrout_client.h>
  28. #include <auth/auth_srv.h>
  29. #include <auth/asio_link.h>
  30. #include <dns/tests/unittest_util.h>
  31. using isc::UnitTestUtil;
  32. using namespace std;
  33. using namespace isc::cc;
  34. using namespace isc::dns;
  35. using namespace isc::data;
  36. using namespace isc::xfr;
  37. using namespace asio_link;
  38. namespace {
  39. const char* const CONFIG_TESTDB =
  40. "{\"database_file\": \"" TEST_DATA_DIR "/example.sqlite3\"}";
  41. // The following file must be non existent and must be non"creatable" (see
  42. // the sqlite3 test).
  43. const char* const BADCONFIG_TESTDB =
  44. "{ \"database_file\": \"" TEST_DATA_DIR "/nodir/notexist\"}";
  45. const char* const DEFAULT_REMOTE_ADDRESS = "192.0.2.1";
  46. class AuthSrvTest : public ::testing::Test {
  47. private:
  48. class MockXfroutClient : public AbstractXfroutClient {
  49. public:
  50. MockXfroutClient() :
  51. is_connected_(false), connect_ok_(true), send_ok_(true),
  52. disconnect_ok_(true)
  53. {}
  54. virtual void connect();
  55. virtual void disconnect();
  56. virtual int sendXfroutRequestInfo(int tcp_sock, const void* msg_data,
  57. uint16_t msg_len);
  58. bool isConnected() const { return (is_connected_); }
  59. void disableConnect() { connect_ok_ = false; }
  60. void disableDisconnect() { disconnect_ok_ = false; }
  61. void enableDisconnect() { disconnect_ok_ = true; }
  62. void disableSend() { send_ok_ = false; }
  63. private:
  64. bool is_connected_;
  65. bool connect_ok_;
  66. bool send_ok_;
  67. bool disconnect_ok_;
  68. };
  69. class MockSession : public AbstractSession {
  70. public:
  71. MockSession() :
  72. // by default we return a simple "success" message.
  73. msg_(Element::fromJSON("{\"result\": [0, \"SUCCESS\"]}")),
  74. send_ok_(true), receive_ok_(true)
  75. {}
  76. virtual void establish(const char* socket_file);
  77. virtual void disconnect();
  78. virtual int group_sendmsg(ConstElementPtr msg, string group,
  79. string instance, string to);
  80. virtual bool group_recvmsg(ConstElementPtr& envelope,
  81. ConstElementPtr& msg,
  82. bool nonblock, int seq);
  83. virtual void subscribe(string group, string instance);
  84. virtual void unsubscribe(string group, string instance);
  85. virtual void startRead(boost::function<void()> read_callback);
  86. virtual int reply(ConstElementPtr envelope, ConstElementPtr newmsg);
  87. virtual bool hasQueuedMsgs() const;
  88. virtual void setTimeout(size_t timeout UNUSED_PARAM) {};
  89. virtual size_t getTimeout() const { return 0; };
  90. void setMessage(ConstElementPtr msg) { msg_ = msg; }
  91. void disableSend() { send_ok_ = false; }
  92. void disableReceive() { receive_ok_ = false; }
  93. ConstElementPtr sent_msg;
  94. string msg_destination;
  95. private:
  96. ConstElementPtr msg_;
  97. bool send_ok_;
  98. bool receive_ok_;
  99. };
  100. protected:
  101. AuthSrvTest() : server(true, xfrout),
  102. request_message(Message::RENDER),
  103. parse_message(Message::PARSE), default_qid(0x1035),
  104. opcode(Opcode::QUERY()), qname("www.example.com"),
  105. qclass(RRClass::IN()), qtype(RRType::A()),
  106. io_message(NULL), endpoint(NULL), request_obuffer(0),
  107. request_renderer(request_obuffer),
  108. response_obuffer(0), response_renderer(response_obuffer)
  109. {
  110. server.setXfrinSession(&notify_session);
  111. }
  112. ~AuthSrvTest() {
  113. delete io_message;
  114. delete endpoint;
  115. }
  116. MockSession notify_session;
  117. MockXfroutClient xfrout;
  118. AuthSrv server;
  119. Message request_message;
  120. Message parse_message;
  121. const qid_t default_qid;
  122. const Opcode opcode;
  123. const Name qname;
  124. const RRClass qclass;
  125. const RRType qtype;
  126. IOMessage* io_message;
  127. const IOEndpoint* endpoint;
  128. OutputBuffer request_obuffer;
  129. MessageRenderer request_renderer;
  130. OutputBuffer response_obuffer;
  131. MessageRenderer response_renderer;
  132. vector<uint8_t> data;
  133. void createDataFromFile(const char* const datafile, int protocol);
  134. void createRequestMessage(const Opcode& opcode, const Name& request_name,
  135. const RRClass& rrclass, const RRType& rrtype);
  136. void createRequestPacket(const Opcode& opcode, const Name& request_name,
  137. const RRClass& rrclass, const RRType& rrtype,
  138. int protocol);
  139. void createRequestPacket(int protocol);
  140. };
  141. void
  142. AuthSrvTest::MockSession::establish(const char* socket_file UNUSED_PARAM) {}
  143. void
  144. AuthSrvTest::MockSession::disconnect() {}
  145. void
  146. AuthSrvTest::MockSession::subscribe(string group UNUSED_PARAM,
  147. string instance UNUSED_PARAM)
  148. {}
  149. void
  150. AuthSrvTest::MockSession::unsubscribe(string group UNUSED_PARAM,
  151. string instance UNUSED_PARAM)
  152. {}
  153. void
  154. AuthSrvTest::MockSession::startRead(
  155. boost::function<void()> read_callback UNUSED_PARAM)
  156. {}
  157. int
  158. AuthSrvTest::MockSession::reply(ConstElementPtr envelope UNUSED_PARAM,
  159. ConstElementPtr newmsg UNUSED_PARAM)
  160. {
  161. return (-1);
  162. }
  163. bool
  164. AuthSrvTest::MockSession::hasQueuedMsgs() const {
  165. return (false);
  166. }
  167. int
  168. AuthSrvTest::MockSession::group_sendmsg(ConstElementPtr msg, string group,
  169. string instance UNUSED_PARAM,
  170. string to UNUSED_PARAM)
  171. {
  172. if (!send_ok_) {
  173. isc_throw(XfroutError, "mock session send is disabled for test");
  174. }
  175. sent_msg = msg;
  176. msg_destination = group;
  177. return (0);
  178. }
  179. bool
  180. AuthSrvTest::MockSession::group_recvmsg(ConstElementPtr& envelope UNUSED_PARAM,
  181. ConstElementPtr& msg,
  182. bool nonblock UNUSED_PARAM,
  183. int seq UNUSED_PARAM)
  184. {
  185. if (!receive_ok_) {
  186. isc_throw(XfroutError, "mock session receive is disabled for test");
  187. }
  188. msg = msg_;
  189. return (true);
  190. }
  191. void
  192. AuthSrvTest::MockXfroutClient::connect() {
  193. if (!connect_ok_) {
  194. isc_throw(XfroutError, "xfrout connection disabled for test");
  195. }
  196. is_connected_ = true;
  197. }
  198. void
  199. AuthSrvTest::MockXfroutClient::disconnect() {
  200. if (!disconnect_ok_) {
  201. isc_throw(XfroutError,
  202. "closing xfrout connection is disabled for test");
  203. }
  204. is_connected_ = false;
  205. }
  206. int
  207. AuthSrvTest::MockXfroutClient::sendXfroutRequestInfo(
  208. const int tcp_sock UNUSED_PARAM,
  209. const void* msg_data UNUSED_PARAM,
  210. const uint16_t msg_len UNUSED_PARAM)
  211. {
  212. if (!send_ok_) {
  213. isc_throw(XfroutError, "xfrout connection send is disabled for test");
  214. }
  215. return (0);
  216. }
  217. // These are flags to indicate whether the corresponding flag bit of the
  218. // DNS header is to be set in the test cases. (Note that the flag values
  219. // is irrelevant to their wire-format values)
  220. const unsigned int QR_FLAG = 0x1;
  221. const unsigned int AA_FLAG = 0x2;
  222. const unsigned int TC_FLAG = 0x4;
  223. const unsigned int RD_FLAG = 0x8;
  224. const unsigned int RA_FLAG = 0x10;
  225. const unsigned int AD_FLAG = 0x20;
  226. const unsigned int CD_FLAG = 0x40;
  227. void
  228. AuthSrvTest::createDataFromFile(const char* const datafile,
  229. const int protocol = IPPROTO_UDP)
  230. {
  231. delete io_message;
  232. data.clear();
  233. delete endpoint;
  234. endpoint = IOEndpoint::create(protocol,
  235. IOAddress(DEFAULT_REMOTE_ADDRESS), 5300);
  236. UnitTestUtil::readWireData(datafile, data);
  237. io_message = new IOMessage(&data[0], data.size(),
  238. protocol == IPPROTO_UDP ?
  239. IOSocket::getDummyUDPSocket() :
  240. IOSocket::getDummyTCPSocket(), *endpoint);
  241. }
  242. void
  243. AuthSrvTest::createRequestMessage(const Opcode& opcode,
  244. const Name& request_name,
  245. const RRClass& rrclass,
  246. const RRType& rrtype)
  247. {
  248. request_message.clear(Message::RENDER);
  249. request_message.setOpcode(opcode);
  250. request_message.setRcode(Rcode::NOERROR());
  251. request_message.setQid(default_qid);
  252. request_message.addQuestion(Question(request_name, rrclass, rrtype));
  253. }
  254. void
  255. AuthSrvTest::createRequestPacket(const Opcode& opcode,
  256. const Name& request_name,
  257. const RRClass& rrclass, const RRType& rrtype,
  258. const int protocol = IPPROTO_UDP)
  259. {
  260. createRequestMessage(opcode, request_name, rrclass, rrtype);
  261. createRequestPacket(protocol);
  262. }
  263. void
  264. AuthSrvTest::createRequestPacket(const int protocol = IPPROTO_UDP) {
  265. request_message.toWire(request_renderer);
  266. delete io_message;
  267. endpoint = IOEndpoint::create(protocol,
  268. IOAddress(DEFAULT_REMOTE_ADDRESS), 5300);
  269. io_message = new IOMessage(request_renderer.getData(),
  270. request_renderer.getLength(),
  271. protocol == IPPROTO_UDP ?
  272. IOSocket::getDummyUDPSocket() :
  273. IOSocket::getDummyTCPSocket(), *endpoint);
  274. }
  275. void
  276. headerCheck(const Message& message, const qid_t qid, const Rcode& rcode,
  277. const uint16_t opcodeval, const unsigned int flags,
  278. const unsigned int qdcount,
  279. const unsigned int ancount, const unsigned int nscount,
  280. const unsigned int arcount)
  281. {
  282. EXPECT_EQ(qid, message.getQid());
  283. EXPECT_EQ(rcode, message.getRcode());
  284. EXPECT_EQ(opcodeval, message.getOpcode().getCode());
  285. EXPECT_EQ((flags & QR_FLAG) != 0, message.getHeaderFlag(MessageFlag::QR()));
  286. EXPECT_EQ((flags & AA_FLAG) != 0, message.getHeaderFlag(MessageFlag::AA()));
  287. EXPECT_EQ((flags & TC_FLAG) != 0, message.getHeaderFlag(MessageFlag::TC()));
  288. EXPECT_EQ((flags & RA_FLAG) != 0, message.getHeaderFlag(MessageFlag::RA()));
  289. EXPECT_EQ((flags & RD_FLAG) != 0, message.getHeaderFlag(MessageFlag::RD()));
  290. EXPECT_EQ((flags & AD_FLAG) != 0, message.getHeaderFlag(MessageFlag::AD()));
  291. EXPECT_EQ((flags & CD_FLAG) != 0, message.getHeaderFlag(MessageFlag::CD()));
  292. EXPECT_EQ(qdcount, message.getRRCount(Section::QUESTION()));
  293. EXPECT_EQ(ancount, message.getRRCount(Section::ANSWER()));
  294. EXPECT_EQ(nscount, message.getRRCount(Section::AUTHORITY()));
  295. EXPECT_EQ(arcount, message.getRRCount(Section::ADDITIONAL()));
  296. }
  297. // Unsupported requests. Should result in NOTIMP.
  298. TEST_F(AuthSrvTest, unsupportedRequest) {
  299. for (unsigned int i = 0; i < 16; ++i) {
  300. // set Opcode to 'i', which iterators over all possible codes except
  301. // the standard query and notify
  302. if (i == Opcode::QUERY().getCode() ||
  303. i == Opcode::NOTIFY().getCode()) {
  304. continue;
  305. }
  306. createDataFromFile("simplequery_fromWire");
  307. data[2] = ((i << 3) & 0xff);
  308. parse_message.clear(Message::PARSE);
  309. EXPECT_EQ(true, server.processMessage(*io_message, parse_message,
  310. response_renderer));
  311. headerCheck(parse_message, default_qid, Rcode::NOTIMP(), i, QR_FLAG,
  312. 0, 0, 0, 0);
  313. }
  314. }
  315. // Simple API check
  316. TEST_F(AuthSrvTest, verbose) {
  317. EXPECT_FALSE(server.getVerbose());
  318. server.setVerbose(true);
  319. EXPECT_TRUE(server.getVerbose());
  320. server.setVerbose(false);
  321. EXPECT_FALSE(server.getVerbose());
  322. }
  323. // Multiple questions. Should result in FORMERR.
  324. TEST_F(AuthSrvTest, multiQuestion) {
  325. createDataFromFile("multiquestion_fromWire");
  326. EXPECT_EQ(true, server.processMessage(*io_message, parse_message,
  327. response_renderer));
  328. headerCheck(parse_message, default_qid, Rcode::FORMERR(), opcode.getCode(),
  329. QR_FLAG, 2, 0, 0, 0);
  330. QuestionIterator qit = parse_message.beginQuestion();
  331. EXPECT_EQ(Name("example.com"), (*qit)->getName());
  332. EXPECT_EQ(RRClass::IN(), (*qit)->getClass());
  333. EXPECT_EQ(RRType::A(), (*qit)->getType());
  334. ++qit;
  335. EXPECT_EQ(Name("example.com"), (*qit)->getName());
  336. EXPECT_EQ(RRClass::IN(), (*qit)->getClass());
  337. EXPECT_EQ(RRType::AAAA(), (*qit)->getType());
  338. ++qit;
  339. EXPECT_TRUE(qit == parse_message.endQuestion());
  340. }
  341. // Incoming data doesn't even contain the complete header. Must be silently
  342. // dropped.
  343. TEST_F(AuthSrvTest, shortMessage) {
  344. createDataFromFile("shortmessage_fromWire");
  345. EXPECT_EQ(false, server.processMessage(*io_message, parse_message,
  346. response_renderer));
  347. }
  348. // Response messages. Must be silently dropped, whether it's a valid response
  349. // or malformed or could otherwise cause a protocol error.
  350. TEST_F(AuthSrvTest, response) {
  351. // A valid (although unusual) response
  352. createDataFromFile("simpleresponse_fromWire");
  353. EXPECT_EQ(false, server.processMessage(*io_message, parse_message,
  354. response_renderer));
  355. // A response with a broken question section. must be dropped rather than
  356. // returning FORMERR.
  357. createDataFromFile("shortresponse_fromWire");
  358. EXPECT_EQ(false, server.processMessage(*io_message, parse_message,
  359. response_renderer));
  360. // A response to iquery. must be dropped rather than returning NOTIMP.
  361. createDataFromFile("iqueryresponse_fromWire");
  362. EXPECT_EQ(false, server.processMessage(*io_message, parse_message,
  363. response_renderer));
  364. }
  365. // Query with a broken question
  366. TEST_F(AuthSrvTest, shortQuestion) {
  367. createDataFromFile("shortquestion_fromWire");
  368. EXPECT_EQ(true, server.processMessage(*io_message, parse_message,
  369. response_renderer));
  370. // Since the query's question is broken, the question section of the
  371. // response should be empty.
  372. headerCheck(parse_message, default_qid, Rcode::FORMERR(), opcode.getCode(),
  373. QR_FLAG, 0, 0, 0, 0);
  374. }
  375. // Query with a broken answer section
  376. TEST_F(AuthSrvTest, shortAnswer) {
  377. createDataFromFile("shortanswer_fromWire");
  378. EXPECT_EQ(true, server.processMessage(*io_message, parse_message,
  379. response_renderer));
  380. // This is a bogus query, but question section is valid. So the response
  381. // should copy the question section.
  382. headerCheck(parse_message, default_qid, Rcode::FORMERR(), opcode.getCode(),
  383. QR_FLAG, 1, 0, 0, 0);
  384. QuestionIterator qit = parse_message.beginQuestion();
  385. EXPECT_EQ(Name("example.com"), (*qit)->getName());
  386. EXPECT_EQ(RRClass::IN(), (*qit)->getClass());
  387. EXPECT_EQ(RRType::A(), (*qit)->getType());
  388. ++qit;
  389. EXPECT_TRUE(qit == parse_message.endQuestion());
  390. }
  391. // Query with unsupported version of EDNS.
  392. TEST_F(AuthSrvTest, ednsBadVers) {
  393. createDataFromFile("queryBadEDNS_fromWire");
  394. EXPECT_EQ(true, server.processMessage(*io_message, parse_message,
  395. response_renderer));
  396. // The response must have an EDNS OPT RR in the additional section.
  397. // Note that the DNSSEC DO bit is cleared even if this bit in the query
  398. // is set. This is a limitation of the current implementation.
  399. headerCheck(parse_message, default_qid, Rcode::BADVERS(), opcode.getCode(),
  400. QR_FLAG, 1, 0, 0, 1);
  401. EXPECT_EQ(4096, parse_message.getUDPSize());
  402. EXPECT_FALSE(parse_message.isDNSSECSupported());
  403. }
  404. TEST_F(AuthSrvTest, AXFROverUDP) {
  405. // AXFR over UDP is invalid and should result in FORMERR.
  406. createRequestPacket(opcode, Name("example.com"), RRClass::IN(),
  407. RRType::AXFR(), IPPROTO_UDP);
  408. EXPECT_EQ(true, server.processMessage(*io_message, parse_message,
  409. response_renderer));
  410. headerCheck(parse_message, default_qid, Rcode::FORMERR(), opcode.getCode(),
  411. QR_FLAG, 1, 0, 0, 0);
  412. }
  413. TEST_F(AuthSrvTest, AXFRSuccess) {
  414. EXPECT_FALSE(xfrout.isConnected());
  415. createRequestPacket(opcode, Name("example.com"), RRClass::IN(),
  416. RRType::AXFR(), IPPROTO_TCP);
  417. // On success, the AXFR query has been passed to a separate process,
  418. // so we shouldn't have to respond.
  419. EXPECT_EQ(false, server.processMessage(*io_message, parse_message,
  420. response_renderer));
  421. EXPECT_FALSE(xfrout.isConnected());
  422. }
  423. TEST_F(AuthSrvTest, AXFRConnectFail) {
  424. EXPECT_FALSE(xfrout.isConnected()); // check prerequisite
  425. xfrout.disableConnect();
  426. createRequestPacket(opcode, Name("example.com"), RRClass::IN(),
  427. RRType::AXFR(), IPPROTO_TCP);
  428. EXPECT_TRUE(server.processMessage(*io_message, parse_message,
  429. response_renderer));
  430. headerCheck(parse_message, default_qid, Rcode::SERVFAIL(),
  431. opcode.getCode(), QR_FLAG, 1, 0, 0, 0);
  432. // For a shot term workaround with xfrout we currently close the connection
  433. // for each AXFR attempt
  434. EXPECT_FALSE(xfrout.isConnected());
  435. }
  436. TEST_F(AuthSrvTest, AXFRSendFail) {
  437. // first send a valid query, making the connection with the xfr process
  438. // open.
  439. createRequestPacket(opcode, Name("example.com"), RRClass::IN(),
  440. RRType::AXFR(), IPPROTO_TCP);
  441. server.processMessage(*io_message, parse_message, response_renderer);
  442. EXPECT_FALSE(xfrout.isConnected()); // see above
  443. xfrout.disableSend();
  444. parse_message.clear(Message::PARSE);
  445. response_renderer.clear();
  446. createRequestPacket(opcode, Name("example.com"), RRClass::IN(),
  447. RRType::AXFR(), IPPROTO_TCP);
  448. EXPECT_TRUE(server.processMessage(*io_message, parse_message,
  449. response_renderer));
  450. headerCheck(parse_message, default_qid, Rcode::SERVFAIL(),
  451. opcode.getCode(), QR_FLAG, 1, 0, 0, 0);
  452. // The connection should have been closed due to the send failure.
  453. EXPECT_FALSE(xfrout.isConnected());
  454. }
  455. TEST_F(AuthSrvTest, AXFRDisconnectFail) {
  456. // In our usage disconnect() shouldn't fail. So we'll see the exception
  457. // should it be thrown.
  458. xfrout.disableSend();
  459. xfrout.disableDisconnect();
  460. createRequestPacket(opcode, Name("example.com"), RRClass::IN(),
  461. RRType::AXFR(), IPPROTO_TCP);
  462. EXPECT_THROW(server.processMessage(*io_message, parse_message,
  463. response_renderer),
  464. XfroutError);
  465. EXPECT_TRUE(xfrout.isConnected());
  466. // XXX: we need to re-enable disconnect. otherwise an exception would be
  467. // thrown via the destructor of the server.
  468. xfrout.enableDisconnect();
  469. }
  470. TEST_F(AuthSrvTest, notify) {
  471. createRequestMessage(Opcode::NOTIFY(), Name("example.com"), RRClass::IN(),
  472. RRType::SOA());
  473. request_message.setHeaderFlag(MessageFlag::AA());
  474. createRequestPacket(IPPROTO_UDP);
  475. EXPECT_EQ(true, server.processMessage(*io_message, parse_message,
  476. response_renderer));
  477. // An internal command message should have been created and sent to an
  478. // external module. Check them.
  479. EXPECT_EQ("Zonemgr", notify_session.msg_destination);
  480. EXPECT_EQ("notify",
  481. notify_session.sent_msg->get("command")->get(0)->stringValue());
  482. ConstElementPtr notify_args =
  483. notify_session.sent_msg->get("command")->get(1);
  484. EXPECT_EQ("example.com.", notify_args->get("zone_name")->stringValue());
  485. EXPECT_EQ(DEFAULT_REMOTE_ADDRESS,
  486. notify_args->get("master")->stringValue());
  487. EXPECT_EQ("IN", notify_args->get("zone_class")->stringValue());
  488. // On success, the server should return a response to the notify.
  489. headerCheck(parse_message, default_qid, Rcode::NOERROR(),
  490. Opcode::NOTIFY().getCode(), QR_FLAG | AA_FLAG, 1, 0, 0, 0);
  491. // The question must be identical to that of the received notify
  492. ConstQuestionPtr question = *parse_message.beginQuestion();
  493. EXPECT_EQ(Name("example.com"), question->getName());
  494. EXPECT_EQ(RRClass::IN(), question->getClass());
  495. EXPECT_EQ(RRType::SOA(), question->getType());
  496. }
  497. TEST_F(AuthSrvTest, notifyForCHClass) {
  498. // Same as the previous test, but for the CH RRClass.
  499. createRequestMessage(Opcode::NOTIFY(), Name("example.com"), RRClass::CH(),
  500. RRType::SOA());
  501. request_message.setHeaderFlag(MessageFlag::AA());
  502. createRequestPacket(IPPROTO_UDP);
  503. EXPECT_EQ(true, server.processMessage(*io_message, parse_message,
  504. response_renderer));
  505. // Other conditions should be the same, so simply confirm the RR class is
  506. // set correctly.
  507. ConstElementPtr notify_args =
  508. notify_session.sent_msg->get("command")->get(1);
  509. EXPECT_EQ("CH", notify_args->get("zone_class")->stringValue());
  510. }
  511. TEST_F(AuthSrvTest, notifyEmptyQuestion) {
  512. request_message.clear(Message::RENDER);
  513. request_message.setOpcode(Opcode::NOTIFY());
  514. request_message.setRcode(Rcode::NOERROR());
  515. request_message.setHeaderFlag(MessageFlag::AA());
  516. request_message.setQid(default_qid);
  517. request_message.toWire(request_renderer);
  518. createRequestPacket(IPPROTO_UDP);
  519. EXPECT_EQ(true, server.processMessage(*io_message, parse_message,
  520. response_renderer));
  521. headerCheck(parse_message, default_qid, Rcode::FORMERR(),
  522. Opcode::NOTIFY().getCode(), QR_FLAG, 0, 0, 0, 0);
  523. }
  524. TEST_F(AuthSrvTest, notifyMultiQuestions) {
  525. createRequestMessage(Opcode::NOTIFY(), Name("example.com"), RRClass::IN(),
  526. RRType::SOA());
  527. // add one more SOA question
  528. request_message.addQuestion(Question(Name("example.com"), RRClass::IN(),
  529. RRType::SOA()));
  530. request_message.setHeaderFlag(MessageFlag::AA());
  531. createRequestPacket(IPPROTO_UDP);
  532. EXPECT_EQ(true, server.processMessage(*io_message, parse_message,
  533. response_renderer));
  534. headerCheck(parse_message, default_qid, Rcode::FORMERR(),
  535. Opcode::NOTIFY().getCode(), QR_FLAG, 2, 0, 0, 0);
  536. }
  537. TEST_F(AuthSrvTest, notifyNonSOAQuestion) {
  538. createRequestMessage(Opcode::NOTIFY(), Name("example.com"), RRClass::IN(),
  539. RRType::NS());
  540. request_message.setHeaderFlag(MessageFlag::AA());
  541. createRequestPacket(IPPROTO_UDP);
  542. EXPECT_EQ(true, server.processMessage(*io_message, parse_message,
  543. response_renderer));
  544. headerCheck(parse_message, default_qid, Rcode::FORMERR(),
  545. Opcode::NOTIFY().getCode(), QR_FLAG, 1, 0, 0, 0);
  546. }
  547. TEST_F(AuthSrvTest, notifyWithoutAA) {
  548. // implicitly leave the AA bit off. our implementation will accept it.
  549. createRequestPacket(Opcode::NOTIFY(), Name("example.com"), RRClass::IN(),
  550. RRType::SOA());
  551. EXPECT_EQ(true, server.processMessage(*io_message, parse_message,
  552. response_renderer));
  553. headerCheck(parse_message, default_qid, Rcode::NOERROR(),
  554. Opcode::NOTIFY().getCode(), QR_FLAG | AA_FLAG, 1, 0, 0, 0);
  555. }
  556. TEST_F(AuthSrvTest, notifyWithErrorRcode) {
  557. createRequestMessage(Opcode::NOTIFY(), Name("example.com"), RRClass::IN(),
  558. RRType::SOA());
  559. request_message.setHeaderFlag(MessageFlag::AA());
  560. request_message.setRcode(Rcode::SERVFAIL());
  561. createRequestPacket(IPPROTO_UDP);
  562. EXPECT_EQ(true, server.processMessage(*io_message, parse_message,
  563. response_renderer));
  564. headerCheck(parse_message, default_qid, Rcode::NOERROR(),
  565. Opcode::NOTIFY().getCode(), QR_FLAG | AA_FLAG, 1, 0, 0, 0);
  566. }
  567. TEST_F(AuthSrvTest, notifyWithoutSession) {
  568. server.setXfrinSession(NULL);
  569. createRequestMessage(Opcode::NOTIFY(), Name("example.com"), RRClass::IN(),
  570. RRType::SOA());
  571. request_message.setHeaderFlag(MessageFlag::AA());
  572. createRequestPacket(IPPROTO_UDP);
  573. // we simply ignore the notify and let it be resent if an internal error
  574. // happens.
  575. EXPECT_FALSE(server.processMessage(*io_message, parse_message,
  576. response_renderer));
  577. }
  578. TEST_F(AuthSrvTest, notifySendFail) {
  579. notify_session.disableSend();
  580. createRequestMessage(Opcode::NOTIFY(), Name("example.com"), RRClass::IN(),
  581. RRType::SOA());
  582. request_message.setHeaderFlag(MessageFlag::AA());
  583. createRequestPacket(IPPROTO_UDP);
  584. EXPECT_FALSE(server.processMessage(*io_message, parse_message,
  585. response_renderer));
  586. }
  587. TEST_F(AuthSrvTest, notifyReceiveFail) {
  588. notify_session.disableReceive();
  589. createRequestMessage(Opcode::NOTIFY(), Name("example.com"), RRClass::IN(),
  590. RRType::SOA());
  591. request_message.setHeaderFlag(MessageFlag::AA());
  592. createRequestPacket(IPPROTO_UDP);
  593. EXPECT_FALSE(server.processMessage(*io_message, parse_message,
  594. response_renderer));
  595. }
  596. TEST_F(AuthSrvTest, notifyWithBogusSessionMessage) {
  597. notify_session.setMessage(Element::fromJSON("{\"foo\": 1}"));
  598. createRequestMessage(Opcode::NOTIFY(), Name("example.com"), RRClass::IN(),
  599. RRType::SOA());
  600. request_message.setHeaderFlag(MessageFlag::AA());
  601. createRequestPacket(IPPROTO_UDP);
  602. EXPECT_FALSE(server.processMessage(*io_message, parse_message,
  603. response_renderer));
  604. }
  605. TEST_F(AuthSrvTest, notifyWithSessionMessageError) {
  606. notify_session.setMessage(
  607. Element::fromJSON("{\"result\": [1, \"FAIL\"]}"));
  608. createRequestMessage(Opcode::NOTIFY(), Name("example.com"), RRClass::IN(),
  609. RRType::SOA());
  610. request_message.setHeaderFlag(MessageFlag::AA());
  611. createRequestPacket(IPPROTO_UDP);
  612. EXPECT_FALSE(server.processMessage(*io_message, parse_message,
  613. response_renderer));
  614. }
  615. void
  616. updateConfig(AuthSrv* server, const char* const dbfile,
  617. const bool expect_success)
  618. {
  619. ConstElementPtr config_answer =
  620. server->updateConfig(Element::fromJSON(dbfile));
  621. EXPECT_EQ(Element::map, config_answer->getType());
  622. EXPECT_TRUE(config_answer->contains("result"));
  623. ConstElementPtr result = config_answer->get("result");
  624. EXPECT_EQ(Element::list, result->getType());
  625. EXPECT_EQ(expect_success ? 0 : 1, result->get(0)->intValue());
  626. }
  627. // Install a Sqlite3 data source with testing data.
  628. TEST_F(AuthSrvTest, updateConfig) {
  629. updateConfig(&server, CONFIG_TESTDB, true);
  630. // query for existent data in the installed data source. The resulting
  631. // response should have the AA flag on, and have an RR in each answer
  632. // and authority section.
  633. createDataFromFile("examplequery_fromWire");
  634. EXPECT_EQ(true, server.processMessage(*io_message, parse_message,
  635. response_renderer));
  636. headerCheck(parse_message, default_qid, Rcode::NOERROR(), opcode.getCode(),
  637. QR_FLAG | AA_FLAG, 1, 1, 1, 0);
  638. }
  639. TEST_F(AuthSrvTest, datasourceFail) {
  640. updateConfig(&server, CONFIG_TESTDB, true);
  641. // This query will hit a corrupted entry of the data source (the zoneload
  642. // tool and the data source itself naively accept it). This will result
  643. // in a SERVFAIL response, and the answer and authority sections should
  644. // be empty.
  645. createDataFromFile("badExampleQuery_fromWire");
  646. EXPECT_EQ(true, server.processMessage(*io_message, parse_message,
  647. response_renderer));
  648. headerCheck(parse_message, default_qid, Rcode::SERVFAIL(),
  649. opcode.getCode(), QR_FLAG, 1, 0, 0, 0);
  650. }
  651. TEST_F(AuthSrvTest, updateConfigFail) {
  652. // First, load a valid data source.
  653. updateConfig(&server, CONFIG_TESTDB, true);
  654. // Next, try to update it with a non-existent one. This should fail.
  655. updateConfig(&server, BADCONFIG_TESTDB, false);
  656. // The original data source should still exist.
  657. createDataFromFile("examplequery_fromWire");
  658. EXPECT_EQ(true, server.processMessage(*io_message, parse_message,
  659. response_renderer));
  660. headerCheck(parse_message, default_qid, Rcode::NOERROR(), opcode.getCode(),
  661. QR_FLAG | AA_FLAG, 1, 1, 1, 0);
  662. }
  663. TEST_F(AuthSrvTest, cacheSlots) {
  664. // simple check for the get/set operations
  665. server.setCacheSlots(10); // 10 = arbitrary choice
  666. EXPECT_EQ(10, server.getCacheSlots());
  667. // 0 is a valid size
  668. server.setCacheSlots(0);
  669. EXPECT_EQ(00, server.getCacheSlots());
  670. }
  671. }