summaryrefslogtreecommitdiffstats
path: root/chrome/browser/safe_browsing/incident_reporting/state_store_unittest.cc
blob: 25774ffeef730ed3ab0959eecf4d7bddfcf56df8 (plain)
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/safe_browsing/incident_reporting/state_store.h"

#include "base/files/scoped_temp_dir.h"
#include "base/json/json_file_value_serializer.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/test_simple_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "base/values.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/browser/safe_browsing/incident_reporting/incident.h"
#include "chrome/browser/safe_browsing/incident_reporting/platform_state_store.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/syncable_prefs/pref_service_syncable.h"
#include "components/syncable_prefs/pref_service_syncable_factory.h"
#include "testing/gtest/include/gtest/gtest.h"

#if defined(OS_WIN)
#include "base/test/test_reg_util_win.h"
#endif

namespace safe_browsing {

#if defined(OS_WIN)

// A base test fixture that redirects HKCU for testing the platform state store
// backed by the Windows registry to prevent interference with existing Chrome
// installs or other tests.
class PlatformStateStoreTestBase : public ::testing::Test {
 protected:
  PlatformStateStoreTestBase() {}

  void SetUp() override {
    ::testing::Test::SetUp();
    registry_override_manager_.OverrideRegistry(HKEY_CURRENT_USER);
  }

 private:
  registry_util::RegistryOverrideManager registry_override_manager_;

  DISALLOW_COPY_AND_ASSIGN(PlatformStateStoreTestBase);
};

#else  // OS_WIN

using PlatformStateStoreTestBase = ::testing::Test;

#endif  // !OS_WIN

// A test fixture with a testing profile that writes its user prefs to a json
// file.
class StateStoreTest : public PlatformStateStoreTestBase {
 protected:
  struct TestData {
    IncidentType type;
    const char* key;
    uint32_t digest;
  };

  StateStoreTest()
      : profile_(nullptr),
        task_runner_(new base::TestSimpleTaskRunner()),
        thread_task_runner_handle_(task_runner_),
        profile_manager_(TestingBrowserProcess::GetGlobal()) {}

  void SetUp() override {
    PlatformStateStoreTestBase::SetUp();
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    ASSERT_TRUE(profile_manager_.SetUp());
    CreateProfile();
  }

  void DeleteProfile() {
    if (profile_) {
      profile_ = nullptr;
      profile_manager_.DeleteTestingProfile(kProfileName_);
    }
  }

  // Removes the safebrowsing.incidents_sent preference from the profile's pref
  // store.
  void TrimPref() {
    ASSERT_EQ(nullptr, profile_);
    scoped_ptr<base::Value> prefs(JSONFileValueDeserializer(GetPrefsPath())
                                      .Deserialize(nullptr, nullptr));
    ASSERT_NE(nullptr, prefs.get());
    base::DictionaryValue* dict = nullptr;
    ASSERT_TRUE(prefs->GetAsDictionary(&dict));
    ASSERT_TRUE(dict->Remove(prefs::kSafeBrowsingIncidentsSent, nullptr));
    ASSERT_TRUE(JSONFileValueSerializer(GetPrefsPath()).Serialize(*dict));
  }

  void CreateProfile() {
    ASSERT_EQ(nullptr, profile_);
    // Create the testing profile with a file-backed user pref store.
    syncable_prefs::PrefServiceSyncableFactory factory;
    factory.SetUserPrefsFile(GetPrefsPath(), task_runner_.get());
    user_prefs::PrefRegistrySyncable* pref_registry =
        new user_prefs::PrefRegistrySyncable();
    chrome::RegisterUserProfilePrefs(pref_registry);
    profile_ = profile_manager_.CreateTestingProfile(
        kProfileName_, factory.CreateSyncable(pref_registry).Pass(),
        base::UTF8ToUTF16(kProfileName_), 0, std::string(),
        TestingProfile::TestingFactories());
  }

  static const char kProfileName_[];
  static const TestData kTestData_[];
  TestingProfile* profile_;
  scoped_refptr<base::TestSimpleTaskRunner> task_runner_;

 private:
  base::FilePath GetPrefsPath() {
    return temp_dir_.path().AppendASCII("prefs");
  }

  base::ScopedTempDir temp_dir_;
  base::ThreadTaskRunnerHandle thread_task_runner_handle_;
  TestingProfileManager profile_manager_;

