Provide a copy ctor for Digest that copies the current hash context
[folly.git] / folly / ssl / test / OpenSSLHashTest.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <folly/ssl/OpenSSLHash.h>
18
19 #include <folly/io/IOBufQueue.h>
20 #include <folly/portability/GTest.h>
21
22 using namespace std;
23 using namespace folly;
24 using namespace folly::ssl;
25
26 namespace {
27
28 class OpenSSLHashTest : public testing::Test {};
29
30 }
31
32 TEST_F(OpenSSLHashTest, sha256) {
33   IOBuf buf;
34   buf.prependChain(IOBuf::wrapBuffer(ByteRange(StringPiece("foo"))));
35   buf.prependChain(IOBuf::wrapBuffer(ByteRange(StringPiece("bar"))));
36   EXPECT_EQ(3, buf.countChainElements());
37   EXPECT_EQ(6, buf.computeChainDataLength());
38
39   auto expected = vector<uint8_t>(32);
40   auto combined = ByteRange(StringPiece("foobar"));
41   SHA256(combined.data(), combined.size(), expected.data());
42
43   auto out = vector<uint8_t>(32);
44   OpenSSLHash::sha256(range(out), buf);
45   EXPECT_EQ(expected, out);
46 }
47
48 TEST_F(OpenSSLHashTest, sha256_hashcopy) {
49   std::array<uint8_t, 32> expected, actual;
50
51   OpenSSLHash::Digest digest;
52   digest.hash_init(EVP_sha256());
53   digest.hash_update(ByteRange(StringPiece("foobar")));
54
55   OpenSSLHash::Digest copy(digest);
56
57   digest.hash_final(range(expected));
58   copy.hash_final(range(actual));
59
60   EXPECT_EQ(expected, actual);
61 }
62
63 TEST_F(OpenSSLHashTest, sha256_hashcopy_intermediate) {
64   std::array<uint8_t, 32> expected, actual;
65
66   OpenSSLHash::Digest digest;
67   digest.hash_init(EVP_sha256());
68   digest.hash_update(ByteRange(StringPiece("foo")));
69
70   OpenSSLHash::Digest copy(digest);
71
72   digest.hash_update(ByteRange(StringPiece("bar")));
73   copy.hash_update(ByteRange(StringPiece("bar")));
74
75   digest.hash_final(range(expected));
76   copy.hash_final(range(actual));
77
78   EXPECT_EQ(expected, actual);
79 }
80
81 TEST_F(OpenSSLHashTest, hmac_sha256) {
82   auto key = ByteRange(StringPiece("qwerty"));
83
84   IOBuf buf;
85   buf.prependChain(IOBuf::wrapBuffer(ByteRange(StringPiece("foo"))));
86   buf.prependChain(IOBuf::wrapBuffer(ByteRange(StringPiece("bar"))));
87   EXPECT_EQ(3, buf.countChainElements());
88   EXPECT_EQ(6, buf.computeChainDataLength());
89
90   auto expected = vector<uint8_t>(32);
91   auto combined = ByteRange(StringPiece("foobar"));
92   HMAC(
93       EVP_sha256(),
94       key.data(),
95       int(key.size()),
96       combined.data(),
97       combined.size(),
98       expected.data(),
99       nullptr);
100
101   auto out = vector<uint8_t>(32);
102   OpenSSLHash::hmac_sha256(range(out), key, buf);
103   EXPECT_EQ(expected, out);
104 }