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