sha_ut.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include "sha.h"
  3. constexpr TStringBuf SomeAlignedShaTestData = "some _aligned_ test data for SHA-family: align align align align";
  4. Y_UNIT_TEST_SUITE(SHA){
  5. Y_UNIT_TEST(CheckOfTestDataAlignment){
  6. UNIT_ASSERT_VALUES_EQUAL(SomeAlignedShaTestData.size() % sizeof(ui32), 0);
  7. }
  8. Y_UNIT_TEST(Sha1Value) {
  9. // bash$ echo -n $SomeAlignedShaTestData | sha1sum
  10. const TStringBuf precalculatedDigest =
  11. "\xA2\x29\x8E\xE2\xEA\x06\x27\x45"
  12. "\x27\xC7\x78\x87\x16\x21\x8A\xA5"
  13. "\x0D\xBA\xBA\xB2"sv;
  14. auto digest = NOpenSsl::NSha1::Calc(SomeAlignedShaTestData.data(), SomeAlignedShaTestData.size());
  15. UNIT_ASSERT_VALUES_EQUAL(precalculatedDigest.size(), digest.size());
  16. UNIT_ASSERT_VALUES_EQUAL(memcmp(precalculatedDigest.data(), digest.data(), digest.size()), 0);
  17. }
  18. Y_UNIT_TEST(Sha256Value) {
  19. // bash$ echo -n $SomeAlignedShaTestData | sha256sum
  20. const TStringBuf precalculatedDigest =
  21. "\xED\x64\x0D\x43\xF7\x6D\x71\x98"
  22. "\x39\x19\xF6\xE6\x70\x21\x82\x11"
  23. "\xEF\x3B\xF0\xF4\x35\xBF\x42\xAB"
  24. "\x1C\x5C\x01\xCD\x20\x33\xD2\xFA"sv;
  25. auto digest = NOpenSsl::NSha256::Calc(SomeAlignedShaTestData.data(), SomeAlignedShaTestData.size());
  26. UNIT_ASSERT_VALUES_EQUAL(precalculatedDigest.size(), digest.size());
  27. UNIT_ASSERT_VALUES_EQUAL(memcmp(precalculatedDigest.data(), digest.data(), digest.size()), 0);
  28. }
  29. Y_UNIT_TEST(Sha224Value) {
  30. // bash$ echo -n $SomeAlignedShaTestData | sha224sum
  31. const TStringBuf precalculatedDigest =
  32. "\xD4\x8B\x12\xA8\x0B\x29\x01\x92"
  33. "\xC1\xF9\x2A\x71\x17\x99\x9C\x83"
  34. "\xDB\xC5\xBB\x7B\xBE\xC1\xF1\xD9"
  35. "\x97\x75\x38\xCB"sv;
  36. auto digest = NOpenSsl::NSha224::Calc(SomeAlignedShaTestData.data(), SomeAlignedShaTestData.size());
  37. UNIT_ASSERT_VALUES_EQUAL(precalculatedDigest.size(), digest.size());
  38. UNIT_ASSERT_VALUES_EQUAL(memcmp(precalculatedDigest.data(), digest.data(), digest.size()), 0);
  39. }
  40. Y_UNIT_TEST(FragmentedEqualNotFragmented) {
  41. const char* head = SomeAlignedShaTestData.data();
  42. const char* current = head;
  43. NOpenSsl::NSha1::TCalcer sha;
  44. int intValue;
  45. std::copy_n(current, sizeof(intValue), (char*)&intValue);
  46. current += sizeof(intValue);
  47. sha.UpdateWithPodValue(intValue);
  48. double doubleValue;
  49. std::copy_n(current, sizeof(doubleValue), (char*)&doubleValue);
  50. current += sizeof(doubleValue);
  51. sha.UpdateWithPodValue(doubleValue);
  52. char str[7];
  53. std::copy_n(current, std::size(str), str);
  54. current += std::size(str);
  55. sha.UpdateWithPodValue(str);
  56. sha.Update(current, SomeAlignedShaTestData.size() - (current - head));
  57. auto fragmentedDigest = sha.Final();
  58. auto notFragmentedDigest = NOpenSsl::NSha1::Calc(SomeAlignedShaTestData.data(), SomeAlignedShaTestData.size());
  59. UNIT_ASSERT_VALUES_EQUAL(memcmp(fragmentedDigest.data(), notFragmentedDigest.data(), notFragmentedDigest.size()), 0);
  60. }
  61. } // UNITTEST_SIMPLE_SUITE(SHA)