| 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/util/cryptographer.h" | 5 #include "sync/util/cryptographer.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/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "sync/internal_api/public/base/model_type_test_util.h" | |
| 12 #include "sync/protocol/nigori_specifics.pb.h" | |
| 13 #include "sync/protocol/password_specifics.pb.h" | 11 #include "sync/protocol/password_specifics.pb.h" |
| 14 #include "sync/test/fake_encryptor.h" | 12 #include "sync/test/fake_encryptor.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 15 |
| 18 namespace syncer { | 16 namespace syncer { |
| 19 | 17 |
| 20 namespace { | 18 namespace { |
| 21 | 19 |
| 22 using ::testing::_; | 20 using ::testing::_; |
| 23 using ::testing::Mock; | |
| 24 using ::testing::StrictMock; | |
| 25 | |
| 26 class MockObserver : public Cryptographer::Observer { | |
| 27 public: | |
| 28 MOCK_METHOD2(OnEncryptedTypesChanged, void(ModelTypeSet, bool)); | |
| 29 }; | |
| 30 | 21 |
| 31 } // namespace | 22 } // namespace |
| 32 | 23 |
| 33 class SyncCryptographerTest : public ::testing::Test { | 24 class SyncCryptographerTest : public ::testing::Test { |
| 34 protected: | 25 protected: |
| 35 SyncCryptographerTest() : cryptographer_(&encryptor_) {} | 26 SyncCryptographerTest() : cryptographer_(&encryptor_) {} |
| 36 | 27 |
| 37 FakeEncryptor encryptor_; | 28 FakeEncryptor encryptor_; |
| 38 Cryptographer cryptographer_; | 29 Cryptographer cryptographer_; |
| 39 }; | 30 }; |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 cryptographer_.GetKeystoreKeyBootstrapToken(&token); | 235 cryptographer_.GetKeystoreKeyBootstrapToken(&token); |
| 245 EXPECT_FALSE(token.empty()); | 236 EXPECT_FALSE(token.empty()); |
| 246 | 237 |
| 247 Cryptographer cryptographer2(&encryptor_); | 238 Cryptographer cryptographer2(&encryptor_); |
| 248 EXPECT_FALSE(cryptographer2.HasKeystoreKey()); | 239 EXPECT_FALSE(cryptographer2.HasKeystoreKey()); |
| 249 cryptographer2.BootstrapKeystoreKey(token); | 240 cryptographer2.BootstrapKeystoreKey(token); |
| 250 EXPECT_TRUE(cryptographer2.HasKeystoreKey()); | 241 EXPECT_TRUE(cryptographer2.HasKeystoreKey()); |
| 251 EXPECT_FALSE(cryptographer2.is_initialized()); | 242 EXPECT_FALSE(cryptographer2.is_initialized()); |
| 252 } | 243 } |
| 253 | 244 |
| 254 TEST_F(SyncCryptographerTest, NigoriEncryptionTypes) { | |
| 255 Cryptographer cryptographer2(&encryptor_); | |
| 256 sync_pb::NigoriSpecifics nigori; | |
| 257 | |
| 258 StrictMock<MockObserver> observer; | |
| 259 cryptographer_.AddObserver(&observer); | |
| 260 StrictMock<MockObserver> observer2; | |
| 261 cryptographer2.AddObserver(&observer2); | |
| 262 | |
| 263 // Just set the sensitive types (shouldn't trigger any | |
| 264 // notifications). | |
| 265 ModelTypeSet encrypted_types(Cryptographer::SensitiveTypes()); | |
| 266 cryptographer_.MergeEncryptedTypesForTest(encrypted_types); | |
| 267 cryptographer_.UpdateNigoriFromEncryptedTypes(&nigori); | |
| 268 cryptographer2.UpdateEncryptedTypesFromNigori(nigori); | |
| 269 EXPECT_TRUE(encrypted_types.Equals(cryptographer_.GetEncryptedTypes())); | |
| 270 EXPECT_TRUE(encrypted_types.Equals(cryptographer2.GetEncryptedTypes())); | |
| 271 | |
| 272 Mock::VerifyAndClearExpectations(&observer); | |
| 273 Mock::VerifyAndClearExpectations(&observer2); | |
| 274 | |
| 275 EXPECT_CALL(observer, | |
| 276 OnEncryptedTypesChanged( | |
| 277 HasModelTypes(ModelTypeSet::All()), false)); | |
| 278 EXPECT_CALL(observer2, | |
| 279 OnEncryptedTypesChanged( | |
| 280 HasModelTypes(ModelTypeSet::All()), false)); | |
| 281 | |
| 282 // Set all encrypted types | |
| 283 encrypted_types = ModelTypeSet::All(); | |
| 284 cryptographer_.MergeEncryptedTypesForTest(encrypted_types); | |
| 285 cryptographer_.UpdateNigoriFromEncryptedTypes(&nigori); | |
| 286 cryptographer2.UpdateEncryptedTypesFromNigori(nigori); | |
| 287 EXPECT_TRUE(encrypted_types.Equals(cryptographer_.GetEncryptedTypes())); | |
| 288 EXPECT_TRUE(encrypted_types.Equals(cryptographer2.GetEncryptedTypes())); | |
| 289 | |
| 290 // Receiving an empty nigori should not reset any encrypted types or trigger | |
| 291 // an observer notification. | |
| 292 Mock::VerifyAndClearExpectations(&observer); | |
| 293 nigori = sync_pb::NigoriSpecifics(); | |
| 294 cryptographer_.UpdateEncryptedTypesFromNigori(nigori); | |
| 295 EXPECT_TRUE(encrypted_types.Equals(cryptographer_.GetEncryptedTypes())); | |
| 296 } | |
| 297 | |
| 298 TEST_F(SyncCryptographerTest, EncryptEverythingExplicit) { | |
| 299 ModelTypeSet real_types = ModelTypeSet::All(); | |
| 300 sync_pb::NigoriSpecifics specifics; | |
| 301 specifics.set_encrypt_everything(true); | |
| 302 | |
| 303 StrictMock<MockObserver> observer; | |
| 304 cryptographer_.AddObserver(&observer); | |
| 305 | |
| 306 EXPECT_CALL(observer, | |
| 307 OnEncryptedTypesChanged( | |
| 308 HasModelTypes(ModelTypeSet::All()), true)); | |
| 309 | |
| 310 EXPECT_FALSE(cryptographer_.encrypt_everything()); | |
| 311 ModelTypeSet encrypted_types = cryptographer_.GetEncryptedTypes(); | |
| 312 for (ModelTypeSet::Iterator iter = real_types.First(); | |
| 313 iter.Good(); iter.Inc()) { | |
| 314 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) | |
| 315 EXPECT_TRUE(encrypted_types.Has(iter.Get())); | |
| 316 else | |
| 317 EXPECT_FALSE(encrypted_types.Has(iter.Get())); | |
| 318 } | |
| 319 | |
| 320 cryptographer_.UpdateEncryptedTypesFromNigori(specifics); | |
| 321 | |
| 322 EXPECT_TRUE(cryptographer_.encrypt_everything()); | |
| 323 encrypted_types = cryptographer_.GetEncryptedTypes(); | |
| 324 for (ModelTypeSet::Iterator iter = real_types.First(); | |
| 325 iter.Good(); iter.Inc()) { | |
| 326 EXPECT_TRUE(encrypted_types.Has(iter.Get())); | |
| 327 } | |
| 328 | |
| 329 // Shouldn't trigger another notification. | |
| 330 specifics.set_encrypt_everything(true); | |
| 331 | |
| 332 cryptographer_.RemoveObserver(&observer); | |
| 333 } | |
| 334 | |
| 335 TEST_F(SyncCryptographerTest, EncryptEverythingImplicit) { | |
| 336 ModelTypeSet real_types = ModelTypeSet::All(); | |
| 337 sync_pb::NigoriSpecifics specifics; | |
| 338 specifics.set_encrypt_bookmarks(true); // Non-passwords = encrypt everything | |
| 339 | |
| 340 StrictMock<MockObserver> observer; | |
| 341 cryptographer_.AddObserver(&observer); | |
| 342 | |
| 343 EXPECT_CALL(observer, | |
| 344 OnEncryptedTypesChanged( | |
| 345 HasModelTypes(ModelTypeSet::All()), true)); | |
| 346 | |
| 347 EXPECT_FALSE(cryptographer_.encrypt_everything()); | |
| 348 ModelTypeSet encrypted_types = cryptographer_.GetEncryptedTypes(); | |
| 349 for (ModelTypeSet::Iterator iter = real_types.First(); | |
| 350 iter.Good(); iter.Inc()) { | |
| 351 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) | |
| 352 EXPECT_TRUE(encrypted_types.Has(iter.Get())); | |
| 353 else | |
| 354 EXPECT_FALSE(encrypted_types.Has(iter.Get())); | |
| 355 } | |
| 356 | |
| 357 cryptographer_.UpdateEncryptedTypesFromNigori(specifics); | |
| 358 | |
| 359 EXPECT_TRUE(cryptographer_.encrypt_everything()); | |
| 360 encrypted_types = cryptographer_.GetEncryptedTypes(); | |
| 361 for (ModelTypeSet::Iterator iter = real_types.First(); | |
| 362 iter.Good(); iter.Inc()) { | |
| 363 EXPECT_TRUE(encrypted_types.Has(iter.Get())); | |
| 364 } | |
| 365 | |
| 366 // Shouldn't trigger another notification. | |
| 367 specifics.set_encrypt_everything(true); | |
| 368 | |
| 369 cryptographer_.RemoveObserver(&observer); | |
| 370 } | |
| 371 | |
| 372 TEST_F(SyncCryptographerTest, UnknownSensitiveTypes) { | |
| 373 ModelTypeSet real_types = ModelTypeSet::All(); | |
| 374 sync_pb::NigoriSpecifics specifics; | |
| 375 // Explicitly setting encrypt everything should override logic for implicit | |
| 376 // encrypt everything. | |
| 377 specifics.set_encrypt_everything(false); | |
| 378 specifics.set_encrypt_bookmarks(true); | |
| 379 | |
| 380 StrictMock<MockObserver> observer; | |
| 381 cryptographer_.AddObserver(&observer); | |
| 382 | |
| 383 ModelTypeSet expected_encrypted_types = Cryptographer::SensitiveTypes(); | |
| 384 expected_encrypted_types.Put(BOOKMARKS); | |
| 385 | |
| 386 EXPECT_CALL(observer, | |
| 387 OnEncryptedTypesChanged( | |
| 388 HasModelTypes(expected_encrypted_types), false)); | |
| 389 | |
| 390 EXPECT_FALSE(cryptographer_.encrypt_everything()); | |
| 391 ModelTypeSet encrypted_types = cryptographer_.GetEncryptedTypes(); | |
| 392 for (ModelTypeSet::Iterator iter = real_types.First(); | |
| 393 iter.Good(); iter.Inc()) { | |
| 394 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) | |
| 395 EXPECT_TRUE(encrypted_types.Has(iter.Get())); | |
| 396 else | |
| 397 EXPECT_FALSE(encrypted_types.Has(iter.Get())); | |
| 398 } | |
| 399 | |
| 400 cryptographer_.UpdateEncryptedTypesFromNigori(specifics); | |
| 401 | |
| 402 EXPECT_FALSE(cryptographer_.encrypt_everything()); | |
| 403 encrypted_types = cryptographer_.GetEncryptedTypes(); | |
| 404 for (ModelTypeSet::Iterator iter = real_types.First(); | |
| 405 iter.Good(); iter.Inc()) { | |
| 406 if (iter.Get() == PASSWORDS || | |
| 407 iter.Get() == NIGORI || | |
| 408 iter.Get() == BOOKMARKS) | |
| 409 EXPECT_TRUE(encrypted_types.Has(iter.Get())); | |
| 410 else | |
| 411 EXPECT_FALSE(encrypted_types.Has(iter.Get())); | |
| 412 } | |
| 413 | |
| 414 cryptographer_.RemoveObserver(&observer); | |
| 415 } | |
| 416 | |
| 417 } // namespace syncer | 245 } // namespace syncer |
| OLD | NEW |