File: single_client_backup_rollback_test.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (441 lines) | stat: -rw-r--r-- 15,999 bytes parent folder | download
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright 2014 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 "base/command_line.h"
#include "base/files/file_util.h"
#include "base/message_loop/message_loop.h"
#include "base/prefs/pref_service.h"
#include "base/run_loop.h"
#include "base/test/test_timeouts.h"
#include "chrome/browser/browsing_data/browsing_data_remover.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/test/integration/bookmarks_helper.h"
#include "chrome/browser/sync/test/integration/preferences_helper.h"
#include "chrome/browser/sync/test/integration/sync_integration_test_util.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "chrome/common/chrome_switches.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "sync/internal_api/public/util/sync_db_util.h"
#include "sync/test/fake_server/fake_server_verifier.h"
#include "sync/util/time.h"

using bookmarks_helper::AddFolder;
using bookmarks_helper::AddURL;
using bookmarks_helper::GetOtherNode;
using bookmarks_helper::ModelMatchesVerifier;
using bookmarks_helper::Move;
using bookmarks_helper::Remove;
using sync_integration_test_util::AwaitCommitActivityCompletion;

namespace {
const char kUrl1[] = "http://www.google.com";
const char kUrl2[] = "http://map.google.com";
const char kUrl3[] = "http://plus.google.com";
}  // anonymous namespace

class SingleClientBackupRollbackTest : public SyncTest {
 public:
  SingleClientBackupRollbackTest() : SyncTest(SINGLE_CLIENT) {}
  ~SingleClientBackupRollbackTest() override {}

  void DisableBackup() {
    base::CommandLine::ForCurrentProcess()->AppendSwitch(
        switches::kSyncDisableBackup);
  }

  void DisableRollback() {
    base::CommandLine::ForCurrentProcess()->AppendSwitch(
        switches::kSyncDisableRollback);
  }

  base::Time GetBackupDbLastModified() {
    base::RunLoop run_loop;

    base::Time backup_time;
    syncer::CheckSyncDbLastModifiedTime(
        GetProfile(0)->GetPath().Append(FILE_PATH_LITERAL("Sync Data Backup")),
        base::MessageLoopProxy::current(),
        base::Bind(&SingleClientBackupRollbackTest::CheckDbCallback,
                   base::Unretained(this), &backup_time));
    base::MessageLoopProxy::current()->PostTask(
        FROM_HERE, run_loop.QuitClosure());
    run_loop.Run();
    return backup_time;
  }

 private:
  void CheckDbCallback(base::Time* time_out, base::Time time_in) {
    *time_out = syncer::ProtoTimeToTime(syncer::TimeToProtoTime(time_in));
  }

  DISALLOW_COPY_AND_ASSIGN(SingleClientBackupRollbackTest);
};

// Waits until the ProfileSyncService's backend is in IDLE mode.
class SyncBackendStoppedChecker : public ProfileSyncServiceBase::Observer {
 public:
  explicit SyncBackendStoppedChecker(ProfileSyncService* service)
      : pss_(service),
        timeout_(TestTimeouts::action_max_timeout()),
        done_(false) {}

  void OnStateChanged() override {
    if (ProfileSyncService::IDLE == pss_->backend_mode()) {
      done_ = true;
      run_loop_.Quit();
    }
  }

  bool Wait() {
    pss_->AddObserver(this);
    if (ProfileSyncService::IDLE == pss_->backend_mode())
      return true;
    base::MessageLoop::current()->PostDelayedTask(
        FROM_HERE,
        run_loop_.QuitClosure(),
        timeout_);
    run_loop_.Run();
    pss_->RemoveObserver(this);
    return done_;
  }

 private:

  ProfileSyncService* const pss_;
  const base::TimeDelta timeout_;
  base::RunLoop run_loop_;
  bool done_;
};

