Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1283)

Side by Side Diff: sync/internal_api/sync_manager_impl_unittest.cc

Issue 1161463005: [Sync] Don't crash for encryption errors (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address final comments Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
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
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
1111 // Not owned (ownership is passed to the SyncManager).
1112 MockUnrecoverableErrorHandler* mock_unrecoverable_error_handler_;
1102 }; 1113 };
1103 1114
1104 TEST_F(SyncManagerTest, GetAllNodesForTypeTest) { 1115 TEST_F(SyncManagerTest, GetAllNodesForTypeTest) {
1105 ModelSafeRoutingInfo routing_info; 1116 ModelSafeRoutingInfo routing_info;
1106 GetModelSafeRoutingInfo(&routing_info); 1117 GetModelSafeRoutingInfo(&routing_info);
1107 sync_manager_.StartSyncingNormally(routing_info, base::Time()); 1118 sync_manager_.StartSyncingNormally(routing_info, base::Time());
1108 1119
1109 scoped_ptr<base::ListValue> node_list( 1120 scoped_ptr<base::ListValue> node_list(
1110 sync_manager_.GetAllNodesForType(syncer::PREFERENCES)); 1121 sync_manager_.GetAllNodesForType(syncer::PREFERENCES));
1111 1122
(...skipping 939 matching lines...) Expand 10 before | Expand all | Expand 10 after
2051 // Force a re-encrypt everything. Should not set is_unsynced. 2062 // Force a re-encrypt everything. Should not set is_unsynced.
2052 testing::Mock::VerifyAndClearExpectations(&encryption_observer_); 2063 testing::Mock::VerifyAndClearExpectations(&encryption_observer_);
2053 EXPECT_CALL(encryption_observer_, OnEncryptionComplete()); 2064 EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
2054 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_)); 2065 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
2055 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, false)); 2066 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, false));
2056 sync_manager_.GetEncryptionHandler()->Init(); 2067 sync_manager_.GetEncryptionHandler()->Init();
2057 PumpLoop(); 2068 PumpLoop();
2058 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag)); 2069 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag));
2059 } 2070 }
2060 2071
2072 // Test that attempting to start up with corrupted password data triggers
2073 // an unrecoverable error (rather than crashing).
2074 TEST_F(SyncManagerTest, ReencryptEverythingWithUnrecoverableErrorPasswords) {
2075 const char kClientTag[] = "client_tag";
2076
2077 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
2078 sync_pb::EntitySpecifics entity_specifics;
2079 {
2080 // Create a synced bookmark with undecryptable data.
2081 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2082
2083 Cryptographer other_cryptographer(&encryptor_);
2084 KeyParams fake_params = {"localhost", "dummy", "fake_key"};
2085 other_cryptographer.AddKey(fake_params);
2086 sync_pb::PasswordSpecificsData data;
2087 data.set_password_value("secret");
2088 other_cryptographer.Encrypt(
2089 data,
2090 entity_specifics.mutable_password()->mutable_encrypted());
2091
2092 // Set up the real cryptographer with a different key.
2093 KeyParams real_params = {"localhost", "username", "real_key"};
2094 trans.GetCryptographer()->AddKey(real_params);
2095 }
2096 MakeServerNode(sync_manager_.GetUserShare(), PASSWORDS, kClientTag,
2097 syncable::GenerateSyncableHash(PASSWORDS,
2098 kClientTag),
2099 entity_specifics);
2100 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, kClientTag));
2101
2102 // Force a re-encrypt everything. Should trigger an unrecoverable error due
2103 // to being unable to decrypt the data that was previously applied.
2104 testing::Mock::VerifyAndClearExpectations(&encryption_observer_);
2105 EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
2106 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
2107 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, false));
2108 EXPECT_FALSE(HasUnrecoverableError());
2109 sync_manager_.GetEncryptionHandler()->Init();
2110 PumpLoop();
2111 EXPECT_TRUE(HasUnrecoverableError());
2112 }
2113
2114 // Test that attempting to start up with corrupted bookmark data triggers
2115 // an unrecoverable error (rather than crashing).
2116 TEST_F(SyncManagerTest, ReencryptEverythingWithUnrecoverableErrorBookmarks) {
2117 const char kClientTag[] = "client_tag";
2118 EXPECT_CALL(encryption_observer_,
2119 OnEncryptedTypesChanged(
2120 HasModelTypes(EncryptableUserTypes()), true));
2121 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, FULL_ENCRYPTION));
2122 sync_pb::EntitySpecifics entity_specifics;
2123 {
2124 // Create a synced bookmark with undecryptable data.
2125 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2126
2127 Cryptographer other_cryptographer(&encryptor_);
2128 KeyParams fake_params = {"localhost", "dummy", "fake_key"};
2129 other_cryptographer.AddKey(fake_params);
2130 sync_pb::EntitySpecifics bm_specifics;
2131 bm_specifics.mutable_bookmark()->set_title("title");
2132 bm_specifics.mutable_bookmark()->set_url("url");
2133 sync_pb::EncryptedData encrypted;
2134 other_cryptographer.Encrypt(bm_specifics, &encrypted);
2135 entity_specifics.mutable_encrypted()->CopyFrom(encrypted);
2136
2137 // Set up the real cryptographer with a different key.
2138 KeyParams real_params = {"localhost", "username", "real_key"};
2139 trans.GetCryptographer()->AddKey(real_params);
2140 }
2141 MakeServerNode(sync_manager_.GetUserShare(), BOOKMARKS, kClientTag,
2142 syncable::GenerateSyncableHash(BOOKMARKS,
2143 kClientTag),
2144 entity_specifics);
2145 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, kClientTag));
2146
2147 // Force a re-encrypt everything. Should trigger an unrecoverable error due
2148 // to being unable to decrypt the data that was previously applied.
2149 testing::Mock::VerifyAndClearExpectations(&encryption_observer_);
2150 EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
2151 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
2152 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, true));
2153 EXPECT_FALSE(HasUnrecoverableError());
2154 sync_manager_.GetEncryptionHandler()->Init();
2155 PumpLoop();
2156 EXPECT_TRUE(HasUnrecoverableError());
2157 }
2158
2061 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for bookmarks 2159 // 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. 2160 // when we write the same data, but does set it when we write new data.
2063 TEST_F(SyncManagerTest, SetBookmarkTitle) { 2161 TEST_F(SyncManagerTest, SetBookmarkTitle) {
2064 std::string client_tag = "title"; 2162 std::string client_tag = "title";
2065 sync_pb::EntitySpecifics entity_specifics; 2163 sync_pb::EntitySpecifics entity_specifics;
2066 entity_specifics.mutable_bookmark()->set_url("url"); 2164 entity_specifics.mutable_bookmark()->set_url("url");
2067 entity_specifics.mutable_bookmark()->set_title("title"); 2165 entity_specifics.mutable_bookmark()->set_title("title");
2068 MakeServerNode(sync_manager_.GetUserShare(), BOOKMARKS, client_tag, 2166 MakeServerNode(sync_manager_.GetUserShare(), BOOKMARKS, client_tag,
2069 syncable::GenerateSyncableHash(BOOKMARKS, 2167 syncable::GenerateSyncableHash(BOOKMARKS,
2070 client_tag), 2168 client_tag),
(...skipping 1155 matching lines...) Expand 10 before | Expand all | Expand 10 after
3226 // SyncManagerInitInvalidStorageTest::GetFactory will return 3324 // SyncManagerInitInvalidStorageTest::GetFactory will return
3227 // DirectoryBackingStore that ensures that SyncManagerImpl::OpenDirectory fails. 3325 // DirectoryBackingStore that ensures that SyncManagerImpl::OpenDirectory fails.
3228 // SyncManagerImpl initialization is done in SyncManagerTest::SetUp. This test's 3326 // SyncManagerImpl initialization is done in SyncManagerTest::SetUp. This test's
3229 // task is to ensure that SyncManagerImpl reported initialization failure in 3327 // task is to ensure that SyncManagerImpl reported initialization failure in
3230 // OnInitializationComplete callback. 3328 // OnInitializationComplete callback.
3231 TEST_F(SyncManagerInitInvalidStorageTest, FailToOpenDatabase) { 3329 TEST_F(SyncManagerInitInvalidStorageTest, FailToOpenDatabase) {
3232 EXPECT_FALSE(initialization_succeeded_); 3330 EXPECT_FALSE(initialization_succeeded_);
3233 } 3331 }
3234 3332
3235 } // namespace syncer 3333 } // namespace syncer
OLDNEW
« no previous file with comments | « sync/internal_api/sync_encryption_handler_impl.cc ('k') | sync/internal_api/syncapi_internal.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698