]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
8 | class CryptoEnvironment: public ::testing::Environment { | |
9 | public: | |
10 | void SetUp() override { | |
9f95a23c | 11 | ceph::crypto::init(); |
7c673cae FG |
12 | } |
13 | }; | |
14 | ||
15 | TEST(MD5, Simple) { | |
16 | ceph::crypto::MD5 h; | |
11fdf7f2 | 17 | h.Update((const unsigned char*)"foo", 3); |
7c673cae FG |
18 | unsigned char digest[CEPH_CRYPTO_MD5_DIGESTSIZE]; |
19 | h.Final(digest); | |
20 | int err; | |
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, | |
24 | }; | |
25 | err = memcmp(digest, want_digest, CEPH_CRYPTO_MD5_DIGESTSIZE); | |
26 | ASSERT_EQ(0, err); | |
27 | } | |
28 | ||
29 | TEST(MD5, MultiUpdate) { | |
30 | ceph::crypto::MD5 h; | |
11fdf7f2 TL |
31 | h.Update((const unsigned char*)"", 0); |
32 | h.Update((const unsigned char*)"fo", 2); | |
33 | h.Update((const unsigned char*)"", 0); | |
34 | h.Update((const unsigned char*)"o", 1); | |
35 | h.Update((const unsigned char*)"", 0); | |
7c673cae FG |
36 | unsigned char digest[CEPH_CRYPTO_MD5_DIGESTSIZE]; |
37 | h.Final(digest); | |
38 | int err; | |
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, | |
42 | }; | |
43 | err = memcmp(digest, want_digest, CEPH_CRYPTO_MD5_DIGESTSIZE); | |
44 | ASSERT_EQ(0, err); | |
45 | } | |
46 | ||
47 | TEST(MD5, Restart) { | |
48 | ceph::crypto::MD5 h; | |
11fdf7f2 | 49 | h.Update((const unsigned char*)"bar", 3); |
7c673cae | 50 | h.Restart(); |
11fdf7f2 | 51 | h.Update((const unsigned char*)"foo", 3); |
7c673cae FG |
52 | unsigned char digest[CEPH_CRYPTO_MD5_DIGESTSIZE]; |
53 | h.Final(digest); | |
54 | int err; | |
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, | |
58 | }; | |
59 | err = memcmp(digest, want_digest, CEPH_CRYPTO_MD5_DIGESTSIZE); | |
60 | ASSERT_EQ(0, err); | |
61 | } | |
62 | ||
63 | TEST(HMACSHA1, Simple) { | |
11fdf7f2 TL |
64 | ceph::crypto::HMACSHA1 h((const unsigned char*)"sekrit", 6); |
65 | h.Update((const unsigned char*)"foo", 3); | |
7c673cae FG |
66 | unsigned char digest[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE]; |
67 | h.Final(digest); | |
68 | int err; | |
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, | |
72 | }; | |
73 | err = memcmp(digest, want_digest, CEPH_CRYPTO_HMACSHA1_DIGESTSIZE); | |
74 | ASSERT_EQ(0, err); | |
75 | } | |
76 | ||
77 | TEST(HMACSHA1, MultiUpdate) { | |
11fdf7f2 TL |
78 | ceph::crypto::HMACSHA1 h((const unsigned char*)"sekrit", 6); |
79 | h.Update((const unsigned char*)"", 0); | |
80 | h.Update((const unsigned char*)"fo", 2); | |
81 | h.Update((const unsigned char*)"", 0); | |
82 | h.Update((const unsigned char*)"o", 1); | |
83 | h.Update((const unsigned char*)"", 0); | |
7c673cae FG |
84 | unsigned char digest[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE]; |
85 | h.Final(digest); | |
86 | int err; | |
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, | |
90 | }; | |
91 | err = memcmp(digest, want_digest, CEPH_CRYPTO_HMACSHA1_DIGESTSIZE); | |
92 | ASSERT_EQ(0, err); | |
93 | } | |
94 | ||
95 | TEST(HMACSHA1, Restart) { | |
11fdf7f2 TL |
96 | ceph::crypto::HMACSHA1 h((const unsigned char*)"sekrit", 6); |
97 | h.Update((const unsigned char*)"bar", 3); | |
7c673cae | 98 | h.Restart(); |
11fdf7f2 | 99 | h.Update((const unsigned char*)"foo", 3); |
7c673cae FG |
100 | unsigned char digest[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE]; |
101 | h.Final(digest); | |
102 | int err; | |
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, | |
106 | }; | |
107 | err = memcmp(digest, want_digest, CEPH_CRYPTO_HMACSHA1_DIGESTSIZE); | |
108 | ASSERT_EQ(0, err); | |
109 | } | |
110 | ||
9f95a23c TL |
111 | TEST(Digest, SHA1) { |
112 | auto digest = [](const bufferlist& bl) { | |
113 | return ceph::crypto::digest<ceph::crypto::SHA1>(bl); | |
114 | }; | |
115 | { | |
116 | bufferlist bl; | |
117 | sha1_digest_t sha1 = digest(bl); | |
118 | EXPECT_EQ("da39a3ee5e6b4b0d3255bfef95601890afd80709", sha1.to_str()); | |
119 | } | |
120 | { | |
121 | bufferlist bl; | |
122 | bl.append(""); | |
123 | sha1_digest_t sha1 = digest(bl); | |
124 | EXPECT_EQ("da39a3ee5e6b4b0d3255bfef95601890afd80709", sha1.to_str()); | |
125 | } | |
126 | { | |
127 | bufferlist bl; | |
128 | bl.append("Hello"); | |
129 | sha1_digest_t sha1 = digest(bl); | |
130 | EXPECT_EQ("f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0", sha1.to_str()); | |
131 | } | |
132 | { | |
133 | bufferlist bl, bl2; | |
134 | bl.append("Hello"); | |
135 | bl2.append(", world!"); | |
136 | bl.claim_append(bl2); | |
137 | sha1_digest_t sha1 = digest(bl); | |
138 | EXPECT_EQ("943a702d06f34599aee1f8da8ef9f7296031d699", sha1.to_str()); | |
139 | bl2.append(" How are you today?"); | |
140 | bl.claim_append(bl2); | |
141 | sha1 = digest(bl); | |
142 | EXPECT_EQ("778b5d10e5133aa28fb8de71d35b6999b9a25eb4", sha1.to_str()); | |
143 | } | |
144 | { | |
145 | bufferptr p(65536); | |
146 | memset(p.c_str(), 0, 65536); | |
147 | bufferlist bl; | |
148 | bl.append(p); | |
149 | sha1_digest_t sha1 = digest(bl); | |
150 | EXPECT_EQ("1adc95bebe9eea8c112d40cd04ab7a8d75c4f961", sha1.to_str()); | |
151 | } | |
152 | } | |
153 | ||
154 | TEST(Digest, SHA256) { | |
155 | auto digest = [](const bufferlist& bl) { | |
156 | return ceph::crypto::digest<ceph::crypto::SHA256>(bl); | |
157 | }; | |
158 | { | |
159 | bufferlist bl; | |
160 | sha256_digest_t sha256 = digest(bl); | |
161 | EXPECT_EQ("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", sha256.to_str()); | |
162 | } | |
163 | { | |
164 | bufferlist bl; | |
165 | bl.append(""); | |
166 | sha256_digest_t sha256 = digest(bl); | |
167 | EXPECT_EQ("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", sha256.to_str()); | |
168 | } | |
169 | { | |
170 | bufferlist bl; | |
171 | bl.append("Hello"); | |
172 | sha256_digest_t sha256 = digest(bl); | |
173 | EXPECT_EQ("185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969", sha256.to_str()); | |
174 | } | |
175 | { | |
176 | bufferlist bl, bl2; | |
177 | bl.append("Hello"); | |
178 | bl2.append(", world!"); | |
179 | bl.claim_append(bl2); | |
180 | sha256_digest_t sha256 = digest(bl); | |
181 | EXPECT_EQ("315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3", sha256.to_str()); | |
182 | bl2.append(" How are you today?"); | |
183 | bl.claim_append(bl2); | |
184 | sha256 = digest(bl); | |
185 | EXPECT_EQ("e85f57f8bb018bd4f7beed6f27488cef22b13d5e06e8b8a27cac8b087c2a549e", sha256.to_str()); | |
186 | } | |
187 | { | |
188 | bufferptr p(65536); | |
189 | memset(p.c_str(), 0, 65536); | |
190 | bufferlist bl; | |
191 | bl.append(p); | |
192 | sha256_digest_t sha256 = digest(bl); | |
193 | EXPECT_EQ("de2f256064a0af797747c2b97505dc0b9f3df0de4f489eac731c23ae9ca9cc31", sha256.to_str()); | |
194 | } | |
195 | } | |
196 | ||
197 | TEST(Digest, SHA512) { | |
198 | auto digest = [](const bufferlist& bl) { | |
199 | return ceph::crypto::digest<ceph::crypto::SHA512>(bl); | |
200 | }; | |
201 | { | |
202 | bufferlist bl; | |
203 | sha512_digest_t sha512 = digest(bl); | |
204 | EXPECT_EQ("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", sha512.to_str()); | |
205 | } | |
206 | { | |
207 | bufferlist bl; | |
208 | bl.append(""); | |
209 | sha512_digest_t sha512 = digest(bl); | |
210 | EXPECT_EQ("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", sha512.to_str()); | |
211 | } | |
212 | { | |
213 | bufferlist bl; | |
214 | bl.append("Hello"); | |
215 | sha512_digest_t sha512 = digest(bl); | |
216 | EXPECT_EQ("3615f80c9d293ed7402687f94b22d58e529b8cc7916f8fac7fddf7fbd5af4cf777d3d795a7a00a16bf7e7f3fb9561ee9baae480da9fe7a18769e71886b03f315", sha512.to_str()); | |
217 | } | |
218 | { | |
219 | bufferlist bl, bl2; | |
220 | bl.append("Hello"); | |
221 | bl2.append(", world!"); | |
222 | bl.claim_append(bl2); | |
223 | sha512_digest_t sha512 = digest(bl); | |
224 | EXPECT_EQ("c1527cd893c124773d811911970c8fe6e857d6df5dc9226bd8a160614c0cd963a4ddea2b94bb7d36021ef9d865d5cea294a82dd49a0bb269f51f6e7a57f79421", sha512.to_str()); | |
225 | bl2.append(" How are you today?"); | |
226 | bl.claim_append(bl2); | |
227 | sha512 = digest(bl); | |
228 | EXPECT_EQ("7d50e299496754f9a0d158e018d4b733f2ef51c487b43b50719ffdabe3c3da5a347029741056887b4ffa2ddd0aa9e0dd358b8ed9da9a4f3455f44896fc8e5395", sha512.to_str()); | |
229 | } | |
230 | { | |
231 | bufferptr p(65536); | |
232 | memset(p.c_str(), 0, 65536); | |
233 | bufferlist bl; | |
234 | bl.append(p); | |
235 | sha512_digest_t sha512 = digest(bl); | |
236 | EXPECT_EQ("73e4153936dab198397b74ee9efc26093dda721eaab2f8d92786891153b45b04265a161b169c988edb0db2c53124607b6eaaa816559c5ce54f3dbc9fa6a7a4b2", sha512.to_str()); | |
237 | } | |
238 | } | |
239 | ||
7c673cae FG |
240 | class ForkDeathTest : public ::testing::Test { |
241 | protected: | |
242 | void SetUp() override { | |
243 | // shutdown NSS so it can be reinitialized after the fork | |
244 | // some data structures used by NSPR are only initialized once, and they | |
245 | // will be cleaned up with ceph::crypto::shutdown(false), so we need to | |
246 | // keep them around after fork. | |
247 | ceph::crypto::shutdown(true); | |
248 | } | |
249 | ||
250 | void TearDown() override { | |
251 | // undo the NSS shutdown we did in the parent process, after the | |
252 | // test is done | |
9f95a23c | 253 | ceph::crypto::init(); |
7c673cae FG |
254 | } |
255 | }; | |
256 | ||
257 | void do_simple_crypto() { | |
258 | // ensure that the shutdown/fork/init sequence results in a working | |
259 | // NSS crypto library; this function is run in the child, after the | |
260 | // fork, and if you comment out the ceph::crypto::init, or if the | |
261 | // trick were to fail, you would see this ending in an assert and | |
262 | // not exit status 0 | |
9f95a23c | 263 | ceph::crypto::init(); |
7c673cae | 264 | ceph::crypto::MD5 h; |
11fdf7f2 | 265 | h.Update((const unsigned char*)"foo", 3); |
7c673cae FG |
266 | unsigned char digest[CEPH_CRYPTO_MD5_DIGESTSIZE]; |
267 | h.Final(digest); | |
268 | exit(0); | |
269 | } | |
270 | ||
271 | #if GTEST_HAS_DEATH_TEST | |
272 | TEST_F(ForkDeathTest, MD5) { | |
273 | ASSERT_EXIT(do_simple_crypto(), ::testing::ExitedWithCode(0), "^$"); | |
274 | } | |
275 | #endif //GTEST_HAS_DEATH_TEST | |
276 | ||
277 | int main(int argc, char **argv) { | |
278 | std::vector<const char*> args(argv, argv + argc); | |
7c673cae FG |
279 | auto cct = global_init(NULL, args, |
280 | CEPH_ENTITY_TYPE_CLIENT, | |
281 | CODE_ENVIRONMENT_UTILITY, | |
282 | CINIT_FLAG_NO_DEFAULT_CONFIG_FILE); | |
283 | common_init_finish(g_ceph_context); | |
284 | ::testing::InitGoogleTest(&argc, argv); | |
285 | return RUN_ALL_TESTS(); | |
286 | } |