| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Unit tests for the SyncApi. Note that a lot of the underlying | 5 // Unit tests for the SyncApi. Note that a lot of the underlying |
| 6 // functionality is provided by the Syncable layer, which has its own | 6 // functionality is provided by the Syncable layer, which has its own |
| 7 // unit tests. We'll test SyncApi specific things in this harness. | 7 // unit tests. We'll test SyncApi specific things in this harness. |
| 8 | 8 |
| 9 #include <cstddef> | 9 #include <cstddef> |
| 10 #include <map> | 10 #include <map> |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 public: | 203 public: |
| 204 virtual void SetUp() { | 204 virtual void SetUp() { |
| 205 test_user_share_.SetUp(); | 205 test_user_share_.SetUp(); |
| 206 } | 206 } |
| 207 | 207 |
| 208 virtual void TearDown() { | 208 virtual void TearDown() { |
| 209 test_user_share_.TearDown(); | 209 test_user_share_.TearDown(); |
| 210 } | 210 } |
| 211 | 211 |
| 212 protected: | 212 protected: |
| 213 // Create an entry with the given |model_type|, |client_tag| and |
| 214 // |attachment_metadata|. |
| 215 void CreateEntryWithAttachmentMetadata( |
| 216 const ModelType& model_type, |
| 217 const std::string& client_tag, |
| 218 const sync_pb::AttachmentMetadata& attachment_metadata); |
| 219 |
| 220 // Attempts to load the entry specified by |model_type| and |client_tag| and |
| 221 // returns the lookup result code. |
| 222 BaseNode::InitByLookupResult LookupEntryByClientTag( |
| 223 const ModelType& model_type, |
| 224 const std::string& client_tag); |
| 225 |
| 226 // Replace the entry specified by |model_type| and |client_tag| with a |
| 227 // tombstone. |
| 228 void ReplaceWithTombstone(const ModelType& model_type, |
| 229 const std::string& client_tag); |
| 230 |
| 231 // Save changes to the Directory, destroy it then reload it. |
| 232 bool ReloadDir(); |
| 233 |
| 234 UserShare* user_share(); |
| 235 syncable::Directory* dir(); |
| 236 SyncEncryptionHandler* encryption_handler(); |
| 237 |
| 238 private: |
| 213 base::MessageLoop message_loop_; | 239 base::MessageLoop message_loop_; |
| 214 TestUserShare test_user_share_; | 240 TestUserShare test_user_share_; |
| 215 }; | 241 }; |
| 216 | 242 |
| 243 UserShare* SyncApiTest::user_share() { |
| 244 return test_user_share_.user_share(); |
| 245 } |
| 246 |
| 247 syncable::Directory* SyncApiTest::dir() { |
| 248 return test_user_share_.user_share()->directory.get(); |
| 249 } |
| 250 |
| 251 SyncEncryptionHandler* SyncApiTest::encryption_handler() { |
| 252 return test_user_share_.encryption_handler(); |
| 253 } |
| 254 |
| 255 bool SyncApiTest::ReloadDir() { |
| 256 return test_user_share_.Reload(); |
| 257 } |
| 258 |
| 259 void SyncApiTest::CreateEntryWithAttachmentMetadata( |
| 260 const ModelType& model_type, |
| 261 const std::string& client_tag, |
| 262 const sync_pb::AttachmentMetadata& attachment_metadata) { |
| 263 syncer::WriteTransaction trans(FROM_HERE, user_share()); |
| 264 syncer::ReadNode root_node(&trans); |
| 265 root_node.InitByRootLookup(); |
| 266 syncer::WriteNode node(&trans); |
| 267 ASSERT_EQ(node.InitUniqueByCreation(model_type, root_node, client_tag), |
| 268 syncer::WriteNode::INIT_SUCCESS); |
| 269 node.SetAttachmentMetadata(attachment_metadata); |
| 270 } |
| 271 |
| 272 BaseNode::InitByLookupResult SyncApiTest::LookupEntryByClientTag( |
| 273 const ModelType& model_type, |
| 274 const std::string& client_tag) { |
| 275 syncer::ReadTransaction trans(FROM_HERE, user_share()); |
| 276 syncer::ReadNode node(&trans); |
| 277 return node.InitByClientTagLookup(model_type, client_tag); |
| 278 } |
| 279 |
| 280 void SyncApiTest::ReplaceWithTombstone(const ModelType& model_type, |
| 281 const std::string& client_tag) { |
| 282 syncer::WriteTransaction trans(FROM_HERE, user_share()); |
| 283 syncer::WriteNode node(&trans); |
| 284 ASSERT_EQ(node.InitByClientTagLookup(model_type, client_tag), |
| 285 syncer::WriteNode::INIT_OK); |
| 286 node.Tombstone(); |
| 287 } |
| 288 |
| 217 TEST_F(SyncApiTest, SanityCheckTest) { | 289 TEST_F(SyncApiTest, SanityCheckTest) { |
| 218 { | 290 { |
| 219 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 291 ReadTransaction trans(FROM_HERE, user_share()); |
| 220 EXPECT_TRUE(trans.GetWrappedTrans()); | 292 EXPECT_TRUE(trans.GetWrappedTrans()); |
| 221 } | 293 } |
| 222 { | 294 { |
| 223 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | 295 WriteTransaction trans(FROM_HERE, user_share()); |
| 224 EXPECT_TRUE(trans.GetWrappedTrans()); | 296 EXPECT_TRUE(trans.GetWrappedTrans()); |
| 225 } | 297 } |
| 226 { | 298 { |
| 227 // No entries but root should exist | 299 // No entries but root should exist |
| 228 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 300 ReadTransaction trans(FROM_HERE, user_share()); |
| 229 ReadNode node(&trans); | 301 ReadNode node(&trans); |
| 230 // Metahandle 1 can be root, sanity check 2 | 302 // Metahandle 1 can be root, sanity check 2 |
| 231 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_NOT_GOOD, node.InitByIdLookup(2)); | 303 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_NOT_GOOD, node.InitByIdLookup(2)); |
| 232 } | 304 } |
| 233 } | 305 } |
| 234 | 306 |
| 235 TEST_F(SyncApiTest, BasicTagWrite) { | 307 TEST_F(SyncApiTest, BasicTagWrite) { |
| 236 { | 308 { |
| 237 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 309 ReadTransaction trans(FROM_HERE, user_share()); |
| 238 ReadNode root_node(&trans); | 310 ReadNode root_node(&trans); |
| 239 root_node.InitByRootLookup(); | 311 root_node.InitByRootLookup(); |
| 240 EXPECT_EQ(root_node.GetFirstChildId(), 0); | 312 EXPECT_EQ(root_node.GetFirstChildId(), 0); |
| 241 } | 313 } |
| 242 | 314 |
| 243 ignore_result(MakeNode(test_user_share_.user_share(), | 315 ignore_result(MakeNode(user_share(), BOOKMARKS, "testtag")); |
| 244 BOOKMARKS, "testtag")); | |
| 245 | 316 |
| 246 { | 317 { |
| 247 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 318 ReadTransaction trans(FROM_HERE, user_share()); |
| 248 ReadNode node(&trans); | 319 ReadNode node(&trans); |
| 249 EXPECT_EQ(BaseNode::INIT_OK, | 320 EXPECT_EQ(BaseNode::INIT_OK, |
| 250 node.InitByClientTagLookup(BOOKMARKS, "testtag")); | 321 node.InitByClientTagLookup(BOOKMARKS, "testtag")); |
| 251 | 322 |
| 252 ReadNode root_node(&trans); | 323 ReadNode root_node(&trans); |
| 253 root_node.InitByRootLookup(); | 324 root_node.InitByRootLookup(); |
| 254 EXPECT_NE(node.GetId(), 0); | 325 EXPECT_NE(node.GetId(), 0); |
| 255 EXPECT_EQ(node.GetId(), root_node.GetFirstChildId()); | 326 EXPECT_EQ(node.GetId(), root_node.GetFirstChildId()); |
| 256 } | 327 } |
| 257 } | 328 } |
| 258 | 329 |
| 259 TEST_F(SyncApiTest, ModelTypesSiloed) { | 330 TEST_F(SyncApiTest, ModelTypesSiloed) { |
| 260 { | 331 { |
| 261 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | 332 WriteTransaction trans(FROM_HERE, user_share()); |
| 262 ReadNode root_node(&trans); | 333 ReadNode root_node(&trans); |
| 263 root_node.InitByRootLookup(); | 334 root_node.InitByRootLookup(); |
| 264 EXPECT_EQ(root_node.GetFirstChildId(), 0); | 335 EXPECT_EQ(root_node.GetFirstChildId(), 0); |
| 265 } | 336 } |
| 266 | 337 |
| 267 ignore_result(MakeNode(test_user_share_.user_share(), | 338 ignore_result(MakeNode(user_share(), BOOKMARKS, "collideme")); |
| 268 BOOKMARKS, "collideme")); | 339 ignore_result(MakeNode(user_share(), PREFERENCES, "collideme")); |
| 269 ignore_result(MakeNode(test_user_share_.user_share(), | 340 ignore_result(MakeNode(user_share(), AUTOFILL, "collideme")); |
| 270 PREFERENCES, "collideme")); | |
| 271 ignore_result(MakeNode(test_user_share_.user_share(), | |
| 272 AUTOFILL, "collideme")); | |
| 273 | 341 |
| 274 { | 342 { |
| 275 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 343 ReadTransaction trans(FROM_HERE, user_share()); |
| 276 | 344 |
| 277 ReadNode bookmarknode(&trans); | 345 ReadNode bookmarknode(&trans); |
| 278 EXPECT_EQ(BaseNode::INIT_OK, | 346 EXPECT_EQ(BaseNode::INIT_OK, |
| 279 bookmarknode.InitByClientTagLookup(BOOKMARKS, | 347 bookmarknode.InitByClientTagLookup(BOOKMARKS, |
| 280 "collideme")); | 348 "collideme")); |
| 281 | 349 |
| 282 ReadNode prefnode(&trans); | 350 ReadNode prefnode(&trans); |
| 283 EXPECT_EQ(BaseNode::INIT_OK, | 351 EXPECT_EQ(BaseNode::INIT_OK, |
| 284 prefnode.InitByClientTagLookup(PREFERENCES, | 352 prefnode.InitByClientTagLookup(PREFERENCES, |
| 285 "collideme")); | 353 "collideme")); |
| 286 | 354 |
| 287 ReadNode autofillnode(&trans); | 355 ReadNode autofillnode(&trans); |
| 288 EXPECT_EQ(BaseNode::INIT_OK, | 356 EXPECT_EQ(BaseNode::INIT_OK, |
| 289 autofillnode.InitByClientTagLookup(AUTOFILL, | 357 autofillnode.InitByClientTagLookup(AUTOFILL, |
| 290 "collideme")); | 358 "collideme")); |
| 291 | 359 |
| 292 EXPECT_NE(bookmarknode.GetId(), prefnode.GetId()); | 360 EXPECT_NE(bookmarknode.GetId(), prefnode.GetId()); |
| 293 EXPECT_NE(autofillnode.GetId(), prefnode.GetId()); | 361 EXPECT_NE(autofillnode.GetId(), prefnode.GetId()); |
| 294 EXPECT_NE(bookmarknode.GetId(), autofillnode.GetId()); | 362 EXPECT_NE(bookmarknode.GetId(), autofillnode.GetId()); |
| 295 } | 363 } |
| 296 } | 364 } |
| 297 | 365 |
| 298 TEST_F(SyncApiTest, ReadMissingTagsFails) { | 366 TEST_F(SyncApiTest, ReadMissingTagsFails) { |
| 299 { | 367 { |
| 300 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 368 ReadTransaction trans(FROM_HERE, user_share()); |
| 301 ReadNode node(&trans); | 369 ReadNode node(&trans); |
| 302 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_NOT_GOOD, | 370 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_NOT_GOOD, |
| 303 node.InitByClientTagLookup(BOOKMARKS, | 371 node.InitByClientTagLookup(BOOKMARKS, |
| 304 "testtag")); | 372 "testtag")); |
| 305 } | 373 } |
| 306 { | 374 { |
| 307 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | 375 WriteTransaction trans(FROM_HERE, user_share()); |
| 308 WriteNode node(&trans); | 376 WriteNode node(&trans); |
| 309 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_NOT_GOOD, | 377 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_NOT_GOOD, |
| 310 node.InitByClientTagLookup(BOOKMARKS, | 378 node.InitByClientTagLookup(BOOKMARKS, |
| 311 "testtag")); | 379 "testtag")); |
| 312 } | 380 } |
| 313 } | 381 } |
| 314 | 382 |
| 315 // TODO(chron): Hook this all up to the server and write full integration tests | 383 // TODO(chron): Hook this all up to the server and write full integration tests |
| 316 // for update->undelete behavior. | 384 // for update->undelete behavior. |
| 317 TEST_F(SyncApiTest, TestDeleteBehavior) { | 385 TEST_F(SyncApiTest, TestDeleteBehavior) { |
| 318 int64 node_id; | 386 int64 node_id; |
| 319 int64 folder_id; | 387 int64 folder_id; |
| 320 std::string test_title("test1"); | 388 std::string test_title("test1"); |
| 321 | 389 |
| 322 { | 390 { |
| 323 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | 391 WriteTransaction trans(FROM_HERE, user_share()); |
| 324 ReadNode root_node(&trans); | 392 ReadNode root_node(&trans); |
| 325 root_node.InitByRootLookup(); | 393 root_node.InitByRootLookup(); |
| 326 | 394 |
| 327 // we'll use this spare folder later | 395 // we'll use this spare folder later |
| 328 WriteNode folder_node(&trans); | 396 WriteNode folder_node(&trans); |
| 329 EXPECT_TRUE(folder_node.InitBookmarkByCreation(root_node, NULL)); | 397 EXPECT_TRUE(folder_node.InitBookmarkByCreation(root_node, NULL)); |
| 330 folder_id = folder_node.GetId(); | 398 folder_id = folder_node.GetId(); |
| 331 | 399 |
| 332 WriteNode wnode(&trans); | 400 WriteNode wnode(&trans); |
| 333 WriteNode::InitUniqueByCreationResult result = | 401 WriteNode::InitUniqueByCreationResult result = |
| 334 wnode.InitUniqueByCreation(BOOKMARKS, root_node, "testtag"); | 402 wnode.InitUniqueByCreation(BOOKMARKS, root_node, "testtag"); |
| 335 EXPECT_EQ(WriteNode::INIT_SUCCESS, result); | 403 EXPECT_EQ(WriteNode::INIT_SUCCESS, result); |
| 336 wnode.SetIsFolder(false); | 404 wnode.SetIsFolder(false); |
| 337 wnode.SetTitle(test_title); | 405 wnode.SetTitle(test_title); |
| 338 | 406 |
| 339 node_id = wnode.GetId(); | 407 node_id = wnode.GetId(); |
| 340 } | 408 } |
| 341 | 409 |
| 342 // Ensure we can delete something with a tag. | 410 // Ensure we can delete something with a tag. |
| 343 { | 411 { |
| 344 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | 412 WriteTransaction trans(FROM_HERE, user_share()); |
| 345 WriteNode wnode(&trans); | 413 WriteNode wnode(&trans); |
| 346 EXPECT_EQ(BaseNode::INIT_OK, | 414 EXPECT_EQ(BaseNode::INIT_OK, |
| 347 wnode.InitByClientTagLookup(BOOKMARKS, | 415 wnode.InitByClientTagLookup(BOOKMARKS, |
| 348 "testtag")); | 416 "testtag")); |
| 349 EXPECT_FALSE(wnode.GetIsFolder()); | 417 EXPECT_FALSE(wnode.GetIsFolder()); |
| 350 EXPECT_EQ(wnode.GetTitle(), test_title); | 418 EXPECT_EQ(wnode.GetTitle(), test_title); |
| 351 | 419 |
| 352 wnode.Tombstone(); | 420 wnode.Tombstone(); |
| 353 } | 421 } |
| 354 | 422 |
| 355 // Lookup of a node which was deleted should return failure, | 423 // Lookup of a node which was deleted should return failure, |
| 356 // but have found some data about the node. | 424 // but have found some data about the node. |
| 357 { | 425 { |
| 358 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 426 ReadTransaction trans(FROM_HERE, user_share()); |
| 359 ReadNode node(&trans); | 427 ReadNode node(&trans); |
| 360 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_IS_DEL, | 428 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_IS_DEL, |
| 361 node.InitByClientTagLookup(BOOKMARKS, | 429 node.InitByClientTagLookup(BOOKMARKS, |
| 362 "testtag")); | 430 "testtag")); |
| 363 // Note that for proper function of this API this doesn't need to be | 431 // Note that for proper function of this API this doesn't need to be |
| 364 // filled, we're checking just to make sure the DB worked in this test. | 432 // filled, we're checking just to make sure the DB worked in this test. |
| 365 EXPECT_EQ(node.GetTitle(), test_title); | 433 EXPECT_EQ(node.GetTitle(), test_title); |
| 366 } | 434 } |
| 367 | 435 |
| 368 { | 436 { |
| 369 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | 437 WriteTransaction trans(FROM_HERE, user_share()); |
| 370 ReadNode folder_node(&trans); | 438 ReadNode folder_node(&trans); |
| 371 EXPECT_EQ(BaseNode::INIT_OK, folder_node.InitByIdLookup(folder_id)); | 439 EXPECT_EQ(BaseNode::INIT_OK, folder_node.InitByIdLookup(folder_id)); |
| 372 | 440 |
| 373 WriteNode wnode(&trans); | 441 WriteNode wnode(&trans); |
| 374 // This will undelete the tag. | 442 // This will undelete the tag. |
| 375 WriteNode::InitUniqueByCreationResult result = | 443 WriteNode::InitUniqueByCreationResult result = |
| 376 wnode.InitUniqueByCreation(BOOKMARKS, folder_node, "testtag"); | 444 wnode.InitUniqueByCreation(BOOKMARKS, folder_node, "testtag"); |
| 377 EXPECT_EQ(WriteNode::INIT_SUCCESS, result); | 445 EXPECT_EQ(WriteNode::INIT_SUCCESS, result); |
| 378 EXPECT_EQ(wnode.GetIsFolder(), false); | 446 EXPECT_EQ(wnode.GetIsFolder(), false); |
| 379 EXPECT_EQ(wnode.GetParentId(), folder_node.GetId()); | 447 EXPECT_EQ(wnode.GetParentId(), folder_node.GetId()); |
| 380 EXPECT_EQ(wnode.GetId(), node_id); | 448 EXPECT_EQ(wnode.GetId(), node_id); |
| 381 EXPECT_NE(wnode.GetTitle(), test_title); // Title should be cleared | 449 EXPECT_NE(wnode.GetTitle(), test_title); // Title should be cleared |
| 382 wnode.SetTitle(test_title); | 450 wnode.SetTitle(test_title); |
| 383 } | 451 } |
| 384 | 452 |
| 385 // Now look up should work. | 453 // Now look up should work. |
| 386 { | 454 { |
| 387 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 455 ReadTransaction trans(FROM_HERE, user_share()); |
| 388 ReadNode node(&trans); | 456 ReadNode node(&trans); |
| 389 EXPECT_EQ(BaseNode::INIT_OK, | 457 EXPECT_EQ(BaseNode::INIT_OK, |
| 390 node.InitByClientTagLookup(BOOKMARKS, | 458 node.InitByClientTagLookup(BOOKMARKS, |
| 391 "testtag")); | 459 "testtag")); |
| 392 EXPECT_EQ(node.GetTitle(), test_title); | 460 EXPECT_EQ(node.GetTitle(), test_title); |
| 393 EXPECT_EQ(node.GetModelType(), BOOKMARKS); | 461 EXPECT_EQ(node.GetModelType(), BOOKMARKS); |
| 394 } | 462 } |
| 395 } | 463 } |
| 396 | 464 |
| 397 TEST_F(SyncApiTest, WriteAndReadPassword) { | 465 TEST_F(SyncApiTest, WriteAndReadPassword) { |
| 398 KeyParams params = {"localhost", "username", "passphrase"}; | 466 KeyParams params = {"localhost", "username", "passphrase"}; |
| 399 { | 467 { |
| 400 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 468 ReadTransaction trans(FROM_HERE, user_share()); |
| 401 trans.GetCryptographer()->AddKey(params); | 469 trans.GetCryptographer()->AddKey(params); |
| 402 } | 470 } |
| 403 { | 471 { |
| 404 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | 472 WriteTransaction trans(FROM_HERE, user_share()); |
| 405 ReadNode root_node(&trans); | 473 ReadNode root_node(&trans); |
| 406 root_node.InitByRootLookup(); | 474 root_node.InitByRootLookup(); |
| 407 | 475 |
| 408 WriteNode password_node(&trans); | 476 WriteNode password_node(&trans); |
| 409 WriteNode::InitUniqueByCreationResult result = | 477 WriteNode::InitUniqueByCreationResult result = |
| 410 password_node.InitUniqueByCreation(PASSWORDS, | 478 password_node.InitUniqueByCreation(PASSWORDS, |
| 411 root_node, "foo"); | 479 root_node, "foo"); |
| 412 EXPECT_EQ(WriteNode::INIT_SUCCESS, result); | 480 EXPECT_EQ(WriteNode::INIT_SUCCESS, result); |
| 413 sync_pb::PasswordSpecificsData data; | 481 sync_pb::PasswordSpecificsData data; |
| 414 data.set_password_value("secret"); | 482 data.set_password_value("secret"); |
| 415 password_node.SetPasswordSpecifics(data); | 483 password_node.SetPasswordSpecifics(data); |
| 416 } | 484 } |
| 417 { | 485 { |
| 418 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 486 ReadTransaction trans(FROM_HERE, user_share()); |
| 419 ReadNode root_node(&trans); | 487 ReadNode root_node(&trans); |
| 420 root_node.InitByRootLookup(); | 488 root_node.InitByRootLookup(); |
| 421 | 489 |
| 422 ReadNode password_node(&trans); | 490 ReadNode password_node(&trans); |
| 423 EXPECT_EQ(BaseNode::INIT_OK, | 491 EXPECT_EQ(BaseNode::INIT_OK, |
| 424 password_node.InitByClientTagLookup(PASSWORDS, "foo")); | 492 password_node.InitByClientTagLookup(PASSWORDS, "foo")); |
| 425 const sync_pb::PasswordSpecificsData& data = | 493 const sync_pb::PasswordSpecificsData& data = |
| 426 password_node.GetPasswordSpecifics(); | 494 password_node.GetPasswordSpecifics(); |
| 427 EXPECT_EQ("secret", data.password_value()); | 495 EXPECT_EQ("secret", data.password_value()); |
| 428 } | 496 } |
| 429 } | 497 } |
| 430 | 498 |
| 431 TEST_F(SyncApiTest, WriteEncryptedTitle) { | 499 TEST_F(SyncApiTest, WriteEncryptedTitle) { |
| 432 KeyParams params = {"localhost", "username", "passphrase"}; | 500 KeyParams params = {"localhost", "username", "passphrase"}; |
| 433 { | 501 { |
| 434 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 502 ReadTransaction trans(FROM_HERE, user_share()); |
| 435 trans.GetCryptographer()->AddKey(params); | 503 trans.GetCryptographer()->AddKey(params); |
| 436 } | 504 } |
| 437 test_user_share_.encryption_handler()->EnableEncryptEverything(); | 505 encryption_handler()->EnableEncryptEverything(); |
| 438 int bookmark_id; | 506 int bookmark_id; |
| 439 { | 507 { |
| 440 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | 508 WriteTransaction trans(FROM_HERE, user_share()); |
| 441 ReadNode root_node(&trans); | 509 ReadNode root_node(&trans); |
| 442 root_node.InitByRootLookup(); | 510 root_node.InitByRootLookup(); |
| 443 | 511 |
| 444 WriteNode bookmark_node(&trans); | 512 WriteNode bookmark_node(&trans); |
| 445 ASSERT_TRUE(bookmark_node.InitBookmarkByCreation(root_node, NULL)); | 513 ASSERT_TRUE(bookmark_node.InitBookmarkByCreation(root_node, NULL)); |
| 446 bookmark_id = bookmark_node.GetId(); | 514 bookmark_id = bookmark_node.GetId(); |
| 447 bookmark_node.SetTitle("foo"); | 515 bookmark_node.SetTitle("foo"); |
| 448 | 516 |
| 449 WriteNode pref_node(&trans); | 517 WriteNode pref_node(&trans); |
| 450 WriteNode::InitUniqueByCreationResult result = | 518 WriteNode::InitUniqueByCreationResult result = |
| 451 pref_node.InitUniqueByCreation(PREFERENCES, root_node, "bar"); | 519 pref_node.InitUniqueByCreation(PREFERENCES, root_node, "bar"); |
| 452 ASSERT_EQ(WriteNode::INIT_SUCCESS, result); | 520 ASSERT_EQ(WriteNode::INIT_SUCCESS, result); |
| 453 pref_node.SetTitle("bar"); | 521 pref_node.SetTitle("bar"); |
| 454 } | 522 } |
| 455 { | 523 { |
| 456 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 524 ReadTransaction trans(FROM_HERE, user_share()); |
| 457 ReadNode root_node(&trans); | 525 ReadNode root_node(&trans); |
| 458 root_node.InitByRootLookup(); | 526 root_node.InitByRootLookup(); |
| 459 | 527 |
| 460 ReadNode bookmark_node(&trans); | 528 ReadNode bookmark_node(&trans); |
| 461 ASSERT_EQ(BaseNode::INIT_OK, bookmark_node.InitByIdLookup(bookmark_id)); | 529 ASSERT_EQ(BaseNode::INIT_OK, bookmark_node.InitByIdLookup(bookmark_id)); |
| 462 EXPECT_EQ("foo", bookmark_node.GetTitle()); | 530 EXPECT_EQ("foo", bookmark_node.GetTitle()); |
| 463 EXPECT_EQ(kEncryptedString, | 531 EXPECT_EQ(kEncryptedString, |
| 464 bookmark_node.GetEntry()->GetNonUniqueName()); | 532 bookmark_node.GetEntry()->GetNonUniqueName()); |
| 465 | 533 |
| 466 ReadNode pref_node(&trans); | 534 ReadNode pref_node(&trans); |
| 467 ASSERT_EQ(BaseNode::INIT_OK, | 535 ASSERT_EQ(BaseNode::INIT_OK, |
| 468 pref_node.InitByClientTagLookup(PREFERENCES, | 536 pref_node.InitByClientTagLookup(PREFERENCES, |
| 469 "bar")); | 537 "bar")); |
| 470 EXPECT_EQ(kEncryptedString, pref_node.GetTitle()); | 538 EXPECT_EQ(kEncryptedString, pref_node.GetTitle()); |
| 471 } | 539 } |
| 472 } | 540 } |
| 473 | 541 |
| 474 TEST_F(SyncApiTest, BaseNodeSetSpecifics) { | 542 TEST_F(SyncApiTest, BaseNodeSetSpecifics) { |
| 475 int64 child_id = MakeNode(test_user_share_.user_share(), | 543 int64 child_id = MakeNode(user_share(), BOOKMARKS, "testtag"); |
| 476 BOOKMARKS, "testtag"); | 544 WriteTransaction trans(FROM_HERE, user_share()); |
| 477 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
| 478 WriteNode node(&trans); | 545 WriteNode node(&trans); |
| 479 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(child_id)); | 546 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(child_id)); |
| 480 | 547 |
| 481 sync_pb::EntitySpecifics entity_specifics; | 548 sync_pb::EntitySpecifics entity_specifics; |
| 482 entity_specifics.mutable_bookmark()->set_url("http://www.google.com"); | 549 entity_specifics.mutable_bookmark()->set_url("http://www.google.com"); |
| 483 | 550 |
| 484 EXPECT_NE(entity_specifics.SerializeAsString(), | 551 EXPECT_NE(entity_specifics.SerializeAsString(), |
| 485 node.GetEntitySpecifics().SerializeAsString()); | 552 node.GetEntitySpecifics().SerializeAsString()); |
| 486 node.SetEntitySpecifics(entity_specifics); | 553 node.SetEntitySpecifics(entity_specifics); |
| 487 EXPECT_EQ(entity_specifics.SerializeAsString(), | 554 EXPECT_EQ(entity_specifics.SerializeAsString(), |
| 488 node.GetEntitySpecifics().SerializeAsString()); | 555 node.GetEntitySpecifics().SerializeAsString()); |
| 489 } | 556 } |
| 490 | 557 |
| 491 TEST_F(SyncApiTest, BaseNodeSetSpecificsPreservesUnknownFields) { | 558 TEST_F(SyncApiTest, BaseNodeSetSpecificsPreservesUnknownFields) { |
| 492 int64 child_id = MakeNode(test_user_share_.user_share(), | 559 int64 child_id = MakeNode(user_share(), BOOKMARKS, "testtag"); |
| 493 BOOKMARKS, "testtag"); | 560 WriteTransaction trans(FROM_HERE, user_share()); |
| 494 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
| 495 WriteNode node(&trans); | 561 WriteNode node(&trans); |
| 496 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(child_id)); | 562 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(child_id)); |
| 497 EXPECT_TRUE(node.GetEntitySpecifics().unknown_fields().empty()); | 563 EXPECT_TRUE(node.GetEntitySpecifics().unknown_fields().empty()); |
| 498 | 564 |
| 499 sync_pb::EntitySpecifics entity_specifics; | 565 sync_pb::EntitySpecifics entity_specifics; |
| 500 entity_specifics.mutable_bookmark()->set_url("http://www.google.com"); | 566 entity_specifics.mutable_bookmark()->set_url("http://www.google.com"); |
| 501 entity_specifics.mutable_unknown_fields()->AddFixed32(5, 100); | 567 entity_specifics.mutable_unknown_fields()->AddFixed32(5, 100); |
| 502 node.SetEntitySpecifics(entity_specifics); | 568 node.SetEntitySpecifics(entity_specifics); |
| 503 EXPECT_FALSE(node.GetEntitySpecifics().unknown_fields().empty()); | 569 EXPECT_FALSE(node.GetEntitySpecifics().unknown_fields().empty()); |
| 504 | 570 |
| 505 entity_specifics.mutable_unknown_fields()->Clear(); | 571 entity_specifics.mutable_unknown_fields()->Clear(); |
| 506 node.SetEntitySpecifics(entity_specifics); | 572 node.SetEntitySpecifics(entity_specifics); |
| 507 EXPECT_FALSE(node.GetEntitySpecifics().unknown_fields().empty()); | 573 EXPECT_FALSE(node.GetEntitySpecifics().unknown_fields().empty()); |
| 508 } | 574 } |
| 509 | 575 |
| 510 TEST_F(SyncApiTest, EmptyTags) { | 576 TEST_F(SyncApiTest, EmptyTags) { |
| 511 WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | 577 WriteTransaction trans(FROM_HERE, user_share()); |
| 512 ReadNode root_node(&trans); | 578 ReadNode root_node(&trans); |
| 513 root_node.InitByRootLookup(); | 579 root_node.InitByRootLookup(); |
| 514 WriteNode node(&trans); | 580 WriteNode node(&trans); |
| 515 std::string empty_tag; | 581 std::string empty_tag; |
| 516 WriteNode::InitUniqueByCreationResult result = | 582 WriteNode::InitUniqueByCreationResult result = |
| 517 node.InitUniqueByCreation(TYPED_URLS, root_node, empty_tag); | 583 node.InitUniqueByCreation(TYPED_URLS, root_node, empty_tag); |
| 518 EXPECT_NE(WriteNode::INIT_SUCCESS, result); | 584 EXPECT_NE(WriteNode::INIT_SUCCESS, result); |
| 519 EXPECT_EQ(BaseNode::INIT_FAILED_PRECONDITION, | 585 EXPECT_EQ(BaseNode::INIT_FAILED_PRECONDITION, |
| 520 node.InitByTagLookup(empty_tag)); | 586 node.InitByTagLookup(empty_tag)); |
| 521 } | 587 } |
| 522 | 588 |
| 523 // Test counting nodes when the type's root node has no children. | 589 // Test counting nodes when the type's root node has no children. |
| 524 TEST_F(SyncApiTest, GetTotalNodeCountEmpty) { | 590 TEST_F(SyncApiTest, GetTotalNodeCountEmpty) { |
| 525 int64 type_root = MakeServerNodeForType(test_user_share_.user_share(), | 591 int64 type_root = MakeServerNodeForType(user_share(), BOOKMARKS); |
| 526 BOOKMARKS); | |
| 527 { | 592 { |
| 528 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 593 ReadTransaction trans(FROM_HERE, user_share()); |
| 529 ReadNode type_root_node(&trans); | 594 ReadNode type_root_node(&trans); |
| 530 EXPECT_EQ(BaseNode::INIT_OK, | 595 EXPECT_EQ(BaseNode::INIT_OK, |
| 531 type_root_node.InitByIdLookup(type_root)); | 596 type_root_node.InitByIdLookup(type_root)); |
| 532 EXPECT_EQ(1, type_root_node.GetTotalNodeCount()); | 597 EXPECT_EQ(1, type_root_node.GetTotalNodeCount()); |
| 533 } | 598 } |
| 534 } | 599 } |
| 535 | 600 |
| 536 // Test counting nodes when there is one child beneath the type's root. | 601 // Test counting nodes when there is one child beneath the type's root. |
| 537 TEST_F(SyncApiTest, GetTotalNodeCountOneChild) { | 602 TEST_F(SyncApiTest, GetTotalNodeCountOneChild) { |
| 538 int64 type_root = MakeServerNodeForType(test_user_share_.user_share(), | 603 int64 type_root = MakeServerNodeForType(user_share(), BOOKMARKS); |
| 539 BOOKMARKS); | 604 int64 parent = MakeFolderWithParent(user_share(), BOOKMARKS, type_root, NULL); |
| 540 int64 parent = MakeFolderWithParent(test_user_share_.user_share(), | |
| 541 BOOKMARKS, | |
| 542 type_root, | |
| 543 NULL); | |
| 544 { | 605 { |
| 545 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 606 ReadTransaction trans(FROM_HERE, user_share()); |
| 546 ReadNode type_root_node(&trans); | 607 ReadNode type_root_node(&trans); |
| 547 EXPECT_EQ(BaseNode::INIT_OK, | 608 EXPECT_EQ(BaseNode::INIT_OK, |
| 548 type_root_node.InitByIdLookup(type_root)); | 609 type_root_node.InitByIdLookup(type_root)); |
| 549 EXPECT_EQ(2, type_root_node.GetTotalNodeCount()); | 610 EXPECT_EQ(2, type_root_node.GetTotalNodeCount()); |
| 550 ReadNode parent_node(&trans); | 611 ReadNode parent_node(&trans); |
| 551 EXPECT_EQ(BaseNode::INIT_OK, | 612 EXPECT_EQ(BaseNode::INIT_OK, |
| 552 parent_node.InitByIdLookup(parent)); | 613 parent_node.InitByIdLookup(parent)); |
| 553 EXPECT_EQ(1, parent_node.GetTotalNodeCount()); | 614 EXPECT_EQ(1, parent_node.GetTotalNodeCount()); |
| 554 } | 615 } |
| 555 } | 616 } |
| 556 | 617 |
| 557 // Test counting nodes when there are multiple children beneath the type root, | 618 // Test counting nodes when there are multiple children beneath the type root, |
| 558 // and one of those children has children of its own. | 619 // and one of those children has children of its own. |
| 559 TEST_F(SyncApiTest, GetTotalNodeCountMultipleChildren) { | 620 TEST_F(SyncApiTest, GetTotalNodeCountMultipleChildren) { |
| 560 int64 type_root = MakeServerNodeForType(test_user_share_.user_share(), | 621 int64 type_root = MakeServerNodeForType(user_share(), BOOKMARKS); |
| 561 BOOKMARKS); | 622 int64 parent = MakeFolderWithParent(user_share(), BOOKMARKS, type_root, NULL); |
| 562 int64 parent = MakeFolderWithParent(test_user_share_.user_share(), | 623 ignore_result(MakeFolderWithParent(user_share(), BOOKMARKS, type_root, NULL)); |
| 563 BOOKMARKS, | 624 int64 child1 = MakeFolderWithParent(user_share(), BOOKMARKS, parent, NULL); |
| 564 type_root, | 625 ignore_result(MakeBookmarkWithParent(user_share(), parent, NULL)); |
| 565 NULL); | 626 ignore_result(MakeBookmarkWithParent(user_share(), child1, NULL)); |
| 566 ignore_result(MakeFolderWithParent(test_user_share_.user_share(), | |
| 567 BOOKMARKS, | |
| 568 type_root, | |
| 569 NULL)); | |
| 570 int64 child1 = MakeFolderWithParent( | |
| 571 test_user_share_.user_share(), | |
| 572 BOOKMARKS, | |
| 573 parent, | |
| 574 NULL); | |
| 575 ignore_result(MakeBookmarkWithParent( | |
| 576 test_user_share_.user_share(), | |
| 577 parent, | |
| 578 NULL)); | |
| 579 ignore_result(MakeBookmarkWithParent( | |
| 580 test_user_share_.user_share(), | |
| 581 child1, | |
| 582 NULL)); | |
| 583 | 627 |
| 584 { | 628 { |
| 585 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 629 ReadTransaction trans(FROM_HERE, user_share()); |
| 586 ReadNode type_root_node(&trans); | 630 ReadNode type_root_node(&trans); |
| 587 EXPECT_EQ(BaseNode::INIT_OK, | 631 EXPECT_EQ(BaseNode::INIT_OK, |
| 588 type_root_node.InitByIdLookup(type_root)); | 632 type_root_node.InitByIdLookup(type_root)); |
| 589 EXPECT_EQ(6, type_root_node.GetTotalNodeCount()); | 633 EXPECT_EQ(6, type_root_node.GetTotalNodeCount()); |
| 590 ReadNode node(&trans); | 634 ReadNode node(&trans); |
| 591 EXPECT_EQ(BaseNode::INIT_OK, | 635 EXPECT_EQ(BaseNode::INIT_OK, |
| 592 node.InitByIdLookup(parent)); | 636 node.InitByIdLookup(parent)); |
| 593 EXPECT_EQ(4, node.GetTotalNodeCount()); | 637 EXPECT_EQ(4, node.GetTotalNodeCount()); |
| 594 } | 638 } |
| 595 } | 639 } |
| 596 | 640 |
| 641 // Verify that Directory keeps track of which attachments are referenced by |
| 642 // which entries. |
| 643 TEST_F(SyncApiTest, AttachmentLinking) { |
| 644 // Add an entry with an attachment. |
| 645 std::string tag1("some tag"); |
| 646 syncer::AttachmentId attachment_id(syncer::AttachmentId::Create()); |
| 647 sync_pb::AttachmentMetadata attachment_metadata; |
| 648 sync_pb::AttachmentMetadataRecord* record = attachment_metadata.add_record(); |
| 649 *record->mutable_id() = attachment_id.GetProto(); |
| 650 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id.GetProto())); |
| 651 CreateEntryWithAttachmentMetadata(PREFERENCES, tag1, attachment_metadata); |
| 652 |
| 653 // See that the directory knows it's linked. |
| 654 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id.GetProto())); |
| 655 |
| 656 // Add a second entry referencing the same attachment. |
| 657 std::string tag2("some other tag"); |
| 658 CreateEntryWithAttachmentMetadata(PREFERENCES, tag2, attachment_metadata); |
| 659 |
| 660 // See that the directory knows it's still linked. |
| 661 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id.GetProto())); |
| 662 |
| 663 // Tombstone the first entry. |
| 664 ReplaceWithTombstone(syncer::PREFERENCES, tag1); |
| 665 |
| 666 // See that the attachment is still considered linked because the entry hasn't |
| 667 // been purged from the Directory. |
| 668 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id.GetProto())); |
| 669 |
| 670 // Save changes and see that the entry is truly gone. |
| 671 ASSERT_TRUE(dir()->SaveChanges()); |
| 672 ASSERT_EQ(LookupEntryByClientTag(PREFERENCES, tag1), |
| 673 syncer::WriteNode::INIT_FAILED_ENTRY_NOT_GOOD); |
| 674 |
| 675 // However, the attachment is still linked. |
| 676 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id.GetProto())); |
| 677 |
| 678 // Save, destroy, and recreate the directory. See that it's still linked. |
| 679 ASSERT_TRUE(ReloadDir()); |
| 680 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id.GetProto())); |
| 681 |
| 682 // Tombstone the second entry, save changes, see that it's truly gone. |
| 683 ReplaceWithTombstone(syncer::PREFERENCES, tag2); |
| 684 ASSERT_TRUE(dir()->SaveChanges()); |
| 685 ASSERT_EQ(LookupEntryByClientTag(PREFERENCES, tag2), |
| 686 syncer::WriteNode::INIT_FAILED_ENTRY_NOT_GOOD); |
| 687 |
| 688 // Finally, the attachment is no longer linked. |
| 689 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id.GetProto())); |
| 690 } |
| 691 |
| 597 namespace { | 692 namespace { |
| 598 | 693 |
| 599 class TestHttpPostProviderInterface : public HttpPostProviderInterface { | 694 class TestHttpPostProviderInterface : public HttpPostProviderInterface { |
| 600 public: | 695 public: |
| 601 virtual ~TestHttpPostProviderInterface() {} | 696 virtual ~TestHttpPostProviderInterface() {} |
| 602 | 697 |
| 603 virtual void SetExtraRequestHeaders(const char* headers) OVERRIDE {} | 698 virtual void SetExtraRequestHeaders(const char* headers) OVERRIDE {} |
| 604 virtual void SetURL(const char* url, int port) OVERRIDE {} | 699 virtual void SetURL(const char* url, int port) OVERRIDE {} |
| 605 virtual void SetPostPayload(const char* content_type, | 700 virtual void SetPostPayload(const char* content_type, |
| 606 int content_length, | 701 int content_length, |
| (...skipping 2433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3040 // SyncManagerInitInvalidStorageTest::GetFactory will return | 3135 // SyncManagerInitInvalidStorageTest::GetFactory will return |
| 3041 // DirectoryBackingStore that ensures that SyncManagerImpl::OpenDirectory fails. | 3136 // DirectoryBackingStore that ensures that SyncManagerImpl::OpenDirectory fails. |
| 3042 // SyncManagerImpl initialization is done in SyncManagerTest::SetUp. This test's | 3137 // SyncManagerImpl initialization is done in SyncManagerTest::SetUp. This test's |
| 3043 // task is to ensure that SyncManagerImpl reported initialization failure in | 3138 // task is to ensure that SyncManagerImpl reported initialization failure in |
| 3044 // OnInitializationComplete callback. | 3139 // OnInitializationComplete callback. |
| 3045 TEST_F(SyncManagerInitInvalidStorageTest, FailToOpenDatabase) { | 3140 TEST_F(SyncManagerInitInvalidStorageTest, FailToOpenDatabase) { |
| 3046 EXPECT_FALSE(initialization_succeeded_); | 3141 EXPECT_FALSE(initialization_succeeded_); |
| 3047 } | 3142 } |
| 3048 | 3143 |
| 3049 } // namespace syncer | 3144 } // namespace syncer |
| OLD | NEW |