| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "sync/internal_api/sync_encryption_handler_impl.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop.h" |
| 11 #include "base/tracked_objects.h" |
| 12 #include "sync/internal_api/public/base/model_type_test_util.h" |
| 13 #include "sync/internal_api/public/read_node.h" |
| 14 #include "sync/internal_api/public/read_transaction.h" |
| 15 #include "sync/internal_api/public/write_transaction.h" |
| 16 #include "sync/internal_api/public/test/test_user_share.h" |
| 17 #include "sync/protocol/nigori_specifics.pb.h" |
| 18 #include "sync/protocol/sync.pb.h" |
| 19 #include "sync/syncable/entry.h" |
| 20 #include "sync/syncable/mutable_entry.h" |
| 21 #include "sync/syncable/write_transaction.h" |
| 22 #include "sync/test/engine/test_id_factory.h" |
| 23 #include "sync/util/cryptographer.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 |
| 27 namespace syncer { |
| 28 |
| 29 namespace { |
| 30 |
| 31 using ::testing::_; |
| 32 using ::testing::Mock; |
| 33 using ::testing::StrictMock; |
| 34 |
| 35 class SyncEncryptionHandlerObserverMock |
| 36 : public SyncEncryptionHandler::Observer { |
| 37 public: |
| 38 MOCK_METHOD2(OnPassphraseRequired, |
| 39 void(PassphraseRequiredReason, |
| 40 const sync_pb::EncryptedData&)); // NOLINT |
| 41 MOCK_METHOD0(OnPassphraseAccepted, void()); // NOLINT |
| 42 MOCK_METHOD1(OnBootstrapTokenUpdated, void(const std::string&)); // NOLINT |
| 43 MOCK_METHOD2(OnEncryptedTypesChanged, |
| 44 void(ModelTypeSet, bool)); // NOLINT |
| 45 MOCK_METHOD0(OnEncryptionComplete, void()); // NOLINT |
| 46 MOCK_METHOD1(OnCryptographerStateChanged, void(Cryptographer*)); // NOLINT |
| 47 }; |
| 48 |
| 49 } // namespace |
| 50 |
| 51 class SyncEncryptionHandlerImplTest : public ::testing::Test { |
| 52 public: |
| 53 SyncEncryptionHandlerImplTest() : cryptographer_(NULL) {} |
| 54 virtual ~SyncEncryptionHandlerImplTest() {} |
| 55 |
| 56 virtual void SetUp() { |
| 57 test_user_share_.SetUp(); |
| 58 SetUpEncryption(); |
| 59 encryption_handler_->AddObserver(&observer_); |
| 60 CreateRootForType(NIGORI); |
| 61 } |
| 62 |
| 63 virtual void TearDown() { |
| 64 test_user_share_.TearDown(); |
| 65 } |
| 66 |
| 67 protected: |
| 68 void SetUpEncryption() { |
| 69 ReadTransaction trans(FROM_HERE, user_share()); |
| 70 cryptographer_ = trans.GetCryptographer(); |
| 71 encryption_handler_.reset( |
| 72 new SyncEncryptionHandlerImpl(user_share(), |
| 73 cryptographer_)); |
| 74 cryptographer_->SetSyncEncryptionHandlerDelegate( |
| 75 encryption_handler_.get()); |
| 76 |
| 77 } |
| 78 |
| 79 void CreateRootForType(ModelType model_type) { |
| 80 syncer::syncable::Directory* directory = user_share()->directory.get(); |
| 81 |
| 82 std::string tag_name = ModelTypeToRootTag(model_type); |
| 83 |
| 84 syncable::WriteTransaction wtrans(FROM_HERE, syncable::UNITTEST, directory); |
| 85 syncable::MutableEntry node(&wtrans, |
| 86 syncable::CREATE, |
| 87 wtrans.root_id(), |
| 88 tag_name); |
| 89 node.Put(syncable::UNIQUE_SERVER_TAG, tag_name); |
| 90 node.Put(syncable::IS_DIR, true); |
| 91 node.Put(syncable::SERVER_IS_DIR, false); |
| 92 node.Put(syncable::IS_UNSYNCED, false); |
| 93 node.Put(syncable::IS_UNAPPLIED_UPDATE, false); |
| 94 node.Put(syncable::SERVER_VERSION, 20); |
| 95 node.Put(syncable::BASE_VERSION, 20); |
| 96 node.Put(syncable::IS_DEL, false); |
| 97 node.Put(syncable::ID, ids_.MakeServer(tag_name)); |
| 98 sync_pb::EntitySpecifics specifics; |
| 99 syncer::AddDefaultFieldValue(model_type, &specifics); |
| 100 node.Put(syncable::SPECIFICS, specifics); |
| 101 } |
| 102 |
| 103 void PumpLoop() { |
| 104 message_loop_.RunAllPending(); |
| 105 } |
| 106 |
| 107 // Getters for tests. |
| 108 UserShare* user_share() { return test_user_share_.user_share(); } |
| 109 SyncEncryptionHandlerImpl* encryption_handler() { |
| 110 return encryption_handler_.get(); |
| 111 } |
| 112 SyncEncryptionHandlerObserverMock* observer() { return &observer_; } |
| 113 Cryptographer* cryptographer() { return cryptographer_; } |
| 114 |
| 115 private: |
| 116 TestUserShare test_user_share_; |
| 117 scoped_ptr<SyncEncryptionHandlerImpl> encryption_handler_; |
| 118 StrictMock<SyncEncryptionHandlerObserverMock> observer_; |
| 119 Cryptographer* cryptographer_; |
| 120 TestIdFactory ids_; |
| 121 MessageLoop message_loop_; |
| 122 }; |
| 123 |
| 124 TEST_F(SyncEncryptionHandlerImplTest, NigoriEncryptionTypes) { |
| 125 sync_pb::NigoriSpecifics nigori; |
| 126 |
| 127 StrictMock<SyncEncryptionHandlerObserverMock> observer2; |
| 128 SyncEncryptionHandlerImpl handler2(user_share(), |
| 129 cryptographer()); |
| 130 handler2.AddObserver(&observer2); |
| 131 |
| 132 // Just set the sensitive types (shouldn't trigger any |
| 133 // notifications). |
| 134 ModelTypeSet encrypted_types(SyncEncryptionHandler::SensitiveTypes()); |
| 135 encryption_handler()->MergeEncryptedTypes(encrypted_types); |
| 136 encryption_handler()->UpdateNigoriFromEncryptedTypes(&nigori); |
| 137 handler2.UpdateEncryptedTypesFromNigori(nigori); |
| 138 EXPECT_TRUE(encrypted_types.Equals( |
| 139 encryption_handler()->GetEncryptedTypes())); |
| 140 EXPECT_TRUE(encrypted_types.Equals( |
| 141 handler2.GetEncryptedTypes())); |
| 142 |
| 143 Mock::VerifyAndClearExpectations(observer()); |
| 144 Mock::VerifyAndClearExpectations(&observer2); |
| 145 |
| 146 EXPECT_CALL(*observer(), |
| 147 OnEncryptedTypesChanged( |
| 148 HasModelTypes(ModelTypeSet::All()), false)); |
| 149 EXPECT_CALL(observer2, |
| 150 OnEncryptedTypesChanged( |
| 151 HasModelTypes(ModelTypeSet::All()), false)); |
| 152 |
| 153 // Set all encrypted types |
| 154 encrypted_types = ModelTypeSet::All(); |
| 155 encryption_handler()->MergeEncryptedTypes(encrypted_types); |
| 156 encryption_handler()->UpdateNigoriFromEncryptedTypes(&nigori); |
| 157 handler2.UpdateEncryptedTypesFromNigori(nigori); |
| 158 EXPECT_TRUE(encrypted_types.Equals( |
| 159 encryption_handler()->GetEncryptedTypes())); |
| 160 EXPECT_TRUE(encrypted_types.Equals(handler2.GetEncryptedTypes())); |
| 161 |
| 162 // Receiving an empty nigori should not reset any encrypted types or trigger |
| 163 // an observer notification. |
| 164 Mock::VerifyAndClearExpectations(observer()); |
| 165 Mock::VerifyAndClearExpectations(&observer2); |
| 166 nigori = sync_pb::NigoriSpecifics(); |
| 167 encryption_handler()->UpdateEncryptedTypesFromNigori(nigori); |
| 168 EXPECT_TRUE(encrypted_types.Equals( |
| 169 encryption_handler()->GetEncryptedTypes())); |
| 170 } |
| 171 |
| 172 TEST_F(SyncEncryptionHandlerImplTest, EncryptEverythingExplicit) { |
| 173 ModelTypeSet real_types = ModelTypeSet::All(); |
| 174 sync_pb::NigoriSpecifics specifics; |
| 175 specifics.set_encrypt_everything(true); |
| 176 |
| 177 EXPECT_CALL(*observer(), |
| 178 OnEncryptedTypesChanged( |
| 179 HasModelTypes(ModelTypeSet::All()), true)); |
| 180 |
| 181 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); |
| 182 ModelTypeSet encrypted_types = encryption_handler()->GetEncryptedTypes(); |
| 183 for (ModelTypeSet::Iterator iter = real_types.First(); |
| 184 iter.Good(); iter.Inc()) { |
| 185 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) |
| 186 EXPECT_TRUE(encrypted_types.Has(iter.Get())); |
| 187 else |
| 188 EXPECT_FALSE(encrypted_types.Has(iter.Get())); |
| 189 } |
| 190 |
| 191 encryption_handler()->UpdateEncryptedTypesFromNigori(specifics); |
| 192 |
| 193 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); |
| 194 encrypted_types = encryption_handler()->GetEncryptedTypes(); |
| 195 for (ModelTypeSet::Iterator iter = real_types.First(); |
| 196 iter.Good(); iter.Inc()) { |
| 197 EXPECT_TRUE(encrypted_types.Has(iter.Get())); |
| 198 } |
| 199 |
| 200 // Shouldn't trigger another notification. |
| 201 Mock::VerifyAndClearExpectations(observer()); |
| 202 specifics.set_encrypt_everything(true); |
| 203 encryption_handler()->UpdateEncryptedTypesFromNigori(specifics); |
| 204 } |
| 205 |
| 206 TEST_F(SyncEncryptionHandlerImplTest, EncryptEverythingImplicit) { |
| 207 ModelTypeSet real_types = ModelTypeSet::All(); |
| 208 sync_pb::NigoriSpecifics specifics; |
| 209 specifics.set_encrypt_bookmarks(true); // Non-passwords = encrypt everything |
| 210 |
| 211 EXPECT_CALL(*observer(), |
| 212 OnEncryptedTypesChanged( |
| 213 HasModelTypes(ModelTypeSet::All()), true)); |
| 214 |
| 215 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); |
| 216 ModelTypeSet encrypted_types = encryption_handler()->GetEncryptedTypes(); |
| 217 for (ModelTypeSet::Iterator iter = real_types.First(); |
| 218 iter.Good(); iter.Inc()) { |
| 219 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) |
| 220 EXPECT_TRUE(encrypted_types.Has(iter.Get())); |
| 221 else |
| 222 EXPECT_FALSE(encrypted_types.Has(iter.Get())); |
| 223 } |
| 224 |
| 225 encryption_handler()->UpdateEncryptedTypesFromNigori(specifics); |
| 226 |
| 227 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); |
| 228 encrypted_types = encryption_handler()->GetEncryptedTypes(); |
| 229 for (ModelTypeSet::Iterator iter = real_types.First(); |
| 230 iter.Good(); iter.Inc()) { |
| 231 EXPECT_TRUE(encrypted_types.Has(iter.Get())); |
| 232 } |
| 233 |
| 234 // Shouldn't trigger another notification. |
| 235 Mock::VerifyAndClearExpectations(observer()); |
| 236 specifics.set_encrypt_everything(true); |
| 237 encryption_handler()->UpdateEncryptedTypesFromNigori(specifics); |
| 238 } |
| 239 |
| 240 TEST_F(SyncEncryptionHandlerImplTest, UnknownSensitiveTypes) { |
| 241 ModelTypeSet real_types = ModelTypeSet::All(); |
| 242 sync_pb::NigoriSpecifics specifics; |
| 243 // Explicitly setting encrypt everything should override logic for implicit |
| 244 // encrypt everything. |
| 245 specifics.set_encrypt_everything(false); |
| 246 specifics.set_encrypt_bookmarks(true); |
| 247 |
| 248 ModelTypeSet expected_encrypted_types = |
| 249 SyncEncryptionHandler::SensitiveTypes(); |
| 250 expected_encrypted_types.Put(BOOKMARKS); |
| 251 |
| 252 EXPECT_CALL(*observer(), |
| 253 OnEncryptedTypesChanged( |
| 254 HasModelTypes(expected_encrypted_types), false)); |
| 255 |
| 256 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); |
| 257 ModelTypeSet encrypted_types = encryption_handler()->GetEncryptedTypes(); |
| 258 for (ModelTypeSet::Iterator iter = real_types.First(); |
| 259 iter.Good(); iter.Inc()) { |
| 260 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) |
| 261 EXPECT_TRUE(encrypted_types.Has(iter.Get())); |
| 262 else |
| 263 EXPECT_FALSE(encrypted_types.Has(iter.Get())); |
| 264 } |
| 265 |
| 266 encryption_handler()->UpdateEncryptedTypesFromNigori(specifics); |
| 267 |
| 268 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); |
| 269 encrypted_types = encryption_handler()->GetEncryptedTypes(); |
| 270 for (ModelTypeSet::Iterator iter = real_types.First(); |
| 271 iter.Good(); iter.Inc()) { |
| 272 if (iter.Get() == PASSWORDS || |
| 273 iter.Get() == NIGORI || |
| 274 iter.Get() == BOOKMARKS) |
| 275 EXPECT_TRUE(encrypted_types.Has(iter.Get())); |
| 276 else |
| 277 EXPECT_FALSE(encrypted_types.Has(iter.Get())); |
| 278 } |
| 279 } |
| 280 |
| 281 // Receive an old nigori with old encryption keys and encrypted types. We should |
| 282 // not revert our default key or encrypted types, and should post a task to |
| 283 // overwrite the existing nigori with the correct data. |
| 284 TEST_F(SyncEncryptionHandlerImplTest, ReceiveOldNigori) { |
| 285 KeyParams old_key = {"localhost", "dummy", "old"}; |
| 286 KeyParams current_key = {"localhost", "dummy", "cur"}; |
| 287 |
| 288 // Data for testing encryption/decryption. |
| 289 Cryptographer other_cryptographer(cryptographer()->encryptor()); |
| 290 other_cryptographer.AddKey(old_key); |
| 291 sync_pb::EntitySpecifics other_encrypted_specifics; |
| 292 other_encrypted_specifics.mutable_bookmark()->set_title("title"); |
| 293 other_cryptographer.Encrypt( |
| 294 other_encrypted_specifics, |
| 295 other_encrypted_specifics.mutable_encrypted()); |
| 296 sync_pb::EntitySpecifics our_encrypted_specifics; |
| 297 our_encrypted_specifics.mutable_bookmark()->set_title("title2"); |
| 298 ModelTypeSet encrypted_types = ModelTypeSet::All(); |
| 299 |
| 300 // Set up the current encryption state (containing both keys and encrypt |
| 301 // everything). |
| 302 sync_pb::NigoriSpecifics current_nigori_specifics; |
| 303 cryptographer()->AddKey(old_key); |
| 304 cryptographer()->AddKey(current_key); |
| 305 cryptographer()->Encrypt( |
| 306 our_encrypted_specifics, |
| 307 our_encrypted_specifics.mutable_encrypted()); |
| 308 cryptographer()->GetKeys( |
| 309 current_nigori_specifics.mutable_encrypted()); |
| 310 current_nigori_specifics.set_encrypt_everything(true); |
| 311 |
| 312 EXPECT_CALL(*observer(), OnCryptographerStateChanged(_)); |
| 313 EXPECT_CALL(*observer(), OnEncryptedTypesChanged( |
| 314 HasModelTypes(ModelTypeSet::All()), true)); |
| 315 { |
| 316 // Update the encryption handler. |
| 317 WriteTransaction trans(FROM_HERE, user_share()); |
| 318 encryption_handler()->UpdateFromNigori(current_nigori_specifics); |
| 319 } |
| 320 Mock::VerifyAndClearExpectations(observer()); |
| 321 |
| 322 // Now set up the old nigori specifics and apply it on top. |
| 323 // Has an old set of keys, and no encrypted types. |
| 324 sync_pb::NigoriSpecifics old_nigori; |
| 325 other_cryptographer.GetKeys(old_nigori.mutable_encrypted()); |
| 326 |
| 327 EXPECT_CALL(*observer(), OnCryptographerStateChanged(_)); |
| 328 { |
| 329 // Update the encryption handler. |
| 330 WriteTransaction trans(FROM_HERE, user_share()); |
| 331 encryption_handler()->UpdateFromNigori(old_nigori); |
| 332 } |
| 333 |
| 334 // Encryption handler should have posted a task to overwrite the old |
| 335 // specifics. |
| 336 PumpLoop(); |
| 337 |
| 338 { |
| 339 // The cryptographer should be able to decrypt both sets of keys and still |
| 340 // be encrypting with the newest, and the encrypted types should be the |
| 341 // most recent. |
| 342 // In addition, the nigori node should match the current encryption state. |
| 343 ReadTransaction trans(FROM_HERE, user_share()); |
| 344 ReadNode nigori_node(&trans); |
| 345 ASSERT_EQ(nigori_node.InitByTagLookup(ModelTypeToRootTag(NIGORI)), |
| 346 BaseNode::INIT_OK); |
| 347 const sync_pb::NigoriSpecifics& nigori = nigori_node.GetNigoriSpecifics(); |
| 348 EXPECT_TRUE(cryptographer()->CanDecryptUsingDefaultKey( |
| 349 our_encrypted_specifics.encrypted())); |
| 350 EXPECT_TRUE(cryptographer()->CanDecrypt( |
| 351 other_encrypted_specifics.encrypted())); |
| 352 EXPECT_TRUE(cryptographer()->CanDecrypt(nigori.encrypted())); |
| 353 EXPECT_TRUE(nigori.encrypt_everything()); |
| 354 EXPECT_TRUE(cryptographer()->CanDecryptUsingDefaultKey(nigori.encrypted())); |
| 355 } |
| 356 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); |
| 357 } |
| 358 |
| 359 } // namespace syncer |
| OLD | NEW |