#include "components/offline_pages/core/archive_validator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/callback.h"
#include "base/path_service.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/test/test_file_util.h"
#endif
namespace offline_pages {
namespace {
const char kTestData1[] = …;
const char kTestData2[] = …;
const int kSmallFileSize = …;
const int kBigFileSize = …;
#if BUILDFLAG(IS_ANDROID)
const int kSizeForTestContentUri = 173;
#endif
const std::string kExpectedDigestForTestData(
"\x9D\xBF\xED\xE4\x54\x16\xA6\xA3\x36\x2C\x88\xD8\xA8\x2A\x3A\xF3\x51\x1A"
"\x6E\x34\x7E\xEF\xA4\xD5\x1D\xDE\x2A\xD0\xFE\x39\xE8\xA8",
32);
const std::string kExpectedDigestForSmallFile(
"\x10\xFC\x3C\x51\xA1\x52\xE9\x0E\x5B\x90\x31\x9B\x60\x1D\x92\xCC\xF3\x72"
"\x90\xEF\x53\xC3\x5F\xF9\x25\x07\x68\x7D\x8A\x91\x1A\x08",
32);
const std::string kExpectedDigestForBigFile(
"\xF6\xDD\x7F\xEC\x85\x84\xAD\x00\x21\x9A\x44\x70\x71\xC1\xFA\x36\x8A\x1C"
"\xAE\xE4\xD9\xC1\x46\x08\x3D\x23\x37\x13\xDD\xCC\xD2\xC0",
32);
const std::string kExpectedDigestForContentUri(
"\xEB\x7E\xB8\xE7\x3E\xD1\xE5\x45\x55\xCF\xA1\x8B\x7D\xD6\x75\x26\x2F\x8C"
"\x8C\xDE\x31\x2B\x94\x43\x46\xE2\xF7\x74\xC8\xF7\x3A\x18",
32);
std::string MakeContentOfSize(int size) { … }
#if BUILDFLAG(IS_ANDROID)
base::FilePath GetContentUriPathForTest() {
base::FilePath test_dir;
base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &test_dir);
test_dir = test_dir.AppendASCII("net");
test_dir = test_dir.AppendASCII("data");
test_dir = test_dir.AppendASCII("file_stream_unittest");
EXPECT_TRUE(base::PathExists(test_dir));
base::FilePath image_file = test_dir.Append(FILE_PATH_LITERAL("red.png"));
base::FilePath path = base::InsertImageIntoMediaStore(image_file);
EXPECT_TRUE(path.IsContentUri());
EXPECT_TRUE(base::PathExists(path));
return path;
}
#endif
}
class ArchiveValidatorTest : public testing::Test { … };
base::FilePath ArchiveValidatorTest::CreateFileWithContent(
const std::string& content) { … }
TEST_F(ArchiveValidatorTest, ComputeDigestOnData) { … }
TEST_F(ArchiveValidatorTest, GetSizeAndComputeDigestOnTinyFile) { … }
TEST_F(ArchiveValidatorTest, GetSizeAndComputeDigestOnSmallFile) { … }
TEST_F(ArchiveValidatorTest, GetSizeAndComputeDigestOnBigFile) { … }
#if BUILDFLAG(IS_ANDROID)
TEST_F(ArchiveValidatorTest, DISABLED_GetSizeAndComputeDigestOnContentUri) {
base::FilePath content_uri_path = GetContentUriPathForTest();
std::pair<int64_t, std::string> actual_size_and_digest =
ArchiveValidator::GetSizeAndComputeDigest(content_uri_path);
EXPECT_EQ(kSizeForTestContentUri, actual_size_and_digest.first);
EXPECT_EQ(kExpectedDigestForContentUri, actual_size_and_digest.second);
}
#endif
TEST_F(ArchiveValidatorTest, ValidateSmallFile) { … }
TEST_F(ArchiveValidatorTest, ValidateBigFile) { … }
#if BUILDFLAG(IS_ANDROID)
TEST_F(ArchiveValidatorTest, DISABLED_ValidateContentUri) {
base::FilePath content_uri_path = GetContentUriPathForTest();
EXPECT_TRUE(ArchiveValidator::ValidateFile(
content_uri_path, kSizeForTestContentUri, kExpectedDigestForContentUri));
}
#endif
}