OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Unit tests for the SyncApi. Note that a lot of the underlying | |
6 // functionality is provided by the Syncable layer, which has its own | |
7 // unit tests. We'll test SyncApi specific things in this harness. | |
8 | |
9 #include <cstddef> | |
10 #include <map> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/callback.h" | |
14 #include "base/compiler_specific.h" | |
15 #include "base/format_macros.h" | |
16 #include "base/location.h" | |
17 #include "base/memory/scoped_ptr.h" | |
18 #include "base/message_loop.h" | |
19 #include "base/message_loop_proxy.h" | |
20 #include "base/scoped_temp_dir.h" | |
21 #include "base/string_number_conversions.h" | |
22 #include "base/stringprintf.h" | |
23 #include "base/test/values_test_util.h" | |
24 #include "base/utf_string_conversions.h" | |
25 #include "base/values.h" | |
26 #include "chrome/browser/sync/internal_api/change_record.h" | |
27 #include "chrome/browser/sync/internal_api/http_post_provider_factory.h" | |
28 #include "chrome/browser/sync/internal_api/http_post_provider_interface.h" | |
29 #include "chrome/browser/sync/internal_api/read_node.h" | |
30 #include "chrome/browser/sync/internal_api/read_transaction.h" | |
31 #include "chrome/browser/sync/internal_api/sync_manager.h" | |
32 #include "chrome/browser/sync/internal_api/syncapi_internal.h" | |
33 #include "chrome/browser/sync/internal_api/write_node.h" | |
34 #include "chrome/browser/sync/internal_api/write_transaction.h" | |
35 #include "chrome/browser/sync/test/engine/test_user_share.h" | |
36 #include "sync/engine/model_safe_worker.h" | |
37 #include "sync/engine/nigori_util.h" | |
38 #include "sync/engine/polling_constants.h" | |
39 #include "sync/js/js_arg_list.h" | |
40 #include "sync/js/js_backend.h" | |
41 #include "sync/js/js_event_handler.h" | |
42 #include "sync/js/js_reply_handler.h" | |
43 #include "sync/js/js_test_util.h" | |
44 #include "sync/notifier/sync_notifier.h" | |
45 #include "sync/notifier/sync_notifier_observer.h" | |
46 #include "sync/protocol/bookmark_specifics.pb.h" | |
47 #include "sync/protocol/encryption.pb.h" | |
48 #include "sync/protocol/extension_specifics.pb.h" | |
49 #include "sync/protocol/password_specifics.pb.h" | |
50 #include "sync/protocol/preference_specifics.pb.h" | |
51 #include "sync/protocol/proto_value_conversions.h" | |
52 #include "sync/protocol/sync.pb.h" | |
53 #include "sync/sessions/sync_session.h" | |
54 #include "sync/syncable/model_type_test_util.h" | |
55 #include "sync/syncable/syncable.h" | |
56 #include "sync/syncable/syncable_id.h" | |
57 #include "sync/test/fake_encryptor.h" | |
58 #include "sync/test/fake_extensions_activity_monitor.h" | |
59 #include "sync/util/cryptographer.h" | |
60 #include "sync/util/extensions_activity_monitor.h" | |
61 #include "sync/util/test_unrecoverable_error_handler.h" | |
62 #include "sync/util/time.h" | |
63 #include "testing/gmock/include/gmock/gmock.h" | |
64 #include "testing/gtest/include/gtest/gtest.h" | |
65 | |
66 using base::ExpectDictStringValue; | |
67 using browser_sync::Cryptographer; | |
68 using browser_sync::FakeEncryptor; | |
69 using browser_sync::FakeExtensionsActivityMonitor; | |
70 using browser_sync::HasArgsAsList; | |
71 using browser_sync::HasDetailsAsDictionary; | |
72 using browser_sync::KeyParams; | |
73 using browser_sync::kNigoriTag; | |
74 using browser_sync::JsArgList; | |
75 using browser_sync::JsBackend; | |
76 using browser_sync::JsEventHandler; | |
77 using browser_sync::JsReplyHandler; | |
78 using browser_sync::MockJsEventHandler; | |
79 using browser_sync::MockJsReplyHandler; | |
80 using browser_sync::ModelSafeRoutingInfo; | |
81 using browser_sync::ModelSafeWorker; | |
82 using browser_sync::ModelSafeWorkerRegistrar; | |
83 using browser_sync::sessions::SyncSessionSnapshot; | |
84 using browser_sync::TestUnrecoverableErrorHandler; | |
85 using browser_sync::WeakHandle; | |
86 using syncable::IS_DEL; | |
87 using syncable::IS_UNSYNCED; | |
88 using syncable::kEncryptedString; | |
89 using syncable::ModelTypeSet; | |
90 using syncable::ModelType; | |
91 using syncable::NON_UNIQUE_NAME; | |
92 using syncable::SPECIFICS; | |
93 using testing::_; | |
94 using testing::AnyNumber; | |
95 using testing::AtLeast; | |
96 using testing::InSequence; | |
97 using testing::Invoke; | |
98 using testing::SaveArg; | |
99 using testing::StrictMock; | |
100 | |
101 namespace sync_api { | |
102 | |
103 namespace { | |
104 | |
105 const char kTestChromeVersion[] = "test chrome version"; | |
106 | |
107 void DoNothing() {} | |
108 | |
109 void ExpectInt64Value(int64 expected_value, | |
110 const DictionaryValue& value, const std::string& key) { | |
111 std::string int64_str; | |
112 EXPECT_TRUE(value.GetString(key, &int64_str)); | |
113 int64 val = 0; | |
114 EXPECT_TRUE(base::StringToInt64(int64_str, &val)); | |
115 EXPECT_EQ(expected_value, val); | |
116 } | |
117 | |
118 void ExpectTimeValue(const base::Time& expected_value, | |
119 const DictionaryValue& value, const std::string& key) { | |
120 std::string time_str; | |
121 EXPECT_TRUE(value.GetString(key, &time_str)); | |
122 EXPECT_EQ(browser_sync::GetTimeDebugString(expected_value), time_str); | |
123 } | |
124 | |
125 // Makes a non-folder child of the root node. Returns the id of the | |
126 // newly-created node. | |
127 int64 MakeNode(UserShare* share, | |
128 ModelType model_type, | |
129 const std::string& client_tag) { | |
130 WriteTransaction trans(FROM_HERE, share); | |
131 ReadNode root_node(&trans); | |
132 root_node.InitByRootLookup(); | |
133 WriteNode node(&trans); | |
134 EXPECT_TRUE(node.InitUniqueByCreation(model_type, root_node, client_tag)); | |
135 node.SetIsFolder(false); | |
136 return node.GetId(); | |
137 } | |
138 | |
139 // Makes a non-folder child of a non-root node. Returns the id of the | |
140 // newly-created node. | |
141 int64 MakeNodeWithParent(UserShare* share, | |
142 ModelType model_type, | |
143 const std::string& client_tag, | |
144 int64 parent_id) { | |
145 WriteTransaction trans(FROM_HERE, share); | |
146 ReadNode parent_node(&trans); | |
147 EXPECT_TRUE(parent_node.InitByIdLookup(parent_id)); | |
148 WriteNode node(&trans); | |
149 EXPECT_TRUE(node.InitUniqueByCreation(model_type, parent_node, client_tag)); | |
150 node.SetIsFolder(false); | |
151 return node.GetId(); | |
152 } | |
153 | |
154 // Makes a folder child of a non-root node. Returns the id of the | |
155 // newly-created node. | |
156 int64 MakeFolderWithParent(UserShare* share, | |
157 ModelType model_type, | |
158 int64 parent_id, | |
159 BaseNode* predecessor) { | |
160 WriteTransaction trans(FROM_HERE, share); | |
161 ReadNode parent_node(&trans); | |
162 EXPECT_TRUE(parent_node.InitByIdLookup(parent_id)); | |
163 WriteNode node(&trans); | |
164 EXPECT_TRUE(node.InitByCreation(model_type, parent_node, predecessor)); | |
165 node.SetIsFolder(true); | |
166 return node.GetId(); | |
167 } | |
168 | |
169 // Creates the "synced" root node for a particular datatype. We use the syncable | |
170 // methods here so that the syncer treats these nodes as if they were already | |
171 // received from the server. | |
172 int64 MakeServerNodeForType(UserShare* share, | |
173 ModelType model_type) { | |
174 sync_pb::EntitySpecifics specifics; | |
175 syncable::AddDefaultFieldValue(model_type, &specifics); | |
176 syncable::WriteTransaction trans( | |
177 FROM_HERE, syncable::UNITTEST, share->directory.get()); | |
178 // Attempt to lookup by nigori tag. | |
179 std::string type_tag = syncable::ModelTypeToRootTag(model_type); | |
180 syncable::Id node_id = syncable::Id::CreateFromServerId(type_tag); | |
181 syncable::MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, | |
182 node_id); | |
183 EXPECT_TRUE(entry.good()); | |
184 entry.Put(syncable::BASE_VERSION, 1); | |
185 entry.Put(syncable::SERVER_VERSION, 1); | |
186 entry.Put(syncable::IS_UNAPPLIED_UPDATE, false); | |
187 entry.Put(syncable::SERVER_PARENT_ID, syncable::GetNullId()); | |
188 entry.Put(syncable::SERVER_IS_DIR, true); | |
189 entry.Put(syncable::IS_DIR, true); | |
190 entry.Put(syncable::SERVER_SPECIFICS, specifics); | |
191 entry.Put(syncable::UNIQUE_SERVER_TAG, type_tag); | |
192 entry.Put(syncable::NON_UNIQUE_NAME, type_tag); | |
193 entry.Put(syncable::IS_DEL, false); | |
194 entry.Put(syncable::SPECIFICS, specifics); | |
195 return entry.Get(syncable::META_HANDLE); | |
196 } | |
197 | |
198 // Simulates creating a "synced" node as a child of the root datatype node. | |
199 int64 MakeServerNode(UserShare* share, ModelType model_type, | |
200 const std::string& client_tag, | |
201 const std::string& hashed_tag, | |
202 const sync_pb::EntitySpecifics& specifics) { | |
203 syncable::WriteTransaction trans( | |
204 FROM_HERE, syncable::UNITTEST, share->directory.get()); | |
205 syncable::Entry root_entry(&trans, syncable::GET_BY_SERVER_TAG, | |
206 syncable::ModelTypeToRootTag(model_type)); | |
207 EXPECT_TRUE(root_entry.good()); | |
208 syncable::Id root_id = root_entry.Get(syncable::ID); | |
209 syncable::Id node_id = syncable::Id::CreateFromServerId(client_tag); | |
210 syncable::MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, | |
211 node_id); | |
212 EXPECT_TRUE(entry.good()); | |
213 entry.Put(syncable::BASE_VERSION, 1); | |
214 entry.Put(syncable::SERVER_VERSION, 1); | |
215 entry.Put(syncable::IS_UNAPPLIED_UPDATE, false); | |
216 entry.Put(syncable::SERVER_PARENT_ID, root_id); | |
217 entry.Put(syncable::PARENT_ID, root_id); | |
218 entry.Put(syncable::SERVER_IS_DIR, false); | |
219 entry.Put(syncable::IS_DIR, false); | |
220 entry.Put(syncable::SERVER_SPECIFICS, specifics); | |
221 entry.Put(syncable::NON_UNIQUE_NAME, client_tag); | |
222 entry.Put(syncable::UNIQUE_CLIENT_TAG, hashed_tag); | |
223 entry.Put(syncable::IS_DEL, false); | |
224 entry.Put(syncable::SPECIFICS, specifics); | |
225 return entry.Get(syncable::META_HANDLE); | |
226 } | |
227 | |
228 } // namespace | |
229 | |
230 class SyncApiTest : public testing::Test { | |
231 public: | |
232 virtual void SetUp() { | |
233 test_user_share_.SetUp(); | |
234 } | |
235 | |
236 virtual void TearDown() { | |
237 test_user_share_.TearDown(); | |
238 } | |
239 | |
240 protected: | |
241 MessageLoop message_loop_; | |
242 browser_sync::TestUserShare test_user_share_; | |
243 }; | |
244 | |
245 TEST_F(SyncApiTest, SanityCheckTest) { | |
246 { | |
247 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
248 EXPECT_TRUE(trans.GetWrappedTrans() != NULL); | |
249 } | |
250 { | |
251 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
252 EXPECT_TRUE(trans.GetWrappedTrans() != NULL); | |
253 } | |
254 { | |
255 // No entries but root should exist | |
256 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
257 ReadNode node(&trans); | |
258 // Metahandle 1 can be root, sanity check 2 | |
259 EXPECT_FALSE(node.InitByIdLookup(2)); | |
260 } | |
261 } | |
262 | |
263 TEST_F(SyncApiTest, BasicTagWrite) { | |
264 { | |
265 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
266 ReadNode root_node(&trans); | |
267 root_node.InitByRootLookup(); | |
268 EXPECT_EQ(root_node.GetFirstChildId(), 0); | |
269 } | |
270 | |
271 ignore_result(MakeNode(test_user_share_.user_share(), | |
272 syncable::BOOKMARKS, "testtag")); | |
273 | |
274 { | |
275 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
276 ReadNode node(&trans); | |
277 EXPECT_TRUE(node.InitByClientTagLookup(syncable::BOOKMARKS, | |
278 "testtag")); | |
279 | |
280 ReadNode root_node(&trans); | |
281 root_node.InitByRootLookup(); | |
282 EXPECT_NE(node.GetId(), 0); | |
283 EXPECT_EQ(node.GetId(), root_node.GetFirstChildId()); | |
284 } | |
285 } | |
286 | |
287 TEST_F(SyncApiTest, GenerateSyncableHash) { | |
288 EXPECT_EQ("OyaXV5mEzrPS4wbogmtKvRfekAI=", | |
289 BaseNode::GenerateSyncableHash(syncable::BOOKMARKS, "tag1")); | |
290 EXPECT_EQ("iNFQtRFQb+IZcn1kKUJEZDDkLs4=", | |
291 BaseNode::GenerateSyncableHash(syncable::PREFERENCES, "tag1")); | |
292 EXPECT_EQ("gO1cPZQXaM73sHOvSA+tKCKFs58=", | |
293 BaseNode::GenerateSyncableHash(syncable::AUTOFILL, "tag1")); | |
294 | |
295 EXPECT_EQ("A0eYIHXM1/jVwKDDp12Up20IkKY=", | |
296 BaseNode::GenerateSyncableHash(syncable::BOOKMARKS, "tag2")); | |
297 EXPECT_EQ("XYxkF7bhS4eItStFgiOIAU23swI=", | |
298 BaseNode::GenerateSyncableHash(syncable::PREFERENCES, "tag2")); | |
299 EXPECT_EQ("GFiWzo5NGhjLlN+OyCfhy28DJTQ=", | |
300 BaseNode::GenerateSyncableHash(syncable::AUTOFILL, "tag2")); | |
301 } | |
302 | |
303 TEST_F(SyncApiTest, ModelTypesSiloed) { | |
304 { | |
305 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
306 ReadNode root_node(&trans); | |
307 root_node.InitByRootLookup(); | |
308 EXPECT_EQ(root_node.GetFirstChildId(), 0); | |
309 } | |
310 | |
311 ignore_result(MakeNode(test_user_share_.user_share(), | |
312 syncable::BOOKMARKS, "collideme")); | |
313 ignore_result(MakeNode(test_user_share_.user_share(), | |
314 syncable::PREFERENCES, "collideme")); | |
315 ignore_result(MakeNode(test_user_share_.user_share(), | |
316 syncable::AUTOFILL, "collideme")); | |
317 | |
318 { | |
319 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
320 | |
321 ReadNode bookmarknode(&trans); | |
322 EXPECT_TRUE(bookmarknode.InitByClientTagLookup(syncable::BOOKMARKS, | |
323 "collideme")); | |
324 | |
325 ReadNode prefnode(&trans); | |
326 EXPECT_TRUE(prefnode.InitByClientTagLookup(syncable::PREFERENCES, | |
327 "collideme")); | |
328 | |
329 ReadNode autofillnode(&trans); | |
330 EXPECT_TRUE(autofillnode.InitByClientTagLookup(syncable::AUTOFILL, | |
331 "collideme")); | |
332 | |
333 EXPECT_NE(bookmarknode.GetId(), prefnode.GetId()); | |
334 EXPECT_NE(autofillnode.GetId(), prefnode.GetId()); | |
335 EXPECT_NE(bookmarknode.GetId(), autofillnode.GetId()); | |
336 } | |
337 } | |
338 | |
339 TEST_F(SyncApiTest, ReadMissingTagsFails) { | |
340 { | |
341 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
342 ReadNode node(&trans); | |
343 EXPECT_FALSE(node.InitByClientTagLookup(syncable::BOOKMARKS, | |
344 "testtag")); | |
345 } | |
346 { | |
347 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
348 WriteNode node(&trans); | |
349 EXPECT_FALSE(node.InitByClientTagLookup(syncable::BOOKMARKS, | |
350 "testtag")); | |
351 } | |
352 } | |
353 | |
354 // TODO(chron): Hook this all up to the server and write full integration tests | |
355 // for update->undelete behavior. | |
356 TEST_F(SyncApiTest, TestDeleteBehavior) { | |
357 int64 node_id; | |
358 int64 folder_id; | |
359 std::string test_title("test1"); | |
360 | |
361 { | |
362 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
363 ReadNode root_node(&trans); | |
364 root_node.InitByRootLookup(); | |
365 | |
366 // we'll use this spare folder later | |
367 WriteNode folder_node(&trans); | |
368 EXPECT_TRUE(folder_node.InitByCreation(syncable::BOOKMARKS, | |
369 root_node, NULL)); | |
370 folder_id = folder_node.GetId(); | |
371 | |
372 WriteNode wnode(&trans); | |
373 EXPECT_TRUE(wnode.InitUniqueByCreation(syncable::BOOKMARKS, | |
374 root_node, "testtag")); | |
375 wnode.SetIsFolder(false); | |
376 wnode.SetTitle(UTF8ToWide(test_title)); | |
377 | |
378 node_id = wnode.GetId(); | |
379 } | |
380 | |
381 // Ensure we can delete something with a tag. | |
382 { | |
383 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
384 WriteNode wnode(&trans); | |
385 EXPECT_TRUE(wnode.InitByClientTagLookup(syncable::BOOKMARKS, | |
386 "testtag")); | |
387 EXPECT_FALSE(wnode.GetIsFolder()); | |
388 EXPECT_EQ(wnode.GetTitle(), test_title); | |
389 | |
390 wnode.Remove(); | |
391 } | |
392 | |
393 // Lookup of a node which was deleted should return failure, | |
394 // but have found some data about the node. | |
395 { | |
396 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
397 ReadNode node(&trans); | |
398 EXPECT_FALSE(node.InitByClientTagLookup(syncable::BOOKMARKS, | |
399 "testtag")); | |
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. | |
402 EXPECT_EQ(node.GetTitle(), test_title); | |
403 } | |
404 | |
405 { | |
406 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
407 ReadNode folder_node(&trans); | |
408 EXPECT_TRUE(folder_node.InitByIdLookup(folder_id)); | |
409 | |
410 WriteNode wnode(&trans); | |
411 // This will undelete the tag. | |
412 EXPECT_TRUE(wnode.InitUniqueByCreation(syncable::BOOKMARKS, | |
413 folder_node, "testtag")); | |
414 EXPECT_EQ(wnode.GetIsFolder(), false); | |
415 EXPECT_EQ(wnode.GetParentId(), folder_node.GetId()); | |
416 EXPECT_EQ(wnode.GetId(), node_id); | |
417 EXPECT_NE(wnode.GetTitle(), test_title); // Title should be cleared | |
418 wnode.SetTitle(UTF8ToWide(test_title)); | |
419 } | |
420 | |
421 // Now look up should work. | |
422 { | |
423 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
424 ReadNode node(&trans); | |
425 EXPECT_TRUE(node.InitByClientTagLookup(syncable::BOOKMARKS, | |
426 "testtag")); | |
427 EXPECT_EQ(node.GetTitle(), test_title); | |
428 EXPECT_EQ(node.GetModelType(), syncable::BOOKMARKS); | |
429 } | |
430 } | |
431 | |
432 TEST_F(SyncApiTest, WriteAndReadPassword) { | |
433 KeyParams params = {"localhost", "username", "passphrase"}; | |
434 { | |
435 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
436 trans.GetCryptographer()->AddKey(params); | |
437 } | |
438 { | |
439 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
440 ReadNode root_node(&trans); | |
441 root_node.InitByRootLookup(); | |
442 | |
443 WriteNode password_node(&trans); | |
444 EXPECT_TRUE(password_node.InitUniqueByCreation(syncable::PASSWORDS, | |
445 root_node, "foo")); | |
446 sync_pb::PasswordSpecificsData data; | |
447 data.set_password_value("secret"); | |
448 password_node.SetPasswordSpecifics(data); | |
449 } | |
450 { | |
451 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
452 ReadNode root_node(&trans); | |
453 root_node.InitByRootLookup(); | |
454 | |
455 ReadNode password_node(&trans); | |
456 EXPECT_TRUE(password_node.InitByClientTagLookup(syncable::PASSWORDS, | |
457 "foo")); | |
458 const sync_pb::PasswordSpecificsData& data = | |
459 password_node.GetPasswordSpecifics(); | |
460 EXPECT_EQ("secret", data.password_value()); | |
461 } | |
462 } | |
463 | |
464 TEST_F(SyncApiTest, WriteEncryptedTitle) { | |
465 KeyParams params = {"localhost", "username", "passphrase"}; | |
466 { | |
467 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
468 trans.GetCryptographer()->AddKey(params); | |
469 trans.GetCryptographer()->set_encrypt_everything(); | |
470 } | |
471 { | |
472 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
473 ReadNode root_node(&trans); | |
474 root_node.InitByRootLookup(); | |
475 | |
476 WriteNode bookmark_node(&trans); | |
477 EXPECT_TRUE(bookmark_node.InitUniqueByCreation(syncable::BOOKMARKS, | |
478 root_node, "foo")); | |
479 bookmark_node.SetTitle(UTF8ToWide("foo")); | |
480 | |
481 WriteNode pref_node(&trans); | |
482 EXPECT_TRUE(pref_node.InitUniqueByCreation(syncable::PREFERENCES, | |
483 root_node, "bar")); | |
484 pref_node.SetTitle(UTF8ToWide("bar")); | |
485 } | |
486 { | |
487 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
488 ReadNode root_node(&trans); | |
489 root_node.InitByRootLookup(); | |
490 | |
491 ReadNode bookmark_node(&trans); | |
492 EXPECT_TRUE(bookmark_node.InitByClientTagLookup(syncable::BOOKMARKS, | |
493 "foo")); | |
494 EXPECT_EQ("foo", bookmark_node.GetTitle()); | |
495 EXPECT_EQ(kEncryptedString, | |
496 bookmark_node.GetEntry()->Get(syncable::NON_UNIQUE_NAME)); | |
497 | |
498 ReadNode pref_node(&trans); | |
499 EXPECT_TRUE(pref_node.InitByClientTagLookup(syncable::PREFERENCES, | |
500 "bar")); | |
501 EXPECT_EQ(kEncryptedString, pref_node.GetTitle()); | |
502 } | |
503 } | |
504 | |
505 TEST_F(SyncApiTest, BaseNodeSetSpecifics) { | |
506 int64 child_id = MakeNode(test_user_share_.user_share(), | |
507 syncable::BOOKMARKS, "testtag"); | |
508 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
509 WriteNode node(&trans); | |
510 EXPECT_TRUE(node.InitByIdLookup(child_id)); | |
511 | |
512 sync_pb::EntitySpecifics entity_specifics; | |
513 entity_specifics.mutable_bookmark()->set_url("http://www.google.com"); | |
514 | |
515 EXPECT_NE(entity_specifics.SerializeAsString(), | |
516 node.GetEntitySpecifics().SerializeAsString()); | |
517 node.SetEntitySpecifics(entity_specifics); | |
518 EXPECT_EQ(entity_specifics.SerializeAsString(), | |
519 node.GetEntitySpecifics().SerializeAsString()); | |
520 } | |
521 | |
522 TEST_F(SyncApiTest, BaseNodeSetSpecificsPreservesUnknownFields) { | |
523 int64 child_id = MakeNode(test_user_share_.user_share(), | |
524 syncable::BOOKMARKS, "testtag"); | |
525 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
526 WriteNode node(&trans); | |
527 EXPECT_TRUE(node.InitByIdLookup(child_id)); | |
528 EXPECT_TRUE(node.GetEntitySpecifics().unknown_fields().empty()); | |
529 | |
530 sync_pb::EntitySpecifics entity_specifics; | |
531 entity_specifics.mutable_bookmark()->set_url("http://www.google.com"); | |
532 entity_specifics.mutable_unknown_fields()->AddFixed32(5, 100); | |
533 node.SetEntitySpecifics(entity_specifics); | |
534 EXPECT_FALSE(node.GetEntitySpecifics().unknown_fields().empty()); | |
535 | |
536 entity_specifics.mutable_unknown_fields()->Clear(); | |
537 node.SetEntitySpecifics(entity_specifics); | |
538 EXPECT_FALSE(node.GetEntitySpecifics().unknown_fields().empty()); | |
539 } | |
540 | |
541 namespace { | |
542 | |
543 void CheckNodeValue(const BaseNode& node, const DictionaryValue& value, | |
544 bool is_detailed) { | |
545 ExpectInt64Value(node.GetId(), value, "id"); | |
546 { | |
547 bool is_folder = false; | |
548 EXPECT_TRUE(value.GetBoolean("isFolder", &is_folder)); | |
549 EXPECT_EQ(node.GetIsFolder(), is_folder); | |
550 } | |
551 ExpectDictStringValue(node.GetTitle(), value, "title"); | |
552 { | |
553 ModelType expected_model_type = node.GetModelType(); | |
554 std::string type_str; | |
555 EXPECT_TRUE(value.GetString("type", &type_str)); | |
556 if (expected_model_type >= syncable::FIRST_REAL_MODEL_TYPE) { | |
557 ModelType model_type = | |
558 syncable::ModelTypeFromString(type_str); | |
559 EXPECT_EQ(expected_model_type, model_type); | |
560 } else if (expected_model_type == syncable::TOP_LEVEL_FOLDER) { | |
561 EXPECT_EQ("Top-level folder", type_str); | |
562 } else if (expected_model_type == syncable::UNSPECIFIED) { | |
563 EXPECT_EQ("Unspecified", type_str); | |
564 } else { | |
565 ADD_FAILURE(); | |
566 } | |
567 } | |
568 if (is_detailed) { | |
569 ExpectInt64Value(node.GetParentId(), value, "parentId"); | |
570 ExpectTimeValue(node.GetModificationTime(), value, "modificationTime"); | |
571 ExpectInt64Value(node.GetExternalId(), value, "externalId"); | |
572 ExpectInt64Value(node.GetPredecessorId(), value, "predecessorId"); | |
573 ExpectInt64Value(node.GetSuccessorId(), value, "successorId"); | |
574 ExpectInt64Value(node.GetFirstChildId(), value, "firstChildId"); | |
575 { | |
576 scoped_ptr<DictionaryValue> expected_entry(node.GetEntry()->ToValue()); | |
577 Value* entry = NULL; | |
578 EXPECT_TRUE(value.Get("entry", &entry)); | |
579 EXPECT_TRUE(Value::Equals(entry, expected_entry.get())); | |
580 } | |
581 EXPECT_EQ(11u, value.size()); | |
582 } else { | |
583 EXPECT_EQ(4u, value.size()); | |
584 } | |
585 } | |
586 | |
587 } // namespace | |
588 | |
589 TEST_F(SyncApiTest, BaseNodeGetSummaryAsValue) { | |
590 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
591 ReadNode node(&trans); | |
592 node.InitByRootLookup(); | |
593 scoped_ptr<DictionaryValue> details(node.GetSummaryAsValue()); | |
594 if (details.get()) { | |
595 CheckNodeValue(node, *details, false); | |
596 } else { | |
597 ADD_FAILURE(); | |
598 } | |
599 } | |
600 | |
601 TEST_F(SyncApiTest, BaseNodeGetDetailsAsValue) { | |
602 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
603 ReadNode node(&trans); | |
604 node.InitByRootLookup(); | |
605 scoped_ptr<DictionaryValue> details(node.GetDetailsAsValue()); | |
606 if (details.get()) { | |
607 CheckNodeValue(node, *details, true); | |
608 } else { | |
609 ADD_FAILURE(); | |
610 } | |
611 } | |
612 | |
613 TEST_F(SyncApiTest, EmptyTags) { | |
614 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
615 ReadNode root_node(&trans); | |
616 root_node.InitByRootLookup(); | |
617 WriteNode node(&trans); | |
618 std::string empty_tag; | |
619 EXPECT_FALSE(node.InitUniqueByCreation( | |
620 syncable::TYPED_URLS, root_node, empty_tag)); | |
621 EXPECT_FALSE(node.InitByTagLookup(empty_tag)); | |
622 } | |
623 | |
624 namespace { | |
625 | |
626 class TestHttpPostProviderInterface : public HttpPostProviderInterface { | |
627 public: | |
628 virtual ~TestHttpPostProviderInterface() {} | |
629 | |
630 virtual void SetUserAgent(const char* user_agent) OVERRIDE {} | |
631 virtual void SetExtraRequestHeaders(const char* headers) OVERRIDE {} | |
632 virtual void SetURL(const char* url, int port) OVERRIDE {} | |
633 virtual void SetPostPayload(const char* content_type, | |
634 int content_length, | |
635 const char* content) OVERRIDE {} | |
636 virtual bool MakeSynchronousPost(int* error_code, int* response_code) | |
637 OVERRIDE { | |
638 return false; | |
639 } | |
640 virtual int GetResponseContentLength() const OVERRIDE { | |
641 return 0; | |
642 } | |
643 virtual const char* GetResponseContent() const OVERRIDE { | |
644 return ""; | |
645 } | |
646 virtual const std::string GetResponseHeaderValue( | |
647 const std::string& name) const OVERRIDE { | |
648 return ""; | |
649 } | |
650 virtual void Abort() OVERRIDE {} | |
651 }; | |
652 | |
653 class TestHttpPostProviderFactory : public HttpPostProviderFactory { | |
654 public: | |
655 virtual ~TestHttpPostProviderFactory() {} | |
656 virtual HttpPostProviderInterface* Create() OVERRIDE { | |
657 return new TestHttpPostProviderInterface(); | |
658 } | |
659 virtual void Destroy(HttpPostProviderInterface* http) OVERRIDE { | |
660 delete http; | |
661 } | |
662 }; | |
663 | |
664 class SyncManagerObserverMock : public SyncManager::Observer { | |
665 public: | |
666 MOCK_METHOD1(OnSyncCycleCompleted, | |
667 void(const SyncSessionSnapshot*)); // NOLINT | |
668 MOCK_METHOD2(OnInitializationComplete, | |
669 void(const WeakHandle<JsBackend>&, bool)); // NOLINT | |
670 MOCK_METHOD1(OnConnectionStatusChange, void(ConnectionStatus)); // NOLINT | |
671 MOCK_METHOD2(OnPassphraseRequired, | |
672 void(sync_api::PassphraseRequiredReason, | |
673 const sync_pb::EncryptedData&)); // NOLINT | |
674 MOCK_METHOD0(OnPassphraseAccepted, void()); // NOLINT | |
675 MOCK_METHOD1(OnBootstrapTokenUpdated, void(const std::string&)); // NOLINT | |
676 MOCK_METHOD0(OnStopSyncingPermanently, void()); // NOLINT | |
677 MOCK_METHOD1(OnUpdatedToken, void(const std::string&)); // NOLINT | |
678 MOCK_METHOD0(OnClearServerDataFailed, void()); // NOLINT | |
679 MOCK_METHOD0(OnClearServerDataSucceeded, void()); // NOLINT | |
680 MOCK_METHOD2(OnEncryptedTypesChanged, | |
681 void(ModelTypeSet, bool)); // NOLINT | |
682 MOCK_METHOD0(OnEncryptionComplete, void()); // NOLINT | |
683 MOCK_METHOD1(OnActionableError, | |
684 void(const browser_sync::SyncProtocolError&)); // NOLINT | |
685 }; | |
686 | |
687 class SyncNotifierMock : public sync_notifier::SyncNotifier { | |
688 public: | |
689 MOCK_METHOD1(AddObserver, void(sync_notifier::SyncNotifierObserver*)); | |
690 MOCK_METHOD1(RemoveObserver, void(sync_notifier::SyncNotifierObserver*)); | |
691 MOCK_METHOD1(SetUniqueId, void(const std::string&)); | |
692 MOCK_METHOD1(SetState, void(const std::string&)); | |
693 MOCK_METHOD2(UpdateCredentials, | |
694 void(const std::string&, const std::string&)); | |
695 MOCK_METHOD1(UpdateEnabledTypes, | |
696 void(syncable::ModelTypeSet)); | |
697 MOCK_METHOD1(SendNotification, void(syncable::ModelTypeSet)); | |
698 }; | |
699 | |
700 } // namespace | |
701 | |
702 class SyncManagerTest : public testing::Test, | |
703 public ModelSafeWorkerRegistrar, | |
704 public SyncManager::ChangeDelegate { | |
705 protected: | |
706 enum NigoriStatus { | |
707 DONT_WRITE_NIGORI, | |
708 WRITE_TO_NIGORI | |
709 }; | |
710 | |
711 enum EncryptionStatus { | |
712 UNINITIALIZED, | |
713 DEFAULT_ENCRYPTION, | |
714 FULL_ENCRYPTION | |
715 }; | |
716 | |
717 SyncManagerTest() | |
718 : sync_notifier_mock_(NULL), | |
719 sync_manager_("Test sync manager"), | |
720 sync_notifier_observer_(NULL), | |
721 update_enabled_types_call_count_(0) {} | |
722 | |
723 virtual ~SyncManagerTest() { | |
724 EXPECT_FALSE(sync_notifier_mock_); | |
725 } | |
726 | |
727 // Test implementation. | |
728 void SetUp() { | |
729 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
730 | |
731 SyncCredentials credentials; | |
732 credentials.email = "foo@bar.com"; | |
733 credentials.sync_token = "sometoken"; | |
734 | |
735 sync_notifier_mock_ = new StrictMock<SyncNotifierMock>(); | |
736 EXPECT_CALL(*sync_notifier_mock_, AddObserver(_)). | |
737 WillOnce(Invoke(this, &SyncManagerTest::SyncNotifierAddObserver)); | |
738 EXPECT_CALL(*sync_notifier_mock_, SetUniqueId(_)); | |
739 EXPECT_CALL(*sync_notifier_mock_, SetState("")); | |
740 EXPECT_CALL(*sync_notifier_mock_, | |
741 UpdateCredentials(credentials.email, credentials.sync_token)); | |
742 EXPECT_CALL(*sync_notifier_mock_, UpdateEnabledTypes(_)). | |
743 Times(AtLeast(1)). | |
744 WillRepeatedly( | |
745 Invoke(this, &SyncManagerTest::SyncNotifierUpdateEnabledTypes)); | |
746 EXPECT_CALL(*sync_notifier_mock_, RemoveObserver(_)). | |
747 WillOnce(Invoke(this, &SyncManagerTest::SyncNotifierRemoveObserver)); | |
748 | |
749 sync_manager_.AddObserver(&observer_); | |
750 EXPECT_CALL(observer_, OnInitializationComplete(_, _)). | |
751 WillOnce(SaveArg<0>(&js_backend_)); | |
752 | |
753 EXPECT_FALSE(sync_notifier_observer_); | |
754 EXPECT_FALSE(js_backend_.IsInitialized()); | |
755 | |
756 // Takes ownership of |sync_notifier_mock_|. | |
757 sync_manager_.Init(temp_dir_.path(), | |
758 WeakHandle<JsEventHandler>(), | |
759 "bogus", 0, false, | |
760 base::MessageLoopProxy::current(), | |
761 new TestHttpPostProviderFactory(), this, | |
762 &extensions_activity_monitor_, this, "bogus", | |
763 credentials, | |
764 false /* enable_sync_tabs_for_other_clients */, | |
765 sync_notifier_mock_, "", | |
766 sync_api::SyncManager::TEST_IN_MEMORY, | |
767 &encryptor_, | |
768 &handler_, | |
769 NULL); | |
770 | |
771 EXPECT_TRUE(sync_notifier_observer_); | |
772 EXPECT_TRUE(js_backend_.IsInitialized()); | |
773 | |
774 EXPECT_EQ(1, update_enabled_types_call_count_); | |
775 | |
776 ModelSafeRoutingInfo routes; | |
777 GetModelSafeRoutingInfo(&routes); | |
778 for (ModelSafeRoutingInfo::iterator i = routes.begin(); i != routes.end(); | |
779 ++i) { | |
780 type_roots_[i->first] = MakeServerNodeForType( | |
781 sync_manager_.GetUserShare(), i->first); | |
782 } | |
783 PumpLoop(); | |
784 } | |
785 | |
786 void TearDown() { | |
787 sync_manager_.RemoveObserver(&observer_); | |
788 sync_manager_.ShutdownOnSyncThread(); | |
789 sync_notifier_mock_ = NULL; | |
790 EXPECT_FALSE(sync_notifier_observer_); | |
791 PumpLoop(); | |
792 } | |
793 | |
794 // ModelSafeWorkerRegistrar implementation. | |
795 virtual void GetWorkers(std::vector<ModelSafeWorker*>* out) OVERRIDE { | |
796 NOTIMPLEMENTED(); | |
797 out->clear(); | |
798 } | |
799 virtual void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) OVERRIDE { | |
800 (*out)[syncable::NIGORI] = browser_sync::GROUP_PASSIVE; | |
801 (*out)[syncable::BOOKMARKS] = browser_sync::GROUP_PASSIVE; | |
802 (*out)[syncable::THEMES] = browser_sync::GROUP_PASSIVE; | |
803 (*out)[syncable::SESSIONS] = browser_sync::GROUP_PASSIVE; | |
804 (*out)[syncable::PASSWORDS] = browser_sync::GROUP_PASSIVE; | |
805 (*out)[syncable::PREFERENCES] = browser_sync::GROUP_PASSIVE; | |
806 } | |
807 | |
808 virtual void OnChangesApplied( | |
809 syncable::ModelType model_type, | |
810 const BaseTransaction* trans, | |
811 const ImmutableChangeRecordList& changes) OVERRIDE {} | |
812 | |
813 virtual void OnChangesComplete(syncable::ModelType model_type) OVERRIDE {} | |
814 | |
815 // Helper methods. | |
816 bool SetUpEncryption(NigoriStatus nigori_status, | |
817 EncryptionStatus encryption_status) { | |
818 UserShare* share = sync_manager_.GetUserShare(); | |
819 share->directory->set_initial_sync_ended_for_type(syncable::NIGORI, true); | |
820 | |
821 // We need to create the nigori node as if it were an applied server update. | |
822 int64 nigori_id = GetIdForDataType(syncable::NIGORI); | |
823 if (nigori_id == kInvalidId) | |
824 return false; | |
825 | |
826 // Set the nigori cryptographer information. | |
827 WriteTransaction trans(FROM_HERE, share); | |
828 Cryptographer* cryptographer = trans.GetCryptographer(); | |
829 if (!cryptographer) | |
830 return false; | |
831 if (encryption_status != UNINITIALIZED) { | |
832 KeyParams params = {"localhost", "dummy", "foobar"}; | |
833 cryptographer->AddKey(params); | |
834 } else { | |
835 DCHECK_NE(nigori_status, WRITE_TO_NIGORI); | |
836 } | |
837 if (encryption_status == FULL_ENCRYPTION) | |
838 cryptographer->set_encrypt_everything(); | |
839 if (nigori_status == WRITE_TO_NIGORI) { | |
840 sync_pb::NigoriSpecifics nigori; | |
841 cryptographer->GetKeys(nigori.mutable_encrypted()); | |
842 cryptographer->UpdateNigoriFromEncryptedTypes(&nigori); | |
843 WriteNode node(&trans); | |
844 EXPECT_TRUE(node.InitByIdLookup(nigori_id)); | |
845 node.SetNigoriSpecifics(nigori); | |
846 } | |
847 return cryptographer->is_ready(); | |
848 } | |
849 | |
850 int64 GetIdForDataType(ModelType type) { | |
851 if (type_roots_.count(type) == 0) | |
852 return 0; | |
853 return type_roots_[type]; | |
854 } | |
855 | |
856 void SyncNotifierAddObserver( | |
857 sync_notifier::SyncNotifierObserver* sync_notifier_observer) { | |
858 EXPECT_EQ(NULL, sync_notifier_observer_); | |
859 sync_notifier_observer_ = sync_notifier_observer; | |
860 } | |
861 | |
862 void SyncNotifierRemoveObserver( | |
863 sync_notifier::SyncNotifierObserver* sync_notifier_observer) { | |
864 EXPECT_EQ(sync_notifier_observer_, sync_notifier_observer); | |
865 sync_notifier_observer_ = NULL; | |
866 } | |
867 | |
868 void SyncNotifierUpdateEnabledTypes(syncable::ModelTypeSet types) { | |
869 ModelSafeRoutingInfo routes; | |
870 GetModelSafeRoutingInfo(&routes); | |
871 const syncable::ModelTypeSet expected_types = | |
872 GetRoutingInfoTypes(routes); | |
873 EXPECT_TRUE(types.Equals(expected_types)); | |
874 ++update_enabled_types_call_count_; | |
875 } | |
876 | |
877 void PumpLoop() { | |
878 message_loop_.RunAllPending(); | |
879 } | |
880 | |
881 void SendJsMessage(const std::string& name, const JsArgList& args, | |
882 const WeakHandle<JsReplyHandler>& reply_handler) { | |
883 js_backend_.Call(FROM_HERE, &JsBackend::ProcessJsMessage, | |
884 name, args, reply_handler); | |
885 PumpLoop(); | |
886 } | |
887 | |
888 void SetJsEventHandler(const WeakHandle<JsEventHandler>& event_handler) { | |
889 js_backend_.Call(FROM_HERE, &JsBackend::SetJsEventHandler, | |
890 event_handler); | |
891 PumpLoop(); | |
892 } | |
893 | |
894 // Looks up an entry by client tag and resets IS_UNSYNCED value to false. | |
895 // Returns true if entry was previously unsynced, false if IS_UNSYNCED was | |
896 // already false. | |
897 bool ResetUnsyncedEntry(syncable::ModelType type, | |
898 const std::string& client_tag) { | |
899 UserShare* share = sync_manager_.GetUserShare(); | |
900 syncable::WriteTransaction trans( | |
901 FROM_HERE, syncable::UNITTEST, share->directory.get()); | |
902 const std::string hash = BaseNode::GenerateSyncableHash(type, client_tag); | |
903 syncable::MutableEntry entry(&trans, syncable::GET_BY_CLIENT_TAG, | |
904 hash); | |
905 EXPECT_TRUE(entry.good()); | |
906 if (!entry.Get(IS_UNSYNCED)) | |
907 return false; | |
908 entry.Put(IS_UNSYNCED, false); | |
909 return true; | |
910 } | |
911 | |
912 private: | |
913 // Needed by |sync_manager_|. | |
914 MessageLoop message_loop_; | |
915 // Needed by |sync_manager_|. | |
916 ScopedTempDir temp_dir_; | |
917 // Sync Id's for the roots of the enabled datatypes. | |
918 std::map<ModelType, int64> type_roots_; | |
919 FakeExtensionsActivityMonitor extensions_activity_monitor_; | |
920 StrictMock<SyncNotifierMock>* sync_notifier_mock_; | |
921 | |
922 protected: | |
923 FakeEncryptor encryptor_; | |
924 TestUnrecoverableErrorHandler handler_; | |
925 SyncManager sync_manager_; | |
926 WeakHandle<JsBackend> js_backend_; | |
927 StrictMock<SyncManagerObserverMock> observer_; | |
928 sync_notifier::SyncNotifierObserver* sync_notifier_observer_; | |
929 int update_enabled_types_call_count_; | |
930 }; | |
931 | |
932 TEST_F(SyncManagerTest, UpdateEnabledTypes) { | |
933 EXPECT_EQ(1, update_enabled_types_call_count_); | |
934 // Triggers SyncNotifierUpdateEnabledTypes. | |
935 sync_manager_.UpdateEnabledTypes(); | |
936 EXPECT_EQ(2, update_enabled_types_call_count_); | |
937 } | |
938 | |
939 TEST_F(SyncManagerTest, DoNotSyncTabsInNigoriNode) { | |
940 const syncable::ModelTypeSet encrypted_types(syncable::TYPED_URLS); | |
941 sync_manager_.MaybeSetSyncTabsInNigoriNode(encrypted_types); | |
942 | |
943 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
944 ReadNode node(&trans); | |
945 ASSERT_TRUE(node.InitByIdLookup(GetIdForDataType(syncable::NIGORI))); | |
946 EXPECT_FALSE(node.GetNigoriSpecifics().sync_tabs()); | |
947 } | |
948 | |
949 TEST_F(SyncManagerTest, SyncTabsInNigoriNode) { | |
950 const syncable::ModelTypeSet encrypted_types(syncable::SESSIONS); | |
951 sync_manager_.MaybeSetSyncTabsInNigoriNode(encrypted_types); | |
952 | |
953 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
954 ReadNode node(&trans); | |
955 ASSERT_TRUE(node.InitByIdLookup(GetIdForDataType(syncable::NIGORI))); | |
956 EXPECT_TRUE(node.GetNigoriSpecifics().sync_tabs()); | |
957 } | |
958 | |
959 TEST_F(SyncManagerTest, ProcessJsMessage) { | |
960 const JsArgList kNoArgs; | |
961 | |
962 StrictMock<MockJsReplyHandler> reply_handler; | |
963 | |
964 ListValue false_args; | |
965 false_args.Append(Value::CreateBooleanValue(false)); | |
966 | |
967 EXPECT_CALL(reply_handler, | |
968 HandleJsReply("getNotificationState", | |
969 HasArgsAsList(false_args))); | |
970 | |
971 // This message should be dropped. | |
972 SendJsMessage("unknownMessage", kNoArgs, reply_handler.AsWeakHandle()); | |
973 | |
974 SendJsMessage("getNotificationState", kNoArgs, reply_handler.AsWeakHandle()); | |
975 } | |
976 | |
977 TEST_F(SyncManagerTest, ProcessJsMessageGetRootNodeDetails) { | |
978 const JsArgList kNoArgs; | |
979 | |
980 StrictMock<MockJsReplyHandler> reply_handler; | |
981 | |
982 JsArgList return_args; | |
983 | |
984 EXPECT_CALL(reply_handler, | |
985 HandleJsReply("getRootNodeDetails", _)) | |
986 .WillOnce(SaveArg<1>(&return_args)); | |
987 | |
988 SendJsMessage("getRootNodeDetails", kNoArgs, reply_handler.AsWeakHandle()); | |
989 | |
990 EXPECT_EQ(1u, return_args.Get().GetSize()); | |
991 DictionaryValue* node_info = NULL; | |
992 EXPECT_TRUE(return_args.Get().GetDictionary(0, &node_info)); | |
993 if (node_info) { | |
994 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
995 ReadNode node(&trans); | |
996 node.InitByRootLookup(); | |
997 CheckNodeValue(node, *node_info, true); | |
998 } else { | |
999 ADD_FAILURE(); | |
1000 } | |
1001 } | |
1002 | |
1003 void CheckGetNodesByIdReturnArgs(const SyncManager& sync_manager, | |
1004 const JsArgList& return_args, | |
1005 int64 id, | |
1006 bool is_detailed) { | |
1007 EXPECT_EQ(1u, return_args.Get().GetSize()); | |
1008 ListValue* nodes = NULL; | |
1009 ASSERT_TRUE(return_args.Get().GetList(0, &nodes)); | |
1010 ASSERT_TRUE(nodes); | |
1011 EXPECT_EQ(1u, nodes->GetSize()); | |
1012 DictionaryValue* node_info = NULL; | |
1013 EXPECT_TRUE(nodes->GetDictionary(0, &node_info)); | |
1014 ASSERT_TRUE(node_info); | |
1015 ReadTransaction trans(FROM_HERE, sync_manager.GetUserShare()); | |
1016 ReadNode node(&trans); | |
1017 EXPECT_TRUE(node.InitByIdLookup(id)); | |
1018 CheckNodeValue(node, *node_info, is_detailed); | |
1019 } | |
1020 | |
1021 class SyncManagerGetNodesByIdTest : public SyncManagerTest { | |
1022 protected: | |
1023 virtual ~SyncManagerGetNodesByIdTest() {} | |
1024 | |
1025 void RunGetNodesByIdTest(const char* message_name, bool is_detailed) { | |
1026 int64 root_id = kInvalidId; | |
1027 { | |
1028 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1029 ReadNode root_node(&trans); | |
1030 root_node.InitByRootLookup(); | |
1031 root_id = root_node.GetId(); | |
1032 } | |
1033 | |
1034 int64 child_id = | |
1035 MakeNode(sync_manager_.GetUserShare(), | |
1036 syncable::BOOKMARKS, "testtag"); | |
1037 | |
1038 StrictMock<MockJsReplyHandler> reply_handler; | |
1039 | |
1040 JsArgList return_args; | |
1041 | |
1042 const int64 ids[] = { root_id, child_id }; | |
1043 | |
1044 EXPECT_CALL(reply_handler, | |
1045 HandleJsReply(message_name, _)) | |
1046 .Times(arraysize(ids)).WillRepeatedly(SaveArg<1>(&return_args)); | |
1047 | |
1048 for (size_t i = 0; i < arraysize(ids); ++i) { | |
1049 ListValue args; | |
1050 ListValue* id_values = new ListValue(); | |
1051 args.Append(id_values); | |
1052 id_values->Append(Value::CreateStringValue(base::Int64ToString(ids[i]))); | |
1053 SendJsMessage(message_name, | |
1054 JsArgList(&args), reply_handler.AsWeakHandle()); | |
1055 | |
1056 CheckGetNodesByIdReturnArgs(sync_manager_, return_args, | |
1057 ids[i], is_detailed); | |
1058 } | |
1059 } | |
1060 | |
1061 void RunGetNodesByIdFailureTest(const char* message_name) { | |
1062 StrictMock<MockJsReplyHandler> reply_handler; | |
1063 | |
1064 ListValue empty_list_args; | |
1065 empty_list_args.Append(new ListValue()); | |
1066 | |
1067 EXPECT_CALL(reply_handler, | |
1068 HandleJsReply(message_name, | |
1069 HasArgsAsList(empty_list_args))) | |
1070 .Times(6); | |
1071 | |
1072 { | |
1073 ListValue args; | |
1074 SendJsMessage(message_name, | |
1075 JsArgList(&args), reply_handler.AsWeakHandle()); | |
1076 } | |
1077 | |
1078 { | |
1079 ListValue args; | |
1080 args.Append(new ListValue()); | |
1081 SendJsMessage(message_name, | |
1082 JsArgList(&args), reply_handler.AsWeakHandle()); | |
1083 } | |
1084 | |
1085 { | |
1086 ListValue args; | |
1087 ListValue* ids = new ListValue(); | |
1088 args.Append(ids); | |
1089 ids->Append(Value::CreateStringValue("")); | |
1090 SendJsMessage(message_name, | |
1091 JsArgList(&args), reply_handler.AsWeakHandle()); | |
1092 } | |
1093 | |
1094 { | |
1095 ListValue args; | |
1096 ListValue* ids = new ListValue(); | |
1097 args.Append(ids); | |
1098 ids->Append(Value::CreateStringValue("nonsense")); | |
1099 SendJsMessage(message_name, | |
1100 JsArgList(&args), reply_handler.AsWeakHandle()); | |
1101 } | |
1102 | |
1103 { | |
1104 ListValue args; | |
1105 ListValue* ids = new ListValue(); | |
1106 args.Append(ids); | |
1107 ids->Append(Value::CreateStringValue("0")); | |
1108 SendJsMessage(message_name, | |
1109 JsArgList(&args), reply_handler.AsWeakHandle()); | |
1110 } | |
1111 | |
1112 { | |
1113 ListValue args; | |
1114 ListValue* ids = new ListValue(); | |
1115 args.Append(ids); | |
1116 ids->Append(Value::CreateStringValue("9999")); | |
1117 SendJsMessage(message_name, | |
1118 JsArgList(&args), reply_handler.AsWeakHandle()); | |
1119 } | |
1120 } | |
1121 }; | |
1122 | |
1123 TEST_F(SyncManagerGetNodesByIdTest, GetNodeSummariesById) { | |
1124 RunGetNodesByIdTest("getNodeSummariesById", false); | |
1125 } | |
1126 | |
1127 TEST_F(SyncManagerGetNodesByIdTest, GetNodeDetailsById) { | |
1128 RunGetNodesByIdTest("getNodeDetailsById", true); | |
1129 } | |
1130 | |
1131 TEST_F(SyncManagerGetNodesByIdTest, GetNodeSummariesByIdFailure) { | |
1132 RunGetNodesByIdFailureTest("getNodeSummariesById"); | |
1133 } | |
1134 | |
1135 TEST_F(SyncManagerGetNodesByIdTest, GetNodeDetailsByIdFailure) { | |
1136 RunGetNodesByIdFailureTest("getNodeDetailsById"); | |
1137 } | |
1138 | |
1139 TEST_F(SyncManagerTest, GetChildNodeIds) { | |
1140 StrictMock<MockJsReplyHandler> reply_handler; | |
1141 | |
1142 JsArgList return_args; | |
1143 | |
1144 EXPECT_CALL(reply_handler, | |
1145 HandleJsReply("getChildNodeIds", _)) | |
1146 .Times(1).WillRepeatedly(SaveArg<1>(&return_args)); | |
1147 | |
1148 { | |
1149 ListValue args; | |
1150 args.Append(Value::CreateStringValue("1")); | |
1151 SendJsMessage("getChildNodeIds", | |
1152 JsArgList(&args), reply_handler.AsWeakHandle()); | |
1153 } | |
1154 | |
1155 EXPECT_EQ(1u, return_args.Get().GetSize()); | |
1156 ListValue* nodes = NULL; | |
1157 ASSERT_TRUE(return_args.Get().GetList(0, &nodes)); | |
1158 ASSERT_TRUE(nodes); | |
1159 EXPECT_EQ(6u, nodes->GetSize()); | |
1160 } | |
1161 | |
1162 TEST_F(SyncManagerTest, GetChildNodeIdsFailure) { | |
1163 StrictMock<MockJsReplyHandler> reply_handler; | |
1164 | |
1165 ListValue empty_list_args; | |
1166 empty_list_args.Append(new ListValue()); | |
1167 | |
1168 EXPECT_CALL(reply_handler, | |
1169 HandleJsReply("getChildNodeIds", | |
1170 HasArgsAsList(empty_list_args))) | |
1171 .Times(5); | |
1172 | |
1173 { | |
1174 ListValue args; | |
1175 SendJsMessage("getChildNodeIds", | |
1176 JsArgList(&args), reply_handler.AsWeakHandle()); | |
1177 } | |
1178 | |
1179 { | |
1180 ListValue args; | |
1181 args.Append(Value::CreateStringValue("")); | |
1182 SendJsMessage("getChildNodeIds", | |
1183 JsArgList(&args), reply_handler.AsWeakHandle()); | |
1184 } | |
1185 | |
1186 { | |
1187 ListValue args; | |
1188 args.Append(Value::CreateStringValue("nonsense")); | |
1189 SendJsMessage("getChildNodeIds", | |
1190 JsArgList(&args), reply_handler.AsWeakHandle()); | |
1191 } | |
1192 | |
1193 { | |
1194 ListValue args; | |
1195 args.Append(Value::CreateStringValue("0")); | |
1196 SendJsMessage("getChildNodeIds", | |
1197 JsArgList(&args), reply_handler.AsWeakHandle()); | |
1198 } | |
1199 | |
1200 { | |
1201 ListValue args; | |
1202 args.Append(Value::CreateStringValue("9999")); | |
1203 SendJsMessage("getChildNodeIds", | |
1204 JsArgList(&args), reply_handler.AsWeakHandle()); | |
1205 } | |
1206 } | |
1207 | |
1208 TEST_F(SyncManagerTest, GetAllNodesTest) { | |
1209 StrictMock<MockJsReplyHandler> reply_handler; | |
1210 JsArgList return_args; | |
1211 | |
1212 EXPECT_CALL(reply_handler, | |
1213 HandleJsReply("getAllNodes", _)) | |
1214 .Times(1).WillRepeatedly(SaveArg<1>(&return_args)); | |
1215 | |
1216 { | |
1217 ListValue args; | |
1218 SendJsMessage("getAllNodes", | |
1219 JsArgList(&args), reply_handler.AsWeakHandle()); | |
1220 } | |
1221 | |
1222 // There's not much value in verifying every attribute on every node here. | |
1223 // Most of the value of this test has already been achieved: we've verified we | |
1224 // can call the above function without crashing or leaking memory. | |
1225 // | |
1226 // Let's just check the list size and a few of its elements. Anything more | |
1227 // would make this test brittle without greatly increasing our chances of | |
1228 // catching real bugs. | |
1229 | |
1230 ListValue* node_list; | |
1231 DictionaryValue* first_result; | |
1232 | |
1233 // The resulting argument list should have one argument, a list of nodes. | |
1234 ASSERT_EQ(1U, return_args.Get().GetSize()); | |
1235 ASSERT_TRUE(return_args.Get().GetList(0, &node_list)); | |
1236 | |
1237 // The database creation logic depends on the routing info. | |
1238 // Refer to setup methods for more information. | |
1239 ModelSafeRoutingInfo routes; | |
1240 GetModelSafeRoutingInfo(&routes); | |
1241 size_t directory_size = routes.size() + 1; | |
1242 | |
1243 ASSERT_EQ(directory_size, node_list->GetSize()); | |
1244 ASSERT_TRUE(node_list->GetDictionary(0, &first_result)); | |
1245 EXPECT_TRUE(first_result->HasKey("ID")); | |
1246 EXPECT_TRUE(first_result->HasKey("NON_UNIQUE_NAME")); | |
1247 } | |
1248 | |
1249 TEST_F(SyncManagerTest, OnNotificationStateChange) { | |
1250 InSequence dummy; | |
1251 StrictMock<MockJsEventHandler> event_handler; | |
1252 | |
1253 DictionaryValue true_details; | |
1254 true_details.SetBoolean("enabled", true); | |
1255 DictionaryValue false_details; | |
1256 false_details.SetBoolean("enabled", false); | |
1257 | |
1258 EXPECT_CALL(event_handler, | |
1259 HandleJsEvent("onNotificationStateChange", | |
1260 HasDetailsAsDictionary(true_details))); | |
1261 EXPECT_CALL(event_handler, | |
1262 HandleJsEvent("onNotificationStateChange", | |
1263 HasDetailsAsDictionary(false_details))); | |
1264 | |
1265 sync_manager_.TriggerOnNotificationStateChangeForTest(true); | |
1266 sync_manager_.TriggerOnNotificationStateChangeForTest(false); | |
1267 | |
1268 SetJsEventHandler(event_handler.AsWeakHandle()); | |
1269 sync_manager_.TriggerOnNotificationStateChangeForTest(true); | |
1270 sync_manager_.TriggerOnNotificationStateChangeForTest(false); | |
1271 SetJsEventHandler(WeakHandle<JsEventHandler>()); | |
1272 | |
1273 sync_manager_.TriggerOnNotificationStateChangeForTest(true); | |
1274 sync_manager_.TriggerOnNotificationStateChangeForTest(false); | |
1275 | |
1276 // Should trigger the replies. | |
1277 PumpLoop(); | |
1278 } | |
1279 | |
1280 TEST_F(SyncManagerTest, OnIncomingNotification) { | |
1281 StrictMock<MockJsEventHandler> event_handler; | |
1282 | |
1283 const syncable::ModelTypeSet empty_model_types; | |
1284 const syncable::ModelTypeSet model_types( | |
1285 syncable::BOOKMARKS, syncable::THEMES); | |
1286 | |
1287 // Build expected_args to have a single argument with the string | |
1288 // equivalents of model_types. | |
1289 DictionaryValue expected_details; | |
1290 { | |
1291 ListValue* model_type_list = new ListValue(); | |
1292 expected_details.SetString("source", "REMOTE_NOTIFICATION"); | |
1293 expected_details.Set("changedTypes", model_type_list); | |
1294 for (syncable::ModelTypeSet::Iterator it = model_types.First(); | |
1295 it.Good(); it.Inc()) { | |
1296 model_type_list->Append( | |
1297 Value::CreateStringValue( | |
1298 syncable::ModelTypeToString(it.Get()))); | |
1299 } | |
1300 } | |
1301 | |
1302 EXPECT_CALL(event_handler, | |
1303 HandleJsEvent("onIncomingNotification", | |
1304 HasDetailsAsDictionary(expected_details))); | |
1305 | |
1306 sync_manager_.TriggerOnIncomingNotificationForTest(empty_model_types); | |
1307 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); | |
1308 | |
1309 SetJsEventHandler(event_handler.AsWeakHandle()); | |
1310 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); | |
1311 SetJsEventHandler(WeakHandle<JsEventHandler>()); | |
1312 | |
1313 sync_manager_.TriggerOnIncomingNotificationForTest(empty_model_types); | |
1314 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); | |
1315 | |
1316 // Should trigger the replies. | |
1317 PumpLoop(); | |
1318 } | |
1319 | |
1320 TEST_F(SyncManagerTest, RefreshEncryptionReady) { | |
1321 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
1322 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
1323 | |
1324 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing)); | |
1325 PumpLoop(); | |
1326 | |
1327 const syncable::ModelTypeSet encrypted_types = | |
1328 sync_manager_.GetEncryptedDataTypesForTest(); | |
1329 EXPECT_TRUE(encrypted_types.Has(syncable::PASSWORDS)); | |
1330 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest()); | |
1331 { | |
1332 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1333 ReadNode node(&trans); | |
1334 EXPECT_TRUE(node.InitByIdLookup(GetIdForDataType(syncable::NIGORI))); | |
1335 sync_pb::NigoriSpecifics nigori = node.GetNigoriSpecifics(); | |
1336 EXPECT_TRUE(nigori.has_encrypted()); | |
1337 Cryptographer* cryptographer = trans.GetCryptographer(); | |
1338 EXPECT_TRUE(cryptographer->is_ready()); | |
1339 EXPECT_TRUE(cryptographer->CanDecrypt(nigori.encrypted())); | |
1340 } | |
1341 } | |
1342 | |
1343 // Attempt to refresh encryption when nigori not downloaded. | |
1344 TEST_F(SyncManagerTest, RefreshEncryptionNotReady) { | |
1345 // Don't set up encryption (no nigori node created). | |
1346 | |
1347 // Should fail. | |
1348 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing)); | |
1349 PumpLoop(); | |
1350 | |
1351 const syncable::ModelTypeSet encrypted_types = | |
1352 sync_manager_.GetEncryptedDataTypesForTest(); | |
1353 EXPECT_TRUE(encrypted_types.Has(syncable::PASSWORDS)); // Hardcoded. | |
1354 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest()); | |
1355 } | |
1356 | |
1357 // Attempt to refresh encryption when nigori is empty. | |
1358 TEST_F(SyncManagerTest, RefreshEncryptionEmptyNigori) { | |
1359 EXPECT_TRUE(SetUpEncryption(DONT_WRITE_NIGORI, DEFAULT_ENCRYPTION)); | |
1360 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
1361 | |
1362 // Should write to nigori. | |
1363 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing)); | |
1364 PumpLoop(); | |
1365 | |
1366 const syncable::ModelTypeSet encrypted_types = | |
1367 sync_manager_.GetEncryptedDataTypesForTest(); | |
1368 EXPECT_TRUE(encrypted_types.Has(syncable::PASSWORDS)); // Hardcoded. | |
1369 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest()); | |
1370 { | |
1371 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1372 ReadNode node(&trans); | |
1373 EXPECT_TRUE(node.InitByIdLookup(GetIdForDataType(syncable::NIGORI))); | |
1374 sync_pb::NigoriSpecifics nigori = node.GetNigoriSpecifics(); | |
1375 EXPECT_TRUE(nigori.has_encrypted()); | |
1376 Cryptographer* cryptographer = trans.GetCryptographer(); | |
1377 EXPECT_TRUE(cryptographer->is_ready()); | |
1378 EXPECT_TRUE(cryptographer->CanDecrypt(nigori.encrypted())); | |
1379 } | |
1380 } | |
1381 | |
1382 TEST_F(SyncManagerTest, EncryptDataTypesWithNoData) { | |
1383 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
1384 EXPECT_CALL(observer_, | |
1385 OnEncryptedTypesChanged( | |
1386 HasModelTypes(syncable::ModelTypeSet::All()), true)); | |
1387 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
1388 sync_manager_.EnableEncryptEverything(); | |
1389 EXPECT_TRUE(sync_manager_.EncryptEverythingEnabledForTest()); | |
1390 } | |
1391 | |
1392 TEST_F(SyncManagerTest, EncryptDataTypesWithData) { | |
1393 size_t batch_size = 5; | |
1394 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
1395 | |
1396 // Create some unencrypted unsynced data. | |
1397 int64 folder = MakeFolderWithParent(sync_manager_.GetUserShare(), | |
1398 syncable::BOOKMARKS, | |
1399 GetIdForDataType(syncable::BOOKMARKS), | |
1400 NULL); | |
1401 // First batch_size nodes are children of folder. | |
1402 size_t i; | |
1403 for (i = 0; i < batch_size; ++i) { | |
1404 MakeNodeWithParent(sync_manager_.GetUserShare(), syncable::BOOKMARKS, | |
1405 base::StringPrintf("%"PRIuS"", i), folder); | |
1406 } | |
1407 // Next batch_size nodes are a different type and on their own. | |
1408 for (; i < 2*batch_size; ++i) { | |
1409 MakeNodeWithParent(sync_manager_.GetUserShare(), syncable::SESSIONS, | |
1410 base::StringPrintf("%"PRIuS"", i), | |
1411 GetIdForDataType(syncable::SESSIONS)); | |
1412 } | |
1413 // Last batch_size nodes are a third type that will not need encryption. | |
1414 for (; i < 3*batch_size; ++i) { | |
1415 MakeNodeWithParent(sync_manager_.GetUserShare(), syncable::THEMES, | |
1416 base::StringPrintf("%"PRIuS"", i), | |
1417 GetIdForDataType(syncable::THEMES)); | |
1418 } | |
1419 | |
1420 { | |
1421 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1422 EXPECT_TRUE(GetEncryptedTypes(&trans).Equals( | |
1423 Cryptographer::SensitiveTypes())); | |
1424 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
1425 trans.GetWrappedTrans(), | |
1426 trans.GetCryptographer(), | |
1427 syncable::BOOKMARKS, | |
1428 false /* not encrypted */)); | |
1429 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
1430 trans.GetWrappedTrans(), | |
1431 trans.GetCryptographer(), | |
1432 syncable::SESSIONS, | |
1433 false /* not encrypted */)); | |
1434 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
1435 trans.GetWrappedTrans(), | |
1436 trans.GetCryptographer(), | |
1437 syncable::THEMES, | |
1438 false /* not encrypted */)); | |
1439 } | |
1440 | |
1441 EXPECT_CALL(observer_, | |
1442 OnEncryptedTypesChanged( | |
1443 HasModelTypes(syncable::ModelTypeSet::All()), true)); | |
1444 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
1445 sync_manager_.EnableEncryptEverything(); | |
1446 EXPECT_TRUE(sync_manager_.EncryptEverythingEnabledForTest()); | |
1447 { | |
1448 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1449 EXPECT_TRUE(GetEncryptedTypes(&trans).Equals( | |
1450 syncable::ModelTypeSet::All())); | |
1451 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
1452 trans.GetWrappedTrans(), | |
1453 trans.GetCryptographer(), | |
1454 syncable::BOOKMARKS, | |
1455 true /* is encrypted */)); | |
1456 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
1457 trans.GetWrappedTrans(), | |
1458 trans.GetCryptographer(), | |
1459 syncable::SESSIONS, | |
1460 true /* is encrypted */)); | |
1461 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
1462 trans.GetWrappedTrans(), | |
1463 trans.GetCryptographer(), | |
1464 syncable::THEMES, | |
1465 true /* is encrypted */)); | |
1466 } | |
1467 | |
1468 // Trigger's a ReEncryptEverything with new passphrase. | |
1469 testing::Mock::VerifyAndClearExpectations(&observer_); | |
1470 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_)); | |
1471 EXPECT_CALL(observer_, OnPassphraseAccepted()); | |
1472 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
1473 sync_manager_.SetEncryptionPassphrase("new_passphrase", true); | |
1474 EXPECT_TRUE(sync_manager_.EncryptEverythingEnabledForTest()); | |
1475 { | |
1476 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1477 EXPECT_TRUE(GetEncryptedTypes(&trans).Equals( | |
1478 syncable::ModelTypeSet::All())); | |
1479 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
1480 trans.GetWrappedTrans(), | |
1481 trans.GetCryptographer(), | |
1482 syncable::BOOKMARKS, | |
1483 true /* is encrypted */)); | |
1484 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
1485 trans.GetWrappedTrans(), | |
1486 trans.GetCryptographer(), | |
1487 syncable::SESSIONS, | |
1488 true /* is encrypted */)); | |
1489 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
1490 trans.GetWrappedTrans(), | |
1491 trans.GetCryptographer(), | |
1492 syncable::THEMES, | |
1493 true /* is encrypted */)); | |
1494 } | |
1495 // Calling EncryptDataTypes with an empty encrypted types should not trigger | |
1496 // a reencryption and should just notify immediately. | |
1497 // TODO(zea): add logic to ensure nothing was written. | |
1498 testing::Mock::VerifyAndClearExpectations(&observer_); | |
1499 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_)).Times(0); | |
1500 EXPECT_CALL(observer_, OnPassphraseAccepted()).Times(0); | |
1501 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
1502 sync_manager_.EnableEncryptEverything(); | |
1503 } | |
1504 | |
1505 // Test that when there are no pending keys and the cryptographer is not | |
1506 // initialized, we add a key based on the current GAIA password. | |
1507 // (case 1 in SyncManager::SyncInternal::SetEncryptionPassphrase) | |
1508 TEST_F(SyncManagerTest, SetInitialGaiaPass) { | |
1509 EXPECT_FALSE(SetUpEncryption(DONT_WRITE_NIGORI, UNINITIALIZED)); | |
1510 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_)); | |
1511 EXPECT_CALL(observer_, OnPassphraseAccepted()); | |
1512 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
1513 sync_manager_.SetEncryptionPassphrase("new_passphrase", false); | |
1514 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest()); | |
1515 { | |
1516 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1517 ReadNode node(&trans); | |
1518 EXPECT_TRUE(node.InitByTagLookup(kNigoriTag)); | |
1519 sync_pb::NigoriSpecifics nigori = node.GetNigoriSpecifics(); | |
1520 Cryptographer* cryptographer = trans.GetCryptographer(); | |
1521 EXPECT_TRUE(cryptographer->is_ready()); | |
1522 EXPECT_TRUE(cryptographer->CanDecrypt(nigori.encrypted())); | |
1523 } | |
1524 } | |
1525 | |
1526 // Test that when there are no pending keys and we have on the old GAIA | |
1527 // password, we update and re-encrypt everything with the new GAIA password. | |
1528 // (case 1 in SyncManager::SyncInternal::SetEncryptionPassphrase) | |
1529 TEST_F(SyncManagerTest, UpdateGaiaPass) { | |
1530 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
1531 Cryptographer verifier(&encryptor_); | |
1532 { | |
1533 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1534 Cryptographer* cryptographer = trans.GetCryptographer(); | |
1535 std::string bootstrap_token; | |
1536 cryptographer->GetBootstrapToken(&bootstrap_token); | |
1537 verifier.Bootstrap(bootstrap_token); | |
1538 } | |
1539 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_)); | |
1540 EXPECT_CALL(observer_, OnPassphraseAccepted()); | |
1541 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
1542 sync_manager_.SetEncryptionPassphrase("new_passphrase", false); | |
1543 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest()); | |
1544 { | |
1545 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1546 Cryptographer* cryptographer = trans.GetCryptographer(); | |
1547 EXPECT_TRUE(cryptographer->is_ready()); | |
1548 // Verify the default key has changed. | |
1549 sync_pb::EncryptedData encrypted; | |
1550 cryptographer->GetKeys(&encrypted); | |
1551 EXPECT_FALSE(verifier.CanDecrypt(encrypted)); | |
1552 } | |
1553 } | |
1554 | |
1555 // Sets a new explicit passphrase. This should update the bootstrap token | |
1556 // and re-encrypt everything. | |
1557 // (case 2 in SyncManager::SyncInternal::SetEncryptionPassphrase) | |
1558 TEST_F(SyncManagerTest, SetPassphraseWithPassword) { | |
1559 Cryptographer verifier(&encryptor_); | |
1560 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
1561 { | |
1562 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1563 // Store the default (soon to be old) key. | |
1564 Cryptographer* cryptographer = trans.GetCryptographer(); | |
1565 std::string bootstrap_token; | |
1566 cryptographer->GetBootstrapToken(&bootstrap_token); | |
1567 verifier.Bootstrap(bootstrap_token); | |
1568 | |
1569 ReadNode root_node(&trans); | |
1570 root_node.InitByRootLookup(); | |
1571 | |
1572 WriteNode password_node(&trans); | |
1573 EXPECT_TRUE(password_node.InitUniqueByCreation(syncable::PASSWORDS, | |
1574 root_node, "foo")); | |
1575 sync_pb::PasswordSpecificsData data; | |
1576 data.set_password_value("secret"); | |
1577 password_node.SetPasswordSpecifics(data); | |
1578 } | |
1579 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_)); | |
1580 EXPECT_CALL(observer_, OnPassphraseAccepted()); | |
1581 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
1582 sync_manager_.SetEncryptionPassphrase("new_passphrase", true); | |
1583 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest()); | |
1584 { | |
1585 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1586 Cryptographer* cryptographer = trans.GetCryptographer(); | |
1587 EXPECT_TRUE(cryptographer->is_ready()); | |
1588 // Verify the default key has changed. | |
1589 sync_pb::EncryptedData encrypted; | |
1590 cryptographer->GetKeys(&encrypted); | |
1591 EXPECT_FALSE(verifier.CanDecrypt(encrypted)); | |
1592 | |
1593 ReadNode password_node(&trans); | |
1594 EXPECT_TRUE(password_node.InitByClientTagLookup(syncable::PASSWORDS, | |
1595 "foo")); | |
1596 const sync_pb::PasswordSpecificsData& data = | |
1597 password_node.GetPasswordSpecifics(); | |
1598 EXPECT_EQ("secret", data.password_value()); | |
1599 } | |
1600 } | |
1601 | |
1602 // Manually set the pending keys in the cryptographer/nigori to reflect the data | |
1603 // being encrypted with a new (unprovided) GAIA password, then supply the | |
1604 // password. | |
1605 // (case 7 in SyncManager::SyncInternal::SetDecryptionPassphrase) | |
1606 TEST_F(SyncManagerTest, SupplyPendingGAIAPass) { | |
1607 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
1608 Cryptographer other_cryptographer(&encryptor_); | |
1609 { | |
1610 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1611 Cryptographer* cryptographer = trans.GetCryptographer(); | |
1612 std::string bootstrap_token; | |
1613 cryptographer->GetBootstrapToken(&bootstrap_token); | |
1614 other_cryptographer.Bootstrap(bootstrap_token); | |
1615 | |
1616 // Now update the nigori to reflect the new keys, and update the | |
1617 // cryptographer to have pending keys. | |
1618 KeyParams params = {"localhost", "dummy", "passphrase2"}; | |
1619 other_cryptographer.AddKey(params); | |
1620 WriteNode node(&trans); | |
1621 EXPECT_TRUE(node.InitByTagLookup(kNigoriTag)); | |
1622 sync_pb::NigoriSpecifics nigori; | |
1623 other_cryptographer.GetKeys(nigori.mutable_encrypted()); | |
1624 cryptographer->Update(nigori); | |
1625 EXPECT_TRUE(cryptographer->has_pending_keys()); | |
1626 node.SetNigoriSpecifics(nigori); | |
1627 } | |
1628 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_)); | |
1629 EXPECT_CALL(observer_, OnPassphraseAccepted()); | |
1630 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
1631 sync_manager_.SetDecryptionPassphrase("passphrase2"); | |
1632 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest()); | |
1633 { | |
1634 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1635 Cryptographer* cryptographer = trans.GetCryptographer(); | |
1636 EXPECT_TRUE(cryptographer->is_ready()); | |
1637 // Verify we're encrypting with the new key. | |
1638 sync_pb::EncryptedData encrypted; | |
1639 cryptographer->GetKeys(&encrypted); | |
1640 EXPECT_TRUE(other_cryptographer.CanDecrypt(encrypted)); | |
1641 } | |
1642 } | |
1643 | |
1644 // Manually set the pending keys in the cryptographer/nigori to reflect the data | |
1645 // being encrypted with an old (unprovided) GAIA password. Attempt to supply | |
1646 // the current GAIA password and verify the bootstrap token is updated. Then | |
1647 // supply the old GAIA password, and verify we re-encrypt all data with the | |
1648 // new GAIA password. | |
1649 // (cases 4 and 5 in SyncManager::SyncInternal::SetEncryptionPassphrase) | |
1650 TEST_F(SyncManagerTest, SupplyPendingOldGAIAPass) { | |
1651 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
1652 Cryptographer other_cryptographer(&encryptor_); | |
1653 { | |
1654 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1655 Cryptographer* cryptographer = trans.GetCryptographer(); | |
1656 std::string bootstrap_token; | |
1657 cryptographer->GetBootstrapToken(&bootstrap_token); | |
1658 other_cryptographer.Bootstrap(bootstrap_token); | |
1659 | |
1660 // Now update the nigori to reflect the new keys, and update the | |
1661 // cryptographer to have pending keys. | |
1662 KeyParams params = {"localhost", "dummy", "old_gaia"}; | |
1663 other_cryptographer.AddKey(params); | |
1664 WriteNode node(&trans); | |
1665 EXPECT_TRUE(node.InitByTagLookup(kNigoriTag)); | |
1666 sync_pb::NigoriSpecifics nigori; | |
1667 other_cryptographer.GetKeys(nigori.mutable_encrypted()); | |
1668 node.SetNigoriSpecifics(nigori); | |
1669 cryptographer->Update(nigori); | |
1670 | |
1671 // other_cryptographer now contains all encryption keys, and is encrypting | |
1672 // with the newest gaia. | |
1673 KeyParams new_params = {"localhost", "dummy", "new_gaia"}; | |
1674 other_cryptographer.AddKey(new_params); | |
1675 } | |
1676 // The bootstrap token should have been updated. Save it to ensure it's based | |
1677 // on the new GAIA password. | |
1678 std::string bootstrap_token; | |
1679 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_)) | |
1680 .WillOnce(SaveArg<0>(&bootstrap_token)); | |
1681 EXPECT_CALL(observer_, OnPassphraseRequired(_,_)); | |
1682 sync_manager_.SetEncryptionPassphrase("new_gaia", false); | |
1683 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest()); | |
1684 testing::Mock::VerifyAndClearExpectations(&observer_); | |
1685 { | |
1686 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1687 Cryptographer* cryptographer = trans.GetCryptographer(); | |
1688 EXPECT_TRUE(cryptographer->is_initialized()); | |
1689 EXPECT_FALSE(cryptographer->is_ready()); | |
1690 // Verify we're encrypting with the new key, even though we have pending | |
1691 // keys. | |
1692 sync_pb::EncryptedData encrypted; | |
1693 other_cryptographer.GetKeys(&encrypted); | |
1694 EXPECT_TRUE(cryptographer->CanDecrypt(encrypted)); | |
1695 } | |
1696 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_)); | |
1697 EXPECT_CALL(observer_, OnPassphraseAccepted()); | |
1698 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
1699 sync_manager_.SetEncryptionPassphrase("old_gaia", false); | |
1700 { | |
1701 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1702 Cryptographer* cryptographer = trans.GetCryptographer(); | |
1703 EXPECT_TRUE(cryptographer->is_ready()); | |
1704 | |
1705 // Verify we're encrypting with the new key. | |
1706 sync_pb::EncryptedData encrypted; | |
1707 other_cryptographer.GetKeys(&encrypted); | |
1708 EXPECT_TRUE(cryptographer->CanDecrypt(encrypted)); | |
1709 | |
1710 // Verify the saved bootstrap token is based on the new gaia password. | |
1711 Cryptographer temp_cryptographer(&encryptor_); | |
1712 temp_cryptographer.Bootstrap(bootstrap_token); | |
1713 EXPECT_TRUE(temp_cryptographer.CanDecrypt(encrypted)); | |
1714 } | |
1715 } | |
1716 | |
1717 // Manually set the pending keys in the cryptographer/nigori to reflect the data | |
1718 // being encrypted with an explicit (unprovided) passphrase, then supply the | |
1719 // passphrase. | |
1720 // (case 9 in SyncManager::SyncInternal::SetDecryptionPassphrase) | |
1721 TEST_F(SyncManagerTest, SupplyPendingExplicitPass) { | |
1722 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
1723 Cryptographer other_cryptographer(&encryptor_); | |
1724 { | |
1725 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1726 Cryptographer* cryptographer = trans.GetCryptographer(); | |
1727 std::string bootstrap_token; | |
1728 cryptographer->GetBootstrapToken(&bootstrap_token); | |
1729 other_cryptographer.Bootstrap(bootstrap_token); | |
1730 | |
1731 // Now update the nigori to reflect the new keys, and update the | |
1732 // cryptographer to have pending keys. | |
1733 KeyParams params = {"localhost", "dummy", "explicit"}; | |
1734 other_cryptographer.AddKey(params); | |
1735 WriteNode node(&trans); | |
1736 EXPECT_TRUE(node.InitByTagLookup(kNigoriTag)); | |
1737 sync_pb::NigoriSpecifics nigori; | |
1738 other_cryptographer.GetKeys(nigori.mutable_encrypted()); | |
1739 cryptographer->Update(nigori); | |
1740 EXPECT_TRUE(cryptographer->has_pending_keys()); | |
1741 nigori.set_using_explicit_passphrase(true); | |
1742 node.SetNigoriSpecifics(nigori); | |
1743 } | |
1744 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_)); | |
1745 EXPECT_CALL(observer_, OnPassphraseAccepted()); | |
1746 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
1747 sync_manager_.SetDecryptionPassphrase("explicit"); | |
1748 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest()); | |
1749 { | |
1750 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1751 Cryptographer* cryptographer = trans.GetCryptographer(); | |
1752 EXPECT_TRUE(cryptographer->is_ready()); | |
1753 // Verify we're encrypting with the new key. | |
1754 sync_pb::EncryptedData encrypted; | |
1755 cryptographer->GetKeys(&encrypted); | |
1756 EXPECT_TRUE(other_cryptographer.CanDecrypt(encrypted)); | |
1757 } | |
1758 } | |
1759 | |
1760 // Manually set the pending keys in the cryptographer/nigori to reflect the data | |
1761 // being encrypted with a new (unprovided) GAIA password, then supply the | |
1762 // password as a user-provided password. | |
1763 // This is the android case 7/8. | |
1764 TEST_F(SyncManagerTest, SupplyPendingGAIAPassUserProvided) { | |
1765 EXPECT_FALSE(SetUpEncryption(DONT_WRITE_NIGORI, UNINITIALIZED)); | |
1766 Cryptographer other_cryptographer(&encryptor_); | |
1767 { | |
1768 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1769 Cryptographer* cryptographer = trans.GetCryptographer(); | |
1770 // Now update the nigori to reflect the new keys, and update the | |
1771 // cryptographer to have pending keys. | |
1772 KeyParams params = {"localhost", "dummy", "passphrase"}; | |
1773 other_cryptographer.AddKey(params); | |
1774 WriteNode node(&trans); | |
1775 EXPECT_TRUE(node.InitByTagLookup(kNigoriTag)); | |
1776 sync_pb::NigoriSpecifics nigori; | |
1777 other_cryptographer.GetKeys(nigori.mutable_encrypted()); | |
1778 node.SetNigoriSpecifics(nigori); | |
1779 cryptographer->Update(nigori); | |
1780 EXPECT_FALSE(cryptographer->is_ready()); | |
1781 } | |
1782 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_)); | |
1783 EXPECT_CALL(observer_, OnPassphraseAccepted()); | |
1784 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
1785 sync_manager_.SetEncryptionPassphrase("passphrase", false); | |
1786 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest()); | |
1787 { | |
1788 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1789 Cryptographer* cryptographer = trans.GetCryptographer(); | |
1790 EXPECT_TRUE(cryptographer->is_ready()); | |
1791 } | |
1792 } | |
1793 | |
1794 TEST_F(SyncManagerTest, SetPassphraseWithEmptyPasswordNode) { | |
1795 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
1796 int64 node_id = 0; | |
1797 std::string tag = "foo"; | |
1798 { | |
1799 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1800 ReadNode root_node(&trans); | |
1801 root_node.InitByRootLookup(); | |
1802 | |
1803 WriteNode password_node(&trans); | |
1804 EXPECT_TRUE(password_node.InitUniqueByCreation(syncable::PASSWORDS, | |
1805 root_node, tag)); | |
1806 node_id = password_node.GetId(); | |
1807 } | |
1808 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_)); | |
1809 EXPECT_CALL(observer_, OnPassphraseAccepted()); | |
1810 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
1811 sync_manager_.SetEncryptionPassphrase("new_passphrase", true); | |
1812 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest()); | |
1813 { | |
1814 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1815 ReadNode password_node(&trans); | |
1816 EXPECT_FALSE(password_node.InitByClientTagLookup(syncable::PASSWORDS, | |
1817 tag)); | |
1818 } | |
1819 { | |
1820 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1821 ReadNode password_node(&trans); | |
1822 EXPECT_FALSE(password_node.InitByIdLookup(node_id)); | |
1823 } | |
1824 } | |
1825 | |
1826 TEST_F(SyncManagerTest, NudgeDelayTest) { | |
1827 EXPECT_EQ(sync_manager_.GetNudgeDelayTimeDelta(syncable::BOOKMARKS), | |
1828 base::TimeDelta::FromMilliseconds( | |
1829 SyncManager::kDefaultNudgeDelayMilliseconds)); | |
1830 | |
1831 EXPECT_EQ(sync_manager_.GetNudgeDelayTimeDelta(syncable::AUTOFILL), | |
1832 base::TimeDelta::FromSeconds( | |
1833 browser_sync::kDefaultShortPollIntervalSeconds)); | |
1834 | |
1835 EXPECT_EQ(sync_manager_.GetNudgeDelayTimeDelta(syncable::PREFERENCES), | |
1836 base::TimeDelta::FromMilliseconds( | |
1837 SyncManager::kPreferencesNudgeDelayMilliseconds)); | |
1838 } | |
1839 | |
1840 // Friended by WriteNode, so can't be in an anonymouse namespace. | |
1841 TEST_F(SyncManagerTest, EncryptBookmarksWithLegacyData) { | |
1842 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
1843 std::string title; | |
1844 SyncAPINameToServerName("Google", &title); | |
1845 std::string url = "http://www.google.com"; | |
1846 std::string raw_title2 = ".."; // An invalid cosmo title. | |
1847 std::string title2; | |
1848 SyncAPINameToServerName(raw_title2, &title2); | |
1849 std::string url2 = "http://www.bla.com"; | |
1850 | |
1851 // Create a bookmark using the legacy format. | |
1852 int64 node_id1 = MakeNode(sync_manager_.GetUserShare(), | |
1853 syncable::BOOKMARKS, | |
1854 "testtag"); | |
1855 int64 node_id2 = MakeNode(sync_manager_.GetUserShare(), | |
1856 syncable::BOOKMARKS, | |
1857 "testtag2"); | |
1858 { | |
1859 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1860 WriteNode node(&trans); | |
1861 EXPECT_TRUE(node.InitByIdLookup(node_id1)); | |
1862 | |
1863 sync_pb::EntitySpecifics entity_specifics; | |
1864 entity_specifics.mutable_bookmark()->set_url(url); | |
1865 node.SetEntitySpecifics(entity_specifics); | |
1866 | |
1867 // Set the old style title. | |
1868 syncable::MutableEntry* node_entry = node.entry_; | |
1869 node_entry->Put(syncable::NON_UNIQUE_NAME, title); | |
1870 | |
1871 WriteNode node2(&trans); | |
1872 EXPECT_TRUE(node2.InitByIdLookup(node_id2)); | |
1873 | |
1874 sync_pb::EntitySpecifics entity_specifics2; | |
1875 entity_specifics2.mutable_bookmark()->set_url(url2); | |
1876 node2.SetEntitySpecifics(entity_specifics2); | |
1877 | |
1878 // Set the old style title. | |
1879 syncable::MutableEntry* node_entry2 = node2.entry_; | |
1880 node_entry2->Put(syncable::NON_UNIQUE_NAME, title2); | |
1881 } | |
1882 | |
1883 { | |
1884 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1885 ReadNode node(&trans); | |
1886 EXPECT_TRUE(node.InitByIdLookup(node_id1)); | |
1887 EXPECT_EQ(syncable::BOOKMARKS, node.GetModelType()); | |
1888 EXPECT_EQ(title, node.GetTitle()); | |
1889 EXPECT_EQ(title, node.GetBookmarkSpecifics().title()); | |
1890 EXPECT_EQ(url, node.GetBookmarkSpecifics().url()); | |
1891 | |
1892 ReadNode node2(&trans); | |
1893 EXPECT_TRUE(node2.InitByIdLookup(node_id2)); | |
1894 EXPECT_EQ(syncable::BOOKMARKS, node2.GetModelType()); | |
1895 // We should de-canonicalize the title in GetTitle(), but the title in the | |
1896 // specifics should be stored in the server legal form. | |
1897 EXPECT_EQ(raw_title2, node2.GetTitle()); | |
1898 EXPECT_EQ(title2, node2.GetBookmarkSpecifics().title()); | |
1899 EXPECT_EQ(url2, node2.GetBookmarkSpecifics().url()); | |
1900 } | |
1901 | |
1902 { | |
1903 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1904 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
1905 trans.GetWrappedTrans(), | |
1906 trans.GetCryptographer(), | |
1907 syncable::BOOKMARKS, | |
1908 false /* not encrypted */)); | |
1909 } | |
1910 | |
1911 EXPECT_CALL(observer_, | |
1912 OnEncryptedTypesChanged( | |
1913 HasModelTypes(syncable::ModelTypeSet::All()), true)); | |
1914 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
1915 sync_manager_.EnableEncryptEverything(); | |
1916 EXPECT_TRUE(sync_manager_.EncryptEverythingEnabledForTest()); | |
1917 | |
1918 { | |
1919 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1920 EXPECT_TRUE(GetEncryptedTypes(&trans).Equals( | |
1921 syncable::ModelTypeSet::All())); | |
1922 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest( | |
1923 trans.GetWrappedTrans(), | |
1924 trans.GetCryptographer(), | |
1925 syncable::BOOKMARKS, | |
1926 true /* is encrypted */)); | |
1927 | |
1928 ReadNode node(&trans); | |
1929 EXPECT_TRUE(node.InitByIdLookup(node_id1)); | |
1930 EXPECT_EQ(syncable::BOOKMARKS, node.GetModelType()); | |
1931 EXPECT_EQ(title, node.GetTitle()); | |
1932 EXPECT_EQ(title, node.GetBookmarkSpecifics().title()); | |
1933 EXPECT_EQ(url, node.GetBookmarkSpecifics().url()); | |
1934 | |
1935 ReadNode node2(&trans); | |
1936 EXPECT_TRUE(node2.InitByIdLookup(node_id2)); | |
1937 EXPECT_EQ(syncable::BOOKMARKS, node2.GetModelType()); | |
1938 // We should de-canonicalize the title in GetTitle(), but the title in the | |
1939 // specifics should be stored in the server legal form. | |
1940 EXPECT_EQ(raw_title2, node2.GetTitle()); | |
1941 EXPECT_EQ(title2, node2.GetBookmarkSpecifics().title()); | |
1942 EXPECT_EQ(url2, node2.GetBookmarkSpecifics().url()); | |
1943 } | |
1944 } | |
1945 | |
1946 // Create a bookmark and set the title/url, then verify the data was properly | |
1947 // set. This replicates the unique way bookmarks have of creating sync nodes. | |
1948 // See BookmarkChangeProcessor::PlaceSyncNode(..). | |
1949 TEST_F(SyncManagerTest, CreateLocalBookmark) { | |
1950 std::string title = "title"; | |
1951 GURL url("url"); | |
1952 { | |
1953 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1954 ReadNode root_node(&trans); | |
1955 root_node.InitByRootLookup(); | |
1956 WriteNode node(&trans); | |
1957 ASSERT_TRUE(node.InitByCreation(syncable::BOOKMARKS, root_node, NULL)); | |
1958 node.SetIsFolder(false); | |
1959 node.SetTitle(UTF8ToWide(title)); | |
1960 node.SetURL(url); | |
1961 } | |
1962 { | |
1963 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1964 ReadNode root_node(&trans); | |
1965 root_node.InitByRootLookup(); | |
1966 int64 child_id = root_node.GetFirstChildId(); | |
1967 | |
1968 ReadNode node(&trans); | |
1969 ASSERT_TRUE(node.InitByIdLookup(child_id)); | |
1970 EXPECT_FALSE(node.GetIsFolder()); | |
1971 EXPECT_EQ(title, node.GetTitle()); | |
1972 EXPECT_EQ(url, node.GetURL()); | |
1973 } | |
1974 } | |
1975 | |
1976 // Verifies WriteNode::UpdateEntryWithEncryption does not make unnecessary | |
1977 // changes. | |
1978 TEST_F(SyncManagerTest, UpdateEntryWithEncryption) { | |
1979 std::string client_tag = "title"; | |
1980 sync_pb::EntitySpecifics entity_specifics; | |
1981 entity_specifics.mutable_bookmark()->set_url("url"); | |
1982 entity_specifics.mutable_bookmark()->set_title("title"); | |
1983 MakeServerNode(sync_manager_.GetUserShare(), syncable::BOOKMARKS, client_tag, | |
1984 BaseNode::GenerateSyncableHash(syncable::BOOKMARKS, | |
1985 client_tag), | |
1986 entity_specifics); | |
1987 // New node shouldn't start off unsynced. | |
1988 EXPECT_FALSE(ResetUnsyncedEntry(syncable::BOOKMARKS, client_tag)); | |
1989 // Manually change to the same data. Should not set is_unsynced. | |
1990 { | |
1991 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
1992 WriteNode node(&trans); | |
1993 EXPECT_TRUE(node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag)); | |
1994 node.SetEntitySpecifics(entity_specifics); | |
1995 } | |
1996 EXPECT_FALSE(ResetUnsyncedEntry(syncable::BOOKMARKS, client_tag)); | |
1997 | |
1998 // Encrypt the datatatype, should set is_unsynced. | |
1999 EXPECT_CALL(observer_, | |
2000 OnEncryptedTypesChanged( | |
2001 HasModelTypes(syncable::ModelTypeSet::All()), true)); | |
2002 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
2003 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, FULL_ENCRYPTION)); | |
2004 | |
2005 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing)); | |
2006 PumpLoop(); | |
2007 { | |
2008 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2009 ReadNode node(&trans); | |
2010 EXPECT_TRUE(node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag)); | |
2011 const syncable::Entry* node_entry = node.GetEntry(); | |
2012 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); | |
2013 EXPECT_TRUE(specifics.has_encrypted()); | |
2014 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); | |
2015 Cryptographer* cryptographer = trans.GetCryptographer(); | |
2016 EXPECT_TRUE(cryptographer->is_ready()); | |
2017 EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey( | |
2018 specifics.encrypted())); | |
2019 } | |
2020 EXPECT_TRUE(ResetUnsyncedEntry(syncable::BOOKMARKS, client_tag)); | |
2021 | |
2022 // Set a new passphrase. Should set is_unsynced. | |
2023 testing::Mock::VerifyAndClearExpectations(&observer_); | |
2024 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_)); | |
2025 EXPECT_CALL(observer_, OnPassphraseAccepted()); | |
2026 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
2027 sync_manager_.SetEncryptionPassphrase("new_passphrase", true); | |
2028 { | |
2029 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2030 ReadNode node(&trans); | |
2031 EXPECT_TRUE(node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag)); | |
2032 const syncable::Entry* node_entry = node.GetEntry(); | |
2033 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); | |
2034 EXPECT_TRUE(specifics.has_encrypted()); | |
2035 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); | |
2036 Cryptographer* cryptographer = trans.GetCryptographer(); | |
2037 EXPECT_TRUE(cryptographer->is_ready()); | |
2038 EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey( | |
2039 specifics.encrypted())); | |
2040 } | |
2041 EXPECT_TRUE(ResetUnsyncedEntry(syncable::BOOKMARKS, client_tag)); | |
2042 | |
2043 // Force a re-encrypt everything. Should not set is_unsynced. | |
2044 testing::Mock::VerifyAndClearExpectations(&observer_); | |
2045 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
2046 | |
2047 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing)); | |
2048 PumpLoop(); | |
2049 | |
2050 { | |
2051 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2052 ReadNode node(&trans); | |
2053 EXPECT_TRUE(node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag)); | |
2054 const syncable::Entry* node_entry = node.GetEntry(); | |
2055 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); | |
2056 EXPECT_TRUE(specifics.has_encrypted()); | |
2057 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); | |
2058 Cryptographer* cryptographer = trans.GetCryptographer(); | |
2059 EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey( | |
2060 specifics.encrypted())); | |
2061 } | |
2062 EXPECT_FALSE(ResetUnsyncedEntry(syncable::BOOKMARKS, client_tag)); | |
2063 | |
2064 // Manually change to the same data. Should not set is_unsynced. | |
2065 { | |
2066 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2067 WriteNode node(&trans); | |
2068 EXPECT_TRUE(node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag)); | |
2069 node.SetEntitySpecifics(entity_specifics); | |
2070 const syncable::Entry* node_entry = node.GetEntry(); | |
2071 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); | |
2072 EXPECT_TRUE(specifics.has_encrypted()); | |
2073 EXPECT_FALSE(node_entry->Get(IS_UNSYNCED)); | |
2074 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); | |
2075 Cryptographer* cryptographer = trans.GetCryptographer(); | |
2076 EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey( | |
2077 specifics.encrypted())); | |
2078 } | |
2079 EXPECT_FALSE(ResetUnsyncedEntry(syncable::BOOKMARKS, client_tag)); | |
2080 | |
2081 // Manually change to different data. Should set is_unsynced. | |
2082 { | |
2083 entity_specifics.mutable_bookmark()->set_url("url2"); | |
2084 entity_specifics.mutable_bookmark()->set_title("title2"); | |
2085 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2086 WriteNode node(&trans); | |
2087 EXPECT_TRUE(node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag)); | |
2088 node.SetEntitySpecifics(entity_specifics); | |
2089 const syncable::Entry* node_entry = node.GetEntry(); | |
2090 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); | |
2091 EXPECT_TRUE(specifics.has_encrypted()); | |
2092 EXPECT_TRUE(node_entry->Get(IS_UNSYNCED)); | |
2093 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); | |
2094 Cryptographer* cryptographer = trans.GetCryptographer(); | |
2095 EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey( | |
2096 specifics.encrypted())); | |
2097 } | |
2098 } | |
2099 | |
2100 // Passwords have their own handling for encryption. Verify it does not result | |
2101 // in unnecessary writes via SetEntitySpecifics. | |
2102 TEST_F(SyncManagerTest, UpdatePasswordSetEntitySpecificsNoChange) { | |
2103 std::string client_tag = "title"; | |
2104 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
2105 sync_pb::EntitySpecifics entity_specifics; | |
2106 { | |
2107 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2108 Cryptographer* cryptographer = trans.GetCryptographer(); | |
2109 sync_pb::PasswordSpecificsData data; | |
2110 data.set_password_value("secret"); | |
2111 cryptographer->Encrypt( | |
2112 data, | |
2113 entity_specifics.mutable_password()-> | |
2114 mutable_encrypted()); | |
2115 } | |
2116 MakeServerNode(sync_manager_.GetUserShare(), syncable::PASSWORDS, client_tag, | |
2117 BaseNode::GenerateSyncableHash(syncable::PASSWORDS, | |
2118 client_tag), | |
2119 entity_specifics); | |
2120 // New node shouldn't start off unsynced. | |
2121 EXPECT_FALSE(ResetUnsyncedEntry(syncable::PASSWORDS, client_tag)); | |
2122 | |
2123 // Manually change to the same data via SetEntitySpecifics. Should not set | |
2124 // is_unsynced. | |
2125 { | |
2126 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2127 WriteNode node(&trans); | |
2128 EXPECT_TRUE(node.InitByClientTagLookup(syncable::PASSWORDS, client_tag)); | |
2129 node.SetEntitySpecifics(entity_specifics); | |
2130 } | |
2131 EXPECT_FALSE(ResetUnsyncedEntry(syncable::PASSWORDS, client_tag)); | |
2132 } | |
2133 | |
2134 // Passwords have their own handling for encryption. Verify it does not result | |
2135 // in unnecessary writes via SetPasswordSpecifics. | |
2136 TEST_F(SyncManagerTest, UpdatePasswordSetPasswordSpecifics) { | |
2137 std::string client_tag = "title"; | |
2138 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
2139 sync_pb::EntitySpecifics entity_specifics; | |
2140 { | |
2141 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2142 Cryptographer* cryptographer = trans.GetCryptographer(); | |
2143 sync_pb::PasswordSpecificsData data; | |
2144 data.set_password_value("secret"); | |
2145 cryptographer->Encrypt( | |
2146 data, | |
2147 entity_specifics.mutable_password()-> | |
2148 mutable_encrypted()); | |
2149 } | |
2150 MakeServerNode(sync_manager_.GetUserShare(), syncable::PASSWORDS, client_tag, | |
2151 BaseNode::GenerateSyncableHash(syncable::PASSWORDS, | |
2152 client_tag), | |
2153 entity_specifics); | |
2154 // New node shouldn't start off unsynced. | |
2155 EXPECT_FALSE(ResetUnsyncedEntry(syncable::PASSWORDS, client_tag)); | |
2156 | |
2157 // Manually change to the same data via SetPasswordSpecifics. Should not set | |
2158 // is_unsynced. | |
2159 { | |
2160 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2161 WriteNode node(&trans); | |
2162 EXPECT_TRUE(node.InitByClientTagLookup(syncable::PASSWORDS, client_tag)); | |
2163 node.SetPasswordSpecifics(node.GetPasswordSpecifics()); | |
2164 } | |
2165 EXPECT_FALSE(ResetUnsyncedEntry(syncable::PASSWORDS, client_tag)); | |
2166 | |
2167 // Manually change to different data. Should set is_unsynced. | |
2168 { | |
2169 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2170 WriteNode node(&trans); | |
2171 EXPECT_TRUE(node.InitByClientTagLookup(syncable::PASSWORDS, client_tag)); | |
2172 Cryptographer* cryptographer = trans.GetCryptographer(); | |
2173 sync_pb::PasswordSpecificsData data; | |
2174 data.set_password_value("secret2"); | |
2175 cryptographer->Encrypt( | |
2176 data, | |
2177 entity_specifics.mutable_password()->mutable_encrypted()); | |
2178 node.SetPasswordSpecifics(data); | |
2179 const syncable::Entry* node_entry = node.GetEntry(); | |
2180 EXPECT_TRUE(node_entry->Get(IS_UNSYNCED)); | |
2181 } | |
2182 } | |
2183 | |
2184 // Passwords have their own handling for encryption. Verify setting a new | |
2185 // passphrase updates the data. | |
2186 TEST_F(SyncManagerTest, UpdatePasswordNewPassphrase) { | |
2187 std::string client_tag = "title"; | |
2188 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
2189 sync_pb::EntitySpecifics entity_specifics; | |
2190 { | |
2191 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2192 Cryptographer* cryptographer = trans.GetCryptographer(); | |
2193 sync_pb::PasswordSpecificsData data; | |
2194 data.set_password_value("secret"); | |
2195 cryptographer->Encrypt( | |
2196 data, | |
2197 entity_specifics.mutable_password()->mutable_encrypted()); | |
2198 } | |
2199 MakeServerNode(sync_manager_.GetUserShare(), syncable::PASSWORDS, client_tag, | |
2200 BaseNode::GenerateSyncableHash(syncable::PASSWORDS, | |
2201 client_tag), | |
2202 entity_specifics); | |
2203 // New node shouldn't start off unsynced. | |
2204 EXPECT_FALSE(ResetUnsyncedEntry(syncable::PASSWORDS, client_tag)); | |
2205 | |
2206 // Set a new passphrase. Should set is_unsynced. | |
2207 testing::Mock::VerifyAndClearExpectations(&observer_); | |
2208 EXPECT_CALL(observer_, OnBootstrapTokenUpdated(_)); | |
2209 EXPECT_CALL(observer_, OnPassphraseAccepted()); | |
2210 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
2211 sync_manager_.SetEncryptionPassphrase("new_passphrase", true); | |
2212 EXPECT_TRUE(ResetUnsyncedEntry(syncable::PASSWORDS, client_tag)); | |
2213 } | |
2214 | |
2215 // Passwords have their own handling for encryption. Verify it does not result | |
2216 // in unnecessary writes via ReencryptEverything. | |
2217 TEST_F(SyncManagerTest, UpdatePasswordReencryptEverything) { | |
2218 std::string client_tag = "title"; | |
2219 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
2220 sync_pb::EntitySpecifics entity_specifics; | |
2221 { | |
2222 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2223 Cryptographer* cryptographer = trans.GetCryptographer(); | |
2224 sync_pb::PasswordSpecificsData data; | |
2225 data.set_password_value("secret"); | |
2226 cryptographer->Encrypt( | |
2227 data, | |
2228 entity_specifics.mutable_password()->mutable_encrypted()); | |
2229 } | |
2230 MakeServerNode(sync_manager_.GetUserShare(), syncable::PASSWORDS, client_tag, | |
2231 BaseNode::GenerateSyncableHash(syncable::PASSWORDS, | |
2232 client_tag), | |
2233 entity_specifics); | |
2234 // New node shouldn't start off unsynced. | |
2235 EXPECT_FALSE(ResetUnsyncedEntry(syncable::PASSWORDS, client_tag)); | |
2236 | |
2237 // Force a re-encrypt everything. Should not set is_unsynced. | |
2238 testing::Mock::VerifyAndClearExpectations(&observer_); | |
2239 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
2240 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing)); | |
2241 PumpLoop(); | |
2242 EXPECT_FALSE(ResetUnsyncedEntry(syncable::PASSWORDS, client_tag)); | |
2243 } | |
2244 | |
2245 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for bookmarks | |
2246 // when we write the same data, but does set it when we write new data. | |
2247 TEST_F(SyncManagerTest, SetBookmarkTitle) { | |
2248 std::string client_tag = "title"; | |
2249 sync_pb::EntitySpecifics entity_specifics; | |
2250 entity_specifics.mutable_bookmark()->set_url("url"); | |
2251 entity_specifics.mutable_bookmark()->set_title("title"); | |
2252 MakeServerNode(sync_manager_.GetUserShare(), syncable::BOOKMARKS, client_tag, | |
2253 BaseNode::GenerateSyncableHash(syncable::BOOKMARKS, | |
2254 client_tag), | |
2255 entity_specifics); | |
2256 // New node shouldn't start off unsynced. | |
2257 EXPECT_FALSE(ResetUnsyncedEntry(syncable::BOOKMARKS, client_tag)); | |
2258 | |
2259 // Manually change to the same title. Should not set is_unsynced. | |
2260 { | |
2261 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2262 WriteNode node(&trans); | |
2263 EXPECT_TRUE(node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag)); | |
2264 node.SetTitle(UTF8ToWide(client_tag)); | |
2265 } | |
2266 EXPECT_FALSE(ResetUnsyncedEntry(syncable::BOOKMARKS, client_tag)); | |
2267 | |
2268 // Manually change to new title. Should set is_unsynced. | |
2269 { | |
2270 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2271 WriteNode node(&trans); | |
2272 EXPECT_TRUE(node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag)); | |
2273 node.SetTitle(UTF8ToWide("title2")); | |
2274 } | |
2275 EXPECT_TRUE(ResetUnsyncedEntry(syncable::BOOKMARKS, client_tag)); | |
2276 } | |
2277 | |
2278 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for encrypted | |
2279 // bookmarks when we write the same data, but does set it when we write new | |
2280 // data. | |
2281 TEST_F(SyncManagerTest, SetBookmarkTitleWithEncryption) { | |
2282 std::string client_tag = "title"; | |
2283 sync_pb::EntitySpecifics entity_specifics; | |
2284 entity_specifics.mutable_bookmark()->set_url("url"); | |
2285 entity_specifics.mutable_bookmark()->set_title("title"); | |
2286 MakeServerNode(sync_manager_.GetUserShare(), syncable::BOOKMARKS, client_tag, | |
2287 BaseNode::GenerateSyncableHash(syncable::BOOKMARKS, | |
2288 client_tag), | |
2289 entity_specifics); | |
2290 // New node shouldn't start off unsynced. | |
2291 EXPECT_FALSE(ResetUnsyncedEntry(syncable::BOOKMARKS, client_tag)); | |
2292 | |
2293 // Encrypt the datatatype, should set is_unsynced. | |
2294 EXPECT_CALL(observer_, | |
2295 OnEncryptedTypesChanged( | |
2296 HasModelTypes(syncable::ModelTypeSet::All()), true)); | |
2297 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
2298 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, FULL_ENCRYPTION)); | |
2299 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing)); | |
2300 PumpLoop(); | |
2301 EXPECT_TRUE(ResetUnsyncedEntry(syncable::BOOKMARKS, client_tag)); | |
2302 | |
2303 // Manually change to the same title. Should not set is_unsynced. | |
2304 // NON_UNIQUE_NAME should be kEncryptedString. | |
2305 { | |
2306 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2307 WriteNode node(&trans); | |
2308 EXPECT_TRUE(node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag)); | |
2309 node.SetTitle(UTF8ToWide(client_tag)); | |
2310 const syncable::Entry* node_entry = node.GetEntry(); | |
2311 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); | |
2312 EXPECT_TRUE(specifics.has_encrypted()); | |
2313 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); | |
2314 } | |
2315 EXPECT_FALSE(ResetUnsyncedEntry(syncable::BOOKMARKS, client_tag)); | |
2316 | |
2317 // Manually change to new title. Should set is_unsynced. NON_UNIQUE_NAME | |
2318 // should still be kEncryptedString. | |
2319 { | |
2320 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2321 WriteNode node(&trans); | |
2322 EXPECT_TRUE(node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag)); | |
2323 node.SetTitle(UTF8ToWide("title2")); | |
2324 const syncable::Entry* node_entry = node.GetEntry(); | |
2325 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); | |
2326 EXPECT_TRUE(specifics.has_encrypted()); | |
2327 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); | |
2328 } | |
2329 EXPECT_TRUE(ResetUnsyncedEntry(syncable::BOOKMARKS, client_tag)); | |
2330 } | |
2331 | |
2332 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for non-bookmarks | |
2333 // when we write the same data, but does set it when we write new data. | |
2334 TEST_F(SyncManagerTest, SetNonBookmarkTitle) { | |
2335 std::string client_tag = "title"; | |
2336 sync_pb::EntitySpecifics entity_specifics; | |
2337 entity_specifics.mutable_preference()->set_name("name"); | |
2338 entity_specifics.mutable_preference()->set_value("value"); | |
2339 MakeServerNode(sync_manager_.GetUserShare(), | |
2340 syncable::PREFERENCES, | |
2341 client_tag, | |
2342 BaseNode::GenerateSyncableHash(syncable::PREFERENCES, | |
2343 client_tag), | |
2344 entity_specifics); | |
2345 // New node shouldn't start off unsynced. | |
2346 EXPECT_FALSE(ResetUnsyncedEntry(syncable::PREFERENCES, client_tag)); | |
2347 | |
2348 // Manually change to the same title. Should not set is_unsynced. | |
2349 { | |
2350 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2351 WriteNode node(&trans); | |
2352 EXPECT_TRUE(node.InitByClientTagLookup(syncable::PREFERENCES, client_tag)); | |
2353 node.SetTitle(UTF8ToWide(client_tag)); | |
2354 } | |
2355 EXPECT_FALSE(ResetUnsyncedEntry(syncable::PREFERENCES, client_tag)); | |
2356 | |
2357 // Manually change to new title. Should set is_unsynced. | |
2358 { | |
2359 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2360 WriteNode node(&trans); | |
2361 EXPECT_TRUE(node.InitByClientTagLookup(syncable::PREFERENCES, client_tag)); | |
2362 node.SetTitle(UTF8ToWide("title2")); | |
2363 } | |
2364 EXPECT_TRUE(ResetUnsyncedEntry(syncable::PREFERENCES, client_tag)); | |
2365 } | |
2366 | |
2367 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for encrypted | |
2368 // non-bookmarks when we write the same data or when we write new data | |
2369 // data (should remained kEncryptedString). | |
2370 TEST_F(SyncManagerTest, SetNonBookmarkTitleWithEncryption) { | |
2371 std::string client_tag = "title"; | |
2372 sync_pb::EntitySpecifics entity_specifics; | |
2373 entity_specifics.mutable_preference()->set_name("name"); | |
2374 entity_specifics.mutable_preference()->set_value("value"); | |
2375 MakeServerNode(sync_manager_.GetUserShare(), | |
2376 syncable::PREFERENCES, | |
2377 client_tag, | |
2378 BaseNode::GenerateSyncableHash(syncable::PREFERENCES, | |
2379 client_tag), | |
2380 entity_specifics); | |
2381 // New node shouldn't start off unsynced. | |
2382 EXPECT_FALSE(ResetUnsyncedEntry(syncable::PREFERENCES, client_tag)); | |
2383 | |
2384 // Encrypt the datatatype, should set is_unsynced. | |
2385 EXPECT_CALL(observer_, | |
2386 OnEncryptedTypesChanged( | |
2387 HasModelTypes(syncable::ModelTypeSet::All()), true)); | |
2388 EXPECT_CALL(observer_, OnEncryptionComplete()); | |
2389 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, FULL_ENCRYPTION)); | |
2390 sync_manager_.RefreshNigori(kTestChromeVersion, base::Bind(&DoNothing)); | |
2391 PumpLoop(); | |
2392 EXPECT_TRUE(ResetUnsyncedEntry(syncable::PREFERENCES, client_tag)); | |
2393 | |
2394 // Manually change to the same title. Should not set is_unsynced. | |
2395 // NON_UNIQUE_NAME should be kEncryptedString. | |
2396 { | |
2397 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2398 WriteNode node(&trans); | |
2399 EXPECT_TRUE(node.InitByClientTagLookup(syncable::PREFERENCES, client_tag)); | |
2400 node.SetTitle(UTF8ToWide(client_tag)); | |
2401 const syncable::Entry* node_entry = node.GetEntry(); | |
2402 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); | |
2403 EXPECT_TRUE(specifics.has_encrypted()); | |
2404 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); | |
2405 } | |
2406 EXPECT_FALSE(ResetUnsyncedEntry(syncable::PREFERENCES, client_tag)); | |
2407 | |
2408 // Manually change to new title. Should not set is_unsynced because the | |
2409 // NON_UNIQUE_NAME should still be kEncryptedString. | |
2410 { | |
2411 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2412 WriteNode node(&trans); | |
2413 EXPECT_TRUE(node.InitByClientTagLookup(syncable::PREFERENCES, client_tag)); | |
2414 node.SetTitle(UTF8ToWide("title2")); | |
2415 const syncable::Entry* node_entry = node.GetEntry(); | |
2416 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); | |
2417 EXPECT_TRUE(specifics.has_encrypted()); | |
2418 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); | |
2419 EXPECT_FALSE(node_entry->Get(IS_UNSYNCED)); | |
2420 } | |
2421 } | |
2422 | |
2423 // Create an encrypted entry when the cryptographer doesn't think the type is | |
2424 // marked for encryption. Ensure reads/writes don't break and don't unencrypt | |
2425 // the data. | |
2426 TEST_F(SyncManagerTest, SetPreviouslyEncryptedSpecifics) { | |
2427 std::string client_tag = "tag"; | |
2428 std::string url = "url"; | |
2429 std::string url2 = "new_url"; | |
2430 std::string title = "title"; | |
2431 sync_pb::EntitySpecifics entity_specifics; | |
2432 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
2433 { | |
2434 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2435 browser_sync::Cryptographer* crypto = trans.GetCryptographer(); | |
2436 sync_pb::EntitySpecifics bm_specifics; | |
2437 bm_specifics.mutable_bookmark()->set_title("title"); | |
2438 bm_specifics.mutable_bookmark()->set_url("url"); | |
2439 sync_pb::EncryptedData encrypted; | |
2440 crypto->Encrypt(bm_specifics, &encrypted); | |
2441 entity_specifics.mutable_encrypted()->CopyFrom(encrypted); | |
2442 syncable::AddDefaultFieldValue(syncable::BOOKMARKS, &entity_specifics); | |
2443 } | |
2444 MakeServerNode(sync_manager_.GetUserShare(), syncable::BOOKMARKS, client_tag, | |
2445 BaseNode::GenerateSyncableHash(syncable::BOOKMARKS, | |
2446 client_tag), | |
2447 entity_specifics); | |
2448 | |
2449 { | |
2450 // Verify the data. | |
2451 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2452 ReadNode node(&trans); | |
2453 EXPECT_TRUE(node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag)); | |
2454 EXPECT_EQ(title, node.GetTitle()); | |
2455 EXPECT_EQ(GURL(url), node.GetURL()); | |
2456 } | |
2457 | |
2458 { | |
2459 // Overwrite the url (which overwrites the specifics). | |
2460 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2461 WriteNode node(&trans); | |
2462 EXPECT_TRUE(node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag)); | |
2463 node.SetURL(GURL(url2)); | |
2464 } | |
2465 | |
2466 { | |
2467 // Verify it's still encrypted and it has the most recent url. | |
2468 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
2469 ReadNode node(&trans); | |
2470 EXPECT_TRUE(node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag)); | |
2471 EXPECT_EQ(title, node.GetTitle()); | |
2472 EXPECT_EQ(GURL(url2), node.GetURL()); | |
2473 const syncable::Entry* node_entry = node.GetEntry(); | |
2474 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); | |
2475 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); | |
2476 EXPECT_TRUE(specifics.has_encrypted()); | |
2477 } | |
2478 } | |
2479 | |
2480 } // namespace browser_sync | |
OLD | NEW |