2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/ssl/OpenSSLHash.h>
19 #include <folly/io/IOBufQueue.h>
20 #include <folly/portability/GTest.h>
23 using namespace folly;
24 using namespace folly::ssl;
28 class OpenSSLHashTest : public testing::Test {};
32 TEST_F(OpenSSLHashTest, sha256) {
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());
39 auto expected = vector<uint8_t>(32);
40 auto combined = ByteRange(StringPiece("foobar"));
41 SHA256(combined.data(), combined.size(), expected.data());
43 auto out = vector<uint8_t>(32);
44 OpenSSLHash::sha256(range(out), buf);
45 EXPECT_EQ(expected, out);
48 TEST_F(OpenSSLHashTest, hmac_sha256) {
49 auto key = ByteRange(StringPiece("qwerty"));
52 buf.prependChain(IOBuf::wrapBuffer(ByteRange(StringPiece("foo"))));
53 buf.prependChain(IOBuf::wrapBuffer(ByteRange(StringPiece("bar"))));
54 EXPECT_EQ(3, buf.countChainElements());
55 EXPECT_EQ(6, buf.computeChainDataLength());
57 auto expected = vector<uint8_t>(32);
58 auto combined = ByteRange(StringPiece("foobar"));
61 key.data(), key.size(),
62 combined.data(), combined.size(),
63 expected.data(), nullptr);
65 auto out = vector<uint8_t>(32);
66 OpenSSLHash::hmac_sha256(range(out), key, buf);
67 EXPECT_EQ(expected, out);