You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

393 lines
14 KiB

  1. #include <config.h>
  2. #include <apt-pkg/configuration.h>
  3. #include <apt-pkg/md5.h>
  4. #include <apt-pkg/sha1.h>
  5. #include <apt-pkg/sha2.h>
  6. #include <apt-pkg/strutl.h>
  7. #include <apt-pkg/hashes.h>
  8. #include <apt-pkg/fileutl.h>
  9. #include <iostream>
  10. #include <stdlib.h>
  11. #include <string>
  12. #include <gtest/gtest.h>
  13. #include "file-helpers.h"
  14. template <class T> void Test(const char *In,const char *Out)
  15. {
  16. T Sum;
  17. Sum.Add(In);
  18. equals(Sum.Result().Value(), Out);
  19. }
  20. TEST(HashSumsTest,SummationStrings)
  21. {
  22. #define EXPECT_SUM(Summation, In, Out) \
  23. { \
  24. Summation Sum; \
  25. Sum.Add(In); \
  26. EXPECT_EQ(Sum.Result().Value(), Out) << #Summation << " for '" << In << "'"; \
  27. }
  28. // From FIPS PUB 180-1
  29. EXPECT_SUM(SHA1Summation, "","da39a3ee5e6b4b0d3255bfef95601890afd80709");
  30. EXPECT_SUM(SHA1Summation, "abc","a9993e364706816aba3e25717850c26c9cd0d89d");
  31. EXPECT_SUM(SHA1Summation, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  32. "84983e441c3bd26ebaae4aa1f95129e5e54670f1");
  33. // MD5 tests from RFC 1321
  34. EXPECT_SUM(MD5Summation, "","d41d8cd98f00b204e9800998ecf8427e");
  35. EXPECT_SUM(MD5Summation, "a","0cc175b9c0f1b6a831c399e269772661");
  36. EXPECT_SUM(MD5Summation, "abc","900150983cd24fb0d6963f7d28e17f72");
  37. EXPECT_SUM(MD5Summation, "message digest","f96b697d7cb7938d525a2f31aaf161d0");
  38. EXPECT_SUM(MD5Summation, "abcdefghijklmnopqrstuvwxyz","c3fcd3d76192e4007dfb496cca67e13b");
  39. EXPECT_SUM(MD5Summation, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
  40. "d174ab98d277d9f5a5611c2c9f419d9f");
  41. EXPECT_SUM(MD5Summation, "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
  42. "57edf4a22be3c955ac49da2e2107b67a");
  43. // SHA-256, From FIPS 180-2
  44. EXPECT_SUM(SHA256Summation, "", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
  45. EXPECT_SUM(SHA256Summation, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  46. "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
  47. // SHA-512
  48. EXPECT_SUM(SHA512Summation, "",
  49. "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"
  50. "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
  51. EXPECT_SUM(SHA512Summation, "abc",
  52. "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"
  53. "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f");
  54. EXPECT_SUM(MD5Summation, "The quick brown fox jumps over the lazy dog", "9e107d9d372bb6826bd81d3542a419d6");
  55. EXPECT_SUM(MD5Summation, "The quick brown fox jumps over the lazy dog.", "e4d909c290d0fb1ca068ffaddf22cbd0");
  56. EXPECT_SUM(SHA1Summation, "The quick brown fox jumps over the lazy dog", "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12");
  57. EXPECT_SUM(SHA1Summation, "The quick brown fox jumps over the lazy cog", "de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3");
  58. EXPECT_SUM(SHA256Summation, "The quick brown fox jumps over the lazy dog", "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592");
  59. EXPECT_SUM(SHA256Summation, "The quick brown fox jumps over the lazy dog.", "ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c");
  60. EXPECT_SUM(SHA512Summation, "The quick brown fox jumps over the lazy dog", "07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb64"
  61. "2e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6");
  62. EXPECT_SUM(SHA512Summation, "The quick brown fox jumps over the lazy dog.", "91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bb"
  63. "c6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed");
  64. #undef EXPECT_SUM
  65. }
  66. TEST(HashSumsTest, Mill)
  67. {
  68. SHA1Summation Sum1;
  69. const unsigned char As[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
  70. size_t const AsCount = sizeof(As)/sizeof(As[0]) - 1;
  71. size_t Count = 1000000;
  72. while (Count != 0)
  73. {
  74. if (Count >= AsCount)
  75. {
  76. Sum1.Add(As, AsCount);
  77. Count -= AsCount;
  78. }
  79. else
  80. {
  81. Sum1.Add(As,Count);
  82. Count = 0;
  83. }
  84. }
  85. EXPECT_EQ("34aa973cd4c4daa4f61eeb2bdbad27316534016f", Sum1.Result().Value());
  86. }
  87. static void getSummationString(char const * const type, std::string &sum)
  88. {
  89. /* to compare our result with an independent source we call the specific binaries
  90. and read their result back. We do this with a little trick by claiming that the
  91. summation is a compressor – and open the 'compressed' file later on directly to
  92. read out the summation sum calculated by it */
  93. APT::Configuration::Compressor compress(type, ".ext", type, NULL, NULL, 99);
  94. std::string name("apt-test-");
  95. name.append("hashsums").append(".XXXXXX");
  96. char * tempfile = strdup(name.c_str());
  97. int tempfile_fd = mkstemp(tempfile);
  98. close(tempfile_fd);
  99. ASSERT_NE(-1, tempfile_fd);
  100. FileFd fd;
  101. ASSERT_TRUE(fd.Open(tempfile, FileFd::WriteOnly | FileFd::Empty, compress));
  102. ASSERT_TRUE(fd.IsOpen());
  103. FileFd input("/etc/os-release", FileFd::ReadOnly);
  104. ASSERT_TRUE(input.IsOpen());
  105. ASSERT_NE(0, input.FileSize());
  106. ASSERT_TRUE(CopyFile(input, fd));
  107. ASSERT_TRUE(input.IsOpen());
  108. ASSERT_TRUE(fd.IsOpen());
  109. ASSERT_FALSE(fd.Failed());
  110. input.Close();
  111. fd.Close();
  112. ASSERT_TRUE(fd.Open(tempfile, FileFd::ReadOnly, FileFd::None));
  113. ASSERT_TRUE(fd.IsOpen());
  114. ASSERT_NE(0, fd.FileSize());
  115. ASSERT_FALSE(fd.Failed());
  116. unlink(tempfile);
  117. free(tempfile);
  118. char readback[2000];
  119. unsigned long long actual;
  120. ASSERT_TRUE(fd.Read(readback, sizeof(readback)/sizeof(readback[0]), &actual));
  121. actual -= 4;
  122. readback[actual] = '\0';
  123. sum = readback;
  124. }
  125. TEST(HashSumsTest, FileBased)
  126. {
  127. std::string summation;
  128. getSummationString("md5sum", summation);
  129. MD5SumValue md5(summation);
  130. EXPECT_EQ(md5.Value(), summation);
  131. getSummationString("sha1sum", summation);
  132. SHA1SumValue sha1(summation);
  133. EXPECT_EQ(sha1.Value(), summation);
  134. getSummationString("sha256sum", summation);
  135. SHA256SumValue sha256(summation);
  136. EXPECT_EQ(sha256.Value(), summation);
  137. getSummationString("sha512sum", summation);
  138. SHA512SumValue sha512(summation);
  139. EXPECT_EQ(sha512.Value(), summation);
  140. FileFd fd("/etc/os-release", FileFd::ReadOnly);
  141. EXPECT_TRUE(fd.IsOpen());
  142. std::string FileSize;
  143. strprintf(FileSize, "%llu", fd.FileSize());
  144. {
  145. Hashes hashes;
  146. hashes.AddFD(fd.Fd());
  147. HashStringList list = hashes.GetHashStringList();
  148. EXPECT_FALSE(list.empty());
  149. EXPECT_EQ(5, list.size());
  150. EXPECT_EQ(md5.Value(), list.find("MD5Sum")->HashValue());
  151. EXPECT_EQ(sha1.Value(), list.find("SHA1")->HashValue());
  152. EXPECT_EQ(sha256.Value(), list.find("SHA256")->HashValue());
  153. EXPECT_EQ(sha512.Value(), list.find("SHA512")->HashValue());
  154. EXPECT_EQ(FileSize, list.find("Checksum-FileSize")->HashValue());
  155. }
  156. unsigned long long sz = fd.FileSize();
  157. fd.Seek(0);
  158. {
  159. Hashes hashes;
  160. hashes.AddFD(fd.Fd(), sz);
  161. HashStringList list = hashes.GetHashStringList();
  162. EXPECT_FALSE(list.empty());
  163. EXPECT_EQ(5, list.size());
  164. EXPECT_EQ(md5.Value(), list.find("MD5Sum")->HashValue());
  165. EXPECT_EQ(sha1.Value(), list.find("SHA1")->HashValue());
  166. EXPECT_EQ(sha256.Value(), list.find("SHA256")->HashValue());
  167. EXPECT_EQ(sha512.Value(), list.find("SHA512")->HashValue());
  168. EXPECT_EQ(FileSize, list.find("Checksum-FileSize")->HashValue());
  169. }
  170. fd.Seek(0);
  171. {
  172. Hashes hashes(Hashes::MD5SUM | Hashes::SHA512SUM);
  173. hashes.AddFD(fd);
  174. HashStringList list = hashes.GetHashStringList();
  175. EXPECT_FALSE(list.empty());
  176. EXPECT_EQ(3, list.size());
  177. EXPECT_EQ(md5.Value(), list.find("MD5Sum")->HashValue());
  178. EXPECT_EQ(NULL, list.find("SHA1"));
  179. EXPECT_EQ(NULL, list.find("SHA256"));
  180. EXPECT_EQ(sha512.Value(), list.find("SHA512")->HashValue());
  181. EXPECT_EQ(FileSize, list.find("Checksum-FileSize")->HashValue());
  182. fd.Seek(0);
  183. Hashes hashes2(list);
  184. hashes2.AddFD(fd);
  185. list = hashes2.GetHashStringList();
  186. EXPECT_FALSE(list.empty());
  187. EXPECT_EQ(3, list.size());
  188. EXPECT_EQ(md5.Value(), list.find("MD5Sum")->HashValue());
  189. EXPECT_EQ(NULL, list.find("SHA1"));
  190. EXPECT_EQ(NULL, list.find("SHA256"));
  191. EXPECT_EQ(sha512.Value(), list.find("SHA512")->HashValue());
  192. EXPECT_EQ(FileSize, list.find("Checksum-FileSize")->HashValue());
  193. }
  194. fd.Seek(0);
  195. {
  196. MD5Summation MD5;
  197. MD5.AddFD(fd.Fd());
  198. EXPECT_EQ(md5.Value(), MD5.Result().Value());
  199. }
  200. fd.Seek(0);
  201. {
  202. SHA1Summation SHA1;
  203. SHA1.AddFD(fd.Fd());
  204. EXPECT_EQ(sha1.Value(), SHA1.Result().Value());
  205. }
  206. fd.Seek(0);
  207. {
  208. SHA256Summation SHA2;
  209. SHA2.AddFD(fd.Fd());
  210. EXPECT_EQ(sha256.Value(), SHA2.Result().Value());
  211. }
  212. fd.Seek(0);
  213. {
  214. SHA512Summation SHA2;
  215. SHA2.AddFD(fd.Fd());
  216. EXPECT_EQ(sha512.Value(), SHA2.Result().Value());
  217. }
  218. fd.Close();
  219. HashString sha2file("SHA512", sha512.Value());
  220. EXPECT_TRUE(sha2file.VerifyFile("/etc/os-release"));
  221. HashString sha2wrong("SHA512", "00000000000");
  222. EXPECT_FALSE(sha2wrong.VerifyFile("/etc/os-release"));
  223. EXPECT_EQ(sha2file, sha2file);
  224. EXPECT_TRUE(sha2file == sha2file);
  225. EXPECT_NE(sha2file, sha2wrong);
  226. EXPECT_TRUE(sha2file != sha2wrong);
  227. HashString sha2big("SHA256", sha256.Value());
  228. EXPECT_TRUE(sha2big.VerifyFile("/etc/os-release"));
  229. HashString sha2small("sha256:" + sha256.Value());
  230. EXPECT_TRUE(sha2small.VerifyFile("/etc/os-release"));
  231. EXPECT_EQ(sha2big, sha2small);
  232. EXPECT_TRUE(sha2big == sha2small);
  233. EXPECT_FALSE(sha2big != sha2small);
  234. HashStringList hashes;
  235. EXPECT_TRUE(hashes.empty());
  236. EXPECT_TRUE(hashes.push_back(sha2file));
  237. EXPECT_FALSE(hashes.empty());
  238. EXPECT_EQ(1, hashes.size());
  239. HashStringList wrong;
  240. EXPECT_TRUE(wrong.push_back(sha2wrong));
  241. EXPECT_NE(wrong, hashes);
  242. EXPECT_FALSE(wrong == hashes);
  243. EXPECT_TRUE(wrong != hashes);
  244. HashStringList similar;
  245. EXPECT_TRUE(similar.push_back(sha2big));
  246. EXPECT_NE(similar, hashes);
  247. EXPECT_FALSE(similar == hashes);
  248. EXPECT_TRUE(similar != hashes);
  249. EXPECT_TRUE(hashes.push_back(sha2big));
  250. EXPECT_EQ(2, hashes.size());
  251. EXPECT_TRUE(hashes.push_back(sha2small));
  252. EXPECT_EQ(2, hashes.size());
  253. EXPECT_FALSE(hashes.push_back(sha2wrong));
  254. EXPECT_EQ(2, hashes.size());
  255. EXPECT_TRUE(hashes.VerifyFile("/etc/os-release"));
  256. EXPECT_EQ(similar, hashes);
  257. EXPECT_TRUE(similar == hashes);
  258. EXPECT_FALSE(similar != hashes);
  259. similar.clear();
  260. EXPECT_TRUE(similar.empty());
  261. EXPECT_EQ(0, similar.size());
  262. EXPECT_NE(similar, hashes);
  263. EXPECT_FALSE(similar == hashes);
  264. EXPECT_TRUE(similar != hashes);
  265. }
  266. TEST(HashSumsTest, HashStringList)
  267. {
  268. _config->Clear("Acquire::ForceHash");
  269. HashStringList list;
  270. EXPECT_TRUE(list.empty());
  271. EXPECT_FALSE(list.usable());
  272. EXPECT_EQ(0, list.size());
  273. EXPECT_EQ(NULL, list.find(NULL));
  274. EXPECT_EQ(NULL, list.find(""));
  275. EXPECT_EQ(NULL, list.find("MD5Sum"));
  276. EXPECT_EQ(NULL, list.find("ROT26"));
  277. EXPECT_EQ(NULL, list.find("SHA1"));
  278. EXPECT_EQ(0, list.FileSize());
  279. // empty lists aren't equal
  280. HashStringList list2;
  281. EXPECT_FALSE(list == list2);
  282. EXPECT_TRUE(list != list2);
  283. // some hashes don't really contribute to usability
  284. list.push_back(HashString("Checksum-FileSize", "29"));
  285. EXPECT_FALSE(list.empty());
  286. EXPECT_FALSE(list.usable());
  287. EXPECT_EQ(1, list.size());
  288. EXPECT_EQ(29, list.FileSize());
  289. list.push_back(HashString("MD5Sum", "d41d8cd98f00b204e9800998ecf8427e"));
  290. EXPECT_FALSE(list.empty());
  291. EXPECT_FALSE(list.usable());
  292. EXPECT_EQ(2, list.size());
  293. EXPECT_EQ(29, list.FileSize());
  294. EXPECT_TRUE(NULL != list.find("MD5Sum"));
  295. list.push_back(HashString("SHA1", "cacecbd74968bc90ea3342767e6b94f46ddbcafc"));
  296. EXPECT_FALSE(list.usable());
  297. EXPECT_EQ(3, list.size());
  298. EXPECT_EQ(29, list.FileSize());
  299. EXPECT_TRUE(NULL != list.find("MD5Sum"));
  300. EXPECT_TRUE(NULL != list.find("SHA1"));
  301. list.push_back(HashString("SHA256", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"));
  302. EXPECT_TRUE(list.usable());
  303. EXPECT_EQ(4, list.size());
  304. EXPECT_EQ(29, list.FileSize());
  305. EXPECT_TRUE(NULL != list.find("MD5Sum"));
  306. EXPECT_TRUE(NULL != list.find("SHA1"));
  307. EXPECT_TRUE(NULL != list.find("SHA256"));
  308. Hashes hashes;
  309. hashes.Add("The quick brown fox jumps over the lazy dog");
  310. list = hashes.GetHashStringList();
  311. EXPECT_FALSE(list.empty());
  312. EXPECT_TRUE(list.usable());
  313. EXPECT_EQ(5, list.size());
  314. EXPECT_TRUE(NULL != list.find(NULL));
  315. EXPECT_TRUE(NULL != list.find(""));
  316. EXPECT_TRUE(NULL != list.find("MD5Sum"));
  317. EXPECT_TRUE(NULL != list.find("Checksum-FileSize"));
  318. EXPECT_TRUE(NULL == list.find("ROT26"));
  319. _config->Set("Acquire::ForceHash", "MD5Sum");
  320. EXPECT_FALSE(list.empty());
  321. EXPECT_TRUE(list.usable());
  322. EXPECT_EQ(5, list.size());
  323. EXPECT_TRUE(NULL != list.find(NULL));
  324. EXPECT_TRUE(NULL != list.find(""));
  325. EXPECT_TRUE(NULL != list.find("MD5Sum"));
  326. EXPECT_TRUE(NULL != list.find("Checksum-FileSize"));
  327. EXPECT_TRUE(NULL == list.find("ROT26"));
  328. _config->Set("Acquire::ForceHash", "ROT26");
  329. EXPECT_FALSE(list.empty());
  330. EXPECT_FALSE(list.usable());
  331. EXPECT_EQ(5, list.size());
  332. EXPECT_TRUE(NULL == list.find(NULL));
  333. EXPECT_TRUE(NULL == list.find(""));
  334. EXPECT_TRUE(NULL != list.find("MD5Sum"));
  335. EXPECT_TRUE(NULL != list.find("Checksum-FileSize"));
  336. EXPECT_TRUE(NULL == list.find("ROT26"));
  337. _config->Clear("Acquire::ForceHash");
  338. list2.push_back(*list.find("MD5Sum"));
  339. EXPECT_TRUE(list == list2);
  340. EXPECT_FALSE(list != list2);
  341. // introduce a mismatch to the list
  342. list2.push_back(HashString("SHA1", "cacecbd74968bc90ea3342767e6b94f46ddbcafc"));
  343. EXPECT_FALSE(list == list2);
  344. EXPECT_TRUE(list != list2);
  345. _config->Set("Acquire::ForceHash", "MD5Sum");
  346. EXPECT_TRUE(list == list2);
  347. EXPECT_FALSE(list != list2);
  348. _config->Clear("Acquire::ForceHash");
  349. }