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"
8 class CryptoEnvironment
: public ::testing::Environment
{
10 void SetUp() override
{
11 ceph::crypto::init(g_ceph_context
);
17 h
.Update((const byte
*)"foo", 3);
18 unsigned char digest
[CEPH_CRYPTO_MD5_DIGESTSIZE
];
21 unsigned char want_digest
[CEPH_CRYPTO_MD5_DIGESTSIZE
] = {
22 0xac, 0xbd, 0x18, 0xdb, 0x4c, 0xc2, 0xf8, 0x5c,
23 0xed, 0xef, 0x65, 0x4f, 0xcc, 0xc4, 0xa4, 0xd8,
25 err
= memcmp(digest
, want_digest
, CEPH_CRYPTO_MD5_DIGESTSIZE
);
29 TEST(MD5
, MultiUpdate
) {
31 h
.Update((const byte
*)"", 0);
32 h
.Update((const byte
*)"fo", 2);
33 h
.Update((const byte
*)"", 0);
34 h
.Update((const byte
*)"o", 1);
35 h
.Update((const byte
*)"", 0);
36 unsigned char digest
[CEPH_CRYPTO_MD5_DIGESTSIZE
];
39 unsigned char want_digest
[CEPH_CRYPTO_MD5_DIGESTSIZE
] = {
40 0xac, 0xbd, 0x18, 0xdb, 0x4c, 0xc2, 0xf8, 0x5c,
41 0xed, 0xef, 0x65, 0x4f, 0xcc, 0xc4, 0xa4, 0xd8,
43 err
= memcmp(digest
, want_digest
, CEPH_CRYPTO_MD5_DIGESTSIZE
);
49 h
.Update((const byte
*)"bar", 3);
51 h
.Update((const byte
*)"foo", 3);
52 unsigned char digest
[CEPH_CRYPTO_MD5_DIGESTSIZE
];
55 unsigned char want_digest
[CEPH_CRYPTO_MD5_DIGESTSIZE
] = {
56 0xac, 0xbd, 0x18, 0xdb, 0x4c, 0xc2, 0xf8, 0x5c,
57 0xed, 0xef, 0x65, 0x4f, 0xcc, 0xc4, 0xa4, 0xd8,
59 err
= memcmp(digest
, want_digest
, CEPH_CRYPTO_MD5_DIGESTSIZE
);
63 TEST(HMACSHA1
, Simple
) {
64 ceph::crypto::HMACSHA1
h((const byte
*)"sekrit", 6);
65 h
.Update((const byte
*)"foo", 3);
66 unsigned char digest
[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE
];
69 unsigned char want_digest
[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE
] = {
70 0x04, 0xbc, 0x52, 0x66, 0xb6, 0xff, 0xad, 0xad, 0x9d, 0x57,
71 0xce, 0x13, 0xea, 0x8c, 0xf5, 0x6b, 0xf9, 0x95, 0x2f, 0xd6,
73 err
= memcmp(digest
, want_digest
, CEPH_CRYPTO_HMACSHA1_DIGESTSIZE
);
77 TEST(HMACSHA1
, MultiUpdate
) {
78 ceph::crypto::HMACSHA1
h((const byte
*)"sekrit", 6);
79 h
.Update((const byte
*)"", 0);
80 h
.Update((const byte
*)"fo", 2);
81 h
.Update((const byte
*)"", 0);
82 h
.Update((const byte
*)"o", 1);
83 h
.Update((const byte
*)"", 0);
84 unsigned char digest
[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE
];
87 unsigned char want_digest
[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE
] = {
88 0x04, 0xbc, 0x52, 0x66, 0xb6, 0xff, 0xad, 0xad, 0x9d, 0x57,
89 0xce, 0x13, 0xea, 0x8c, 0xf5, 0x6b, 0xf9, 0x95, 0x2f, 0xd6,
91 err
= memcmp(digest
, want_digest
, CEPH_CRYPTO_HMACSHA1_DIGESTSIZE
);
95 TEST(HMACSHA1
, Restart
) {
96 ceph::crypto::HMACSHA1
h((const byte
*)"sekrit", 6);
97 h
.Update((const byte
*)"bar", 3);
99 h
.Update((const byte
*)"foo", 3);
100 unsigned char digest
[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE
];
103 unsigned char want_digest
[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE
] = {
104 0x04, 0xbc, 0x52, 0x66, 0xb6, 0xff, 0xad, 0xad, 0x9d, 0x57,
105 0xce, 0x13, 0xea, 0x8c, 0xf5, 0x6b, 0xf9, 0x95, 0x2f, 0xd6,
107 err
= memcmp(digest
, want_digest
, CEPH_CRYPTO_HMACSHA1_DIGESTSIZE
);
111 class ForkDeathTest
: public ::testing::Test
{
113 void SetUp() override
{
114 // shutdown NSS so it can be reinitialized after the fork
115 // some data structures used by NSPR are only initialized once, and they
116 // will be cleaned up with ceph::crypto::shutdown(false), so we need to
117 // keep them around after fork.
118 ceph::crypto::shutdown(true);
121 void TearDown() override
{
122 // undo the NSS shutdown we did in the parent process, after the
124 ceph::crypto::init(g_ceph_context
);
128 void do_simple_crypto() {
129 // ensure that the shutdown/fork/init sequence results in a working
130 // NSS crypto library; this function is run in the child, after the
131 // fork, and if you comment out the ceph::crypto::init, or if the
132 // trick were to fail, you would see this ending in an assert and
134 ceph::crypto::init(g_ceph_context
);
136 h
.Update((const byte
*)"foo", 3);
137 unsigned char digest
[CEPH_CRYPTO_MD5_DIGESTSIZE
];
142 #if GTEST_HAS_DEATH_TEST
143 TEST_F(ForkDeathTest
, MD5
) {
144 ASSERT_EXIT(do_simple_crypto(), ::testing::ExitedWithCode(0), "^$");
146 #endif //GTEST_HAS_DEATH_TEST
148 int main(int argc
, char **argv
) {
149 std::vector
<const char*> args(argv
, argv
+ argc
);
151 auto cct
= global_init(NULL
, args
,
152 CEPH_ENTITY_TYPE_CLIENT
,
153 CODE_ENVIRONMENT_UTILITY
,
154 CINIT_FLAG_NO_DEFAULT_CONFIG_FILE
);
155 common_init_finish(g_ceph_context
);
156 ::testing::InitGoogleTest(&argc
, argv
);
157 return RUN_ALL_TESTS();