hash-test.cc 3.0 KB

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