sqlite3_accessor_unittest.cc 62 KB

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