database_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // Copyright (C) 2011 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. #include <gtest/gtest.h>
  15. #include <dns/name.h>
  16. #include <dns/rrttl.h>
  17. #include <exceptions/exceptions.h>
  18. #include <datasrc/database.h>
  19. #include <datasrc/data_source.h>
  20. #include <datasrc/iterator.h>
  21. using namespace isc::datasrc;
  22. using namespace std;
  23. using namespace boost;
  24. using namespace isc::dns;
  25. namespace {
  26. /*
  27. * A connection with minimum implementation, keeping the original
  28. * "NotImplemented" methods.
  29. */
  30. class NopConnection : public DatabaseConnection {
  31. public:
  32. virtual std::pair<bool, int> getZone(const Name& name) const {
  33. if (name == Name("example.org")) {
  34. return (std::pair<bool, int>(true, 42));
  35. } else if (name == Name("null.example.org")) {
  36. return (std::pair<bool, int>(true, 13));
  37. } else if (name == Name("empty.example.org")) {
  38. return (std::pair<bool, int>(true, 0));
  39. } else if (name == Name("bad.example.org")) {
  40. return (std::pair<bool, int>(true, -1));
  41. } else {
  42. return (std::pair<bool, int>(false, 0));
  43. }
  44. }
  45. };
  46. /*
  47. * A virtual database connection that pretends it contains single zone --
  48. * example.org.
  49. *
  50. * It has the same getZone method as NopConnection, but it provides
  51. * implementation of the optional functionality.
  52. */
  53. class MockConnection : public NopConnection {
  54. private:
  55. class MockIteratorContext : public IteratorContext {
  56. private:
  57. int step;
  58. public:
  59. MockIteratorContext() :
  60. step(0)
  61. { }
  62. virtual bool getNext(string& name, string& rtype, int& ttl,
  63. string& data)
  64. {
  65. switch (step ++) {
  66. case 0:
  67. name = "example.org";
  68. rtype = "SOA";
  69. ttl = 300;
  70. data = "ns1.example.org. admin.example.org. "
  71. "1234 3600 1800 2419200 7200";
  72. return (true);
  73. case 1:
  74. name = "x.example.org";
  75. rtype = "A";
  76. ttl = 300;
  77. data = "192.0.2.1";
  78. return (true);
  79. case 2:
  80. name = "x.example.org";
  81. rtype = "A";
  82. ttl = 300;
  83. data = "192.0.2.2";
  84. return (true);
  85. case 3:
  86. name = "x.example.org";
  87. rtype = "AAAA";
  88. ttl = 300;
  89. data = "2001:db8::1";
  90. return (true);
  91. case 4:
  92. name = "x.example.org";
  93. rtype = "AAAA";
  94. ttl = 300;
  95. data = "2001:db8::2";
  96. return (true);
  97. default:
  98. ADD_FAILURE() <<
  99. "Request past the end of iterator context";
  100. case 5:
  101. return (false);
  102. }
  103. }
  104. };
  105. class EmptyIteratorContext : public IteratorContext {
  106. public:
  107. virtual bool getNext(string&, string&, int&, string&) {
  108. return (false);
  109. }
  110. };
  111. class BadIteratorContext : public IteratorContext {
  112. private:
  113. int step;
  114. public:
  115. BadIteratorContext() :
  116. step(0)
  117. { }
  118. virtual bool getNext(string& name, string& rtype, int& ttl,
  119. string& data)
  120. {
  121. switch (step ++) {
  122. case 0:
  123. name = "x.example.org";
  124. rtype = "A";
  125. ttl = 300;
  126. data = "192.0.2.1";
  127. return (true);
  128. case 1:
  129. name = "x.example.org";
  130. rtype = "A";
  131. ttl = 301;
  132. data = "192.0.2.2";
  133. return (true);
  134. default:
  135. ADD_FAILURE() <<
  136. "Request past the end of iterator context";
  137. case 2:
  138. return (false);
  139. }
  140. }
  141. };
  142. public:
  143. virtual IteratorContextPtr getIteratorContext(const Name&, int id) const {
  144. if (id == 42) {
  145. return (IteratorContextPtr(new MockIteratorContext()));
  146. } else if (id == 13) {
  147. return (IteratorContextPtr());
  148. } else if (id == 0) {
  149. return (IteratorContextPtr(new EmptyIteratorContext()));
  150. } else if (id == -1) {
  151. return (IteratorContextPtr(new BadIteratorContext()));
  152. } else {
  153. isc_throw(isc::Unexpected, "Unknown zone ID");
  154. }
  155. }
  156. };
  157. // This tests the default getIteratorContext behaviour, throwing NotImplemented
  158. TEST(DatabaseConnectionTest, getIteratorContext) {
  159. // The parameters don't matter
  160. EXPECT_THROW(NopConnection().getIteratorContext(Name("."), 1),
  161. isc::NotImplemented);
  162. }
  163. class DatabaseClientTest : public ::testing::Test {
  164. public:
  165. DatabaseClientTest() {
  166. createClient();
  167. }
  168. /*
  169. * We initialize the client from a function, so we can call it multiple
  170. * times per test.
  171. */
  172. void createClient() {
  173. current_connection_ = new MockConnection();
  174. client_.reset(new DatabaseClient(auto_ptr<DatabaseConnection>(
  175. current_connection_)));
  176. }
  177. // Will be deleted by client_, just keep the current value for comparison.
  178. MockConnection* current_connection_;
  179. auto_ptr<DatabaseClient> client_;
  180. /**
  181. * Check the zone finder is a valid one and references the zone ID and
  182. * connection available here.
  183. */
  184. void checkZoneFinder(const DataSourceClient::FindResult& zone) {
  185. ASSERT_NE(ZoneFinderPtr(), zone.zone_finder) << "No zone finder";
  186. shared_ptr<DatabaseClient::Finder> finder(
  187. dynamic_pointer_cast<DatabaseClient::Finder>(zone.zone_finder));
  188. ASSERT_NE(shared_ptr<DatabaseClient::Finder>(), finder) <<
  189. "Wrong type of finder";
  190. EXPECT_EQ(42, finder->zone_id());
  191. EXPECT_EQ(current_connection_, &finder->connection());
  192. }
  193. };
  194. TEST_F(DatabaseClientTest, zoneNotFound) {
  195. DataSourceClient::FindResult zone(client_->findZone(Name("example.com")));
  196. EXPECT_EQ(result::NOTFOUND, zone.code);
  197. }
  198. TEST_F(DatabaseClientTest, exactZone) {
  199. DataSourceClient::FindResult zone(client_->findZone(Name("example.org")));
  200. EXPECT_EQ(result::SUCCESS, zone.code);
  201. checkZoneFinder(zone);
  202. }
  203. TEST_F(DatabaseClientTest, superZone) {
  204. DataSourceClient::FindResult zone(client_->findZone(Name(
  205. "sub.example.org")));
  206. EXPECT_EQ(result::PARTIALMATCH, zone.code);
  207. checkZoneFinder(zone);
  208. }
  209. TEST_F(DatabaseClientTest, noConnException) {
  210. EXPECT_THROW(DatabaseClient(auto_ptr<DatabaseConnection>()),
  211. isc::InvalidParameter);
  212. }
  213. // If the zone doesn't exist, exception is thrown
  214. TEST_F(DatabaseClientTest, noZoneIterator) {
  215. EXPECT_THROW(client_->getIterator(Name("example.com")), DataSourceError);
  216. }
  217. // If the zone doesn't exist and iteration is not implemented, it still throws
  218. // the exception it doesn't exist
  219. TEST_F(DatabaseClientTest, noZoneNotImplementedIterator) {
  220. EXPECT_THROW(DatabaseClient(auto_ptr<DatabaseConnection>(
  221. new NopConnection())).getIterator(Name("example.com")),
  222. DataSourceError);
  223. }
  224. TEST_F(DatabaseClientTest, notImplementedIterator) {
  225. EXPECT_THROW(DatabaseClient(auto_ptr<DatabaseConnection>(
  226. new NopConnection())).getIterator(Name("example.org")),
  227. isc::NotImplemented);
  228. }
  229. // Pretend a bug in the connection and pass NULL as the context
  230. // Should not crash, but gracefully throw
  231. TEST_F(DatabaseClientTest, nullIteratorContext) {
  232. EXPECT_THROW(client_->getIterator(Name("null.example.org")),
  233. isc::Unexpected);
  234. }
  235. // It doesn't crash or anything if the zone is completely empty
  236. TEST_F(DatabaseClientTest, emptyIterator) {
  237. ZoneIteratorPtr it(client_->getIterator(Name("empty.example.org")));
  238. EXPECT_EQ(ConstRRsetPtr(), it->getNextRRset());
  239. // This is past the end, it should throw
  240. EXPECT_THROW(it->getNextRRset(), isc::Unexpected);
  241. }
  242. // Iterate trough a zone
  243. TEST_F(DatabaseClientTest, iterator) {
  244. ZoneIteratorPtr it(client_->getIterator(Name("example.org")));
  245. ConstRRsetPtr rrset(it->getNextRRset());
  246. ASSERT_NE(ConstRRsetPtr(), rrset);
  247. EXPECT_EQ(Name("example.org"), rrset->getName());
  248. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  249. EXPECT_EQ(RRType::SOA(), rrset->getType());
  250. EXPECT_EQ(RRTTL(300), rrset->getTTL());
  251. RdataIteratorPtr rit(rrset->getRdataIterator());
  252. ASSERT_FALSE(rit->isLast());
  253. rit->next();
  254. EXPECT_TRUE(rit->isLast());
  255. rrset = it->getNextRRset();
  256. ASSERT_NE(ConstRRsetPtr(), rrset);
  257. EXPECT_EQ(Name("x.example.org"), rrset->getName());
  258. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  259. EXPECT_EQ(RRType::A(), rrset->getType());
  260. EXPECT_EQ(RRTTL(300), rrset->getTTL());
  261. rit = rrset->getRdataIterator();
  262. ASSERT_FALSE(rit->isLast());
  263. EXPECT_EQ("192.0.2.1", rit->getCurrent().toText());
  264. rit->next();
  265. ASSERT_FALSE(rit->isLast());
  266. EXPECT_EQ("192.0.2.2", rit->getCurrent().toText());
  267. rit->next();
  268. EXPECT_TRUE(rit->isLast());
  269. rrset = it->getNextRRset();
  270. ASSERT_NE(ConstRRsetPtr(), rrset);
  271. EXPECT_EQ(Name("x.example.org"), rrset->getName());
  272. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  273. EXPECT_EQ(RRType::AAAA(), rrset->getType());
  274. EXPECT_EQ(RRTTL(300), rrset->getTTL());
  275. EXPECT_EQ(ConstRRsetPtr(), it->getNextRRset());
  276. rit = rrset->getRdataIterator();
  277. ASSERT_FALSE(rit->isLast());
  278. EXPECT_EQ("2001:db8::1", rit->getCurrent().toText());
  279. rit->next();
  280. ASSERT_FALSE(rit->isLast());
  281. EXPECT_EQ("2001:db8::2", rit->getCurrent().toText());
  282. rit->next();
  283. EXPECT_TRUE(rit->isLast());
  284. }
  285. // This has inconsistent TTL in the set (the rest, like nonsense in
  286. // the data is handled in rdata itself).
  287. TEST_F(DatabaseClientTest, badIterator) {
  288. ZoneIteratorPtr it(client_->getIterator(Name("bad.example.org")));
  289. EXPECT_THROW(it->getNextRRset(), DataSourceError);
  290. }
  291. }