// Waits until a rollback finishes.
class SyncRollbackChecker : public ProfileSyncServiceBase::Observer,
                            public BrowsingDataRemover::Observer {
 public:
  explicit SyncRollbackChecker(ProfileSyncService* service)
      : pss_(service),
        timeout_(TestTimeouts::action_max_timeout()),
        rollback_started_(false),
        clear_done_(false) {}

  // ProfileSyncServiceBase::Observer implementation.
  void OnStateChanged() override {
    if (ProfileSyncService::ROLLBACK == pss_->backend_mode()) {
      rollback_started_ = true;
      if (clear_done_)
        run_loop_.Quit();
    }
  }

  // BrowsingDataRemoverObserver::Observer implementation.
  void OnBrowsingDataRemoverDone() override {
    clear_done_ = true;
    if (rollback_started_) {
      run_loop_.Quit();
    }
  }

  bool Wait() {
    pss_->AddObserver(this);
    pss_->SetBrowsingDataRemoverObserverForTesting(this);
    base::MessageLoop::current()->PostDelayedTask(
        FROM_HERE,
        run_loop_.QuitClosure(),
        timeout_);
    run_loop_.Run();
    pss_->RemoveObserver(this);
    return rollback_started_ && clear_done_;
  }

  ProfileSyncService* const pss_;
  const base::TimeDelta timeout_;
  base::RunLoop run_loop_;
  bool rollback_started_;
  bool clear_done_;
};

#if defined(ENABLE_PRE_SYNC_BACKUP)
#define MAYBE_TestBackup TestBackup
#else
#define MAYBE_TestBackup DISABLED_TestBackup
#endif
IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
                       MAYBE_TestBackup) {
  ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";

  // Setup sync, wait for its completion, and make sure changes were synced.
  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0)));
  ASSERT_TRUE(ModelMatchesVerifier(0));

  // Verify backup DB is created and backup time is set on device info.
  base::Time backup_time = GetBackupDbLastModified();
  ASSERT_FALSE(backup_time.is_null());
  ASSERT_EQ(backup_time, GetSyncService(0)->GetDeviceBackupTimeForTesting());
}

#if defined(ENABLE_PRE_SYNC_BACKUP)
#define MAYBE_TestBackupDisabled TestBackupDisabled
#else
#define MAYBE_TestBackupDisabled DISABLED_TestBackupDisabled
#endif
IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
                       MAYBE_TestBackupDisabled) {
  DisableBackup();

  // Setup sync, wait for its completion, and make sure changes were synced.
  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0)));
  ASSERT_TRUE(ModelMatchesVerifier(0));

  // Verify backup DB is not created and backup time is not set on device info.
  ASSERT_FALSE(base::PathExists(
      GetProfile(0)->GetPath().Append(FILE_PATH_LITERAL("Sync Data Backup"))));
  ASSERT_TRUE(GetSyncService(0)->GetDeviceBackupTimeForTesting().is_null());
}

#if defined(ENABLE_PRE_SYNC_BACKUP)
#define MAYBE_TestRollback TestRollback
#else
#define MAYBE_TestRollback DISABLED_TestRollback
#endif
IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
                       MAYBE_TestRollback) {
  ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";

  // Starting state:
  // other_node
  //    -> top
  //      -> tier1_a
  //        -> http://mail.google.com  "tier1_a_url0"
  //      -> tier1_b
  //        -> http://www.nhl.com "tier1_b_url0"
  const BookmarkNode* top = AddFolder(0, GetOtherNode(0), 0, "top");
  const BookmarkNode* tier1_a = AddFolder(0, top, 0, "tier1_a");
  const BookmarkNode* tier1_b = AddFolder(0, top, 1, "tier1_b");
  ASSERT_TRUE(AddURL(0, tier1_a, 0, "tier1_a_url0",
                     GURL("http://mail.google.com")));
  ASSERT_TRUE(AddURL(0, tier1_b, 0, "tier1_b_url0",
                     GURL("http://www.nhl.com")));

  // Setup sync, wait for its completion, and make sure changes were synced.
  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0)));
  ASSERT_TRUE(ModelMatchesVerifier(0));

  // Made bookmark changes while sync is on.
  Move(0, tier1_a->GetChild(0), tier1_b, 1);
  Remove(0, tier1_b, 0);
  ASSERT_TRUE(AddFolder(0, tier1_b, 1, "tier2_c"));
  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0)));
  ASSERT_TRUE(ModelMatchesVerifier(0));

  // Let server to return rollback command on next sync request.
  ASSERT_TRUE(GetFakeServer()->TriggerError(sync_pb::SyncEnums::USER_ROLLBACK));

  // Make another change to trigger downloading of rollback command.
  Remove(0, tier1_b, 0);

  // Wait for rollback to finish and sync backend is completely shut down.
  SyncRollbackChecker rollback_checker(GetSyncService(0));
  ASSERT_TRUE(rollback_checker.Wait());
  SyncBackendStoppedChecker shutdown_checker(GetSyncService(0));
  ASSERT_TRUE(shutdown_checker.Wait());

  // Verify bookmarks are restored.
  ASSERT_EQ(1, tier1_a->child_count());
  const BookmarkNode* url1 = tier1_a->GetChild(0);
  ASSERT_EQ(GURL("http://mail.google.com"), url1->url());

  ASSERT_EQ(1, tier1_b->child_count());
  const BookmarkNode* url2 = tier1_b->GetChild(0);
  ASSERT_EQ(GURL("http://www.nhl.com"), url2->url());
}

