| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sync/internal_api/sync_encryption_handler_impl.h" | 5 #include "sync/internal_api/sync_encryption_handler_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 using ::testing::Mock; | 33 using ::testing::Mock; |
| 34 using ::testing::StrictMock; | 34 using ::testing::StrictMock; |
| 35 | 35 |
| 36 class SyncEncryptionHandlerObserverMock | 36 class SyncEncryptionHandlerObserverMock |
| 37 : public SyncEncryptionHandler::Observer { | 37 : public SyncEncryptionHandler::Observer { |
| 38 public: | 38 public: |
| 39 MOCK_METHOD2(OnPassphraseRequired, | 39 MOCK_METHOD2(OnPassphraseRequired, |
| 40 void(PassphraseRequiredReason, | 40 void(PassphraseRequiredReason, |
| 41 const sync_pb::EncryptedData&)); // NOLINT | 41 const sync_pb::EncryptedData&)); // NOLINT |
| 42 MOCK_METHOD0(OnPassphraseAccepted, void()); // NOLINT | 42 MOCK_METHOD0(OnPassphraseAccepted, void()); // NOLINT |
| 43 MOCK_METHOD1(OnBootstrapTokenUpdated, void(const std::string&)); // NOLINT | 43 MOCK_METHOD2(OnBootstrapTokenUpdated, |
| 44 void(const std::string&, BootstrapTokenType type)); // NOLINT |
| 44 MOCK_METHOD2(OnEncryptedTypesChanged, | 45 MOCK_METHOD2(OnEncryptedTypesChanged, |
| 45 void(ModelTypeSet, bool)); // NOLINT | 46 void(ModelTypeSet, bool)); // NOLINT |
| 46 MOCK_METHOD0(OnEncryptionComplete, void()); // NOLINT | 47 MOCK_METHOD0(OnEncryptionComplete, void()); // NOLINT |
| 47 MOCK_METHOD1(OnCryptographerStateChanged, void(Cryptographer*)); // NOLINT | 48 MOCK_METHOD1(OnCryptographerStateChanged, void(Cryptographer*)); // NOLINT |
| 48 MOCK_METHOD1(OnPassphraseStateChanged, void(PassphraseState)); // NOLINT | 49 MOCK_METHOD1(OnPassphraseStateChanged, void(PassphraseState)); // NOLINT |
| 49 }; | 50 }; |
| 50 | 51 |
| 51 } // namespace | 52 } // namespace |
| 52 | 53 |
| 53 class SyncEncryptionHandlerImplTest : public ::testing::Test { | 54 class SyncEncryptionHandlerImplTest : public ::testing::Test { |
| 54 public: | 55 public: |
| 55 SyncEncryptionHandlerImplTest() {} | 56 SyncEncryptionHandlerImplTest() {} |
| 56 virtual ~SyncEncryptionHandlerImplTest() {} | 57 virtual ~SyncEncryptionHandlerImplTest() {} |
| 57 | 58 |
| 58 virtual void SetUp() { | 59 virtual void SetUp() { |
| 59 test_user_share_.SetUp(); | 60 test_user_share_.SetUp(); |
| 60 SetUpEncryption(); | 61 SetUpEncryption(); |
| 61 CreateRootForType(NIGORI); | 62 CreateRootForType(NIGORI); |
| 62 } | 63 } |
| 63 | 64 |
| 64 virtual void TearDown() { | 65 virtual void TearDown() { |
| 65 test_user_share_.TearDown(); | 66 test_user_share_.TearDown(); |
| 66 } | 67 } |
| 67 | 68 |
| 68 protected: | 69 protected: |
| 69 void SetUpEncryption() { | 70 void SetUpEncryption() { |
| 70 ReadTransaction trans(FROM_HERE, user_share()); | |
| 71 encryption_handler_.reset( | 71 encryption_handler_.reset( |
| 72 new SyncEncryptionHandlerImpl(user_share(), | 72 new SyncEncryptionHandlerImpl(user_share(), |
| 73 &encryptor_)); | 73 &encryptor_, |
| 74 "", "" /* bootstrap tokens */)); |
| 74 encryption_handler_->AddObserver(&observer_); | 75 encryption_handler_->AddObserver(&observer_); |
| 75 } | 76 } |
| 76 | 77 |
| 77 void CreateRootForType(ModelType model_type) { | 78 void CreateRootForType(ModelType model_type) { |
| 78 syncer::syncable::Directory* directory = user_share()->directory.get(); | 79 syncer::syncable::Directory* directory = user_share()->directory.get(); |
| 79 | 80 |
| 80 std::string tag_name = ModelTypeToRootTag(model_type); | 81 std::string tag_name = ModelTypeToRootTag(model_type); |
| 81 | 82 |
| 82 syncable::WriteTransaction wtrans(FROM_HERE, syncable::UNITTEST, directory); | 83 syncable::WriteTransaction wtrans(FROM_HERE, syncable::UNITTEST, directory); |
| 83 syncable::MutableEntry node(&wtrans, | 84 syncable::MutableEntry node(&wtrans, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 MessageLoop message_loop_; | 122 MessageLoop message_loop_; |
| 122 }; | 123 }; |
| 123 | 124 |
| 124 // Verify that the encrypted types are being written to and read from the | 125 // Verify that the encrypted types are being written to and read from the |
| 125 // nigori node properly. | 126 // nigori node properly. |
| 126 TEST_F(SyncEncryptionHandlerImplTest, NigoriEncryptionTypes) { | 127 TEST_F(SyncEncryptionHandlerImplTest, NigoriEncryptionTypes) { |
| 127 sync_pb::NigoriSpecifics nigori; | 128 sync_pb::NigoriSpecifics nigori; |
| 128 | 129 |
| 129 StrictMock<SyncEncryptionHandlerObserverMock> observer2; | 130 StrictMock<SyncEncryptionHandlerObserverMock> observer2; |
| 130 SyncEncryptionHandlerImpl handler2(user_share(), | 131 SyncEncryptionHandlerImpl handler2(user_share(), |
| 131 &encryptor_); | 132 &encryptor_, |
| 133 "", "" /* bootstrap tokens */); |
| 132 handler2.AddObserver(&observer2); | 134 handler2.AddObserver(&observer2); |
| 133 | 135 |
| 134 // Just set the sensitive types (shouldn't trigger any notifications). | 136 // Just set the sensitive types (shouldn't trigger any notifications). |
| 135 ModelTypeSet encrypted_types(SyncEncryptionHandler::SensitiveTypes()); | 137 ModelTypeSet encrypted_types(SyncEncryptionHandler::SensitiveTypes()); |
| 136 { | 138 { |
| 137 WriteTransaction trans(FROM_HERE, user_share()); | 139 WriteTransaction trans(FROM_HERE, user_share()); |
| 138 encryption_handler()->MergeEncryptedTypes( | 140 encryption_handler()->MergeEncryptedTypes( |
| 139 encrypted_types, | 141 encrypted_types, |
| 140 trans.GetWrappedTrans()); | 142 trans.GetWrappedTrans()); |
| 141 encryption_handler()->UpdateNigoriFromEncryptedTypes( | 143 encryption_handler()->UpdateNigoriFromEncryptedTypes( |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 EXPECT_TRUE(GetCryptographer()->CanDecrypt( | 409 EXPECT_TRUE(GetCryptographer()->CanDecrypt( |
| 408 other_encrypted_specifics.encrypted())); | 410 other_encrypted_specifics.encrypted())); |
| 409 EXPECT_TRUE(GetCryptographer()->CanDecrypt(nigori.encrypted())); | 411 EXPECT_TRUE(GetCryptographer()->CanDecrypt(nigori.encrypted())); |
| 410 EXPECT_TRUE(nigori.encrypt_everything()); | 412 EXPECT_TRUE(nigori.encrypt_everything()); |
| 411 EXPECT_TRUE( | 413 EXPECT_TRUE( |
| 412 GetCryptographer()->CanDecryptUsingDefaultKey(nigori.encrypted())); | 414 GetCryptographer()->CanDecryptUsingDefaultKey(nigori.encrypted())); |
| 413 } | 415 } |
| 414 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); | 416 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); |
| 415 } | 417 } |
| 416 | 418 |
| 419 // Ensure setting the keystore key works, updates the bootstrap token, and |
| 420 // doesn't modify the cryptographer. |
| 421 TEST_F(SyncEncryptionHandlerImplTest, SetKeystoreUpdatedBoostrapToken) { |
| 422 WriteTransaction trans(FROM_HERE, user_share()); |
| 423 EXPECT_CALL(*observer(), OnBootstrapTokenUpdated(_, _)).Times(0); |
| 424 EXPECT_FALSE(GetCryptographer()->is_initialized()); |
| 425 EXPECT_TRUE(encryption_handler()->NeedKeystoreKey(trans.GetWrappedTrans())); |
| 426 EXPECT_FALSE(encryption_handler()->SetKeystoreKey("", |
| 427 trans.GetWrappedTrans())); |
| 428 EXPECT_TRUE(encryption_handler()->NeedKeystoreKey(trans.GetWrappedTrans())); |
| 429 Mock::VerifyAndClearExpectations(observer()); |
| 430 |
| 431 const char kValidKey[] = "keystore_key"; |
| 432 EXPECT_CALL(*observer(), |
| 433 OnBootstrapTokenUpdated(kValidKey, KEYSTORE_BOOTSTRAP_TOKEN)); |
| 434 EXPECT_TRUE(encryption_handler()->SetKeystoreKey(kValidKey, |
| 435 trans.GetWrappedTrans())); |
| 436 EXPECT_FALSE(encryption_handler()->NeedKeystoreKey(trans.GetWrappedTrans())); |
| 437 EXPECT_FALSE(GetCryptographer()->is_initialized()); |
| 438 } |
| 439 |
| 417 } // namespace syncer | 440 } // namespace syncer |
| OLD | NEW |