Chromium Code Reviews| 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 std::string(), std::string())); | |
|
akalin
2012/08/22 22:09:33
why not ""?
Nicolas Zea
2012/08/22 22:48:59
Done.
| |
| 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 std::string(), std::string()); | |
| 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 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 404 EXPECT_TRUE(GetCryptographer()->CanDecrypt( | 406 EXPECT_TRUE(GetCryptographer()->CanDecrypt( |
| 405 other_encrypted_specifics.encrypted())); | 407 other_encrypted_specifics.encrypted())); |
| 406 EXPECT_TRUE(GetCryptographer()->CanDecrypt(nigori.encrypted())); | 408 EXPECT_TRUE(GetCryptographer()->CanDecrypt(nigori.encrypted())); |
| 407 EXPECT_TRUE(nigori.encrypt_everything()); | 409 EXPECT_TRUE(nigori.encrypt_everything()); |
| 408 EXPECT_TRUE( | 410 EXPECT_TRUE( |
| 409 GetCryptographer()->CanDecryptUsingDefaultKey(nigori.encrypted())); | 411 GetCryptographer()->CanDecryptUsingDefaultKey(nigori.encrypted())); |
| 410 } | 412 } |
| 411 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); | 413 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); |
| 412 } | 414 } |
| 413 | 415 |
| 416 // Ensure setting the keystore key works, updates the bootstrap token, and | |
| 417 // doesn't modify the cryptographer. | |
| 418 TEST_F(SyncEncryptionHandlerImplTest, SetKeystoreUpdatedBoostrapToken) { | |
| 419 WriteTransaction trans(FROM_HERE, user_share()); | |
| 420 EXPECT_CALL(*observer(), OnBootstrapTokenUpdated(_, _)).Times(0); | |
| 421 EXPECT_FALSE(encryption_handler()->cryptographer_unsafe()->is_initialized()); | |
| 422 EXPECT_TRUE(encryption_handler()->NeedKeystoreKey(trans.GetWrappedTrans())); | |
| 423 EXPECT_FALSE(encryption_handler()->SetKeystoreKey("", | |
| 424 trans.GetWrappedTrans())); | |
| 425 EXPECT_TRUE(encryption_handler()->NeedKeystoreKey(trans.GetWrappedTrans())); | |
| 426 Mock::VerifyAndClearExpectations(observer()); | |
| 427 | |
| 428 std::string valid_key = "keystore_key"; | |
|
akalin
2012/08/22 22:09:33
const char kValidKey[] = ...
Nicolas Zea
2012/08/22 22:48:59
Done.
| |
| 429 EXPECT_CALL(*observer(), | |
| 430 OnBootstrapTokenUpdated(valid_key, KEYSTORE_BOOTSTRAP_TOKEN)); | |
| 431 EXPECT_TRUE(encryption_handler()->SetKeystoreKey(valid_key, | |
| 432 trans.GetWrappedTrans())); | |
| 433 EXPECT_FALSE(encryption_handler()->NeedKeystoreKey(trans.GetWrappedTrans())); | |
| 434 EXPECT_FALSE(encryption_handler()->cryptographer_unsafe()->is_initialized()); | |
| 435 } | |
| 436 | |
| 414 } // namespace syncer | 437 } // namespace syncer |
| OLD | NEW |