#if defined(ENABLE_PRE_SYNC_BACKUP)
#define MAYBE_TestRollbackDisabled TestRollbackDisabled
#else
#define MAYBE_TestRollbackDisabled DISABLED_TestRollbackDisabled
#endif
IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
                       MAYBE_TestRollbackDisabled) {
  DisableRollback();

  ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";

  // Starting state:
  // other_node
  //    -> http://mail.google.com  "url0"
  //    -> http://www.nhl.com "url1"
  ASSERT_TRUE(AddURL(0, GetOtherNode(0), 0, "url0",
                     GURL("http://mail.google.com")));
  ASSERT_TRUE(AddURL(0, GetOtherNode(0), 1, "url1",
                     GURL("http://www.nhl.com")));

  // Setup sync, wait for its completion, and make sure changes were synced.
  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0)));
  ASSERT_TRUE(ModelMatchesVerifier(0));

  // Made bookmark changes while sync is on.
  Remove(0, GetOtherNode(0), 1);
  ASSERT_TRUE(AddURL(0, GetOtherNode(0), 1, "url2",
                     GURL("http://www.yahoo.com")));
  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((0))));
  ASSERT_TRUE(ModelMatchesVerifier(0));

  // Let server to return rollback command on next sync request.
  ASSERT_TRUE(GetFakeServer()->TriggerError(sync_pb::SyncEnums::USER_ROLLBACK));

  // Make another change to trigger downloading of rollback command.
  Remove(0, GetOtherNode(0), 0);

  // Wait for sync backend is completely shut down.
  SyncBackendStoppedChecker shutdown_checker(GetSyncService(0));
  ASSERT_TRUE(shutdown_checker.Wait());

  // With rollback disabled, bookmarks in backup DB should not be restored.
  // Only bookmark added during sync is present.
  ASSERT_EQ(1, GetOtherNode(0)->child_count());
  ASSERT_EQ(GURL("http://www.yahoo.com"),
            GetOtherNode(0)->GetChild(0)->url());
}

#if defined(ENABLE_PRE_SYNC_BACKUP)
#define MAYBE_TestSyncDisabled TestSyncDisabled
#else
#define MAYBE_TestSyncDisabled DISABLED_TestSyncDisabled
#endif
IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
                       MAYBE_TestSyncDisabled) {
  ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";

  // Starting state:
  // other_node
  //    -> http://mail.google.com  "url0"
  //    -> http://www.nhl.com "url1"
  ASSERT_TRUE(AddURL(0, GetOtherNode(0), 0, "url0",
                     GURL("http://mail.google.com")));
  ASSERT_TRUE(AddURL(0, GetOtherNode(0), 1, "url1",
                     GURL("http://www.nhl.com")));

  // Setup sync, wait for its completion, and make sure changes were synced.
  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0)));
  ASSERT_TRUE(ModelMatchesVerifier(0));

  // Made bookmark changes while sync is on.
  Remove(0, GetOtherNode(0), 1);
  ASSERT_TRUE(AddURL(0, GetOtherNode(0), 1, "url2",
                     GURL("http://www.yahoo.com")));
  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((0))));
  ASSERT_TRUE(ModelMatchesVerifier(0));

  // Let server to return birthday error on next sync request.
  ASSERT_TRUE(GetFakeServer()->TriggerError(
      sync_pb::SyncEnums::NOT_MY_BIRTHDAY));

  // Make another change to trigger downloading of rollback command.
  Remove(0, GetOtherNode(0), 0);

  // Wait sync backend is completely shut down.
  SyncBackendStoppedChecker shutdown_checker(GetSyncService(0));
  ASSERT_TRUE(shutdown_checker.Wait());

  // Shouldn't restore bookmarks with sign-out only.
  ASSERT_EQ(1, GetOtherNode(0)->child_count());
  ASSERT_EQ(GURL("http://www.yahoo.com"),
            GetOtherNode(0)->GetChild(0)->url());
}

