faked_nsec3.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 <dns/name.h>
  16. #include <testutils/dnsmessage_test.h>
  17. #include <map>
  18. #include <gtest/gtest.h>
  19. using namespace std;
  20. using namespace isc::dns;
  21. using namespace isc::testutils;
  22. namespace isc {
  23. namespace datasrc {
  24. namespace test {
  25. // Constant data definitions
  26. const char* const nsec3_common = " 300 IN NSEC3 1 1 12 aabbccdd "
  27. "2T7B4G4VSA5SMI47K61MV5BV1A22BOJR A RRSIG";
  28. const char* const nsec3_rrsig_common = " 300 IN RRSIG NSEC3 5 3 3600 "
  29. "20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE";
  30. const char* const apex_hash = "0P9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM";
  31. const char* const apex_hash_lower = "0p9mhaveqvm6t7vbl5lop2u3t2rp3tom";
  32. const char* const ns1_hash = "2T7B4G4VSA5SMI47K61MV5BV1A22BOJR";
  33. const char* const w_hash = "01UDEMVP1J2F7EG6JEBPS17VP3N8I58H";
  34. const char* const xyw_hash = "2vptu5timamqttgl4luu9kg21e0aor3s";
  35. const char* const zzz_hash = "R53BQ7CC2UVMUBFU5OCMM6PERS9TK9EN";
  36. class TestNSEC3HashCreator::TestNSEC3Hash : public NSEC3Hash {
  37. private:
  38. typedef map<Name, string> NSEC3HashMap;
  39. typedef NSEC3HashMap::value_type NSEC3HashPair;
  40. NSEC3HashMap map_;
  41. public:
  42. TestNSEC3Hash() {
  43. // Build pre-defined hash
  44. map_[Name("example.org")] = apex_hash;
  45. map_[Name("www.example.org")] = "2S9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM";
  46. map_[Name("xxx.example.org")] = "Q09MHAVEQVM6T7VBL5LOP2U3T2RP3TOM";
  47. map_[Name("yyy.example.org")] = "0A9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM";
  48. map_[Name("x.y.w.example.org")] =
  49. "2VPTU5TIMAMQTTGL4LUU9KG21E0AOR3S";
  50. map_[Name("y.w.example.org")] = "K8UDEMVP1J2F7EG6JEBPS17VP3N8I58H";
  51. map_[Name("w.example.org")] = w_hash;
  52. map_[Name("zzz.example.org")] = zzz_hash;
  53. map_[Name("smallest.example.org")] =
  54. "00000000000000000000000000000000";
  55. map_[Name("largest.example.org")] =
  56. "UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU";
  57. }
  58. virtual string calculate(const Name& name) const {
  59. const NSEC3HashMap::const_iterator found = map_.find(name);
  60. if (found != map_.end()) {
  61. return (found->second);
  62. }
  63. isc_throw(isc::Unexpected, "unexpected name for NSEC3 test: "
  64. << name);
  65. }
  66. virtual bool match(const rdata::generic::NSEC3PARAM&) const {
  67. return (true);
  68. }
  69. virtual bool match(const rdata::generic::NSEC3&) const {
  70. return (true);
  71. }
  72. };
  73. NSEC3Hash* TestNSEC3HashCreator::create(const rdata::generic::NSEC3PARAM&)
  74. const
  75. {
  76. return (new TestNSEC3Hash);
  77. }
  78. NSEC3Hash* TestNSEC3HashCreator::create(const rdata::generic::NSEC3&) const {
  79. return (new TestNSEC3Hash);
  80. }
  81. void
  82. findNSEC3Check(bool expected_matched, uint8_t expected_labels,
  83. const string& expected_closest,
  84. const string& expected_next,
  85. const ZoneFinder::FindNSEC3Result& result,
  86. bool expected_sig)
  87. {
  88. EXPECT_EQ(expected_matched, result.matched);
  89. // Convert to int so the error messages would be more readable:
  90. EXPECT_EQ(static_cast<int>(expected_labels),
  91. static_cast<int>(result.closest_labels));
  92. vector<ConstRRsetPtr> actual_rrsets;
  93. ASSERT_TRUE(result.closest_proof);
  94. actual_rrsets.push_back(result.closest_proof);
  95. if (expected_sig) {
  96. actual_rrsets.push_back(result.closest_proof->getRRsig());
  97. }
  98. rrsetsCheck(expected_closest, actual_rrsets.begin(),
  99. actual_rrsets.end());
  100. actual_rrsets.clear();
  101. if (expected_next.empty()) {
  102. EXPECT_FALSE(result.next_proof);
  103. } else {
  104. ASSERT_TRUE(result.next_proof);
  105. actual_rrsets.push_back(result.next_proof);
  106. if (expected_sig) {
  107. actual_rrsets.push_back(result.next_proof->getRRsig());
  108. }
  109. rrsetsCheck(expected_next, actual_rrsets.begin(),
  110. actual_rrsets.end());
  111. }
  112. }
  113. void
  114. performNSEC3Test(ZoneFinder &finder) {
  115. // Parameter validation: the query name must be in or below the zone
  116. EXPECT_THROW(finder.findNSEC3(Name("example.com"), false), OutOfZone);
  117. EXPECT_THROW(finder.findNSEC3(Name("org"), true), OutOfZone);
  118. const Name origin("example.org");
  119. const string apex_nsec3_text = string(apex_hash) + ".example.org." +
  120. string(nsec3_common);
  121. const string ns1_nsec3_text = string(ns1_hash) + ".example.org." +
  122. string(nsec3_common);
  123. const string w_nsec3_text = string(w_hash) + ".example.org." +
  124. string(nsec3_common);
  125. const string zzz_nsec3_text = string(zzz_hash) + ".example.org." +
  126. string(nsec3_common);
  127. // Apex name. It should have a matching NSEC3.
  128. {
  129. SCOPED_TRACE("apex, non recursive mode");
  130. findNSEC3Check(true, origin.getLabelCount(), apex_nsec3_text, "",
  131. finder.findNSEC3(origin, false));
  132. }
  133. // Recursive mode doesn't change the result in this case.
  134. {
  135. SCOPED_TRACE("apex, recursive mode");
  136. findNSEC3Check(true, origin.getLabelCount(), apex_nsec3_text, "",
  137. finder.findNSEC3(origin, true));
  138. }
  139. // Non existent name (in the NSEC3 namespace -- the findNSEC3 does
  140. // not look into the normal data). Disabling recursion, a covering
  141. // NSEC3 should be returned.
  142. const Name www_name("www.example.org");
  143. {
  144. SCOPED_TRACE("non existent name, non recursive mode");
  145. findNSEC3Check(false, www_name.getLabelCount(), apex_nsec3_text, "",
  146. finder.findNSEC3(www_name, false));
  147. }
  148. // Non existent name. The closest provable encloser is the apex,
  149. // and next closer is the query name itself (which NSEC3 for ns1
  150. // covers)
  151. // H(ns1) = 2T... < H(xxx) = Q0... < H(zzz) = R5...
  152. {
  153. SCOPED_TRACE("non existent name, recursive mode");
  154. findNSEC3Check(true, origin.getLabelCount(), apex_nsec3_text,
  155. ns1_nsec3_text,
  156. finder.findNSEC3(Name("xxx.example.org"), true));
  157. }
  158. // Similar to the previous case, but next closer name is different
  159. // from the query name. The closet encloser is w.example.org, and
  160. // next closer is y.w.example.org.
  161. // H(ns1) = 2T.. < H(y.w) = K8.. < H(zzz) = R5
  162. {
  163. SCOPED_TRACE("non existent name, non qname next closer");
  164. findNSEC3Check(true, Name("w.example.org").getLabelCount(),
  165. w_nsec3_text, ns1_nsec3_text,
  166. finder.findNSEC3(Name("x.y.w.example.org"),
  167. true));
  168. }
  169. // In the rest of test we check hash comparison for wrap around cases.
  170. {
  171. SCOPED_TRACE("very small hash");
  172. const Name smallest_name("smallest.example.org");
  173. findNSEC3Check(false, smallest_name.getLabelCount(),
  174. zzz_nsec3_text, "",
  175. finder.findNSEC3(smallest_name, false));
  176. }
  177. {
  178. SCOPED_TRACE("very large hash");
  179. const Name largest_name("largest.example.org");
  180. findNSEC3Check(false, largest_name.getLabelCount(),
  181. zzz_nsec3_text, "",
  182. finder.findNSEC3(largest_name, false));
  183. }
  184. }
  185. }
  186. }
  187. }