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/location.h" | |
8 #include "base/run_loop.h" | |
9 #include "base/values.h" | |
10 #include "sync/internal_api/public/base/model_type.h" | |
11 #include "sync/internal_api/public/util/sync_string_conversions.h" | |
12 #include "sync/internal_api/public/util/weak_handle.h" | |
13 #include "sync/js/js_event_details.h" | |
14 #include "sync/js/js_test_util.h" | |
15 #include "sync/test/fake_encryptor.h" | |
16 #include "sync/util/cryptographer.h" | |
17 #include "sync/util/time.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() { | |
43 base::RunLoop().RunUntilIdle(); | |
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 base::DictionaryValue reason_passphrase_not_required_details; | |
62 base::DictionaryValue reason_encryption_details; | |
63 base::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, OnBootstrapTokenUpdated) { | |
97 base::DictionaryValue bootstrap_token_details; | |
98 bootstrap_token_details.SetString("bootstrapToken", "<redacted>"); | |
99 bootstrap_token_details.SetString("type", "PASSPHRASE_BOOTSTRAP_TOKEN"); | |
100 | |
101 EXPECT_CALL(mock_js_event_handler_, | |
102 HandleJsEvent( | |
103 "onBootstrapTokenUpdated", | |
104 HasDetailsAsDictionary(bootstrap_token_details))); | |
105 | |
106 js_sync_encryption_handler_observer_.OnBootstrapTokenUpdated( | |
107 "sensitive_token", PASSPHRASE_BOOTSTRAP_TOKEN); | |
108 PumpLoop(); | |
109 } | |
110 | |
111 TEST_F(JsSyncEncryptionHandlerObserverTest, OnEncryptedTypesChanged) { | |
112 base::DictionaryValue expected_details; | |
113 base::ListValue* encrypted_type_values = new base::ListValue(); | |
114 const bool encrypt_everything = false; | |
115 expected_details.Set("encryptedTypes", encrypted_type_values); | |
116 expected_details.SetBoolean("encryptEverything", encrypt_everything); | |
117 ModelTypeSet encrypted_types; | |
118 | |
119 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) { | |
120 ModelType type = ModelTypeFromInt(i); | |
121 encrypted_types.Put(type); | |
122 encrypted_type_values->AppendString(ModelTypeToString(type)); | |
123 } | |
124 | |
125 EXPECT_CALL(mock_js_event_handler_, | |
126 HandleJsEvent("onEncryptedTypesChanged", | |
127 HasDetailsAsDictionary(expected_details))); | |
128 | |
129 js_sync_encryption_handler_observer_.OnEncryptedTypesChanged( | |
130 encrypted_types, encrypt_everything); | |
131 PumpLoop(); | |
132 } | |
133 | |
134 | |
135 TEST_F(JsSyncEncryptionHandlerObserverTest, OnCryptographerStateChanged) { | |
136 base::DictionaryValue expected_details; | |
137 bool expected_ready = false; | |
138 bool expected_pending = false; | |
139 expected_details.SetBoolean("ready", expected_ready); | |
140 expected_details.SetBoolean("hasPendingKeys", expected_pending); | |
141 ModelTypeSet encrypted_types; | |
142 | |
143 EXPECT_CALL(mock_js_event_handler_, | |
144 HandleJsEvent("onCryptographerStateChanged", | |
145 HasDetailsAsDictionary(expected_details))); | |
146 | |
147 FakeEncryptor encryptor; | |
148 Cryptographer cryptographer(&encryptor); | |
149 | |
150 js_sync_encryption_handler_observer_.OnCryptographerStateChanged( | |
151 &cryptographer); | |
152 PumpLoop(); | |
153 } | |
154 | |
155 TEST_F(JsSyncEncryptionHandlerObserverTest, OnPassphraseTypeChanged) { | |
156 InSequence dummy; | |
157 | |
158 base::DictionaryValue passphrase_type_details; | |
159 passphrase_type_details.SetString("passphraseType", "IMPLICIT_PASSPHRASE"); | |
160 passphrase_type_details.SetInteger("explicitPassphraseTime", 10); | |
161 EXPECT_CALL(mock_js_event_handler_, | |
162 HandleJsEvent("onPassphraseTypeChanged", | |
163 HasDetailsAsDictionary(passphrase_type_details))); | |
164 | |
165 js_sync_encryption_handler_observer_.OnPassphraseTypeChanged( | |
166 IMPLICIT_PASSPHRASE, ProtoTimeToTime(10)); | |
167 PumpLoop(); | |
168 } | |
169 | |
170 } // namespace | |
171 } // namespace syncer | |
OLD | NEW |