Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(429)

Side by Side Diff: sync/internal_api/sync_encryption_handler_impl_unittest.cc

Issue 10844005: [Sync] Refactor GetEncryptedTypes usage. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase + add dcheck Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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_->GetCryptographerUnsafe();
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()->GetEncryptedTypesUnsafe()));
145 EXPECT_TRUE(encrypted_types.Equals( 147 EXPECT_TRUE(encrypted_types.Equals(
146 handler2.GetEncryptedTypes())); 148 handler2.GetEncryptedTypesUnsafe()));
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()->GetEncryptedTypesUnsafe()));
170 EXPECT_TRUE(encrypted_types.Equals(handler2.GetEncryptedTypes())); 174 EXPECT_TRUE(encrypted_types.Equals(handler2.GetEncryptedTypesUnsafe()));
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()->GetEncryptedTypesUnsafe()));
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 =
202 encryption_handler()->GetEncryptedTypesUnsafe();
195 for (ModelTypeSet::Iterator iter = real_types.First(); 203 for (ModelTypeSet::Iterator iter = real_types.First();
196 iter.Good(); iter.Inc()) { 204 iter.Good(); iter.Inc()) {
197 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) 205 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI)
198 EXPECT_TRUE(encrypted_types.Has(iter.Get())); 206 EXPECT_TRUE(encrypted_types.Has(iter.Get()));
199 else 207 else
200 EXPECT_FALSE(encrypted_types.Has(iter.Get())); 208 EXPECT_FALSE(encrypted_types.Has(iter.Get()));
201 } 209 }
202 210
203 encryption_handler()->UpdateEncryptedTypesFromNigori(specifics); 211 {
212 WriteTransaction trans(FROM_HERE, user_share());
213 encryption_handler()->UpdateEncryptedTypesFromNigori(
214 nigori,
215 trans.GetWrappedTrans());
216 }
204 217
205 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); 218 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
206 encrypted_types = encryption_handler()->GetEncryptedTypes(); 219 encrypted_types = encryption_handler()->GetEncryptedTypesUnsafe();
207 for (ModelTypeSet::Iterator iter = real_types.First(); 220 for (ModelTypeSet::Iterator iter = real_types.First();
208 iter.Good(); iter.Inc()) { 221 iter.Good(); iter.Inc()) {
209 EXPECT_TRUE(encrypted_types.Has(iter.Get())); 222 EXPECT_TRUE(encrypted_types.Has(iter.Get()));
210 } 223 }
211 224
212 // Receiving the nigori node again shouldn't trigger another notification. 225 // Receiving the nigori node again shouldn't trigger another notification.
213 Mock::VerifyAndClearExpectations(observer()); 226 Mock::VerifyAndClearExpectations(observer());
214 encryption_handler()->UpdateEncryptedTypesFromNigori(specifics); 227 {
228 WriteTransaction trans(FROM_HERE, user_share());
229 encryption_handler()->UpdateEncryptedTypesFromNigori(
230 nigori,
231 trans.GetWrappedTrans());
232 }
215 } 233 }
216 234
217 // Verify the encryption handler can detect an implicit encrypt everything state 235 // Verify the encryption handler can detect an implicit encrypt everything state
218 // (from clients that failed to write the encrypt everything field). 236 // (from clients that failed to write the encrypt everything field).
219 TEST_F(SyncEncryptionHandlerImplTest, EncryptEverythingImplicit) { 237 TEST_F(SyncEncryptionHandlerImplTest, EncryptEverythingImplicit) {
220 ModelTypeSet real_types = ModelTypeSet::All(); 238 ModelTypeSet real_types = ModelTypeSet::All();
221 sync_pb::NigoriSpecifics specifics; 239 sync_pb::NigoriSpecifics nigori;
222 specifics.set_encrypt_bookmarks(true); // Non-passwords = encrypt everything 240 nigori.set_encrypt_bookmarks(true); // Non-passwords = encrypt everything
223 241
224 EXPECT_CALL(*observer(), 242 EXPECT_CALL(*observer(),
225 OnEncryptedTypesChanged( 243 OnEncryptedTypesChanged(
226 HasModelTypes(ModelTypeSet::All()), true)); 244 HasModelTypes(ModelTypeSet::All()), true));
227 245
228 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); 246 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
229 ModelTypeSet encrypted_types = encryption_handler()->GetEncryptedTypes(); 247 ModelTypeSet encrypted_types =
248 encryption_handler()->GetEncryptedTypesUnsafe();
230 for (ModelTypeSet::Iterator iter = real_types.First(); 249 for (ModelTypeSet::Iterator iter = real_types.First();
231 iter.Good(); iter.Inc()) { 250 iter.Good(); iter.Inc()) {
232 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) 251 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI)
233 EXPECT_TRUE(encrypted_types.Has(iter.Get())); 252 EXPECT_TRUE(encrypted_types.Has(iter.Get()));
234 else 253 else
235 EXPECT_FALSE(encrypted_types.Has(iter.Get())); 254 EXPECT_FALSE(encrypted_types.Has(iter.Get()));
236 } 255 }
237 256
238 encryption_handler()->UpdateEncryptedTypesFromNigori(specifics); 257 {
258 WriteTransaction trans(FROM_HERE, user_share());
259 encryption_handler()->UpdateEncryptedTypesFromNigori(
260 nigori,
261 trans.GetWrappedTrans());
262 }
239 263
240 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); 264 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
241 encrypted_types = encryption_handler()->GetEncryptedTypes(); 265 encrypted_types = encryption_handler()->GetEncryptedTypesUnsafe();
242 for (ModelTypeSet::Iterator iter = real_types.First(); 266 for (ModelTypeSet::Iterator iter = real_types.First();
243 iter.Good(); iter.Inc()) { 267 iter.Good(); iter.Inc()) {
244 EXPECT_TRUE(encrypted_types.Has(iter.Get())); 268 EXPECT_TRUE(encrypted_types.Has(iter.Get()));
245 } 269 }
246 270
247 // Receiving a nigori node with encrypt everything explicitly set shouldn't 271 // Receiving a nigori node with encrypt everything explicitly set shouldn't
248 // trigger another notification. 272 // trigger another notification.
249 Mock::VerifyAndClearExpectations(observer()); 273 Mock::VerifyAndClearExpectations(observer());
250 specifics.set_encrypt_everything(true); 274 nigori.set_encrypt_everything(true);
251 encryption_handler()->UpdateEncryptedTypesFromNigori(specifics); 275 {
276 WriteTransaction trans(FROM_HERE, user_share());
277 encryption_handler()->UpdateEncryptedTypesFromNigori(
278 nigori,
279 trans.GetWrappedTrans());
280 }
252 } 281 }
253 282
254 // Verify the encryption handler can deal with new versions treating new types 283 // 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 284 // as Sensitive, and that it does not consider this an implicit encrypt
256 // everything case. 285 // everything case.
257 TEST_F(SyncEncryptionHandlerImplTest, UnknownSensitiveTypes) { 286 TEST_F(SyncEncryptionHandlerImplTest, UnknownSensitiveTypes) {
258 ModelTypeSet real_types = ModelTypeSet::All(); 287 ModelTypeSet real_types = ModelTypeSet::All();
259 sync_pb::NigoriSpecifics specifics; 288 sync_pb::NigoriSpecifics nigori;
260 specifics.set_encrypt_everything(false); 289 nigori.set_encrypt_everything(false);
261 specifics.set_encrypt_bookmarks(true); 290 nigori.set_encrypt_bookmarks(true);
262 291
263 ModelTypeSet expected_encrypted_types = 292 ModelTypeSet expected_encrypted_types =
264 SyncEncryptionHandler::SensitiveTypes(); 293 SyncEncryptionHandler::SensitiveTypes();
265 expected_encrypted_types.Put(BOOKMARKS); 294 expected_encrypted_types.Put(BOOKMARKS);
266 295
267 EXPECT_CALL(*observer(), 296 EXPECT_CALL(*observer(),
268 OnEncryptedTypesChanged( 297 OnEncryptedTypesChanged(
269 HasModelTypes(expected_encrypted_types), false)); 298 HasModelTypes(expected_encrypted_types), false));
270 299
271 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); 300 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
272 ModelTypeSet encrypted_types = encryption_handler()->GetEncryptedTypes(); 301 ModelTypeSet encrypted_types =
302 encryption_handler()->GetEncryptedTypesUnsafe();
273 for (ModelTypeSet::Iterator iter = real_types.First(); 303 for (ModelTypeSet::Iterator iter = real_types.First();
274 iter.Good(); iter.Inc()) { 304 iter.Good(); iter.Inc()) {
275 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) 305 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI)
276 EXPECT_TRUE(encrypted_types.Has(iter.Get())); 306 EXPECT_TRUE(encrypted_types.Has(iter.Get()));
277 else 307 else
278 EXPECT_FALSE(encrypted_types.Has(iter.Get())); 308 EXPECT_FALSE(encrypted_types.Has(iter.Get()));
279 } 309 }
280 310
281 encryption_handler()->UpdateEncryptedTypesFromNigori(specifics); 311 {
312 WriteTransaction trans(FROM_HERE, user_share());
313 encryption_handler()->UpdateEncryptedTypesFromNigori(
314 nigori,
315 trans.GetWrappedTrans());
316 }
282 317
283 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); 318 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
284 encrypted_types = encryption_handler()->GetEncryptedTypes(); 319 encrypted_types = encryption_handler()->GetEncryptedTypesUnsafe();
285 for (ModelTypeSet::Iterator iter = real_types.First(); 320 for (ModelTypeSet::Iterator iter = real_types.First();
286 iter.Good(); iter.Inc()) { 321 iter.Good(); iter.Inc()) {
287 if (iter.Get() == PASSWORDS || 322 if (iter.Get() == PASSWORDS ||
288 iter.Get() == NIGORI || 323 iter.Get() == NIGORI ||
289 iter.Get() == BOOKMARKS) 324 iter.Get() == BOOKMARKS)
290 EXPECT_TRUE(encrypted_types.Has(iter.Get())); 325 EXPECT_TRUE(encrypted_types.Has(iter.Get()));
291 else 326 else
292 EXPECT_FALSE(encrypted_types.Has(iter.Get())); 327 EXPECT_FALSE(encrypted_types.Has(iter.Get()));
293 } 328 }
294 } 329 }
295 330
296 // Receive an old nigori with old encryption keys and encrypted types. We should 331 // 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 332 // not revert our default key or encrypted types, and should post a task to
298 // overwrite the existing nigori with the correct data. 333 // overwrite the existing nigori with the correct data.
299 TEST_F(SyncEncryptionHandlerImplTest, ReceiveOldNigori) { 334 TEST_F(SyncEncryptionHandlerImplTest, ReceiveOldNigori) {
300 KeyParams old_key = {"localhost", "dummy", "old"}; 335 KeyParams old_key = {"localhost", "dummy", "old"};
301 KeyParams current_key = {"localhost", "dummy", "cur"}; 336 KeyParams current_key = {"localhost", "dummy", "cur"};
302 337
303 // Data for testing encryption/decryption. 338 // Data for testing encryption/decryption.
304 Cryptographer other_cryptographer(cryptographer()->encryptor()); 339 Cryptographer other_cryptographer(GetCryptographer()->encryptor());
305 other_cryptographer.AddKey(old_key); 340 other_cryptographer.AddKey(old_key);
306 sync_pb::EntitySpecifics other_encrypted_specifics; 341 sync_pb::EntitySpecifics other_encrypted_specifics;
307 other_encrypted_specifics.mutable_bookmark()->set_title("title"); 342 other_encrypted_specifics.mutable_bookmark()->set_title("title");
308 other_cryptographer.Encrypt( 343 other_cryptographer.Encrypt(
309 other_encrypted_specifics, 344 other_encrypted_specifics,
310 other_encrypted_specifics.mutable_encrypted()); 345 other_encrypted_specifics.mutable_encrypted());
311 sync_pb::EntitySpecifics our_encrypted_specifics; 346 sync_pb::EntitySpecifics our_encrypted_specifics;
312 our_encrypted_specifics.mutable_bookmark()->set_title("title2"); 347 our_encrypted_specifics.mutable_bookmark()->set_title("title2");
313 ModelTypeSet encrypted_types = ModelTypeSet::All(); 348 ModelTypeSet encrypted_types = ModelTypeSet::All();
314 349
315 // Set up the current encryption state (containing both keys and encrypt 350 // Set up the current encryption state (containing both keys and encrypt
316 // everything). 351 // everything).
317 sync_pb::NigoriSpecifics current_nigori_specifics; 352 sync_pb::NigoriSpecifics current_nigori_specifics;
318 cryptographer()->AddKey(old_key); 353 GetCryptographer()->AddKey(old_key);
319 cryptographer()->AddKey(current_key); 354 GetCryptographer()->AddKey(current_key);
320 cryptographer()->Encrypt( 355 GetCryptographer()->Encrypt(
321 our_encrypted_specifics, 356 our_encrypted_specifics,
322 our_encrypted_specifics.mutable_encrypted()); 357 our_encrypted_specifics.mutable_encrypted());
323 cryptographer()->GetKeys( 358 GetCryptographer()->GetKeys(
324 current_nigori_specifics.mutable_encrypted()); 359 current_nigori_specifics.mutable_encrypted());
325 current_nigori_specifics.set_encrypt_everything(true); 360 current_nigori_specifics.set_encrypt_everything(true);
326 361
327 EXPECT_CALL(*observer(), OnCryptographerStateChanged(_)); 362 EXPECT_CALL(*observer(), OnCryptographerStateChanged(_));
328 EXPECT_CALL(*observer(), OnEncryptedTypesChanged( 363 EXPECT_CALL(*observer(), OnEncryptedTypesChanged(
329 HasModelTypes(ModelTypeSet::All()), true)); 364 HasModelTypes(ModelTypeSet::All()), true));
330 { 365 {
331 // Update the encryption handler. 366 // Update the encryption handler.
332 WriteTransaction trans(FROM_HERE, user_share()); 367 WriteTransaction trans(FROM_HERE, user_share());
333 encryption_handler()->ApplyNigoriUpdate( 368 encryption_handler()->ApplyNigoriUpdate(
334 current_nigori_specifics, 369 current_nigori_specifics,
335 trans.GetWrappedTrans()); 370 trans.GetWrappedTrans());
336 } 371 }
337 Mock::VerifyAndClearExpectations(observer()); 372 Mock::VerifyAndClearExpectations(observer());
338 373
339 // Now set up the old nigori specifics and apply it on top. 374 // Now set up the old nigori specifics and apply it on top.
340 // Has an old set of keys, and no encrypted types. 375 // Has an old set of keys, and no encrypted types.
341 sync_pb::NigoriSpecifics old_nigori; 376 sync_pb::NigoriSpecifics old_nigori;
342 other_cryptographer.GetKeys(old_nigori.mutable_encrypted()); 377 other_cryptographer.GetKeys(old_nigori.mutable_encrypted());
343 378
344 EXPECT_CALL(*observer(), OnCryptographerStateChanged(_)); 379 EXPECT_CALL(*observer(), OnCryptographerStateChanged(_));
345 { 380 {
346 // Update the encryption handler. 381 // Update the encryption handler.
347 WriteTransaction trans(FROM_HERE, user_share()); 382 WriteTransaction trans(FROM_HERE, user_share());
348 encryption_handler()->ApplyNigoriUpdate( 383 encryption_handler()->ApplyNigoriUpdate(
349 old_nigori, 384 old_nigori,
350 trans.GetWrappedTrans()); 385 trans.GetWrappedTrans());
351 } 386 }
352 EXPECT_TRUE(cryptographer()->is_ready()); 387 EXPECT_TRUE(GetCryptographer()->is_ready());
353 EXPECT_FALSE(cryptographer()->has_pending_keys()); 388 EXPECT_FALSE(GetCryptographer()->has_pending_keys());
354 389
355 // Encryption handler should have posted a task to overwrite the old 390 // Encryption handler should have posted a task to overwrite the old
356 // specifics. 391 // specifics.
357 PumpLoop(); 392 PumpLoop();
358 393
359 { 394 {
360 // The cryptographer should be able to decrypt both sets of keys and still 395 // 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 396 // be encrypting with the newest, and the encrypted types should be the
362 // most recent. 397 // most recent.
363 // In addition, the nigori node should match the current encryption state. 398 // In addition, the nigori node should match the current encryption state.
364 ReadTransaction trans(FROM_HERE, user_share()); 399 ReadTransaction trans(FROM_HERE, user_share());
365 ReadNode nigori_node(&trans); 400 ReadNode nigori_node(&trans);
366 ASSERT_EQ(nigori_node.InitByTagLookup(ModelTypeToRootTag(NIGORI)), 401 ASSERT_EQ(nigori_node.InitByTagLookup(ModelTypeToRootTag(NIGORI)),
367 BaseNode::INIT_OK); 402 BaseNode::INIT_OK);
368 const sync_pb::NigoriSpecifics& nigori = nigori_node.GetNigoriSpecifics(); 403 const sync_pb::NigoriSpecifics& nigori = nigori_node.GetNigoriSpecifics();
369 EXPECT_TRUE(cryptographer()->CanDecryptUsingDefaultKey( 404 EXPECT_TRUE(GetCryptographer()->CanDecryptUsingDefaultKey(
370 our_encrypted_specifics.encrypted())); 405 our_encrypted_specifics.encrypted()));
371 EXPECT_TRUE(cryptographer()->CanDecrypt( 406 EXPECT_TRUE(GetCryptographer()->CanDecrypt(
372 other_encrypted_specifics.encrypted())); 407 other_encrypted_specifics.encrypted()));
373 EXPECT_TRUE(cryptographer()->CanDecrypt(nigori.encrypted())); 408 EXPECT_TRUE(GetCryptographer()->CanDecrypt(nigori.encrypted()));
374 EXPECT_TRUE(nigori.encrypt_everything()); 409 EXPECT_TRUE(nigori.encrypt_everything());
375 EXPECT_TRUE(cryptographer()->CanDecryptUsingDefaultKey(nigori.encrypted())); 410 EXPECT_TRUE(
411 GetCryptographer()->CanDecryptUsingDefaultKey(nigori.encrypted()));
376 } 412 }
377 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); 413 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
378 } 414 }
379 415
380 } // namespace syncer 416 } // namespace syncer
OLDNEW
« no previous file with comments | « sync/internal_api/sync_encryption_handler_impl.cc ('k') | sync/internal_api/sync_manager_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698