proxygen
OpenSSLHashTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-present 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>
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 } // namespace
30 
31 TEST_F(OpenSSLHashTest, sha256) {
32  IOBuf buf;
33  buf.prependChain(IOBuf::wrapBuffer(ByteRange(StringPiece("foo"))));
34  buf.prependChain(IOBuf::wrapBuffer(ByteRange(StringPiece("bar"))));
35  EXPECT_EQ(3, buf.countChainElements());
37 
38  auto expected = vector<uint8_t>(32);
39  auto combined = ByteRange(StringPiece("foobar"));
40  SHA256(combined.data(), combined.size(), expected.data());
41 
42  auto out = vector<uint8_t>(32);
43  OpenSSLHash::sha256(range(out), buf);
44  EXPECT_EQ(expected, out);
45 }
46 
47 TEST_F(OpenSSLHashTest, sha256_hashcopy) {
48  std::array<uint8_t, 32> expected, actual;
49 
50  OpenSSLHash::Digest digest;
51  digest.hash_init(EVP_sha256());
52  digest.hash_update(ByteRange(StringPiece("foobar")));
53 
54  OpenSSLHash::Digest copy(digest);
55 
56  digest.hash_final(range(expected));
57  copy.hash_final(range(actual));
58 
59  EXPECT_EQ(expected, actual);
60 }
61 
62 TEST_F(OpenSSLHashTest, sha256_hashcopy_intermediate) {
63  std::array<uint8_t, 32> expected, actual;
64 
65  OpenSSLHash::Digest digest;
66  digest.hash_init(EVP_sha256());
67  digest.hash_update(ByteRange(StringPiece("foo")));
68 
69  OpenSSLHash::Digest copy(digest);
70 
71  digest.hash_update(ByteRange(StringPiece("bar")));
72  copy.hash_update(ByteRange(StringPiece("bar")));
73 
74  digest.hash_final(range(expected));
75  copy.hash_final(range(actual));
76 
77  EXPECT_EQ(expected, actual);
78 }
79 
80 TEST_F(OpenSSLHashTest, hmac_sha256) {
81  auto key = ByteRange(StringPiece("qwerty"));
82 
83  IOBuf buf;
84  buf.prependChain(IOBuf::wrapBuffer(ByteRange(StringPiece("foo"))));
85  buf.prependChain(IOBuf::wrapBuffer(ByteRange(StringPiece("bar"))));
86  EXPECT_EQ(3, buf.countChainElements());
88 
89  auto expected = vector<uint8_t>(32);
90  auto combined = ByteRange(StringPiece("foobar"));
91  HMAC(
92  EVP_sha256(),
93  key.data(),
94  int(key.size()),
95  combined.data(),
96  combined.size(),
97  expected.data(),
98  nullptr);
99 
100  auto out = vector<uint8_t>(32);
101  OpenSSLHash::hmac_sha256(range(out), key, buf);
102  EXPECT_EQ(expected, out);
103 }
void hash_init(const EVP_MD *md)
Definition: OpenSSLHash.h:49
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
STL namespace.
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
size_t countChainElements() const
Definition: IOBuf.cpp:493
Gen range(Value begin, Value end)
Definition: Base.h:467
constexpr std::decay< T >::type copy(T &&value) noexcept(noexcept(typename std::decay< T >::type(std::forward< T >(value))))
Definition: Utility.h:72
void hash_final(MutableByteRange out)
Definition: OpenSSLHash.h:62
void hash_update(ByteRange data)
Definition: OpenSSLHash.h:53
void prependChain(std::unique_ptr< IOBuf > &&iobuf)
Definition: IOBuf.cpp:509
std::size_t computeChainDataLength() const
Definition: IOBuf.cpp:501
Range< const unsigned char * > ByteRange
Definition: Range.h:1163
Range< const char * > StringPiece
TEST_F(OpenSSLHashTest, sha256)