1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/json_file_sanitizer.h"
#include <memory>
#include <optional>
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/gmock_expected_support.h"
#include "base/types/expected.h"
#include "content/public/test/browser_task_environment.h"
#include "extensions/browser/extension_file_task_runner.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::base::test::ErrorIs;
using ::base::test::HasValue;
namespace extensions {
namespace {
class JsonFileSanitizerTest : public testing::Test {
public:
JsonFileSanitizerTest() {}
JsonFileSanitizerTest(const JsonFileSanitizerTest&) = delete;
JsonFileSanitizerTest& operator=(const JsonFileSanitizerTest&) = delete;
protected:
base::FilePath CreateFilePath(const base::FilePath::StringType& file_name) {
return temp_dir_.GetPath().Append(file_name);
}
void CreateValidJsonFile(const base::FilePath& path) {
std::string kJson = "{\"hello\":\"bonjour\"}";
ASSERT_TRUE(base::WriteFile(path, kJson));
}
void CreateInvalidJsonFile(const base::FilePath& path) {
std::string kJson = "sjkdsk;'<?js";
ASSERT_TRUE(base::WriteFile(path, kJson));
}
const base::FilePath& GetJsonFilePath() const { return temp_dir_.GetPath(); }
void WaitForSanitizationDone() {
ASSERT_FALSE(done_callback_);
base::RunLoop run_loop;
done_callback_ = run_loop.QuitClosure();
run_loop.Run();
}
void CreateAndStartSanitizer(const std::set<base::FilePath>& file_paths) {
sanitizer_ = JsonFileSanitizer::CreateAndStart(
file_paths,
base::BindOnce(&JsonFileSanitizerTest::SanitizationDone,
base::Unretained(this)),
GetExtensionFileTaskRunner());
}
base::expected<void, JsonFileSanitizer::Error> last_reported_status() const {
return last_status_;
}
private:
void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }
void SanitizationDone(base::expected<void, JsonFileSanitizer::Error> status) {
last_status_ = status;
if (done_callback_) {
std::move(done_callback_).Run();
}
}
content::BrowserTaskEnvironment task_environment_;
base::expected<void, JsonFileSanitizer::Error> last_status_;
base::OnceClosure done_callback_;
std::unique_ptr<JsonFileSanitizer> sanitizer_;
base::ScopedTempDir temp_dir_;
};
} // namespace
TEST_F(JsonFileSanitizerTest, NoFilesProvided) {
CreateAndStartSanitizer(std::set<base::FilePath>());
WaitForSanitizationDone();
EXPECT_THAT(last_reported_status(), HasValue());
}
TEST_F(JsonFileSanitizerTest, ValidCase) {
constexpr std::array<const base::FilePath::CharType* const, 10> kFileNames{
{FILE_PATH_LITERAL("test0"), FILE_PATH_LITERAL("test1"),
FILE_PATH_LITERAL("test2"), FILE_PATH_LITERAL("test3"),
FILE_PATH_LITERAL("test4"), FILE_PATH_LITERAL("test5"),
FILE_PATH_LITERAL("test6"), FILE_PATH_LITERAL("test7"),
FILE_PATH_LITERAL("test8"), FILE_PATH_LITERAL("test9")}};
std::set<base::FilePath> paths;
for (const base::FilePath::CharType* file_name : kFileNames) {
base::FilePath path = CreateFilePath(file_name);
CreateValidJsonFile(path);
paths.insert(path);
}
CreateAndStartSanitizer(paths);
WaitForSanitizationDone();
EXPECT_THAT(last_reported_status(), HasValue());
// Make sure the JSON files are there and non empty.
for (const auto& path : paths) {
std::optional<int64_t> file_size = base::GetFileSize(path);
ASSERT_TRUE(file_size.has_value());
EXPECT_GT(file_size.value(), 0);
}
}
TEST_F(JsonFileSanitizerTest, MissingJsonFile) {
constexpr base::FilePath::CharType kGoodName[] =
FILE_PATH_LITERAL("i_exists");
constexpr base::FilePath::CharType kNonExistingName[] =
FILE_PATH_LITERAL("i_don_t_exist");
base::FilePath good_path = CreateFilePath(kGoodName);
CreateValidJsonFile(good_path);
base::FilePath invalid_path = CreateFilePath(kNonExistingName);
CreateAndStartSanitizer({good_path, invalid_path});
WaitForSanitizationDone();
EXPECT_THAT(last_reported_status(),
ErrorIs(JsonFileSanitizer::Error::kFileReadError));
}
TEST_F(JsonFileSanitizerTest, InvalidJson) {
constexpr base::FilePath::CharType kGoodJsonFileName[] =
FILE_PATH_LITERAL("good.json");
constexpr base::FilePath::CharType kBadJsonFileName[] =
FILE_PATH_LITERAL("bad.json");
base::FilePath good_path = CreateFilePath(kGoodJsonFileName);
CreateValidJsonFile(good_path);
base::FilePath badd_path = CreateFilePath(kBadJsonFileName);
CreateInvalidJsonFile(badd_path);
CreateAndStartSanitizer({good_path, badd_path});
WaitForSanitizationDone();
EXPECT_THAT(last_reported_status(),
ErrorIs(JsonFileSanitizer::Error::kDecodingError));
}
} // namespace extensions
|