  DISALLOW_COPY_AND_ASSIGN(StateStoreTest);
};

// static
const char StateStoreTest::kProfileName_[] = "test_profile";
const StateStoreTest::TestData StateStoreTest::kTestData_[] = {
    {IncidentType::TRACKED_PREFERENCE, "tp_one", 1},
    {IncidentType::TRACKED_PREFERENCE, "tp_two", 2},
    {IncidentType::TRACKED_PREFERENCE, "tp_three", 3},
    {IncidentType::BINARY_INTEGRITY, "bi", 0},
    {IncidentType::BLACKLIST_LOAD, "bl", 0x47},
};

TEST_F(StateStoreTest, MarkAsAndHasBeenReported) {
  StateStore state_store(profile_);

  for (const auto& data : kTestData_)
    ASSERT_FALSE(state_store.HasBeenReported(data.type, data.key, data.digest));

  {
    StateStore::Transaction transaction(&state_store);
    for (const auto& data : kTestData_) {
      transaction.MarkAsReported(data.type, data.key, data.digest);
      ASSERT_TRUE(
          state_store.HasBeenReported(data.type, data.key, data.digest));
    }
  }

  for (const auto& data : kTestData_)
    ASSERT_TRUE(state_store.HasBeenReported(data.type, data.key, data.digest));
}

TEST_F(StateStoreTest, ClearForType) {
  StateStore state_store(profile_);

  {
    StateStore::Transaction transaction(&state_store);
    for (const auto& data : kTestData_)
      transaction.MarkAsReported(data.type, data.key, data.digest);
  }

  for (const auto& data : kTestData_)
    ASSERT_TRUE(state_store.HasBeenReported(data.type, data.key, data.digest));

  const IncidentType removed_type = IncidentType::TRACKED_PREFERENCE;
  StateStore::Transaction(&state_store).ClearForType(removed_type);

  for (const auto& data : kTestData_) {
    if (data.type == removed_type) {
      ASSERT_FALSE(
          state_store.HasBeenReported(data.type, data.key, data.digest));
    } else {
      ASSERT_TRUE(
          state_store.HasBeenReported(data.type, data.key, data.digest));
    }
  }
}

TEST_F(StateStoreTest, ClearAll) {
  StateStore state_store(profile_);
  // Write some state to the store.
  {
    StateStore::Transaction transaction(&state_store);
    for (const auto& data : kTestData_)
      transaction.MarkAsReported(data.type, data.key, data.digest);
  }

  StateStore::Transaction(&state_store).ClearAll();

  for (const auto& data : kTestData_) {
    ASSERT_FALSE(state_store.HasBeenReported(data.type, data.key, data.digest));
  }

  // Run tasks to write prefs out to the JsonPrefStore.
  task_runner_->RunUntilIdle();

  // Delete the profile.
  DeleteProfile();

  // Recreate the profile.
  CreateProfile();

  StateStore store_2(profile_);
  for (const auto& data : kTestData_) {
    // Verify that the state did not survive through the Platform State Store.
    ASSERT_FALSE(store_2.HasBeenReported(data.type, data.key, data.digest));
  }
}

TEST_F(StateStoreTest, Persistence) {
  // Write some state to the store.
  {
    StateStore state_store(profile_);
    StateStore::Transaction transaction(&state_store);
    for (const auto& data : kTestData_)
      transaction.MarkAsReported(data.type, data.key, data.digest);
  }

  // Run tasks to write prefs out to the JsonPrefStore.
  task_runner_->RunUntilIdle();

  // Delete the profile.
  DeleteProfile();

  // Recreate the profile.
  CreateProfile();

  // Verify that the state survived.
  StateStore state_store(profile_);
  for (const auto& data : kTestData_)
    ASSERT_TRUE(state_store.HasBeenReported(data.type, data.key, data.digest));
}

TEST_F(StateStoreTest, PersistenceWithStoreDelete) {
  // Write some state to the store.
  {
    StateStore state_store(profile_);
    StateStore::Transaction transaction(&state_store);
    for (const auto& data : kTestData_)
      transaction.MarkAsReported(data.type, data.key, data.digest);
  }

  // Run tasks to write prefs out to the JsonPrefStore.
  task_runner_->RunUntilIdle();

  // Delete the profile.
  DeleteProfile();

  // Delete the state pref.
  TrimPref();

  // Recreate the profile.
  CreateProfile();

  StateStore state_store(profile_);
  for (const auto& data : kTestData_) {
#if defined(USE_PLATFORM_STATE_STORE)
    // Verify that the state survived.
    ASSERT_TRUE(state_store.HasBeenReported(data.type, data.key, data.digest));
#else
    // Verify that the state did not survive.
    ASSERT_FALSE(state_store.HasBeenReported(data.type, data.key, data.digest));
#endif
  }
}

}  // namespace safe_browsing