| Index: sync/internal_api/sync_encryption_handler_impl_unittest.cc
|
| diff --git a/sync/internal_api/sync_encryption_handler_impl_unittest.cc b/sync/internal_api/sync_encryption_handler_impl_unittest.cc
|
| index af58d54be3ac359e94108cf194b103dc7478a1e7..b37afa483af26bd18ba017d537b9ce2bb212ffb6 100644
|
| --- a/sync/internal_api/sync_encryption_handler_impl_unittest.cc
|
| +++ b/sync/internal_api/sync_encryption_handler_impl_unittest.cc
|
| @@ -20,6 +20,7 @@
|
| #include "sync/syncable/mutable_entry.h"
|
| #include "sync/syncable/write_transaction.h"
|
| #include "sync/test/engine/test_id_factory.h"
|
| +#include "sync/test/fake_encryptor.h"
|
| #include "sync/util/cryptographer.h"
|
| #include "testing/gmock/include/gmock/gmock.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
| @@ -50,7 +51,7 @@ class SyncEncryptionHandlerObserverMock
|
|
|
| class SyncEncryptionHandlerImplTest : public ::testing::Test {
|
| public:
|
| - SyncEncryptionHandlerImplTest() : cryptographer_(NULL) {}
|
| + SyncEncryptionHandlerImplTest() {}
|
| virtual ~SyncEncryptionHandlerImplTest() {}
|
|
|
| virtual void SetUp() {
|
| @@ -66,12 +67,9 @@ class SyncEncryptionHandlerImplTest : public ::testing::Test {
|
| protected:
|
| void SetUpEncryption() {
|
| ReadTransaction trans(FROM_HERE, user_share());
|
| - cryptographer_ = trans.GetCryptographer();
|
| encryption_handler_.reset(
|
| new SyncEncryptionHandlerImpl(user_share(),
|
| - cryptographer_));
|
| - cryptographer_->SetNigoriHandler(
|
| - encryption_handler_.get());
|
| + &encryptor_));
|
| encryption_handler_->AddObserver(&observer_);
|
| }
|
|
|
| @@ -109,13 +107,15 @@ class SyncEncryptionHandlerImplTest : public ::testing::Test {
|
| return encryption_handler_.get();
|
| }
|
| SyncEncryptionHandlerObserverMock* observer() { return &observer_; }
|
| - Cryptographer* cryptographer() { return cryptographer_; }
|
| + Cryptographer* GetCryptographer() {
|
| + return encryption_handler_->GetCryptographerUnsafe();
|
| + }
|
|
|
| - private:
|
| + protected:
|
| TestUserShare test_user_share_;
|
| + FakeEncryptor encryptor_;
|
| scoped_ptr<SyncEncryptionHandlerImpl> encryption_handler_;
|
| StrictMock<SyncEncryptionHandlerObserverMock> observer_;
|
| - Cryptographer* cryptographer_;
|
| TestIdFactory ids_;
|
| MessageLoop message_loop_;
|
| };
|
| @@ -127,23 +127,25 @@ TEST_F(SyncEncryptionHandlerImplTest, NigoriEncryptionTypes) {
|
|
|
| StrictMock<SyncEncryptionHandlerObserverMock> observer2;
|
| SyncEncryptionHandlerImpl handler2(user_share(),
|
| - cryptographer());
|
| + &encryptor_);
|
| handler2.AddObserver(&observer2);
|
|
|
| // Just set the sensitive types (shouldn't trigger any notifications).
|
| ModelTypeSet encrypted_types(SyncEncryptionHandler::SensitiveTypes());
|
| - encryption_handler()->MergeEncryptedTypes(encrypted_types);
|
| {
|
| WriteTransaction trans(FROM_HERE, user_share());
|
| + encryption_handler()->MergeEncryptedTypes(
|
| + encrypted_types,
|
| + trans.GetWrappedTrans());
|
| encryption_handler()->UpdateNigoriFromEncryptedTypes(
|
| &nigori,
|
| trans.GetWrappedTrans());
|
| + handler2.UpdateEncryptedTypesFromNigori(nigori, trans.GetWrappedTrans());
|
| }
|
| - handler2.UpdateEncryptedTypesFromNigori(nigori);
|
| EXPECT_TRUE(encrypted_types.Equals(
|
| - encryption_handler()->GetEncryptedTypes()));
|
| + encryption_handler()->GetEncryptedTypesUnsafe()));
|
| EXPECT_TRUE(encrypted_types.Equals(
|
| - handler2.GetEncryptedTypes()));
|
| + handler2.GetEncryptedTypesUnsafe()));
|
|
|
| Mock::VerifyAndClearExpectations(observer());
|
| Mock::VerifyAndClearExpectations(&observer2);
|
| @@ -157,41 +159,47 @@ TEST_F(SyncEncryptionHandlerImplTest, NigoriEncryptionTypes) {
|
|
|
| // Set all encrypted types
|
| encrypted_types = ModelTypeSet::All();
|
| - encryption_handler()->MergeEncryptedTypes(encrypted_types);
|
| {
|
| WriteTransaction trans(FROM_HERE, user_share());
|
| + encryption_handler()->MergeEncryptedTypes(
|
| + encrypted_types,
|
| + trans.GetWrappedTrans());
|
| encryption_handler()->UpdateNigoriFromEncryptedTypes(
|
| &nigori,
|
| trans.GetWrappedTrans());
|
| + handler2.UpdateEncryptedTypesFromNigori(nigori, trans.GetWrappedTrans());
|
| }
|
| - handler2.UpdateEncryptedTypesFromNigori(nigori);
|
| EXPECT_TRUE(encrypted_types.Equals(
|
| - encryption_handler()->GetEncryptedTypes()));
|
| - EXPECT_TRUE(encrypted_types.Equals(handler2.GetEncryptedTypes()));
|
| + encryption_handler()->GetEncryptedTypesUnsafe()));
|
| + EXPECT_TRUE(encrypted_types.Equals(handler2.GetEncryptedTypesUnsafe()));
|
|
|
| // Receiving an empty nigori should not reset any encrypted types or trigger
|
| // an observer notification.
|
| Mock::VerifyAndClearExpectations(observer());
|
| Mock::VerifyAndClearExpectations(&observer2);
|
| nigori = sync_pb::NigoriSpecifics();
|
| - encryption_handler()->UpdateEncryptedTypesFromNigori(nigori);
|
| + {
|
| + WriteTransaction trans(FROM_HERE, user_share());
|
| + handler2.UpdateEncryptedTypesFromNigori(nigori, trans.GetWrappedTrans());
|
| + }
|
| EXPECT_TRUE(encrypted_types.Equals(
|
| - encryption_handler()->GetEncryptedTypes()));
|
| + encryption_handler()->GetEncryptedTypesUnsafe()));
|
| }
|
|
|
| // Verify the encryption handler processes the encrypt everything field
|
| // properly.
|
| TEST_F(SyncEncryptionHandlerImplTest, EncryptEverythingExplicit) {
|
| ModelTypeSet real_types = ModelTypeSet::All();
|
| - sync_pb::NigoriSpecifics specifics;
|
| - specifics.set_encrypt_everything(true);
|
| + sync_pb::NigoriSpecifics nigori;
|
| + nigori.set_encrypt_everything(true);
|
|
|
| EXPECT_CALL(*observer(),
|
| OnEncryptedTypesChanged(
|
| HasModelTypes(ModelTypeSet::All()), true));
|
|
|
| EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
|
| - ModelTypeSet encrypted_types = encryption_handler()->GetEncryptedTypes();
|
| + ModelTypeSet encrypted_types =
|
| + encryption_handler()->GetEncryptedTypesUnsafe();
|
| for (ModelTypeSet::Iterator iter = real_types.First();
|
| iter.Good(); iter.Inc()) {
|
| if (iter.Get() == PASSWORDS || iter.Get() == NIGORI)
|
| @@ -200,10 +208,15 @@ TEST_F(SyncEncryptionHandlerImplTest, EncryptEverythingExplicit) {
|
| EXPECT_FALSE(encrypted_types.Has(iter.Get()));
|
| }
|
|
|
| - encryption_handler()->UpdateEncryptedTypesFromNigori(specifics);
|
| + {
|
| + WriteTransaction trans(FROM_HERE, user_share());
|
| + encryption_handler()->UpdateEncryptedTypesFromNigori(
|
| + nigori,
|
| + trans.GetWrappedTrans());
|
| + }
|
|
|
| EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
|
| - encrypted_types = encryption_handler()->GetEncryptedTypes();
|
| + encrypted_types = encryption_handler()->GetEncryptedTypesUnsafe();
|
| for (ModelTypeSet::Iterator iter = real_types.First();
|
| iter.Good(); iter.Inc()) {
|
| EXPECT_TRUE(encrypted_types.Has(iter.Get()));
|
| @@ -211,22 +224,28 @@ TEST_F(SyncEncryptionHandlerImplTest, EncryptEverythingExplicit) {
|
|
|
| // Receiving the nigori node again shouldn't trigger another notification.
|
| Mock::VerifyAndClearExpectations(observer());
|
| - encryption_handler()->UpdateEncryptedTypesFromNigori(specifics);
|
| + {
|
| + WriteTransaction trans(FROM_HERE, user_share());
|
| + encryption_handler()->UpdateEncryptedTypesFromNigori(
|
| + nigori,
|
| + trans.GetWrappedTrans());
|
| + }
|
| }
|
|
|
| // Verify the encryption handler can detect an implicit encrypt everything state
|
| // (from clients that failed to write the encrypt everything field).
|
| TEST_F(SyncEncryptionHandlerImplTest, EncryptEverythingImplicit) {
|
| ModelTypeSet real_types = ModelTypeSet::All();
|
| - sync_pb::NigoriSpecifics specifics;
|
| - specifics.set_encrypt_bookmarks(true); // Non-passwords = encrypt everything
|
| + sync_pb::NigoriSpecifics nigori;
|
| + nigori.set_encrypt_bookmarks(true); // Non-passwords = encrypt everything
|
|
|
| EXPECT_CALL(*observer(),
|
| OnEncryptedTypesChanged(
|
| HasModelTypes(ModelTypeSet::All()), true));
|
|
|
| EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
|
| - ModelTypeSet encrypted_types = encryption_handler()->GetEncryptedTypes();
|
| + ModelTypeSet encrypted_types =
|
| + encryption_handler()->GetEncryptedTypesUnsafe();
|
| for (ModelTypeSet::Iterator iter = real_types.First();
|
| iter.Good(); iter.Inc()) {
|
| if (iter.Get() == PASSWORDS || iter.Get() == NIGORI)
|
| @@ -235,10 +254,15 @@ TEST_F(SyncEncryptionHandlerImplTest, EncryptEverythingImplicit) {
|
| EXPECT_FALSE(encrypted_types.Has(iter.Get()));
|
| }
|
|
|
| - encryption_handler()->UpdateEncryptedTypesFromNigori(specifics);
|
| + {
|
| + WriteTransaction trans(FROM_HERE, user_share());
|
| + encryption_handler()->UpdateEncryptedTypesFromNigori(
|
| + nigori,
|
| + trans.GetWrappedTrans());
|
| + }
|
|
|
| EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
|
| - encrypted_types = encryption_handler()->GetEncryptedTypes();
|
| + encrypted_types = encryption_handler()->GetEncryptedTypesUnsafe();
|
| for (ModelTypeSet::Iterator iter = real_types.First();
|
| iter.Good(); iter.Inc()) {
|
| EXPECT_TRUE(encrypted_types.Has(iter.Get()));
|
| @@ -247,8 +271,13 @@ TEST_F(SyncEncryptionHandlerImplTest, EncryptEverythingImplicit) {
|
| // Receiving a nigori node with encrypt everything explicitly set shouldn't
|
| // trigger another notification.
|
| Mock::VerifyAndClearExpectations(observer());
|
| - specifics.set_encrypt_everything(true);
|
| - encryption_handler()->UpdateEncryptedTypesFromNigori(specifics);
|
| + nigori.set_encrypt_everything(true);
|
| + {
|
| + WriteTransaction trans(FROM_HERE, user_share());
|
| + encryption_handler()->UpdateEncryptedTypesFromNigori(
|
| + nigori,
|
| + trans.GetWrappedTrans());
|
| + }
|
| }
|
|
|
| // Verify the encryption handler can deal with new versions treating new types
|
| @@ -256,9 +285,9 @@ TEST_F(SyncEncryptionHandlerImplTest, EncryptEverythingImplicit) {
|
| // everything case.
|
| TEST_F(SyncEncryptionHandlerImplTest, UnknownSensitiveTypes) {
|
| ModelTypeSet real_types = ModelTypeSet::All();
|
| - sync_pb::NigoriSpecifics specifics;
|
| - specifics.set_encrypt_everything(false);
|
| - specifics.set_encrypt_bookmarks(true);
|
| + sync_pb::NigoriSpecifics nigori;
|
| + nigori.set_encrypt_everything(false);
|
| + nigori.set_encrypt_bookmarks(true);
|
|
|
| ModelTypeSet expected_encrypted_types =
|
| SyncEncryptionHandler::SensitiveTypes();
|
| @@ -269,7 +298,8 @@ TEST_F(SyncEncryptionHandlerImplTest, UnknownSensitiveTypes) {
|
| HasModelTypes(expected_encrypted_types), false));
|
|
|
| EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
|
| - ModelTypeSet encrypted_types = encryption_handler()->GetEncryptedTypes();
|
| + ModelTypeSet encrypted_types =
|
| + encryption_handler()->GetEncryptedTypesUnsafe();
|
| for (ModelTypeSet::Iterator iter = real_types.First();
|
| iter.Good(); iter.Inc()) {
|
| if (iter.Get() == PASSWORDS || iter.Get() == NIGORI)
|
| @@ -278,10 +308,15 @@ TEST_F(SyncEncryptionHandlerImplTest, UnknownSensitiveTypes) {
|
| EXPECT_FALSE(encrypted_types.Has(iter.Get()));
|
| }
|
|
|
| - encryption_handler()->UpdateEncryptedTypesFromNigori(specifics);
|
| + {
|
| + WriteTransaction trans(FROM_HERE, user_share());
|
| + encryption_handler()->UpdateEncryptedTypesFromNigori(
|
| + nigori,
|
| + trans.GetWrappedTrans());
|
| + }
|
|
|
| EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
|
| - encrypted_types = encryption_handler()->GetEncryptedTypes();
|
| + encrypted_types = encryption_handler()->GetEncryptedTypesUnsafe();
|
| for (ModelTypeSet::Iterator iter = real_types.First();
|
| iter.Good(); iter.Inc()) {
|
| if (iter.Get() == PASSWORDS ||
|
| @@ -301,7 +336,7 @@ TEST_F(SyncEncryptionHandlerImplTest, ReceiveOldNigori) {
|
| KeyParams current_key = {"localhost", "dummy", "cur"};
|
|
|
| // Data for testing encryption/decryption.
|
| - Cryptographer other_cryptographer(cryptographer()->encryptor());
|
| + Cryptographer other_cryptographer(GetCryptographer()->encryptor());
|
| other_cryptographer.AddKey(old_key);
|
| sync_pb::EntitySpecifics other_encrypted_specifics;
|
| other_encrypted_specifics.mutable_bookmark()->set_title("title");
|
| @@ -315,12 +350,12 @@ TEST_F(SyncEncryptionHandlerImplTest, ReceiveOldNigori) {
|
| // Set up the current encryption state (containing both keys and encrypt
|
| // everything).
|
| sync_pb::NigoriSpecifics current_nigori_specifics;
|
| - cryptographer()->AddKey(old_key);
|
| - cryptographer()->AddKey(current_key);
|
| - cryptographer()->Encrypt(
|
| + GetCryptographer()->AddKey(old_key);
|
| + GetCryptographer()->AddKey(current_key);
|
| + GetCryptographer()->Encrypt(
|
| our_encrypted_specifics,
|
| our_encrypted_specifics.mutable_encrypted());
|
| - cryptographer()->GetKeys(
|
| + GetCryptographer()->GetKeys(
|
| current_nigori_specifics.mutable_encrypted());
|
| current_nigori_specifics.set_encrypt_everything(true);
|
|
|
| @@ -349,8 +384,8 @@ TEST_F(SyncEncryptionHandlerImplTest, ReceiveOldNigori) {
|
| old_nigori,
|
| trans.GetWrappedTrans());
|
| }
|
| - EXPECT_TRUE(cryptographer()->is_ready());
|
| - EXPECT_FALSE(cryptographer()->has_pending_keys());
|
| + EXPECT_TRUE(GetCryptographer()->is_ready());
|
| + EXPECT_FALSE(GetCryptographer()->has_pending_keys());
|
|
|
| // Encryption handler should have posted a task to overwrite the old
|
| // specifics.
|
| @@ -366,13 +401,14 @@ TEST_F(SyncEncryptionHandlerImplTest, ReceiveOldNigori) {
|
| ASSERT_EQ(nigori_node.InitByTagLookup(ModelTypeToRootTag(NIGORI)),
|
| BaseNode::INIT_OK);
|
| const sync_pb::NigoriSpecifics& nigori = nigori_node.GetNigoriSpecifics();
|
| - EXPECT_TRUE(cryptographer()->CanDecryptUsingDefaultKey(
|
| + EXPECT_TRUE(GetCryptographer()->CanDecryptUsingDefaultKey(
|
| our_encrypted_specifics.encrypted()));
|
| - EXPECT_TRUE(cryptographer()->CanDecrypt(
|
| + EXPECT_TRUE(GetCryptographer()->CanDecrypt(
|
| other_encrypted_specifics.encrypted()));
|
| - EXPECT_TRUE(cryptographer()->CanDecrypt(nigori.encrypted()));
|
| + EXPECT_TRUE(GetCryptographer()->CanDecrypt(nigori.encrypted()));
|
| EXPECT_TRUE(nigori.encrypt_everything());
|
| - EXPECT_TRUE(cryptographer()->CanDecryptUsingDefaultKey(nigori.encrypted()));
|
| + EXPECT_TRUE(
|
| + GetCryptographer()->CanDecryptUsingDefaultKey(nigori.encrypted()));
|
| }
|
| EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
|
| }
|
|
|