X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/67dc3830798e6b59a9fce03682c9802233b0a1fe..47d7761243a02368bb60b7227bd05a19ca4660d1:/test/libapt/hashsums_test.cc diff --git a/test/libapt/hashsums_test.cc b/test/libapt/hashsums_test.cc index 12096be15..3da89052b 100644 --- a/test/libapt/hashsums_test.cc +++ b/test/libapt/hashsums_test.cc @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -42,6 +43,28 @@ template void TestMill(const char *Out) int main(int argc, char** argv) { + // test HashSumValue which doesn't calculate but just stores sums + { + string md5sum = argv[2]; + MD5SumValue md5(md5sum); + equals(md5.Value(), md5sum); + } + { + string sha1sum = argv[3]; + SHA1SumValue sha1(sha1sum); + equals(sha1.Value(), sha1sum); + } + { + string sha2sum = argv[4]; + SHA256SumValue sha2(sha2sum); + equals(sha2.Value(), sha2sum); + } + { + string sha2sum = argv[5]; + SHA512SumValue sha2(sha2sum); + equals(sha2.Value(), sha2sum); + } + // From FIPS PUB 180-1 Test("","da39a3ee5e6b4b0d3255bfef95601890afd80709"); Test("abc","a9993e364706816aba3e25717850c26c9cd0d89d"); @@ -86,29 +109,68 @@ int main(int argc, char** argv) Test("The quick brown fox jumps over the lazy dog.", "91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bb" "c6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed"); - FILE* fd = fopen(argv[1], "r"); - if (fd == NULL) { + FileFd fd(argv[1], FileFd::ReadOnly); + if (fd.IsOpen() == false) { std::cerr << "Can't open file for 1. testing: " << argv[1] << std::endl; return 1; } + { + Hashes hashes; + hashes.AddFD(fd.Fd()); + equals(argv[2], hashes.MD5.Result().Value()); + equals(argv[3], hashes.SHA1.Result().Value()); + equals(argv[4], hashes.SHA256.Result().Value()); + equals(argv[5], hashes.SHA512.Result().Value()); + } + unsigned long sz = fd.FileSize(); + fd.Seek(0); + { Hashes hashes; - hashes.AddFD(fileno(fd)); + hashes.AddFD(fd.Fd(), sz); equals(argv[2], hashes.MD5.Result().Value()); equals(argv[3], hashes.SHA1.Result().Value()); equals(argv[4], hashes.SHA256.Result().Value()); equals(argv[5], hashes.SHA512.Result().Value()); + } + fd.Seek(0); + { + MD5Summation md5; + md5.AddFD(fd.Fd()); + equals(argv[2], md5.Result().Value()); + } + fd.Seek(0); + { + SHA1Summation sha1; + sha1.AddFD(fd.Fd()); + equals(argv[3], sha1.Result().Value()); + } + fd.Seek(0); + { + SHA256Summation sha2; + sha2.AddFD(fd.Fd()); + equals(argv[4], sha2.Result().Value()); + } + fd.Seek(0); + { + SHA512Summation sha2; + sha2.AddFD(fd.Fd()); + equals(argv[5], sha2.Result().Value()); + } + fd.Close(); - fseek(fd, 0L, SEEK_END); - unsigned long sz = ftell(fd); - fseek(fd, 0L, SEEK_SET); - - Hashes hashes2; - hashes2.AddFD(fileno(fd), sz); - fclose(fd); - equals(argv[2], hashes2.MD5.Result().Value()); - equals(argv[3], hashes2.SHA1.Result().Value()); - equals(argv[4], hashes2.SHA256.Result().Value()); - equals(argv[5], hashes2.SHA512.Result().Value()); + // test HashString code + { + HashString sha2("SHA256", argv[4]); + equals(sha2.VerifyFile(argv[1]), true); + } + { + HashString sha2("SHA512", argv[5]); + equals(sha2.VerifyFile(argv[1]), true); + } + { + HashString sha2("SHA256:" + std::string(argv[4])); + equals(sha2.VerifyFile(argv[1]), true); + } return 0; }