summaryrefslogtreecommitdiff
path: root/chromium/components/previews/core/previews_opt_out_store_sql_unittest.cc
blob: 9974885b1d8a935459ed2d0cf6ab9237b842a886 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright (c) 2016 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 "components/previews/core/previews_opt_out_store_sql.h"

#include <map>
#include <memory>
#include <string>

#include "base/bind.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/field_trial_param_associator.h"
#include "base/metrics/field_trial_params.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/histogram_tester.h"
#include "base/test/simple_test_clock.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "components/previews/core/previews_black_list_item.h"
#include "components/previews/core/previews_opt_out_store.h"
#include "sql/test/test_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace previews {

namespace {

const base::FilePath::CharType kOptOutFilename[] = FILE_PATH_LITERAL("OptOut");

}  // namespace

class PreviewsOptOutStoreSQLTest : public testing::Test {
 public:
  PreviewsOptOutStoreSQLTest()
      : field_trials_(new base::FieldTrialList(nullptr)) {}
  ~PreviewsOptOutStoreSQLTest() override {}

  // Called when |store_| is done loading.
  void OnLoaded(std::unique_ptr<BlackListItemMap> black_list_map,
                std::unique_ptr<PreviewsBlackListItem> host_indifferent_item) {
    black_list_map_ = std::move(black_list_map);
    host_indifferent_item_ = std::move(host_indifferent_item);
  }

  // Initializes the store and get the data from it.
  void Load() {
    store_->LoadBlackList(base::Bind(&PreviewsOptOutStoreSQLTest::OnLoaded,
                                     base::Unretained(this)));
    base::RunLoop().RunUntilIdle();
  }

  // Destroys the database connection and |store_|.
  void DestroyStore() {
    store_.reset();
    base::RunLoop().RunUntilIdle();
  }

  // Creates a store that operates on one thread.
  void Create() {
    store_ = base::MakeUnique<PreviewsOptOutStoreSQL>(
        base::ThreadTaskRunnerHandle::Get(),
        base::ThreadTaskRunnerHandle::Get(),
        temp_dir_.GetPath().Append(kOptOutFilename));
  }

  // Sets up initialization of |store_|.
  void CreateAndLoad() {
    Create();
    Load();
  }

  // Creates a directory for the test.
  void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }

  // Delete |store_| if it hasn't been deleted.
  void TearDown() override { DestroyStore(); }

 protected:
  void ResetFieldTrials() {
    // Destroy existing FieldTrialList before creating new one to avoid DCHECK.
    field_trials_.reset();
    field_trials_.reset(new base::FieldTrialList(nullptr));
    base::FieldTrialParamAssociator::GetInstance()->ClearAllParamsForTesting();
  }

  base::HistogramTester histogram_tester_;

  base::MessageLoop message_loop_;

  // The backing SQL store.
  std::unique_ptr<PreviewsOptOutStoreSQL> store_;

  // The map returned from |store_|.
  std::unique_ptr<BlackListItemMap> black_list_map_;

  // The host indifferent item from |store_|
  std::unique_ptr<PreviewsBlackListItem> host_indifferent_item_;

  // The directory for the database.
  base::ScopedTempDir temp_dir_;

 private:
  std::unique_ptr<base::FieldTrialList> field_trials_;
};

TEST_F(PreviewsOptOutStoreSQLTest, TestErrorRecovery) {
  // Creates the database and corrupt to test the recovery method.
  std::string test_host = "host.com";
  CreateAndLoad();
  store_->AddPreviewNavigation(true, test_host, PreviewsType::OFFLINE,
                               base::Time::Now());
  base::RunLoop().RunUntilIdle();
  DestroyStore();

  // Corrupts the database by adjusting the header size.
  EXPECT_TRUE(sql::test::CorruptSizeInHeader(
      temp_dir_.GetPath().Append(kOptOutFilename)));
  base::RunLoop().RunUntilIdle();

  CreateAndLoad();
  // The data should be recovered.
  EXPECT_EQ(1U, black_list_map_->size());
  auto iter = black_list_map_->find(test_host);

  EXPECT_NE(black_list_map_->end(), iter);
  EXPECT_EQ(1U, iter->second->OptOutRecordsSizeForTesting());
}

