Chromium Code Reviews| 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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 62 #include "sync/syncable/syncable_read_transaction.h" | 62 #include "sync/syncable/syncable_read_transaction.h" |
| 63 #include "sync/syncable/syncable_util.h" | 63 #include "sync/syncable/syncable_util.h" |
| 64 #include "sync/syncable/syncable_write_transaction.h" | 64 #include "sync/syncable/syncable_write_transaction.h" |
| 65 #include "sync/test/callback_counter.h" | 65 #include "sync/test/callback_counter.h" |
| 66 #include "sync/test/engine/fake_model_worker.h" | 66 #include "sync/test/engine/fake_model_worker.h" |
| 67 #include "sync/test/engine/fake_sync_scheduler.h" | 67 #include "sync/test/engine/fake_sync_scheduler.h" |
| 68 #include "sync/test/engine/test_id_factory.h" | 68 #include "sync/test/engine/test_id_factory.h" |
| 69 #include "sync/test/fake_encryptor.h" | 69 #include "sync/test/fake_encryptor.h" |
| 70 #include "sync/util/cryptographer.h" | 70 #include "sync/util/cryptographer.h" |
| 71 #include "sync/util/extensions_activity.h" | 71 #include "sync/util/extensions_activity.h" |
| 72 #include "sync/util/test_unrecoverable_error_handler.h" | 72 #include "sync/util/mock_unrecoverable_error_handler.h" |
| 73 #include "sync/util/time.h" | 73 #include "sync/util/time.h" |
| 74 #include "testing/gmock/include/gmock/gmock.h" | 74 #include "testing/gmock/include/gmock/gmock.h" |
| 75 #include "testing/gtest/include/gtest/gtest.h" | 75 #include "testing/gtest/include/gtest/gtest.h" |
| 76 #include "url/gurl.h" | 76 #include "url/gurl.h" |
| 77 | 77 |
| 78 using base::ExpectDictStringValue; | 78 using base::ExpectDictStringValue; |
| 79 using testing::_; | 79 using testing::_; |
| 80 using testing::DoAll; | 80 using testing::DoAll; |
| 81 using testing::InSequence; | 81 using testing::InSequence; |
| 82 using testing::Return; | 82 using testing::Return; |
| (...skipping 746 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 829 WRITE_TO_NIGORI | 829 WRITE_TO_NIGORI |
| 830 }; | 830 }; |
| 831 | 831 |
| 832 enum EncryptionStatus { | 832 enum EncryptionStatus { |
| 833 UNINITIALIZED, | 833 UNINITIALIZED, |
| 834 DEFAULT_ENCRYPTION, | 834 DEFAULT_ENCRYPTION, |
| 835 FULL_ENCRYPTION | 835 FULL_ENCRYPTION |
| 836 }; | 836 }; |
| 837 | 837 |
| 838 SyncManagerTest() | 838 SyncManagerTest() |
| 839 : sync_manager_("Test sync manager") { | 839 : sync_manager_("Test sync manager"), |
| 840 mock_unrecoverable_error_handler_(NULL) { | |
| 840 switches_.encryption_method = | 841 switches_.encryption_method = |
| 841 InternalComponentsFactory::ENCRYPTION_KEYSTORE; | 842 InternalComponentsFactory::ENCRYPTION_KEYSTORE; |
| 842 } | 843 } |
| 843 | 844 |
| 844 virtual ~SyncManagerTest() { | 845 virtual ~SyncManagerTest() { |
| 845 } | 846 } |
| 846 | 847 |
| 847 // Test implementation. | 848 // Test implementation. |
| 848 void SetUp() { | 849 void SetUp() { |
| 849 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 850 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 879 args.service_url = GURL("https://example.com/"); | 880 args.service_url = GURL("https://example.com/"); |
| 880 args.post_factory = | 881 args.post_factory = |
| 881 scoped_ptr<HttpPostProviderFactory>(new TestHttpPostProviderFactory()); | 882 scoped_ptr<HttpPostProviderFactory>(new TestHttpPostProviderFactory()); |
| 882 args.workers = workers; | 883 args.workers = workers; |
| 883 args.extensions_activity = extensions_activity_.get(), | 884 args.extensions_activity = extensions_activity_.get(), |
| 884 args.change_delegate = this; | 885 args.change_delegate = this; |
| 885 args.credentials = credentials; | 886 args.credentials = credentials; |
| 886 args.invalidator_client_id = "fake_invalidator_client_id"; | 887 args.invalidator_client_id = "fake_invalidator_client_id"; |
| 887 args.internal_components_factory.reset(GetFactory()); | 888 args.internal_components_factory.reset(GetFactory()); |
| 888 args.encryptor = &encryptor_; | 889 args.encryptor = &encryptor_; |
| 889 args.unrecoverable_error_handler.reset(new TestUnrecoverableErrorHandler); | 890 mock_unrecoverable_error_handler_ = new MockUnrecoverableErrorHandler(); |
| 891 args.unrecoverable_error_handler.reset(mock_unrecoverable_error_handler_); | |
| 890 args.cancelation_signal = &cancelation_signal_; | 892 args.cancelation_signal = &cancelation_signal_; |
| 891 sync_manager_.Init(&args); | 893 sync_manager_.Init(&args); |
| 892 | 894 |
| 893 sync_manager_.GetEncryptionHandler()->AddObserver(&encryption_observer_); | 895 sync_manager_.GetEncryptionHandler()->AddObserver(&encryption_observer_); |
| 894 | 896 |
| 895 EXPECT_TRUE(js_backend_.IsInitialized()); | 897 EXPECT_TRUE(js_backend_.IsInitialized()); |
| 896 EXPECT_EQ(InternalComponentsFactory::STORAGE_ON_DISK, | 898 EXPECT_EQ(InternalComponentsFactory::STORAGE_ON_DISK, |
| 897 storage_used_); | 899 storage_used_); |
| 898 | 900 |
| 899 if (initialization_succeeded_) { | 901 if (initialization_succeeded_) { |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1073 void SetCustomPassphraseAndCheck(const std::string& passphrase) { | 1075 void SetCustomPassphraseAndCheck(const std::string& passphrase) { |
| 1074 EXPECT_CALL(encryption_observer_, | 1076 EXPECT_CALL(encryption_observer_, |
| 1075 OnPassphraseTypeChanged(CUSTOM_PASSPHRASE, _)); | 1077 OnPassphraseTypeChanged(CUSTOM_PASSPHRASE, _)); |
| 1076 sync_manager_.GetEncryptionHandler()->SetEncryptionPassphrase( | 1078 sync_manager_.GetEncryptionHandler()->SetEncryptionPassphrase( |
| 1077 passphrase, | 1079 passphrase, |
| 1078 true); | 1080 true); |
| 1079 EXPECT_EQ(CUSTOM_PASSPHRASE, | 1081 EXPECT_EQ(CUSTOM_PASSPHRASE, |
| 1080 sync_manager_.GetEncryptionHandler()->GetPassphraseType()); | 1082 sync_manager_.GetEncryptionHandler()->GetPassphraseType()); |
| 1081 } | 1083 } |
| 1082 | 1084 |
| 1085 bool HasUnrecoverableError() { | |
| 1086 if (mock_unrecoverable_error_handler_) | |
| 1087 return mock_unrecoverable_error_handler_->invocation_count() > 0; | |
| 1088 return false; | |
| 1089 } | |
| 1090 | |
| 1083 private: | 1091 private: |
| 1084 // Needed by |sync_manager_|. | 1092 // Needed by |sync_manager_|. |
| 1085 base::MessageLoop message_loop_; | 1093 base::MessageLoop message_loop_; |
| 1086 // Needed by |sync_manager_|. | 1094 // Needed by |sync_manager_|. |
| 1087 base::ScopedTempDir temp_dir_; | 1095 base::ScopedTempDir temp_dir_; |
| 1088 // Sync Id's for the roots of the enabled datatypes. | 1096 // Sync Id's for the roots of the enabled datatypes. |
| 1089 std::map<ModelType, int64> type_roots_; | 1097 std::map<ModelType, int64> type_roots_; |
| 1090 scoped_refptr<ExtensionsActivity> extensions_activity_; | 1098 scoped_refptr<ExtensionsActivity> extensions_activity_; |
| 1091 | 1099 |
| 1092 protected: | 1100 protected: |
| 1093 FakeEncryptor encryptor_; | 1101 FakeEncryptor encryptor_; |
| 1094 SyncManagerImpl sync_manager_; | 1102 SyncManagerImpl sync_manager_; |
| 1095 CancelationSignal cancelation_signal_; | 1103 CancelationSignal cancelation_signal_; |
| 1096 WeakHandle<JsBackend> js_backend_; | 1104 WeakHandle<JsBackend> js_backend_; |
| 1097 bool initialization_succeeded_; | 1105 bool initialization_succeeded_; |
| 1098 StrictMock<SyncManagerObserverMock> manager_observer_; | 1106 StrictMock<SyncManagerObserverMock> manager_observer_; |
| 1099 StrictMock<SyncEncryptionHandlerObserverMock> encryption_observer_; | 1107 StrictMock<SyncEncryptionHandlerObserverMock> encryption_observer_; |
| 1100 InternalComponentsFactory::Switches switches_; | 1108 InternalComponentsFactory::Switches switches_; |
| 1101 InternalComponentsFactory::StorageOption storage_used_; | 1109 InternalComponentsFactory::StorageOption storage_used_; |
| 1110 MockUnrecoverableErrorHandler* mock_unrecoverable_error_handler_; | |
|
stanisc
2015/06/09 00:04:02
Consider adding a comment about the ownership.
Nicolas Zea
2015/06/09 19:42:03
Done.
| |
| 1102 }; | 1111 }; |
| 1103 | 1112 |
| 1104 TEST_F(SyncManagerTest, GetAllNodesForTypeTest) { | 1113 TEST_F(SyncManagerTest, GetAllNodesForTypeTest) { |
| 1105 ModelSafeRoutingInfo routing_info; | 1114 ModelSafeRoutingInfo routing_info; |
| 1106 GetModelSafeRoutingInfo(&routing_info); | 1115 GetModelSafeRoutingInfo(&routing_info); |
| 1107 sync_manager_.StartSyncingNormally(routing_info, base::Time()); | 1116 sync_manager_.StartSyncingNormally(routing_info, base::Time()); |
| 1108 | 1117 |
| 1109 scoped_ptr<base::ListValue> node_list( | 1118 scoped_ptr<base::ListValue> node_list( |
| 1110 sync_manager_.GetAllNodesForType(syncer::PREFERENCES)); | 1119 sync_manager_.GetAllNodesForType(syncer::PREFERENCES)); |
| 1111 | 1120 |
| (...skipping 939 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2051 // Force a re-encrypt everything. Should not set is_unsynced. | 2060 // Force a re-encrypt everything. Should not set is_unsynced. |
| 2052 testing::Mock::VerifyAndClearExpectations(&encryption_observer_); | 2061 testing::Mock::VerifyAndClearExpectations(&encryption_observer_); |
| 2053 EXPECT_CALL(encryption_observer_, OnEncryptionComplete()); | 2062 EXPECT_CALL(encryption_observer_, OnEncryptionComplete()); |
| 2054 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_)); | 2063 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_)); |
| 2055 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, false)); | 2064 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, false)); |
| 2056 sync_manager_.GetEncryptionHandler()->Init(); | 2065 sync_manager_.GetEncryptionHandler()->Init(); |
| 2057 PumpLoop(); | 2066 PumpLoop(); |
| 2058 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag)); | 2067 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag)); |
| 2059 } | 2068 } |
| 2060 | 2069 |
| 2070 // Test that attempting to start up with corrupted password data triggers | |
| 2071 // an unrecoverable error (rather than crashing). | |
| 2072 TEST_F(SyncManagerTest, ReencryptEverythingWithUnrecoverableErrorPasswords) { | |
| 2073 const char kClientTag[] = "client_tag"; | |
| 2074 | |
| 2075 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | |
| 2076 sync_pb::EntitySpecifics entity_specifics; | |
| 2077 { | |
| 2078 // Create a synced bookmark with undecryptable data. | |
| 2079 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2080 | |
| 2081 Cryptographer other_cryptographer(&encryptor_); | |
| 2082 KeyParams fake_params = {"localhost", "dummy", "fake_key"}; | |
| 2083 other_cryptographer.AddKey(fake_params); | |
| 2084 sync_pb::PasswordSpecificsData data; | |
| 2085 data.set_password_value("secret"); | |
| 2086 other_cryptographer.Encrypt( | |
| 2087 data, | |
| 2088 entity_specifics.mutable_password()->mutable_encrypted()); | |
| 2089 | |
| 2090 // Set up the real cryptographer with a different key. | |
| 2091 KeyParams real_params = {"localhost", "username", "real_key"}; | |
| 2092 trans.GetCryptographer()->AddKey(real_params); | |
| 2093 } | |
| 2094 MakeServerNode(sync_manager_.GetUserShare(), PASSWORDS, kClientTag, | |
| 2095 syncable::GenerateSyncableHash(PASSWORDS, | |
| 2096 kClientTag), | |
| 2097 entity_specifics); | |
| 2098 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, kClientTag)); | |
| 2099 | |
| 2100 // Force a re-encrypt everything. Should trigger an unrecoverable error due | |
| 2101 // to being unable to decrypt the data that was previously applied. | |
| 2102 testing::Mock::VerifyAndClearExpectations(&encryption_observer_); | |
| 2103 EXPECT_CALL(encryption_observer_, OnEncryptionComplete()); | |
| 2104 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_)); | |
| 2105 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, false)); | |
| 2106 EXPECT_FALSE(HasUnrecoverableError()); | |
| 2107 sync_manager_.GetEncryptionHandler()->Init(); | |
| 2108 PumpLoop(); | |
| 2109 EXPECT_TRUE(HasUnrecoverableError()); | |
| 2110 } | |
| 2111 | |
| 2112 // Test that attempting to start up with corrupted bookmark data triggers | |
| 2113 // an unrecoverable error (rather than crashing). | |
| 2114 TEST_F(SyncManagerTest, ReencryptEverythingWithUnrecoverableErrorBookmarks) { | |
| 2115 const char kClientTag[] = "client_tag"; | |
| 2116 EXPECT_CALL(encryption_observer_, | |
| 2117 OnEncryptedTypesChanged( | |
| 2118 HasModelTypes(EncryptableUserTypes()), true)); | |
| 2119 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, FULL_ENCRYPTION)); | |
| 2120 sync_pb::EntitySpecifics entity_specifics; | |
| 2121 { | |
| 2122 // Create a synced bookmark with undecryptable data. | |
| 2123 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | |
| 2124 | |
| 2125 Cryptographer other_cryptographer(&encryptor_); | |
| 2126 KeyParams fake_params = {"localhost", "dummy", "fake_key"}; | |
| 2127 other_cryptographer.AddKey(fake_params); | |
| 2128 sync_pb::EntitySpecifics bm_specifics; | |
| 2129 bm_specifics.mutable_bookmark()->set_title("title"); | |
| 2130 bm_specifics.mutable_bookmark()->set_url("url"); | |
| 2131 sync_pb::EncryptedData encrypted; | |
| 2132 other_cryptographer.Encrypt(bm_specifics, &encrypted); | |
| 2133 entity_specifics.mutable_encrypted()->CopyFrom(encrypted); | |
| 2134 | |
| 2135 // Set up the real cryptographer with a different key. | |
| 2136 KeyParams real_params = {"localhost", "username", "real_key"}; | |
| 2137 trans.GetCryptographer()->AddKey(real_params); | |
| 2138 } | |
| 2139 MakeServerNode(sync_manager_.GetUserShare(), BOOKMARKS, kClientTag, | |
| 2140 syncable::GenerateSyncableHash(BOOKMARKS, | |
| 2141 kClientTag), | |
| 2142 entity_specifics); | |
| 2143 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, kClientTag)); | |
| 2144 | |
| 2145 // Force a re-encrypt everything. Should trigger an unrecoverable error due | |
| 2146 // to being unable to decrypt the data that was previously applied. | |
| 2147 testing::Mock::VerifyAndClearExpectations(&encryption_observer_); | |
| 2148 EXPECT_CALL(encryption_observer_, OnEncryptionComplete()); | |
| 2149 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_)); | |
| 2150 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, true)); | |
| 2151 EXPECT_FALSE(HasUnrecoverableError()); | |
| 2152 sync_manager_.GetEncryptionHandler()->Init(); | |
| 2153 PumpLoop(); | |
| 2154 EXPECT_TRUE(HasUnrecoverableError()); | |
| 2155 } | |
| 2156 | |
| 2061 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for bookmarks | 2157 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for bookmarks |
| 2062 // when we write the same data, but does set it when we write new data. | 2158 // when we write the same data, but does set it when we write new data. |
| 2063 TEST_F(SyncManagerTest, SetBookmarkTitle) { | 2159 TEST_F(SyncManagerTest, SetBookmarkTitle) { |
| 2064 std::string client_tag = "title"; | 2160 std::string client_tag = "title"; |
| 2065 sync_pb::EntitySpecifics entity_specifics; | 2161 sync_pb::EntitySpecifics entity_specifics; |
| 2066 entity_specifics.mutable_bookmark()->set_url("url"); | 2162 entity_specifics.mutable_bookmark()->set_url("url"); |
| 2067 entity_specifics.mutable_bookmark()->set_title("title"); | 2163 entity_specifics.mutable_bookmark()->set_title("title"); |
| 2068 MakeServerNode(sync_manager_.GetUserShare(), BOOKMARKS, client_tag, | 2164 MakeServerNode(sync_manager_.GetUserShare(), BOOKMARKS, client_tag, |
| 2069 syncable::GenerateSyncableHash(BOOKMARKS, | 2165 syncable::GenerateSyncableHash(BOOKMARKS, |
| 2070 client_tag), | 2166 client_tag), |
| (...skipping 1155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3226 // SyncManagerInitInvalidStorageTest::GetFactory will return | 3322 // SyncManagerInitInvalidStorageTest::GetFactory will return |
| 3227 // DirectoryBackingStore that ensures that SyncManagerImpl::OpenDirectory fails. | 3323 // DirectoryBackingStore that ensures that SyncManagerImpl::OpenDirectory fails. |
| 3228 // SyncManagerImpl initialization is done in SyncManagerTest::SetUp. This test's | 3324 // SyncManagerImpl initialization is done in SyncManagerTest::SetUp. This test's |
| 3229 // task is to ensure that SyncManagerImpl reported initialization failure in | 3325 // task is to ensure that SyncManagerImpl reported initialization failure in |
| 3230 // OnInitializationComplete callback. | 3326 // OnInitializationComplete callback. |
| 3231 TEST_F(SyncManagerInitInvalidStorageTest, FailToOpenDatabase) { | 3327 TEST_F(SyncManagerInitInvalidStorageTest, FailToOpenDatabase) { |
| 3232 EXPECT_FALSE(initialization_succeeded_); | 3328 EXPECT_FALSE(initialization_succeeded_); |
| 3233 } | 3329 } |
| 3234 | 3330 |
| 3235 } // namespace syncer | 3331 } // namespace syncer |
| OLD | NEW |