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/js_sync_encryption_handler_observer.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/location.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "base/values.h" |
| 11 #include "sync/internal_api/public/base/model_type.h" |
| 12 #include "sync/internal_api/public/util/sync_string_conversions.h" |
| 13 #include "sync/internal_api/public/util/weak_handle.h" |
| 14 #include "sync/js/js_event_details.h" |
| 15 #include "sync/js/js_test_util.h" |
| 16 #include "sync/util/cryptographer.h" |
| 17 #include "sync/test/fake_encryptor.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 MessageLoop message_loop_; |
| 37 |
| 38 protected: |
| 39 StrictMock<MockJsEventHandler> mock_js_event_handler_; |
| 40 JsSyncEncryptionHandlerObserver js_sync_encryption_handler_observer_; |
| 41 |
| 42 void PumpLoop() { |
| 43 message_loop_.RunAllPending(); |
| 44 } |
| 45 }; |
| 46 |
| 47 TEST_F(JsSyncEncryptionHandlerObserverTest, NoArgNotifiations) { |
| 48 InSequence dummy; |
| 49 |
| 50 EXPECT_CALL(mock_js_event_handler_, |
| 51 HandleJsEvent("onEncryptionComplete", |
| 52 HasDetails(JsEventDetails()))); |
| 53 |
| 54 js_sync_encryption_handler_observer_.OnEncryptionComplete(); |
| 55 PumpLoop(); |
| 56 } |
| 57 |
| 58 TEST_F(JsSyncEncryptionHandlerObserverTest, OnPassphraseRequired) { |
| 59 InSequence dummy; |
| 60 |
| 61 DictionaryValue reason_passphrase_not_required_details; |
| 62 DictionaryValue reason_encryption_details; |
| 63 DictionaryValue reason_decryption_details; |
| 64 |
| 65 reason_passphrase_not_required_details.SetString( |
| 66 "reason", |
| 67 PassphraseRequiredReasonToString(REASON_PASSPHRASE_NOT_REQUIRED)); |
| 68 reason_encryption_details.SetString( |
| 69 "reason", |
| 70 PassphraseRequiredReasonToString(REASON_ENCRYPTION)); |
| 71 reason_decryption_details.SetString( |
| 72 "reason", |
| 73 PassphraseRequiredReasonToString(REASON_DECRYPTION)); |
| 74 |
| 75 EXPECT_CALL(mock_js_event_handler_, |
| 76 HandleJsEvent("onPassphraseRequired", |
| 77 HasDetailsAsDictionary( |
| 78 reason_passphrase_not_required_details))); |
| 79 EXPECT_CALL(mock_js_event_handler_, |
| 80 HandleJsEvent("onPassphraseRequired", |
| 81 HasDetailsAsDictionary(reason_encryption_details))); |
| 82 EXPECT_CALL(mock_js_event_handler_, |
| 83 HandleJsEvent("onPassphraseRequired", |
| 84 HasDetailsAsDictionary(reason_decryption_details))); |
| 85 |
| 86 js_sync_encryption_handler_observer_.OnPassphraseRequired( |
| 87 REASON_PASSPHRASE_NOT_REQUIRED, |
| 88 sync_pb::EncryptedData()); |
| 89 js_sync_encryption_handler_observer_.OnPassphraseRequired(REASON_ENCRYPTION, |
| 90 sync_pb::EncryptedData()); |
| 91 js_sync_encryption_handler_observer_.OnPassphraseRequired(REASON_DECRYPTION, |
| 92 sync_pb::EncryptedData()); |
| 93 PumpLoop(); |
| 94 } |
| 95 |
| 96 TEST_F(JsSyncEncryptionHandlerObserverTest, SensitiveNotifiations) { |
| 97 DictionaryValue redacted_token_details; |
| 98 redacted_token_details.SetString("token", "<redacted>"); |
| 99 DictionaryValue redacted_bootstrap_token_details; |
| 100 redacted_bootstrap_token_details.SetString("bootstrapToken", "<redacted>"); |
| 101 |
| 102 EXPECT_CALL(mock_js_event_handler_, |
| 103 HandleJsEvent( |
| 104 "OnBootstrapTokenUpdated", |
| 105 HasDetailsAsDictionary(redacted_bootstrap_token_details))); |
| 106 |
| 107 js_sync_encryption_handler_observer_.OnBootstrapTokenUpdated( |
| 108 "sensitive_token"); |
| 109 PumpLoop(); |
| 110 } |
| 111 |
| 112 TEST_F(JsSyncEncryptionHandlerObserverTest, OnEncryptedTypesChanged) { |
| 113 DictionaryValue expected_details; |
| 114 ListValue* encrypted_type_values = new ListValue(); |
| 115 const bool encrypt_everything = false; |
| 116 expected_details.Set("encryptedTypes", encrypted_type_values); |
| 117 expected_details.SetBoolean("encryptEverything", encrypt_everything); |
| 118 ModelTypeSet encrypted_types; |
| 119 |
| 120 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) { |
| 121 ModelType type = ModelTypeFromInt(i); |
| 122 encrypted_types.Put(type); |
| 123 encrypted_type_values->Append(Value::CreateStringValue( |
| 124 ModelTypeToString(type))); |
| 125 } |
| 126 |
| 127 EXPECT_CALL(mock_js_event_handler_, |
| 128 HandleJsEvent("onEncryptedTypesChanged", |
| 129 HasDetailsAsDictionary(expected_details))); |
| 130 |
| 131 js_sync_encryption_handler_observer_.OnEncryptedTypesChanged( |
| 132 encrypted_types, encrypt_everything); |
| 133 PumpLoop(); |
| 134 } |
| 135 |
| 136 |
| 137 TEST_F(JsSyncEncryptionHandlerObserverTest, OnCryptographerStateChanged) { |
| 138 DictionaryValue expected_details; |
| 139 bool expected_ready = false; |
| 140 bool expected_pending = false; |
| 141 expected_details.SetBoolean("ready", expected_ready); |
| 142 expected_details.SetBoolean("hasPendingKeys", expected_pending); |
| 143 ModelTypeSet encrypted_types; |
| 144 |
| 145 EXPECT_CALL(mock_js_event_handler_, |
| 146 HandleJsEvent("onCryptographerStateChanged", |
| 147 HasDetailsAsDictionary(expected_details))); |
| 148 |
| 149 FakeEncryptor encryptor; |
| 150 Cryptographer cryptographer(&encryptor); |
| 151 |
| 152 js_sync_encryption_handler_observer_.OnCryptographerStateChanged( |
| 153 &cryptographer); |
| 154 PumpLoop(); |
| 155 } |
| 156 |
| 157 } // namespace |
| 158 } // namespace syncer |
OLD | NEW |