sqlite3_accessor_unittest.cc 49 KB

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