hashsums_test.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include <config.h>
  2. #include <apt-pkg/md5.h>
  3. #include <apt-pkg/sha1.h>
  4. #include <apt-pkg/sha2.h>
  5. #include <apt-pkg/strutl.h>
  6. #include <apt-pkg/hashes.h>
  7. #include <apt-pkg/fileutl.h>
  8. #include <iostream>
  9. #include <stdlib.h>
  10. #include <string>
  11. #include <gtest/gtest.h>
  12. #include "file-helpers.h"
  13. template <class T> void Test(const char *In,const char *Out)
  14. {
  15. T Sum;
  16. Sum.Add(In);
  17. equals(Sum.Result().Value(), Out);
  18. }
  19. TEST(HashSumsTest,SummationStrings)
  20. {
  21. #define EXPECT_SUM(Summation, In, Out) \
  22. { \
  23. Summation Sum; \
  24. Sum.Add(In); \
  25. EXPECT_EQ(Sum.Result().Value(), Out) << #Summation << " for '" << In << "'"; \
  26. }
  27. // From FIPS PUB 180-1
  28. EXPECT_SUM(SHA1Summation, "","da39a3ee5e6b4b0d3255bfef95601890afd80709");
  29. EXPECT_SUM(SHA1Summation, "abc","a9993e364706816aba3e25717850c26c9cd0d89d");
  30. EXPECT_SUM(SHA1Summation, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  31. "84983e441c3bd26ebaae4aa1f95129e5e54670f1");
  32. // MD5 tests from RFC 1321
  33. EXPECT_SUM(MD5Summation, "","d41d8cd98f00b204e9800998ecf8427e");
  34. EXPECT_SUM(MD5Summation, "a","0cc175b9c0f1b6a831c399e269772661");
  35. EXPECT_SUM(MD5Summation, "abc","900150983cd24fb0d6963f7d28e17f72");
  36. EXPECT_SUM(MD5Summation, "message digest","f96b697d7cb7938d525a2f31aaf161d0");
  37. EXPECT_SUM(MD5Summation, "abcdefghijklmnopqrstuvwxyz","c3fcd3d76192e4007dfb496cca67e13b");
  38. EXPECT_SUM(MD5Summation, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
  39. "d174ab98d277d9f5a5611c2c9f419d9f");
  40. EXPECT_SUM(MD5Summation, "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
  41. "57edf4a22be3c955ac49da2e2107b67a");
  42. // SHA-256, From FIPS 180-2
  43. EXPECT_SUM(SHA256Summation, "", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
  44. EXPECT_SUM(SHA256Summation, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  45. "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
  46. // SHA-512
  47. EXPECT_SUM(SHA512Summation, "",
  48. "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"
  49. "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
  50. EXPECT_SUM(SHA512Summation, "abc",
  51. "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"
  52. "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f");
  53. EXPECT_SUM(MD5Summation, "The quick brown fox jumps over the lazy dog", "9e107d9d372bb6826bd81d3542a419d6");
  54. EXPECT_SUM(MD5Summation, "The quick brown fox jumps over the lazy dog.", "e4d909c290d0fb1ca068ffaddf22cbd0");
  55. EXPECT_SUM(SHA1Summation, "The quick brown fox jumps over the lazy dog", "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12");
  56. EXPECT_SUM(SHA1Summation, "The quick brown fox jumps over the lazy cog", "de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3");
  57. EXPECT_SUM(SHA256Summation, "The quick brown fox jumps over the lazy dog", "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592");
  58. EXPECT_SUM(SHA256Summation, "The quick brown fox jumps over the lazy dog.", "ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c");
  59. EXPECT_SUM(SHA512Summation, "The quick brown fox jumps over the lazy dog", "07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb64"
  60. "2e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6");
  61. EXPECT_SUM(SHA512Summation, "The quick brown fox jumps over the lazy dog.", "91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bb"
  62. "c6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed");
  63. #undef EXPECT_SUM
  64. }
  65. TEST(HashSumsTest, Mill)
  66. {
  67. SHA1Summation Sum1;
  68. const unsigned char As[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
  69. size_t const AsCount = sizeof(As)/sizeof(As[0]) - 1;
  70. size_t Count = 1000000;
  71. while (Count != 0)
  72. {
  73. if (Count >= AsCount)
  74. {
  75. Sum1.Add(As, AsCount);
  76. Count -= AsCount;
  77. }
  78. else
  79. {
  80. Sum1.Add(As,Count);
  81. Count = 0;
  82. }
  83. }
  84. EXPECT_EQ("34aa973cd4c4daa4f61eeb2bdbad27316534016f", Sum1.Result().Value());
  85. }
  86. static void getSummationString(char const * const type, std::string &sum)
  87. {
  88. /* to compare our result with an independent source we call the specific binaries
  89. and read their result back. We do this with a little trick by claiming that the
  90. summation is a compressor – and open the 'compressed' file later on directly to
  91. read out the summation sum calculated by it */
  92. APT::Configuration::Compressor compress(type, ".ext", type, NULL, NULL, 99);
  93. std::string name("apt-test-");
  94. name.append("hashsums").append(".XXXXXX");
  95. char * tempfile = strdup(name.c_str());
  96. int tempfile_fd = mkstemp(tempfile);
  97. close(tempfile_fd);
  98. ASSERT_NE(-1, tempfile_fd);
  99. FileFd fd;
  100. ASSERT_TRUE(fd.Open(tempfile, FileFd::WriteOnly | FileFd::Empty, compress));
  101. ASSERT_TRUE(fd.IsOpen());
  102. FileFd input(__FILE__, FileFd::ReadOnly);
  103. ASSERT_TRUE(input.IsOpen());
  104. ASSERT_NE(0, input.FileSize());
  105. ASSERT_TRUE(CopyFile(input, fd));
  106. ASSERT_TRUE(input.IsOpen());
  107. ASSERT_TRUE(fd.IsOpen());
  108. ASSERT_FALSE(fd.Failed());
  109. input.Close();
  110. fd.Close();
  111. ASSERT_TRUE(fd.Open(tempfile, FileFd::ReadOnly, FileFd::None));
  112. ASSERT_TRUE(fd.IsOpen());
  113. ASSERT_NE(0, fd.FileSize());
  114. ASSERT_FALSE(fd.Failed());
  115. unlink(tempfile);
  116. free(tempfile);
  117. char readback[2000];
  118. unsigned long long actual;
  119. ASSERT_TRUE(fd.Read(readback, sizeof(readback)/sizeof(readback[0]), &actual));
  120. actual -= 4;
  121. readback[actual] = '\0';
  122. sum = readback;
  123. }
  124. TEST(HashSumsTest, FileBased)
  125. {
  126. std::string summation;
  127. getSummationString("md5sum", summation);
  128. MD5SumValue md5(summation);
  129. EXPECT_EQ(md5.Value(), summation);
  130. getSummationString("sha1sum", summation);
  131. SHA1SumValue sha1(summation);
  132. EXPECT_EQ(sha1.Value(), summation);
  133. getSummationString("sha256sum", summation);
  134. SHA256SumValue sha256(summation);
  135. EXPECT_EQ(sha256.Value(), summation);
  136. getSummationString("sha512sum", summation);
  137. SHA512SumValue sha512(summation);
  138. EXPECT_EQ(sha512.Value(), summation);
  139. FileFd fd(__FILE__, FileFd::ReadOnly);
  140. EXPECT_TRUE(fd.IsOpen());
  141. {
  142. Hashes hashes;
  143. hashes.AddFD(fd.Fd());
  144. EXPECT_EQ(md5.Value(), hashes.MD5.Result().Value());
  145. EXPECT_EQ(sha1.Value(), hashes.SHA1.Result().Value());
  146. EXPECT_EQ(sha256.Value(), hashes.SHA256.Result().Value());
  147. EXPECT_EQ(sha512.Value(), hashes.SHA512.Result().Value());
  148. }
  149. unsigned long sz = fd.FileSize();
  150. fd.Seek(0);
  151. {
  152. Hashes hashes;
  153. hashes.AddFD(fd.Fd(), sz);
  154. EXPECT_EQ(md5.Value(), hashes.MD5.Result().Value());
  155. EXPECT_EQ(sha1.Value(), hashes.SHA1.Result().Value());
  156. EXPECT_EQ(sha256.Value(), hashes.SHA256.Result().Value());
  157. EXPECT_EQ(sha512.Value(), hashes.SHA512.Result().Value());
  158. }
  159. fd.Seek(0);
  160. {
  161. MD5Summation MD5;
  162. MD5.AddFD(fd.Fd());
  163. EXPECT_EQ(md5.Value(), MD5.Result().Value());
  164. }
  165. fd.Seek(0);
  166. {
  167. SHA1Summation SHA1;
  168. SHA1.AddFD(fd.Fd());
  169. EXPECT_EQ(sha1.Value(), SHA1.Result().Value());
  170. }
  171. fd.Seek(0);
  172. {
  173. SHA256Summation SHA2;
  174. SHA2.AddFD(fd.Fd());
  175. EXPECT_EQ(sha256.Value(), SHA2.Result().Value());
  176. }
  177. fd.Seek(0);
  178. {
  179. SHA512Summation SHA2;
  180. SHA2.AddFD(fd.Fd());
  181. EXPECT_EQ(sha512.Value(), SHA2.Result().Value());
  182. }
  183. fd.Close();
  184. {
  185. HashString sha2("SHA256", sha256.Value());
  186. EXPECT_TRUE(sha2.VerifyFile(__FILE__));
  187. }
  188. {
  189. HashString sha2("SHA512", sha512.Value());
  190. EXPECT_TRUE(sha2.VerifyFile(__FILE__));
  191. }
  192. {
  193. HashString sha2("SHA256:" + sha256.Value());
  194. EXPECT_TRUE(sha2.VerifyFile(__FILE__));
  195. }
  196. }