aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/digest/md5/md5_ut.cpp
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/digest/md5/md5_ut.cpp
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/digest/md5/md5_ut.cpp')
-rw-r--r--library/cpp/digest/md5/md5_ut.cpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/library/cpp/digest/md5/md5_ut.cpp b/library/cpp/digest/md5/md5_ut.cpp
new file mode 100644
index 0000000000..fecccca9ba
--- /dev/null
+++ b/library/cpp/digest/md5/md5_ut.cpp
@@ -0,0 +1,66 @@
+#include "md5.h"
+
+#include <library/cpp/testing/unittest/registar.h>
+
+#include <util/system/fs.h>
+#include <util/stream/file.h>
+
+Y_UNIT_TEST_SUITE(TMD5Test) {
+ Y_UNIT_TEST(TestMD5) {
+ // echo -n 'qwertyuiopqwertyuiopasdfghjklasdfghjkl' | md5sum
+ constexpr const char* b = "qwertyuiopqwertyuiopasdfghjklasdfghjkl";
+
+ MD5 r;
+ r.Update((const unsigned char*)b, 15);
+ r.Update((const unsigned char*)b + 15, strlen(b) - 15);
+
+ char rs[33];
+ TString s(r.End(rs));
+ s.to_lower();
+
+ UNIT_ASSERT_EQUAL(s, TStringBuf("3ac00dd696b966fd74deee3c35a59d8f"));
+
+ TString result = r.Calc(TStringBuf(b));
+ result.to_lower();
+ UNIT_ASSERT_EQUAL(result, TStringBuf("3ac00dd696b966fd74deee3c35a59d8f"));
+ }
+
+ Y_UNIT_TEST(TestFile) {
+ TString s = NUnitTest::RandomString(1000000, 1);
+ const TString tmpFile = "tmp";
+
+ {
+ TFixedBufferFileOutput fo(tmpFile);
+ fo.Write(s.data(), s.size());
+ }
+
+ char fileBuf[100];
+ char memBuf[100];
+ TString fileHash = MD5::File(tmpFile.data(), fileBuf);
+ TString memoryHash = MD5::Data((const unsigned char*)s.data(), s.size(), memBuf);
+
+ UNIT_ASSERT_EQUAL(fileHash, memoryHash);
+
+ fileHash = MD5::File(tmpFile);
+ UNIT_ASSERT_EQUAL(fileHash, memoryHash);
+
+ NFs::Remove(tmpFile);
+ fileHash = MD5::File(tmpFile);
+ UNIT_ASSERT_EQUAL(fileHash.size(), 0);
+ }
+
+ Y_UNIT_TEST(TestIsMD5) {
+ UNIT_ASSERT_EQUAL(false, MD5::IsMD5(TStringBuf()));
+ UNIT_ASSERT_EQUAL(false, MD5::IsMD5(TStringBuf("4136ebb0e4c45d21e2b09294c75cfa0"))); // length 31
+ UNIT_ASSERT_EQUAL(false, MD5::IsMD5(TStringBuf("4136ebb0e4c45d21e2b09294c75cfa000"))); // length 33
+ UNIT_ASSERT_EQUAL(false, MD5::IsMD5(TStringBuf("4136ebb0e4c45d21e2b09294c75cfa0g"))); // wrong character 'g'
+ UNIT_ASSERT_EQUAL(true, MD5::IsMD5(TStringBuf("4136EBB0E4C45D21E2B09294C75CFA08")));
+ UNIT_ASSERT_EQUAL(true, MD5::IsMD5(TStringBuf("4136ebb0E4C45D21e2b09294C75CfA08")));
+ UNIT_ASSERT_EQUAL(true, MD5::IsMD5(TStringBuf("4136ebb0e4c45d21e2b09294c75cfa08")));
+ }
+
+ Y_UNIT_TEST(TestMd5HalfMix) {
+ UNIT_ASSERT_EQUAL(MD5::CalcHalfMix(""), 7203772011789518145ul);
+ UNIT_ASSERT_EQUAL(MD5::CalcHalfMix("qwertyuiopqwertyuiopasdfghjklasdfghjkl"), 11753545595885642730ul);
+ }
+}