| 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" |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 EXPECT_TRUE(encrypted_types.Equals( | 146 EXPECT_TRUE(encrypted_types.Equals( |
| 147 encryption_handler()->GetEncryptedTypesUnsafe())); | 147 encryption_handler()->GetEncryptedTypesUnsafe())); |
| 148 EXPECT_TRUE(encrypted_types.Equals( | 148 EXPECT_TRUE(encrypted_types.Equals( |
| 149 handler2.GetEncryptedTypesUnsafe())); | 149 handler2.GetEncryptedTypesUnsafe())); |
| 150 | 150 |
| 151 Mock::VerifyAndClearExpectations(observer()); | 151 Mock::VerifyAndClearExpectations(observer()); |
| 152 Mock::VerifyAndClearExpectations(&observer2); | 152 Mock::VerifyAndClearExpectations(&observer2); |
| 153 | 153 |
| 154 EXPECT_CALL(*observer(), | 154 EXPECT_CALL(*observer(), |
| 155 OnEncryptedTypesChanged( | 155 OnEncryptedTypesChanged( |
| 156 HasModelTypes(ModelTypeSet::All()), false)); | 156 HasModelTypes(UserTypes()), false)); |
| 157 EXPECT_CALL(observer2, | 157 EXPECT_CALL(observer2, |
| 158 OnEncryptedTypesChanged( | 158 OnEncryptedTypesChanged( |
| 159 HasModelTypes(ModelTypeSet::All()), false)); | 159 HasModelTypes(UserTypes()), false)); |
| 160 | 160 |
| 161 // Set all encrypted types | 161 // Set all encrypted types |
| 162 encrypted_types = ModelTypeSet::All(); | 162 encrypted_types = UserTypes(); |
| 163 { | 163 { |
| 164 WriteTransaction trans(FROM_HERE, user_share()); | 164 WriteTransaction trans(FROM_HERE, user_share()); |
| 165 encryption_handler()->MergeEncryptedTypes( | 165 encryption_handler()->MergeEncryptedTypes( |
| 166 encrypted_types, | 166 encrypted_types, |
| 167 trans.GetWrappedTrans()); | 167 trans.GetWrappedTrans()); |
| 168 encryption_handler()->UpdateNigoriFromEncryptedTypes( | 168 encryption_handler()->UpdateNigoriFromEncryptedTypes( |
| 169 &nigori, | 169 &nigori, |
| 170 trans.GetWrappedTrans()); | 170 trans.GetWrappedTrans()); |
| 171 handler2.UpdateEncryptedTypesFromNigori(nigori, trans.GetWrappedTrans()); | 171 handler2.UpdateEncryptedTypesFromNigori(nigori, trans.GetWrappedTrans()); |
| 172 } | 172 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 183 WriteTransaction trans(FROM_HERE, user_share()); | 183 WriteTransaction trans(FROM_HERE, user_share()); |
| 184 handler2.UpdateEncryptedTypesFromNigori(nigori, trans.GetWrappedTrans()); | 184 handler2.UpdateEncryptedTypesFromNigori(nigori, trans.GetWrappedTrans()); |
| 185 } | 185 } |
| 186 EXPECT_TRUE(encrypted_types.Equals( | 186 EXPECT_TRUE(encrypted_types.Equals( |
| 187 encryption_handler()->GetEncryptedTypesUnsafe())); | 187 encryption_handler()->GetEncryptedTypesUnsafe())); |
| 188 } | 188 } |
| 189 | 189 |
| 190 // Verify the encryption handler processes the encrypt everything field | 190 // Verify the encryption handler processes the encrypt everything field |
| 191 // properly. | 191 // properly. |
| 192 TEST_F(SyncEncryptionHandlerImplTest, EncryptEverythingExplicit) { | 192 TEST_F(SyncEncryptionHandlerImplTest, EncryptEverythingExplicit) { |
| 193 ModelTypeSet real_types = ModelTypeSet::All(); | |
| 194 sync_pb::NigoriSpecifics nigori; | 193 sync_pb::NigoriSpecifics nigori; |
| 195 nigori.set_encrypt_everything(true); | 194 nigori.set_encrypt_everything(true); |
| 196 | 195 |
| 197 EXPECT_CALL(*observer(), | 196 EXPECT_CALL(*observer(), |
| 198 OnEncryptedTypesChanged( | 197 OnEncryptedTypesChanged( |
| 199 HasModelTypes(ModelTypeSet::All()), true)); | 198 HasModelTypes(UserTypes()), true)); |
| 200 | 199 |
| 201 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); | 200 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); |
| 202 ModelTypeSet encrypted_types = | 201 ModelTypeSet encrypted_types = |
| 203 encryption_handler()->GetEncryptedTypesUnsafe(); | 202 encryption_handler()->GetEncryptedTypesUnsafe(); |
| 204 for (ModelTypeSet::Iterator iter = real_types.First(); | 203 EXPECT_TRUE(encrypted_types.Equals(ModelTypeSet(PASSWORDS))); |
| 205 iter.Good(); iter.Inc()) { | |
| 206 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) | |
| 207 EXPECT_TRUE(encrypted_types.Has(iter.Get())); | |
| 208 else | |
| 209 EXPECT_FALSE(encrypted_types.Has(iter.Get())); | |
| 210 } | |
| 211 | 204 |
| 212 { | 205 { |
| 213 WriteTransaction trans(FROM_HERE, user_share()); | 206 WriteTransaction trans(FROM_HERE, user_share()); |
| 214 encryption_handler()->UpdateEncryptedTypesFromNigori( | 207 encryption_handler()->UpdateEncryptedTypesFromNigori( |
| 215 nigori, | 208 nigori, |
| 216 trans.GetWrappedTrans()); | 209 trans.GetWrappedTrans()); |
| 217 } | 210 } |
| 218 | 211 |
| 219 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); | 212 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); |
| 220 encrypted_types = encryption_handler()->GetEncryptedTypesUnsafe(); | 213 encrypted_types = encryption_handler()->GetEncryptedTypesUnsafe(); |
| 221 for (ModelTypeSet::Iterator iter = real_types.First(); | 214 EXPECT_TRUE(encrypted_types.HasAll(UserTypes())); |
| 222 iter.Good(); iter.Inc()) { | |
| 223 EXPECT_TRUE(encrypted_types.Has(iter.Get())); | |
| 224 } | |
| 225 | 215 |
| 226 // Receiving the nigori node again shouldn't trigger another notification. | 216 // Receiving the nigori node again shouldn't trigger another notification. |
| 227 Mock::VerifyAndClearExpectations(observer()); | 217 Mock::VerifyAndClearExpectations(observer()); |
| 228 { | 218 { |
| 229 WriteTransaction trans(FROM_HERE, user_share()); | 219 WriteTransaction trans(FROM_HERE, user_share()); |
| 230 encryption_handler()->UpdateEncryptedTypesFromNigori( | 220 encryption_handler()->UpdateEncryptedTypesFromNigori( |
| 231 nigori, | 221 nigori, |
| 232 trans.GetWrappedTrans()); | 222 trans.GetWrappedTrans()); |
| 233 } | 223 } |
| 234 } | 224 } |
| 235 | 225 |
| 236 // Verify the encryption handler can detect an implicit encrypt everything state | 226 // Verify the encryption handler can detect an implicit encrypt everything state |
| 237 // (from clients that failed to write the encrypt everything field). | 227 // (from clients that failed to write the encrypt everything field). |
| 238 TEST_F(SyncEncryptionHandlerImplTest, EncryptEverythingImplicit) { | 228 TEST_F(SyncEncryptionHandlerImplTest, EncryptEverythingImplicit) { |
| 239 ModelTypeSet real_types = ModelTypeSet::All(); | |
| 240 sync_pb::NigoriSpecifics nigori; | 229 sync_pb::NigoriSpecifics nigori; |
| 241 nigori.set_encrypt_bookmarks(true); // Non-passwords = encrypt everything | 230 nigori.set_encrypt_bookmarks(true); // Non-passwords = encrypt everything |
| 242 | 231 |
| 243 EXPECT_CALL(*observer(), | 232 EXPECT_CALL(*observer(), |
| 244 OnEncryptedTypesChanged( | 233 OnEncryptedTypesChanged( |
| 245 HasModelTypes(ModelTypeSet::All()), true)); | 234 HasModelTypes(UserTypes()), true)); |
| 246 | 235 |
| 247 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); | 236 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); |
| 248 ModelTypeSet encrypted_types = | 237 ModelTypeSet encrypted_types = |
| 249 encryption_handler()->GetEncryptedTypesUnsafe(); | 238 encryption_handler()->GetEncryptedTypesUnsafe(); |
| 250 for (ModelTypeSet::Iterator iter = real_types.First(); | 239 EXPECT_TRUE(encrypted_types.Equals(ModelTypeSet(PASSWORDS))); |
| 251 iter.Good(); iter.Inc()) { | |
| 252 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) | |
| 253 EXPECT_TRUE(encrypted_types.Has(iter.Get())); | |
| 254 else | |
| 255 EXPECT_FALSE(encrypted_types.Has(iter.Get())); | |
| 256 } | |
| 257 | 240 |
| 258 { | 241 { |
| 259 WriteTransaction trans(FROM_HERE, user_share()); | 242 WriteTransaction trans(FROM_HERE, user_share()); |
| 260 encryption_handler()->UpdateEncryptedTypesFromNigori( | 243 encryption_handler()->UpdateEncryptedTypesFromNigori( |
| 261 nigori, | 244 nigori, |
| 262 trans.GetWrappedTrans()); | 245 trans.GetWrappedTrans()); |
| 263 } | 246 } |
| 264 | 247 |
| 265 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); | 248 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); |
| 266 encrypted_types = encryption_handler()->GetEncryptedTypesUnsafe(); | 249 encrypted_types = encryption_handler()->GetEncryptedTypesUnsafe(); |
| 267 for (ModelTypeSet::Iterator iter = real_types.First(); | 250 EXPECT_TRUE(encrypted_types.HasAll(UserTypes())); |
| 268 iter.Good(); iter.Inc()) { | |
| 269 EXPECT_TRUE(encrypted_types.Has(iter.Get())); | |
| 270 } | |
| 271 | 251 |
| 272 // Receiving a nigori node with encrypt everything explicitly set shouldn't | 252 // Receiving a nigori node with encrypt everything explicitly set shouldn't |
| 273 // trigger another notification. | 253 // trigger another notification. |
| 274 Mock::VerifyAndClearExpectations(observer()); | 254 Mock::VerifyAndClearExpectations(observer()); |
| 275 nigori.set_encrypt_everything(true); | 255 nigori.set_encrypt_everything(true); |
| 276 { | 256 { |
| 277 WriteTransaction trans(FROM_HERE, user_share()); | 257 WriteTransaction trans(FROM_HERE, user_share()); |
| 278 encryption_handler()->UpdateEncryptedTypesFromNigori( | 258 encryption_handler()->UpdateEncryptedTypesFromNigori( |
| 279 nigori, | 259 nigori, |
| 280 trans.GetWrappedTrans()); | 260 trans.GetWrappedTrans()); |
| 281 } | 261 } |
| 282 } | 262 } |
| 283 | 263 |
| 284 // Verify the encryption handler can deal with new versions treating new types | 264 // Verify the encryption handler can deal with new versions treating new types |
| 285 // as Sensitive, and that it does not consider this an implicit encrypt | 265 // as Sensitive, and that it does not consider this an implicit encrypt |
| 286 // everything case. | 266 // everything case. |
| 287 TEST_F(SyncEncryptionHandlerImplTest, UnknownSensitiveTypes) { | 267 TEST_F(SyncEncryptionHandlerImplTest, UnknownSensitiveTypes) { |
| 288 ModelTypeSet real_types = ModelTypeSet::All(); | |
| 289 sync_pb::NigoriSpecifics nigori; | 268 sync_pb::NigoriSpecifics nigori; |
| 290 nigori.set_encrypt_everything(false); | 269 nigori.set_encrypt_everything(false); |
| 291 nigori.set_encrypt_bookmarks(true); | 270 nigori.set_encrypt_bookmarks(true); |
| 292 | 271 |
| 293 ModelTypeSet expected_encrypted_types = | 272 ModelTypeSet expected_encrypted_types = |
| 294 SyncEncryptionHandler::SensitiveTypes(); | 273 SyncEncryptionHandler::SensitiveTypes(); |
| 295 expected_encrypted_types.Put(BOOKMARKS); | 274 expected_encrypted_types.Put(BOOKMARKS); |
| 296 | 275 |
| 297 EXPECT_CALL(*observer(), | 276 EXPECT_CALL(*observer(), |
| 298 OnEncryptedTypesChanged( | 277 OnEncryptedTypesChanged( |
| 299 HasModelTypes(expected_encrypted_types), false)); | 278 HasModelTypes(expected_encrypted_types), false)); |
| 300 | 279 |
| 301 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); | 280 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); |
| 302 ModelTypeSet encrypted_types = | 281 ModelTypeSet encrypted_types = |
| 303 encryption_handler()->GetEncryptedTypesUnsafe(); | 282 encryption_handler()->GetEncryptedTypesUnsafe(); |
| 304 for (ModelTypeSet::Iterator iter = real_types.First(); | 283 EXPECT_TRUE(encrypted_types.Equals(ModelTypeSet(PASSWORDS))); |
| 305 iter.Good(); iter.Inc()) { | |
| 306 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) | |
| 307 EXPECT_TRUE(encrypted_types.Has(iter.Get())); | |
| 308 else | |
| 309 EXPECT_FALSE(encrypted_types.Has(iter.Get())); | |
| 310 } | |
| 311 | 284 |
| 312 { | 285 { |
| 313 WriteTransaction trans(FROM_HERE, user_share()); | 286 WriteTransaction trans(FROM_HERE, user_share()); |
| 314 encryption_handler()->UpdateEncryptedTypesFromNigori( | 287 encryption_handler()->UpdateEncryptedTypesFromNigori( |
| 315 nigori, | 288 nigori, |
| 316 trans.GetWrappedTrans()); | 289 trans.GetWrappedTrans()); |
| 317 } | 290 } |
| 318 | 291 |
| 319 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); | 292 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled()); |
| 320 encrypted_types = encryption_handler()->GetEncryptedTypesUnsafe(); | 293 encrypted_types = encryption_handler()->GetEncryptedTypesUnsafe(); |
| 321 for (ModelTypeSet::Iterator iter = real_types.First(); | 294 EXPECT_TRUE(encrypted_types.Equals(ModelTypeSet(BOOKMARKS, PASSWORDS))); |
| 322 iter.Good(); iter.Inc()) { | |
| 323 if (iter.Get() == PASSWORDS || | |
| 324 iter.Get() == NIGORI || | |
| 325 iter.Get() == BOOKMARKS) | |
| 326 EXPECT_TRUE(encrypted_types.Has(iter.Get())); | |
| 327 else | |
| 328 EXPECT_FALSE(encrypted_types.Has(iter.Get())); | |
| 329 } | |
| 330 } | 295 } |
| 331 | 296 |
| 332 // Receive an old nigori with old encryption keys and encrypted types. We should | 297 // Receive an old nigori with old encryption keys and encrypted types. We should |
| 333 // not revert our default key or encrypted types, and should post a task to | 298 // not revert our default key or encrypted types, and should post a task to |
| 334 // overwrite the existing nigori with the correct data. | 299 // overwrite the existing nigori with the correct data. |
| 335 TEST_F(SyncEncryptionHandlerImplTest, ReceiveOldNigori) { | 300 TEST_F(SyncEncryptionHandlerImplTest, ReceiveOldNigori) { |
| 336 KeyParams old_key = {"localhost", "dummy", "old"}; | 301 KeyParams old_key = {"localhost", "dummy", "old"}; |
| 337 KeyParams current_key = {"localhost", "dummy", "cur"}; | 302 KeyParams current_key = {"localhost", "dummy", "cur"}; |
| 338 | 303 |
| 339 // Data for testing encryption/decryption. | 304 // Data for testing encryption/decryption. |
| 340 Cryptographer other_cryptographer(GetCryptographer()->encryptor()); | 305 Cryptographer other_cryptographer(GetCryptographer()->encryptor()); |
| 341 other_cryptographer.AddKey(old_key); | 306 other_cryptographer.AddKey(old_key); |
| 342 sync_pb::EntitySpecifics other_encrypted_specifics; | 307 sync_pb::EntitySpecifics other_encrypted_specifics; |
| 343 other_encrypted_specifics.mutable_bookmark()->set_title("title"); | 308 other_encrypted_specifics.mutable_bookmark()->set_title("title"); |
| 344 other_cryptographer.Encrypt( | 309 other_cryptographer.Encrypt( |
| 345 other_encrypted_specifics, | 310 other_encrypted_specifics, |
| 346 other_encrypted_specifics.mutable_encrypted()); | 311 other_encrypted_specifics.mutable_encrypted()); |
| 347 sync_pb::EntitySpecifics our_encrypted_specifics; | 312 sync_pb::EntitySpecifics our_encrypted_specifics; |
| 348 our_encrypted_specifics.mutable_bookmark()->set_title("title2"); | 313 our_encrypted_specifics.mutable_bookmark()->set_title("title2"); |
| 349 ModelTypeSet encrypted_types = ModelTypeSet::All(); | 314 ModelTypeSet encrypted_types = UserTypes(); |
| 350 | 315 |
| 351 // Set up the current encryption state (containing both keys and encrypt | 316 // Set up the current encryption state (containing both keys and encrypt |
| 352 // everything). | 317 // everything). |
| 353 sync_pb::NigoriSpecifics current_nigori_specifics; | 318 sync_pb::NigoriSpecifics current_nigori_specifics; |
| 354 GetCryptographer()->AddKey(old_key); | 319 GetCryptographer()->AddKey(old_key); |
| 355 GetCryptographer()->AddKey(current_key); | 320 GetCryptographer()->AddKey(current_key); |
| 356 GetCryptographer()->Encrypt( | 321 GetCryptographer()->Encrypt( |
| 357 our_encrypted_specifics, | 322 our_encrypted_specifics, |
| 358 our_encrypted_specifics.mutable_encrypted()); | 323 our_encrypted_specifics.mutable_encrypted()); |
| 359 GetCryptographer()->GetKeys( | 324 GetCryptographer()->GetKeys( |
| 360 current_nigori_specifics.mutable_encrypted()); | 325 current_nigori_specifics.mutable_encrypted()); |
| 361 current_nigori_specifics.set_encrypt_everything(true); | 326 current_nigori_specifics.set_encrypt_everything(true); |
| 362 | 327 |
| 363 EXPECT_CALL(*observer(), OnCryptographerStateChanged(_)); | 328 EXPECT_CALL(*observer(), OnCryptographerStateChanged(_)); |
| 364 EXPECT_CALL(*observer(), OnEncryptedTypesChanged( | 329 EXPECT_CALL(*observer(), OnEncryptedTypesChanged( |
| 365 HasModelTypes(ModelTypeSet::All()), true)); | 330 HasModelTypes(UserTypes()), true)); |
| 366 { | 331 { |
| 367 // Update the encryption handler. | 332 // Update the encryption handler. |
| 368 WriteTransaction trans(FROM_HERE, user_share()); | 333 WriteTransaction trans(FROM_HERE, user_share()); |
| 369 encryption_handler()->ApplyNigoriUpdate( | 334 encryption_handler()->ApplyNigoriUpdate( |
| 370 current_nigori_specifics, | 335 current_nigori_specifics, |
| 371 trans.GetWrappedTrans()); | 336 trans.GetWrappedTrans()); |
| 372 } | 337 } |
| 373 Mock::VerifyAndClearExpectations(observer()); | 338 Mock::VerifyAndClearExpectations(observer()); |
| 374 | 339 |
| 375 // Now set up the old nigori specifics and apply it on top. | 340 // Now set up the old nigori specifics and apply it on top. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 other_encrypted_specifics.encrypted())); | 373 other_encrypted_specifics.encrypted())); |
| 409 EXPECT_TRUE(GetCryptographer()->CanDecrypt(nigori.encrypted())); | 374 EXPECT_TRUE(GetCryptographer()->CanDecrypt(nigori.encrypted())); |
| 410 EXPECT_TRUE(nigori.encrypt_everything()); | 375 EXPECT_TRUE(nigori.encrypt_everything()); |
| 411 EXPECT_TRUE( | 376 EXPECT_TRUE( |
| 412 GetCryptographer()->CanDecryptUsingDefaultKey(nigori.encrypted())); | 377 GetCryptographer()->CanDecryptUsingDefaultKey(nigori.encrypted())); |
| 413 } | 378 } |
| 414 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); | 379 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); |
| 415 } | 380 } |
| 416 | 381 |
| 417 } // namespace syncer | 382 } // namespace syncer |
| OLD | NEW |