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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/component_updater/zxcvbn_data_component_installer.h"
#include <optional>
#include <string_view>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/strings/string_split.h"
#include "base/test/task_environment.h"
#include "base/values.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/zxcvbn-cpp/native-src/zxcvbn/frequency_lists.hpp"
namespace component_updater {
namespace {
using ::testing::Pair;
using ::testing::Pointee;
using ::testing::UnorderedElementsAre;
constexpr char kTextfilesOnlyVersion[] = "1";
constexpr char kMemoryMappedVersion[] = "2";
constexpr char kFutureVersion[] = "2.1";
// Use this function to generate a new combined file from the updated
// dictionaries.
zxcvbn::RankedDicts ParseRankedDictionaries(const base::FilePath& install_dir) {
std::vector<std::string> raw_dicts;
for (const auto& file_name : ZxcvbnDataComponentInstallerPolicy::kFileNames) {
base::FilePath dictionary_path = install_dir.Append(file_name);
DVLOG(1) << "Reading Dictionary from file: " << dictionary_path;
std::string dictionary;
if (base::ReadFileToString(dictionary_path, &dictionary)) {
raw_dicts.push_back(std::move(dictionary));
} else {
DVLOG(1) << "Failed reading from " << dictionary_path;
}
}
// The contained StringPieces hold references to the strings in raw_dicts.
std::vector<std::vector<std::string_view>> dicts;
for (const auto& raw_dict : raw_dicts) {
dicts.push_back(base::SplitStringPiece(
raw_dict, "\r\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY));
}
// This copies the words; after this call, the original strings can be
// discarded.
return zxcvbn::RankedDicts(dicts);
}
} // namespace
class ZxcvbnDataComponentInstallerPolicyTest : public ::testing::Test {
public:
void SetUp() override {
ASSERT_TRUE(component_install_dir_.CreateUniqueTempDir());
SetVersion(kTextfilesOnlyVersion);
}
ZxcvbnDataComponentInstallerPolicy& policy() { return policy_; }
base::test::TaskEnvironment& task_env() { return task_env_; }
const base::Version& version() const { return version_; }
const base::Value::Dict& manifest() const { return manifest_; }
const base::FilePath& GetPath() const {
return component_install_dir_.GetPath();
}
void SetVersion(std::string_view version_str) {
version_ = base::Version(version_str);
manifest_.Set("version", version_str);
}
void CreateEmptyTextFiles() {
for (const auto& filename :
ZxcvbnDataComponentInstallerPolicy::kFileNames) {
base::WriteFile(GetPath().Append(filename), "");
}
}
void CreateInvalidCombinedBinaryFile() {
static constexpr uint8_t marker[1] = {0x70};
ASSERT_TRUE(base::WriteFile(
GetPath().Append(
ZxcvbnDataComponentInstallerPolicy::kCombinedRankedDictsFileName),
marker));
}
void CreateValidCombinedBinaryFile() {
static constexpr uint8_t marker[1] = {0x80};
ASSERT_TRUE(base::WriteFile(
GetPath().Append(
ZxcvbnDataComponentInstallerPolicy::kCombinedRankedDictsFileName),
marker));
}
void CreateTextFiles() {
// Populated files should be read and fed to the correct ranked zxcvbn
// dictionary.
ASSERT_TRUE(base::WriteFile(
GetPath().Append(
ZxcvbnDataComponentInstallerPolicy::kEnglishWikipediaTxtFileName),
"english\nwikipedia"));
ASSERT_TRUE(base::WriteFile(
GetPath().Append(
ZxcvbnDataComponentInstallerPolicy::kFemaleNamesTxtFileName),
"female\nfnames"));
ASSERT_TRUE(base::WriteFile(
GetPath().Append(
ZxcvbnDataComponentInstallerPolicy::kMaleNamesTxtFileName),
"male\nmnames"));
ASSERT_TRUE(base::WriteFile(
GetPath().Append(
ZxcvbnDataComponentInstallerPolicy::kPasswordsTxtFileName),
"passwords"));
ASSERT_TRUE(base::WriteFile(
GetPath().Append(
ZxcvbnDataComponentInstallerPolicy::kSurnamesTxtFileName),
"surnames"));
ASSERT_TRUE(base::WriteFile(
GetPath().Append(
ZxcvbnDataComponentInstallerPolicy::kUsTvAndFilmTxtFileName),
"us\ntv\nand\nfilm"));
}
void CreateCombinedBinaryFile() {
zxcvbn::RankedDicts dicts = ParseRankedDictionaries(GetPath());
ASSERT_TRUE(base::WriteFile(
GetPath().Append(
ZxcvbnDataComponentInstallerPolicy::kCombinedRankedDictsFileName),
dicts.DataForTesting()));
}
void VerifyRankedDicts() {
zxcvbn::RankedDicts& ranked_dicts = zxcvbn::default_ranked_dicts();
EXPECT_EQ(ranked_dicts.Find("english"), 1UL);
EXPECT_EQ(ranked_dicts.Find("wikipedia"), 2UL);
EXPECT_EQ(ranked_dicts.Find("female"), 1UL);
EXPECT_EQ(ranked_dicts.Find("fnames"), 2UL);
EXPECT_EQ(ranked_dicts.Find("male"), 1UL);
EXPECT_EQ(ranked_dicts.Find("mnames"), 2UL);
EXPECT_EQ(ranked_dicts.Find("passwords"), 1UL);
EXPECT_EQ(ranked_dicts.Find("surnames"), 1UL);
EXPECT_EQ(ranked_dicts.Find("us"), 1UL);
EXPECT_EQ(ranked_dicts.Find("tv"), 2UL);
EXPECT_EQ(ranked_dicts.Find("and"), 3UL);
EXPECT_EQ(ranked_dicts.Find("film"), 4UL);
}
private:
base::test::TaskEnvironment task_env_;
base::Version version_;
base::Value::Dict manifest_;
ZxcvbnDataComponentInstallerPolicy policy_;
base::ScopedTempDir component_install_dir_;
};
// Tests that VerifyInstallation only returns true when both the text files and
// a combined binary file with a valid marker are present in the case of the
// version with support for memory mapping.
TEST_F(ZxcvbnDataComponentInstallerPolicyTest,
VerifyInstallationForMemoryMappedVersion) {
SetVersion(kMemoryMappedVersion);
// An empty directory lacks all required files.
EXPECT_FALSE(policy().VerifyInstallation(manifest(), GetPath()));
CreateEmptyTextFiles();
// The combined data file is still missing.
EXPECT_FALSE(policy().VerifyInstallation(manifest(), GetPath()));
CreateValidCombinedBinaryFile();
EXPECT_TRUE(policy().VerifyInstallation(manifest(), GetPath()));
base::DeleteFile(GetPath().Append(
ZxcvbnDataComponentInstallerPolicy::kEnglishWikipediaTxtFileName));
EXPECT_FALSE(policy().VerifyInstallation(manifest(), GetPath()));
}
// Tests that VerifyInstallation fails if the first bit of the memory mapped
// file is not a valid marker bit.
TEST_F(ZxcvbnDataComponentInstallerPolicyTest,
VerifyInstallationForMemoryMappedVersionWithInvalidMarkerBit) {
SetVersion(kMemoryMappedVersion);
CreateEmptyTextFiles();
CreateInvalidCombinedBinaryFile();
EXPECT_FALSE(policy().VerifyInstallation(manifest(), GetPath()));
}
TEST_F(ZxcvbnDataComponentInstallerPolicyTest,
VerifyInstallationExpectsValidVersion) {
// Verification fails for a missing version.
EXPECT_FALSE(policy().VerifyInstallation(base::Value::Dict(), GetPath()));
// Verification fails for an invalid version.
SetVersion("1.x2");
EXPECT_FALSE(policy().VerifyInstallation(manifest(), GetPath()));
}
TEST_F(ZxcvbnDataComponentInstallerPolicyTest, ComponentReadyForMissingFiles) {
// Empty / non-existent files should result in empty dictionaries.
policy().ComponentReady(version(), GetPath(), manifest().Clone());
task_env().RunUntilIdle();
EXPECT_FALSE(zxcvbn::default_ranked_dicts().Find("english").has_value());
}
// Tests that ComponentReady reads in the file contents and properly populates
// zxcvbn::default_ranked_dicts() when using a memory mapped file.
TEST_F(ZxcvbnDataComponentInstallerPolicyTest,
ComponentReadyForMemoryMappedVersion) {
SetVersion(kMemoryMappedVersion);
CreateTextFiles();
CreateCombinedBinaryFile();
policy().ComponentReady(version(), GetPath(), manifest().Clone());
task_env().RunUntilIdle();
VerifyRankedDicts();
}
// Tests that updates are handled gracefully and despite potentially blocking
// behavior while closing a memory mapped file.
TEST_F(ZxcvbnDataComponentInstallerPolicyTest,
ComponentReadyHandlesUpdateProperly) {
SetVersion(kMemoryMappedVersion);
CreateTextFiles();
CreateCombinedBinaryFile();
policy().ComponentReady(version(), GetPath(), manifest().Clone());
task_env().RunUntilIdle();
VerifyRankedDicts();
SetVersion(kFutureVersion);
policy().ComponentReady(version(), GetPath(), manifest().Clone());
task_env().RunUntilIdle();
VerifyRankedDicts();
}
} // namespace component_updater
|