Browse Source

[trac781] some additional tests

Jelte Jansen 14 years ago
parent
commit
2aa47d167c
3 changed files with 56 additions and 5 deletions
  1. 5 4
      src/lib/crypto/crypto.cc
  2. 1 1
      src/lib/crypto/crypto.h
  3. 50 0
      src/lib/crypto/tests/crypto_unittests.cc

+ 5 - 4
src/lib/crypto/crypto.cc

@@ -25,6 +25,8 @@
 
 #include <string>
 
+#include <boost/scoped_ptr.hpp>
+
 using namespace std;
 using namespace isc::dns;
 
@@ -91,7 +93,7 @@ public:
                       "Unknown hash algorithm: " + hash_algorithm);
         }
 
-        hmac_ = new Botan::HMAC::HMAC(hash);
+        hmac_.reset(new Botan::HMAC::HMAC(hash));
 
         // If the key length is larger than the block size, we hash the
         // key itself first.
@@ -106,12 +108,11 @@ public:
                                secret_len);
             }
         } catch (const Botan::Invalid_Key_Length& ikl) {
-            delete hmac_;
             isc_throw(BadKey, ikl.what());
         }
     }
 
-    ~HMACImpl() { delete hmac_; }
+    ~HMACImpl() { }
 
     size_t getOutputLength() const {
         return (hmac_->OUTPUT_LENGTH);
@@ -163,7 +164,7 @@ public:
     }
 
 private:
-    Botan::HMAC* hmac_;
+    boost::scoped_ptr<Botan::HMAC> hmac_;
 };
 
 HMAC::HMAC(const void* secret, size_t secret_length,

+ 1 - 1
src/lib/crypto/crypto.h

@@ -82,7 +82,7 @@ class HMACImpl;
 ///
 /// This class is used to create and verify HMAC signatures
 ///
-class HMAC : public boost::noncopyable {
+class HMAC : private boost::noncopyable {
 public:
     enum HashAlgorithm {
         MD5 = 0,

+ 50 - 0
src/lib/crypto/tests/crypto_unittests.cc

@@ -403,6 +403,56 @@ TEST(CryptoTest, HMAC_SHA256_RFC2202_SIGN) {
                secret6.c_str(), 131, HMAC::SHA256, hmac_expected7, 32);
 }
 
+namespace {
+    std::vector<uint8_t>
+    createSigForLengthCheck(HMAC::HashAlgorithm alg,
+                            size_t len) {
+        HMAC hmac_sign("asdf", 4, alg);
+        hmac_sign.update("asdf", 4);
+        return hmac_sign.sign(len);
+    }
+}
+
+TEST(CryptoTest, HMACSigLengthArgument)
+{
+    std::vector<uint8_t> sig;
+
+    // Default size
+    sig = createSigForLengthCheck(HMAC::MD5, 0);
+    EXPECT_EQ(16, sig.size());
+    sig = createSigForLengthCheck(HMAC::MD5, 12);
+    EXPECT_EQ(12, sig.size());
+    sig = createSigForLengthCheck(HMAC::MD5, 16);
+    EXPECT_EQ(16, sig.size());
+    sig = createSigForLengthCheck(HMAC::MD5, 24);
+    EXPECT_EQ(16, sig.size());
+    sig = createSigForLengthCheck(HMAC::MD5, 12345);
+    EXPECT_EQ(16, sig.size());
+
+    sig = createSigForLengthCheck(HMAC::SHA1, 0);
+    EXPECT_EQ(20, sig.size());
+    sig = createSigForLengthCheck(HMAC::SHA1, 12);
+    EXPECT_EQ(12, sig.size());
+    sig = createSigForLengthCheck(HMAC::SHA1, 20);
+    EXPECT_EQ(20, sig.size());
+    sig = createSigForLengthCheck(HMAC::SHA1, 24);
+    EXPECT_EQ(20, sig.size());
+    sig = createSigForLengthCheck(HMAC::SHA1, 12345);
+    EXPECT_EQ(20, sig.size());
+
+    sig = createSigForLengthCheck(HMAC::SHA256, 0);
+    EXPECT_EQ(32, sig.size());
+    sig = createSigForLengthCheck(HMAC::SHA256, 12);
+    EXPECT_EQ(12, sig.size());
+    sig = createSigForLengthCheck(HMAC::SHA256, 32);
+    EXPECT_EQ(32, sig.size());
+    sig = createSigForLengthCheck(HMAC::SHA256, 36);
+    EXPECT_EQ(32, sig.size());
+    sig = createSigForLengthCheck(HMAC::SHA256, 12345);
+    EXPECT_EQ(32, sig.size());
+
+}
+
 TEST(CryptoTest, BadKey) {
     OutputBuffer data_buf(0);
     OutputBuffer hmac_sig(0);