]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph_crypto: Add unittests for the MD5 compatibility shim.
authorTommi Virtanen <tommi.virtanen@dreamhost.com>
Thu, 10 Mar 2011 19:48:51 +0000 (11:48 -0800)
committerTommi Virtanen <tommi.virtanen@dreamhost.com>
Fri, 11 Mar 2011 21:13:40 +0000 (13:13 -0800)
Signed-off-by: Tommi Virtanen <tommi.virtanen@dreamhost.com>
src/Makefile.am
src/test/ceph_crypto.cc [new file with mode: 0644]

index c2b9c44a8cdffa2d2941077514abf49446d9b47f..27fcfb393d1ae04a9b94b11e3165bd4df03f3fab 100644 (file)
@@ -403,6 +403,13 @@ unittest_crypto_LDADD =  libceph.la \
 unittest_crypto_CXXFLAGS = ${AM_CXXFLAGS} ${UNITTEST_CXXFLAGS}
 check_PROGRAMS += unittest_crypto
 
+unittest_ceph_crypto_SOURCES = test/ceph_crypto.cc
+unittest_ceph_crypto_LDFLAGS = ${CRYPTO_LDFLAGS} ${AM_LDFLAGS}
+unittest_ceph_crypto_LDADD = ${CRYPTO_LIBS} \
+                       ${UNITTEST_LDADD}
+unittest_ceph_crypto_CXXFLAGS = ${CRYPTO_CXXFLAGS} ${AM_CXXFLAGS} ${UNITTEST_CXXFLAGS}
+check_PROGRAMS += unittest_ceph_crypto
+
 # shell scripts
 editpaths = sed \
        -e 's|@bindir[@]|$(bindir)|g' \
diff --git a/src/test/ceph_crypto.cc b/src/test/ceph_crypto.cc
new file mode 100644 (file)
index 0000000..ff2b87f
--- /dev/null
@@ -0,0 +1,65 @@
+#include "common/ceph_crypto.h"
+
+#include "gtest/gtest.h"
+
+class CryptoEnvironment: public ::testing::Environment {
+public:
+  void SetUp() {
+    ceph::crypto::init();
+  }
+};
+
+::testing::Environment* const crypto_env = ::testing::AddGlobalTestEnvironment(new CryptoEnvironment);
+
+TEST(MD5, DigestSize) {
+  int s = ceph::crypto::MD5::DIGESTSIZE;
+  ASSERT_EQ(16, s);
+}
+
+TEST(MD5, Simple) {
+  ceph::crypto::MD5 h;
+  h.Update((const byte*)"foo", 3);
+  unsigned char digest[ceph::crypto::MD5::DIGESTSIZE];
+  h.Final(digest);
+  int err;
+  unsigned char want_digest[ceph::crypto::MD5::DIGESTSIZE] = {
+    0xac, 0xbd, 0x18, 0xdb, 0x4c, 0xc2, 0xf8, 0x5c,
+    0xed, 0xef, 0x65, 0x4f, 0xcc, 0xc4, 0xa4, 0xd8,
+  };
+  err = memcmp(digest, want_digest, ceph::crypto::MD5::DIGESTSIZE);
+  ASSERT_EQ(0, err);
+}
+
+TEST(MD5, MultiUpdate) {
+  ceph::crypto::MD5 h;
+  h.Update((const byte*)"", 0);
+  h.Update((const byte*)"fo", 2);
+  h.Update((const byte*)"", 0);
+  h.Update((const byte*)"o", 1);
+  h.Update((const byte*)"", 0);
+  unsigned char digest[ceph::crypto::MD5::DIGESTSIZE];
+  h.Final(digest);
+  int err;
+  unsigned char want_digest[ceph::crypto::MD5::DIGESTSIZE] = {
+    0xac, 0xbd, 0x18, 0xdb, 0x4c, 0xc2, 0xf8, 0x5c,
+    0xed, 0xef, 0x65, 0x4f, 0xcc, 0xc4, 0xa4, 0xd8,
+  };
+  err = memcmp(digest, want_digest, ceph::crypto::MD5::DIGESTSIZE);
+  ASSERT_EQ(0, err);
+}
+
+TEST(MD5, Restart) {
+  ceph::crypto::MD5 h;
+  h.Update((const byte*)"bar", 3);
+  h.Restart();
+  h.Update((const byte*)"foo", 3);
+  unsigned char digest[ceph::crypto::MD5::DIGESTSIZE];
+  h.Final(digest);
+  int err;
+  unsigned char want_digest[ceph::crypto::MD5::DIGESTSIZE] = {
+    0xac, 0xbd, 0x18, 0xdb, 0x4c, 0xc2, 0xf8, 0x5c,
+    0xed, 0xef, 0x65, 0x4f, 0xcc, 0xc4, 0xa4, 0xd8,
+  };
+  err = memcmp(digest, want_digest, ceph::crypto::MD5::DIGESTSIZE);
+  ASSERT_EQ(0, err);
+}