TEST_F(PreviewsOptOutStoreSQLTest, TestPersistance) {
  // Tests if data is stored as expected in the SQLite database.
  std::string test_host = "host.com";
  CreateAndLoad();
  histogram_tester_.ExpectUniqueSample("Previews.OptOut.DBRowCount", 0, 1);
  base::Time now = base::Time::Now();
  store_->AddPreviewNavigation(true, test_host, PreviewsType::OFFLINE, now);
  base::RunLoop().RunUntilIdle();

  // Replace the store effectively destroying the current one and forcing it
  // to write its data to disk.
  DestroyStore();

  // Reload and test for persistence
  CreateAndLoad();
  EXPECT_EQ(1U, black_list_map_->size());
  auto iter = black_list_map_->find(test_host);

  EXPECT_NE(black_list_map_->end(), iter);
  EXPECT_EQ(1U, iter->second->OptOutRecordsSizeForTesting());
  EXPECT_EQ(now, iter->second->most_recent_opt_out_time().value());
  EXPECT_EQ(1U, host_indifferent_item_->OptOutRecordsSizeForTesting());
  EXPECT_EQ(now, host_indifferent_item_->most_recent_opt_out_time().value());
  histogram_tester_.ExpectBucketCount("Previews.OptOut.DBRowCount", 1, 1);
  histogram_tester_.ExpectTotalCount("Previews.OptOut.DBRowCount", 2);
}

TEST_F(PreviewsOptOutStoreSQLTest, TestMaxRows) {
  // Tests that the number of rows are culled down to the row limit at each
  // load.
  std::string test_host_a = "host_a.com";
  std::string test_host_b = "host_b.com";
  std::string test_host_c = "host_c.com";
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  size_t row_limit = 2;
  std::string row_limit_string = base::SizeTToString(row_limit);
  command_line->AppendSwitchASCII("previews-max-opt-out-rows",
                                  row_limit_string);
  CreateAndLoad();
  histogram_tester_.ExpectUniqueSample("Previews.OptOut.DBRowCount", 0, 1);
  base::SimpleTestClock clock;

  // Create three different entries with different hosts.
  store_->AddPreviewNavigation(true, test_host_a, PreviewsType::OFFLINE,
                               clock.Now());
  clock.Advance(base::TimeDelta::FromSeconds(1));

  store_->AddPreviewNavigation(true, test_host_b, PreviewsType::OFFLINE,
                               clock.Now());
  base::Time host_b_time = clock.Now();
  clock.Advance(base::TimeDelta::FromSeconds(1));

  store_->AddPreviewNavigation(false, test_host_c, PreviewsType::OFFLINE,
                               clock.Now());
  base::RunLoop().RunUntilIdle();
  // Replace the store effectively destroying the current one and forcing it
  // to write its data to disk.
  DestroyStore();

  // Reload and test for persistence
  CreateAndLoad();
  histogram_tester_.ExpectBucketCount("Previews.OptOut.DBRowCount",
                                      static_cast<int>(row_limit) + 1, 1);
  // The delete happens after the load, so it is possible to load more than
  // |row_limit| into the in memory map.
  EXPECT_EQ(row_limit + 1, black_list_map_->size());
  EXPECT_EQ(row_limit + 1,
            host_indifferent_item_->OptOutRecordsSizeForTesting());

  DestroyStore();
  CreateAndLoad();
  histogram_tester_.ExpectBucketCount("Previews.OptOut.DBRowCount",
                                      static_cast<int>(row_limit), 1);

  EXPECT_EQ(row_limit, black_list_map_->size());
  auto iter_host_b = black_list_map_->find(test_host_b);
  auto iter_host_c = black_list_map_->find(test_host_c);

  EXPECT_EQ(black_list_map_->end(), black_list_map_->find(test_host_a));
  EXPECT_NE(black_list_map_->end(), iter_host_b);
  EXPECT_NE(black_list_map_->end(), iter_host_c);
  EXPECT_EQ(host_b_time,
            iter_host_b->second->most_recent_opt_out_time().value());
  EXPECT_EQ(1U, iter_host_b->second->OptOutRecordsSizeForTesting());
  EXPECT_EQ(host_b_time,
            host_indifferent_item_->most_recent_opt_out_time().value());
  EXPECT_EQ(row_limit, host_indifferent_item_->OptOutRecordsSizeForTesting());
  histogram_tester_.ExpectTotalCount("Previews.OptOut.DBRowCount", 3);
}

