OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/sync/util/cryptographer.h" | 5 #include "chrome/browser/sync/util/cryptographer.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/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 | 80 |
81 sync_pb::EncryptedData encrypted; | 81 sync_pb::EncryptedData encrypted; |
82 EXPECT_TRUE(cryptographer.Encrypt(original, &encrypted)); | 82 EXPECT_TRUE(cryptographer.Encrypt(original, &encrypted)); |
83 | 83 |
84 sync_pb::PasswordSpecificsData decrypted; | 84 sync_pb::PasswordSpecificsData decrypted; |
85 EXPECT_TRUE(cryptographer.Decrypt(encrypted, &decrypted)); | 85 EXPECT_TRUE(cryptographer.Decrypt(encrypted, &decrypted)); |
86 | 86 |
87 EXPECT_EQ(original.SerializeAsString(), decrypted.SerializeAsString()); | 87 EXPECT_EQ(original.SerializeAsString(), decrypted.SerializeAsString()); |
88 } | 88 } |
89 | 89 |
| 90 TEST(CryptographerTest, EncryptOnlyIfDifferent) { |
| 91 Cryptographer cryptographer; |
| 92 |
| 93 KeyParams params = {"localhost", "dummy", "dummy"}; |
| 94 EXPECT_TRUE(cryptographer.AddKey(params)); |
| 95 EXPECT_TRUE(cryptographer.is_ready()); |
| 96 |
| 97 sync_pb::PasswordSpecificsData original; |
| 98 original.set_origin("http://example.com"); |
| 99 original.set_username_value("azure"); |
| 100 original.set_password_value("hunter2"); |
| 101 |
| 102 sync_pb::EncryptedData encrypted; |
| 103 EXPECT_TRUE(cryptographer.Encrypt(original, &encrypted)); |
| 104 |
| 105 sync_pb::EncryptedData encrypted2, encrypted3; |
| 106 encrypted2.CopyFrom(encrypted); |
| 107 encrypted3.CopyFrom(encrypted); |
| 108 EXPECT_TRUE(cryptographer.Encrypt(original, &encrypted2)); |
| 109 |
| 110 // Now encrypt with a new default key. Should overwrite the old data. |
| 111 KeyParams params_new = {"localhost", "dummy", "dummy2"}; |
| 112 cryptographer.AddKey(params_new); |
| 113 EXPECT_TRUE(cryptographer.Encrypt(original, &encrypted3)); |
| 114 |
| 115 sync_pb::PasswordSpecificsData decrypted; |
| 116 EXPECT_TRUE(cryptographer.Decrypt(encrypted2, &decrypted)); |
| 117 // encrypted2 should match encrypted, encrypted3 should not (due to salting). |
| 118 EXPECT_EQ(encrypted.SerializeAsString(), encrypted2.SerializeAsString()); |
| 119 EXPECT_NE(encrypted.SerializeAsString(), encrypted3.SerializeAsString()); |
| 120 EXPECT_EQ(original.SerializeAsString(), decrypted.SerializeAsString()); |
| 121 } |
| 122 |
90 TEST(CryptographerTest, AddKeySetsDefault) { | 123 TEST(CryptographerTest, AddKeySetsDefault) { |
91 Cryptographer cryptographer; | 124 Cryptographer cryptographer; |
92 | 125 |
93 KeyParams params1 = {"localhost", "dummy", "dummy1"}; | 126 KeyParams params1 = {"localhost", "dummy", "dummy1"}; |
94 EXPECT_TRUE(cryptographer.AddKey(params1)); | 127 EXPECT_TRUE(cryptographer.AddKey(params1)); |
95 EXPECT_TRUE(cryptographer.is_ready()); | 128 EXPECT_TRUE(cryptographer.is_ready()); |
96 | 129 |
97 sync_pb::PasswordSpecificsData original; | 130 sync_pb::PasswordSpecificsData original; |
98 original.set_origin("http://example.com"); | 131 original.set_origin("http://example.com"); |
99 original.set_username_value("azure"); | 132 original.set_username_value("azure"); |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 iter.Get() == syncable::BOOKMARKS) | 393 iter.Get() == syncable::BOOKMARKS) |
361 EXPECT_TRUE(encrypted_types.Has(iter.Get())); | 394 EXPECT_TRUE(encrypted_types.Has(iter.Get())); |
362 else | 395 else |
363 EXPECT_FALSE(encrypted_types.Has(iter.Get())); | 396 EXPECT_FALSE(encrypted_types.Has(iter.Get())); |
364 } | 397 } |
365 | 398 |
366 cryptographer.RemoveObserver(&observer); | 399 cryptographer.RemoveObserver(&observer); |
367 } | 400 } |
368 | 401 |
369 } // namespace browser_sync | 402 } // namespace browser_sync |
OLD | NEW |