sync_errors_test.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/prefs/pref_member.h"
6#include "base/prefs/pref_service.h"
7#include "chrome/browser/sync/profile_sync_service.h"
8#include "chrome/browser/sync/profile_sync_service_harness.h"
9#include "chrome/browser/sync/test/integration/bookmarks_helper.h"
10#include "chrome/browser/sync/test/integration/passwords_helper.h"
11#include "chrome/browser/sync/test/integration/sync_test.h"
12#include "chrome/common/pref_names.h"
13#include "google_apis/gaia/google_service_auth_error.h"
14#include "sync/protocol/sync_protocol_error.h"
15
16using bookmarks_helper::AddFolder;
17using bookmarks_helper::SetTitle;
18
19class SyncErrorTest : public SyncTest {
20 public:
21  SyncErrorTest() : SyncTest(SINGLE_CLIENT) {}
22  virtual ~SyncErrorTest() {}
23
24 private:
25  DISALLOW_COPY_AND_ASSIGN(SyncErrorTest);
26};
27
28IN_PROC_BROWSER_TEST_F(SyncErrorTest, BirthdayErrorTest) {
29  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
30
31  const BookmarkNode* node1 = AddFolder(0, 0, L"title1");
32  SetTitle(0, node1, L"new_title1");
33  ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion("Offline state change."));
34  TriggerBirthdayError();
35
36  // Now make one more change so we will do another sync.
37  const BookmarkNode* node2 = AddFolder(0, 0, L"title2");
38  SetTitle(0, node2, L"new_title2");
39  ASSERT_TRUE(GetClient(0)->AwaitSyncDisabled("Birthday error."));
40}
41
42IN_PROC_BROWSER_TEST_F(SyncErrorTest, TransientErrorTest) {
43  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
44
45  const BookmarkNode* node1 = AddFolder(0, 0, L"title1");
46  SetTitle(0, node1, L"new_title1");
47  ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion("Offline state change."));
48  TriggerTransientError();
49
50  // Now make one more change so we will do another sync.
51  const BookmarkNode* node2 = AddFolder(0, 0, L"title2");
52  SetTitle(0, node2, L"new_title2");
53  ASSERT_TRUE(
54      GetClient(0)->AwaitExponentialBackoffVerification());
55}
56
57IN_PROC_BROWSER_TEST_F(SyncErrorTest, ActionableErrorTest) {
58  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
59
60  const BookmarkNode* node1 = AddFolder(0, 0, L"title1");
61  SetTitle(0, node1, L"new_title1");
62  ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion("Sync."));
63
64  syncer::SyncProtocolError protocol_error;
65  protocol_error.error_type = syncer::TRANSIENT_ERROR;
66  protocol_error.action = syncer::UPGRADE_CLIENT;
67  protocol_error.error_description = "Not My Fault";
68  protocol_error.url = "www.google.com";
69  TriggerSyncError(protocol_error, SyncTest::ERROR_FREQUENCY_ALWAYS);
70
71  // Now make one more change so we will do another sync.
72  const BookmarkNode* node2 = AddFolder(0, 0, L"title2");
73  SetTitle(0, node2, L"new_title2");
74  ASSERT_TRUE(
75      GetClient(0)->AwaitActionableError());
76  ProfileSyncService::Status status = GetClient(0)->GetStatus();
77  ASSERT_EQ(status.sync_protocol_error.error_type, protocol_error.error_type);
78  ASSERT_EQ(status.sync_protocol_error.action, protocol_error.action);
79  ASSERT_EQ(status.sync_protocol_error.url, protocol_error.url);
80  ASSERT_EQ(status.sync_protocol_error.error_description,
81      protocol_error.error_description);
82}
83
84IN_PROC_BROWSER_TEST_F(SyncErrorTest, ErrorWhileSettingUp) {
85  ASSERT_TRUE(SetupClients());
86
87  syncer::SyncProtocolError protocol_error;
88  protocol_error.error_type = syncer::TRANSIENT_ERROR;
89  protocol_error.error_description = "Not My Fault";
90  protocol_error.url = "www.google.com";
91
92  if (clients()[0]->AutoStartEnabled()) {
93    // In auto start enabled platforms like chrome os we should be
94    // able to set up even if the first sync while setting up fails.
95    // Trigger error on every 2 out of 3 requests.
96    TriggerSyncError(protocol_error, SyncTest::ERROR_FREQUENCY_TWO_THIRDS);
97    // Now setup sync and it should succeed.
98    ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
99  } else {
100    // In Non auto start enabled environments if the setup sync fails then
101    // the setup would fail. So setup sync normally.
102    ASSERT_TRUE(SetupSync()) << "Setup sync failed";
103    ASSERT_TRUE(clients()[0]->DisableSyncForDatatype(syncer::AUTOFILL));
104
105    // Trigger error on every 2 out of 3 requests.
106    TriggerSyncError(protocol_error, SyncTest::ERROR_FREQUENCY_TWO_THIRDS);
107
108    // Now enable a datatype, whose first 2 syncs would fail, but we should
109    // recover and setup succesfully on the third attempt.
110    ASSERT_TRUE(clients()[0]->EnableSyncForDatatype(syncer::AUTOFILL));
111  }
112}
113
114
115IN_PROC_BROWSER_TEST_F(SyncErrorTest,
116    BirthdayErrorUsingActionableErrorTest) {
117  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
118
119  const BookmarkNode* node1 = AddFolder(0, 0, L"title1");
120  SetTitle(0, node1, L"new_title1");
121  ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion("Sync."));
122
123  syncer::SyncProtocolError protocol_error;
124  protocol_error.error_type = syncer::NOT_MY_BIRTHDAY;
125  protocol_error.action = syncer::DISABLE_SYNC_ON_CLIENT;
126  protocol_error.error_description = "Not My Fault";
127  protocol_error.url = "www.google.com";
128  TriggerSyncError(protocol_error, SyncTest::ERROR_FREQUENCY_ALWAYS);
129
130  // Now make one more change so we will do another sync.
131  const BookmarkNode* node2 = AddFolder(0, 0, L"title2");
132  SetTitle(0, node2, L"new_title2");
133  ASSERT_TRUE(
134      GetClient(0)->AwaitSyncDisabled("Birthday Error."));
135  ProfileSyncService::Status status = GetClient(0)->GetStatus();
136  ASSERT_EQ(status.sync_protocol_error.error_type, protocol_error.error_type);
137  ASSERT_EQ(status.sync_protocol_error.action, protocol_error.action);
138  ASSERT_EQ(status.sync_protocol_error.url, protocol_error.url);
139  ASSERT_EQ(status.sync_protocol_error.error_description,
140      protocol_error.error_description);
141}
142
143// Trigger an auth error and make sure the sync client detects it when
144// trying to commit.
145IN_PROC_BROWSER_TEST_F(SyncErrorTest, AuthErrorTest) {
146  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
147
148  const BookmarkNode* node1 = AddFolder(0, 0, L"title1");
149  SetTitle(0, node1, L"new_title1");
150  ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion("Sync."));
151
152  TriggerAuthError();
153
154  const BookmarkNode* node2 = AddFolder(0, 0, L"title2");
155  SetTitle(0, node2, L"new_title2");
156  ASSERT_TRUE(GetClient(0)->AwaitExponentialBackoffVerification());
157  ASSERT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS,
158            GetClient(0)->service()->GetAuthError().state());
159}
160
161// Trigger an XMPP auth error, and make sure sync treats it like any
162// other auth error.
163IN_PROC_BROWSER_TEST_F(SyncErrorTest, XmppAuthErrorTest) {
164  ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
165
166  TriggerXmppAuthError();
167
168  ASSERT_FALSE(GetClient(0)->SetupSync());
169
170  ASSERT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS,
171            GetClient(0)->service()->GetAuthError().state());
172}
173
174// TODO(lipalani): Fix the typed_url dtc so this test case can pass.
175IN_PROC_BROWSER_TEST_F(SyncErrorTest, DISABLED_DisableDatatypeWhileRunning) {
176  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
177  syncer::ModelTypeSet synced_datatypes =
178      GetClient(0)->service()->GetPreferredDataTypes();
179  ASSERT_TRUE(synced_datatypes.Has(syncer::TYPED_URLS));
180  GetProfile(0)->GetPrefs()->SetBoolean(
181      prefs::kSavingBrowserHistoryDisabled, true);
182
183  synced_datatypes = GetClient(0)->service()->GetPreferredDataTypes();
184  ASSERT_FALSE(synced_datatypes.Has(syncer::TYPED_URLS));
185
186  const BookmarkNode* node1 = AddFolder(0, 0, L"title1");
187  SetTitle(0, node1, L"new_title1");
188  ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion("Sync."));
189  // TODO(lipalani)" Verify initial sync ended for typed url is false.
190}
191
192