TEST_F(PreviewsOptOutStoreSQLTest, TestMaxRowsPerHost) {
  // Tests that each host is limited to |row_limit| rows.
  std::string test_host = "host.com";
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  size_t row_limit = 2;
  std::string row_limit_string = base::SizeTToString(row_limit);
  command_line->AppendSwitchASCII("previews-max-opt-out-rows-per-host",
                                  row_limit_string);
  CreateAndLoad();
  histogram_tester_.ExpectUniqueSample("Previews.OptOut.DBRowCount", 0, 1);
  base::SimpleTestClock clock;

  base::Time last_opt_out_time;
  for (size_t i = 0; i < row_limit; i++) {
    store_->AddPreviewNavigation(true, test_host, PreviewsType::OFFLINE,
                                 clock.Now());
    last_opt_out_time = clock.Now();
    clock.Advance(base::TimeDelta::FromSeconds(1));
  }

  clock.Advance(base::TimeDelta::FromSeconds(1));
  store_->AddPreviewNavigation(false, test_host, PreviewsType::OFFLINE,
                               clock.Now());

  base::RunLoop().RunUntilIdle();
  // Replace the store effectively destroying the current one and forcing it
  // to write its data to disk.
  DestroyStore();

  // Reload and test for persistence.
  CreateAndLoad();
  histogram_tester_.ExpectBucketCount("Previews.OptOut.DBRowCount",
                                      static_cast<int>(row_limit), 1);

  EXPECT_EQ(1U, black_list_map_->size());
  auto iter = black_list_map_->find(test_host);

  EXPECT_NE(black_list_map_->end(), iter);
  EXPECT_EQ(last_opt_out_time,
            iter->second->most_recent_opt_out_time().value());
  EXPECT_EQ(row_limit, iter->second->OptOutRecordsSizeForTesting());
  EXPECT_EQ(row_limit, host_indifferent_item_->OptOutRecordsSizeForTesting());
  clock.Advance(base::TimeDelta::FromSeconds(1));
  // If both entries' opt out states are stored correctly, then this should not
  // be black listed.
  EXPECT_FALSE(iter->second->IsBlackListed(clock.Now()));
  histogram_tester_.ExpectTotalCount("Previews.OptOut.DBRowCount", 2);
}

TEST_F(PreviewsOptOutStoreSQLTest, TestPreviewsDisabledClearsBlacklistEntry) {
  // Tests if data is cleared for previews type when it is disabled.
  // Enable offline previews and add black list entry for it.
  std::map<std::string, std::string> params;
  params["show_offline_pages"] = "true";
  EXPECT_TRUE(
      base::AssociateFieldTrialParams("ClientSidePreviews", "Enabled", params));
  EXPECT_TRUE(
      base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled"));
  std::string test_host = "host.com";
  CreateAndLoad();
  histogram_tester_.ExpectUniqueSample("Previews.OptOut.DBRowCount", 0, 1);
  base::Time now = base::Time::Now();
  store_->AddPreviewNavigation(true, test_host, PreviewsType::OFFLINE, now);
  base::RunLoop().RunUntilIdle();

  // Force data write to database then reload it and verify black list entry
  // is present.
  DestroyStore();
  CreateAndLoad();
  auto iter = black_list_map_->find(test_host);
  EXPECT_NE(black_list_map_->end(), iter);
  EXPECT_EQ(1U, iter->second->OptOutRecordsSizeForTesting());

  // Now reload with offline pages previews disabled and verify black list
  // entry dropped.
  ResetFieldTrials();
  params["show_offline_pages"] = "false";
  EXPECT_TRUE(
      base::AssociateFieldTrialParams("ClientSidePreviews", "Enabled", params));
  EXPECT_TRUE(
      base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled"));
  DestroyStore();
  CreateAndLoad();
  iter = black_list_map_->find(test_host);
  EXPECT_EQ(black_list_map_->end(), iter);

  // Clean up field trials set in this test.
  ResetFieldTrials();
}

TEST_F(PreviewsOptOutStoreSQLTest,
       TestPreviewsVersionUpdateClearsBlacklistEntry) {
  // Tests if data is cleared for new version of previews type.
  // Enable offline previews and add black list entry for it.
  std::map<std::string, std::string> params;
  params["show_offline_pages"] = "true";
  params["version"] = "1";
  EXPECT_TRUE(
      base::AssociateFieldTrialParams("ClientSidePreviews", "Enabled", params));
  EXPECT_TRUE(
      base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled"));
  std::string test_host = "host.com";
  CreateAndLoad();
  histogram_tester_.ExpectUniqueSample("Previews.OptOut.DBRowCount", 0, 1);
  base::Time now = base::Time::Now();
  store_->AddPreviewNavigation(true, test_host, PreviewsType::OFFLINE, now);
  base::RunLoop().RunUntilIdle();

  // Force data write to database then reload it and verify black list entry
  // is present.
  DestroyStore();
  CreateAndLoad();
  auto iter = black_list_map_->find(test_host);
  EXPECT_NE(black_list_map_->end(), iter);
  EXPECT_EQ(1U, iter->second->OptOutRecordsSizeForTesting());

  // Now reload with incremented previews version and verify black list
  // entry dropped.
  ResetFieldTrials();
  params["version"] = "2";
  EXPECT_TRUE(
      base::AssociateFieldTrialParams("ClientSidePreviews", "Enabled", params));
  EXPECT_TRUE(
      base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled"));
  DestroyStore();
  CreateAndLoad();
  iter = black_list_map_->find(test_host);
  EXPECT_EQ(black_list_map_->end(), iter);

  // Clean up field trials set in this test.
  ResetFieldTrials();
}

}  // namespace net