archive_validator_unittest.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/offline_pages/core/archive_validator.h"
  5. #include "base/callback.h"
  6. #include "base/files/file_path.h"
  7. #include "base/files/file_util.h"
  8. #include "base/files/scoped_temp_dir.h"
  9. #include "base/path_service.h"
  10. #include "build/build_config.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. #if BUILDFLAG(IS_ANDROID)
  13. #include "base/test/test_file_util.h"
  14. #endif // BUILDFLAG(IS_ANDROID)
  15. namespace offline_pages {
  16. namespace {
  17. const char kTestData1[] = "This is a test. ";
  18. const char kTestData2[] = "Hello World!";
  19. const int kSmallFileSize = 2 * 1024;
  20. const int kBigFileSize = 3 * 1024 * 1024;
  21. #if BUILDFLAG(IS_ANDROID)
  22. const int kSizeForTestContentUri = 173;
  23. #endif // BUILDFLAG(IS_ANDROID)
  24. // Digest for kTestData1 + kTestData2.
  25. const std::string kExpectedDigestForTestData(
  26. "\x9D\xBF\xED\xE4\x54\x16\xA6\xA3\x36\x2C\x88\xD8\xA8\x2A\x3A\xF3\x51\x1A"
  27. "\x6E\x34\x7E\xEF\xA4\xD5\x1D\xDE\x2A\xD0\xFE\x39\xE8\xA8",
  28. 32);
  29. // Digest for file with size 2K, filled with test data.
  30. const std::string kExpectedDigestForSmallFile(
  31. "\x10\xFC\x3C\x51\xA1\x52\xE9\x0E\x5B\x90\x31\x9B\x60\x1D\x92\xCC\xF3\x72"
  32. "\x90\xEF\x53\xC3\x5F\xF9\x25\x07\x68\x7D\x8A\x91\x1A\x08",
  33. 32);
  34. // Digest for file with size 3M, filled with test data.
  35. const std::string kExpectedDigestForBigFile(
  36. "\xF6\xDD\x7F\xEC\x85\x84\xAD\x00\x21\x9A\x44\x70\x71\xC1\xFA\x36\x8A\x1C"
  37. "\xAE\xE4\xD9\xC1\x46\x08\x3D\x23\x37\x13\xDD\xCC\xD2\xC0",
  38. 32);
  39. // Digest for content URI generated from net/data/file_stream_unittest/red.png.
  40. const std::string kExpectedDigestForContentUri(
  41. "\xEB\x7E\xB8\xE7\x3E\xD1\xE5\x45\x55\xCF\xA1\x8B\x7D\xD6\x75\x26\x2F\x8C"
  42. "\x8C\xDE\x31\x2B\x94\x43\x46\xE2\xF7\x74\xC8\xF7\x3A\x18",
  43. 32);
  44. // Helper function to make a character array filled with |size| bytes of
  45. // test content.
  46. std::string MakeContentOfSize(int size) {
  47. EXPECT_GE(size, 0);
  48. std::string result;
  49. result.reserve(size);
  50. for (int i = 0; i < size; i++)
  51. result.append(1, static_cast<char>(i % 256));
  52. return result;
  53. }
  54. #if BUILDFLAG(IS_ANDROID)
  55. base::FilePath GetContentUriPathForTest() {
  56. base::FilePath test_dir;
  57. base::PathService::Get(base::DIR_SOURCE_ROOT, &test_dir);
  58. test_dir = test_dir.AppendASCII("net");
  59. test_dir = test_dir.AppendASCII("data");
  60. test_dir = test_dir.AppendASCII("file_stream_unittest");
  61. EXPECT_TRUE(base::PathExists(test_dir));
  62. base::FilePath image_file = test_dir.Append(FILE_PATH_LITERAL("red.png"));
  63. // Insert the image into MediaStore. MediaStore will do some conversions, and
  64. // return the content URI.
  65. base::FilePath path = base::InsertImageIntoMediaStore(image_file);
  66. EXPECT_TRUE(path.IsContentUri());
  67. EXPECT_TRUE(base::PathExists(path));
  68. return path;
  69. }
  70. #endif // BUILDFLAG(IS_ANDROID)
  71. } // namespace
  72. class ArchiveValidatorTest : public testing::Test {
  73. public:
  74. ArchiveValidatorTest() = default;
  75. ~ArchiveValidatorTest() override = default;
  76. base::FilePath CreateFileWithContent(const std::string& content);
  77. private:
  78. base::ScopedTempDir temp_dir_;
  79. };
  80. base::FilePath ArchiveValidatorTest::CreateFileWithContent(
  81. const std::string& content) {
  82. if (!temp_dir_.CreateUniqueTempDir())
  83. return base::FilePath();
  84. base::FilePath temp_file_path =
  85. temp_dir_.GetPath().Append(FILE_PATH_LITERAL("foo.txt"));
  86. base::WriteFile(temp_file_path, content.c_str(), content.length());
  87. return temp_file_path;
  88. }
  89. TEST_F(ArchiveValidatorTest, ComputeDigestOnData) {
  90. ArchiveValidator archive_validator;
  91. archive_validator.Update(kTestData1, sizeof(kTestData1) - 1);
  92. archive_validator.Update(kTestData2, sizeof(kTestData2) - 1);
  93. std::string actual_digest = archive_validator.Finish();
  94. EXPECT_EQ(kExpectedDigestForTestData, actual_digest);
  95. }
  96. TEST_F(ArchiveValidatorTest, GetSizeAndComputeDigestOnTinyFile) {
  97. std::string expected_data(kTestData1);
  98. expected_data += kTestData2;
  99. base::FilePath temp_file_path = CreateFileWithContent(expected_data);
  100. std::pair<int64_t, std::string> actual_size_and_digest =
  101. ArchiveValidator::GetSizeAndComputeDigest(temp_file_path);
  102. EXPECT_EQ(static_cast<int64_t>(expected_data.size()),
  103. actual_size_and_digest.first);
  104. EXPECT_EQ(kExpectedDigestForTestData, actual_size_and_digest.second);
  105. }
  106. TEST_F(ArchiveValidatorTest, GetSizeAndComputeDigestOnSmallFile) {
  107. std::string expected_data(MakeContentOfSize(kSmallFileSize));
  108. base::FilePath temp_file_path = CreateFileWithContent(expected_data);
  109. std::pair<int64_t, std::string> actual_size_and_digest =
  110. ArchiveValidator::GetSizeAndComputeDigest(temp_file_path);
  111. EXPECT_EQ(kSmallFileSize, actual_size_and_digest.first);
  112. EXPECT_EQ(kExpectedDigestForSmallFile, actual_size_and_digest.second);
  113. }
  114. TEST_F(ArchiveValidatorTest, GetSizeAndComputeDigestOnBigFile) {
  115. std::string expected_data(MakeContentOfSize(kBigFileSize));
  116. base::FilePath temp_file_path = CreateFileWithContent(expected_data);
  117. std::pair<int64_t, std::string> actual_size_and_digest =
  118. ArchiveValidator::GetSizeAndComputeDigest(temp_file_path);
  119. EXPECT_EQ(kBigFileSize, actual_size_and_digest.first);
  120. EXPECT_EQ(kExpectedDigestForBigFile, actual_size_and_digest.second);
  121. }
  122. #if BUILDFLAG(IS_ANDROID)
  123. // Flaky. https://crbug.com/1022323
  124. TEST_F(ArchiveValidatorTest, DISABLED_GetSizeAndComputeDigestOnContentUri) {
  125. base::FilePath content_uri_path = GetContentUriPathForTest();
  126. std::pair<int64_t, std::string> actual_size_and_digest =
  127. ArchiveValidator::GetSizeAndComputeDigest(content_uri_path);
  128. EXPECT_EQ(kSizeForTestContentUri, actual_size_and_digest.first);
  129. EXPECT_EQ(kExpectedDigestForContentUri, actual_size_and_digest.second);
  130. }
  131. #endif // BUILDFLAG(IS_ANDROID)
  132. TEST_F(ArchiveValidatorTest, ValidateSmallFile) {
  133. std::string expected_data(MakeContentOfSize(kSmallFileSize));
  134. base::FilePath temp_file_path = CreateFileWithContent(expected_data);
  135. EXPECT_TRUE(ArchiveValidator::ValidateFile(temp_file_path, kSmallFileSize,
  136. kExpectedDigestForSmallFile));
  137. }
  138. TEST_F(ArchiveValidatorTest, ValidateBigFile) {
  139. std::string expected_data(MakeContentOfSize(kBigFileSize));
  140. base::FilePath temp_file_path = CreateFileWithContent(expected_data);
  141. EXPECT_TRUE(ArchiveValidator::ValidateFile(temp_file_path, kBigFileSize,
  142. kExpectedDigestForBigFile));
  143. }
  144. #if BUILDFLAG(IS_ANDROID)
  145. // Flaky. https://crbug.com/1022322
  146. TEST_F(ArchiveValidatorTest, DISABLED_ValidateContentUri) {
  147. base::FilePath content_uri_path = GetContentUriPathForTest();
  148. EXPECT_TRUE(ArchiveValidator::ValidateFile(
  149. content_uri_path, kSizeForTestContentUri, kExpectedDigestForContentUri));
  150. }
  151. #endif // BUILDFLAG(IS_ANDROID)
  152. } // namespace offline_pages