/src/test/ceph_crypto.cc

https://gitlab.com/unofficial-mirrors/ceph · C++ · 157 lines · 134 code · 12 blank · 11 comment · 0 complexity · 671bdfef1aff3f16c135bc68387bb789 MD5 · raw file

  1. #include "gtest/gtest.h"
  2. #include "common/ceph_argparse.h"
  3. #include "common/ceph_crypto.h"
  4. #include "common/common_init.h"
  5. #include "global/global_init.h"
  6. #include "global/global_context.h"
  7. class CryptoEnvironment: public ::testing::Environment {
  8. public:
  9. void SetUp() override {
  10. ceph::crypto::init(g_ceph_context);
  11. }
  12. };
  13. TEST(MD5, Simple) {
  14. ceph::crypto::MD5 h;
  15. h.Update((const unsigned char*)"foo", 3);
  16. unsigned char digest[CEPH_CRYPTO_MD5_DIGESTSIZE];
  17. h.Final(digest);
  18. int err;
  19. unsigned char want_digest[CEPH_CRYPTO_MD5_DIGESTSIZE] = {
  20. 0xac, 0xbd, 0x18, 0xdb, 0x4c, 0xc2, 0xf8, 0x5c,
  21. 0xed, 0xef, 0x65, 0x4f, 0xcc, 0xc4, 0xa4, 0xd8,
  22. };
  23. err = memcmp(digest, want_digest, CEPH_CRYPTO_MD5_DIGESTSIZE);
  24. ASSERT_EQ(0, err);
  25. }
  26. TEST(MD5, MultiUpdate) {
  27. ceph::crypto::MD5 h;
  28. h.Update((const unsigned char*)"", 0);
  29. h.Update((const unsigned char*)"fo", 2);
  30. h.Update((const unsigned char*)"", 0);
  31. h.Update((const unsigned char*)"o", 1);
  32. h.Update((const unsigned char*)"", 0);
  33. unsigned char digest[CEPH_CRYPTO_MD5_DIGESTSIZE];
  34. h.Final(digest);
  35. int err;
  36. unsigned char want_digest[CEPH_CRYPTO_MD5_DIGESTSIZE] = {
  37. 0xac, 0xbd, 0x18, 0xdb, 0x4c, 0xc2, 0xf8, 0x5c,
  38. 0xed, 0xef, 0x65, 0x4f, 0xcc, 0xc4, 0xa4, 0xd8,
  39. };
  40. err = memcmp(digest, want_digest, CEPH_CRYPTO_MD5_DIGESTSIZE);
  41. ASSERT_EQ(0, err);
  42. }
  43. TEST(MD5, Restart) {
  44. ceph::crypto::MD5 h;
  45. h.Update((const unsigned char*)"bar", 3);
  46. h.Restart();
  47. h.Update((const unsigned char*)"foo", 3);
  48. unsigned char digest[CEPH_CRYPTO_MD5_DIGESTSIZE];
  49. h.Final(digest);
  50. int err;
  51. unsigned char want_digest[CEPH_CRYPTO_MD5_DIGESTSIZE] = {
  52. 0xac, 0xbd, 0x18, 0xdb, 0x4c, 0xc2, 0xf8, 0x5c,
  53. 0xed, 0xef, 0x65, 0x4f, 0xcc, 0xc4, 0xa4, 0xd8,
  54. };
  55. err = memcmp(digest, want_digest, CEPH_CRYPTO_MD5_DIGESTSIZE);
  56. ASSERT_EQ(0, err);
  57. }
  58. TEST(HMACSHA1, Simple) {
  59. ceph::crypto::HMACSHA1 h((const unsigned char*)"sekrit", 6);
  60. h.Update((const unsigned char*)"foo", 3);
  61. unsigned char digest[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE];
  62. h.Final(digest);
  63. int err;
  64. unsigned char want_digest[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE] = {
  65. 0x04, 0xbc, 0x52, 0x66, 0xb6, 0xff, 0xad, 0xad, 0x9d, 0x57,
  66. 0xce, 0x13, 0xea, 0x8c, 0xf5, 0x6b, 0xf9, 0x95, 0x2f, 0xd6,
  67. };
  68. err = memcmp(digest, want_digest, CEPH_CRYPTO_HMACSHA1_DIGESTSIZE);
  69. ASSERT_EQ(0, err);
  70. }
  71. TEST(HMACSHA1, MultiUpdate) {
  72. ceph::crypto::HMACSHA1 h((const unsigned char*)"sekrit", 6);
  73. h.Update((const unsigned char*)"", 0);
  74. h.Update((const unsigned char*)"fo", 2);
  75. h.Update((const unsigned char*)"", 0);
  76. h.Update((const unsigned char*)"o", 1);
  77. h.Update((const unsigned char*)"", 0);
  78. unsigned char digest[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE];
  79. h.Final(digest);
  80. int err;
  81. unsigned char want_digest[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE] = {
  82. 0x04, 0xbc, 0x52, 0x66, 0xb6, 0xff, 0xad, 0xad, 0x9d, 0x57,
  83. 0xce, 0x13, 0xea, 0x8c, 0xf5, 0x6b, 0xf9, 0x95, 0x2f, 0xd6,
  84. };
  85. err = memcmp(digest, want_digest, CEPH_CRYPTO_HMACSHA1_DIGESTSIZE);
  86. ASSERT_EQ(0, err);
  87. }
  88. TEST(HMACSHA1, Restart) {
  89. ceph::crypto::HMACSHA1 h((const unsigned char*)"sekrit", 6);
  90. h.Update((const unsigned char*)"bar", 3);
  91. h.Restart();
  92. h.Update((const unsigned char*)"foo", 3);
  93. unsigned char digest[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE];
  94. h.Final(digest);
  95. int err;
  96. unsigned char want_digest[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE] = {
  97. 0x04, 0xbc, 0x52, 0x66, 0xb6, 0xff, 0xad, 0xad, 0x9d, 0x57,
  98. 0xce, 0x13, 0xea, 0x8c, 0xf5, 0x6b, 0xf9, 0x95, 0x2f, 0xd6,
  99. };
  100. err = memcmp(digest, want_digest, CEPH_CRYPTO_HMACSHA1_DIGESTSIZE);
  101. ASSERT_EQ(0, err);
  102. }
  103. class ForkDeathTest : public ::testing::Test {
  104. protected:
  105. void SetUp() override {
  106. // shutdown NSS so it can be reinitialized after the fork
  107. // some data structures used by NSPR are only initialized once, and they
  108. // will be cleaned up with ceph::crypto::shutdown(false), so we need to
  109. // keep them around after fork.
  110. ceph::crypto::shutdown(true);
  111. }
  112. void TearDown() override {
  113. // undo the NSS shutdown we did in the parent process, after the
  114. // test is done
  115. ceph::crypto::init(g_ceph_context);
  116. }
  117. };
  118. void do_simple_crypto() {
  119. // ensure that the shutdown/fork/init sequence results in a working
  120. // NSS crypto library; this function is run in the child, after the
  121. // fork, and if you comment out the ceph::crypto::init, or if the
  122. // trick were to fail, you would see this ending in an assert and
  123. // not exit status 0
  124. ceph::crypto::init(g_ceph_context);
  125. ceph::crypto::MD5 h;
  126. h.Update((const unsigned char*)"foo", 3);
  127. unsigned char digest[CEPH_CRYPTO_MD5_DIGESTSIZE];
  128. h.Final(digest);
  129. exit(0);
  130. }
  131. #if GTEST_HAS_DEATH_TEST
  132. TEST_F(ForkDeathTest, MD5) {
  133. ASSERT_EXIT(do_simple_crypto(), ::testing::ExitedWithCode(0), "^$");
  134. }
  135. #endif //GTEST_HAS_DEATH_TEST
  136. int main(int argc, char **argv) {
  137. std::vector<const char*> args(argv, argv + argc);
  138. auto cct = global_init(NULL, args,
  139. CEPH_ENTITY_TYPE_CLIENT,
  140. CODE_ENVIRONMENT_UTILITY,
  141. CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
  142. common_init_finish(g_ceph_context);
  143. ::testing::InitGoogleTest(&argc, argv);
  144. return RUN_ALL_TESTS();
  145. }