| 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 "components/sync/core_impl/js_sync_encryption_handler_observer.h" | |
| 6 | |
| 7 #include "base/location.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "base/values.h" | |
| 10 #include "components/sync/base/cryptographer.h" | |
| 11 #include "components/sync/base/fake_encryptor.h" | |
| 12 #include "components/sync/base/model_type.h" | |
| 13 #include "components/sync/base/passphrase_type.h" | |
| 14 #include "components/sync/base/time.h" | |
| 15 #include "components/sync/engine/sync_string_conversions.h" | |
| 16 #include "components/sync/js/js_event_details.h" | |
| 17 #include "components/sync/js/js_test_util.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 namespace syncer { | |
| 21 namespace { | |
| 22 | |
| 23 using ::testing::InSequence; | |
| 24 using ::testing::StrictMock; | |
| 25 | |
| 26 class JsSyncEncryptionHandlerObserverTest : public testing::Test { | |
| 27 protected: | |
| 28 JsSyncEncryptionHandlerObserverTest() { | |
| 29 js_sync_encryption_handler_observer_.SetJsEventHandler( | |
| 30 mock_js_event_handler_.AsWeakHandle()); | |
| 31 } | |
| 32 | |
| 33 private: | |
| 34 // This must be destroyed after the member variables below in order | |
| 35 // for WeakHandles to be destroyed properly. | |
| 36 base::MessageLoop message_loop_; | |
| 37 | |
| 38 protected: | |
| 39 StrictMock<MockJsEventHandler> mock_js_event_handler_; | |
| 40 JsSyncEncryptionHandlerObserver js_sync_encryption_handler_observer_; | |
| 41 | |
| 42 void PumpLoop() { base::RunLoop().RunUntilIdle(); } | |
| 43 }; | |
| 44 | |
| 45 TEST_F(JsSyncEncryptionHandlerObserverTest, NoArgNotifiations) { | |
| 46 InSequence dummy; | |
| 47 | |
| 48 EXPECT_CALL( | |
| 49 mock_js_event_handler_, | |
| 50 HandleJsEvent("onEncryptionComplete", HasDetails(JsEventDetails()))); | |
| 51 | |
| 52 js_sync_encryption_handler_observer_.OnEncryptionComplete(); | |
| 53 PumpLoop(); | |
| 54 } | |
| 55 | |
| 56 TEST_F(JsSyncEncryptionHandlerObserverTest, OnPassphraseRequired) { | |
| 57 InSequence dummy; | |
| 58 | |
| 59 base::DictionaryValue reason_passphrase_not_required_details; | |
| 60 base::DictionaryValue reason_encryption_details; | |
| 61 base::DictionaryValue reason_decryption_details; | |
| 62 | |
| 63 reason_passphrase_not_required_details.SetString( | |
| 64 "reason", | |
| 65 PassphraseRequiredReasonToString(REASON_PASSPHRASE_NOT_REQUIRED)); | |
| 66 reason_encryption_details.SetString( | |
| 67 "reason", PassphraseRequiredReasonToString(REASON_ENCRYPTION)); | |
| 68 reason_decryption_details.SetString( | |
| 69 "reason", PassphraseRequiredReasonToString(REASON_DECRYPTION)); | |
| 70 | |
| 71 EXPECT_CALL(mock_js_event_handler_, | |
| 72 HandleJsEvent("onPassphraseRequired", | |
| 73 HasDetailsAsDictionary( | |
| 74 reason_passphrase_not_required_details))); | |
| 75 EXPECT_CALL(mock_js_event_handler_, | |
| 76 HandleJsEvent("onPassphraseRequired", | |
| 77 HasDetailsAsDictionary(reason_encryption_details))); | |
| 78 EXPECT_CALL(mock_js_event_handler_, | |
| 79 HandleJsEvent("onPassphraseRequired", | |
| 80 HasDetailsAsDictionary(reason_decryption_details))); | |
| 81 | |
| 82 js_sync_encryption_handler_observer_.OnPassphraseRequired( | |
| 83 REASON_PASSPHRASE_NOT_REQUIRED, sync_pb::EncryptedData()); | |
| 84 js_sync_encryption_handler_observer_.OnPassphraseRequired( | |
| 85 REASON_ENCRYPTION, sync_pb::EncryptedData()); | |
| 86 js_sync_encryption_handler_observer_.OnPassphraseRequired( | |
| 87 REASON_DECRYPTION, sync_pb::EncryptedData()); | |
| 88 PumpLoop(); | |
| 89 } | |
| 90 | |
| 91 TEST_F(JsSyncEncryptionHandlerObserverTest, OnBootstrapTokenUpdated) { | |
| 92 base::DictionaryValue bootstrap_token_details; | |
| 93 bootstrap_token_details.SetString("bootstrapToken", "<redacted>"); | |
| 94 bootstrap_token_details.SetString("type", "PASSPHRASE_BOOTSTRAP_TOKEN"); | |
| 95 | |
| 96 EXPECT_CALL(mock_js_event_handler_, | |
| 97 HandleJsEvent("onBootstrapTokenUpdated", | |
| 98 HasDetailsAsDictionary(bootstrap_token_details))); | |
| 99 | |
| 100 js_sync_encryption_handler_observer_.OnBootstrapTokenUpdated( | |
| 101 "sensitive_token", PASSPHRASE_BOOTSTRAP_TOKEN); | |
| 102 PumpLoop(); | |
| 103 } | |
| 104 | |
| 105 TEST_F(JsSyncEncryptionHandlerObserverTest, OnEncryptedTypesChanged) { | |
| 106 base::DictionaryValue expected_details; | |
| 107 base::ListValue* encrypted_type_values = new base::ListValue(); | |
| 108 const bool encrypt_everything = false; | |
| 109 expected_details.Set("encryptedTypes", encrypted_type_values); | |
| 110 expected_details.SetBoolean("encryptEverything", encrypt_everything); | |
| 111 ModelTypeSet encrypted_types; | |
| 112 | |
| 113 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) { | |
| 114 ModelType type = ModelTypeFromInt(i); | |
| 115 encrypted_types.Put(type); | |
| 116 encrypted_type_values->AppendString(ModelTypeToString(type)); | |
| 117 } | |
| 118 | |
| 119 EXPECT_CALL(mock_js_event_handler_, | |
| 120 HandleJsEvent("onEncryptedTypesChanged", | |
| 121 HasDetailsAsDictionary(expected_details))); | |
| 122 | |
| 123 js_sync_encryption_handler_observer_.OnEncryptedTypesChanged( | |
| 124 encrypted_types, encrypt_everything); | |
| 125 PumpLoop(); | |
| 126 } | |
| 127 | |
| 128 TEST_F(JsSyncEncryptionHandlerObserverTest, OnCryptographerStateChanged) { | |
| 129 base::DictionaryValue expected_details; | |
| 130 bool expected_ready = false; | |
| 131 bool expected_pending = false; | |
| 132 expected_details.SetBoolean("ready", expected_ready); | |
| 133 expected_details.SetBoolean("hasPendingKeys", expected_pending); | |
| 134 ModelTypeSet encrypted_types; | |
| 135 | |
| 136 EXPECT_CALL(mock_js_event_handler_, | |
| 137 HandleJsEvent("onCryptographerStateChanged", | |
| 138 HasDetailsAsDictionary(expected_details))); | |
| 139 | |
| 140 FakeEncryptor encryptor; | |
| 141 Cryptographer cryptographer(&encryptor); | |
| 142 | |
| 143 js_sync_encryption_handler_observer_.OnCryptographerStateChanged( | |
| 144 &cryptographer); | |
| 145 PumpLoop(); | |
| 146 } | |
| 147 | |
| 148 TEST_F(JsSyncEncryptionHandlerObserverTest, OnPassphraseTypeChanged) { | |
| 149 InSequence dummy; | |
| 150 | |
| 151 base::DictionaryValue passphrase_type_details; | |
| 152 passphrase_type_details.SetString("passphraseType", | |
| 153 "PassphraseType::IMPLICIT_PASSPHRASE"); | |
| 154 passphrase_type_details.SetInteger("explicitPassphraseTime", 10); | |
| 155 EXPECT_CALL(mock_js_event_handler_, | |
| 156 HandleJsEvent("onPassphraseTypeChanged", | |
| 157 HasDetailsAsDictionary(passphrase_type_details))); | |
| 158 | |
| 159 js_sync_encryption_handler_observer_.OnPassphraseTypeChanged( | |
| 160 PassphraseType::IMPLICIT_PASSPHRASE, ProtoTimeToTime(10)); | |
| 161 PumpLoop(); | |
| 162 } | |
| 163 | |
| 164 } // namespace | |
| 165 } // namespace syncer | |
| OLD | NEW |