OpenSSLHashTest.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #include <folly/ssl/OpenSSLHash.h>
  17. #include <folly/io/IOBufQueue.h>
  18. #include <folly/portability/GTest.h>
  19. using namespace std;
  20. using namespace folly;
  21. using namespace folly::ssl;
  22. namespace {
  23. class OpenSSLHashTest : public testing::Test {};
  24. } // namespace
  25. TEST_F(OpenSSLHashTest, sha256) {
  26. IOBuf buf;
  27. buf.prependChain(IOBuf::wrapBuffer(ByteRange(StringPiece("foo"))));
  28. buf.prependChain(IOBuf::wrapBuffer(ByteRange(StringPiece("bar"))));
  29. EXPECT_EQ(3, buf.countChainElements());
  30. EXPECT_EQ(6, buf.computeChainDataLength());
  31. auto expected = vector<uint8_t>(32);
  32. auto combined = ByteRange(StringPiece("foobar"));
  33. SHA256(combined.data(), combined.size(), expected.data());
  34. auto out = vector<uint8_t>(32);
  35. OpenSSLHash::sha256(range(out), buf);
  36. EXPECT_EQ(expected, out);
  37. }
  38. TEST_F(OpenSSLHashTest, sha256_hashcopy) {
  39. std::array<uint8_t, 32> expected, actual;
  40. OpenSSLHash::Digest digest;
  41. digest.hash_init(EVP_sha256());
  42. digest.hash_update(ByteRange(StringPiece("foobar")));
  43. OpenSSLHash::Digest copy(digest);
  44. digest.hash_final(range(expected));
  45. copy.hash_final(range(actual));
  46. EXPECT_EQ(expected, actual);
  47. }
  48. TEST_F(OpenSSLHashTest, sha256_hashcopy_intermediate) {
  49. std::array<uint8_t, 32> expected, actual;
  50. OpenSSLHash::Digest digest;
  51. digest.hash_init(EVP_sha256());
  52. digest.hash_update(ByteRange(StringPiece("foo")));
  53. OpenSSLHash::Digest copy(digest);
  54. digest.hash_update(ByteRange(StringPiece("bar")));
  55. copy.hash_update(ByteRange(StringPiece("bar")));
  56. digest.hash_final(range(expected));
  57. copy.hash_final(range(actual));
  58. EXPECT_EQ(expected, actual);
  59. }
  60. TEST_F(OpenSSLHashTest, hmac_sha256) {
  61. auto key = ByteRange(StringPiece("qwerty"));
  62. IOBuf buf;
  63. buf.prependChain(IOBuf::wrapBuffer(ByteRange(StringPiece("foo"))));
  64. buf.prependChain(IOBuf::wrapBuffer(ByteRange(StringPiece("bar"))));
  65. EXPECT_EQ(3, buf.countChainElements());
  66. EXPECT_EQ(6, buf.computeChainDataLength());
  67. auto expected = vector<uint8_t>(32);
  68. auto combined = ByteRange(StringPiece("foobar"));
  69. HMAC(
  70. EVP_sha256(),
  71. key.data(),
  72. int(key.size()),
  73. combined.data(),
  74. combined.size(),
  75. expected.data(),
  76. nullptr);
  77. auto out = vector<uint8_t>(32);
  78. OpenSSLHash::hmac_sha256(range(out), key, buf);
  79. EXPECT_EQ(expected, out);
  80. }