md5_stream_ut.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "md5_stream.h"
  2. #include <util/stream/input.h>
  3. #include <util/stream/str.h>
  4. #include <library/cpp/testing/unittest/registar.h>
  5. using namespace NYql;
  6. namespace {
  7. TString Consume(const TString& input) {
  8. TStringInput s(input);
  9. TString output;
  10. TStringOutput outputStream(output);
  11. TMd5OutputStream md5Stream(outputStream);
  12. UNIT_ASSERT_VALUES_EQUAL(input.size(), TransferData(&s, &md5Stream));
  13. UNIT_ASSERT_VALUES_EQUAL(input, output);
  14. return md5Stream.Finalize();
  15. }
  16. }
  17. Y_UNIT_TEST_SUITE(TStreamMd5Tests) {
  18. Y_UNIT_TEST(Empty) {
  19. const auto md5 = Consume("");
  20. const TString emptyStringMd5 = "d41d8cd98f00b204e9800998ecf8427e";
  21. UNIT_ASSERT_VALUES_EQUAL(md5, emptyStringMd5);
  22. }
  23. Y_UNIT_TEST(ShortText) {
  24. const auto md5 = Consume("hello from Y!");
  25. const TString expectedMd5 = "abf59ed7b0daa71085e76e461a737cc2";
  26. UNIT_ASSERT_VALUES_EQUAL(md5, expectedMd5);
  27. }
  28. Y_UNIT_TEST(BigText) {
  29. // TransferData uses TempBuf of 64K
  30. const TString s(1000000, 'A');
  31. const auto md5 = Consume(s.c_str());
  32. /*
  33. $ for i in {1..1000000};do echo -n A >> 1M.txt;done
  34. $ md5sum 1M.txt
  35. 48fcdb8b87ce8ef779774199a856091d 1M.txt
  36. */
  37. const TString expectedMd5 = "48fcdb8b87ce8ef779774199a856091d";
  38. UNIT_ASSERT_VALUES_EQUAL(md5, expectedMd5);
  39. }
  40. }