#if defined(ENABLE_PRE_SYNC_BACKUP)
#define MAYBE_RollbackNoBackup RollbackNoBackup
#else
#define MAYBE_RollbackNoBackup DISABLED_RollbackNoBackup
#endif
IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
                       MAYBE_RollbackNoBackup) {
  ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";

  // Starting state:
  // other_node
  //    -> http://mail.google.com  "url0"
  //    -> http://www.nhl.com "url1"
  ASSERT_TRUE(AddURL(0, GetOtherNode(0), 0, "url0",
                     GURL("http://mail.google.com")));

  // Setup sync, wait for its completion, and make sure changes were synced.
  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((0))));
  ASSERT_TRUE(ModelMatchesVerifier(0));

  ASSERT_TRUE(AddURL(0, GetOtherNode(0), 1, "url1",
                     GURL("http://www.nhl.com")));

  // Delete backup DB.
  base::DeleteFile(
      GetProfile(0)->GetPath().Append(FILE_PATH_LITERAL("Sync Data Backup")),
      true);

  // Let server to return rollback command on next sync request.
  ASSERT_TRUE(GetFakeServer()->TriggerError(sync_pb::SyncEnums::USER_ROLLBACK));

  // Make another change to trigger downloading of rollback command.
  Remove(0, GetOtherNode(0), 0);

  // Wait for rollback to finish and sync backend is completely shut down.
  SyncRollbackChecker rollback_checker(GetSyncService(0));
  ASSERT_TRUE(rollback_checker.Wait());
  SyncBackendStoppedChecker checker(GetSyncService(0));
  ASSERT_TRUE(checker.Wait());

  // Without backup DB, bookmarks remain at the state when sync stops.
  ASSERT_EQ(1, GetOtherNode(0)->child_count());
  ASSERT_EQ(GURL("http://www.nhl.com"),
            GetOtherNode(0)->GetChild(0)->url());
}

#if defined(ENABLE_PRE_SYNC_BACKUP)
#define MAYBE_DontChangeBookmarkOrdering DontChangeBookmarkOrdering
#else
#define MAYBE_DontChangeBookmarkOrdering DISABLED_DontChangeBookmarkOrdering
#endif
IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
                       MAYBE_DontChangeBookmarkOrdering) {
  ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";

  const BookmarkNode* sub_folder = AddFolder(0, GetOtherNode(0), 0, "test");
  ASSERT_TRUE(AddURL(0, sub_folder, 0, "", GURL(kUrl1)));
  ASSERT_TRUE(AddURL(0, sub_folder, 1, "", GURL(kUrl2)));
  ASSERT_TRUE(AddURL(0, sub_folder, 2, "", GURL(kUrl3)));

  // Setup sync, wait for its completion, and make sure changes were synced.
  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0)));
  ASSERT_TRUE(ModelMatchesVerifier(0));

  // Made bookmark changes while sync is on.
  Remove(0, sub_folder, 0);
  Remove(0, sub_folder, 0);
  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0)));
  ASSERT_TRUE(ModelMatchesVerifier(0));

  // Let server to return rollback command on next sync request.
  ASSERT_TRUE(GetFakeServer()->TriggerError(sync_pb::SyncEnums::USER_ROLLBACK));

  // Make another change to trigger downloading of rollback command.
  Remove(0, sub_folder, 0);

  // Wait for rollback to finish and sync backend is completely shut down.
  SyncRollbackChecker rollback_checker(GetSyncService(0));
  ASSERT_TRUE(rollback_checker.Wait());
  SyncBackendStoppedChecker shutdown_checker(GetSyncService(0));
  ASSERT_TRUE(shutdown_checker.Wait());

  // Verify bookmarks are unchanged.
  ASSERT_EQ(3, sub_folder->child_count());
  ASSERT_EQ(GURL(kUrl1), sub_folder->GetChild(0)->url());
  ASSERT_EQ(GURL(kUrl2), sub_folder->GetChild(1)->url());
  ASSERT_EQ(GURL(kUrl3), sub_folder->GetChild(2)->url());
}