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