]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Add more unit tests directly testing hashQNameWithSalt() 9859/head
authorRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 28 Jan 2021 09:36:28 +0000 (10:36 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 29 Jan 2021 11:12:51 +0000 (12:12 +0100)
pdns/test-signers.cc

index 3d6872309f8d183f4bd0071eb09c35d9943753ce..6302462b88a42b44b1df7096f02c03ab0f2019f9 100644 (file)
@@ -259,27 +259,74 @@ BOOST_AUTO_TEST_CASE(test_ed448_signer) {
 #endif /* defined(HAVE_LIBDECAF) || defined(HAVE_LIBCRYPTO_ED448) */
 
 BOOST_AUTO_TEST_CASE(test_hash_qname_with_salt) {
-  const unsigned char salt[] = { 0xaa, 0xbb, 0xcc, 0xdd };
-  const unsigned int iterations{12};
-  const std::vector<std::pair<std::string, std::string>> namesToHashes = {
+  {
     // rfc5155 appendix A
-    { "example", "0p9mhaveqvm6t7vbl5lop2u3t2rp3tom" },
-    { "a.example", "35mthgpgcu1qg68fab165klnsnk3dpvl" },
-    { "ai.example", "gjeqe526plbf1g8mklp59enfd789njgi" },
-    { "ns1.example", "2t7b4g4vsa5smi47k61mv5bv1a22bojr" },
-    { "ns2.example", "q04jkcevqvmu85r014c7dkba38o0ji5r" },
-    { "w.example", "k8udemvp1j2f7eg6jebps17vp3n8i58h" },
-    { "*.w.example", "r53bq7cc2uvmubfu5ocmm6pers9tk9en" },
-    { "x.w.example", "b4um86eghhds6nea196smvmlo4ors995" },
-    { "y.w.example", "ji6neoaepv8b5o6k4ev33abha8ht9fgc" },
-    { "x.y.w.example", "2vptu5timamqttgl4luu9kg21e0aor3s" },
-    { "xx.example", "t644ebqk9bibcna874givr6joj62mlhv" },
-    { "2t7b4g4vsa5smi47k61mv5bv1a22bojr.example", "kohar7mbb8dc2ce8a9qvl8hon4k53uhi" },
-  };
-
-  for (const auto& [name, expectedHash] : namesToHashes) {
-    auto hash = hashQNameWithSalt(std::string(reinterpret_cast<const char*>(salt), sizeof(salt)), iterations, DNSName(name));
-    BOOST_CHECK_EQUAL(toBase32Hex(hash), expectedHash);
+    const unsigned char salt[] = { 0xaa, 0xbb, 0xcc, 0xdd };
+    const unsigned int iterations{12};
+    const std::vector<std::pair<std::string, std::string>> namesToHashes = {
+      { "example", "0p9mhaveqvm6t7vbl5lop2u3t2rp3tom" },
+      { "a.example", "35mthgpgcu1qg68fab165klnsnk3dpvl" },
+      { "ai.example", "gjeqe526plbf1g8mklp59enfd789njgi" },
+      { "ns1.example", "2t7b4g4vsa5smi47k61mv5bv1a22bojr" },
+      { "ns2.example", "q04jkcevqvmu85r014c7dkba38o0ji5r" },
+      { "w.example", "k8udemvp1j2f7eg6jebps17vp3n8i58h" },
+      { "*.w.example", "r53bq7cc2uvmubfu5ocmm6pers9tk9en" },
+      { "x.w.example", "b4um86eghhds6nea196smvmlo4ors995" },
+      { "y.w.example", "ji6neoaepv8b5o6k4ev33abha8ht9fgc" },
+      { "x.y.w.example", "2vptu5timamqttgl4luu9kg21e0aor3s" },
+      { "xx.example", "t644ebqk9bibcna874givr6joj62mlhv" },
+      { "2t7b4g4vsa5smi47k61mv5bv1a22bojr.example", "kohar7mbb8dc2ce8a9qvl8hon4k53uhi" },
+    };
+
+    for (const auto& [name, expectedHash] : namesToHashes) {
+      auto hash = hashQNameWithSalt(std::string(reinterpret_cast<const char*>(salt), sizeof(salt)), iterations, DNSName(name));
+      BOOST_CHECK_EQUAL(toBase32Hex(hash), expectedHash);
+    }
+  }
+
+  {
+    /* no additional iterations, very short salt */
+    const unsigned char salt[] = { 0xFF };
+    const unsigned int iterations{0};
+    const std::vector<std::pair<std::string, std::string>> namesToHashes = {
+      { "example", "s9dp8o2l6jgqgg26ecobtjooe7p019cs" },
+    };
+
+    for (const auto& [name, expectedHash] : namesToHashes) {
+      auto hash = hashQNameWithSalt(std::string(reinterpret_cast<const char*>(salt), sizeof(salt)), iterations, DNSName(name));
+      BOOST_CHECK_EQUAL(toBase32Hex(hash), expectedHash);
+    }
+  }
+
+  {
+    /* only one iteration */
+    const unsigned char salt[] = { 0xaa, 0xbb, 0xcc, 0xdd };
+    const unsigned int iterations{1};
+    const std::vector<std::pair<std::string, std::string>> namesToHashes = {
+      { "example", "ulddquehrj5jpf50ga76vgqr1oq40133" },
+    };
+
+    for (const auto& [name, expectedHash] : namesToHashes) {
+      auto hash = hashQNameWithSalt(std::string(reinterpret_cast<const char*>(salt), sizeof(salt)), iterations, DNSName(name));
+      BOOST_CHECK_EQUAL(toBase32Hex(hash), expectedHash);
+    }
+  }
+
+  {
+    /* 65535 iterations, long salt */
+    unsigned char salt[255];
+    for (unsigned char idx = 0; idx < 255; idx++) {
+      salt[idx] = idx;
+    };
+    const unsigned int iterations{65535};
+    const std::vector<std::pair<std::string, std::string>> namesToHashes = {
+      { "example", "no95j4cfile8avstr7bn4aj9he18trri" },
+    };
+
+    for (const auto& [name, expectedHash] : namesToHashes) {
+      auto hash = hashQNameWithSalt(std::string(reinterpret_cast<const char*>(salt), sizeof(salt)), iterations, DNSName(name));
+      BOOST_CHECK_EQUAL(toBase32Hex(hash), expectedHash);
+    }
   }
 }