Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(679)

Side by Side Diff: sync/internal_api/syncapi_unittest.cc

Issue 10795018: [Sync] Remove unneeded 'using syncer::' lines and 'syncer::' scopings (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix indent Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Unit tests for the SyncApi. Note that a lot of the underlying 5 // Unit tests for the SyncApi. Note that a lot of the underlying
6 // functionality is provided by the Syncable layer, which has its own 6 // functionality is provided by the Syncable layer, which has its own
7 // unit tests. We'll test SyncApi specific things in this harness. 7 // unit tests. We'll test SyncApi specific things in this harness.
8 8
9 #include <cstddef> 9 #include <cstddef>
10 #include <map> 10 #include <map>
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 EXPECT_TRUE(value.GetString(key, &int64_str)); 100 EXPECT_TRUE(value.GetString(key, &int64_str));
101 int64 val = 0; 101 int64 val = 0;
102 EXPECT_TRUE(base::StringToInt64(int64_str, &val)); 102 EXPECT_TRUE(base::StringToInt64(int64_str, &val));
103 EXPECT_EQ(expected_value, val); 103 EXPECT_EQ(expected_value, val);
104 } 104 }
105 105
106 void ExpectTimeValue(const base::Time& expected_value, 106 void ExpectTimeValue(const base::Time& expected_value,
107 const DictionaryValue& value, const std::string& key) { 107 const DictionaryValue& value, const std::string& key) {
108 std::string time_str; 108 std::string time_str;
109 EXPECT_TRUE(value.GetString(key, &time_str)); 109 EXPECT_TRUE(value.GetString(key, &time_str));
110 EXPECT_EQ(syncer::GetTimeDebugString(expected_value), time_str); 110 EXPECT_EQ(GetTimeDebugString(expected_value), time_str);
111 } 111 }
112 112
113 // Makes a non-folder child of the root node. Returns the id of the 113 // Makes a non-folder child of the root node. Returns the id of the
114 // newly-created node. 114 // newly-created node.
115 int64 MakeNode(UserShare* share, 115 int64 MakeNode(UserShare* share,
116 ModelType model_type, 116 ModelType model_type,
117 const std::string& client_tag) { 117 const std::string& client_tag) {
118 WriteTransaction trans(FROM_HERE, share); 118 WriteTransaction trans(FROM_HERE, share);
119 ReadNode root_node(&trans); 119 ReadNode root_node(&trans);
120 root_node.InitByRootLookup(); 120 root_node.InitByRootLookup();
121 WriteNode node(&trans); 121 WriteNode node(&trans);
122 syncer::WriteNode::InitUniqueByCreationResult result = 122 WriteNode::InitUniqueByCreationResult result =
123 node.InitUniqueByCreation(model_type, root_node, client_tag); 123 node.InitUniqueByCreation(model_type, root_node, client_tag);
124 EXPECT_EQ(syncer::WriteNode::INIT_SUCCESS, result); 124 EXPECT_EQ(WriteNode::INIT_SUCCESS, result);
125 node.SetIsFolder(false); 125 node.SetIsFolder(false);
126 return node.GetId(); 126 return node.GetId();
127 } 127 }
128 128
129 // Makes a non-folder child of a non-root node. Returns the id of the 129 // Makes a non-folder child of a non-root node. Returns the id of the
130 // newly-created node. 130 // newly-created node.
131 int64 MakeNodeWithParent(UserShare* share, 131 int64 MakeNodeWithParent(UserShare* share,
132 ModelType model_type, 132 ModelType model_type,
133 const std::string& client_tag, 133 const std::string& client_tag,
134 int64 parent_id) { 134 int64 parent_id) {
135 WriteTransaction trans(FROM_HERE, share); 135 WriteTransaction trans(FROM_HERE, share);
136 ReadNode parent_node(&trans); 136 ReadNode parent_node(&trans);
137 EXPECT_EQ(BaseNode::INIT_OK, parent_node.InitByIdLookup(parent_id)); 137 EXPECT_EQ(BaseNode::INIT_OK, parent_node.InitByIdLookup(parent_id));
138 WriteNode node(&trans); 138 WriteNode node(&trans);
139 syncer::WriteNode::InitUniqueByCreationResult result = 139 WriteNode::InitUniqueByCreationResult result =
140 node.InitUniqueByCreation(model_type, parent_node, client_tag); 140 node.InitUniqueByCreation(model_type, parent_node, client_tag);
141 EXPECT_EQ(syncer::WriteNode::INIT_SUCCESS, result); 141 EXPECT_EQ(WriteNode::INIT_SUCCESS, result);
142 node.SetIsFolder(false); 142 node.SetIsFolder(false);
143 return node.GetId(); 143 return node.GetId();
144 } 144 }
145 145
146 // Makes a folder child of a non-root node. Returns the id of the 146 // Makes a folder child of a non-root node. Returns the id of the
147 // newly-created node. 147 // newly-created node.
148 int64 MakeFolderWithParent(UserShare* share, 148 int64 MakeFolderWithParent(UserShare* share,
149 ModelType model_type, 149 ModelType model_type,
150 int64 parent_id, 150 int64 parent_id,
151 BaseNode* predecessor) { 151 BaseNode* predecessor) {
152 WriteTransaction trans(FROM_HERE, share); 152 WriteTransaction trans(FROM_HERE, share);
153 ReadNode parent_node(&trans); 153 ReadNode parent_node(&trans);
154 EXPECT_EQ(BaseNode::INIT_OK, parent_node.InitByIdLookup(parent_id)); 154 EXPECT_EQ(BaseNode::INIT_OK, parent_node.InitByIdLookup(parent_id));
155 WriteNode node(&trans); 155 WriteNode node(&trans);
156 EXPECT_TRUE(node.InitByCreation(model_type, parent_node, predecessor)); 156 EXPECT_TRUE(node.InitByCreation(model_type, parent_node, predecessor));
157 node.SetIsFolder(true); 157 node.SetIsFolder(true);
158 return node.GetId(); 158 return node.GetId();
159 } 159 }
160 160
161 // Creates the "synced" root node for a particular datatype. We use the syncable 161 // Creates the "synced" root node for a particular datatype. We use the syncable
162 // methods here so that the syncer treats these nodes as if they were already 162 // methods here so that the syncer treats these nodes as if they were already
163 // received from the server. 163 // received from the server.
164 int64 MakeServerNodeForType(UserShare* share, 164 int64 MakeServerNodeForType(UserShare* share,
165 ModelType model_type) { 165 ModelType model_type) {
166 sync_pb::EntitySpecifics specifics; 166 sync_pb::EntitySpecifics specifics;
167 syncer::AddDefaultFieldValue(model_type, &specifics); 167 AddDefaultFieldValue(model_type, &specifics);
168 syncable::WriteTransaction trans( 168 syncable::WriteTransaction trans(
169 FROM_HERE, syncable::UNITTEST, share->directory.get()); 169 FROM_HERE, syncable::UNITTEST, share->directory.get());
170 // Attempt to lookup by nigori tag. 170 // Attempt to lookup by nigori tag.
171 std::string type_tag = syncer::ModelTypeToRootTag(model_type); 171 std::string type_tag = ModelTypeToRootTag(model_type);
172 syncable::Id node_id = syncable::Id::CreateFromServerId(type_tag); 172 syncable::Id node_id = syncable::Id::CreateFromServerId(type_tag);
173 syncable::MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, 173 syncable::MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM,
174 node_id); 174 node_id);
175 EXPECT_TRUE(entry.good()); 175 EXPECT_TRUE(entry.good());
176 entry.Put(syncable::BASE_VERSION, 1); 176 entry.Put(syncable::BASE_VERSION, 1);
177 entry.Put(syncable::SERVER_VERSION, 1); 177 entry.Put(syncable::SERVER_VERSION, 1);
178 entry.Put(syncable::IS_UNAPPLIED_UPDATE, false); 178 entry.Put(syncable::IS_UNAPPLIED_UPDATE, false);
179 entry.Put(syncable::SERVER_PARENT_ID, syncable::GetNullId()); 179 entry.Put(syncable::SERVER_PARENT_ID, syncable::GetNullId());
180 entry.Put(syncable::SERVER_IS_DIR, true); 180 entry.Put(syncable::SERVER_IS_DIR, true);
181 entry.Put(syncable::IS_DIR, true); 181 entry.Put(syncable::IS_DIR, true);
182 entry.Put(syncable::SERVER_SPECIFICS, specifics); 182 entry.Put(syncable::SERVER_SPECIFICS, specifics);
183 entry.Put(syncable::UNIQUE_SERVER_TAG, type_tag); 183 entry.Put(syncable::UNIQUE_SERVER_TAG, type_tag);
184 entry.Put(syncable::NON_UNIQUE_NAME, type_tag); 184 entry.Put(syncable::NON_UNIQUE_NAME, type_tag);
185 entry.Put(syncable::IS_DEL, false); 185 entry.Put(syncable::IS_DEL, false);
186 entry.Put(syncable::SPECIFICS, specifics); 186 entry.Put(syncable::SPECIFICS, specifics);
187 return entry.Get(syncable::META_HANDLE); 187 return entry.Get(syncable::META_HANDLE);
188 } 188 }
189 189
190 // Simulates creating a "synced" node as a child of the root datatype node. 190 // Simulates creating a "synced" node as a child of the root datatype node.
191 int64 MakeServerNode(UserShare* share, ModelType model_type, 191 int64 MakeServerNode(UserShare* share, ModelType model_type,
192 const std::string& client_tag, 192 const std::string& client_tag,
193 const std::string& hashed_tag, 193 const std::string& hashed_tag,
194 const sync_pb::EntitySpecifics& specifics) { 194 const sync_pb::EntitySpecifics& specifics) {
195 syncable::WriteTransaction trans( 195 syncable::WriteTransaction trans(
196 FROM_HERE, syncable::UNITTEST, share->directory.get()); 196 FROM_HERE, syncable::UNITTEST, share->directory.get());
197 syncable::Entry root_entry(&trans, syncable::GET_BY_SERVER_TAG, 197 syncable::Entry root_entry(&trans, syncable::GET_BY_SERVER_TAG,
198 syncer::ModelTypeToRootTag(model_type)); 198 ModelTypeToRootTag(model_type));
199 EXPECT_TRUE(root_entry.good()); 199 EXPECT_TRUE(root_entry.good());
200 syncable::Id root_id = root_entry.Get(syncable::ID); 200 syncable::Id root_id = root_entry.Get(syncable::ID);
201 syncable::Id node_id = syncable::Id::CreateFromServerId(client_tag); 201 syncable::Id node_id = syncable::Id::CreateFromServerId(client_tag);
202 syncable::MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, 202 syncable::MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM,
203 node_id); 203 node_id);
204 EXPECT_TRUE(entry.good()); 204 EXPECT_TRUE(entry.good());
205 entry.Put(syncable::BASE_VERSION, 1); 205 entry.Put(syncable::BASE_VERSION, 1);
206 entry.Put(syncable::SERVER_VERSION, 1); 206 entry.Put(syncable::SERVER_VERSION, 1);
207 entry.Put(syncable::IS_UNAPPLIED_UPDATE, false); 207 entry.Put(syncable::IS_UNAPPLIED_UPDATE, false);
208 entry.Put(syncable::SERVER_PARENT_ID, root_id); 208 entry.Put(syncable::SERVER_PARENT_ID, root_id);
(...skipping 15 matching lines...) Expand all
224 virtual void SetUp() { 224 virtual void SetUp() {
225 test_user_share_.SetUp(); 225 test_user_share_.SetUp();
226 } 226 }
227 227
228 virtual void TearDown() { 228 virtual void TearDown() {
229 test_user_share_.TearDown(); 229 test_user_share_.TearDown();
230 } 230 }
231 231
232 protected: 232 protected:
233 MessageLoop message_loop_; 233 MessageLoop message_loop_;
234 syncer::TestUserShare test_user_share_; 234 TestUserShare test_user_share_;
235 }; 235 };
236 236
237 TEST_F(SyncApiTest, SanityCheckTest) { 237 TEST_F(SyncApiTest, SanityCheckTest) {
238 { 238 {
239 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); 239 ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
240 EXPECT_TRUE(trans.GetWrappedTrans() != NULL); 240 EXPECT_TRUE(trans.GetWrappedTrans() != NULL);
241 } 241 }
242 { 242 {
243 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); 243 WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
244 EXPECT_TRUE(trans.GetWrappedTrans() != NULL); 244 EXPECT_TRUE(trans.GetWrappedTrans() != NULL);
245 } 245 }
246 { 246 {
247 // No entries but root should exist 247 // No entries but root should exist
248 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); 248 ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
249 ReadNode node(&trans); 249 ReadNode node(&trans);
250 // Metahandle 1 can be root, sanity check 2 250 // Metahandle 1 can be root, sanity check 2
251 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_NOT_GOOD, node.InitByIdLookup(2)); 251 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_NOT_GOOD, node.InitByIdLookup(2));
252 } 252 }
253 } 253 }
254 254
255 TEST_F(SyncApiTest, BasicTagWrite) { 255 TEST_F(SyncApiTest, BasicTagWrite) {
256 { 256 {
257 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); 257 ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
258 ReadNode root_node(&trans); 258 ReadNode root_node(&trans);
259 root_node.InitByRootLookup(); 259 root_node.InitByRootLookup();
260 EXPECT_EQ(root_node.GetFirstChildId(), 0); 260 EXPECT_EQ(root_node.GetFirstChildId(), 0);
261 } 261 }
262 262
263 ignore_result(MakeNode(test_user_share_.user_share(), 263 ignore_result(MakeNode(test_user_share_.user_share(),
264 syncer::BOOKMARKS, "testtag")); 264 BOOKMARKS, "testtag"));
265 265
266 { 266 {
267 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); 267 ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
268 ReadNode node(&trans); 268 ReadNode node(&trans);
269 EXPECT_EQ(BaseNode::INIT_OK, 269 EXPECT_EQ(BaseNode::INIT_OK,
270 node.InitByClientTagLookup(syncer::BOOKMARKS, "testtag")); 270 node.InitByClientTagLookup(BOOKMARKS, "testtag"));
271 271
272 ReadNode root_node(&trans); 272 ReadNode root_node(&trans);
273 root_node.InitByRootLookup(); 273 root_node.InitByRootLookup();
274 EXPECT_NE(node.GetId(), 0); 274 EXPECT_NE(node.GetId(), 0);
275 EXPECT_EQ(node.GetId(), root_node.GetFirstChildId()); 275 EXPECT_EQ(node.GetId(), root_node.GetFirstChildId());
276 } 276 }
277 } 277 }
278 278
279 TEST_F(SyncApiTest, GenerateSyncableHash) { 279 TEST_F(SyncApiTest, GenerateSyncableHash) {
280 EXPECT_EQ("OyaXV5mEzrPS4wbogmtKvRfekAI=", 280 EXPECT_EQ("OyaXV5mEzrPS4wbogmtKvRfekAI=",
281 BaseNode::GenerateSyncableHash(syncer::BOOKMARKS, "tag1")); 281 BaseNode::GenerateSyncableHash(BOOKMARKS, "tag1"));
282 EXPECT_EQ("iNFQtRFQb+IZcn1kKUJEZDDkLs4=", 282 EXPECT_EQ("iNFQtRFQb+IZcn1kKUJEZDDkLs4=",
283 BaseNode::GenerateSyncableHash(syncer::PREFERENCES, "tag1")); 283 BaseNode::GenerateSyncableHash(PREFERENCES, "tag1"));
284 EXPECT_EQ("gO1cPZQXaM73sHOvSA+tKCKFs58=", 284 EXPECT_EQ("gO1cPZQXaM73sHOvSA+tKCKFs58=",
285 BaseNode::GenerateSyncableHash(syncer::AUTOFILL, "tag1")); 285 BaseNode::GenerateSyncableHash(AUTOFILL, "tag1"));
286 286
287 EXPECT_EQ("A0eYIHXM1/jVwKDDp12Up20IkKY=", 287 EXPECT_EQ("A0eYIHXM1/jVwKDDp12Up20IkKY=",
288 BaseNode::GenerateSyncableHash(syncer::BOOKMARKS, "tag2")); 288 BaseNode::GenerateSyncableHash(BOOKMARKS, "tag2"));
289 EXPECT_EQ("XYxkF7bhS4eItStFgiOIAU23swI=", 289 EXPECT_EQ("XYxkF7bhS4eItStFgiOIAU23swI=",
290 BaseNode::GenerateSyncableHash(syncer::PREFERENCES, "tag2")); 290 BaseNode::GenerateSyncableHash(PREFERENCES, "tag2"));
291 EXPECT_EQ("GFiWzo5NGhjLlN+OyCfhy28DJTQ=", 291 EXPECT_EQ("GFiWzo5NGhjLlN+OyCfhy28DJTQ=",
292 BaseNode::GenerateSyncableHash(syncer::AUTOFILL, "tag2")); 292 BaseNode::GenerateSyncableHash(AUTOFILL, "tag2"));
293 } 293 }
294 294
295 TEST_F(SyncApiTest, ModelTypesSiloed) { 295 TEST_F(SyncApiTest, ModelTypesSiloed) {
296 { 296 {
297 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); 297 WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
298 ReadNode root_node(&trans); 298 ReadNode root_node(&trans);
299 root_node.InitByRootLookup(); 299 root_node.InitByRootLookup();
300 EXPECT_EQ(root_node.GetFirstChildId(), 0); 300 EXPECT_EQ(root_node.GetFirstChildId(), 0);
301 } 301 }
302 302
303 ignore_result(MakeNode(test_user_share_.user_share(), 303 ignore_result(MakeNode(test_user_share_.user_share(),
304 syncer::BOOKMARKS, "collideme")); 304 BOOKMARKS, "collideme"));
305 ignore_result(MakeNode(test_user_share_.user_share(), 305 ignore_result(MakeNode(test_user_share_.user_share(),
306 syncer::PREFERENCES, "collideme")); 306 PREFERENCES, "collideme"));
307 ignore_result(MakeNode(test_user_share_.user_share(), 307 ignore_result(MakeNode(test_user_share_.user_share(),
308 syncer::AUTOFILL, "collideme")); 308 AUTOFILL, "collideme"));
309 309
310 { 310 {
311 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); 311 ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
312 312
313 ReadNode bookmarknode(&trans); 313 ReadNode bookmarknode(&trans);
314 EXPECT_EQ(BaseNode::INIT_OK, 314 EXPECT_EQ(BaseNode::INIT_OK,
315 bookmarknode.InitByClientTagLookup(syncer::BOOKMARKS, 315 bookmarknode.InitByClientTagLookup(BOOKMARKS,
316 "collideme")); 316 "collideme"));
317 317
318 ReadNode prefnode(&trans); 318 ReadNode prefnode(&trans);
319 EXPECT_EQ(BaseNode::INIT_OK, 319 EXPECT_EQ(BaseNode::INIT_OK,
320 prefnode.InitByClientTagLookup(syncer::PREFERENCES, 320 prefnode.InitByClientTagLookup(PREFERENCES,
321 "collideme")); 321 "collideme"));
322 322
323 ReadNode autofillnode(&trans); 323 ReadNode autofillnode(&trans);
324 EXPECT_EQ(BaseNode::INIT_OK, 324 EXPECT_EQ(BaseNode::INIT_OK,
325 autofillnode.InitByClientTagLookup(syncer::AUTOFILL, 325 autofillnode.InitByClientTagLookup(AUTOFILL,
326 "collideme")); 326 "collideme"));
327 327
328 EXPECT_NE(bookmarknode.GetId(), prefnode.GetId()); 328 EXPECT_NE(bookmarknode.GetId(), prefnode.GetId());
329 EXPECT_NE(autofillnode.GetId(), prefnode.GetId()); 329 EXPECT_NE(autofillnode.GetId(), prefnode.GetId());
330 EXPECT_NE(bookmarknode.GetId(), autofillnode.GetId()); 330 EXPECT_NE(bookmarknode.GetId(), autofillnode.GetId());
331 } 331 }
332 } 332 }
333 333
334 TEST_F(SyncApiTest, ReadMissingTagsFails) { 334 TEST_F(SyncApiTest, ReadMissingTagsFails) {
335 { 335 {
336 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); 336 ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
337 ReadNode node(&trans); 337 ReadNode node(&trans);
338 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_NOT_GOOD, 338 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_NOT_GOOD,
339 node.InitByClientTagLookup(syncer::BOOKMARKS, 339 node.InitByClientTagLookup(BOOKMARKS,
340 "testtag")); 340 "testtag"));
341 } 341 }
342 { 342 {
343 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); 343 WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
344 WriteNode node(&trans); 344 WriteNode node(&trans);
345 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_NOT_GOOD, 345 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_NOT_GOOD,
346 node.InitByClientTagLookup(syncer::BOOKMARKS, 346 node.InitByClientTagLookup(BOOKMARKS,
347 "testtag")); 347 "testtag"));
348 } 348 }
349 } 349 }
350 350
351 // TODO(chron): Hook this all up to the server and write full integration tests 351 // TODO(chron): Hook this all up to the server and write full integration tests
352 // for update->undelete behavior. 352 // for update->undelete behavior.
353 TEST_F(SyncApiTest, TestDeleteBehavior) { 353 TEST_F(SyncApiTest, TestDeleteBehavior) {
354 int64 node_id; 354 int64 node_id;
355 int64 folder_id; 355 int64 folder_id;
356 std::string test_title("test1"); 356 std::string test_title("test1");
357 357
358 { 358 {
359 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); 359 WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
360 ReadNode root_node(&trans); 360 ReadNode root_node(&trans);
361 root_node.InitByRootLookup(); 361 root_node.InitByRootLookup();
362 362
363 // we'll use this spare folder later 363 // we'll use this spare folder later
364 WriteNode folder_node(&trans); 364 WriteNode folder_node(&trans);
365 EXPECT_TRUE(folder_node.InitByCreation(syncer::BOOKMARKS, 365 EXPECT_TRUE(folder_node.InitByCreation(BOOKMARKS,
366 root_node, NULL)); 366 root_node, NULL));
367 folder_id = folder_node.GetId(); 367 folder_id = folder_node.GetId();
368 368
369 WriteNode wnode(&trans); 369 WriteNode wnode(&trans);
370 syncer::WriteNode::InitUniqueByCreationResult result = 370 WriteNode::InitUniqueByCreationResult result =
371 wnode.InitUniqueByCreation(syncer::BOOKMARKS, root_node, "testtag"); 371 wnode.InitUniqueByCreation(BOOKMARKS, root_node, "testtag");
372 EXPECT_EQ(syncer::WriteNode::INIT_SUCCESS, result); 372 EXPECT_EQ(WriteNode::INIT_SUCCESS, result);
373 wnode.SetIsFolder(false); 373 wnode.SetIsFolder(false);
374 wnode.SetTitle(UTF8ToWide(test_title)); 374 wnode.SetTitle(UTF8ToWide(test_title));
375 375
376 node_id = wnode.GetId(); 376 node_id = wnode.GetId();
377 } 377 }
378 378
379 // Ensure we can delete something with a tag. 379 // Ensure we can delete something with a tag.
380 { 380 {
381 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); 381 WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
382 WriteNode wnode(&trans); 382 WriteNode wnode(&trans);
383 EXPECT_EQ(BaseNode::INIT_OK, 383 EXPECT_EQ(BaseNode::INIT_OK,
384 wnode.InitByClientTagLookup(syncer::BOOKMARKS, 384 wnode.InitByClientTagLookup(BOOKMARKS,
385 "testtag")); 385 "testtag"));
386 EXPECT_FALSE(wnode.GetIsFolder()); 386 EXPECT_FALSE(wnode.GetIsFolder());
387 EXPECT_EQ(wnode.GetTitle(), test_title); 387 EXPECT_EQ(wnode.GetTitle(), test_title);
388 388
389 wnode.Remove(); 389 wnode.Remove();
390 } 390 }
391 391
392 // Lookup of a node which was deleted should return failure, 392 // Lookup of a node which was deleted should return failure,
393 // but have found some data about the node. 393 // but have found some data about the node.
394 { 394 {
395 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); 395 ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
396 ReadNode node(&trans); 396 ReadNode node(&trans);
397 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_IS_DEL, 397 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_IS_DEL,
398 node.InitByClientTagLookup(syncer::BOOKMARKS, 398 node.InitByClientTagLookup(BOOKMARKS,
399 "testtag")); 399 "testtag"));
400 // Note that for proper function of this API this doesn't need to be 400 // Note that for proper function of this API this doesn't need to be
401 // filled, we're checking just to make sure the DB worked in this test. 401 // filled, we're checking just to make sure the DB worked in this test.
402 EXPECT_EQ(node.GetTitle(), test_title); 402 EXPECT_EQ(node.GetTitle(), test_title);
403 } 403 }
404 404
405 { 405 {
406 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); 406 WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
407 ReadNode folder_node(&trans); 407 ReadNode folder_node(&trans);
408 EXPECT_EQ(BaseNode::INIT_OK, folder_node.InitByIdLookup(folder_id)); 408 EXPECT_EQ(BaseNode::INIT_OK, folder_node.InitByIdLookup(folder_id));
409 409
410 WriteNode wnode(&trans); 410 WriteNode wnode(&trans);
411 // This will undelete the tag. 411 // This will undelete the tag.
412 syncer::WriteNode::InitUniqueByCreationResult result = 412 WriteNode::InitUniqueByCreationResult result =
413 wnode.InitUniqueByCreation(syncer::BOOKMARKS, folder_node, "testtag"); 413 wnode.InitUniqueByCreation(BOOKMARKS, folder_node, "testtag");
414 EXPECT_EQ(syncer::WriteNode::INIT_SUCCESS, result); 414 EXPECT_EQ(WriteNode::INIT_SUCCESS, result);
415 EXPECT_EQ(wnode.GetIsFolder(), false); 415 EXPECT_EQ(wnode.GetIsFolder(), false);
416 EXPECT_EQ(wnode.GetParentId(), folder_node.GetId()); 416 EXPECT_EQ(wnode.GetParentId(), folder_node.GetId());
417 EXPECT_EQ(wnode.GetId(), node_id); 417 EXPECT_EQ(wnode.GetId(), node_id);
418 EXPECT_NE(wnode.GetTitle(), test_title); // Title should be cleared 418 EXPECT_NE(wnode.GetTitle(), test_title); // Title should be cleared
419 wnode.SetTitle(UTF8ToWide(test_title)); 419 wnode.SetTitle(UTF8ToWide(test_title));
420 } 420 }
421 421
422 // Now look up should work. 422 // Now look up should work.
423 { 423 {
424 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); 424 ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
425 ReadNode node(&trans); 425 ReadNode node(&trans);
426 EXPECT_EQ(BaseNode::INIT_OK, 426 EXPECT_EQ(BaseNode::INIT_OK,
427 node.InitByClientTagLookup(syncer::BOOKMARKS, 427 node.InitByClientTagLookup(BOOKMARKS,
428 "testtag")); 428 "testtag"));
429 EXPECT_EQ(node.GetTitle(), test_title); 429 EXPECT_EQ(node.GetTitle(), test_title);
430 EXPECT_EQ(node.GetModelType(), syncer::BOOKMARKS); 430 EXPECT_EQ(node.GetModelType(), BOOKMARKS);
431 } 431 }
432 } 432 }
433 433
434 TEST_F(SyncApiTest, WriteAndReadPassword) { 434 TEST_F(SyncApiTest, WriteAndReadPassword) {
435 KeyParams params = {"localhost", "username", "passphrase"}; 435 KeyParams params = {"localhost", "username", "passphrase"};
436 { 436 {
437 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); 437 ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
438 trans.GetCryptographer()->AddKey(params); 438 trans.GetCryptographer()->AddKey(params);
439 } 439 }
440 { 440 {
441 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); 441 WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
442 ReadNode root_node(&trans); 442 ReadNode root_node(&trans);
443 root_node.InitByRootLookup(); 443 root_node.InitByRootLookup();
444 444
445 WriteNode password_node(&trans); 445 WriteNode password_node(&trans);
446 syncer::WriteNode::InitUniqueByCreationResult result = 446 WriteNode::InitUniqueByCreationResult result =
447 password_node.InitUniqueByCreation(syncer::PASSWORDS, 447 password_node.InitUniqueByCreation(PASSWORDS,
448 root_node, "foo"); 448 root_node, "foo");
449 EXPECT_EQ(syncer::WriteNode::INIT_SUCCESS, result); 449 EXPECT_EQ(WriteNode::INIT_SUCCESS, result);
450 sync_pb::PasswordSpecificsData data; 450 sync_pb::PasswordSpecificsData data;
451 data.set_password_value("secret"); 451 data.set_password_value("secret");
452 password_node.SetPasswordSpecifics(data); 452 password_node.SetPasswordSpecifics(data);
453 } 453 }
454 { 454 {
455 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); 455 ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
456 ReadNode root_node(&trans); 456 ReadNode root_node(&trans);
457 root_node.InitByRootLookup(); 457 root_node.InitByRootLookup();
458 458
459 ReadNode password_node(&trans); 459 ReadNode password_node(&trans);
460 EXPECT_EQ(BaseNode::INIT_OK, 460 EXPECT_EQ(BaseNode::INIT_OK,
461 password_node.InitByClientTagLookup(syncer::PASSWORDS, 461 password_node.InitByClientTagLookup(PASSWORDS, "foo"));
462 "foo"));
463 const sync_pb::PasswordSpecificsData& data = 462 const sync_pb::PasswordSpecificsData& data =
464 password_node.GetPasswordSpecifics(); 463 password_node.GetPasswordSpecifics();
465 EXPECT_EQ("secret", data.password_value()); 464 EXPECT_EQ("secret", data.password_value());
466 } 465 }
467 } 466 }
468 467
469 TEST_F(SyncApiTest, WriteEncryptedTitle) { 468 TEST_F(SyncApiTest, WriteEncryptedTitle) {
470 KeyParams params = {"localhost", "username", "passphrase"}; 469 KeyParams params = {"localhost", "username", "passphrase"};
471 { 470 {
472 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); 471 ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
473 trans.GetCryptographer()->AddKey(params); 472 trans.GetCryptographer()->AddKey(params);
474 trans.GetCryptographer()->set_encrypt_everything(); 473 trans.GetCryptographer()->set_encrypt_everything();
475 } 474 }
476 { 475 {
477 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); 476 WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
478 ReadNode root_node(&trans); 477 ReadNode root_node(&trans);
479 root_node.InitByRootLookup(); 478 root_node.InitByRootLookup();
480 479
481 WriteNode bookmark_node(&trans); 480 WriteNode bookmark_node(&trans);
482 syncer::WriteNode::InitUniqueByCreationResult result = 481 WriteNode::InitUniqueByCreationResult result =
483 bookmark_node.InitUniqueByCreation(syncer::BOOKMARKS, 482 bookmark_node.InitUniqueByCreation(BOOKMARKS,
484 root_node, "foo"); 483 root_node, "foo");
485 EXPECT_EQ(syncer::WriteNode::INIT_SUCCESS, result); 484 EXPECT_EQ(WriteNode::INIT_SUCCESS, result);
486 bookmark_node.SetTitle(UTF8ToWide("foo")); 485 bookmark_node.SetTitle(UTF8ToWide("foo"));
487 486
488 WriteNode pref_node(&trans); 487 WriteNode pref_node(&trans);
489 result = 488 result =
490 pref_node.InitUniqueByCreation(syncer::PREFERENCES, root_node, "bar"); 489 pref_node.InitUniqueByCreation(PREFERENCES, root_node, "bar");
491 EXPECT_EQ(syncer::WriteNode::INIT_SUCCESS, result); 490 EXPECT_EQ(WriteNode::INIT_SUCCESS, result);
492 pref_node.SetTitle(UTF8ToWide("bar")); 491 pref_node.SetTitle(UTF8ToWide("bar"));
493 } 492 }
494 { 493 {
495 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); 494 ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
496 ReadNode root_node(&trans); 495 ReadNode root_node(&trans);
497 root_node.InitByRootLookup(); 496 root_node.InitByRootLookup();
498 497
499 ReadNode bookmark_node(&trans); 498 ReadNode bookmark_node(&trans);
500 EXPECT_EQ(BaseNode::INIT_OK, 499 EXPECT_EQ(BaseNode::INIT_OK,
501 bookmark_node.InitByClientTagLookup(syncer::BOOKMARKS, 500 bookmark_node.InitByClientTagLookup(BOOKMARKS,
502 "foo")); 501 "foo"));
503 EXPECT_EQ("foo", bookmark_node.GetTitle()); 502 EXPECT_EQ("foo", bookmark_node.GetTitle());
504 EXPECT_EQ(kEncryptedString, 503 EXPECT_EQ(kEncryptedString,
505 bookmark_node.GetEntry()->Get(syncable::NON_UNIQUE_NAME)); 504 bookmark_node.GetEntry()->Get(syncable::NON_UNIQUE_NAME));
506 505
507 ReadNode pref_node(&trans); 506 ReadNode pref_node(&trans);
508 EXPECT_EQ(BaseNode::INIT_OK, 507 EXPECT_EQ(BaseNode::INIT_OK,
509 pref_node.InitByClientTagLookup(syncer::PREFERENCES, 508 pref_node.InitByClientTagLookup(PREFERENCES,
510 "bar")); 509 "bar"));
511 EXPECT_EQ(kEncryptedString, pref_node.GetTitle()); 510 EXPECT_EQ(kEncryptedString, pref_node.GetTitle());
512 } 511 }
513 } 512 }
514 513
515 TEST_F(SyncApiTest, BaseNodeSetSpecifics) { 514 TEST_F(SyncApiTest, BaseNodeSetSpecifics) {
516 int64 child_id = MakeNode(test_user_share_.user_share(), 515 int64 child_id = MakeNode(test_user_share_.user_share(),
517 syncer::BOOKMARKS, "testtag"); 516 BOOKMARKS, "testtag");
518 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); 517 WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
519 WriteNode node(&trans); 518 WriteNode node(&trans);
520 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(child_id)); 519 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(child_id));
521 520
522 sync_pb::EntitySpecifics entity_specifics; 521 sync_pb::EntitySpecifics entity_specifics;
523 entity_specifics.mutable_bookmark()->set_url("http://www.google.com"); 522 entity_specifics.mutable_bookmark()->set_url("http://www.google.com");
524 523
525 EXPECT_NE(entity_specifics.SerializeAsString(), 524 EXPECT_NE(entity_specifics.SerializeAsString(),
526 node.GetEntitySpecifics().SerializeAsString()); 525 node.GetEntitySpecifics().SerializeAsString());
527 node.SetEntitySpecifics(entity_specifics); 526 node.SetEntitySpecifics(entity_specifics);
528 EXPECT_EQ(entity_specifics.SerializeAsString(), 527 EXPECT_EQ(entity_specifics.SerializeAsString(),
529 node.GetEntitySpecifics().SerializeAsString()); 528 node.GetEntitySpecifics().SerializeAsString());
530 } 529 }
531 530
532 TEST_F(SyncApiTest, BaseNodeSetSpecificsPreservesUnknownFields) { 531 TEST_F(SyncApiTest, BaseNodeSetSpecificsPreservesUnknownFields) {
533 int64 child_id = MakeNode(test_user_share_.user_share(), 532 int64 child_id = MakeNode(test_user_share_.user_share(),
534 syncer::BOOKMARKS, "testtag"); 533 BOOKMARKS, "testtag");
535 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); 534 WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
536 WriteNode node(&trans); 535 WriteNode node(&trans);
537 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(child_id)); 536 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(child_id));
538 EXPECT_TRUE(node.GetEntitySpecifics().unknown_fields().empty()); 537 EXPECT_TRUE(node.GetEntitySpecifics().unknown_fields().empty());
539 538
540 sync_pb::EntitySpecifics entity_specifics; 539 sync_pb::EntitySpecifics entity_specifics;
541 entity_specifics.mutable_bookmark()->set_url("http://www.google.com"); 540 entity_specifics.mutable_bookmark()->set_url("http://www.google.com");
542 entity_specifics.mutable_unknown_fields()->AddFixed32(5, 100); 541 entity_specifics.mutable_unknown_fields()->AddFixed32(5, 100);
543 node.SetEntitySpecifics(entity_specifics); 542 node.SetEntitySpecifics(entity_specifics);
544 EXPECT_FALSE(node.GetEntitySpecifics().unknown_fields().empty()); 543 EXPECT_FALSE(node.GetEntitySpecifics().unknown_fields().empty());
(...skipping 11 matching lines...) Expand all
556 { 555 {
557 bool is_folder = false; 556 bool is_folder = false;
558 EXPECT_TRUE(value.GetBoolean("isFolder", &is_folder)); 557 EXPECT_TRUE(value.GetBoolean("isFolder", &is_folder));
559 EXPECT_EQ(node.GetIsFolder(), is_folder); 558 EXPECT_EQ(node.GetIsFolder(), is_folder);
560 } 559 }
561 ExpectDictStringValue(node.GetTitle(), value, "title"); 560 ExpectDictStringValue(node.GetTitle(), value, "title");
562 { 561 {
563 ModelType expected_model_type = node.GetModelType(); 562 ModelType expected_model_type = node.GetModelType();
564 std::string type_str; 563 std::string type_str;
565 EXPECT_TRUE(value.GetString("type", &type_str)); 564 EXPECT_TRUE(value.GetString("type", &type_str));
566 if (expected_model_type >= syncer::FIRST_REAL_MODEL_TYPE) { 565 if (expected_model_type >= FIRST_REAL_MODEL_TYPE) {
567 ModelType model_type = syncer::ModelTypeFromString(type_str); 566 ModelType model_type = ModelTypeFromString(type_str);
568 EXPECT_EQ(expected_model_type, model_type); 567 EXPECT_EQ(expected_model_type, model_type);
569 } else if (expected_model_type == syncer::TOP_LEVEL_FOLDER) { 568 } else if (expected_model_type == TOP_LEVEL_FOLDER) {
570 EXPECT_EQ("Top-level folder", type_str); 569 EXPECT_EQ("Top-level folder", type_str);
571 } else if (expected_model_type == syncer::UNSPECIFIED) { 570 } else if (expected_model_type == UNSPECIFIED) {
572 EXPECT_EQ("Unspecified", type_str); 571 EXPECT_EQ("Unspecified", type_str);
573 } else { 572 } else {
574 ADD_FAILURE(); 573 ADD_FAILURE();
575 } 574 }
576 } 575 }
577 if (is_detailed) { 576 if (is_detailed) {
578 ExpectInt64Value(node.GetParentId(), value, "parentId"); 577 ExpectInt64Value(node.GetParentId(), value, "parentId");
579 ExpectTimeValue(node.GetModificationTime(), value, "modificationTime"); 578 ExpectTimeValue(node.GetModificationTime(), value, "modificationTime");
580 ExpectInt64Value(node.GetExternalId(), value, "externalId"); 579 ExpectInt64Value(node.GetExternalId(), value, "externalId");
581 ExpectInt64Value(node.GetPredecessorId(), value, "predecessorId"); 580 ExpectInt64Value(node.GetPredecessorId(), value, "predecessorId");
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
618 ADD_FAILURE(); 617 ADD_FAILURE();
619 } 618 }
620 } 619 }
621 620
622 TEST_F(SyncApiTest, EmptyTags) { 621 TEST_F(SyncApiTest, EmptyTags) {
623 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); 622 WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
624 ReadNode root_node(&trans); 623 ReadNode root_node(&trans);
625 root_node.InitByRootLookup(); 624 root_node.InitByRootLookup();
626 WriteNode node(&trans); 625 WriteNode node(&trans);
627 std::string empty_tag; 626 std::string empty_tag;
628 syncer::WriteNode::InitUniqueByCreationResult result = 627 WriteNode::InitUniqueByCreationResult result =
629 node.InitUniqueByCreation(syncer::TYPED_URLS, root_node, empty_tag); 628 node.InitUniqueByCreation(TYPED_URLS, root_node, empty_tag);
630 EXPECT_NE(syncer::WriteNode::INIT_SUCCESS, result); 629 EXPECT_NE(WriteNode::INIT_SUCCESS, result);
631 EXPECT_EQ(BaseNode::INIT_FAILED_PRECONDITION, 630 EXPECT_EQ(BaseNode::INIT_FAILED_PRECONDITION,
632 node.InitByTagLookup(empty_tag)); 631 node.InitByTagLookup(empty_tag));
633 } 632 }
634 633
635 namespace { 634 namespace {
636 635
637 class TestHttpPostProviderInterface : public HttpPostProviderInterface { 636 class TestHttpPostProviderInterface : public HttpPostProviderInterface {
638 public: 637 public:
639 virtual ~TestHttpPostProviderInterface() {} 638 virtual ~TestHttpPostProviderInterface() {}
640 639
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
672 }; 671 };
673 672
674 class SyncManagerObserverMock : public SyncManager::Observer { 673 class SyncManagerObserverMock : public SyncManager::Observer {
675 public: 674 public:
676 MOCK_METHOD1(OnSyncCycleCompleted, 675 MOCK_METHOD1(OnSyncCycleCompleted,
677 void(const SyncSessionSnapshot&)); // NOLINT 676 void(const SyncSessionSnapshot&)); // NOLINT
678 MOCK_METHOD2(OnInitializationComplete, 677 MOCK_METHOD2(OnInitializationComplete,
679 void(const WeakHandle<JsBackend>&, bool)); // NOLINT 678 void(const WeakHandle<JsBackend>&, bool)); // NOLINT
680 MOCK_METHOD1(OnConnectionStatusChange, void(ConnectionStatus)); // NOLINT 679 MOCK_METHOD1(OnConnectionStatusChange, void(ConnectionStatus)); // NOLINT
681 MOCK_METHOD2(OnPassphraseRequired, 680 MOCK_METHOD2(OnPassphraseRequired,
682 void(syncer::PassphraseRequiredReason, 681 void(PassphraseRequiredReason,
683 const sync_pb::EncryptedData&)); // NOLINT 682 const sync_pb::EncryptedData&)); // NOLINT
684 MOCK_METHOD0(OnPassphraseAccepted, void()); // NOLINT 683 MOCK_METHOD0(OnPassphraseAccepted, void()); // NOLINT
685 MOCK_METHOD1(OnBootstrapTokenUpdated, void(const std::string&)); // NOLINT 684 MOCK_METHOD1(OnBootstrapTokenUpdated, void(const std::string&)); // NOLINT
686 MOCK_METHOD0(OnStopSyncingPermanently, void()); // NOLINT 685 MOCK_METHOD0(OnStopSyncingPermanently, void()); // NOLINT
687 MOCK_METHOD1(OnUpdatedToken, void(const std::string&)); // NOLINT 686 MOCK_METHOD1(OnUpdatedToken, void(const std::string&)); // NOLINT
688 MOCK_METHOD2(OnEncryptedTypesChanged, 687 MOCK_METHOD2(OnEncryptedTypesChanged,
689 void(ModelTypeSet, bool)); // NOLINT 688 void(ModelTypeSet, bool)); // NOLINT
690 MOCK_METHOD0(OnEncryptionComplete, void()); // NOLINT 689 MOCK_METHOD0(OnEncryptionComplete, void()); // NOLINT
691 MOCK_METHOD1(OnActionableError, 690 MOCK_METHOD1(OnActionableError,
692 void(const syncer::SyncProtocolError&)); // NOLINT 691 void(const SyncProtocolError&)); // NOLINT
693 }; 692 };
694 693
695 class SyncNotifierMock : public syncer::SyncNotifier { 694 class SyncNotifierMock : public SyncNotifier {
696 public: 695 public:
697 MOCK_METHOD1(AddObserver, void(syncer::SyncNotifierObserver*)); 696 MOCK_METHOD1(AddObserver, void(SyncNotifierObserver*));
698 MOCK_METHOD1(RemoveObserver, void(syncer::SyncNotifierObserver*)); 697 MOCK_METHOD1(RemoveObserver, void(SyncNotifierObserver*));
699 MOCK_METHOD1(SetUniqueId, void(const std::string&)); 698 MOCK_METHOD1(SetUniqueId, void(const std::string&));
700 MOCK_METHOD1(SetStateDeprecated, void(const std::string&)); 699 MOCK_METHOD1(SetStateDeprecated, void(const std::string&));
701 MOCK_METHOD2(UpdateCredentials, 700 MOCK_METHOD2(UpdateCredentials,
702 void(const std::string&, const std::string&)); 701 void(const std::string&, const std::string&));
703 MOCK_METHOD1(UpdateEnabledTypes, 702 MOCK_METHOD1(UpdateEnabledTypes,
704 void(syncer::ModelTypeSet)); 703 void(ModelTypeSet));
705 MOCK_METHOD1(SendNotification, void(syncer::ModelTypeSet)); 704 MOCK_METHOD1(SendNotification, void(ModelTypeSet));
706 }; 705 };
707 706
708 } // namespace 707 } // namespace
709 708
710 class SyncManagerTest : public testing::Test, 709 class SyncManagerTest : public testing::Test,
711 public SyncManager::ChangeDelegate { 710 public SyncManager::ChangeDelegate {
712 protected: 711 protected:
713 enum NigoriStatus { 712 enum NigoriStatus {
714 DONT_WRITE_NIGORI, 713 DONT_WRITE_NIGORI,
715 WRITE_TO_NIGORI 714 WRITE_TO_NIGORI
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
768 WeakHandle<JsEventHandler>(), 767 WeakHandle<JsEventHandler>(),
769 "bogus", 0, false, 768 "bogus", 0, false,
770 base::MessageLoopProxy::current(), 769 base::MessageLoopProxy::current(),
771 scoped_ptr<HttpPostProviderFactory>( 770 scoped_ptr<HttpPostProviderFactory>(
772 new TestHttpPostProviderFactory()), 771 new TestHttpPostProviderFactory()),
773 routing_info, workers, 772 routing_info, workers,
774 &extensions_activity_monitor_, this, 773 &extensions_activity_monitor_, this,
775 credentials, 774 credentials,
776 scoped_ptr<SyncNotifier>(sync_notifier_mock_), 775 scoped_ptr<SyncNotifier>(sync_notifier_mock_),
777 "", 776 "",
778 syncer::SyncManager::TEST_IN_MEMORY, 777 SyncManager::TEST_IN_MEMORY,
779 &encryptor_, 778 &encryptor_,
780 &handler_, 779 &handler_,
781 NULL); 780 NULL);
782 781
783 EXPECT_TRUE(sync_notifier_observer_); 782 EXPECT_TRUE(sync_notifier_observer_);
784 EXPECT_TRUE(js_backend_.IsInitialized()); 783 EXPECT_TRUE(js_backend_.IsInitialized());
785 784
786 EXPECT_EQ(0, update_enabled_types_call_count_); 785 EXPECT_EQ(0, update_enabled_types_call_count_);
787 786
788 for (ModelSafeRoutingInfo::iterator i = routing_info.begin(); 787 for (ModelSafeRoutingInfo::iterator i = routing_info.begin();
789 i != routing_info.end(); ++i) { 788 i != routing_info.end(); ++i) {
790 type_roots_[i->first] = MakeServerNodeForType( 789 type_roots_[i->first] = MakeServerNodeForType(
791 sync_manager_.GetUserShare(), i->first); 790 sync_manager_.GetUserShare(), i->first);
792 } 791 }
793 PumpLoop(); 792 PumpLoop();
794 } 793 }
795 794
796 void TearDown() { 795 void TearDown() {
797 sync_manager_.RemoveObserver(&observer_); 796 sync_manager_.RemoveObserver(&observer_);
798 sync_manager_.ShutdownOnSyncThread(); 797 sync_manager_.ShutdownOnSyncThread();
799 sync_notifier_mock_ = NULL; 798 sync_notifier_mock_ = NULL;
800 EXPECT_FALSE(sync_notifier_observer_); 799 EXPECT_FALSE(sync_notifier_observer_);
801 PumpLoop(); 800 PumpLoop();
802 } 801 }
803 802
804 void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) { 803 void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) {
805 (*out)[syncer::NIGORI] = syncer::GROUP_PASSIVE; 804 (*out)[NIGORI] = GROUP_PASSIVE;
806 (*out)[syncer::BOOKMARKS] = syncer::GROUP_PASSIVE; 805 (*out)[BOOKMARKS] = GROUP_PASSIVE;
807 (*out)[syncer::THEMES] = syncer::GROUP_PASSIVE; 806 (*out)[THEMES] = GROUP_PASSIVE;
808 (*out)[syncer::SESSIONS] = syncer::GROUP_PASSIVE; 807 (*out)[SESSIONS] = GROUP_PASSIVE;
809 (*out)[syncer::PASSWORDS] = syncer::GROUP_PASSIVE; 808 (*out)[PASSWORDS] = GROUP_PASSIVE;
810 (*out)[syncer::PREFERENCES] = syncer::GROUP_PASSIVE; 809 (*out)[PREFERENCES] = GROUP_PASSIVE;
811 } 810 }
812 811
813 virtual void OnChangesApplied( 812 virtual void OnChangesApplied(
814 syncer::ModelType model_type, 813 ModelType model_type,
815 const BaseTransaction* trans, 814 const BaseTransaction* trans,
816 const ImmutableChangeRecordList& changes) OVERRIDE {} 815 const ImmutableChangeRecordList& changes) OVERRIDE {}
817 816
818 virtual void OnChangesComplete(syncer::ModelType model_type) OVERRIDE {} 817 virtual void OnChangesComplete(ModelType model_type) OVERRIDE {}
819 818
820 // Helper methods. 819 // Helper methods.
821 bool SetUpEncryption(NigoriStatus nigori_status, 820 bool SetUpEncryption(NigoriStatus nigori_status,
822 EncryptionStatus encryption_status) { 821 EncryptionStatus encryption_status) {
823 UserShare* share = sync_manager_.GetUserShare(); 822 UserShare* share = sync_manager_.GetUserShare();
824 share->directory->set_initial_sync_ended_for_type(syncer::NIGORI, true); 823 share->directory->set_initial_sync_ended_for_type(NIGORI, true);
825 824
826 // We need to create the nigori node as if it were an applied server update. 825 // We need to create the nigori node as if it were an applied server update.
827 int64 nigori_id = GetIdForDataType(syncer::NIGORI); 826 int64 nigori_id = GetIdForDataType(NIGORI);
828 if (nigori_id == kInvalidId) 827 if (nigori_id == kInvalidId)
829 return false; 828 return false;
830 829
831 // Set the nigori cryptographer information. 830 // Set the nigori cryptographer information.
832 WriteTransaction trans(FROM_HERE, share); 831 WriteTransaction trans(FROM_HERE, share);
833 Cryptographer* cryptographer = trans.GetCryptographer(); 832 Cryptographer* cryptographer = trans.GetCryptographer();
834 if (!cryptographer) 833 if (!cryptographer)
835 return false; 834 return false;
836 if (encryption_status != UNINITIALIZED) { 835 if (encryption_status != UNINITIALIZED) {
837 KeyParams params = {"localhost", "dummy", "foobar"}; 836 KeyParams params = {"localhost", "dummy", "foobar"};
(...skipping 14 matching lines...) Expand all
852 return cryptographer->is_ready(); 851 return cryptographer->is_ready();
853 } 852 }
854 853
855 int64 GetIdForDataType(ModelType type) { 854 int64 GetIdForDataType(ModelType type) {
856 if (type_roots_.count(type) == 0) 855 if (type_roots_.count(type) == 0)
857 return 0; 856 return 0;
858 return type_roots_[type]; 857 return type_roots_[type];
859 } 858 }
860 859
861 void SyncNotifierAddObserver( 860 void SyncNotifierAddObserver(
862 syncer::SyncNotifierObserver* sync_notifier_observer) { 861 SyncNotifierObserver* sync_notifier_observer) {
863 EXPECT_EQ(NULL, sync_notifier_observer_); 862 EXPECT_EQ(NULL, sync_notifier_observer_);
864 sync_notifier_observer_ = sync_notifier_observer; 863 sync_notifier_observer_ = sync_notifier_observer;
865 } 864 }
866 865
867 void SyncNotifierRemoveObserver( 866 void SyncNotifierRemoveObserver(
868 syncer::SyncNotifierObserver* sync_notifier_observer) { 867 SyncNotifierObserver* sync_notifier_observer) {
869 EXPECT_EQ(sync_notifier_observer_, sync_notifier_observer); 868 EXPECT_EQ(sync_notifier_observer_, sync_notifier_observer);
870 sync_notifier_observer_ = NULL; 869 sync_notifier_observer_ = NULL;
871 } 870 }
872 871
873 void SyncNotifierUpdateEnabledTypes(syncer::ModelTypeSet types) { 872 void SyncNotifierUpdateEnabledTypes(ModelTypeSet types) {
874 ModelSafeRoutingInfo routes; 873 ModelSafeRoutingInfo routes;
875 GetModelSafeRoutingInfo(&routes); 874 GetModelSafeRoutingInfo(&routes);
876 const syncer::ModelTypeSet expected_types = GetRoutingInfoTypes(routes); 875 const ModelTypeSet expected_types = GetRoutingInfoTypes(routes);
877 EXPECT_TRUE(types.Equals(expected_types)); 876 EXPECT_TRUE(types.Equals(expected_types));
878 ++update_enabled_types_call_count_; 877 ++update_enabled_types_call_count_;
879 } 878 }
880 879
881 void PumpLoop() { 880 void PumpLoop() {
882 message_loop_.RunAllPending(); 881 message_loop_.RunAllPending();
883 } 882 }
884 883
885 void SendJsMessage(const std::string& name, const JsArgList& args, 884 void SendJsMessage(const std::string& name, const JsArgList& args,
886 const WeakHandle<JsReplyHandler>& reply_handler) { 885 const WeakHandle<JsReplyHandler>& reply_handler) {
887 js_backend_.Call(FROM_HERE, &JsBackend::ProcessJsMessage, 886 js_backend_.Call(FROM_HERE, &JsBackend::ProcessJsMessage,
888 name, args, reply_handler); 887 name, args, reply_handler);
889 PumpLoop(); 888 PumpLoop();
890 } 889 }
891 890
892 void SetJsEventHandler(const WeakHandle<JsEventHandler>& event_handler) { 891 void SetJsEventHandler(const WeakHandle<JsEventHandler>& event_handler) {
893 js_backend_.Call(FROM_HERE, &JsBackend::SetJsEventHandler, 892 js_backend_.Call(FROM_HERE, &JsBackend::SetJsEventHandler,
894 event_handler); 893 event_handler);
895 PumpLoop(); 894 PumpLoop();
896 } 895 }
897 896
898 // Looks up an entry by client tag and resets IS_UNSYNCED value to false. 897 // Looks up an entry by client tag and resets IS_UNSYNCED value to false.
899 // Returns true if entry was previously unsynced, false if IS_UNSYNCED was 898 // Returns true if entry was previously unsynced, false if IS_UNSYNCED was
900 // already false. 899 // already false.
901 bool ResetUnsyncedEntry(syncer::ModelType type, 900 bool ResetUnsyncedEntry(ModelType type,
902 const std::string& client_tag) { 901 const std::string& client_tag) {
903 UserShare* share = sync_manager_.GetUserShare(); 902 UserShare* share = sync_manager_.GetUserShare();
904 syncable::WriteTransaction trans( 903 syncable::WriteTransaction trans(
905 FROM_HERE, syncable::UNITTEST, share->directory.get()); 904 FROM_HERE, syncable::UNITTEST, share->directory.get());
906 const std::string hash = BaseNode::GenerateSyncableHash(type, client_tag); 905 const std::string hash = BaseNode::GenerateSyncableHash(type, client_tag);
907 syncable::MutableEntry entry(&trans, syncable::GET_BY_CLIENT_TAG, 906 syncable::MutableEntry entry(&trans, syncable::GET_BY_CLIENT_TAG,
908 hash); 907 hash);
909 EXPECT_TRUE(entry.good()); 908 EXPECT_TRUE(entry.good());
910 if (!entry.Get(IS_UNSYNCED)) 909 if (!entry.Get(IS_UNSYNCED))
911 return false; 910 return false;
(...skipping 14 matching lines...) Expand all
926 std::map<ModelType, int64> type_roots_; 925 std::map<ModelType, int64> type_roots_;
927 FakeExtensionsActivityMonitor extensions_activity_monitor_; 926 FakeExtensionsActivityMonitor extensions_activity_monitor_;
928 StrictMock<SyncNotifierMock>* sync_notifier_mock_; 927 StrictMock<SyncNotifierMock>* sync_notifier_mock_;
929 928
930 protected: 929 protected:
931 FakeEncryptor encryptor_; 930 FakeEncryptor encryptor_;
932 TestUnrecoverableErrorHandler handler_; 931 TestUnrecoverableErrorHandler handler_;
933 SyncManagerImpl sync_manager_; 932 SyncManagerImpl sync_manager_;
934 WeakHandle<JsBackend> js_backend_; 933 WeakHandle<JsBackend> js_backend_;
935 StrictMock<SyncManagerObserverMock> observer_; 934 StrictMock<SyncManagerObserverMock> observer_;
936 syncer::SyncNotifierObserver* sync_notifier_observer_; 935 SyncNotifierObserver* sync_notifier_observer_;
937 int update_enabled_types_call_count_; 936 int update_enabled_types_call_count_;
938 }; 937 };
939 938
940 TEST_F(SyncManagerTest, UpdateEnabledTypes) { 939 TEST_F(SyncManagerTest, UpdateEnabledTypes) {
941 EXPECT_EQ(0, update_enabled_types_call_count_); 940 EXPECT_EQ(0, update_enabled_types_call_count_);
942 941
943 ModelSafeRoutingInfo routes; 942 ModelSafeRoutingInfo routes;
944 GetModelSafeRoutingInfo(&routes); 943 GetModelSafeRoutingInfo(&routes);
945 const syncer::ModelTypeSet enabled_types = GetRoutingInfoTypes(routes); 944 const ModelTypeSet enabled_types = GetRoutingInfoTypes(routes);
946 945
947 sync_manager_.UpdateEnabledTypes(enabled_types); 946 sync_manager_.UpdateEnabledTypes(enabled_types);
948 EXPECT_EQ(1, update_enabled_types_call_count_); 947 EXPECT_EQ(1, update_enabled_types_call_count_);
949 } 948 }
950 949
951 TEST_F(SyncManagerTest, ProcessJsMessage) { 950 TEST_F(SyncManagerTest, ProcessJsMessage) {
952 const JsArgList kNoArgs; 951 const JsArgList kNoArgs;
953 952
954 StrictMock<MockJsReplyHandler> reply_handler; 953 StrictMock<MockJsReplyHandler> reply_handler;
955 954
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
1017 void RunGetNodesByIdTest(const char* message_name, bool is_detailed) { 1016 void RunGetNodesByIdTest(const char* message_name, bool is_detailed) {
1018 int64 root_id = kInvalidId; 1017 int64 root_id = kInvalidId;
1019 { 1018 {
1020 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1019 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1021 ReadNode root_node(&trans); 1020 ReadNode root_node(&trans);
1022 root_node.InitByRootLookup(); 1021 root_node.InitByRootLookup();
1023 root_id = root_node.GetId(); 1022 root_id = root_node.GetId();
1024 } 1023 }
1025 1024
1026 int64 child_id = 1025 int64 child_id =
1027 MakeNode(sync_manager_.GetUserShare(), syncer::BOOKMARKS, "testtag"); 1026 MakeNode(sync_manager_.GetUserShare(), BOOKMARKS, "testtag");
1028 1027
1029 StrictMock<MockJsReplyHandler> reply_handler; 1028 StrictMock<MockJsReplyHandler> reply_handler;
1030 1029
1031 JsArgList return_args; 1030 JsArgList return_args;
1032 1031
1033 const int64 ids[] = { root_id, child_id }; 1032 const int64 ids[] = { root_id, child_id };
1034 1033
1035 EXPECT_CALL(reply_handler, 1034 EXPECT_CALL(reply_handler,
1036 HandleJsReply(message_name, _)) 1035 HandleJsReply(message_name, _))
1037 .Times(arraysize(ids)).WillRepeatedly(SaveArg<1>(&return_args)); 1036 .Times(arraysize(ids)).WillRepeatedly(SaveArg<1>(&return_args));
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
1248 1247
1249 EXPECT_CALL(event_handler, 1248 EXPECT_CALL(event_handler,
1250 HandleJsEvent("onNotificationStateChange", 1249 HandleJsEvent("onNotificationStateChange",
1251 HasDetailsAsDictionary(true_details))); 1250 HasDetailsAsDictionary(true_details)));
1252 EXPECT_CALL(event_handler, 1251 EXPECT_CALL(event_handler,
1253 HandleJsEvent("onNotificationStateChange", 1252 HandleJsEvent("onNotificationStateChange",
1254 HasDetailsAsDictionary(false_details))); 1253 HasDetailsAsDictionary(false_details)));
1255 1254
1256 sync_manager_.SimulateEnableNotificationsForTest(); 1255 sync_manager_.SimulateEnableNotificationsForTest();
1257 sync_manager_.SimulateDisableNotificationsForTest( 1256 sync_manager_.SimulateDisableNotificationsForTest(
1258 syncer::TRANSIENT_NOTIFICATION_ERROR); 1257 TRANSIENT_NOTIFICATION_ERROR);
1259 1258
1260 SetJsEventHandler(event_handler.AsWeakHandle()); 1259 SetJsEventHandler(event_handler.AsWeakHandle());
1261 sync_manager_.SimulateEnableNotificationsForTest(); 1260 sync_manager_.SimulateEnableNotificationsForTest();
1262 sync_manager_.SimulateDisableNotificationsForTest( 1261 sync_manager_.SimulateDisableNotificationsForTest(
1263 syncer::TRANSIENT_NOTIFICATION_ERROR); 1262 TRANSIENT_NOTIFICATION_ERROR);
1264 SetJsEventHandler(WeakHandle<JsEventHandler>()); 1263 SetJsEventHandler(WeakHandle<JsEventHandler>());
1265 1264
1266 sync_manager_.SimulateEnableNotificationsForTest(); 1265 sync_manager_.SimulateEnableNotificationsForTest();
1267 sync_manager_.SimulateDisableNotificationsForTest( 1266 sync_manager_.SimulateDisableNotificationsForTest(
1268 syncer::TRANSIENT_NOTIFICATION_ERROR); 1267 TRANSIENT_NOTIFICATION_ERROR);
1269 1268
1270 // Should trigger the replies. 1269 // Should trigger the replies.
1271 PumpLoop(); 1270 PumpLoop();
1272 } 1271 }
1273 1272
1274 TEST_F(SyncManagerTest, OnIncomingNotification) { 1273 TEST_F(SyncManagerTest, OnIncomingNotification) {
1275 StrictMock<MockJsEventHandler> event_handler; 1274 StrictMock<MockJsEventHandler> event_handler;
1276 1275
1277 const syncer::ModelTypeSet empty_model_types; 1276 const ModelTypeSet empty_model_types;
1278 const syncer::ModelTypeSet model_types( 1277 const ModelTypeSet model_types(
1279 syncer::BOOKMARKS, syncer::THEMES); 1278 BOOKMARKS, THEMES);
1280 1279
1281 // Build expected_args to have a single argument with the string 1280 // Build expected_args to have a single argument with the string
1282 // equivalents of model_types. 1281 // equivalents of model_types.
1283 DictionaryValue expected_details; 1282 DictionaryValue expected_details;
1284 { 1283 {
1285 ListValue* model_type_list = new ListValue(); 1284 ListValue* model_type_list = new ListValue();
1286 expected_details.SetString("source", "REMOTE_NOTIFICATION"); 1285 expected_details.SetString("source", "REMOTE_NOTIFICATION");
1287 expected_details.Set("changedTypes", model_type_list); 1286 expected_details.Set("changedTypes", model_type_list);
1288 for (syncer::ModelTypeSet::Iterator it = model_types.First(); 1287 for (ModelTypeSet::Iterator it = model_types.First();
1289 it.Good(); it.Inc()) { 1288 it.Good(); it.Inc()) {
1290 model_type_list->Append( 1289 model_type_list->Append(
1291 Value::CreateStringValue(syncer::ModelTypeToString(it.Get()))); 1290 Value::CreateStringValue(ModelTypeToString(it.Get())));
1292 } 1291 }
1293 } 1292 }
1294 1293
1295 EXPECT_CALL(event_handler, 1294 EXPECT_CALL(event_handler,
1296 HandleJsEvent("onIncomingNotification", 1295 HandleJsEvent("onIncomingNotification",
1297 HasDetailsAsDictionary(expected_details))); 1296 HasDetailsAsDictionary(expected_details)));
1298 1297
1299 sync_manager_.TriggerOnIncomingNotificationForTest(empty_model_types); 1298 sync_manager_.TriggerOnIncomingNotificationForTest(empty_model_types);
1300 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); 1299 sync_manager_.TriggerOnIncomingNotificationForTest(model_types);
1301 1300
1302 SetJsEventHandler(event_handler.AsWeakHandle()); 1301 SetJsEventHandler(event_handler.AsWeakHandle());
1303 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); 1302 sync_manager_.TriggerOnIncomingNotificationForTest(model_types);
1304 SetJsEventHandler(WeakHandle<JsEventHandler>()); 1303 SetJsEventHandler(WeakHandle<JsEventHandler>());
1305 1304
1306 sync_manager_.TriggerOnIncomingNotificationForTest(empty_model_types); 1305 sync_manager_.TriggerOnIncomingNotificationForTest(empty_model_types);
1307 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); 1306 sync_manager_.TriggerOnIncomingNotificationForTest(model_types);
1308 1307
1309 // Should trigger the replies. 1308 // Should trigger the replies.
1310 PumpLoop(); 1309 PumpLoop();
1311 } 1310 }
1312 1311
1313 TEST_F(SyncManagerTest, RefreshEncryptionReady) { 1312 TEST_F(SyncManagerTest, RefreshEncryptionReady) {
1314 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); 1313 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1315 EXPECT_CALL(observer_, OnEncryptionComplete()); 1314 EXPECT_CALL(observer_, OnEncryptionComplete());
1316 1315
1317 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing)); 1316 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing));
1318 PumpLoop(); 1317 PumpLoop();
1319 1318
1320 const syncer::ModelTypeSet encrypted_types = 1319 const ModelTypeSet encrypted_types =
1321 sync_manager_.GetEncryptedDataTypesForTest(); 1320 sync_manager_.GetEncryptedDataTypesForTest();
1322 EXPECT_TRUE(encrypted_types.Has(syncer::PASSWORDS)); 1321 EXPECT_TRUE(encrypted_types.Has(PASSWORDS));
1323 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest()); 1322 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest());
1324 { 1323 {
1325 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1324 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1326 ReadNode node(&trans); 1325 ReadNode node(&trans);
1327 EXPECT_EQ(BaseNode::INIT_OK, 1326 EXPECT_EQ(BaseNode::INIT_OK,
1328 node.InitByIdLookup(GetIdForDataType(syncer::NIGORI))); 1327 node.InitByIdLookup(GetIdForDataType(NIGORI)));
1329 sync_pb::NigoriSpecifics nigori = node.GetNigoriSpecifics(); 1328 sync_pb::NigoriSpecifics nigori = node.GetNigoriSpecifics();
1330 EXPECT_TRUE(nigori.has_encrypted()); 1329 EXPECT_TRUE(nigori.has_encrypted());
1331 Cryptographer* cryptographer = trans.GetCryptographer(); 1330 Cryptographer* cryptographer = trans.GetCryptographer();
1332 EXPECT_TRUE(cryptographer->is_ready()); 1331 EXPECT_TRUE(cryptographer->is_ready());
1333 EXPECT_TRUE(cryptographer->CanDecrypt(nigori.encrypted())); 1332 EXPECT_TRUE(cryptographer->CanDecrypt(nigori.encrypted()));
1334 } 1333 }
1335 } 1334 }
1336 1335
1337 // Attempt to refresh encryption when nigori not downloaded. 1336 // Attempt to refresh encryption when nigori not downloaded.
1338 TEST_F(SyncManagerTest, RefreshEncryptionNotReady) { 1337 TEST_F(SyncManagerTest, RefreshEncryptionNotReady) {
1339 // Don't set up encryption (no nigori node created). 1338 // Don't set up encryption (no nigori node created).
1340 1339
1341 // Should fail. 1340 // Should fail.
1342 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing)); 1341 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing));
1343 PumpLoop(); 1342 PumpLoop();
1344 1343
1345 const syncer::ModelTypeSet encrypted_types = 1344 const ModelTypeSet encrypted_types =
1346 sync_manager_.GetEncryptedDataTypesForTest(); 1345 sync_manager_.GetEncryptedDataTypesForTest();
1347 EXPECT_TRUE(encrypted_types.Has(syncer::PASSWORDS)); // Hardcoded. 1346 EXPECT_TRUE(encrypted_types.Has(PASSWORDS)); // Hardcoded.
1348 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest()); 1347 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest());
1349 } 1348 }
1350 1349
1351 // Attempt to refresh encryption when nigori is empty. 1350 // Attempt to refresh encryption when nigori is empty.
1352 TEST_F(SyncManagerTest, RefreshEncryptionEmptyNigori) { 1351 TEST_F(SyncManagerTest, RefreshEncryptionEmptyNigori) {
1353 EXPECT_TRUE(SetUpEncryption(DONT_WRITE_NIGORI, DEFAULT_ENCRYPTION)); 1352 EXPECT_TRUE(SetUpEncryption(DONT_WRITE_NIGORI, DEFAULT_ENCRYPTION));
1354 EXPECT_CALL(observer_, OnEncryptionComplete()); 1353 EXPECT_CALL(observer_, OnEncryptionComplete());
1355 1354
1356 // Should write to nigori. 1355 // Should write to nigori.
1357 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing)); 1356 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing));
1358 PumpLoop(); 1357 PumpLoop();
1359 1358
1360 const syncer::ModelTypeSet encrypted_types = 1359 const ModelTypeSet encrypted_types =
1361 sync_manager_.GetEncryptedDataTypesForTest(); 1360 sync_manager_.GetEncryptedDataTypesForTest();
1362 EXPECT_TRUE(encrypted_types.Has(syncer::PASSWORDS)); // Hardcoded. 1361 EXPECT_TRUE(encrypted_types.Has(PASSWORDS)); // Hardcoded.
1363 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest()); 1362 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest());
1364 { 1363 {
1365 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1364 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1366 ReadNode node(&trans); 1365 ReadNode node(&trans);
1367 EXPECT_EQ(BaseNode::INIT_OK, 1366 EXPECT_EQ(BaseNode::INIT_OK,
1368 node.InitByIdLookup(GetIdForDataType(syncer::NIGORI))); 1367 node.InitByIdLookup(GetIdForDataType(NIGORI)));
1369 sync_pb::NigoriSpecifics nigori = node.GetNigoriSpecifics(); 1368 sync_pb::NigoriSpecifics nigori = node.GetNigoriSpecifics();
1370 EXPECT_TRUE(nigori.has_encrypted()); 1369 EXPECT_TRUE(nigori.has_encrypted());
1371 Cryptographer* cryptographer = trans.GetCryptographer(); 1370 Cryptographer* cryptographer = trans.GetCryptographer();
1372 EXPECT_TRUE(cryptographer->is_ready()); 1371 EXPECT_TRUE(cryptographer->is_ready());
1373 EXPECT_TRUE(cryptographer->CanDecrypt(nigori.encrypted())); 1372 EXPECT_TRUE(cryptographer->CanDecrypt(nigori.encrypted()));
1374 } 1373 }
1375 } 1374 }
1376 1375
1377 TEST_F(SyncManagerTest, EncryptDataTypesWithNoData) { 1376 TEST_F(SyncManagerTest, EncryptDataTypesWithNoData) {
1378 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); 1377 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1379 EXPECT_CALL(observer_, 1378 EXPECT_CALL(observer_,
1380 OnEncryptedTypesChanged( 1379 OnEncryptedTypesChanged(
1381 HasModelTypes(syncer::ModelTypeSet::All()), true)); 1380 HasModelTypes(ModelTypeSet::All()), true));
1382 EXPECT_CALL(observer_, OnEncryptionComplete()); 1381 EXPECT_CALL(observer_, OnEncryptionComplete());
1383 sync_manager_.EnableEncryptEverything(); 1382 sync_manager_.EnableEncryptEverything();
1384 EXPECT_TRUE(sync_manager_.EncryptEverythingEnabledForTest()); 1383 EXPECT_TRUE(sync_manager_.EncryptEverythingEnabledForTest());
1385 } 1384 }
1386 1385
1387 TEST_F(SyncManagerTest, EncryptDataTypesWithData) { 1386 TEST_F(SyncManagerTest, EncryptDataTypesWithData) {
1388 size_t batch_size = 5; 1387 size_t batch_size = 5;
1389 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); 1388 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1390 1389
1391 // Create some unencrypted unsynced data. 1390 // Create some unencrypted unsynced data.
1392 int64 folder = MakeFolderWithParent(sync_manager_.GetUserShare(), 1391 int64 folder = MakeFolderWithParent(sync_manager_.GetUserShare(),
1393 syncer::BOOKMARKS, 1392 BOOKMARKS,
1394 GetIdForDataType(syncer::BOOKMARKS), 1393 GetIdForDataType(BOOKMARKS),
1395 NULL); 1394 NULL);
1396 // First batch_size nodes are children of folder. 1395 // First batch_size nodes are children of folder.
1397 size_t i; 1396 size_t i;
1398 for (i = 0; i < batch_size; ++i) { 1397 for (i = 0; i < batch_size; ++i) {
1399 MakeNodeWithParent(sync_manager_.GetUserShare(), syncer::BOOKMARKS, 1398 MakeNodeWithParent(sync_manager_.GetUserShare(), BOOKMARKS,
1400 base::StringPrintf("%"PRIuS"", i), folder); 1399 base::StringPrintf("%"PRIuS"", i), folder);
1401 } 1400 }
1402 // Next batch_size nodes are a different type and on their own. 1401 // Next batch_size nodes are a different type and on their own.
1403 for (; i < 2*batch_size; ++i) { 1402 for (; i < 2*batch_size; ++i) {
1404 MakeNodeWithParent(sync_manager_.GetUserShare(), syncer::SESSIONS, 1403 MakeNodeWithParent(sync_manager_.GetUserShare(), SESSIONS,
1405 base::StringPrintf("%"PRIuS"", i), 1404 base::StringPrintf("%"PRIuS"", i),
1406 GetIdForDataType(syncer::SESSIONS)); 1405 GetIdForDataType(SESSIONS));
1407 } 1406 }
1408 // Last batch_size nodes are a third type that will not need encryption. 1407 // Last batch_size nodes are a third type that will not need encryption.
1409 for (; i < 3*batch_size; ++i) { 1408 for (; i < 3*batch_size; ++i) {
1410 MakeNodeWithParent(sync_manager_.GetUserShare(), syncer::THEMES, 1409 MakeNodeWithParent(sync_manager_.GetUserShare(), THEMES,
1411 base::StringPrintf("%"PRIuS"", i), 1410 base::StringPrintf("%"PRIuS"", i),
1412 GetIdForDataType(syncer::THEMES)); 1411 GetIdForDataType(THEMES));
1413 } 1412 }
1414 1413
1415 { 1414 {
1416 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1415 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1417 EXPECT_TRUE(GetEncryptedTypes(&trans).Equals( 1416 EXPECT_TRUE(GetEncryptedTypes(&trans).Equals(
1418 Cryptographer::SensitiveTypes())); 1417 Cryptographer::SensitiveTypes()));
1419 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( 1418 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1420 trans.GetWrappedTrans(), 1419 trans.GetWrappedTrans(),
1421 trans.GetCryptographer(), 1420 trans.GetCryptographer(),
1422 syncer::BOOKMARKS, 1421 BOOKMARKS,
1423 false /* not encrypted */)); 1422 false /* not encrypted */));
1424 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( 1423 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1425 trans.GetWrappedTrans(), 1424 trans.GetWrappedTrans(),
1426 trans.GetCryptographer(), 1425 trans.GetCryptographer(),
1427 syncer::SESSIONS, 1426 SESSIONS,
1428 false /* not encrypted */)); 1427 false /* not encrypted */));
1429 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( 1428 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1430 trans.GetWrappedTrans(), 1429 trans.GetWrappedTrans(),
1431 trans.GetCryptographer(), 1430 trans.GetCryptographer(),
1432 syncer::THEMES, 1431 THEMES,
1433 false /* not encrypted */)); 1432 false /* not encrypted */));
1434 } 1433 }
1435 1434
1436 EXPECT_CALL(observer_, 1435 EXPECT_CALL(observer_,
1437 OnEncryptedTypesChanged( 1436 OnEncryptedTypesChanged(
1438 HasModelTypes(syncer::ModelTypeSet::All()), true)); 1437 HasModelTypes(ModelTypeSet::All()), true));
1439 EXPECT_CALL(observer_, OnEncryptionComplete()); 1438 EXPECT_CALL(observer_, OnEncryptionComplete());
1440 sync_manager_.EnableEncryptEverything(); 1439 sync_manager_.EnableEncryptEverything();
1441 EXPECT_TRUE(sync_manager_.EncryptEverythingEnabledForTest()); 1440 EXPECT_TRUE(sync_manager_.EncryptEverythingEnabledForTest());
1442 { 1441 {
1443 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1442 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1444 EXPECT_TRUE(GetEncryptedTypes(&trans).Equals( 1443 EXPECT_TRUE(GetEncryptedTypes(&trans).Equals(
1445 syncer::ModelTypeSet::All())); 1444 ModelTypeSet::All()));
1446 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( 1445 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1447 trans.GetWrappedTrans(), 1446 trans.GetWrappedTrans(),
1448 trans.GetCryptographer(), 1447 trans.GetCryptographer(),
1449 syncer::BOOKMARKS, 1448 BOOKMARKS,
1450 true /* is encrypted */)); 1449 true /* is encrypted */));
1451 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( 1450 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1452 trans.GetWrappedTrans(), 1451 trans.GetWrappedTrans(),
1453 trans.GetCryptographer(), 1452 trans.GetCryptographer(),
1454 syncer::SESSIONS, 1453 SESSIONS,
1455 true /* is encrypted */)); 1454 true /* is encrypted */));
1456 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( 1455 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1457 trans.GetWrappedTrans(), 1456 trans.GetWrappedTrans(),
1458 trans.GetCryptographer(), 1457 trans.GetCryptographer(),
1459 syncer::THEMES, 1458 THEMES,
1460 true /* is encrypted */)); 1459 true /* is encrypted */));
1461 } 1460 }
1462 1461
1463 // Trigger's a ReEncryptEverything with new passphrase. 1462 // Trigger's a ReEncryptEverything with new passphrase.
1464 testing::Mock::VerifyAndClearExpectations(&observer_); 1463 testing::Mock::VerifyAndClearExpectations(&observer_);
1465 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_)); 1464 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_));
1466 EXPECT_CALL(observer_, OnPassphraseAccepted()); 1465 EXPECT_CALL(observer_, OnPassphraseAccepted());
1467 EXPECT_CALL(observer_, OnEncryptionComplete()); 1466 EXPECT_CALL(observer_, OnEncryptionComplete());
1468 sync_manager_.SetEncryptionPassphrase("new_passphrase", true); 1467 sync_manager_.SetEncryptionPassphrase("new_passphrase", true);
1469 EXPECT_TRUE(sync_manager_.EncryptEverythingEnabledForTest()); 1468 EXPECT_TRUE(sync_manager_.EncryptEverythingEnabledForTest());
1470 { 1469 {
1471 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1470 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1472 EXPECT_TRUE(GetEncryptedTypes(&trans).Equals(syncer::ModelTypeSet::All())); 1471 EXPECT_TRUE(GetEncryptedTypes(&trans).Equals(ModelTypeSet::All()));
1473 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( 1472 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1474 trans.GetWrappedTrans(), 1473 trans.GetWrappedTrans(),
1475 trans.GetCryptographer(), 1474 trans.GetCryptographer(),
1476 syncer::BOOKMARKS, 1475 BOOKMARKS,
1477 true /* is encrypted */)); 1476 true /* is encrypted */));
1478 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( 1477 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1479 trans.GetWrappedTrans(), 1478 trans.GetWrappedTrans(),
1480 trans.GetCryptographer(), 1479 trans.GetCryptographer(),
1481 syncer::SESSIONS, 1480 SESSIONS,
1482 true /* is encrypted */)); 1481 true /* is encrypted */));
1483 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( 1482 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1484 trans.GetWrappedTrans(), 1483 trans.GetWrappedTrans(),
1485 trans.GetCryptographer(), 1484 trans.GetCryptographer(),
1486 syncer::THEMES, 1485 THEMES,
1487 true /* is encrypted */)); 1486 true /* is encrypted */));
1488 } 1487 }
1489 // Calling EncryptDataTypes with an empty encrypted types should not trigger 1488 // Calling EncryptDataTypes with an empty encrypted types should not trigger
1490 // a reencryption and should just notify immediately. 1489 // a reencryption and should just notify immediately.
1491 // TODO(zea): add logic to ensure nothing was written. 1490 // TODO(zea): add logic to ensure nothing was written.
1492 testing::Mock::VerifyAndClearExpectations(&observer_); 1491 testing::Mock::VerifyAndClearExpectations(&observer_);
1493 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_)).Times(0); 1492 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_)).Times(0);
1494 EXPECT_CALL(observer_, OnPassphraseAccepted()).Times(0); 1493 EXPECT_CALL(observer_, OnPassphraseAccepted()).Times(0);
1495 EXPECT_CALL(observer_, OnEncryptionComplete()); 1494 EXPECT_CALL(observer_, OnEncryptionComplete());
1496 sync_manager_.EnableEncryptEverything(); 1495 sync_manager_.EnableEncryptEverything();
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1557 // Store the default (soon to be old) key. 1556 // Store the default (soon to be old) key.
1558 Cryptographer* cryptographer = trans.GetCryptographer(); 1557 Cryptographer* cryptographer = trans.GetCryptographer();
1559 std::string bootstrap_token; 1558 std::string bootstrap_token;
1560 cryptographer->GetBootstrapToken(&bootstrap_token); 1559 cryptographer->GetBootstrapToken(&bootstrap_token);
1561 verifier.Bootstrap(bootstrap_token); 1560 verifier.Bootstrap(bootstrap_token);
1562 1561
1563 ReadNode root_node(&trans); 1562 ReadNode root_node(&trans);
1564 root_node.InitByRootLookup(); 1563 root_node.InitByRootLookup();
1565 1564
1566 WriteNode password_node(&trans); 1565 WriteNode password_node(&trans);
1567 syncer::WriteNode::InitUniqueByCreationResult result = 1566 WriteNode::InitUniqueByCreationResult result =
1568 password_node.InitUniqueByCreation(syncer::PASSWORDS, 1567 password_node.InitUniqueByCreation(PASSWORDS,
1569 root_node, "foo"); 1568 root_node, "foo");
1570 EXPECT_EQ(syncer::WriteNode::INIT_SUCCESS, result); 1569 EXPECT_EQ(WriteNode::INIT_SUCCESS, result);
1571 sync_pb::PasswordSpecificsData data; 1570 sync_pb::PasswordSpecificsData data;
1572 data.set_password_value("secret"); 1571 data.set_password_value("secret");
1573 password_node.SetPasswordSpecifics(data); 1572 password_node.SetPasswordSpecifics(data);
1574 } 1573 }
1575 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_)); 1574 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_));
1576 EXPECT_CALL(observer_, OnPassphraseAccepted()); 1575 EXPECT_CALL(observer_, OnPassphraseAccepted());
1577 EXPECT_CALL(observer_, OnEncryptionComplete()); 1576 EXPECT_CALL(observer_, OnEncryptionComplete());
1578 sync_manager_.SetEncryptionPassphrase("new_passphrase", true); 1577 sync_manager_.SetEncryptionPassphrase("new_passphrase", true);
1579 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest()); 1578 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest());
1580 { 1579 {
1581 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1580 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1582 Cryptographer* cryptographer = trans.GetCryptographer(); 1581 Cryptographer* cryptographer = trans.GetCryptographer();
1583 EXPECT_TRUE(cryptographer->is_ready()); 1582 EXPECT_TRUE(cryptographer->is_ready());
1584 // Verify the default key has changed. 1583 // Verify the default key has changed.
1585 sync_pb::EncryptedData encrypted; 1584 sync_pb::EncryptedData encrypted;
1586 cryptographer->GetKeys(&encrypted); 1585 cryptographer->GetKeys(&encrypted);
1587 EXPECT_FALSE(verifier.CanDecrypt(encrypted)); 1586 EXPECT_FALSE(verifier.CanDecrypt(encrypted));
1588 1587
1589 ReadNode password_node(&trans); 1588 ReadNode password_node(&trans);
1590 EXPECT_EQ(BaseNode::INIT_OK, 1589 EXPECT_EQ(BaseNode::INIT_OK,
1591 password_node.InitByClientTagLookup(syncer::PASSWORDS, 1590 password_node.InitByClientTagLookup(PASSWORDS,
1592 "foo")); 1591 "foo"));
1593 const sync_pb::PasswordSpecificsData& data = 1592 const sync_pb::PasswordSpecificsData& data =
1594 password_node.GetPasswordSpecifics(); 1593 password_node.GetPasswordSpecifics();
1595 EXPECT_EQ("secret", data.password_value()); 1594 EXPECT_EQ("secret", data.password_value());
1596 } 1595 }
1597 } 1596 }
1598 1597
1599 // Manually set the pending keys in the cryptographer/nigori to reflect the data 1598 // Manually set the pending keys in the cryptographer/nigori to reflect the data
1600 // being encrypted with a new (unprovided) GAIA password, then supply the 1599 // being encrypted with a new (unprovided) GAIA password, then supply the
1601 // password. 1600 // password.
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
1791 TEST_F(SyncManagerTest, SetPassphraseWithEmptyPasswordNode) { 1790 TEST_F(SyncManagerTest, SetPassphraseWithEmptyPasswordNode) {
1792 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); 1791 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1793 int64 node_id = 0; 1792 int64 node_id = 0;
1794 std::string tag = "foo"; 1793 std::string tag = "foo";
1795 { 1794 {
1796 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1795 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1797 ReadNode root_node(&trans); 1796 ReadNode root_node(&trans);
1798 root_node.InitByRootLookup(); 1797 root_node.InitByRootLookup();
1799 1798
1800 WriteNode password_node(&trans); 1799 WriteNode password_node(&trans);
1801 syncer::WriteNode::InitUniqueByCreationResult result = 1800 WriteNode::InitUniqueByCreationResult result =
1802 password_node.InitUniqueByCreation(syncer::PASSWORDS, root_node, tag); 1801 password_node.InitUniqueByCreation(PASSWORDS, root_node, tag);
1803 EXPECT_EQ(syncer::WriteNode::INIT_SUCCESS, result); 1802 EXPECT_EQ(WriteNode::INIT_SUCCESS, result);
1804 node_id = password_node.GetId(); 1803 node_id = password_node.GetId();
1805 } 1804 }
1806 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_)); 1805 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_));
1807 EXPECT_CALL(observer_, OnPassphraseAccepted()); 1806 EXPECT_CALL(observer_, OnPassphraseAccepted());
1808 EXPECT_CALL(observer_, OnEncryptionComplete()); 1807 EXPECT_CALL(observer_, OnEncryptionComplete());
1809 sync_manager_.SetEncryptionPassphrase("new_passphrase", true); 1808 sync_manager_.SetEncryptionPassphrase("new_passphrase", true);
1810 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest()); 1809 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest());
1811 { 1810 {
1812 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1811 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1813 ReadNode password_node(&trans); 1812 ReadNode password_node(&trans);
1814 EXPECT_EQ(BaseNode::INIT_FAILED_DECRYPT_IF_NECESSARY, 1813 EXPECT_EQ(BaseNode::INIT_FAILED_DECRYPT_IF_NECESSARY,
1815 password_node.InitByClientTagLookup(syncer::PASSWORDS, 1814 password_node.InitByClientTagLookup(PASSWORDS,
1816 tag)); 1815 tag));
1817 } 1816 }
1818 { 1817 {
1819 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1818 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1820 ReadNode password_node(&trans); 1819 ReadNode password_node(&trans);
1821 EXPECT_EQ(BaseNode::INIT_FAILED_DECRYPT_IF_NECESSARY, 1820 EXPECT_EQ(BaseNode::INIT_FAILED_DECRYPT_IF_NECESSARY,
1822 password_node.InitByIdLookup(node_id)); 1821 password_node.InitByIdLookup(node_id));
1823 } 1822 }
1824 } 1823 }
1825 1824
1826 TEST_F(SyncManagerTest, NudgeDelayTest) { 1825 TEST_F(SyncManagerTest, NudgeDelayTest) {
1827 EXPECT_EQ(sync_manager_.GetNudgeDelayTimeDelta(syncer::BOOKMARKS), 1826 EXPECT_EQ(sync_manager_.GetNudgeDelayTimeDelta(BOOKMARKS),
1828 base::TimeDelta::FromMilliseconds( 1827 base::TimeDelta::FromMilliseconds(
1829 SyncManagerImpl::GetDefaultNudgeDelay())); 1828 SyncManagerImpl::GetDefaultNudgeDelay()));
1830 1829
1831 EXPECT_EQ(sync_manager_.GetNudgeDelayTimeDelta(syncer::AUTOFILL), 1830 EXPECT_EQ(sync_manager_.GetNudgeDelayTimeDelta(AUTOFILL),
1832 base::TimeDelta::FromSeconds( 1831 base::TimeDelta::FromSeconds(
1833 syncer::kDefaultShortPollIntervalSeconds)); 1832 kDefaultShortPollIntervalSeconds));
1834 1833
1835 EXPECT_EQ(sync_manager_.GetNudgeDelayTimeDelta(syncer::PREFERENCES), 1834 EXPECT_EQ(sync_manager_.GetNudgeDelayTimeDelta(PREFERENCES),
1836 base::TimeDelta::FromMilliseconds( 1835 base::TimeDelta::FromMilliseconds(
1837 SyncManagerImpl::GetPreferencesNudgeDelay())); 1836 SyncManagerImpl::GetPreferencesNudgeDelay()));
1838 } 1837 }
1839 1838
1840 // Friended by WriteNode, so can't be in an anonymouse namespace. 1839 // Friended by WriteNode, so can't be in an anonymouse namespace.
1841 TEST_F(SyncManagerTest, EncryptBookmarksWithLegacyData) { 1840 TEST_F(SyncManagerTest, EncryptBookmarksWithLegacyData) {
1842 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); 1841 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1843 std::string title; 1842 std::string title;
1844 SyncAPINameToServerName("Google", &title); 1843 SyncAPINameToServerName("Google", &title);
1845 std::string url = "http://www.google.com"; 1844 std::string url = "http://www.google.com";
1846 std::string raw_title2 = ".."; // An invalid cosmo title. 1845 std::string raw_title2 = ".."; // An invalid cosmo title.
1847 std::string title2; 1846 std::string title2;
1848 SyncAPINameToServerName(raw_title2, &title2); 1847 SyncAPINameToServerName(raw_title2, &title2);
1849 std::string url2 = "http://www.bla.com"; 1848 std::string url2 = "http://www.bla.com";
1850 1849
1851 // Create a bookmark using the legacy format. 1850 // Create a bookmark using the legacy format.
1852 int64 node_id1 = MakeNode(sync_manager_.GetUserShare(), 1851 int64 node_id1 = MakeNode(sync_manager_.GetUserShare(),
1853 syncer::BOOKMARKS, 1852 BOOKMARKS,
1854 "testtag"); 1853 "testtag");
1855 int64 node_id2 = MakeNode(sync_manager_.GetUserShare(), 1854 int64 node_id2 = MakeNode(sync_manager_.GetUserShare(),
1856 syncer::BOOKMARKS, 1855 BOOKMARKS,
1857 "testtag2"); 1856 "testtag2");
1858 { 1857 {
1859 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1858 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1860 WriteNode node(&trans); 1859 WriteNode node(&trans);
1861 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(node_id1)); 1860 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(node_id1));
1862 1861
1863 sync_pb::EntitySpecifics entity_specifics; 1862 sync_pb::EntitySpecifics entity_specifics;
1864 entity_specifics.mutable_bookmark()->set_url(url); 1863 entity_specifics.mutable_bookmark()->set_url(url);
1865 node.SetEntitySpecifics(entity_specifics); 1864 node.SetEntitySpecifics(entity_specifics);
1866 1865
(...skipping 10 matching lines...) Expand all
1877 1876
1878 // Set the old style title. 1877 // Set the old style title.
1879 syncable::MutableEntry* node_entry2 = node2.entry_; 1878 syncable::MutableEntry* node_entry2 = node2.entry_;
1880 node_entry2->Put(syncable::NON_UNIQUE_NAME, title2); 1879 node_entry2->Put(syncable::NON_UNIQUE_NAME, title2);
1881 } 1880 }
1882 1881
1883 { 1882 {
1884 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1883 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1885 ReadNode node(&trans); 1884 ReadNode node(&trans);
1886 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(node_id1)); 1885 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(node_id1));
1887 EXPECT_EQ(syncer::BOOKMARKS, node.GetModelType()); 1886 EXPECT_EQ(BOOKMARKS, node.GetModelType());
1888 EXPECT_EQ(title, node.GetTitle()); 1887 EXPECT_EQ(title, node.GetTitle());
1889 EXPECT_EQ(title, node.GetBookmarkSpecifics().title()); 1888 EXPECT_EQ(title, node.GetBookmarkSpecifics().title());
1890 EXPECT_EQ(url, node.GetBookmarkSpecifics().url()); 1889 EXPECT_EQ(url, node.GetBookmarkSpecifics().url());
1891 1890
1892 ReadNode node2(&trans); 1891 ReadNode node2(&trans);
1893 EXPECT_EQ(BaseNode::INIT_OK, node2.InitByIdLookup(node_id2)); 1892 EXPECT_EQ(BaseNode::INIT_OK, node2.InitByIdLookup(node_id2));
1894 EXPECT_EQ(syncer::BOOKMARKS, node2.GetModelType()); 1893 EXPECT_EQ(BOOKMARKS, node2.GetModelType());
1895 // We should de-canonicalize the title in GetTitle(), but the title in the 1894 // We should de-canonicalize the title in GetTitle(), but the title in the
1896 // specifics should be stored in the server legal form. 1895 // specifics should be stored in the server legal form.
1897 EXPECT_EQ(raw_title2, node2.GetTitle()); 1896 EXPECT_EQ(raw_title2, node2.GetTitle());
1898 EXPECT_EQ(title2, node2.GetBookmarkSpecifics().title()); 1897 EXPECT_EQ(title2, node2.GetBookmarkSpecifics().title());
1899 EXPECT_EQ(url2, node2.GetBookmarkSpecifics().url()); 1898 EXPECT_EQ(url2, node2.GetBookmarkSpecifics().url());
1900 } 1899 }
1901 1900
1902 { 1901 {
1903 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1902 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1904 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( 1903 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1905 trans.GetWrappedTrans(), 1904 trans.GetWrappedTrans(),
1906 trans.GetCryptographer(), 1905 trans.GetCryptographer(),
1907 syncer::BOOKMARKS, 1906 BOOKMARKS,
1908 false /* not encrypted */)); 1907 false /* not encrypted */));
1909 } 1908 }
1910 1909
1911 EXPECT_CALL(observer_, 1910 EXPECT_CALL(observer_,
1912 OnEncryptedTypesChanged( 1911 OnEncryptedTypesChanged(
1913 HasModelTypes(syncer::ModelTypeSet::All()), true)); 1912 HasModelTypes(ModelTypeSet::All()), true));
1914 EXPECT_CALL(observer_, OnEncryptionComplete()); 1913 EXPECT_CALL(observer_, OnEncryptionComplete());
1915 sync_manager_.EnableEncryptEverything(); 1914 sync_manager_.EnableEncryptEverything();
1916 EXPECT_TRUE(sync_manager_.EncryptEverythingEnabledForTest()); 1915 EXPECT_TRUE(sync_manager_.EncryptEverythingEnabledForTest());
1917 1916
1918 { 1917 {
1919 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1918 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1920 EXPECT_TRUE(GetEncryptedTypes(&trans).Equals(syncer::ModelTypeSet::All())); 1919 EXPECT_TRUE(GetEncryptedTypes(&trans).Equals(ModelTypeSet::All()));
1921 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( 1920 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1922 trans.GetWrappedTrans(), 1921 trans.GetWrappedTrans(),
1923 trans.GetCryptographer(), 1922 trans.GetCryptographer(),
1924 syncer::BOOKMARKS, 1923 BOOKMARKS,
1925 true /* is encrypted */)); 1924 true /* is encrypted */));
1926 1925
1927 ReadNode node(&trans); 1926 ReadNode node(&trans);
1928 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(node_id1)); 1927 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(node_id1));
1929 EXPECT_EQ(syncer::BOOKMARKS, node.GetModelType()); 1928 EXPECT_EQ(BOOKMARKS, node.GetModelType());
1930 EXPECT_EQ(title, node.GetTitle()); 1929 EXPECT_EQ(title, node.GetTitle());
1931 EXPECT_EQ(title, node.GetBookmarkSpecifics().title()); 1930 EXPECT_EQ(title, node.GetBookmarkSpecifics().title());
1932 EXPECT_EQ(url, node.GetBookmarkSpecifics().url()); 1931 EXPECT_EQ(url, node.GetBookmarkSpecifics().url());
1933 1932
1934 ReadNode node2(&trans); 1933 ReadNode node2(&trans);
1935 EXPECT_EQ(BaseNode::INIT_OK, node2.InitByIdLookup(node_id2)); 1934 EXPECT_EQ(BaseNode::INIT_OK, node2.InitByIdLookup(node_id2));
1936 EXPECT_EQ(syncer::BOOKMARKS, node2.GetModelType()); 1935 EXPECT_EQ(BOOKMARKS, node2.GetModelType());
1937 // We should de-canonicalize the title in GetTitle(), but the title in the 1936 // We should de-canonicalize the title in GetTitle(), but the title in the
1938 // specifics should be stored in the server legal form. 1937 // specifics should be stored in the server legal form.
1939 EXPECT_EQ(raw_title2, node2.GetTitle()); 1938 EXPECT_EQ(raw_title2, node2.GetTitle());
1940 EXPECT_EQ(title2, node2.GetBookmarkSpecifics().title()); 1939 EXPECT_EQ(title2, node2.GetBookmarkSpecifics().title());
1941 EXPECT_EQ(url2, node2.GetBookmarkSpecifics().url()); 1940 EXPECT_EQ(url2, node2.GetBookmarkSpecifics().url());
1942 } 1941 }
1943 } 1942 }
1944 1943
1945 // Create a bookmark and set the title/url, then verify the data was properly 1944 // Create a bookmark and set the title/url, then verify the data was properly
1946 // set. This replicates the unique way bookmarks have of creating sync nodes. 1945 // set. This replicates the unique way bookmarks have of creating sync nodes.
1947 // See BookmarkChangeProcessor::PlaceSyncNode(..). 1946 // See BookmarkChangeProcessor::PlaceSyncNode(..).
1948 TEST_F(SyncManagerTest, CreateLocalBookmark) { 1947 TEST_F(SyncManagerTest, CreateLocalBookmark) {
1949 std::string title = "title"; 1948 std::string title = "title";
1950 GURL url("url"); 1949 GURL url("url");
1951 { 1950 {
1952 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1951 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1953 ReadNode root_node(&trans); 1952 ReadNode root_node(&trans);
1954 root_node.InitByRootLookup(); 1953 root_node.InitByRootLookup();
1955 WriteNode node(&trans); 1954 WriteNode node(&trans);
1956 ASSERT_TRUE(node.InitByCreation(syncer::BOOKMARKS, root_node, NULL)); 1955 ASSERT_TRUE(node.InitByCreation(BOOKMARKS, root_node, NULL));
1957 node.SetIsFolder(false); 1956 node.SetIsFolder(false);
1958 node.SetTitle(UTF8ToWide(title)); 1957 node.SetTitle(UTF8ToWide(title));
1959 node.SetURL(url); 1958 node.SetURL(url);
1960 } 1959 }
1961 { 1960 {
1962 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1961 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1963 ReadNode root_node(&trans); 1962 ReadNode root_node(&trans);
1964 root_node.InitByRootLookup(); 1963 root_node.InitByRootLookup();
1965 int64 child_id = root_node.GetFirstChildId(); 1964 int64 child_id = root_node.GetFirstChildId();
1966 1965
1967 ReadNode node(&trans); 1966 ReadNode node(&trans);
1968 ASSERT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(child_id)); 1967 ASSERT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(child_id));
1969 EXPECT_FALSE(node.GetIsFolder()); 1968 EXPECT_FALSE(node.GetIsFolder());
1970 EXPECT_EQ(title, node.GetTitle()); 1969 EXPECT_EQ(title, node.GetTitle());
1971 EXPECT_EQ(url, node.GetURL()); 1970 EXPECT_EQ(url, node.GetURL());
1972 } 1971 }
1973 } 1972 }
1974 1973
1975 // Verifies WriteNode::UpdateEntryWithEncryption does not make unnecessary 1974 // Verifies WriteNode::UpdateEntryWithEncryption does not make unnecessary
1976 // changes. 1975 // changes.
1977 TEST_F(SyncManagerTest, UpdateEntryWithEncryption) { 1976 TEST_F(SyncManagerTest, UpdateEntryWithEncryption) {
1978 std::string client_tag = "title"; 1977 std::string client_tag = "title";
1979 sync_pb::EntitySpecifics entity_specifics; 1978 sync_pb::EntitySpecifics entity_specifics;
1980 entity_specifics.mutable_bookmark()->set_url("url"); 1979 entity_specifics.mutable_bookmark()->set_url("url");
1981 entity_specifics.mutable_bookmark()->set_title("title"); 1980 entity_specifics.mutable_bookmark()->set_title("title");
1982 MakeServerNode(sync_manager_.GetUserShare(), syncer::BOOKMARKS, client_tag, 1981 MakeServerNode(sync_manager_.GetUserShare(), BOOKMARKS, client_tag,
1983 BaseNode::GenerateSyncableHash(syncer::BOOKMARKS, 1982 BaseNode::GenerateSyncableHash(BOOKMARKS,
1984 client_tag), 1983 client_tag),
1985 entity_specifics); 1984 entity_specifics);
1986 // New node shouldn't start off unsynced. 1985 // New node shouldn't start off unsynced.
1987 EXPECT_FALSE(ResetUnsyncedEntry(syncer::BOOKMARKS, client_tag)); 1986 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
1988 // Manually change to the same data. Should not set is_unsynced. 1987 // Manually change to the same data. Should not set is_unsynced.
1989 { 1988 {
1990 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1989 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1991 WriteNode node(&trans); 1990 WriteNode node(&trans);
1992 EXPECT_EQ(BaseNode::INIT_OK, 1991 EXPECT_EQ(BaseNode::INIT_OK,
1993 node.InitByClientTagLookup(syncer::BOOKMARKS, client_tag)); 1992 node.InitByClientTagLookup(BOOKMARKS, client_tag));
1994 node.SetEntitySpecifics(entity_specifics); 1993 node.SetEntitySpecifics(entity_specifics);
1995 } 1994 }
1996 EXPECT_FALSE(ResetUnsyncedEntry(syncer::BOOKMARKS, client_tag)); 1995 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
1997 1996
1998 // Encrypt the datatatype, should set is_unsynced. 1997 // Encrypt the datatatype, should set is_unsynced.
1999 EXPECT_CALL(observer_, 1998 EXPECT_CALL(observer_,
2000 OnEncryptedTypesChanged( 1999 OnEncryptedTypesChanged(
2001 HasModelTypes(syncer::ModelTypeSet::All()), true)); 2000 HasModelTypes(ModelTypeSet::All()), true));
2002 EXPECT_CALL(observer_, OnEncryptionComplete()); 2001 EXPECT_CALL(observer_, OnEncryptionComplete());
2003 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, FULL_ENCRYPTION)); 2002 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, FULL_ENCRYPTION));
2004 2003
2005 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing)); 2004 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing));
2006 PumpLoop(); 2005 PumpLoop();
2007 { 2006 {
2008 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2007 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2009 ReadNode node(&trans); 2008 ReadNode node(&trans);
2010 EXPECT_EQ(BaseNode::INIT_OK, 2009 EXPECT_EQ(BaseNode::INIT_OK,
2011 node.InitByClientTagLookup(syncer::BOOKMARKS, client_tag)); 2010 node.InitByClientTagLookup(BOOKMARKS, client_tag));
2012 const syncable::Entry* node_entry = node.GetEntry(); 2011 const syncable::Entry* node_entry = node.GetEntry();
2013 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); 2012 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS);
2014 EXPECT_TRUE(specifics.has_encrypted()); 2013 EXPECT_TRUE(specifics.has_encrypted());
2015 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); 2014 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME));
2016 Cryptographer* cryptographer = trans.GetCryptographer(); 2015 Cryptographer* cryptographer = trans.GetCryptographer();
2017 EXPECT_TRUE(cryptographer->is_ready()); 2016 EXPECT_TRUE(cryptographer->is_ready());
2018 EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey( 2017 EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey(
2019 specifics.encrypted())); 2018 specifics.encrypted()));
2020 } 2019 }
2021 EXPECT_TRUE(ResetUnsyncedEntry(syncer::BOOKMARKS, client_tag)); 2020 EXPECT_TRUE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
2022 2021
2023 // Set a new passphrase. Should set is_unsynced. 2022 // Set a new passphrase. Should set is_unsynced.
2024 testing::Mock::VerifyAndClearExpectations(&observer_); 2023 testing::Mock::VerifyAndClearExpectations(&observer_);
2025 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_)); 2024 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_));
2026 EXPECT_CALL(observer_, OnPassphraseAccepted()); 2025 EXPECT_CALL(observer_, OnPassphraseAccepted());
2027 EXPECT_CALL(observer_, OnEncryptionComplete()); 2026 EXPECT_CALL(observer_, OnEncryptionComplete());
2028 sync_manager_.SetEncryptionPassphrase("new_passphrase", true); 2027 sync_manager_.SetEncryptionPassphrase("new_passphrase", true);
2029 { 2028 {
2030 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2029 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2031 ReadNode node(&trans); 2030 ReadNode node(&trans);
2032 EXPECT_EQ(BaseNode::INIT_OK, 2031 EXPECT_EQ(BaseNode::INIT_OK,
2033 node.InitByClientTagLookup(syncer::BOOKMARKS, client_tag)); 2032 node.InitByClientTagLookup(BOOKMARKS, client_tag));
2034 const syncable::Entry* node_entry = node.GetEntry(); 2033 const syncable::Entry* node_entry = node.GetEntry();
2035 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); 2034 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS);
2036 EXPECT_TRUE(specifics.has_encrypted()); 2035 EXPECT_TRUE(specifics.has_encrypted());
2037 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); 2036 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME));
2038 Cryptographer* cryptographer = trans.GetCryptographer(); 2037 Cryptographer* cryptographer = trans.GetCryptographer();
2039 EXPECT_TRUE(cryptographer->is_ready()); 2038 EXPECT_TRUE(cryptographer->is_ready());
2040 EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey( 2039 EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey(
2041 specifics.encrypted())); 2040 specifics.encrypted()));
2042 } 2041 }
2043 EXPECT_TRUE(ResetUnsyncedEntry(syncer::BOOKMARKS, client_tag)); 2042 EXPECT_TRUE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
2044 2043
2045 // Force a re-encrypt everything. Should not set is_unsynced. 2044 // Force a re-encrypt everything. Should not set is_unsynced.
2046 testing::Mock::VerifyAndClearExpectations(&observer_); 2045 testing::Mock::VerifyAndClearExpectations(&observer_);
2047 EXPECT_CALL(observer_, OnEncryptionComplete()); 2046 EXPECT_CALL(observer_, OnEncryptionComplete());
2048 2047
2049 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing)); 2048 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing));
2050 PumpLoop(); 2049 PumpLoop();
2051 2050
2052 { 2051 {
2053 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2052 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2054 ReadNode node(&trans); 2053 ReadNode node(&trans);
2055 EXPECT_EQ(BaseNode::INIT_OK, 2054 EXPECT_EQ(BaseNode::INIT_OK,
2056 node.InitByClientTagLookup(syncer::BOOKMARKS, client_tag)); 2055 node.InitByClientTagLookup(BOOKMARKS, client_tag));
2057 const syncable::Entry* node_entry = node.GetEntry(); 2056 const syncable::Entry* node_entry = node.GetEntry();
2058 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); 2057 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS);
2059 EXPECT_TRUE(specifics.has_encrypted()); 2058 EXPECT_TRUE(specifics.has_encrypted());
2060 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); 2059 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME));
2061 Cryptographer* cryptographer = trans.GetCryptographer(); 2060 Cryptographer* cryptographer = trans.GetCryptographer();
2062 EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey( 2061 EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey(
2063 specifics.encrypted())); 2062 specifics.encrypted()));
2064 } 2063 }
2065 EXPECT_FALSE(ResetUnsyncedEntry(syncer::BOOKMARKS, client_tag)); 2064 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
2066 2065
2067 // Manually change to the same data. Should not set is_unsynced. 2066 // Manually change to the same data. Should not set is_unsynced.
2068 { 2067 {
2069 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2068 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2070 WriteNode node(&trans); 2069 WriteNode node(&trans);
2071 EXPECT_EQ(BaseNode::INIT_OK, 2070 EXPECT_EQ(BaseNode::INIT_OK,
2072 node.InitByClientTagLookup(syncer::BOOKMARKS, client_tag)); 2071 node.InitByClientTagLookup(BOOKMARKS, client_tag));
2073 node.SetEntitySpecifics(entity_specifics); 2072 node.SetEntitySpecifics(entity_specifics);
2074 const syncable::Entry* node_entry = node.GetEntry(); 2073 const syncable::Entry* node_entry = node.GetEntry();
2075 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); 2074 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS);
2076 EXPECT_TRUE(specifics.has_encrypted()); 2075 EXPECT_TRUE(specifics.has_encrypted());
2077 EXPECT_FALSE(node_entry->Get(IS_UNSYNCED)); 2076 EXPECT_FALSE(node_entry->Get(IS_UNSYNCED));
2078 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); 2077 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME));
2079 Cryptographer* cryptographer = trans.GetCryptographer(); 2078 Cryptographer* cryptographer = trans.GetCryptographer();
2080 EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey( 2079 EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey(
2081 specifics.encrypted())); 2080 specifics.encrypted()));
2082 } 2081 }
2083 EXPECT_FALSE(ResetUnsyncedEntry(syncer::BOOKMARKS, client_tag)); 2082 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
2084 2083
2085 // Manually change to different data. Should set is_unsynced. 2084 // Manually change to different data. Should set is_unsynced.
2086 { 2085 {
2087 entity_specifics.mutable_bookmark()->set_url("url2"); 2086 entity_specifics.mutable_bookmark()->set_url("url2");
2088 entity_specifics.mutable_bookmark()->set_title("title2"); 2087 entity_specifics.mutable_bookmark()->set_title("title2");
2089 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2088 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2090 WriteNode node(&trans); 2089 WriteNode node(&trans);
2091 EXPECT_EQ(BaseNode::INIT_OK, 2090 EXPECT_EQ(BaseNode::INIT_OK,
2092 node.InitByClientTagLookup(syncer::BOOKMARKS, client_tag)); 2091 node.InitByClientTagLookup(BOOKMARKS, client_tag));
2093 node.SetEntitySpecifics(entity_specifics); 2092 node.SetEntitySpecifics(entity_specifics);
2094 const syncable::Entry* node_entry = node.GetEntry(); 2093 const syncable::Entry* node_entry = node.GetEntry();
2095 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); 2094 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS);
2096 EXPECT_TRUE(specifics.has_encrypted()); 2095 EXPECT_TRUE(specifics.has_encrypted());
2097 EXPECT_TRUE(node_entry->Get(IS_UNSYNCED)); 2096 EXPECT_TRUE(node_entry->Get(IS_UNSYNCED));
2098 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); 2097 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME));
2099 Cryptographer* cryptographer = trans.GetCryptographer(); 2098 Cryptographer* cryptographer = trans.GetCryptographer();
2100 EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey( 2099 EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey(
2101 specifics.encrypted())); 2100 specifics.encrypted()));
2102 } 2101 }
2103 } 2102 }
2104 2103
2105 // Passwords have their own handling for encryption. Verify it does not result 2104 // Passwords have their own handling for encryption. Verify it does not result
2106 // in unnecessary writes via SetEntitySpecifics. 2105 // in unnecessary writes via SetEntitySpecifics.
2107 TEST_F(SyncManagerTest, UpdatePasswordSetEntitySpecificsNoChange) { 2106 TEST_F(SyncManagerTest, UpdatePasswordSetEntitySpecificsNoChange) {
2108 std::string client_tag = "title"; 2107 std::string client_tag = "title";
2109 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); 2108 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
2110 sync_pb::EntitySpecifics entity_specifics; 2109 sync_pb::EntitySpecifics entity_specifics;
2111 { 2110 {
2112 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2111 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2113 Cryptographer* cryptographer = trans.GetCryptographer(); 2112 Cryptographer* cryptographer = trans.GetCryptographer();
2114 sync_pb::PasswordSpecificsData data; 2113 sync_pb::PasswordSpecificsData data;
2115 data.set_password_value("secret"); 2114 data.set_password_value("secret");
2116 cryptographer->Encrypt( 2115 cryptographer->Encrypt(
2117 data, 2116 data,
2118 entity_specifics.mutable_password()-> 2117 entity_specifics.mutable_password()->
2119 mutable_encrypted()); 2118 mutable_encrypted());
2120 } 2119 }
2121 MakeServerNode(sync_manager_.GetUserShare(), syncer::PASSWORDS, client_tag, 2120 MakeServerNode(sync_manager_.GetUserShare(), PASSWORDS, client_tag,
2122 BaseNode::GenerateSyncableHash(syncer::PASSWORDS, 2121 BaseNode::GenerateSyncableHash(PASSWORDS,
2123 client_tag), 2122 client_tag),
2124 entity_specifics); 2123 entity_specifics);
2125 // New node shouldn't start off unsynced. 2124 // New node shouldn't start off unsynced.
2126 EXPECT_FALSE(ResetUnsyncedEntry(syncer::PASSWORDS, client_tag)); 2125 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag));
2127 2126
2128 // Manually change to the same data via SetEntitySpecifics. Should not set 2127 // Manually change to the same data via SetEntitySpecifics. Should not set
2129 // is_unsynced. 2128 // is_unsynced.
2130 { 2129 {
2131 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2130 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2132 WriteNode node(&trans); 2131 WriteNode node(&trans);
2133 EXPECT_EQ(BaseNode::INIT_OK, 2132 EXPECT_EQ(BaseNode::INIT_OK,
2134 node.InitByClientTagLookup(syncer::PASSWORDS, client_tag)); 2133 node.InitByClientTagLookup(PASSWORDS, client_tag));
2135 node.SetEntitySpecifics(entity_specifics); 2134 node.SetEntitySpecifics(entity_specifics);
2136 } 2135 }
2137 EXPECT_FALSE(ResetUnsyncedEntry(syncer::PASSWORDS, client_tag)); 2136 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag));
2138 } 2137 }
2139 2138
2140 // Passwords have their own handling for encryption. Verify it does not result 2139 // Passwords have their own handling for encryption. Verify it does not result
2141 // in unnecessary writes via SetPasswordSpecifics. 2140 // in unnecessary writes via SetPasswordSpecifics.
2142 TEST_F(SyncManagerTest, UpdatePasswordSetPasswordSpecifics) { 2141 TEST_F(SyncManagerTest, UpdatePasswordSetPasswordSpecifics) {
2143 std::string client_tag = "title"; 2142 std::string client_tag = "title";
2144 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); 2143 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
2145 sync_pb::EntitySpecifics entity_specifics; 2144 sync_pb::EntitySpecifics entity_specifics;
2146 { 2145 {
2147 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2146 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2148 Cryptographer* cryptographer = trans.GetCryptographer(); 2147 Cryptographer* cryptographer = trans.GetCryptographer();
2149 sync_pb::PasswordSpecificsData data; 2148 sync_pb::PasswordSpecificsData data;
2150 data.set_password_value("secret"); 2149 data.set_password_value("secret");
2151 cryptographer->Encrypt( 2150 cryptographer->Encrypt(
2152 data, 2151 data,
2153 entity_specifics.mutable_password()-> 2152 entity_specifics.mutable_password()->
2154 mutable_encrypted()); 2153 mutable_encrypted());
2155 } 2154 }
2156 MakeServerNode(sync_manager_.GetUserShare(), syncer::PASSWORDS, client_tag, 2155 MakeServerNode(sync_manager_.GetUserShare(), PASSWORDS, client_tag,
2157 BaseNode::GenerateSyncableHash(syncer::PASSWORDS, 2156 BaseNode::GenerateSyncableHash(PASSWORDS,
2158 client_tag), 2157 client_tag),
2159 entity_specifics); 2158 entity_specifics);
2160 // New node shouldn't start off unsynced. 2159 // New node shouldn't start off unsynced.
2161 EXPECT_FALSE(ResetUnsyncedEntry(syncer::PASSWORDS, client_tag)); 2160 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag));
2162 2161
2163 // Manually change to the same data via SetPasswordSpecifics. Should not set 2162 // Manually change to the same data via SetPasswordSpecifics. Should not set
2164 // is_unsynced. 2163 // is_unsynced.
2165 { 2164 {
2166 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2165 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2167 WriteNode node(&trans); 2166 WriteNode node(&trans);
2168 EXPECT_EQ(BaseNode::INIT_OK, 2167 EXPECT_EQ(BaseNode::INIT_OK,
2169 node.InitByClientTagLookup(syncer::PASSWORDS, client_tag)); 2168 node.InitByClientTagLookup(PASSWORDS, client_tag));
2170 node.SetPasswordSpecifics(node.GetPasswordSpecifics()); 2169 node.SetPasswordSpecifics(node.GetPasswordSpecifics());
2171 } 2170 }
2172 EXPECT_FALSE(ResetUnsyncedEntry(syncer::PASSWORDS, client_tag)); 2171 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag));
2173 2172
2174 // Manually change to different data. Should set is_unsynced. 2173 // Manually change to different data. Should set is_unsynced.
2175 { 2174 {
2176 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2175 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2177 WriteNode node(&trans); 2176 WriteNode node(&trans);
2178 EXPECT_EQ(BaseNode::INIT_OK, 2177 EXPECT_EQ(BaseNode::INIT_OK,
2179 node.InitByClientTagLookup(syncer::PASSWORDS, client_tag)); 2178 node.InitByClientTagLookup(PASSWORDS, client_tag));
2180 Cryptographer* cryptographer = trans.GetCryptographer(); 2179 Cryptographer* cryptographer = trans.GetCryptographer();
2181 sync_pb::PasswordSpecificsData data; 2180 sync_pb::PasswordSpecificsData data;
2182 data.set_password_value("secret2"); 2181 data.set_password_value("secret2");
2183 cryptographer->Encrypt( 2182 cryptographer->Encrypt(
2184 data, 2183 data,
2185 entity_specifics.mutable_password()->mutable_encrypted()); 2184 entity_specifics.mutable_password()->mutable_encrypted());
2186 node.SetPasswordSpecifics(data); 2185 node.SetPasswordSpecifics(data);
2187 const syncable::Entry* node_entry = node.GetEntry(); 2186 const syncable::Entry* node_entry = node.GetEntry();
2188 EXPECT_TRUE(node_entry->Get(IS_UNSYNCED)); 2187 EXPECT_TRUE(node_entry->Get(IS_UNSYNCED));
2189 } 2188 }
2190 } 2189 }
2191 2190
2192 // Passwords have their own handling for encryption. Verify setting a new 2191 // Passwords have their own handling for encryption. Verify setting a new
2193 // passphrase updates the data. 2192 // passphrase updates the data.
2194 TEST_F(SyncManagerTest, UpdatePasswordNewPassphrase) { 2193 TEST_F(SyncManagerTest, UpdatePasswordNewPassphrase) {
2195 std::string client_tag = "title"; 2194 std::string client_tag = "title";
2196 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); 2195 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
2197 sync_pb::EntitySpecifics entity_specifics; 2196 sync_pb::EntitySpecifics entity_specifics;
2198 { 2197 {
2199 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2198 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2200 Cryptographer* cryptographer = trans.GetCryptographer(); 2199 Cryptographer* cryptographer = trans.GetCryptographer();
2201 sync_pb::PasswordSpecificsData data; 2200 sync_pb::PasswordSpecificsData data;
2202 data.set_password_value("secret"); 2201 data.set_password_value("secret");
2203 cryptographer->Encrypt( 2202 cryptographer->Encrypt(
2204 data, 2203 data,
2205 entity_specifics.mutable_password()->mutable_encrypted()); 2204 entity_specifics.mutable_password()->mutable_encrypted());
2206 } 2205 }
2207 MakeServerNode(sync_manager_.GetUserShare(), syncer::PASSWORDS, client_tag, 2206 MakeServerNode(sync_manager_.GetUserShare(), PASSWORDS, client_tag,
2208 BaseNode::GenerateSyncableHash(syncer::PASSWORDS, 2207 BaseNode::GenerateSyncableHash(PASSWORDS,
2209 client_tag), 2208 client_tag),
2210 entity_specifics); 2209 entity_specifics);
2211 // New node shouldn't start off unsynced. 2210 // New node shouldn't start off unsynced.
2212 EXPECT_FALSE(ResetUnsyncedEntry(syncer::PASSWORDS, client_tag)); 2211 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag));
2213 2212
2214 // Set a new passphrase. Should set is_unsynced. 2213 // Set a new passphrase. Should set is_unsynced.
2215 testing::Mock::VerifyAndClearExpectations(&observer_); 2214 testing::Mock::VerifyAndClearExpectations(&observer_);
2216 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_)); 2215 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_));
2217 EXPECT_CALL(observer_, OnPassphraseAccepted()); 2216 EXPECT_CALL(observer_, OnPassphraseAccepted());
2218 EXPECT_CALL(observer_, OnEncryptionComplete()); 2217 EXPECT_CALL(observer_, OnEncryptionComplete());
2219 sync_manager_.SetEncryptionPassphrase("new_passphrase", true); 2218 sync_manager_.SetEncryptionPassphrase("new_passphrase", true);
2220 EXPECT_TRUE(ResetUnsyncedEntry(syncer::PASSWORDS, client_tag)); 2219 EXPECT_TRUE(ResetUnsyncedEntry(PASSWORDS, client_tag));
2221 } 2220 }
2222 2221
2223 // Passwords have their own handling for encryption. Verify it does not result 2222 // Passwords have their own handling for encryption. Verify it does not result
2224 // in unnecessary writes via ReencryptEverything. 2223 // in unnecessary writes via ReencryptEverything.
2225 TEST_F(SyncManagerTest, UpdatePasswordReencryptEverything) { 2224 TEST_F(SyncManagerTest, UpdatePasswordReencryptEverything) {
2226 std::string client_tag = "title"; 2225 std::string client_tag = "title";
2227 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); 2226 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
2228 sync_pb::EntitySpecifics entity_specifics; 2227 sync_pb::EntitySpecifics entity_specifics;
2229 { 2228 {
2230 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2229 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2231 Cryptographer* cryptographer = trans.GetCryptographer(); 2230 Cryptographer* cryptographer = trans.GetCryptographer();
2232 sync_pb::PasswordSpecificsData data; 2231 sync_pb::PasswordSpecificsData data;
2233 data.set_password_value("secret"); 2232 data.set_password_value("secret");
2234 cryptographer->Encrypt( 2233 cryptographer->Encrypt(
2235 data, 2234 data,
2236 entity_specifics.mutable_password()->mutable_encrypted()); 2235 entity_specifics.mutable_password()->mutable_encrypted());
2237 } 2236 }
2238 MakeServerNode(sync_manager_.GetUserShare(), syncer::PASSWORDS, client_tag, 2237 MakeServerNode(sync_manager_.GetUserShare(), PASSWORDS, client_tag,
2239 BaseNode::GenerateSyncableHash(syncer::PASSWORDS, 2238 BaseNode::GenerateSyncableHash(PASSWORDS,
2240 client_tag), 2239 client_tag),
2241 entity_specifics); 2240 entity_specifics);
2242 // New node shouldn't start off unsynced. 2241 // New node shouldn't start off unsynced.
2243 EXPECT_FALSE(ResetUnsyncedEntry(syncer::PASSWORDS, client_tag)); 2242 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag));
2244 2243
2245 // Force a re-encrypt everything. Should not set is_unsynced. 2244 // Force a re-encrypt everything. Should not set is_unsynced.
2246 testing::Mock::VerifyAndClearExpectations(&observer_); 2245 testing::Mock::VerifyAndClearExpectations(&observer_);
2247 EXPECT_CALL(observer_, OnEncryptionComplete()); 2246 EXPECT_CALL(observer_, OnEncryptionComplete());
2248 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing)); 2247 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing));
2249 PumpLoop(); 2248 PumpLoop();
2250 EXPECT_FALSE(ResetUnsyncedEntry(syncer::PASSWORDS, client_tag)); 2249 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag));
2251 } 2250 }
2252 2251
2253 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for bookmarks 2252 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for bookmarks
2254 // when we write the same data, but does set it when we write new data. 2253 // when we write the same data, but does set it when we write new data.
2255 TEST_F(SyncManagerTest, SetBookmarkTitle) { 2254 TEST_F(SyncManagerTest, SetBookmarkTitle) {
2256 std::string client_tag = "title"; 2255 std::string client_tag = "title";
2257 sync_pb::EntitySpecifics entity_specifics; 2256 sync_pb::EntitySpecifics entity_specifics;
2258 entity_specifics.mutable_bookmark()->set_url("url"); 2257 entity_specifics.mutable_bookmark()->set_url("url");
2259 entity_specifics.mutable_bookmark()->set_title("title"); 2258 entity_specifics.mutable_bookmark()->set_title("title");
2260 MakeServerNode(sync_manager_.GetUserShare(), syncer::BOOKMARKS, client_tag, 2259 MakeServerNode(sync_manager_.GetUserShare(), BOOKMARKS, client_tag,
2261 BaseNode::GenerateSyncableHash(syncer::BOOKMARKS, 2260 BaseNode::GenerateSyncableHash(BOOKMARKS,
2262 client_tag), 2261 client_tag),
2263 entity_specifics); 2262 entity_specifics);
2264 // New node shouldn't start off unsynced. 2263 // New node shouldn't start off unsynced.
2265 EXPECT_FALSE(ResetUnsyncedEntry(syncer::BOOKMARKS, client_tag)); 2264 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
2266 2265
2267 // Manually change to the same title. Should not set is_unsynced. 2266 // Manually change to the same title. Should not set is_unsynced.
2268 { 2267 {
2269 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2268 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2270 WriteNode node(&trans); 2269 WriteNode node(&trans);
2271 EXPECT_EQ(BaseNode::INIT_OK, 2270 EXPECT_EQ(BaseNode::INIT_OK,
2272 node.InitByClientTagLookup(syncer::BOOKMARKS, client_tag)); 2271 node.InitByClientTagLookup(BOOKMARKS, client_tag));
2273 node.SetTitle(UTF8ToWide(client_tag)); 2272 node.SetTitle(UTF8ToWide(client_tag));
2274 } 2273 }
2275 EXPECT_FALSE(ResetUnsyncedEntry(syncer::BOOKMARKS, client_tag)); 2274 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
2276 2275
2277 // Manually change to new title. Should set is_unsynced. 2276 // Manually change to new title. Should set is_unsynced.
2278 { 2277 {
2279 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2278 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2280 WriteNode node(&trans); 2279 WriteNode node(&trans);
2281 EXPECT_EQ(BaseNode::INIT_OK, 2280 EXPECT_EQ(BaseNode::INIT_OK,
2282 node.InitByClientTagLookup(syncer::BOOKMARKS, client_tag)); 2281 node.InitByClientTagLookup(BOOKMARKS, client_tag));
2283 node.SetTitle(UTF8ToWide("title2")); 2282 node.SetTitle(UTF8ToWide("title2"));
2284 } 2283 }
2285 EXPECT_TRUE(ResetUnsyncedEntry(syncer::BOOKMARKS, client_tag)); 2284 EXPECT_TRUE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
2286 } 2285 }
2287 2286
2288 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for encrypted 2287 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for encrypted
2289 // bookmarks when we write the same data, but does set it when we write new 2288 // bookmarks when we write the same data, but does set it when we write new
2290 // data. 2289 // data.
2291 TEST_F(SyncManagerTest, SetBookmarkTitleWithEncryption) { 2290 TEST_F(SyncManagerTest, SetBookmarkTitleWithEncryption) {
2292 std::string client_tag = "title"; 2291 std::string client_tag = "title";
2293 sync_pb::EntitySpecifics entity_specifics; 2292 sync_pb::EntitySpecifics entity_specifics;
2294 entity_specifics.mutable_bookmark()->set_url("url"); 2293 entity_specifics.mutable_bookmark()->set_url("url");
2295 entity_specifics.mutable_bookmark()->set_title("title"); 2294 entity_specifics.mutable_bookmark()->set_title("title");
2296 MakeServerNode(sync_manager_.GetUserShare(), syncer::BOOKMARKS, client_tag, 2295 MakeServerNode(sync_manager_.GetUserShare(), BOOKMARKS, client_tag,
2297 BaseNode::GenerateSyncableHash(syncer::BOOKMARKS, 2296 BaseNode::GenerateSyncableHash(BOOKMARKS,
2298 client_tag), 2297 client_tag),
2299 entity_specifics); 2298 entity_specifics);
2300 // New node shouldn't start off unsynced. 2299 // New node shouldn't start off unsynced.
2301 EXPECT_FALSE(ResetUnsyncedEntry(syncer::BOOKMARKS, client_tag)); 2300 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
2302 2301
2303 // Encrypt the datatatype, should set is_unsynced. 2302 // Encrypt the datatatype, should set is_unsynced.
2304 EXPECT_CALL(observer_, 2303 EXPECT_CALL(observer_,
2305 OnEncryptedTypesChanged( 2304 OnEncryptedTypesChanged(
2306 HasModelTypes(syncer::ModelTypeSet::All()), true)); 2305 HasModelTypes(ModelTypeSet::All()), true));
2307 EXPECT_CALL(observer_, OnEncryptionComplete()); 2306 EXPECT_CALL(observer_, OnEncryptionComplete());
2308 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, FULL_ENCRYPTION)); 2307 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, FULL_ENCRYPTION));
2309 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing)); 2308 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing));
2310 PumpLoop(); 2309 PumpLoop();
2311 EXPECT_TRUE(ResetUnsyncedEntry(syncer::BOOKMARKS, client_tag)); 2310 EXPECT_TRUE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
2312 2311
2313 // Manually change to the same title. Should not set is_unsynced. 2312 // Manually change to the same title. Should not set is_unsynced.
2314 // NON_UNIQUE_NAME should be kEncryptedString. 2313 // NON_UNIQUE_NAME should be kEncryptedString.
2315 { 2314 {
2316 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2315 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2317 WriteNode node(&trans); 2316 WriteNode node(&trans);
2318 EXPECT_EQ(BaseNode::INIT_OK, 2317 EXPECT_EQ(BaseNode::INIT_OK,
2319 node.InitByClientTagLookup(syncer::BOOKMARKS, client_tag)); 2318 node.InitByClientTagLookup(BOOKMARKS, client_tag));
2320 node.SetTitle(UTF8ToWide(client_tag)); 2319 node.SetTitle(UTF8ToWide(client_tag));
2321 const syncable::Entry* node_entry = node.GetEntry(); 2320 const syncable::Entry* node_entry = node.GetEntry();
2322 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); 2321 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS);
2323 EXPECT_TRUE(specifics.has_encrypted()); 2322 EXPECT_TRUE(specifics.has_encrypted());
2324 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); 2323 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME));
2325 } 2324 }
2326 EXPECT_FALSE(ResetUnsyncedEntry(syncer::BOOKMARKS, client_tag)); 2325 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
2327 2326
2328 // Manually change to new title. Should set is_unsynced. NON_UNIQUE_NAME 2327 // Manually change to new title. Should set is_unsynced. NON_UNIQUE_NAME
2329 // should still be kEncryptedString. 2328 // should still be kEncryptedString.
2330 { 2329 {
2331 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2330 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2332 WriteNode node(&trans); 2331 WriteNode node(&trans);
2333 EXPECT_EQ(BaseNode::INIT_OK, 2332 EXPECT_EQ(BaseNode::INIT_OK,
2334 node.InitByClientTagLookup(syncer::BOOKMARKS, client_tag)); 2333 node.InitByClientTagLookup(BOOKMARKS, client_tag));
2335 node.SetTitle(UTF8ToWide("title2")); 2334 node.SetTitle(UTF8ToWide("title2"));
2336 const syncable::Entry* node_entry = node.GetEntry(); 2335 const syncable::Entry* node_entry = node.GetEntry();
2337 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); 2336 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS);
2338 EXPECT_TRUE(specifics.has_encrypted()); 2337 EXPECT_TRUE(specifics.has_encrypted());
2339 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); 2338 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME));
2340 } 2339 }
2341 EXPECT_TRUE(ResetUnsyncedEntry(syncer::BOOKMARKS, client_tag)); 2340 EXPECT_TRUE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
2342 } 2341 }
2343 2342
2344 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for non-bookmarks 2343 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for non-bookmarks
2345 // when we write the same data, but does set it when we write new data. 2344 // when we write the same data, but does set it when we write new data.
2346 TEST_F(SyncManagerTest, SetNonBookmarkTitle) { 2345 TEST_F(SyncManagerTest, SetNonBookmarkTitle) {
2347 std::string client_tag = "title"; 2346 std::string client_tag = "title";
2348 sync_pb::EntitySpecifics entity_specifics; 2347 sync_pb::EntitySpecifics entity_specifics;
2349 entity_specifics.mutable_preference()->set_name("name"); 2348 entity_specifics.mutable_preference()->set_name("name");
2350 entity_specifics.mutable_preference()->set_value("value"); 2349 entity_specifics.mutable_preference()->set_value("value");
2351 MakeServerNode(sync_manager_.GetUserShare(), 2350 MakeServerNode(sync_manager_.GetUserShare(),
2352 syncer::PREFERENCES, 2351 PREFERENCES,
2353 client_tag, 2352 client_tag,
2354 BaseNode::GenerateSyncableHash(syncer::PREFERENCES, 2353 BaseNode::GenerateSyncableHash(PREFERENCES,
2355 client_tag), 2354 client_tag),
2356 entity_specifics); 2355 entity_specifics);
2357 // New node shouldn't start off unsynced. 2356 // New node shouldn't start off unsynced.
2358 EXPECT_FALSE(ResetUnsyncedEntry(syncer::PREFERENCES, client_tag)); 2357 EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES, client_tag));
2359 2358
2360 // Manually change to the same title. Should not set is_unsynced. 2359 // Manually change to the same title. Should not set is_unsynced.
2361 { 2360 {
2362 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2361 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2363 WriteNode node(&trans); 2362 WriteNode node(&trans);
2364 EXPECT_EQ(BaseNode::INIT_OK, 2363 EXPECT_EQ(BaseNode::INIT_OK,
2365 node.InitByClientTagLookup(syncer::PREFERENCES, client_tag)); 2364 node.InitByClientTagLookup(PREFERENCES, client_tag));
2366 node.SetTitle(UTF8ToWide(client_tag)); 2365 node.SetTitle(UTF8ToWide(client_tag));
2367 } 2366 }
2368 EXPECT_FALSE(ResetUnsyncedEntry(syncer::PREFERENCES, client_tag)); 2367 EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES, client_tag));
2369 2368
2370 // Manually change to new title. Should set is_unsynced. 2369 // Manually change to new title. Should set is_unsynced.
2371 { 2370 {
2372 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2371 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2373 WriteNode node(&trans); 2372 WriteNode node(&trans);
2374 EXPECT_EQ(BaseNode::INIT_OK, 2373 EXPECT_EQ(BaseNode::INIT_OK,
2375 node.InitByClientTagLookup(syncer::PREFERENCES, client_tag)); 2374 node.InitByClientTagLookup(PREFERENCES, client_tag));
2376 node.SetTitle(UTF8ToWide("title2")); 2375 node.SetTitle(UTF8ToWide("title2"));
2377 } 2376 }
2378 EXPECT_TRUE(ResetUnsyncedEntry(syncer::PREFERENCES, client_tag)); 2377 EXPECT_TRUE(ResetUnsyncedEntry(PREFERENCES, client_tag));
2379 } 2378 }
2380 2379
2381 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for encrypted 2380 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for encrypted
2382 // non-bookmarks when we write the same data or when we write new data 2381 // non-bookmarks when we write the same data or when we write new data
2383 // data (should remained kEncryptedString). 2382 // data (should remained kEncryptedString).
2384 TEST_F(SyncManagerTest, SetNonBookmarkTitleWithEncryption) { 2383 TEST_F(SyncManagerTest, SetNonBookmarkTitleWithEncryption) {
2385 std::string client_tag = "title"; 2384 std::string client_tag = "title";
2386 sync_pb::EntitySpecifics entity_specifics; 2385 sync_pb::EntitySpecifics entity_specifics;
2387 entity_specifics.mutable_preference()->set_name("name"); 2386 entity_specifics.mutable_preference()->set_name("name");
2388 entity_specifics.mutable_preference()->set_value("value"); 2387 entity_specifics.mutable_preference()->set_value("value");
2389 MakeServerNode(sync_manager_.GetUserShare(), 2388 MakeServerNode(sync_manager_.GetUserShare(),
2390 syncer::PREFERENCES, 2389 PREFERENCES,
2391 client_tag, 2390 client_tag,
2392 BaseNode::GenerateSyncableHash(syncer::PREFERENCES, 2391 BaseNode::GenerateSyncableHash(PREFERENCES,
2393 client_tag), 2392 client_tag),
2394 entity_specifics); 2393 entity_specifics);
2395 // New node shouldn't start off unsynced. 2394 // New node shouldn't start off unsynced.
2396 EXPECT_FALSE(ResetUnsyncedEntry(syncer::PREFERENCES, client_tag)); 2395 EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES, client_tag));
2397 2396
2398 // Encrypt the datatatype, should set is_unsynced. 2397 // Encrypt the datatatype, should set is_unsynced.
2399 EXPECT_CALL(observer_, 2398 EXPECT_CALL(observer_,
2400 OnEncryptedTypesChanged( 2399 OnEncryptedTypesChanged(
2401 HasModelTypes(syncer::ModelTypeSet::All()), true)); 2400 HasModelTypes(ModelTypeSet::All()), true));
2402 EXPECT_CALL(observer_, OnEncryptionComplete()); 2401 EXPECT_CALL(observer_, OnEncryptionComplete());
2403 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, FULL_ENCRYPTION)); 2402 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, FULL_ENCRYPTION));
2404 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing)); 2403 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing));
2405 PumpLoop(); 2404 PumpLoop();
2406 EXPECT_TRUE(ResetUnsyncedEntry(syncer::PREFERENCES, client_tag)); 2405 EXPECT_TRUE(ResetUnsyncedEntry(PREFERENCES, client_tag));
2407 2406
2408 // Manually change to the same title. Should not set is_unsynced. 2407 // Manually change to the same title. Should not set is_unsynced.
2409 // NON_UNIQUE_NAME should be kEncryptedString. 2408 // NON_UNIQUE_NAME should be kEncryptedString.
2410 { 2409 {
2411 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2410 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2412 WriteNode node(&trans); 2411 WriteNode node(&trans);
2413 EXPECT_EQ(BaseNode::INIT_OK, 2412 EXPECT_EQ(BaseNode::INIT_OK,
2414 node.InitByClientTagLookup(syncer::PREFERENCES, client_tag)); 2413 node.InitByClientTagLookup(PREFERENCES, client_tag));
2415 node.SetTitle(UTF8ToWide(client_tag)); 2414 node.SetTitle(UTF8ToWide(client_tag));
2416 const syncable::Entry* node_entry = node.GetEntry(); 2415 const syncable::Entry* node_entry = node.GetEntry();
2417 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); 2416 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS);
2418 EXPECT_TRUE(specifics.has_encrypted()); 2417 EXPECT_TRUE(specifics.has_encrypted());
2419 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); 2418 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME));
2420 } 2419 }
2421 EXPECT_FALSE(ResetUnsyncedEntry(syncer::PREFERENCES, client_tag)); 2420 EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES, client_tag));
2422 2421
2423 // Manually change to new title. Should not set is_unsynced because the 2422 // Manually change to new title. Should not set is_unsynced because the
2424 // NON_UNIQUE_NAME should still be kEncryptedString. 2423 // NON_UNIQUE_NAME should still be kEncryptedString.
2425 { 2424 {
2426 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2425 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2427 WriteNode node(&trans); 2426 WriteNode node(&trans);
2428 EXPECT_EQ(BaseNode::INIT_OK, 2427 EXPECT_EQ(BaseNode::INIT_OK,
2429 node.InitByClientTagLookup(syncer::PREFERENCES, client_tag)); 2428 node.InitByClientTagLookup(PREFERENCES, client_tag));
2430 node.SetTitle(UTF8ToWide("title2")); 2429 node.SetTitle(UTF8ToWide("title2"));
2431 const syncable::Entry* node_entry = node.GetEntry(); 2430 const syncable::Entry* node_entry = node.GetEntry();
2432 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); 2431 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS);
2433 EXPECT_TRUE(specifics.has_encrypted()); 2432 EXPECT_TRUE(specifics.has_encrypted());
2434 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); 2433 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME));
2435 EXPECT_FALSE(node_entry->Get(IS_UNSYNCED)); 2434 EXPECT_FALSE(node_entry->Get(IS_UNSYNCED));
2436 } 2435 }
2437 } 2436 }
2438 2437
2439 // Create an encrypted entry when the cryptographer doesn't think the type is 2438 // Create an encrypted entry when the cryptographer doesn't think the type is
2440 // marked for encryption. Ensure reads/writes don't break and don't unencrypt 2439 // marked for encryption. Ensure reads/writes don't break and don't unencrypt
2441 // the data. 2440 // the data.
2442 TEST_F(SyncManagerTest, SetPreviouslyEncryptedSpecifics) { 2441 TEST_F(SyncManagerTest, SetPreviouslyEncryptedSpecifics) {
2443 std::string client_tag = "tag"; 2442 std::string client_tag = "tag";
2444 std::string url = "url"; 2443 std::string url = "url";
2445 std::string url2 = "new_url"; 2444 std::string url2 = "new_url";
2446 std::string title = "title"; 2445 std::string title = "title";
2447 sync_pb::EntitySpecifics entity_specifics; 2446 sync_pb::EntitySpecifics entity_specifics;
2448 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); 2447 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
2449 { 2448 {
2450 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2449 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2451 syncer::Cryptographer* crypto = trans.GetCryptographer(); 2450 Cryptographer* crypto = trans.GetCryptographer();
2452 sync_pb::EntitySpecifics bm_specifics; 2451 sync_pb::EntitySpecifics bm_specifics;
2453 bm_specifics.mutable_bookmark()->set_title("title"); 2452 bm_specifics.mutable_bookmark()->set_title("title");
2454 bm_specifics.mutable_bookmark()->set_url("url"); 2453 bm_specifics.mutable_bookmark()->set_url("url");
2455 sync_pb::EncryptedData encrypted; 2454 sync_pb::EncryptedData encrypted;
2456 crypto->Encrypt(bm_specifics, &encrypted); 2455 crypto->Encrypt(bm_specifics, &encrypted);
2457 entity_specifics.mutable_encrypted()->CopyFrom(encrypted); 2456 entity_specifics.mutable_encrypted()->CopyFrom(encrypted);
2458 syncer::AddDefaultFieldValue(syncer::BOOKMARKS, &entity_specifics); 2457 AddDefaultFieldValue(BOOKMARKS, &entity_specifics);
2459 } 2458 }
2460 MakeServerNode(sync_manager_.GetUserShare(), syncer::BOOKMARKS, client_tag, 2459 MakeServerNode(sync_manager_.GetUserShare(), BOOKMARKS, client_tag,
2461 BaseNode::GenerateSyncableHash(syncer::BOOKMARKS, 2460 BaseNode::GenerateSyncableHash(BOOKMARKS,
2462 client_tag), 2461 client_tag),
2463 entity_specifics); 2462 entity_specifics);
2464 2463
2465 { 2464 {
2466 // Verify the data. 2465 // Verify the data.
2467 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2466 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2468 ReadNode node(&trans); 2467 ReadNode node(&trans);
2469 EXPECT_EQ(BaseNode::INIT_OK, 2468 EXPECT_EQ(BaseNode::INIT_OK,
2470 node.InitByClientTagLookup(syncer::BOOKMARKS, client_tag)); 2469 node.InitByClientTagLookup(BOOKMARKS, client_tag));
2471 EXPECT_EQ(title, node.GetTitle()); 2470 EXPECT_EQ(title, node.GetTitle());
2472 EXPECT_EQ(GURL(url), node.GetURL()); 2471 EXPECT_EQ(GURL(url), node.GetURL());
2473 } 2472 }
2474 2473
2475 { 2474 {
2476 // Overwrite the url (which overwrites the specifics). 2475 // Overwrite the url (which overwrites the specifics).
2477 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2476 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2478 WriteNode node(&trans); 2477 WriteNode node(&trans);
2479 EXPECT_EQ(BaseNode::INIT_OK, 2478 EXPECT_EQ(BaseNode::INIT_OK,
2480 node.InitByClientTagLookup(syncer::BOOKMARKS, client_tag)); 2479 node.InitByClientTagLookup(BOOKMARKS, client_tag));
2481 node.SetURL(GURL(url2)); 2480 node.SetURL(GURL(url2));
2482 } 2481 }
2483 2482
2484 { 2483 {
2485 // Verify it's still encrypted and it has the most recent url. 2484 // Verify it's still encrypted and it has the most recent url.
2486 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2485 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2487 ReadNode node(&trans); 2486 ReadNode node(&trans);
2488 EXPECT_EQ(BaseNode::INIT_OK, 2487 EXPECT_EQ(BaseNode::INIT_OK,
2489 node.InitByClientTagLookup(syncer::BOOKMARKS, client_tag)); 2488 node.InitByClientTagLookup(BOOKMARKS, client_tag));
2490 EXPECT_EQ(title, node.GetTitle()); 2489 EXPECT_EQ(title, node.GetTitle());
2491 EXPECT_EQ(GURL(url2), node.GetURL()); 2490 EXPECT_EQ(GURL(url2), node.GetURL());
2492 const syncable::Entry* node_entry = node.GetEntry(); 2491 const syncable::Entry* node_entry = node.GetEntry();
2493 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); 2492 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME));
2494 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); 2493 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS);
2495 EXPECT_TRUE(specifics.has_encrypted()); 2494 EXPECT_TRUE(specifics.has_encrypted());
2496 } 2495 }
2497 } 2496 }
2498 2497
2499 class MockSyncScheduler : public SyncScheduler { 2498 class MockSyncScheduler : public SyncScheduler {
2500 public: 2499 public:
2501 MockSyncScheduler() : SyncScheduler("name", NULL, NULL) {} 2500 MockSyncScheduler() : SyncScheduler("name", NULL, NULL) {}
2502 virtual ~MockSyncScheduler() {} 2501 virtual ~MockSyncScheduler() {}
2503 2502
2504 MOCK_METHOD1(Start, void(SyncScheduler::Mode)); 2503 MOCK_METHOD1(Start, void(SyncScheduler::Mode));
2505 MOCK_METHOD1(ScheduleConfiguration, bool(const ConfigurationParams&)); 2504 MOCK_METHOD1(ScheduleConfiguration, bool(const ConfigurationParams&));
2506 }; 2505 };
2507 2506
2508 // Test that the configuration params are properly created and sent to 2507 // Test that the configuration params are properly created and sent to
2509 // ScheduleConfigure. No callback should be invoked. 2508 // ScheduleConfigure. No callback should be invoked.
2510 TEST_F(SyncManagerTest, BasicConfiguration) { 2509 TEST_F(SyncManagerTest, BasicConfiguration) {
2511 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION; 2510 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION;
2512 syncer::ModelTypeSet types_to_download(syncer::BOOKMARKS, 2511 ModelTypeSet types_to_download(BOOKMARKS, PREFERENCES);
2513 syncer::PREFERENCES); 2512 ModelSafeRoutingInfo new_routing_info;
2514 syncer::ModelSafeRoutingInfo new_routing_info;
2515 GetModelSafeRoutingInfo(&new_routing_info); 2513 GetModelSafeRoutingInfo(&new_routing_info);
2516 2514
2517 scoped_ptr<MockSyncScheduler> scheduler(new MockSyncScheduler()); 2515 scoped_ptr<MockSyncScheduler> scheduler(new MockSyncScheduler());
2518 ConfigurationParams params; 2516 ConfigurationParams params;
2519 EXPECT_CALL(*scheduler, Start(SyncScheduler::CONFIGURATION_MODE)); 2517 EXPECT_CALL(*scheduler, Start(SyncScheduler::CONFIGURATION_MODE));
2520 EXPECT_CALL(*scheduler, ScheduleConfiguration(_)). 2518 EXPECT_CALL(*scheduler, ScheduleConfiguration(_)).
2521 WillOnce(DoAll(SaveArg<0>(&params), Return(true))); 2519 WillOnce(DoAll(SaveArg<0>(&params), Return(true)));
2522 SetScheduler(scheduler.PassAs<SyncScheduler>()); 2520 SetScheduler(scheduler.PassAs<SyncScheduler>());
2523 2521
2524 CallbackCounter ready_task_counter, retry_task_counter; 2522 CallbackCounter ready_task_counter, retry_task_counter;
2525 sync_manager_.ConfigureSyncer( 2523 sync_manager_.ConfigureSyncer(
2526 reason, 2524 reason,
2527 types_to_download, 2525 types_to_download,
2528 new_routing_info, 2526 new_routing_info,
2529 base::Bind(&CallbackCounter::Callback, 2527 base::Bind(&CallbackCounter::Callback,
2530 base::Unretained(&ready_task_counter)), 2528 base::Unretained(&ready_task_counter)),
2531 base::Bind(&CallbackCounter::Callback, 2529 base::Bind(&CallbackCounter::Callback,
2532 base::Unretained(&retry_task_counter))); 2530 base::Unretained(&retry_task_counter)));
2533 EXPECT_EQ(0, ready_task_counter.times_called()); 2531 EXPECT_EQ(0, ready_task_counter.times_called());
2534 EXPECT_EQ(0, retry_task_counter.times_called()); 2532 EXPECT_EQ(0, retry_task_counter.times_called());
2535 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION, 2533 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION,
2536 params.source); 2534 params.source);
2537 EXPECT_TRUE(types_to_download.Equals(params.types_to_download)); 2535 EXPECT_TRUE(types_to_download.Equals(params.types_to_download));
2538 EXPECT_EQ(new_routing_info, params.routing_info); 2536 EXPECT_EQ(new_routing_info, params.routing_info);
2539 } 2537 }
2540 2538
2541 // Test that the retry callback is invoked on configuration failure. 2539 // Test that the retry callback is invoked on configuration failure.
2542 TEST_F(SyncManagerTest, ConfigurationRetry) { 2540 TEST_F(SyncManagerTest, ConfigurationRetry) {
2543 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION; 2541 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION;
2544 syncer::ModelTypeSet types_to_download(syncer::BOOKMARKS, 2542 ModelTypeSet types_to_download(BOOKMARKS, PREFERENCES);
2545 syncer::PREFERENCES); 2543 ModelSafeRoutingInfo new_routing_info;
2546 syncer::ModelSafeRoutingInfo new_routing_info;
2547 GetModelSafeRoutingInfo(&new_routing_info); 2544 GetModelSafeRoutingInfo(&new_routing_info);
2548 2545
2549 scoped_ptr<MockSyncScheduler> scheduler(new MockSyncScheduler()); 2546 scoped_ptr<MockSyncScheduler> scheduler(new MockSyncScheduler());
2550 ConfigurationParams params; 2547 ConfigurationParams params;
2551 EXPECT_CALL(*scheduler, Start(SyncScheduler::CONFIGURATION_MODE)); 2548 EXPECT_CALL(*scheduler, Start(SyncScheduler::CONFIGURATION_MODE));
2552 EXPECT_CALL(*scheduler, ScheduleConfiguration(_)). 2549 EXPECT_CALL(*scheduler, ScheduleConfiguration(_)).
2553 WillOnce(DoAll(SaveArg<0>(&params), Return(false))); 2550 WillOnce(DoAll(SaveArg<0>(&params), Return(false)));
2554 SetScheduler(scheduler.PassAs<SyncScheduler>()); 2551 SetScheduler(scheduler.PassAs<SyncScheduler>());
2555 2552
2556 CallbackCounter ready_task_counter, retry_task_counter; 2553 CallbackCounter ready_task_counter, retry_task_counter;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
2606 // Ensure only bookmarks and nigori lost their progress marker. Preferences 2603 // Ensure only bookmarks and nigori lost their progress marker. Preferences
2607 // should still have it. 2604 // should still have it.
2608 partial_types = 2605 partial_types =
2609 sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All()); 2606 sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All());
2610 EXPECT_TRUE(partial_types.Has(NIGORI)); 2607 EXPECT_TRUE(partial_types.Has(NIGORI));
2611 EXPECT_TRUE(partial_types.Has(BOOKMARKS)); 2608 EXPECT_TRUE(partial_types.Has(BOOKMARKS));
2612 EXPECT_FALSE(partial_types.Has(PREFERENCES)); 2609 EXPECT_FALSE(partial_types.Has(PREFERENCES));
2613 } 2610 }
2614 2611
2615 } // namespace 2612 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698