hash-test.cc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*############################################################################
  2. # Copyright 2016-2017 Intel Corporation
  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. * \file
  18. * \brief Hash unit tests.
  19. */
  20. #include <cstring>
  21. #include <limits>
  22. #include "epid/common-testhelper/epid_gtest-testhelper.h"
  23. #include "gtest/gtest.h"
  24. #include "epid/common-testhelper/errors-testhelper.h"
  25. extern "C" {
  26. #include "epid/common/math/hash.h"
  27. }
  28. /// compares Sha256Digest values
  29. bool operator==(Sha256Digest const& lhs, Sha256Digest const& rhs) {
  30. return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
  31. }
  32. namespace {
  33. ///////////////////////////////////////////////////////////////////////
  34. // SHA256
  35. TEST(Hash, Sha256MessageDigestFailsGivenNullPtr) {
  36. char msg[] = "abc";
  37. Sha256Digest digest;
  38. EXPECT_EQ(kEpidBadArgErr,
  39. Sha256MessageDigest(nullptr, sizeof(msg) - 1, &digest));
  40. EXPECT_EQ(kEpidBadArgErr, Sha256MessageDigest(msg, sizeof(msg) - 1, nullptr));
  41. }
  42. TEST(Hash, Sha256MessageDigestFailsGivenInvalidBufferSize) {
  43. char msg[] = "abc";
  44. Sha256Digest digest;
  45. EXPECT_EQ(
  46. kEpidBadArgErr,
  47. Sha256MessageDigest(msg, std::numeric_limits<size_t>::max(), &digest));
  48. #if (SIZE_MAX >= 0x100000001) // When size_t value allowed to be 0x100000001
  49. EXPECT_EQ(kEpidBadArgErr, Sha256MessageDigest(msg, 0x100000001, &digest));
  50. #endif
  51. }
  52. TEST(Hash, Sha256MessageDigestComputesCorrectDigest) {
  53. // Test vectors here are taken from
  54. // http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA256.pdf
  55. Sha256Digest digest;
  56. char msg_abc[] = "abc";
  57. Sha256Digest digest_abc = {{0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA,
  58. 0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23,
  59. 0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C,
  60. 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD}};
  61. EXPECT_EQ(kEpidNoErr,
  62. Sha256MessageDigest(msg_abc, sizeof(msg_abc) - 1, &digest));
  63. EXPECT_EQ(digest_abc, digest);
  64. char msg_long[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  65. Sha256Digest digest_long = {{0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8,
  66. 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39,
  67. 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67,
  68. 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1}};
  69. EXPECT_EQ(kEpidNoErr,
  70. Sha256MessageDigest(msg_long, sizeof(msg_long) - 1, &digest));
  71. EXPECT_EQ(digest_long, digest);
  72. }
  73. } // namespace