| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/nigori.h" | 5 #include "chrome/browser/sync/util/nigori.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/scoped_ptr.h" | 9 #include "base/scoped_ptr.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 namespace browser_sync { |
| 14 namespace { |
| 15 |
| 13 TEST(NigoriTest, Parameters) { | 16 TEST(NigoriTest, Parameters) { |
| 14 browser_sync::Nigori nigori("example.com"); | 17 Nigori nigori; |
| 15 EXPECT_TRUE(nigori.Init("username", "password")); | 18 EXPECT_TRUE(nigori.InitByDerivation("example.com", "username", "password")); |
| 16 EXPECT_STREQ("example.com", nigori.hostname().c_str()); | 19 EXPECT_STREQ("example.com", nigori.hostname().c_str()); |
| 17 EXPECT_STREQ("username", nigori.username().c_str()); | 20 EXPECT_STREQ("username", nigori.username().c_str()); |
| 18 EXPECT_STREQ("password", nigori.password().c_str()); | 21 EXPECT_STREQ("password", nigori.password().c_str()); |
| 19 } | 22 } |
| 20 | 23 |
| 21 TEST(NigoriTest, Permute) { | 24 TEST(NigoriTest, Permute) { |
| 22 browser_sync::Nigori nigori("example.com"); | 25 Nigori nigori; |
| 23 EXPECT_TRUE(nigori.Init("username", "password")); | 26 EXPECT_TRUE(nigori.InitByDerivation("example.com", "username", "password")); |
| 24 | 27 |
| 25 std::string permuted; | 28 std::string permuted; |
| 26 EXPECT_TRUE(nigori.Permute(browser_sync::Nigori::Password, "test name", | 29 EXPECT_TRUE(nigori.Permute(Nigori::Password, "test name", |
| 27 &permuted)); | 30 &permuted)); |
| 28 | 31 |
| 29 std::string expected = | 32 std::string expected = |
| 30 "prewwdJj2PrGDczvmsHJEE5ndcCyVze8sY9kD5hjY/Tm" | 33 "prewwdJj2PrGDczvmsHJEE5ndcCyVze8sY9kD5hjY/Tm" |
| 31 "c5kOjXFK7zB3Ss4LlHjEDirMu+vh85JwHOnGrMVe+g=="; | 34 "c5kOjXFK7zB3Ss4LlHjEDirMu+vh85JwHOnGrMVe+g=="; |
| 32 EXPECT_EQ(expected, permuted); | 35 EXPECT_EQ(expected, permuted); |
| 33 } | 36 } |
| 34 | 37 |
| 35 TEST(NigoriTest, PermuteIsConstant) { | 38 TEST(NigoriTest, PermuteIsConstant) { |
| 36 browser_sync::Nigori nigori1("example.com"); | 39 Nigori nigori1; |
| 37 EXPECT_TRUE(nigori1.Init("username", "password")); | 40 EXPECT_TRUE(nigori1.InitByDerivation("example.com", "username", "password")); |
| 38 | 41 |
| 39 std::string permuted1; | 42 std::string permuted1; |
| 40 EXPECT_TRUE(nigori1.Permute(browser_sync::Nigori::Password, | 43 EXPECT_TRUE(nigori1.Permute(Nigori::Password, |
| 41 "name", | 44 "name", |
| 42 &permuted1)); | 45 &permuted1)); |
| 43 | 46 |
| 44 browser_sync::Nigori nigori2("example.com"); | 47 Nigori nigori2; |
| 45 EXPECT_TRUE(nigori2.Init("username", "password")); | 48 EXPECT_TRUE(nigori2.InitByDerivation("example.com", "username", "password")); |
| 46 | 49 |
| 47 std::string permuted2; | 50 std::string permuted2; |
| 48 EXPECT_TRUE(nigori2.Permute(browser_sync::Nigori::Password, | 51 EXPECT_TRUE(nigori2.Permute(Nigori::Password, |
| 49 "name", | 52 "name", |
| 50 &permuted2)); | 53 &permuted2)); |
| 51 | 54 |
| 52 EXPECT_LT(0U, permuted1.size()); | 55 EXPECT_LT(0U, permuted1.size()); |
| 53 EXPECT_EQ(permuted1, permuted2); | 56 EXPECT_EQ(permuted1, permuted2); |
| 54 } | 57 } |
| 55 | 58 |
| 56 TEST(NigoriTest, EncryptDifferentIv) { | 59 TEST(NigoriTest, EncryptDifferentIv) { |
| 57 browser_sync::Nigori nigori("example.com"); | 60 Nigori nigori; |
| 58 EXPECT_TRUE(nigori.Init("username", "password")); | 61 EXPECT_TRUE(nigori.InitByDerivation("example.com", "username", "password")); |
| 59 | 62 |
| 60 std::string plaintext("value"); | 63 std::string plaintext("value"); |
| 61 | 64 |
| 62 std::string encrypted1; | 65 std::string encrypted1; |
| 63 EXPECT_TRUE(nigori.Encrypt(plaintext, &encrypted1)); | 66 EXPECT_TRUE(nigori.Encrypt(plaintext, &encrypted1)); |
| 64 | 67 |
| 65 std::string encrypted2; | 68 std::string encrypted2; |
| 66 EXPECT_TRUE(nigori.Encrypt(plaintext, &encrypted2)); | 69 EXPECT_TRUE(nigori.Encrypt(plaintext, &encrypted2)); |
| 67 | 70 |
| 68 EXPECT_NE(encrypted1, encrypted2); | 71 EXPECT_NE(encrypted1, encrypted2); |
| 69 } | 72 } |
| 70 | 73 |
| 71 TEST(NigoriTest, Decrypt) { | 74 TEST(NigoriTest, Decrypt) { |
| 72 browser_sync::Nigori nigori("example.com"); | 75 Nigori nigori; |
| 73 EXPECT_TRUE(nigori.Init("username", "password")); | 76 EXPECT_TRUE(nigori.InitByDerivation("example.com", "username", "password")); |
| 74 | 77 |
| 75 std::string encrypted = | 78 std::string encrypted = |
| 76 "e7+JyS6ibj6F5qqvpseukNRTZ+oBpu5iuv2VYjOfrH1dNiFLNf7Ov0" | 79 "e7+JyS6ibj6F5qqvpseukNRTZ+oBpu5iuv2VYjOfrH1dNiFLNf7Ov0" |
| 77 "kx/zicKFn0lJcbG1UmkNWqIuR4x+quDNVuLaZGbrJPhrJuj7cokCM="; | 80 "kx/zicKFn0lJcbG1UmkNWqIuR4x+quDNVuLaZGbrJPhrJuj7cokCM="; |
| 78 | 81 |
| 79 std::string plaintext; | 82 std::string plaintext; |
| 80 EXPECT_TRUE(nigori.Decrypt(encrypted, &plaintext)); | 83 EXPECT_TRUE(nigori.Decrypt(encrypted, &plaintext)); |
| 81 | 84 |
| 82 std::string expected("test, test, 1, 2, 3"); | 85 std::string expected("test, test, 1, 2, 3"); |
| 83 EXPECT_EQ(expected, plaintext); | 86 EXPECT_EQ(expected, plaintext); |
| 84 } | 87 } |
| 85 | 88 |
| 86 TEST(NigoriTest, EncryptDecrypt) { | 89 TEST(NigoriTest, EncryptDecrypt) { |
| 87 browser_sync::Nigori nigori("example.com"); | 90 Nigori nigori; |
| 88 EXPECT_TRUE(nigori.Init("username", "password")); | 91 EXPECT_TRUE(nigori.InitByDerivation("example.com", "username", "password")); |
| 89 | 92 |
| 90 std::string plaintext("value"); | 93 std::string plaintext("value"); |
| 91 | 94 |
| 92 std::string encrypted; | 95 std::string encrypted; |
| 93 EXPECT_TRUE(nigori.Encrypt(plaintext, &encrypted)); | 96 EXPECT_TRUE(nigori.Encrypt(plaintext, &encrypted)); |
| 94 | 97 |
| 95 std::string decrypted; | 98 std::string decrypted; |
| 96 EXPECT_TRUE(nigori.Decrypt(encrypted, &decrypted)); | 99 EXPECT_TRUE(nigori.Decrypt(encrypted, &decrypted)); |
| 97 | 100 |
| 98 EXPECT_EQ(plaintext, decrypted); | 101 EXPECT_EQ(plaintext, decrypted); |
| 99 } | 102 } |
| 100 | 103 |
| 101 TEST(NigoriTest, CorruptedIv) { | 104 TEST(NigoriTest, CorruptedIv) { |
| 102 browser_sync::Nigori nigori("example.com"); | 105 Nigori nigori; |
| 103 EXPECT_TRUE(nigori.Init("username", "password")); | 106 EXPECT_TRUE(nigori.InitByDerivation("example.com", "username", "password")); |
| 104 | 107 |
| 105 std::string plaintext("test"); | 108 std::string plaintext("test"); |
| 106 | 109 |
| 107 std::string encrypted; | 110 std::string encrypted; |
| 108 EXPECT_TRUE(nigori.Encrypt(plaintext, &encrypted)); | 111 EXPECT_TRUE(nigori.Encrypt(plaintext, &encrypted)); |
| 109 | 112 |
| 110 // Corrupt the IV by changing one of its byte. | 113 // Corrupt the IV by changing one of its byte. |
| 111 encrypted[0] = (encrypted[0] == 'a' ? 'b' : 'a'); | 114 encrypted[0] = (encrypted[0] == 'a' ? 'b' : 'a'); |
| 112 | 115 |
| 113 std::string decrypted; | 116 std::string decrypted; |
| 114 EXPECT_TRUE(nigori.Decrypt(encrypted, &decrypted)); | 117 EXPECT_TRUE(nigori.Decrypt(encrypted, &decrypted)); |
| 115 | 118 |
| 116 EXPECT_NE(plaintext, decrypted); | 119 EXPECT_NE(plaintext, decrypted); |
| 117 } | 120 } |
| 118 | 121 |
| 119 TEST(NigoriTest, CorruptedCiphertext) { | 122 TEST(NigoriTest, CorruptedCiphertext) { |
| 120 browser_sync::Nigori nigori("example.com"); | 123 Nigori nigori; |
| 121 EXPECT_TRUE(nigori.Init("username", "password")); | 124 EXPECT_TRUE(nigori.InitByDerivation("example.com", "username", "password")); |
| 122 | 125 |
| 123 std::string plaintext("test"); | 126 std::string plaintext("test"); |
| 124 | 127 |
| 125 std::string encrypted; | 128 std::string encrypted; |
| 126 EXPECT_TRUE(nigori.Encrypt(plaintext, &encrypted)); | 129 EXPECT_TRUE(nigori.Encrypt(plaintext, &encrypted)); |
| 127 | 130 |
| 128 // Corrput the ciphertext by changing one of its bytes. | 131 // Corrput the ciphertext by changing one of its bytes. |
| 129 encrypted[browser_sync::Nigori::kIvSize + 10] = | 132 encrypted[Nigori::kIvSize + 10] = |
| 130 (encrypted[browser_sync::Nigori::kIvSize + 10] == 'a' ? 'b' : 'a'); | 133 (encrypted[Nigori::kIvSize + 10] == 'a' ? 'b' : 'a'); |
| 131 | 134 |
| 132 std::string decrypted; | 135 std::string decrypted; |
| 133 EXPECT_FALSE(nigori.Decrypt(encrypted, &decrypted)); | 136 EXPECT_FALSE(nigori.Decrypt(encrypted, &decrypted)); |
| 134 | 137 |
| 135 EXPECT_NE(plaintext, decrypted); | 138 EXPECT_NE(plaintext, decrypted); |
| 136 } | 139 } |
| 140 |
| 141 TEST(NigoriTest, ExportImport) { |
| 142 Nigori nigori1; |
| 143 EXPECT_TRUE(nigori1.InitByDerivation("example.com", "username", "password")); |
| 144 |
| 145 std::string user_key; |
| 146 std::string encryption_key; |
| 147 std::string mac_key; |
| 148 EXPECT_TRUE(nigori1.ExportKeys(&user_key, &encryption_key, &mac_key)); |
| 149 |
| 150 Nigori nigori2; |
| 151 EXPECT_TRUE(nigori2.InitByImport(user_key, encryption_key, mac_key)); |
| 152 |
| 153 std::string original("test"); |
| 154 std::string plaintext; |
| 155 std::string ciphertext; |
| 156 |
| 157 EXPECT_TRUE(nigori1.Encrypt(original, &ciphertext)); |
| 158 EXPECT_TRUE(nigori2.Decrypt(ciphertext, &plaintext)); |
| 159 EXPECT_EQ(original, plaintext); |
| 160 |
| 161 EXPECT_TRUE(nigori2.Encrypt(original, &ciphertext)); |
| 162 EXPECT_TRUE(nigori1.Decrypt(ciphertext, &plaintext)); |
| 163 EXPECT_EQ(original, plaintext); |
| 164 |
| 165 std::string permuted1, permuted2; |
| 166 EXPECT_TRUE(nigori1.Permute(Nigori::Password, original, &permuted1)); |
| 167 EXPECT_TRUE(nigori2.Permute(Nigori::Password, original, &permuted2)); |
| 168 EXPECT_EQ(permuted1, permuted2); |
| 169 } |
| 170 |
| 171 } // anonymous namespace |
| 172 } // namespace browser_sync |
| OLD | NEW |