sqlite3_accessor_unittest.cc 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  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 <algorithm>
  15. #include <vector>
  16. #include <datasrc/sqlite3_accessor.h>
  17. #include <datasrc/data_source.h>
  18. #include <dns/rrclass.h>
  19. #include <gtest/gtest.h>
  20. #include <boost/lexical_cast.hpp>
  21. #include <boost/scoped_ptr.hpp>
  22. #include <fstream>
  23. #include <sqlite3.h>
  24. using namespace std;
  25. using namespace isc::datasrc;
  26. using boost::lexical_cast;
  27. using isc::data::ConstElementPtr;
  28. using isc::data::Element;
  29. using isc::dns::RRClass;
  30. using isc::dns::Name;
  31. namespace {
  32. // Some test data
  33. std::string SQLITE_DBFILE_EXAMPLE = TEST_DATA_DIR "/test.sqlite3";
  34. std::string SQLITE_DBFILE_EXAMPLE2 = TEST_DATA_DIR "/example2.com.sqlite3";
  35. std::string SQLITE_DBNAME_EXAMPLE2 = "sqlite3_example2.com.sqlite3";
  36. std::string SQLITE_DBFILE_EXAMPLE_ROOT = TEST_DATA_DIR "/test-root.sqlite3";
  37. std::string SQLITE_DBNAME_EXAMPLE_ROOT = "sqlite3_test-root.sqlite3";
  38. std::string SQLITE_DBFILE_BROKENDB = TEST_DATA_DIR "/brokendb.sqlite3";
  39. std::string SQLITE_DBFILE_MEMORY = ":memory:";
  40. std::string SQLITE_DBFILE_EXAMPLE_ORG = TEST_DATA_DIR "/example.org.sqlite3";
  41. std::string SQLITE_DBFILE_DIFFS = TEST_DATA_DIR "/diffs.sqlite3";
  42. // The following file must be non existent and must be non"creatable";
  43. // the sqlite3 library will try to create a new DB file if it doesn't exist,
  44. // so to test a failure case the create operation should also fail.
  45. // The "nodir", a non existent directory, is inserted for this purpose.
  46. std::string SQLITE_DBFILE_NOTEXIST = TEST_DATA_DIR "/nodir/notexist";
  47. // new db file, we don't need this to be a std::string, and given the
  48. // raw calls we use it in a const char* is more convenient
  49. const char* SQLITE_NEW_DBFILE = TEST_DATA_BUILDDIR "/newdb.sqlite3";
  50. // Opening works (the content is tested in different tests)
  51. TEST(SQLite3Open, common) {
  52. EXPECT_NO_THROW(SQLite3Accessor accessor(SQLITE_DBFILE_EXAMPLE, "IN"));
  53. }
  54. // The file can't be opened
  55. TEST(SQLite3Open, notExist) {
  56. EXPECT_THROW(SQLite3Accessor accessor(SQLITE_DBFILE_NOTEXIST, "IN"),
  57. SQLite3Error);
  58. }
  59. // It rejects broken DB
  60. TEST(SQLite3Open, brokenDB) {
  61. EXPECT_THROW(SQLite3Accessor accessor(SQLITE_DBFILE_BROKENDB, "IN"),
  62. SQLite3Error);
  63. }
  64. // Test we can create the schema on the fly
  65. TEST(SQLite3Open, memoryDB) {
  66. EXPECT_NO_THROW(SQLite3Accessor accessor(SQLITE_DBFILE_MEMORY, "IN"));
  67. }
  68. // Test fixture for querying the db
  69. class SQLite3AccessorTest : public ::testing::Test {
  70. public:
  71. SQLite3AccessorTest() {
  72. initAccessor(SQLITE_DBFILE_EXAMPLE, "IN");
  73. }
  74. // So it can be re-created with different data
  75. void initAccessor(const std::string& filename, const string& rrclass) {
  76. accessor.reset(new SQLite3Accessor(filename, rrclass));
  77. }
  78. // The tested accessor
  79. boost::shared_ptr<SQLite3Accessor> accessor;
  80. };
  81. // This zone exists in the data, so it should be found
  82. TEST_F(SQLite3AccessorTest, getZone) {
  83. std::pair<bool, int> result(accessor->getZone("example.com."));
  84. EXPECT_TRUE(result.first);
  85. EXPECT_EQ(1, result.second);
  86. }
  87. // But it should find only the zone, nothing below it
  88. TEST_F(SQLite3AccessorTest, subZone) {
  89. EXPECT_FALSE(accessor->getZone("sub.example.com.").first);
  90. }
  91. // This zone is not there at all
  92. TEST_F(SQLite3AccessorTest, noZone) {
  93. EXPECT_FALSE(accessor->getZone("example.org.").first);
  94. }
  95. // This zone is there, but in different class
  96. TEST_F(SQLite3AccessorTest, noClass) {
  97. initAccessor(SQLITE_DBFILE_EXAMPLE, "CH");
  98. EXPECT_FALSE(accessor->getZone("example.com.").first);
  99. }
  100. // Simple check to test that the sequence is valid. It gets the next record
  101. // from the iterator, checks that it is not null, then checks the data.
  102. void checkRR(DatabaseAccessor::IteratorContextPtr& context,
  103. std::string name, std::string ttl, std::string type, std::string rdata) {
  104. // Mark where we are in the text
  105. SCOPED_TRACE(name + " " + ttl + " " + type + " " + rdata);
  106. std::string data[DatabaseAccessor::COLUMN_COUNT];
  107. // Get next record
  108. EXPECT_TRUE(context->getNext(data));
  109. // ... and check expected values
  110. EXPECT_EQ(name, data[DatabaseAccessor::NAME_COLUMN]);
  111. EXPECT_EQ(ttl, data[DatabaseAccessor::TTL_COLUMN]);
  112. EXPECT_EQ(type, data[DatabaseAccessor::TYPE_COLUMN]);
  113. EXPECT_EQ(rdata, data[DatabaseAccessor::RDATA_COLUMN]);
  114. }
  115. // This tests the iterator context
  116. TEST_F(SQLite3AccessorTest, iterator) {
  117. // Our test zone is conveniently small, but not empty
  118. initAccessor(SQLITE_DBFILE_EXAMPLE_ORG, "IN");
  119. const std::pair<bool, int> zone_info(accessor->getZone("example.org."));
  120. ASSERT_TRUE(zone_info.first);
  121. // Get the iterator context
  122. DatabaseAccessor::IteratorContextPtr
  123. context(accessor->getAllRecords(zone_info.second));
  124. ASSERT_NE(DatabaseAccessor::IteratorContextPtr(), context);
  125. std::string data[DatabaseAccessor::COLUMN_COUNT];
  126. checkRR(context, "example.org.", "3600", "MX", "10 mail.example.org.");
  127. checkRR(context, "example.org.", "3600", "NS", "ns1.example.org.");
  128. checkRR(context, "example.org.", "3600", "NS", "ns2.example.org.");
  129. checkRR(context, "example.org.", "3600", "NS", "ns3.example.org.");
  130. checkRR(context, "example.org.", "3600", "SOA",
  131. "ns1.example.org. admin.example.org. 1234 3600 1800 2419200 7200");
  132. checkRR(context, "dname.example.org.", "3600", "DNAME",
  133. "dname.example.info.");
  134. checkRR(context, "dname2.foo.example.org.", "3600", "DNAME",
  135. "dname2.example.info.");
  136. checkRR(context, "mail.example.org.", "3600", "A", "192.0.2.10");
  137. checkRR(context, "sub.example.org.", "3600", "NS", "ns.sub.example.org.");
  138. checkRR(context, "ns.sub.example.org.", "3600", "A", "192.0.2.101");
  139. checkRR(context, "www.example.org.", "3600", "A", "192.0.2.1");
  140. // Check there's no other
  141. EXPECT_FALSE(context->getNext(data));
  142. // And make sure calling it again won't cause problems.
  143. EXPECT_FALSE(context->getNext(data));
  144. }
  145. // This tests getting NSEC3 records
  146. TEST_F(SQLite3AccessorTest, nsec3) {
  147. const std::pair<bool, int>
  148. zone_info(accessor->getZone("sql2.example.com."));
  149. ASSERT_TRUE(zone_info.first);
  150. DatabaseAccessor::IteratorContextPtr
  151. context(accessor->getNSEC3Records("1BB7SO0452U1QHL98UISNDD9218GELR5",
  152. zone_info.second));
  153. // This relies on specific ordering in the DB. Is it OK?
  154. // The name field is empty, as well as the sigtype one. This is OK, as
  155. // both are not needed and the interface allows it.
  156. checkRR(context, "", "7200", "NSEC3",
  157. "1 0 10 FEEDABEE 4KLSVDE8KH8G95VU68R7AHBE1CPQN38J");
  158. checkRR(context, "", "7200", "RRSIG",
  159. "NSEC3 5 4 7200 20100410172647 20100311172647 63192 "
  160. "sql2.example.com. gNIVj4T8t51fEU6kOPpvK7HOGBFZGbalN5ZK "
  161. "mInyrww6UWZsUNdw07ge6/U6HfG+/s61RZ/L is2M6yUWHyXbNbj/"
  162. "QqwqgadG5dhxTArfuR02 xP600x0fWX8LXzW4yLMdKVxGbzYT+vvGz71o "
  163. "8gHSY5vYTtothcZQa4BMKhmGQEk=");
  164. // And that's all
  165. std::string data[DatabaseAccessor::COLUMN_COUNT];
  166. EXPECT_FALSE(context->getNext(data));
  167. // Calling again won't hurt
  168. EXPECT_FALSE(context->getNext(data));
  169. // This one should be empty ‒ no data here
  170. context = accessor->getNSEC3Records("NO_SUCH_HASH", zone_info.second);
  171. EXPECT_FALSE(context->getNext(data));
  172. // Still nothing? ;-)
  173. EXPECT_FALSE(context->getNext(data));
  174. }
  175. // This tests getting a previoeus hash in the NSEC3 namespace of a zone,
  176. // including a wrap-around and asking for a hash that does not exist in the.
  177. // zone at all.
  178. TEST_F(SQLite3AccessorTest, nsec3Previous) {
  179. // Get the zone
  180. const std::pair<bool, int>
  181. zone_info(accessor->getZone("sql2.example.com."));
  182. ASSERT_TRUE(zone_info.first);
  183. std::string data[DatabaseAccessor::COLUMN_COUNT];
  184. // Test a previous hash for something that is in the zone
  185. // (ensuring it is really there)
  186. DatabaseAccessor::IteratorContextPtr
  187. context(accessor->getNSEC3Records("703OOGCKF8VEV1N7U64D1JG19URETN8N",
  188. zone_info.second));
  189. EXPECT_TRUE(context->getNext(data));
  190. EXPECT_EQ("56IEQ664LHDAKVPE2FL179MSM3QAOFVC", accessor->
  191. findPreviousNSEC3Hash(zone_info.second,
  192. "703OOGCKF8VEV1N7U64D1JG19URETN8N"));
  193. // Test a previous hash for something that is not in the
  194. // zone
  195. context = accessor->getNSEC3Records("702OOGCKF8VEV1N7U64D1JG19URETN8N",
  196. zone_info.second);
  197. EXPECT_FALSE(context->getNext(data));
  198. EXPECT_EQ("56IEQ664LHDAKVPE2FL179MSM3QAOFVC", accessor->
  199. findPreviousNSEC3Hash(zone_info.second,
  200. "702OOGCKF8VEV1N7U64D1JG19URETN8N"));
  201. // Search at the first item, should wrap around
  202. context = accessor->getNSEC3Records("1BB7SO0452U1QHL98UISNDD9218GELR5",
  203. zone_info.second);
  204. EXPECT_TRUE(context->getNext(data));
  205. EXPECT_EQ("RKBUCQT8T78GV6QBCGBHCHC019LG73SJ", accessor->
  206. findPreviousNSEC3Hash(zone_info.second,
  207. "1BB7SO0452U1QHL98UISNDD9218GELR5"));
  208. // Search before the first item, should wrap around
  209. context = accessor->getNSEC3Records("0BB7SO0452U1QHL98UISNDD9218GELR5",
  210. zone_info.second);
  211. EXPECT_FALSE(context->getNext(data));
  212. EXPECT_EQ("RKBUCQT8T78GV6QBCGBHCHC019LG73SJ", accessor->
  213. findPreviousNSEC3Hash(zone_info.second,
  214. "0BB7SO0452U1QHL98UISNDD9218GELR5"));
  215. // Search after the last item (should return the last one)
  216. context = accessor->getNSEC3Records("RRBUCQT8T78GV6QBCGBHCHC019LG73SJ",
  217. zone_info.second);
  218. EXPECT_FALSE(context->getNext(data));
  219. EXPECT_EQ("RKBUCQT8T78GV6QBCGBHCHC019LG73SJ", accessor->
  220. findPreviousNSEC3Hash(zone_info.second,
  221. "RRBUCQT8T78GV6QBCGBHCHC019LG73SJ"));
  222. }
  223. // Check it throws when we want a previous NSEC3 hash in an unsigned zone
  224. TEST_F(SQLite3AccessorTest, nsec3PreviousUnsigned) {
  225. // This zone did not look signed in the test file.
  226. const std::pair<bool, int>
  227. unsigned_zone_info(accessor->getZone("example.com."));
  228. EXPECT_THROW(accessor->
  229. findPreviousNSEC3Hash(unsigned_zone_info.second,
  230. "0BB7SO0452U1QHL98UISNDD9218GELR5"),
  231. DataSourceError);
  232. }
  233. // This tests the difference iterator context
  234. // Test that at attempt to create a difference iterator for a serial number
  235. // that does not exist throws an exception.
  236. TEST_F(SQLite3AccessorTest, diffIteratorNoRecords) {
  237. // Our test zone is conveniently small, but not empty
  238. initAccessor(SQLITE_DBFILE_DIFFS, "IN");
  239. const std::pair<bool, int> zone_info(accessor->getZone("example.org."));
  240. ASSERT_TRUE(zone_info.first);
  241. // Get the iterator context. Difference of version 1 does not exist, so
  242. // this should throw an exception.
  243. EXPECT_THROW(accessor->getDiffs(zone_info.second, 1, 1234),
  244. isc::datasrc::NoSuchSerial);
  245. // Check that an invalid high version number also throws an exception.
  246. EXPECT_THROW(accessor->getDiffs(zone_info.second, 1231, 2234),
  247. NoSuchSerial);
  248. // Check that valid versions - but for the wrong zone which does not hold
  249. // any records - also throws this exception.
  250. EXPECT_THROW(accessor->getDiffs(zone_info.second + 42, 1231, 1234),
  251. NoSuchSerial);
  252. }
  253. // Try to iterate through a valid sets of differences
  254. TEST_F(SQLite3AccessorTest, diffIteratorSequences) {
  255. std::string data[DatabaseAccessor::COLUMN_COUNT];
  256. // Our test zone is conveniently small, but not empty
  257. initAccessor(SQLITE_DBFILE_DIFFS, "IN");
  258. const std::pair<bool, int> zone_info(accessor->getZone("example.org."));
  259. ASSERT_TRUE(zone_info.first);
  260. // Check the difference sequence 1230-1231 (two adjacent differences)
  261. // Get the iterator context
  262. DatabaseAccessor::IteratorContextPtr
  263. context1(accessor->getDiffs(zone_info.second, 1230, 1231));
  264. ASSERT_NE(DatabaseAccessor::IteratorContextPtr(), context1);
  265. // Change: 1230-1231
  266. checkRR(context1, "example.org.", "1800", "SOA",
  267. "ns1.example.org. admin.example.org. 1230 3600 1800 2419200 7200");
  268. checkRR(context1, "example.org.", "3600", "SOA",
  269. "ns1.example.org. admin.example.org. 1231 3600 1800 2419200 7200");
  270. // Check there's no other and that calling it again after no records doesn't
  271. // cause problems.
  272. EXPECT_FALSE(context1->getNext(data));
  273. EXPECT_FALSE(context1->getNext(data));
  274. // Check that the difference sequence 1231-1233 (two separate difference
  275. // sequences) is OK.
  276. DatabaseAccessor::IteratorContextPtr
  277. context2(accessor->getDiffs(zone_info.second, 1231, 1233));
  278. ASSERT_NE(DatabaseAccessor::IteratorContextPtr(), context2);
  279. // Change 1231-1232
  280. checkRR(context2, "example.org.", "3600", "SOA",
  281. "ns1.example.org. admin.example.org. 1231 3600 1800 2419200 7200");
  282. checkRR(context2, "unused.example.org.", "3600", "A", "192.0.2.102");
  283. checkRR(context2, "example.org.", "3600", "SOA",
  284. "ns1.example.org. admin.example.org. 1232 3600 1800 2419200 7200");
  285. // Change: 1232-1233
  286. checkRR(context2, "example.org.", "3600", "SOA",
  287. "ns1.example.org. admin.example.org. 1232 3600 1800 2419200 7200");
  288. checkRR(context2, "example.org.", "3600", "SOA",
  289. "ns1.example.org. admin.example.org. 1233 3600 1800 2419200 7200");
  290. checkRR(context2, "sub.example.org.", "3600", "NS", "ns.sub.example.org.");
  291. checkRR(context2, "ns.sub.example.org.", "3600", "A", "192.0.2.101");
  292. // Check there's no other and that calling it again after no records doesn't
  293. // cause problems.
  294. EXPECT_FALSE(context2->getNext(data));
  295. EXPECT_FALSE(context2->getNext(data));
  296. // Check that the difference sequence 4294967280 to 1230 (serial number
  297. // rollover) is OK
  298. DatabaseAccessor::IteratorContextPtr
  299. context3(accessor->getDiffs(zone_info.second, 4294967280U, 1230));
  300. ASSERT_NE(DatabaseAccessor::IteratorContextPtr(), context3);
  301. // Change 4294967280 to 1230.
  302. checkRR(context3, "example.org.", "3600", "SOA",
  303. "ns1.example.org. admin.example.org. 4294967280 3600 1800 2419200 7200");
  304. checkRR(context3, "www.example.org.", "3600", "A", "192.0.2.31");
  305. checkRR(context3, "example.org.", "1800", "SOA",
  306. "ns1.example.org. admin.example.org. 1230 3600 1800 2419200 7200");
  307. checkRR(context3, "www.example.org.", "3600", "A", "192.0.2.21");
  308. EXPECT_FALSE(context3->getNext(data));
  309. EXPECT_FALSE(context3->getNext(data));
  310. // Check the difference sequence 1233-1231 (versions in wrong order). This
  311. // should give an empty difference set.
  312. DatabaseAccessor::IteratorContextPtr
  313. context4(accessor->getDiffs(zone_info.second, 1233, 1231));
  314. ASSERT_NE(DatabaseAccessor::IteratorContextPtr(), context2);
  315. EXPECT_FALSE(context4->getNext(data));
  316. EXPECT_FALSE(context4->getNext(data));
  317. }
  318. TEST(SQLite3Open, getDBNameExample2) {
  319. SQLite3Accessor accessor(SQLITE_DBFILE_EXAMPLE2, "IN");
  320. EXPECT_EQ(SQLITE_DBNAME_EXAMPLE2, accessor.getDBName());
  321. }
  322. TEST(SQLite3Open, getDBNameExampleROOT) {
  323. SQLite3Accessor accessor(SQLITE_DBFILE_EXAMPLE_ROOT, "IN");
  324. EXPECT_EQ(SQLITE_DBNAME_EXAMPLE_ROOT, accessor.getDBName());
  325. }
  326. // Simple function to match records
  327. void
  328. checkRecordRow(const std::string columns[],
  329. const std::string& field0,
  330. const std::string& field1,
  331. const std::string& field2,
  332. const std::string& field3,
  333. const std::string& field4)
  334. {
  335. EXPECT_EQ(field0, columns[DatabaseAccessor::TYPE_COLUMN]);
  336. EXPECT_EQ(field1, columns[DatabaseAccessor::TTL_COLUMN]);
  337. EXPECT_EQ(field2, columns[DatabaseAccessor::SIGTYPE_COLUMN]);
  338. EXPECT_EQ(field3, columns[DatabaseAccessor::RDATA_COLUMN]);
  339. EXPECT_EQ(field4, columns[DatabaseAccessor::NAME_COLUMN]);
  340. }
  341. TEST_F(SQLite3AccessorTest, getRecords) {
  342. const std::pair<bool, int> zone_info(accessor->getZone("example.com."));
  343. ASSERT_TRUE(zone_info.first);
  344. const int zone_id = zone_info.second;
  345. ASSERT_EQ(1, zone_id);
  346. std::string columns[DatabaseAccessor::COLUMN_COUNT];
  347. DatabaseAccessor::IteratorContextPtr
  348. context(accessor->getRecords("foo.bar", 1));
  349. ASSERT_NE(DatabaseAccessor::IteratorContextPtr(),
  350. context);
  351. EXPECT_FALSE(context->getNext(columns));
  352. checkRecordRow(columns, "", "", "", "", "");
  353. // now try some real searches
  354. context = accessor->getRecords("foo.example.com.", zone_id);
  355. ASSERT_TRUE(context->getNext(columns));
  356. checkRecordRow(columns, "CNAME", "3600", "",
  357. "cnametest.example.org.", "");
  358. ASSERT_TRUE(context->getNext(columns));
  359. checkRecordRow(columns, "RRSIG", "3600", "CNAME",
  360. "CNAME 5 3 3600 20100322084538 20100220084538 33495 "
  361. "example.com. FAKEFAKEFAKEFAKE", "");
  362. ASSERT_TRUE(context->getNext(columns));
  363. checkRecordRow(columns, "NSEC", "7200", "",
  364. "mail.example.com. CNAME RRSIG NSEC", "");
  365. ASSERT_TRUE(context->getNext(columns));
  366. checkRecordRow(columns, "RRSIG", "7200", "NSEC",
  367. "NSEC 5 3 7200 20100322084538 20100220084538 33495 "
  368. "example.com. FAKEFAKEFAKEFAKE", "");
  369. EXPECT_FALSE(context->getNext(columns));
  370. // with no more records, the array should not have been modified
  371. checkRecordRow(columns, "RRSIG", "7200", "NSEC",
  372. "NSEC 5 3 7200 20100322084538 20100220084538 33495 "
  373. "example.com. FAKEFAKEFAKEFAKE", "");
  374. context = accessor->getRecords("example.com.", zone_id);
  375. ASSERT_TRUE(context->getNext(columns));
  376. checkRecordRow(columns, "SOA", "3600", "",
  377. "master.example.com. admin.example.com. "
  378. "1234 3600 1800 2419200 7200", "");
  379. ASSERT_TRUE(context->getNext(columns));
  380. checkRecordRow(columns, "RRSIG", "3600", "SOA",
  381. "SOA 5 2 3600 20100322084538 20100220084538 "
  382. "33495 example.com. FAKEFAKEFAKEFAKE", "");
  383. ASSERT_TRUE(context->getNext(columns));
  384. checkRecordRow(columns, "NS", "1200", "", "dns01.example.com.", "");
  385. ASSERT_TRUE(context->getNext(columns));
  386. checkRecordRow(columns, "NS", "3600", "", "dns02.example.com.", "");
  387. ASSERT_TRUE(context->getNext(columns));
  388. checkRecordRow(columns, "NS", "1800", "", "dns03.example.com.", "");
  389. ASSERT_TRUE(context->getNext(columns));
  390. checkRecordRow(columns, "RRSIG", "3600", "NS",
  391. "NS 5 2 3600 20100322084538 20100220084538 "
  392. "33495 example.com. FAKEFAKEFAKEFAKE", "");
  393. ASSERT_TRUE(context->getNext(columns));
  394. checkRecordRow(columns, "MX", "3600", "", "10 mail.example.com.", "");
  395. ASSERT_TRUE(context->getNext(columns));
  396. checkRecordRow(columns, "MX", "3600", "",
  397. "20 mail.subzone.example.com.", "");
  398. ASSERT_TRUE(context->getNext(columns));
  399. checkRecordRow(columns, "RRSIG", "3600", "MX",
  400. "MX 5 2 3600 20100322084538 20100220084538 "
  401. "33495 example.com. FAKEFAKEFAKEFAKE", "");
  402. ASSERT_TRUE(context->getNext(columns));
  403. checkRecordRow(columns, "NSEC", "7200", "",
  404. "cname-ext.example.com. NS SOA MX RRSIG NSEC DNSKEY", "");
  405. ASSERT_TRUE(context->getNext(columns));
  406. checkRecordRow(columns, "RRSIG", "7200", "NSEC",
  407. "NSEC 5 2 7200 20100322084538 20100220084538 "
  408. "33495 example.com. FAKEFAKEFAKEFAKE", "");
  409. ASSERT_TRUE(context->getNext(columns));
  410. checkRecordRow(columns, "DNSKEY", "3600", "",
  411. "256 3 5 AwEAAcOUBllYc1hf7ND9uDy+Yz1BF3sI0m4q NGV7W"
  412. "cTD0WEiuV7IjXgHE36fCmS9QsUxSSOV o1I/FMxI2PJVqTYHkX"
  413. "FBS7AzLGsQYMU7UjBZ SotBJ6Imt5pXMu+lEDNy8TOUzG3xm7g"
  414. "0qcbW YF6qCEfvZoBtAqi5Rk7Mlrqs8agxYyMx", "");
  415. ASSERT_TRUE(context->getNext(columns));
  416. checkRecordRow(columns, "DNSKEY", "3600", "",
  417. "257 3 5 AwEAAe5WFbxdCPq2jZrZhlMj7oJdff3W7syJ tbvzg"
  418. "62tRx0gkoCDoBI9DPjlOQG0UAbj+xUV 4HQZJStJaZ+fHU5AwV"
  419. "NT+bBZdtV+NujSikhd THb4FYLg2b3Cx9NyJvAVukHp/91HnWu"
  420. "G4T36 CzAFrfPwsHIrBz9BsaIQ21VRkcmj7DswfI/i DGd8j6b"
  421. "qiODyNZYQ+ZrLmF0KIJ2yPN3iO6Zq 23TaOrVTjB7d1a/h31OD"
  422. "fiHAxFHrkY3t3D5J R9Nsl/7fdRmSznwtcSDgLXBoFEYmw6p86"
  423. "Acv RyoYNcL1SXjaKVLG5jyU3UR+LcGZT5t/0xGf oIK/aKwEN"
  424. "rsjcKZZj660b1M=", "");
  425. ASSERT_TRUE(context->getNext(columns));
  426. checkRecordRow(columns, "RRSIG", "3600", "DNSKEY",
  427. "DNSKEY 5 2 3600 20100322084538 20100220084538 "
  428. "4456 example.com. FAKEFAKEFAKEFAKE", "");
  429. ASSERT_TRUE(context->getNext(columns));
  430. checkRecordRow(columns, "RRSIG", "3600", "DNSKEY",
  431. "DNSKEY 5 2 3600 20100322084538 20100220084538 "
  432. "33495 example.com. FAKEFAKEFAKEFAKE", "");
  433. EXPECT_FALSE(context->getNext(columns));
  434. // getnextrecord returning false should mean array is not altered
  435. checkRecordRow(columns, "RRSIG", "3600", "DNSKEY",
  436. "DNSKEY 5 2 3600 20100322084538 20100220084538 "
  437. "33495 example.com. FAKEFAKEFAKEFAKE", "");
  438. // check that another getNext does not cause problems
  439. EXPECT_FALSE(context->getNext(columns));
  440. // Try searching for subdomain
  441. // There's foo.bar.example.com in the data
  442. context = accessor->getRecords("bar.example.com.", zone_id, true);
  443. ASSERT_TRUE(context->getNext(columns));
  444. checkRecordRow(columns, "A", "3600", "", "192.0.2.1", "");
  445. EXPECT_FALSE(context->getNext(columns));
  446. // But we shouldn't match mix.example.com here
  447. context = accessor->getRecords("ix.example.com.", zone_id, true);
  448. EXPECT_FALSE(context->getNext(columns));
  449. }
  450. TEST_F(SQLite3AccessorTest, findPrevious) {
  451. EXPECT_EQ("dns01.example.com.",
  452. accessor->findPreviousName(1, "com.example.dns02."));
  453. // A name that doesn't exist
  454. EXPECT_EQ("dns01.example.com.",
  455. accessor->findPreviousName(1, "com.example.dns01x."));
  456. // Largest name
  457. EXPECT_EQ("www.example.com.",
  458. accessor->findPreviousName(1, "com.example.wwww"));
  459. // Out of zone after the last name
  460. EXPECT_EQ("www.example.com.",
  461. accessor->findPreviousName(1, "org.example."));
  462. // Case insensitive?
  463. EXPECT_EQ("dns01.example.com.",
  464. accessor->findPreviousName(1, "com.exaMple.DNS02."));
  465. // A name that doesn't exist
  466. EXPECT_EQ("dns01.example.com.",
  467. accessor->findPreviousName(1, "com.exaMple.DNS01X."));
  468. // The DB contains foo.bar.example.com., which would be in between
  469. // these two names. However, that one does not have an NSEC record,
  470. // which is how this database recognizes glue data, so it should
  471. // be skipped.
  472. EXPECT_EQ("example.com.",
  473. accessor->findPreviousName(1, "com.example.cname-ext."));
  474. // Throw when we are before the origin
  475. EXPECT_THROW(accessor->findPreviousName(1, "com.example."),
  476. isc::NotImplemented);
  477. EXPECT_THROW(accessor->findPreviousName(1, "a.example."),
  478. isc::NotImplemented);
  479. }
  480. TEST_F(SQLite3AccessorTest, findPreviousNoData) {
  481. // This one doesn't hold any NSEC records, so it shouldn't work
  482. // The underlying DB/data don't support DNSSEC, so it's not implemented
  483. // (does it make sense? Or different exception here?)
  484. EXPECT_THROW(accessor->findPreviousName(3, "com.example.sql2.www."),
  485. isc::NotImplemented);
  486. }
  487. // Test fixture for creating a db that automatically deletes it before start,
  488. // and when done
  489. class SQLite3Create : public ::testing::Test {
  490. public:
  491. SQLite3Create() {
  492. remove(SQLITE_NEW_DBFILE);
  493. }
  494. ~SQLite3Create() {
  495. remove(SQLITE_NEW_DBFILE);
  496. }
  497. };
  498. bool isReadable(const char* filename) {
  499. return (std::ifstream(filename).is_open());
  500. }
  501. TEST_F(SQLite3Create, creationtest) {
  502. ASSERT_FALSE(isReadable(SQLITE_NEW_DBFILE));
  503. // Should simply be created
  504. SQLite3Accessor accessor(SQLITE_NEW_DBFILE, "IN");
  505. ASSERT_TRUE(isReadable(SQLITE_NEW_DBFILE));
  506. }
  507. TEST_F(SQLite3Create, emptytest) {
  508. ASSERT_FALSE(isReadable(SQLITE_NEW_DBFILE));
  509. // open one manualle
  510. sqlite3* db;
  511. ASSERT_EQ(SQLITE_OK, sqlite3_open(SQLITE_NEW_DBFILE, &db));
  512. // empty, but not locked, so creating it now should work
  513. SQLite3Accessor accessor2(SQLITE_NEW_DBFILE, "IN");
  514. sqlite3_close(db);
  515. // should work now that we closed it
  516. SQLite3Accessor accessor3(SQLITE_NEW_DBFILE, "IN");
  517. }
  518. TEST_F(SQLite3Create, lockedtest) {
  519. ASSERT_FALSE(isReadable(SQLITE_NEW_DBFILE));
  520. // open one manually
  521. sqlite3* db;
  522. ASSERT_EQ(SQLITE_OK, sqlite3_open(SQLITE_NEW_DBFILE, &db));
  523. sqlite3_exec(db, "BEGIN EXCLUSIVE TRANSACTION", NULL, NULL, NULL);
  524. // should not be able to open it
  525. EXPECT_THROW(SQLite3Accessor accessor2(SQLITE_NEW_DBFILE, "IN"),
  526. SQLite3Error);
  527. sqlite3_exec(db, "ROLLBACK TRANSACTION", NULL, NULL, NULL);
  528. // should work now that we closed it
  529. SQLite3Accessor accessor3(SQLITE_NEW_DBFILE, "IN");
  530. }
  531. TEST_F(SQLite3AccessorTest, clone) {
  532. boost::shared_ptr<DatabaseAccessor> cloned = accessor->clone();
  533. EXPECT_EQ(accessor->getDBName(), cloned->getDBName());
  534. // The cloned accessor should have a separate connection and search
  535. // context, so it should be able to perform search in concurrent with
  536. // the original accessor.
  537. string columns1[DatabaseAccessor::COLUMN_COUNT];
  538. string columns2[DatabaseAccessor::COLUMN_COUNT];
  539. const std::pair<bool, int> zone_info1(
  540. accessor->getZone("example.com."));
  541. DatabaseAccessor::IteratorContextPtr iterator1 =
  542. accessor->getRecords("foo.example.com.", zone_info1.second);
  543. const std::pair<bool, int> zone_info2(
  544. accessor->getZone("example.com."));
  545. DatabaseAccessor::IteratorContextPtr iterator2 =
  546. cloned->getRecords("foo.example.com.", zone_info2.second);
  547. ASSERT_TRUE(iterator1->getNext(columns1));
  548. checkRecordRow(columns1, "CNAME", "3600", "", "cnametest.example.org.",
  549. "");
  550. ASSERT_TRUE(iterator2->getNext(columns2));
  551. checkRecordRow(columns2, "CNAME", "3600", "", "cnametest.example.org.",
  552. "");
  553. }
  554. //
  555. // Commonly used data for update tests
  556. //
  557. const char* const common_expected_data[] = {
  558. // Test record already stored in the tested sqlite3 DB file.
  559. "foo.bar.example.com.", "com.example.bar.foo.", "3600", "A", "",
  560. "192.0.2.1"
  561. };
  562. const char* const new_data[] = {
  563. // Newly added data commonly used by some of the tests below
  564. "newdata.example.com.", "com.example.newdata.", "3600", "A", "",
  565. "192.0.2.1"
  566. };
  567. const char* const deleted_data[] = {
  568. // Existing data to be removed commonly used by some of the tests below
  569. "foo.bar.example.com.", "A", "192.0.2.1"
  570. };
  571. class SQLite3Update : public SQLite3AccessorTest {
  572. protected:
  573. SQLite3Update() {
  574. // Note: if "installing" the test file fails some of the subsequent
  575. // tests would fail.
  576. const char *install_cmd = INSTALL_PROG " " TEST_DATA_DIR
  577. "/test.sqlite3 " TEST_DATA_BUILDDIR
  578. "/test.sqlite3.copied";
  579. if (system(install_cmd) != 0) {
  580. // any exception will do, this is failure in test setup, but nice
  581. // to show the command that fails, and shouldn't be caught
  582. isc_throw(isc::Exception,
  583. "Error setting up; command failed: " << install_cmd);
  584. };
  585. initAccessor(TEST_DATA_BUILDDIR "/test.sqlite3.copied", "IN");
  586. zone_id = accessor->getZone("example.com.").second;
  587. another_accessor.reset(new SQLite3Accessor(
  588. TEST_DATA_BUILDDIR "/test.sqlite3.copied",
  589. "IN"));
  590. expected_stored.push_back(common_expected_data);
  591. }
  592. int zone_id;
  593. std::string get_columns[DatabaseAccessor::COLUMN_COUNT];
  594. std::string add_columns[DatabaseAccessor::ADD_COLUMN_COUNT];
  595. std::string del_params[DatabaseAccessor::DEL_PARAM_COUNT];
  596. std::string diff_params[DatabaseAccessor::DIFF_PARAM_COUNT];
  597. vector<const char* const*> expected_stored; // placeholder for checkRecords
  598. vector<const char* const*> empty_stored; // indicate no corresponding data
  599. // Another accessor, emulating one running on a different process/thread
  600. boost::shared_ptr<SQLite3Accessor> another_accessor;
  601. DatabaseAccessor::IteratorContextPtr iterator;
  602. };
  603. void
  604. checkRecords(SQLite3Accessor& accessor, int zone_id, const std::string& name,
  605. vector<const char* const*> expected_rows)
  606. {
  607. DatabaseAccessor::IteratorContextPtr iterator =
  608. accessor.getRecords(name, zone_id);
  609. std::string columns[DatabaseAccessor::COLUMN_COUNT];
  610. vector<const char* const*>::const_iterator it = expected_rows.begin();
  611. while (iterator->getNext(columns)) {
  612. ASSERT_TRUE(it != expected_rows.end());
  613. checkRecordRow(columns, (*it)[3], (*it)[2], (*it)[4], (*it)[5], "");
  614. ++it;
  615. }
  616. EXPECT_TRUE(it == expected_rows.end());
  617. }
  618. TEST_F(SQLite3Update, emptyUpdate) {
  619. // If we do nothing between start and commit, the zone content
  620. // should be intact.
  621. checkRecords(*accessor, zone_id, "foo.bar.example.com.", expected_stored);
  622. zone_id = accessor->startUpdateZone("example.com.", false).second;
  623. checkRecords(*accessor, zone_id, "foo.bar.example.com.", expected_stored);
  624. accessor->commit();
  625. checkRecords(*accessor, zone_id, "foo.bar.example.com.", expected_stored);
  626. }
  627. TEST_F(SQLite3Update, flushZone) {
  628. // With 'replace' being true startUpdateZone() will flush the existing
  629. // zone content.
  630. checkRecords(*accessor, zone_id, "foo.bar.example.com.", expected_stored);
  631. zone_id = accessor->startUpdateZone("example.com.", true).second;
  632. checkRecords(*accessor, zone_id, "foo.bar.example.com.", empty_stored);
  633. accessor->commit();
  634. checkRecords(*accessor, zone_id, "foo.bar.example.com.", empty_stored);
  635. }
  636. TEST_F(SQLite3Update, readWhileUpdate) {
  637. zone_id = accessor->startUpdateZone("example.com.", true).second;
  638. checkRecords(*accessor, zone_id, "foo.bar.example.com.", empty_stored);
  639. // Until commit is done, the other accessor should see the old data
  640. checkRecords(*another_accessor, zone_id, "foo.bar.example.com.",
  641. expected_stored);
  642. // Once the changes are committed, the other accessor will see the new
  643. // data.
  644. accessor->commit();
  645. checkRecords(*another_accessor, zone_id, "foo.bar.example.com.",
  646. empty_stored);
  647. }
  648. TEST_F(SQLite3Update, rollback) {
  649. zone_id = accessor->startUpdateZone("example.com.", true).second;
  650. checkRecords(*accessor, zone_id, "foo.bar.example.com.", empty_stored);
  651. // Rollback will revert the change made by startUpdateZone(, true).
  652. accessor->rollback();
  653. checkRecords(*accessor, zone_id, "foo.bar.example.com.", expected_stored);
  654. }
  655. // TODO: ticket #1845
  656. TEST_F(SQLite3Update, DISABLED_rollbackFailure) {
  657. // This test emulates a rare scenario of making rollback attempt fail.
  658. // The iterator is paused in the middle of getting records, which prevents
  659. // the rollback operation at the end of the test.
  660. string columns[DatabaseAccessor::COLUMN_COUNT];
  661. iterator = accessor->getRecords("example.com.", zone_id);
  662. EXPECT_TRUE(iterator->getNext(columns));
  663. accessor->startUpdateZone("example.com.", true);
  664. EXPECT_THROW(accessor->rollback(), DataSourceError);
  665. }
  666. TEST_F(SQLite3Update, commitConflict) {
  667. // Start reading the DB by another accessor. We should stop at a single
  668. // call to getNextRecord() to keep holding the lock.
  669. iterator = another_accessor->getRecords("foo.example.com.", zone_id);
  670. EXPECT_TRUE(iterator->getNext(get_columns));
  671. // Due to getNextRecord() above, the other accessor holds a DB lock,
  672. // which will prevent commit.
  673. zone_id = accessor->startUpdateZone("example.com.", true).second;
  674. checkRecords(*accessor, zone_id, "foo.bar.example.com.", empty_stored);
  675. EXPECT_THROW(accessor->commit(), DataSourceError);
  676. accessor->rollback(); // rollback should still succeed
  677. checkRecords(*accessor, zone_id, "foo.bar.example.com.", expected_stored);
  678. }
  679. TEST_F(SQLite3Update, updateConflict) {
  680. // Similar to the previous case, but this is a conflict with another
  681. // update attempt. Note that these two accessors modify disjoint sets
  682. // of data; sqlite3 only has a coarse-grained lock so we cannot allow
  683. // these updates to run concurrently.
  684. EXPECT_TRUE(another_accessor->startUpdateZone("sql1.example.com.",
  685. true).first);
  686. EXPECT_THROW(accessor->startUpdateZone("example.com.", true),
  687. DataSourceError);
  688. checkRecords(*accessor, zone_id, "foo.bar.example.com.", expected_stored);
  689. // Once we rollback the other attempt of change, we should be able to
  690. // start and commit the transaction using the main accessor.
  691. another_accessor->rollback();
  692. accessor->startUpdateZone("example.com.", true);
  693. accessor->commit();
  694. }
  695. TEST_F(SQLite3Update, duplicateUpdate) {
  696. accessor->startUpdateZone("example.com.", false);
  697. EXPECT_THROW(accessor->startUpdateZone("example.com.", false),
  698. DataSourceError);
  699. }
  700. TEST_F(SQLite3Update, commitWithoutTransaction) {
  701. EXPECT_THROW(accessor->commit(), DataSourceError);
  702. }
  703. TEST_F(SQLite3Update, rollbackWithoutTransaction) {
  704. EXPECT_THROW(accessor->rollback(), DataSourceError);
  705. }
  706. TEST_F(SQLite3Update, addRecord) {
  707. // Before update, there should be no record for this name
  708. checkRecords(*accessor, zone_id, "newdata.example.com.", empty_stored);
  709. zone_id = accessor->startUpdateZone("example.com.", false).second;
  710. copy(new_data, new_data + DatabaseAccessor::ADD_COLUMN_COUNT,
  711. add_columns);
  712. accessor->addRecordToZone(add_columns);
  713. expected_stored.clear();
  714. expected_stored.push_back(new_data);
  715. checkRecords(*accessor, zone_id, "newdata.example.com.", expected_stored);
  716. // Commit the change, and confirm the new data is still there.
  717. accessor->commit();
  718. checkRecords(*accessor, zone_id, "newdata.example.com.", expected_stored);
  719. }
  720. TEST_F(SQLite3Update, addThenRollback) {
  721. zone_id = accessor->startUpdateZone("example.com.", false).second;
  722. copy(new_data, new_data + DatabaseAccessor::ADD_COLUMN_COUNT,
  723. add_columns);
  724. accessor->addRecordToZone(add_columns);
  725. expected_stored.clear();
  726. expected_stored.push_back(new_data);
  727. checkRecords(*accessor, zone_id, "newdata.example.com.", expected_stored);
  728. accessor->rollback();
  729. checkRecords(*accessor, zone_id, "newdata.example.com.", empty_stored);
  730. }
  731. TEST_F(SQLite3Update, duplicateAdd) {
  732. const char* const dup_data[] = {
  733. "foo.bar.example.com.", "com.example.bar.foo.", "3600", "A", "",
  734. "192.0.2.1"
  735. };
  736. expected_stored.clear();
  737. expected_stored.push_back(dup_data);
  738. checkRecords(*accessor, zone_id, "foo.bar.example.com.", expected_stored);
  739. // Adding exactly the same data. As this backend is "dumb", another
  740. // row of the same content will be inserted.
  741. copy(dup_data, dup_data + DatabaseAccessor::ADD_COLUMN_COUNT,
  742. add_columns);
  743. zone_id = accessor->startUpdateZone("example.com.", false).second;
  744. accessor->addRecordToZone(add_columns);
  745. expected_stored.push_back(dup_data);
  746. checkRecords(*accessor, zone_id, "foo.bar.example.com.", expected_stored);
  747. }
  748. TEST_F(SQLite3Update, invalidAdd) {
  749. // An attempt of add before an explicit start of transaction
  750. EXPECT_THROW(accessor->addRecordToZone(add_columns), DataSourceError);
  751. }
  752. TEST_F(SQLite3Update, deleteRecord) {
  753. zone_id = accessor->startUpdateZone("example.com.", false).second;
  754. checkRecords(*accessor, zone_id, "foo.bar.example.com.", expected_stored);
  755. copy(deleted_data, deleted_data + DatabaseAccessor::DEL_PARAM_COUNT,
  756. del_params);
  757. accessor->deleteRecordInZone(del_params);
  758. checkRecords(*accessor, zone_id, "foo.bar.example.com.", empty_stored);
  759. // Commit the change, and confirm the deleted data still isn't there.
  760. accessor->commit();
  761. checkRecords(*accessor, zone_id, "foo.bar.example.com.", empty_stored);
  762. }
  763. TEST_F(SQLite3Update, deleteThenRollback) {
  764. zone_id = accessor->startUpdateZone("example.com.", false).second;
  765. copy(deleted_data, deleted_data + DatabaseAccessor::DEL_PARAM_COUNT,
  766. del_params);
  767. accessor->deleteRecordInZone(del_params);
  768. checkRecords(*accessor, zone_id, "foo.bar.example.com.", empty_stored);
  769. // Rollback the change, and confirm the data still exists.
  770. accessor->rollback();
  771. checkRecords(*accessor, zone_id, "foo.bar.example.com.", expected_stored);
  772. }
  773. TEST_F(SQLite3Update, deleteNonexistent) {
  774. zone_id = accessor->startUpdateZone("example.com.", false).second;
  775. copy(deleted_data, deleted_data + DatabaseAccessor::DEL_PARAM_COUNT,
  776. del_params);
  777. // Replace the name with a non existent one, then try to delete it.
  778. // nothing should happen.
  779. del_params[DatabaseAccessor::DEL_NAME] = "no-such-name.example.com.";
  780. checkRecords(*accessor, zone_id, "no-such-name.example.com.",
  781. empty_stored);
  782. accessor->deleteRecordInZone(del_params);
  783. checkRecords(*accessor, zone_id, "no-such-name.example.com.",
  784. empty_stored);
  785. // Name exists but the RR type is different. Delete attempt shouldn't
  786. // delete only by name.
  787. copy(deleted_data, deleted_data + DatabaseAccessor::DEL_PARAM_COUNT,
  788. del_params);
  789. del_params[DatabaseAccessor::DEL_TYPE] = "AAAA";
  790. accessor->deleteRecordInZone(del_params);
  791. checkRecords(*accessor, zone_id, "foo.bar.example.com.", expected_stored);
  792. // Similar to the previous case, but RDATA is different.
  793. copy(deleted_data, deleted_data + DatabaseAccessor::DEL_PARAM_COUNT,
  794. del_params);
  795. del_params[DatabaseAccessor::DEL_RDATA] = "192.0.2.2";
  796. accessor->deleteRecordInZone(del_params);
  797. checkRecords(*accessor, zone_id, "foo.bar.example.com.", expected_stored);
  798. }
  799. TEST_F(SQLite3Update, invalidDelete) {
  800. // An attempt of delete before an explicit start of transaction
  801. EXPECT_THROW(accessor->deleteRecordInZone(del_params), DataSourceError);
  802. }
  803. TEST_F(SQLite3Update, emptyTransaction) {
  804. // A generic transaction without doing anything inside it. Just check
  805. // it doesn't throw or break the database.
  806. checkRecords(*accessor, zone_id, "foo.bar.example.com.", expected_stored);
  807. accessor->startTransaction();
  808. checkRecords(*accessor, zone_id, "foo.bar.example.com.", expected_stored);
  809. accessor->commit();
  810. checkRecords(*accessor, zone_id, "foo.bar.example.com.", expected_stored);
  811. }
  812. TEST_F(SQLite3Update, duplicateTransaction) {
  813. accessor->startTransaction();
  814. EXPECT_THROW(accessor->startTransaction(), DataSourceError);
  815. }
  816. TEST_F(SQLite3Update, transactionInUpdate) {
  817. accessor->startUpdateZone("example.com.", true);
  818. EXPECT_THROW(accessor->startTransaction(), DataSourceError);
  819. }
  820. TEST_F(SQLite3Update, updateInTransaction) {
  821. accessor->startTransaction();
  822. EXPECT_THROW(accessor->startUpdateZone("example.com.", true),
  823. DataSourceError);
  824. }
  825. TEST_F(SQLite3Update, updateWithTransaction) {
  826. // Start a read-only transaction, wherein we execute two reads.
  827. // Meanwhile we start a write (update) transaction. The commit attempt
  828. // for the write transaction will due to the lock held by the read
  829. // transaction. The database should be intact.
  830. another_accessor->startTransaction();
  831. checkRecords(*another_accessor, zone_id, "foo.bar.example.com.",
  832. expected_stored);
  833. ASSERT_TRUE(accessor->startUpdateZone("example.com.", true).first);
  834. EXPECT_THROW(accessor->commit(), DataSourceError);
  835. checkRecords(*another_accessor, zone_id, "foo.bar.example.com.",
  836. expected_stored);
  837. another_accessor->commit(); // this shouldn't throw
  838. }
  839. TEST_F(SQLite3Update, updateWithoutTransaction) {
  840. // Similar to the previous test, but reads are not protected in a
  841. // transaction. So the write transaction will succeed and flush the DB,
  842. // and the result of the second read is different from the first.
  843. checkRecords(*another_accessor, zone_id, "foo.bar.example.com.",
  844. expected_stored);
  845. ASSERT_TRUE(accessor->startUpdateZone("example.com.", true).first);
  846. accessor->commit();
  847. checkRecords(*another_accessor, zone_id, "foo.bar.example.com.",
  848. empty_stored);
  849. }
  850. TEST_F(SQLite3Update, concurrentTransactions) {
  851. // Two read-only transactions coexist (unlike the read vs write)
  852. // Start one transaction.
  853. accessor->startTransaction();
  854. checkRecords(*accessor, zone_id, "foo.bar.example.com.", expected_stored);
  855. // Start a new one.
  856. another_accessor->startTransaction();
  857. // The second transaction doesn't affect the first or vice versa.
  858. checkRecords(*accessor, zone_id, "foo.bar.example.com.", expected_stored);
  859. checkRecords(*another_accessor, zone_id, "foo.bar.example.com.",
  860. expected_stored);
  861. // Commit should be successful for both transactions.
  862. accessor->commit();
  863. another_accessor->commit();
  864. }
  865. //
  866. // Commonly used data for diff related tests. The last two entries are
  867. // a textual representation of "version" and a textual representation of
  868. // diff operation (either DIFF_ADD_TEXT or DIFF_DELETE_TEXT). We use this
  869. // format for the convenience of generating test data and checking the results.
  870. //
  871. const char* const DIFF_ADD_TEXT = "0";
  872. const char* const DIFF_DELETE_TEXT = "1";
  873. const char* const diff_begin_data[] = {
  874. "example.com.", "SOA", "3600",
  875. "ns.example.com. admin.example.com. 1234 3600 1800 2419200 7200",
  876. "1234", DIFF_DELETE_TEXT
  877. };
  878. const char* const diff_del_a_data[] = {
  879. "dns01.example.com.", "A", "3600", "192.0.2.1", "1234", DIFF_DELETE_TEXT
  880. };
  881. const char* const diff_end_data[] = {
  882. "example.com.", "SOA", "3600",
  883. "ns.example.com. admin.example.com. 1300 3600 1800 2419200 7200",
  884. "1300", DIFF_ADD_TEXT
  885. };
  886. const char* const diff_add_a_data[] = {
  887. "dns01.example.com.", "A", "3600", "192.0.2.10", "1300", DIFF_ADD_TEXT
  888. };
  889. // The following two are helper functions to convert textual test data
  890. // to integral zone ID and diff operation.
  891. int
  892. getVersion(const char* const diff_data[]) {
  893. return (lexical_cast<int>(diff_data[DatabaseAccessor::DIFF_PARAM_COUNT]));
  894. }
  895. DatabaseAccessor::DiffOperation
  896. getOperation(const char* const diff_data[]) {
  897. return (static_cast<DatabaseAccessor::DiffOperation>(
  898. lexical_cast<int>(
  899. diff_data[DatabaseAccessor::DIFF_PARAM_COUNT + 1])));
  900. }
  901. // Common checker function that compares expected and actual sequence of
  902. // diffs.
  903. void
  904. checkDiffs(const vector<const char* const*>& expected,
  905. DatabaseAccessor::IteratorContextPtr rr_iterator)
  906. {
  907. vector<vector<string> > actual;
  908. string columns_holder[DatabaseAccessor::COLUMN_COUNT];
  909. while (rr_iterator->getNext(columns_holder)) {
  910. // Reorder the 'actual' vector to be compatible with the expected one.
  911. vector<string> columns;
  912. columns.push_back(columns_holder[DatabaseAccessor::NAME_COLUMN]);
  913. columns.push_back(columns_holder[DatabaseAccessor::TYPE_COLUMN]);
  914. columns.push_back(columns_holder[DatabaseAccessor::TTL_COLUMN]);
  915. columns.push_back(columns_holder[DatabaseAccessor::RDATA_COLUMN]);
  916. actual.push_back(columns);
  917. }
  918. EXPECT_EQ(expected.size(), actual.size());
  919. const size_t n_diffs = std::min(expected.size(), actual.size());
  920. for (size_t i = 0; i < n_diffs; ++i) {
  921. for (int j = 0; j < actual[i].size(); ++j) {
  922. EXPECT_EQ(expected[i][j], actual[i][j]);
  923. }
  924. }
  925. }
  926. TEST_F(SQLite3Update, addRecordDiff) {
  927. // A simple case of adding diffs: just changing the SOA, and confirm
  928. // the diffs are stored as expected.
  929. zone_id = accessor->startUpdateZone("example.com.", false).second;
  930. copy(diff_begin_data, diff_begin_data + DatabaseAccessor::DIFF_PARAM_COUNT,
  931. diff_params);
  932. accessor->addRecordDiff(zone_id, getVersion(diff_begin_data),
  933. getOperation(diff_begin_data), diff_params);
  934. copy(diff_end_data, diff_end_data + DatabaseAccessor::DIFF_PARAM_COUNT,
  935. diff_params);
  936. accessor->addRecordDiff(zone_id, getVersion(diff_end_data),
  937. getOperation(diff_end_data), diff_params);
  938. // Until the diffs are committed, they are not visible to other accessors.
  939. EXPECT_THROW(another_accessor->getDiffs(zone_id, 1234, 1300),
  940. NoSuchSerial);
  941. accessor->commit();
  942. expected_stored.clear();
  943. expected_stored.push_back(diff_begin_data);
  944. expected_stored.push_back(diff_end_data);
  945. checkDiffs(expected_stored, accessor->getDiffs(zone_id, 1234, 1300));
  946. // Now it should be visible to others, too.
  947. checkDiffs(expected_stored, another_accessor->getDiffs(zone_id, 1234,
  948. 1300));
  949. }
  950. TEST_F(SQLite3Update, addRecordOfLargeSerial) {
  951. // This is essentially the same as the previous test, but using a
  952. // very large "version" (SOA serial), which is actually the possible
  953. // largest value to confirm the internal code doesn't have an overflow bug
  954. // or other failure due to the larger value.
  955. zone_id = accessor->startUpdateZone("example.com.", false).second;
  956. const char* const begin_data[] = {
  957. "example.com.", "SOA", "3600",
  958. "ns.example.com. admin.example.com. 4294967295 3600 1800 2419200 7200",
  959. "4294967295", DIFF_DELETE_TEXT
  960. };
  961. copy(begin_data, begin_data + DatabaseAccessor::DIFF_PARAM_COUNT,
  962. diff_params);
  963. // For "serial" parameter, we intentionally hardcode the value rather
  964. // than converting it from the data.
  965. accessor->addRecordDiff(zone_id, 0xffffffff, getOperation(diff_begin_data),
  966. diff_params);
  967. copy(diff_end_data, diff_end_data + DatabaseAccessor::DIFF_PARAM_COUNT,
  968. diff_params);
  969. accessor->addRecordDiff(zone_id, getVersion(diff_end_data),
  970. getOperation(diff_end_data), diff_params);
  971. accessor->commit();
  972. expected_stored.clear();
  973. expected_stored.push_back(begin_data);
  974. expected_stored.push_back(diff_end_data);
  975. checkDiffs(expected_stored, accessor->getDiffs(zone_id, 4294967295, 1300));
  976. }
  977. TEST_F(SQLite3Update, addDiffWithoutUpdate) {
  978. // Right now we require startUpdateZone() prior to performing
  979. // addRecordDiff.
  980. copy(diff_begin_data, diff_begin_data + DatabaseAccessor::DIFF_PARAM_COUNT,
  981. diff_params);
  982. EXPECT_THROW(accessor->addRecordDiff(0, getVersion(diff_begin_data),
  983. getOperation(diff_begin_data),
  984. diff_params),
  985. DataSourceError);
  986. // For now, we don't allow adding diffs in a general transaction either.
  987. accessor->startTransaction();
  988. EXPECT_THROW(accessor->addRecordDiff(0, getVersion(diff_begin_data),
  989. getOperation(diff_begin_data),
  990. diff_params),
  991. DataSourceError);
  992. }
  993. TEST_F(SQLite3Update, addDiffWithBadZoneID) {
  994. // For now, we require zone ID passed to addRecordDiff be equal to
  995. // that for the zone being updated.
  996. zone_id = accessor->startUpdateZone("example.com.", false).second;
  997. copy(diff_begin_data, diff_begin_data + DatabaseAccessor::DIFF_PARAM_COUNT,
  998. diff_params);
  999. EXPECT_THROW(accessor->addRecordDiff(zone_id + 1,
  1000. getVersion(diff_begin_data),
  1001. getOperation(diff_begin_data),
  1002. diff_params),
  1003. DataSourceError);
  1004. }
  1005. TEST_F(SQLite3Update, addDiffRollback) {
  1006. // Rollback tentatively added diffs. This is no different from the
  1007. // update case, but we test it explicitly just in case.
  1008. zone_id = accessor->startUpdateZone("example.com.", false).second;
  1009. copy(diff_begin_data, diff_begin_data + DatabaseAccessor::DIFF_PARAM_COUNT,
  1010. diff_params);
  1011. accessor->addRecordDiff(zone_id, getVersion(diff_begin_data),
  1012. getOperation(diff_begin_data), diff_params);
  1013. accessor->rollback();
  1014. EXPECT_THROW(accessor->getDiffs(zone_id, 1234, 1234), NoSuchSerial);
  1015. }
  1016. TEST_F(SQLite3Update, addDiffInBadOrder) {
  1017. // At this level, the API is naive, and doesn't care if the diff sequence
  1018. // is a valid IXFR order.
  1019. zone_id = accessor->startUpdateZone("example.com.", false).second;
  1020. // Add diff of 'end', then 'begin'
  1021. copy(diff_end_data, diff_end_data + DatabaseAccessor::DIFF_PARAM_COUNT,
  1022. diff_params);
  1023. accessor->addRecordDiff(zone_id, getVersion(diff_end_data),
  1024. static_cast<DatabaseAccessor::DiffOperation>(
  1025. lexical_cast<int>(DIFF_DELETE_TEXT)),
  1026. diff_params);
  1027. copy(diff_begin_data, diff_begin_data + DatabaseAccessor::DIFF_PARAM_COUNT,
  1028. diff_params);
  1029. accessor->addRecordDiff(zone_id, getVersion(diff_begin_data),
  1030. static_cast<DatabaseAccessor::DiffOperation>(
  1031. lexical_cast<int>(DIFF_ADD_TEXT)),
  1032. diff_params);
  1033. accessor->commit();
  1034. expected_stored.clear();
  1035. expected_stored.push_back(diff_end_data);
  1036. expected_stored.push_back(diff_begin_data);
  1037. checkDiffs(expected_stored, accessor->getDiffs(zone_id, 1300, 1234));
  1038. }
  1039. TEST_F(SQLite3Update, addDiffWithUpdate) {
  1040. // A more realistic example: add corresponding diffs while updating zone.
  1041. // Implementation wise, there should be no reason this could fail if
  1042. // the basic tests so far pass. But we check it in case we miss something.
  1043. const char* const old_a_record[] = {
  1044. "dns01.example.com.", "A", "192.0.2.1"
  1045. };
  1046. const char* const new_a_record[] = {
  1047. "dns01.example.com.", "com.example.dns01.", "3600", "A", "",
  1048. "192.0.2.10"
  1049. };
  1050. const char* const old_soa_record[] = {
  1051. "example.com.", "SOA",
  1052. "ns.example.com. admin.example.com. 1234 3600 1800 2419200 7200",
  1053. };
  1054. const char* const new_soa_record[] = {
  1055. "dns01.example.com.", "com.example.dns01.", "3600", "A", "",
  1056. "ns.example.com. admin.example.com. 1300 3600 1800 2419200 7200",
  1057. };
  1058. zone_id = accessor->startUpdateZone("example.com.", false).second;
  1059. // Delete SOA (and add that diff)
  1060. copy(old_soa_record, old_soa_record + DatabaseAccessor::DEL_PARAM_COUNT,
  1061. del_params);
  1062. accessor->deleteRecordInZone(del_params);
  1063. copy(diff_begin_data, diff_begin_data + DatabaseAccessor::DIFF_PARAM_COUNT,
  1064. diff_params);
  1065. accessor->addRecordDiff(zone_id, getVersion(diff_begin_data),
  1066. getOperation(diff_begin_data), diff_params);
  1067. // Delete A
  1068. copy(old_a_record, old_a_record + DatabaseAccessor::DEL_PARAM_COUNT,
  1069. del_params);
  1070. accessor->deleteRecordInZone(del_params);
  1071. copy(diff_del_a_data, diff_del_a_data + DatabaseAccessor::DIFF_PARAM_COUNT,
  1072. diff_params);
  1073. accessor->addRecordDiff(zone_id, getVersion(diff_del_a_data),
  1074. getOperation(diff_del_a_data), diff_params);
  1075. // Add SOA
  1076. copy(new_soa_record, new_soa_record + DatabaseAccessor::ADD_COLUMN_COUNT,
  1077. add_columns);
  1078. accessor->addRecordToZone(add_columns);
  1079. copy(diff_end_data, diff_end_data + DatabaseAccessor::DIFF_PARAM_COUNT,
  1080. diff_params);
  1081. accessor->addRecordDiff(zone_id, getVersion(diff_end_data),
  1082. getOperation(diff_end_data), diff_params);
  1083. // Add A
  1084. copy(new_a_record, new_a_record + DatabaseAccessor::ADD_COLUMN_COUNT,
  1085. add_columns);
  1086. accessor->addRecordToZone(add_columns);
  1087. copy(diff_add_a_data, diff_add_a_data + DatabaseAccessor::DIFF_PARAM_COUNT,
  1088. diff_params);
  1089. accessor->addRecordDiff(zone_id, getVersion(diff_add_a_data),
  1090. getOperation(diff_add_a_data), diff_params);
  1091. accessor->commit();
  1092. expected_stored.clear();
  1093. expected_stored.push_back(diff_begin_data);
  1094. expected_stored.push_back(diff_del_a_data);
  1095. expected_stored.push_back(diff_end_data);
  1096. expected_stored.push_back(diff_add_a_data);
  1097. checkDiffs(expected_stored, accessor->getDiffs(zone_id, 1234, 1300));
  1098. }
  1099. TEST_F(SQLite3Update, addDiffWithNoTable) {
  1100. // An attempt of adding diffs to an old version of database that doesn't
  1101. // have a diffs table. This will fail in preparing the statement.
  1102. initAccessor(SQLITE_DBFILE_EXAMPLE + ".nodiffs", "IN");
  1103. zone_id = accessor->startUpdateZone("example.com.", false).second;
  1104. copy(diff_begin_data, diff_begin_data + DatabaseAccessor::DIFF_PARAM_COUNT,
  1105. diff_params);
  1106. EXPECT_THROW(accessor->addRecordDiff(zone_id, getVersion(diff_begin_data),
  1107. getOperation(diff_begin_data),
  1108. diff_params),
  1109. SQLite3Error);
  1110. }
  1111. } // end anonymous namespace