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 CreateRootForType(NIGORI); |
| 60 } |
| 61 |
| 62 virtual void TearDown() { |
| 63 test_user_share_.TearDown(); |
| 64 } |
| 65 |
| 66 protected: |
| 67 void SetUpEncryption() { |
| 68 ReadTransaction trans(FROM_HERE, user_share()); |
| 69 cryptographer_ = trans.GetCryptographer(); |
| 70 encryption_handler_.reset( |
| 71 new SyncEncryptionHandlerImpl(user_share(), |
| 72 cryptographer_)); |
| 73 cryptographer_->SetSyncEncryptionHandlerDelegate( |
| 74 encryption_handler_.get()); |
| 75 encryption_handler_->AddObserver(&observer_); |
| 76 } |
| 77 |
| 78 void CreateRootForType(ModelType model_type) { |
| 79 syncer::syncable::Directory* directory = user_share()->directory.get(); |
| 80 |
| 81 std::string tag_name = ModelTypeToRootTag(model_type); |
| 82 |
| 83 syncable::WriteTransaction wtrans(FROM_HERE, syncable::UNITTEST, directory); |
| 84 syncable::MutableEntry node(&wtrans, |
| 85 syncable::CREATE, |
| 86 wtrans.root_id(), |
| 87 tag_name); |
| 88 node.Put(syncable::UNIQUE_SERVER_TAG, tag_name); |
| 89 node.Put(syncable::IS_DIR, true); |
| 90 node.Put(syncable::SERVER_IS_DIR, false); |
| 91 node.Put(syncable::IS_UNSYNCED, false); |
| 92 node.Put(syncable::IS_UNAPPLIED_UPDATE, false); |
| 93 node.Put(syncable::SERVER_VERSION, 20); |
| 94 node.Put(syncable::BASE_VERSION, 20); |
| 95 node.Put(syncable::IS_DEL, false); |
| 96 node.Put(syncable::ID, ids_.MakeServer(tag_name)); |
| 97 sync_pb::EntitySpecifics specifics; |
| 98 syncer::AddDefaultFieldValue(model_type, &specifics); |
| 99 node.Put(syncable::SPECIFICS, specifics); |
| 100 } |
| 101 |
| 102 void PumpLoop() { |
| 103 message_loop_.RunAllPending(); |
| 104 } |
| 105 |
| 106 // Getters for tests. |
| 107 UserShare* user_share() { return test_user_share_.user_share(); } |
| 108 SyncEncryptionHandlerImpl* encryption_handler() { |
| 109 return encryption_handler_.get(); |
| 110 } |
| 111 SyncEncryptionHandlerObserverMock* observer() { return &observer_; } |
| 112 Cryptographer* cryptographer() { return cryptographer_; } |
| 113 |
| 114 private: |
| 115 TestUserShare test_user_share_; |
| 116 scoped_ptr<SyncEncryptionHandlerImpl> encryption_handler_; |
| 117 StrictMock<SyncEncryptionHandlerObserverMock> observer_; |
| 118 Cryptographer* cryptographer_; |
| 119 TestIdFactory ids_; |
| 120 MessageLoop message_loop_; |
| 121 }; |
| 122 |
| 123 // Verify that the encrypted types are being written to and read from the |
| 124 // nigori node properly. |
| 125 TEST_F(SyncEncryptionHandlerImplTest, NigoriEncryptionTypes) { |
| 126 sync_pb::NigoriSpecifics nigori; |
| 127 |
| 128 StrictMock<SyncEncryptionHandlerObserverMock> observer2; |
| 129 SyncEncryptionHandlerImpl handler2(user_share(), |
| 130 cryptographer()); |
| 131 handler2.AddObserver(&observer2); |
| 132 |
| 133 // Just set the sensitive types (shouldn't trigger any 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 // Verify the encryption handler processes the encrypt everything field |
| 173 // properly. |
| 174 TEST_F(SyncEncryptionHandlerImplTest, EncryptEverythingExplicit) { |
| 175 ModelTypeSet real_types = ModelTypeSet::All(); |
| 176 sync_pb::NigoriSpecifics specifics; |
| 177 specifics.set_encrypt_everything(true); |
| 178 |
| 179 EXPECT_CALL(*observer(), |
| 180 OnEncryptedTypesChanged( |
| 181 HasModelTypes(ModelTypeSet::All()), true)); |
| 182 |
| 183 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); |
| 184 ModelTypeSet encrypted_types = encryption_handler()->GetEncryptedTypes(); |
| 185 for (ModelTypeSet::Iterator iter = real_types.First(); |
| 186 iter.Good(); iter.Inc()) { |
| 187 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) |
| 188 EXPECT_TRUE(encrypted_types.Has(iter.Get())); |
| 189 else |
| 190 EXPECT_FALSE(encrypted_types.Has(iter.Get())); |
| 191 } |
| 192 |
| 193 encryption_handler()->UpdateEncryptedTypesFromNigori(specifics); |
| 194 |
| 195 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); |
| 196 encrypted_types = encryption_handler()->GetEncryptedTypes(); |
| 197 for (ModelTypeSet::Iterator iter = real_types.First(); |
| 198 iter.Good(); iter.Inc()) { |
| 199 EXPECT_TRUE(encrypted_types.Has(iter.Get())); |
| 200 } |
| 201 |
| 202 // Receiving the nigori node again shouldn't trigger another notification. |
| 203 Mock::VerifyAndClearExpectations(observer()); |
| 204 encryption_handler()->UpdateEncryptedTypesFromNigori(specifics); |
| 205 } |
| 206 |
| 207 // Verify the encryption handler can detect an implicit encrypt everything state |
| 208 // (from clients that failed to write the encrypt everything field). |
| 209 TEST_F(SyncEncryptionHandlerImplTest, EncryptEverythingImplicit) { |
| 210 ModelTypeSet real_types = ModelTypeSet::All(); |
| 211 sync_pb::NigoriSpecifics specifics; |
| 212 specifics.set_encrypt_bookmarks(true); // Non-passwords = encrypt everything |
| 213 |
| 214 EXPECT_CALL(*observer(), |
| 215 OnEncryptedTypesChanged( |
| 216 HasModelTypes(ModelTypeSet::All()), true)); |
| 217 |
| 218 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); |
| 219 ModelTypeSet encrypted_types = encryption_handler()->GetEncryptedTypes(); |
| 220 for (ModelTypeSet::Iterator iter = real_types.First(); |
| 221 iter.Good(); iter.Inc()) { |
| 222 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) |
| 223 EXPECT_TRUE(encrypted_types.Has(iter.Get())); |
| 224 else |
| 225 EXPECT_FALSE(encrypted_types.Has(iter.Get())); |
| 226 } |
| 227 |
| 228 encryption_handler()->UpdateEncryptedTypesFromNigori(specifics); |
| 229 |
| 230 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); |
| 231 encrypted_types = encryption_handler()->GetEncryptedTypes(); |
| 232 for (ModelTypeSet::Iterator iter = real_types.First(); |
| 233 iter.Good(); iter.Inc()) { |
| 234 EXPECT_TRUE(encrypted_types.Has(iter.Get())); |
| 235 } |
| 236 |
| 237 // Receiving a nigori node with encrypt everything explicitly set shouldn't |
| 238 // trigger another notification. |
| 239 Mock::VerifyAndClearExpectations(observer()); |
| 240 specifics.set_encrypt_everything(true); |
| 241 encryption_handler()->UpdateEncryptedTypesFromNigori(specifics); |
| 242 } |
| 243 |
| 244 // Verify the encryption handler can deal with new versions treating new types |
| 245 // as Sensitive, and that it does not consider this an implicit encrypt |
| 246 // everything case. |
| 247 TEST_F(SyncEncryptionHandlerImplTest, UnknownSensitiveTypes) { |
| 248 ModelTypeSet real_types = ModelTypeSet::All(); |
| 249 sync_pb::NigoriSpecifics specifics; |
| 250 specifics.set_encrypt_everything(false); |
| 251 specifics.set_encrypt_bookmarks(true); |
| 252 |
| 253 ModelTypeSet expected_encrypted_types = |
| 254 SyncEncryptionHandler::SensitiveTypes(); |
| 255 expected_encrypted_types.Put(BOOKMARKS); |
| 256 |
| 257 EXPECT_CALL(*observer(), |
| 258 OnEncryptedTypesChanged( |
| 259 HasModelTypes(expected_encrypted_types), false)); |
| 260 |
| 261 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); |
| 262 ModelTypeSet encrypted_types = encryption_handler()->GetEncryptedTypes(); |
| 263 for (ModelTypeSet::Iterator iter = real_types.First(); |
| 264 iter.Good(); iter.Inc()) { |
| 265 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) |
| 266 EXPECT_TRUE(encrypted_types.Has(iter.Get())); |
| 267 else |
| 268 EXPECT_FALSE(encrypted_types.Has(iter.Get())); |
| 269 } |
| 270 |
| 271 encryption_handler()->UpdateEncryptedTypesFromNigori(specifics); |
| 272 |
| 273 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); |
| 274 encrypted_types = encryption_handler()->GetEncryptedTypes(); |
| 275 for (ModelTypeSet::Iterator iter = real_types.First(); |
| 276 iter.Good(); iter.Inc()) { |
| 277 if (iter.Get() == PASSWORDS || |
| 278 iter.Get() == NIGORI || |
| 279 iter.Get() == BOOKMARKS) |
| 280 EXPECT_TRUE(encrypted_types.Has(iter.Get())); |
| 281 else |
| 282 EXPECT_FALSE(encrypted_types.Has(iter.Get())); |
| 283 } |
| 284 } |
| 285 |
| 286 // Receive an old nigori with old encryption keys and encrypted types. We should |
| 287 // not revert our default key or encrypted types, and should post a task to |
| 288 // overwrite the existing nigori with the correct data. |
| 289 TEST_F(SyncEncryptionHandlerImplTest, ReceiveOldNigori) { |
| 290 KeyParams old_key = {"localhost", "dummy", "old"}; |
| 291 KeyParams current_key = {"localhost", "dummy", "cur"}; |
| 292 |
| 293 // Data for testing encryption/decryption. |
| 294 Cryptographer other_cryptographer(cryptographer()->encryptor()); |
| 295 other_cryptographer.AddKey(old_key); |
| 296 sync_pb::EntitySpecifics other_encrypted_specifics; |
| 297 other_encrypted_specifics.mutable_bookmark()->set_title("title"); |
| 298 other_cryptographer.Encrypt( |
| 299 other_encrypted_specifics, |
| 300 other_encrypted_specifics.mutable_encrypted()); |
| 301 sync_pb::EntitySpecifics our_encrypted_specifics; |
| 302 our_encrypted_specifics.mutable_bookmark()->set_title("title2"); |
| 303 ModelTypeSet encrypted_types = ModelTypeSet::All(); |
| 304 |
| 305 // Set up the current encryption state (containing both keys and encrypt |
| 306 // everything). |
| 307 sync_pb::NigoriSpecifics current_nigori_specifics; |
| 308 cryptographer()->AddKey(old_key); |
| 309 cryptographer()->AddKey(current_key); |
| 310 cryptographer()->Encrypt( |
| 311 our_encrypted_specifics, |
| 312 our_encrypted_specifics.mutable_encrypted()); |
| 313 cryptographer()->GetKeys( |
| 314 current_nigori_specifics.mutable_encrypted()); |
| 315 current_nigori_specifics.set_encrypt_everything(true); |
| 316 |
| 317 EXPECT_CALL(*observer(), OnCryptographerStateChanged(_)); |
| 318 EXPECT_CALL(*observer(), OnEncryptedTypesChanged( |
| 319 HasModelTypes(ModelTypeSet::All()), true)); |
| 320 { |
| 321 // Update the encryption handler. |
| 322 WriteTransaction trans(FROM_HERE, user_share()); |
| 323 encryption_handler()->UpdateFromNigori(current_nigori_specifics); |
| 324 } |
| 325 Mock::VerifyAndClearExpectations(observer()); |
| 326 |
| 327 // Now set up the old nigori specifics and apply it on top. |
| 328 // Has an old set of keys, and no encrypted types. |
| 329 sync_pb::NigoriSpecifics old_nigori; |
| 330 other_cryptographer.GetKeys(old_nigori.mutable_encrypted()); |
| 331 |
| 332 EXPECT_CALL(*observer(), OnCryptographerStateChanged(_)); |
| 333 { |
| 334 // Update the encryption handler. |
| 335 WriteTransaction trans(FROM_HERE, user_share()); |
| 336 encryption_handler()->UpdateFromNigori(old_nigori); |
| 337 } |
| 338 EXPECT_TRUE(cryptographer()->is_ready()); |
| 339 EXPECT_FALSE(cryptographer()->has_pending_keys()); |
| 340 |
| 341 // Encryption handler should have posted a task to overwrite the old |
| 342 // specifics. |
| 343 PumpLoop(); |
| 344 |
| 345 { |
| 346 // The cryptographer should be able to decrypt both sets of keys and still |
| 347 // be encrypting with the newest, and the encrypted types should be the |
| 348 // most recent. |
| 349 // In addition, the nigori node should match the current encryption state. |
| 350 ReadTransaction trans(FROM_HERE, user_share()); |
| 351 ReadNode nigori_node(&trans); |
| 352 ASSERT_EQ(nigori_node.InitByTagLookup(ModelTypeToRootTag(NIGORI)), |
| 353 BaseNode::INIT_OK); |
| 354 const sync_pb::NigoriSpecifics& nigori = nigori_node.GetNigoriSpecifics(); |
| 355 EXPECT_TRUE(cryptographer()->CanDecryptUsingDefaultKey( |
| 356 our_encrypted_specifics.encrypted())); |
| 357 EXPECT_TRUE(cryptographer()->CanDecrypt( |
| 358 other_encrypted_specifics.encrypted())); |
| 359 EXPECT_TRUE(cryptographer()->CanDecrypt(nigori.encrypted())); |
| 360 EXPECT_TRUE(nigori.encrypt_everything()); |
| 361 EXPECT_TRUE(cryptographer()->CanDecryptUsingDefaultKey(nigori.encrypted())); |
| 362 } |
| 363 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); |
| 364 } |
| 365 |
| 366 } // namespace syncer |
OLD | NEW |