From 16b63f8cdf5b488ab2f4b0e029478b01a2d6b8a8 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 10 Jul 2018 09:20:25 +0200 Subject: Add support for hashing ifdstream --- tests/sha1/driver.cxx | 38 ++++++++++++++++++++++++++++++++++---- tests/sha256/driver.cxx | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/tests/sha1/driver.cxx b/tests/sha1/driver.cxx index 5d53faa..92a8443 100644 --- a/tests/sha1/driver.cxx +++ b/tests/sha1/driver.cxx @@ -6,7 +6,7 @@ #ifndef __cpp_lib_modules #include -#include +#include // size_t #endif // Other includes. @@ -14,11 +14,16 @@ #ifdef __cpp_modules #ifdef __cpp_lib_modules import std.core; -import std.io; #endif import butl.sha1; +import butl.path; +import butl.fdstream; +import butl.filesystem; #else #include +#include +#include +#include // auto_rmfile #endif using namespace std; @@ -37,8 +42,32 @@ main () "40bd001563085fc35165329ea1ff5c5ecbdbbeef"); { + string s; + for (size_t i (0); i < 1024; ++i) + s += "0123456789"; + + path p (path::temp_path ("butl-sha1")); + + auto_rmfile pr; // Always remove the file after the stream is closed. + ofdstream os (p); + pr = auto_rmfile (p); + + os << s; + os.close (); + + ifdstream is (p); + + assert (string (sha1 (is).string ()) == + sha1 (s.c_str (), s.size ()).string ()); + + assert (is.eof ()); + is.close (); + } + + { sha1 h ("123"); - assert (string (h.string ()) == "cc320164df1a2130045a28f08d3b88bc5bbcc43a"); + assert (string (h.string ()) == + "cc320164df1a2130045a28f08d3b88bc5bbcc43a"); assert (h.abbreviated_string (10) == "cc320164df"); assert (h.abbreviated_string (41) == h.string ()); @@ -53,6 +82,7 @@ main () auto& b (h.binary ()); assert (b[0] == 0x58 && b[19] == 0xfd); - assert (string (h.string ()) == "58c596bafad8d007952934af1db9abc5401d4dfd"); + assert (string (h.string ()) == + "58c596bafad8d007952934af1db9abc5401d4dfd"); } } diff --git a/tests/sha256/driver.cxx b/tests/sha256/driver.cxx index 5653e05..908b6ec 100644 --- a/tests/sha256/driver.cxx +++ b/tests/sha256/driver.cxx @@ -6,7 +6,7 @@ #ifndef __cpp_lib_modules #include -#include +#include // size_t #endif // Other includes. @@ -14,11 +14,16 @@ #ifdef __cpp_modules #ifdef __cpp_lib_modules import std.core; -import std.io; #endif +import butl.path; import butl.sha256; +import butl.fdstream; +import butl.filesystem; #else +#include #include +#include +#include // auto_rmfile #endif using namespace std; @@ -37,6 +42,29 @@ main () "a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3"); { + string s; + for (size_t i (0); i < 1024; ++i) + s += "0123456789"; + + path p (path::temp_path ("butl-sha256")); + + auto_rmfile pr; // Always remove the file after the stream is closed. + ofdstream os (p); + pr = auto_rmfile (p); + + os << s; + os.close (); + + ifdstream is (p); + + assert (string (sha256 (is).string ()) == + sha256 (s.c_str (), s.size ()).string ()); + + assert (is.eof ()); + is.close (); + } + + { sha256 h ("123"); assert (string (h.string ()) == -- cgit v1.1