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/sync_encryption_handler_impl.h" | 5 #include "sync/internal_api/sync_encryption_handler_impl.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/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/tracked_objects.h" | 11 #include "base/tracked_objects.h" |
12 #include "sync/internal_api/public/base/model_type_test_util.h" | 12 #include "sync/internal_api/public/base/model_type_test_util.h" |
13 #include "sync/internal_api/public/read_node.h" | 13 #include "sync/internal_api/public/read_node.h" |
14 #include "sync/internal_api/public/read_transaction.h" | 14 #include "sync/internal_api/public/read_transaction.h" |
15 #include "sync/internal_api/public/write_transaction.h" | 15 #include "sync/internal_api/public/write_transaction.h" |
16 #include "sync/internal_api/public/test/test_user_share.h" | 16 #include "sync/internal_api/public/test/test_user_share.h" |
17 #include "sync/protocol/nigori_specifics.pb.h" | 17 #include "sync/protocol/nigori_specifics.pb.h" |
18 #include "sync/protocol/sync.pb.h" | 18 #include "sync/protocol/sync.pb.h" |
19 #include "sync/syncable/entry.h" | 19 #include "sync/syncable/entry.h" |
20 #include "sync/syncable/mutable_entry.h" | 20 #include "sync/syncable/mutable_entry.h" |
21 #include "sync/syncable/write_transaction.h" | 21 #include "sync/syncable/write_transaction.h" |
22 #include "sync/test/engine/test_id_factory.h" | 22 #include "sync/test/engine/test_id_factory.h" |
| 23 #include "sync/test/fake_encryptor.h" |
23 #include "sync/util/cryptographer.h" | 24 #include "sync/util/cryptographer.h" |
24 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
26 | 27 |
27 namespace syncer { | 28 namespace syncer { |
28 | 29 |
29 namespace { | 30 namespace { |
30 | 31 |
31 using ::testing::_; | 32 using ::testing::_; |
32 using ::testing::Mock; | 33 using ::testing::Mock; |
(...skipping 10 matching lines...) Expand all Loading... |
43 MOCK_METHOD2(OnEncryptedTypesChanged, | 44 MOCK_METHOD2(OnEncryptedTypesChanged, |
44 void(ModelTypeSet, bool)); // NOLINT | 45 void(ModelTypeSet, bool)); // NOLINT |
45 MOCK_METHOD0(OnEncryptionComplete, void()); // NOLINT | 46 MOCK_METHOD0(OnEncryptionComplete, void()); // NOLINT |
46 MOCK_METHOD1(OnCryptographerStateChanged, void(Cryptographer*)); // NOLINT | 47 MOCK_METHOD1(OnCryptographerStateChanged, void(Cryptographer*)); // NOLINT |
47 }; | 48 }; |
48 | 49 |
49 } // namespace | 50 } // namespace |
50 | 51 |
51 class SyncEncryptionHandlerImplTest : public ::testing::Test { | 52 class SyncEncryptionHandlerImplTest : public ::testing::Test { |
52 public: | 53 public: |
53 SyncEncryptionHandlerImplTest() : cryptographer_(NULL) {} | 54 SyncEncryptionHandlerImplTest() {} |
54 virtual ~SyncEncryptionHandlerImplTest() {} | 55 virtual ~SyncEncryptionHandlerImplTest() {} |
55 | 56 |
56 virtual void SetUp() { | 57 virtual void SetUp() { |
57 test_user_share_.SetUp(); | 58 test_user_share_.SetUp(); |
58 SetUpEncryption(); | 59 SetUpEncryption(); |
59 CreateRootForType(NIGORI); | 60 CreateRootForType(NIGORI); |
60 } | 61 } |
61 | 62 |
62 virtual void TearDown() { | 63 virtual void TearDown() { |
63 test_user_share_.TearDown(); | 64 test_user_share_.TearDown(); |
64 } | 65 } |
65 | 66 |
66 protected: | 67 protected: |
67 void SetUpEncryption() { | 68 void SetUpEncryption() { |
68 ReadTransaction trans(FROM_HERE, user_share()); | 69 ReadTransaction trans(FROM_HERE, user_share()); |
69 cryptographer_ = trans.GetCryptographer(); | |
70 encryption_handler_.reset( | 70 encryption_handler_.reset( |
71 new SyncEncryptionHandlerImpl(user_share(), | 71 new SyncEncryptionHandlerImpl(user_share(), |
72 cryptographer_)); | 72 &encryptor_)); |
73 cryptographer_->SetNigoriHandler( | |
74 encryption_handler_.get()); | |
75 encryption_handler_->AddObserver(&observer_); | 73 encryption_handler_->AddObserver(&observer_); |
76 } | 74 } |
77 | 75 |
78 void CreateRootForType(ModelType model_type) { | 76 void CreateRootForType(ModelType model_type) { |
79 syncer::syncable::Directory* directory = user_share()->directory.get(); | 77 syncer::syncable::Directory* directory = user_share()->directory.get(); |
80 | 78 |
81 std::string tag_name = ModelTypeToRootTag(model_type); | 79 std::string tag_name = ModelTypeToRootTag(model_type); |
82 | 80 |
83 syncable::WriteTransaction wtrans(FROM_HERE, syncable::UNITTEST, directory); | 81 syncable::WriteTransaction wtrans(FROM_HERE, syncable::UNITTEST, directory); |
84 syncable::MutableEntry node(&wtrans, | 82 syncable::MutableEntry node(&wtrans, |
(...skipping 17 matching lines...) Expand all Loading... |
102 void PumpLoop() { | 100 void PumpLoop() { |
103 message_loop_.RunAllPending(); | 101 message_loop_.RunAllPending(); |
104 } | 102 } |
105 | 103 |
106 // Getters for tests. | 104 // Getters for tests. |
107 UserShare* user_share() { return test_user_share_.user_share(); } | 105 UserShare* user_share() { return test_user_share_.user_share(); } |
108 SyncEncryptionHandlerImpl* encryption_handler() { | 106 SyncEncryptionHandlerImpl* encryption_handler() { |
109 return encryption_handler_.get(); | 107 return encryption_handler_.get(); |
110 } | 108 } |
111 SyncEncryptionHandlerObserverMock* observer() { return &observer_; } | 109 SyncEncryptionHandlerObserverMock* observer() { return &observer_; } |
112 Cryptographer* cryptographer() { return cryptographer_; } | 110 Cryptographer* GetCryptographer() { |
| 111 return encryption_handler_->cryptographer_unsafe(); |
| 112 } |
113 | 113 |
114 private: | 114 protected: |
115 TestUserShare test_user_share_; | 115 TestUserShare test_user_share_; |
| 116 FakeEncryptor encryptor_; |
116 scoped_ptr<SyncEncryptionHandlerImpl> encryption_handler_; | 117 scoped_ptr<SyncEncryptionHandlerImpl> encryption_handler_; |
117 StrictMock<SyncEncryptionHandlerObserverMock> observer_; | 118 StrictMock<SyncEncryptionHandlerObserverMock> observer_; |
118 Cryptographer* cryptographer_; | |
119 TestIdFactory ids_; | 119 TestIdFactory ids_; |
120 MessageLoop message_loop_; | 120 MessageLoop message_loop_; |
121 }; | 121 }; |
122 | 122 |
123 // Verify that the encrypted types are being written to and read from the | 123 // Verify that the encrypted types are being written to and read from the |
124 // nigori node properly. | 124 // nigori node properly. |
125 TEST_F(SyncEncryptionHandlerImplTest, NigoriEncryptionTypes) { | 125 TEST_F(SyncEncryptionHandlerImplTest, NigoriEncryptionTypes) { |
126 sync_pb::NigoriSpecifics nigori; | 126 sync_pb::NigoriSpecifics nigori; |
127 | 127 |
128 StrictMock<SyncEncryptionHandlerObserverMock> observer2; | 128 StrictMock<SyncEncryptionHandlerObserverMock> observer2; |
129 SyncEncryptionHandlerImpl handler2(user_share(), | 129 SyncEncryptionHandlerImpl handler2(user_share(), |
130 cryptographer()); | 130 &encryptor_); |
131 handler2.AddObserver(&observer2); | 131 handler2.AddObserver(&observer2); |
132 | 132 |
133 // Just set the sensitive types (shouldn't trigger any notifications). | 133 // Just set the sensitive types (shouldn't trigger any notifications). |
134 ModelTypeSet encrypted_types(SyncEncryptionHandler::SensitiveTypes()); | 134 ModelTypeSet encrypted_types(SyncEncryptionHandler::SensitiveTypes()); |
135 encryption_handler()->MergeEncryptedTypes(encrypted_types); | |
136 { | 135 { |
137 WriteTransaction trans(FROM_HERE, user_share()); | 136 WriteTransaction trans(FROM_HERE, user_share()); |
| 137 encryption_handler()->MergeEncryptedTypes( |
| 138 encrypted_types, |
| 139 trans.GetWrappedTrans()); |
138 encryption_handler()->UpdateNigoriFromEncryptedTypes( | 140 encryption_handler()->UpdateNigoriFromEncryptedTypes( |
139 &nigori, | 141 &nigori, |
140 trans.GetWrappedTrans()); | 142 trans.GetWrappedTrans()); |
| 143 handler2.UpdateEncryptedTypesFromNigori(nigori, trans.GetWrappedTrans()); |
141 } | 144 } |
142 handler2.UpdateEncryptedTypesFromNigori(nigori); | |
143 EXPECT_TRUE(encrypted_types.Equals( | 145 EXPECT_TRUE(encrypted_types.Equals( |
144 encryption_handler()->GetEncryptedTypes())); | 146 encryption_handler()->encrypted_types_unsafe())); |
145 EXPECT_TRUE(encrypted_types.Equals( | 147 EXPECT_TRUE(encrypted_types.Equals( |
146 handler2.GetEncryptedTypes())); | 148 handler2.encrypted_types_unsafe())); |
147 | 149 |
148 Mock::VerifyAndClearExpectations(observer()); | 150 Mock::VerifyAndClearExpectations(observer()); |
149 Mock::VerifyAndClearExpectations(&observer2); | 151 Mock::VerifyAndClearExpectations(&observer2); |
150 | 152 |
151 EXPECT_CALL(*observer(), | 153 EXPECT_CALL(*observer(), |
152 OnEncryptedTypesChanged( | 154 OnEncryptedTypesChanged( |
153 HasModelTypes(ModelTypeSet::All()), false)); | 155 HasModelTypes(ModelTypeSet::All()), false)); |
154 EXPECT_CALL(observer2, | 156 EXPECT_CALL(observer2, |
155 OnEncryptedTypesChanged( | 157 OnEncryptedTypesChanged( |
156 HasModelTypes(ModelTypeSet::All()), false)); | 158 HasModelTypes(ModelTypeSet::All()), false)); |
157 | 159 |
158 // Set all encrypted types | 160 // Set all encrypted types |
159 encrypted_types = ModelTypeSet::All(); | 161 encrypted_types = ModelTypeSet::All(); |
160 encryption_handler()->MergeEncryptedTypes(encrypted_types); | |
161 { | 162 { |
162 WriteTransaction trans(FROM_HERE, user_share()); | 163 WriteTransaction trans(FROM_HERE, user_share()); |
| 164 encryption_handler()->MergeEncryptedTypes( |
| 165 encrypted_types, |
| 166 trans.GetWrappedTrans()); |
163 encryption_handler()->UpdateNigoriFromEncryptedTypes( | 167 encryption_handler()->UpdateNigoriFromEncryptedTypes( |
164 &nigori, | 168 &nigori, |
165 trans.GetWrappedTrans()); | 169 trans.GetWrappedTrans()); |
| 170 handler2.UpdateEncryptedTypesFromNigori(nigori, trans.GetWrappedTrans()); |
166 } | 171 } |
167 handler2.UpdateEncryptedTypesFromNigori(nigori); | |
168 EXPECT_TRUE(encrypted_types.Equals( | 172 EXPECT_TRUE(encrypted_types.Equals( |
169 encryption_handler()->GetEncryptedTypes())); | 173 encryption_handler()->encrypted_types_unsafe())); |
170 EXPECT_TRUE(encrypted_types.Equals(handler2.GetEncryptedTypes())); | 174 EXPECT_TRUE(encrypted_types.Equals(handler2.encrypted_types_unsafe())); |
171 | 175 |
172 // Receiving an empty nigori should not reset any encrypted types or trigger | 176 // Receiving an empty nigori should not reset any encrypted types or trigger |
173 // an observer notification. | 177 // an observer notification. |
174 Mock::VerifyAndClearExpectations(observer()); | 178 Mock::VerifyAndClearExpectations(observer()); |
175 Mock::VerifyAndClearExpectations(&observer2); | 179 Mock::VerifyAndClearExpectations(&observer2); |
176 nigori = sync_pb::NigoriSpecifics(); | 180 nigori = sync_pb::NigoriSpecifics(); |
177 encryption_handler()->UpdateEncryptedTypesFromNigori(nigori); | 181 { |
| 182 WriteTransaction trans(FROM_HERE, user_share()); |
| 183 handler2.UpdateEncryptedTypesFromNigori(nigori, trans.GetWrappedTrans()); |
| 184 } |
178 EXPECT_TRUE(encrypted_types.Equals( | 185 EXPECT_TRUE(encrypted_types.Equals( |
179 encryption_handler()->GetEncryptedTypes())); | 186 encryption_handler()->encrypted_types_unsafe())); |
180 } | 187 } |
181 | 188 |
182 // Verify the encryption handler processes the encrypt everything field | 189 // Verify the encryption handler processes the encrypt everything field |
183 // properly. | 190 // properly. |
184 TEST_F(SyncEncryptionHandlerImplTest, EncryptEverythingExplicit) { | 191 TEST_F(SyncEncryptionHandlerImplTest, EncryptEverythingExplicit) { |
185 ModelTypeSet real_types = ModelTypeSet::All(); | 192 ModelTypeSet real_types = ModelTypeSet::All(); |
186 sync_pb::NigoriSpecifics specifics; | 193 sync_pb::NigoriSpecifics nigori; |
187 specifics.set_encrypt_everything(true); | 194 nigori.set_encrypt_everything(true); |
188 | 195 |
189 EXPECT_CALL(*observer(), | 196 EXPECT_CALL(*observer(), |
190 OnEncryptedTypesChanged( | 197 OnEncryptedTypesChanged( |
191 HasModelTypes(ModelTypeSet::All()), true)); | 198 HasModelTypes(ModelTypeSet::All()), true)); |
192 | 199 |
193 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); | 200 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); |
194 ModelTypeSet encrypted_types = encryption_handler()->GetEncryptedTypes(); | 201 ModelTypeSet encrypted_types = encryption_handler()->encrypted_types_unsafe(); |
195 for (ModelTypeSet::Iterator iter = real_types.First(); | 202 for (ModelTypeSet::Iterator iter = real_types.First(); |
196 iter.Good(); iter.Inc()) { | 203 iter.Good(); iter.Inc()) { |
197 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) | 204 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) |
198 EXPECT_TRUE(encrypted_types.Has(iter.Get())); | 205 EXPECT_TRUE(encrypted_types.Has(iter.Get())); |
199 else | 206 else |
200 EXPECT_FALSE(encrypted_types.Has(iter.Get())); | 207 EXPECT_FALSE(encrypted_types.Has(iter.Get())); |
201 } | 208 } |
202 | 209 |
203 encryption_handler()->UpdateEncryptedTypesFromNigori(specifics); | 210 { |
| 211 WriteTransaction trans(FROM_HERE, user_share()); |
| 212 encryption_handler()->UpdateEncryptedTypesFromNigori( |
| 213 nigori, |
| 214 trans.GetWrappedTrans()); |
| 215 } |
204 | 216 |
205 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); | 217 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); |
206 encrypted_types = encryption_handler()->GetEncryptedTypes(); | 218 encrypted_types = encryption_handler()->encrypted_types_unsafe(); |
207 for (ModelTypeSet::Iterator iter = real_types.First(); | 219 for (ModelTypeSet::Iterator iter = real_types.First(); |
208 iter.Good(); iter.Inc()) { | 220 iter.Good(); iter.Inc()) { |
209 EXPECT_TRUE(encrypted_types.Has(iter.Get())); | 221 EXPECT_TRUE(encrypted_types.Has(iter.Get())); |
210 } | 222 } |
211 | 223 |
212 // Receiving the nigori node again shouldn't trigger another notification. | 224 // Receiving the nigori node again shouldn't trigger another notification. |
213 Mock::VerifyAndClearExpectations(observer()); | 225 Mock::VerifyAndClearExpectations(observer()); |
214 encryption_handler()->UpdateEncryptedTypesFromNigori(specifics); | 226 { |
| 227 WriteTransaction trans(FROM_HERE, user_share()); |
| 228 encryption_handler()->UpdateEncryptedTypesFromNigori( |
| 229 nigori, |
| 230 trans.GetWrappedTrans()); |
| 231 } |
215 } | 232 } |
216 | 233 |
217 // Verify the encryption handler can detect an implicit encrypt everything state | 234 // Verify the encryption handler can detect an implicit encrypt everything state |
218 // (from clients that failed to write the encrypt everything field). | 235 // (from clients that failed to write the encrypt everything field). |
219 TEST_F(SyncEncryptionHandlerImplTest, EncryptEverythingImplicit) { | 236 TEST_F(SyncEncryptionHandlerImplTest, EncryptEverythingImplicit) { |
220 ModelTypeSet real_types = ModelTypeSet::All(); | 237 ModelTypeSet real_types = ModelTypeSet::All(); |
221 sync_pb::NigoriSpecifics specifics; | 238 sync_pb::NigoriSpecifics nigori; |
222 specifics.set_encrypt_bookmarks(true); // Non-passwords = encrypt everything | 239 nigori.set_encrypt_bookmarks(true); // Non-passwords = encrypt everything |
223 | 240 |
224 EXPECT_CALL(*observer(), | 241 EXPECT_CALL(*observer(), |
225 OnEncryptedTypesChanged( | 242 OnEncryptedTypesChanged( |
226 HasModelTypes(ModelTypeSet::All()), true)); | 243 HasModelTypes(ModelTypeSet::All()), true)); |
227 | 244 |
228 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); | 245 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); |
229 ModelTypeSet encrypted_types = encryption_handler()->GetEncryptedTypes(); | 246 ModelTypeSet encrypted_types = encryption_handler()->encrypted_types_unsafe(); |
230 for (ModelTypeSet::Iterator iter = real_types.First(); | 247 for (ModelTypeSet::Iterator iter = real_types.First(); |
231 iter.Good(); iter.Inc()) { | 248 iter.Good(); iter.Inc()) { |
232 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) | 249 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) |
233 EXPECT_TRUE(encrypted_types.Has(iter.Get())); | 250 EXPECT_TRUE(encrypted_types.Has(iter.Get())); |
234 else | 251 else |
235 EXPECT_FALSE(encrypted_types.Has(iter.Get())); | 252 EXPECT_FALSE(encrypted_types.Has(iter.Get())); |
236 } | 253 } |
237 | 254 |
238 encryption_handler()->UpdateEncryptedTypesFromNigori(specifics); | 255 { |
| 256 WriteTransaction trans(FROM_HERE, user_share()); |
| 257 encryption_handler()->UpdateEncryptedTypesFromNigori( |
| 258 nigori, |
| 259 trans.GetWrappedTrans()); |
| 260 } |
239 | 261 |
240 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); | 262 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); |
241 encrypted_types = encryption_handler()->GetEncryptedTypes(); | 263 encrypted_types = encryption_handler()->encrypted_types_unsafe(); |
242 for (ModelTypeSet::Iterator iter = real_types.First(); | 264 for (ModelTypeSet::Iterator iter = real_types.First(); |
243 iter.Good(); iter.Inc()) { | 265 iter.Good(); iter.Inc()) { |
244 EXPECT_TRUE(encrypted_types.Has(iter.Get())); | 266 EXPECT_TRUE(encrypted_types.Has(iter.Get())); |
245 } | 267 } |
246 | 268 |
247 // Receiving a nigori node with encrypt everything explicitly set shouldn't | 269 // Receiving a nigori node with encrypt everything explicitly set shouldn't |
248 // trigger another notification. | 270 // trigger another notification. |
249 Mock::VerifyAndClearExpectations(observer()); | 271 Mock::VerifyAndClearExpectations(observer()); |
250 specifics.set_encrypt_everything(true); | 272 nigori.set_encrypt_everything(true); |
251 encryption_handler()->UpdateEncryptedTypesFromNigori(specifics); | 273 { |
| 274 WriteTransaction trans(FROM_HERE, user_share()); |
| 275 encryption_handler()->UpdateEncryptedTypesFromNigori( |
| 276 nigori, |
| 277 trans.GetWrappedTrans()); |
| 278 } |
252 } | 279 } |
253 | 280 |
254 // Verify the encryption handler can deal with new versions treating new types | 281 // Verify the encryption handler can deal with new versions treating new types |
255 // as Sensitive, and that it does not consider this an implicit encrypt | 282 // as Sensitive, and that it does not consider this an implicit encrypt |
256 // everything case. | 283 // everything case. |
257 TEST_F(SyncEncryptionHandlerImplTest, UnknownSensitiveTypes) { | 284 TEST_F(SyncEncryptionHandlerImplTest, UnknownSensitiveTypes) { |
258 ModelTypeSet real_types = ModelTypeSet::All(); | 285 ModelTypeSet real_types = ModelTypeSet::All(); |
259 sync_pb::NigoriSpecifics specifics; | 286 sync_pb::NigoriSpecifics nigori; |
260 specifics.set_encrypt_everything(false); | 287 nigori.set_encrypt_everything(false); |
261 specifics.set_encrypt_bookmarks(true); | 288 nigori.set_encrypt_bookmarks(true); |
262 | 289 |
263 ModelTypeSet expected_encrypted_types = | 290 ModelTypeSet expected_encrypted_types = |
264 SyncEncryptionHandler::SensitiveTypes(); | 291 SyncEncryptionHandler::SensitiveTypes(); |
265 expected_encrypted_types.Put(BOOKMARKS); | 292 expected_encrypted_types.Put(BOOKMARKS); |
266 | 293 |
267 EXPECT_CALL(*observer(), | 294 EXPECT_CALL(*observer(), |
268 OnEncryptedTypesChanged( | 295 OnEncryptedTypesChanged( |
269 HasModelTypes(expected_encrypted_types), false)); | 296 HasModelTypes(expected_encrypted_types), false)); |
270 | 297 |
271 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); | 298 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); |
272 ModelTypeSet encrypted_types = encryption_handler()->GetEncryptedTypes(); | 299 ModelTypeSet encrypted_types = encryption_handler()->encrypted_types_unsafe(); |
273 for (ModelTypeSet::Iterator iter = real_types.First(); | 300 for (ModelTypeSet::Iterator iter = real_types.First(); |
274 iter.Good(); iter.Inc()) { | 301 iter.Good(); iter.Inc()) { |
275 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) | 302 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) |
276 EXPECT_TRUE(encrypted_types.Has(iter.Get())); | 303 EXPECT_TRUE(encrypted_types.Has(iter.Get())); |
277 else | 304 else |
278 EXPECT_FALSE(encrypted_types.Has(iter.Get())); | 305 EXPECT_FALSE(encrypted_types.Has(iter.Get())); |
279 } | 306 } |
280 | 307 |
281 encryption_handler()->UpdateEncryptedTypesFromNigori(specifics); | 308 { |
| 309 WriteTransaction trans(FROM_HERE, user_share()); |
| 310 encryption_handler()->UpdateEncryptedTypesFromNigori( |
| 311 nigori, |
| 312 trans.GetWrappedTrans()); |
| 313 } |
282 | 314 |
283 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); | 315 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); |
284 encrypted_types = encryption_handler()->GetEncryptedTypes(); | 316 encrypted_types = encryption_handler()->encrypted_types_unsafe(); |
285 for (ModelTypeSet::Iterator iter = real_types.First(); | 317 for (ModelTypeSet::Iterator iter = real_types.First(); |
286 iter.Good(); iter.Inc()) { | 318 iter.Good(); iter.Inc()) { |
287 if (iter.Get() == PASSWORDS || | 319 if (iter.Get() == PASSWORDS || |
288 iter.Get() == NIGORI || | 320 iter.Get() == NIGORI || |
289 iter.Get() == BOOKMARKS) | 321 iter.Get() == BOOKMARKS) |
290 EXPECT_TRUE(encrypted_types.Has(iter.Get())); | 322 EXPECT_TRUE(encrypted_types.Has(iter.Get())); |
291 else | 323 else |
292 EXPECT_FALSE(encrypted_types.Has(iter.Get())); | 324 EXPECT_FALSE(encrypted_types.Has(iter.Get())); |
293 } | 325 } |
294 } | 326 } |
295 | 327 |
296 // Receive an old nigori with old encryption keys and encrypted types. We should | 328 // Receive an old nigori with old encryption keys and encrypted types. We should |
297 // not revert our default key or encrypted types, and should post a task to | 329 // not revert our default key or encrypted types, and should post a task to |
298 // overwrite the existing nigori with the correct data. | 330 // overwrite the existing nigori with the correct data. |
299 TEST_F(SyncEncryptionHandlerImplTest, ReceiveOldNigori) { | 331 TEST_F(SyncEncryptionHandlerImplTest, ReceiveOldNigori) { |
300 KeyParams old_key = {"localhost", "dummy", "old"}; | 332 KeyParams old_key = {"localhost", "dummy", "old"}; |
301 KeyParams current_key = {"localhost", "dummy", "cur"}; | 333 KeyParams current_key = {"localhost", "dummy", "cur"}; |
302 | 334 |
303 // Data for testing encryption/decryption. | 335 // Data for testing encryption/decryption. |
304 Cryptographer other_cryptographer(cryptographer()->encryptor()); | 336 Cryptographer other_cryptographer(GetCryptographer()->encryptor()); |
305 other_cryptographer.AddKey(old_key); | 337 other_cryptographer.AddKey(old_key); |
306 sync_pb::EntitySpecifics other_encrypted_specifics; | 338 sync_pb::EntitySpecifics other_encrypted_specifics; |
307 other_encrypted_specifics.mutable_bookmark()->set_title("title"); | 339 other_encrypted_specifics.mutable_bookmark()->set_title("title"); |
308 other_cryptographer.Encrypt( | 340 other_cryptographer.Encrypt( |
309 other_encrypted_specifics, | 341 other_encrypted_specifics, |
310 other_encrypted_specifics.mutable_encrypted()); | 342 other_encrypted_specifics.mutable_encrypted()); |
311 sync_pb::EntitySpecifics our_encrypted_specifics; | 343 sync_pb::EntitySpecifics our_encrypted_specifics; |
312 our_encrypted_specifics.mutable_bookmark()->set_title("title2"); | 344 our_encrypted_specifics.mutable_bookmark()->set_title("title2"); |
313 ModelTypeSet encrypted_types = ModelTypeSet::All(); | 345 ModelTypeSet encrypted_types = ModelTypeSet::All(); |
314 | 346 |
315 // Set up the current encryption state (containing both keys and encrypt | 347 // Set up the current encryption state (containing both keys and encrypt |
316 // everything). | 348 // everything). |
317 sync_pb::NigoriSpecifics current_nigori_specifics; | 349 sync_pb::NigoriSpecifics current_nigori_specifics; |
318 cryptographer()->AddKey(old_key); | 350 GetCryptographer()->AddKey(old_key); |
319 cryptographer()->AddKey(current_key); | 351 GetCryptographer()->AddKey(current_key); |
320 cryptographer()->Encrypt( | 352 GetCryptographer()->Encrypt( |
321 our_encrypted_specifics, | 353 our_encrypted_specifics, |
322 our_encrypted_specifics.mutable_encrypted()); | 354 our_encrypted_specifics.mutable_encrypted()); |
323 cryptographer()->GetKeys( | 355 GetCryptographer()->GetKeys( |
324 current_nigori_specifics.mutable_encrypted()); | 356 current_nigori_specifics.mutable_encrypted()); |
325 current_nigori_specifics.set_encrypt_everything(true); | 357 current_nigori_specifics.set_encrypt_everything(true); |
326 | 358 |
327 EXPECT_CALL(*observer(), OnCryptographerStateChanged(_)); | 359 EXPECT_CALL(*observer(), OnCryptographerStateChanged(_)); |
328 EXPECT_CALL(*observer(), OnEncryptedTypesChanged( | 360 EXPECT_CALL(*observer(), OnEncryptedTypesChanged( |
329 HasModelTypes(ModelTypeSet::All()), true)); | 361 HasModelTypes(ModelTypeSet::All()), true)); |
330 { | 362 { |
331 // Update the encryption handler. | 363 // Update the encryption handler. |
332 WriteTransaction trans(FROM_HERE, user_share()); | 364 WriteTransaction trans(FROM_HERE, user_share()); |
333 encryption_handler()->ApplyNigoriUpdate( | 365 encryption_handler()->ApplyNigoriUpdate( |
334 current_nigori_specifics, | 366 current_nigori_specifics, |
335 trans.GetWrappedTrans()); | 367 trans.GetWrappedTrans()); |
336 } | 368 } |
337 Mock::VerifyAndClearExpectations(observer()); | 369 Mock::VerifyAndClearExpectations(observer()); |
338 | 370 |
339 // Now set up the old nigori specifics and apply it on top. | 371 // Now set up the old nigori specifics and apply it on top. |
340 // Has an old set of keys, and no encrypted types. | 372 // Has an old set of keys, and no encrypted types. |
341 sync_pb::NigoriSpecifics old_nigori; | 373 sync_pb::NigoriSpecifics old_nigori; |
342 other_cryptographer.GetKeys(old_nigori.mutable_encrypted()); | 374 other_cryptographer.GetKeys(old_nigori.mutable_encrypted()); |
343 | 375 |
344 EXPECT_CALL(*observer(), OnCryptographerStateChanged(_)); | 376 EXPECT_CALL(*observer(), OnCryptographerStateChanged(_)); |
345 { | 377 { |
346 // Update the encryption handler. | 378 // Update the encryption handler. |
347 WriteTransaction trans(FROM_HERE, user_share()); | 379 WriteTransaction trans(FROM_HERE, user_share()); |
348 encryption_handler()->ApplyNigoriUpdate( | 380 encryption_handler()->ApplyNigoriUpdate( |
349 old_nigori, | 381 old_nigori, |
350 trans.GetWrappedTrans()); | 382 trans.GetWrappedTrans()); |
351 } | 383 } |
352 EXPECT_TRUE(cryptographer()->is_ready()); | 384 EXPECT_TRUE(GetCryptographer()->is_ready()); |
353 EXPECT_FALSE(cryptographer()->has_pending_keys()); | 385 EXPECT_FALSE(GetCryptographer()->has_pending_keys()); |
354 | 386 |
355 // Encryption handler should have posted a task to overwrite the old | 387 // Encryption handler should have posted a task to overwrite the old |
356 // specifics. | 388 // specifics. |
357 PumpLoop(); | 389 PumpLoop(); |
358 | 390 |
359 { | 391 { |
360 // The cryptographer should be able to decrypt both sets of keys and still | 392 // The cryptographer should be able to decrypt both sets of keys and still |
361 // be encrypting with the newest, and the encrypted types should be the | 393 // be encrypting with the newest, and the encrypted types should be the |
362 // most recent. | 394 // most recent. |
363 // In addition, the nigori node should match the current encryption state. | 395 // In addition, the nigori node should match the current encryption state. |
364 ReadTransaction trans(FROM_HERE, user_share()); | 396 ReadTransaction trans(FROM_HERE, user_share()); |
365 ReadNode nigori_node(&trans); | 397 ReadNode nigori_node(&trans); |
366 ASSERT_EQ(nigori_node.InitByTagLookup(ModelTypeToRootTag(NIGORI)), | 398 ASSERT_EQ(nigori_node.InitByTagLookup(ModelTypeToRootTag(NIGORI)), |
367 BaseNode::INIT_OK); | 399 BaseNode::INIT_OK); |
368 const sync_pb::NigoriSpecifics& nigori = nigori_node.GetNigoriSpecifics(); | 400 const sync_pb::NigoriSpecifics& nigori = nigori_node.GetNigoriSpecifics(); |
369 EXPECT_TRUE(cryptographer()->CanDecryptUsingDefaultKey( | 401 EXPECT_TRUE(GetCryptographer()->CanDecryptUsingDefaultKey( |
370 our_encrypted_specifics.encrypted())); | 402 our_encrypted_specifics.encrypted())); |
371 EXPECT_TRUE(cryptographer()->CanDecrypt( | 403 EXPECT_TRUE(GetCryptographer()->CanDecrypt( |
372 other_encrypted_specifics.encrypted())); | 404 other_encrypted_specifics.encrypted())); |
373 EXPECT_TRUE(cryptographer()->CanDecrypt(nigori.encrypted())); | 405 EXPECT_TRUE(GetCryptographer()->CanDecrypt(nigori.encrypted())); |
374 EXPECT_TRUE(nigori.encrypt_everything()); | 406 EXPECT_TRUE(nigori.encrypt_everything()); |
375 EXPECT_TRUE(cryptographer()->CanDecryptUsingDefaultKey(nigori.encrypted())); | 407 EXPECT_TRUE( |
| 408 GetCryptographer()->CanDecryptUsingDefaultKey(nigori.encrypted())); |
376 } | 409 } |
377 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); | 410 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); |
378 } | 411 } |
379 | 412 |
380 } // namespace syncer | 413 } // namespace syncer |
OLD | NEW |