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.
5 #include "sync/util/nigori.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/string_util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
16 TEST(SyncNigoriTest
, Permute
) {
18 EXPECT_TRUE(nigori
.InitByDerivation("example.com", "username", "password"));
21 EXPECT_TRUE(nigori
.Permute(Nigori::Password
, "test name",
24 std::string expected
=
25 "prewwdJj2PrGDczvmsHJEE5ndcCyVze8sY9kD5hjY/Tm"
26 "c5kOjXFK7zB3Ss4LlHjEDirMu+vh85JwHOnGrMVe+g==";
27 EXPECT_EQ(expected
, permuted
);
30 TEST(SyncNigoriTest
, PermuteIsConstant
) {
32 EXPECT_TRUE(nigori1
.InitByDerivation("example.com", "username", "password"));
34 std::string permuted1
;
35 EXPECT_TRUE(nigori1
.Permute(Nigori::Password
,
40 EXPECT_TRUE(nigori2
.InitByDerivation("example.com", "username", "password"));
42 std::string permuted2
;
43 EXPECT_TRUE(nigori2
.Permute(Nigori::Password
,
47 EXPECT_LT(0U, permuted1
.size());
48 EXPECT_EQ(permuted1
, permuted2
);
51 TEST(SyncNigoriTest
, EncryptDifferentIv
) {
53 EXPECT_TRUE(nigori
.InitByDerivation("example.com", "username", "password"));
55 std::string
plaintext("value");
57 std::string encrypted1
;
58 EXPECT_TRUE(nigori
.Encrypt(plaintext
, &encrypted1
));
60 std::string encrypted2
;
61 EXPECT_TRUE(nigori
.Encrypt(plaintext
, &encrypted2
));
63 EXPECT_NE(encrypted1
, encrypted2
);
66 TEST(SyncNigoriTest
, Decrypt
) {
68 EXPECT_TRUE(nigori
.InitByDerivation("example.com", "username", "password"));
70 std::string encrypted
=
71 "e7+JyS6ibj6F5qqvpseukNRTZ+oBpu5iuv2VYjOfrH1dNiFLNf7Ov0"
72 "kx/zicKFn0lJcbG1UmkNWqIuR4x+quDNVuLaZGbrJPhrJuj7cokCM=";
74 std::string plaintext
;
75 EXPECT_TRUE(nigori
.Decrypt(encrypted
, &plaintext
));
77 std::string
expected("test, test, 1, 2, 3");
78 EXPECT_EQ(expected
, plaintext
);
81 TEST(SyncNigoriTest
, EncryptDecrypt
) {
83 EXPECT_TRUE(nigori
.InitByDerivation("example.com", "username", "password"));
85 std::string
plaintext("value");
87 std::string encrypted
;
88 EXPECT_TRUE(nigori
.Encrypt(plaintext
, &encrypted
));
90 std::string decrypted
;
91 EXPECT_TRUE(nigori
.Decrypt(encrypted
, &decrypted
));
93 EXPECT_EQ(plaintext
, decrypted
);
96 TEST(SyncNigoriTest
, CorruptedIv
) {
98 EXPECT_TRUE(nigori
.InitByDerivation("example.com", "username", "password"));
100 std::string
plaintext("test");
102 std::string encrypted
;
103 EXPECT_TRUE(nigori
.Encrypt(plaintext
, &encrypted
));
105 // Corrupt the IV by changing one of its byte.
106 encrypted
[0] = (encrypted
[0] == 'a' ? 'b' : 'a');
108 std::string decrypted
;
109 EXPECT_TRUE(nigori
.Decrypt(encrypted
, &decrypted
));
111 EXPECT_NE(plaintext
, decrypted
);
114 TEST(SyncNigoriTest
, CorruptedCiphertext
) {
116 EXPECT_TRUE(nigori
.InitByDerivation("example.com", "username", "password"));
118 std::string
plaintext("test");
120 std::string encrypted
;
121 EXPECT_TRUE(nigori
.Encrypt(plaintext
, &encrypted
));
123 // Corrput the ciphertext by changing one of its bytes.
124 encrypted
[Nigori::kIvSize
+ 10] =
125 (encrypted
[Nigori::kIvSize
+ 10] == 'a' ? 'b' : 'a');
127 std::string decrypted
;
128 EXPECT_FALSE(nigori
.Decrypt(encrypted
, &decrypted
));
130 EXPECT_NE(plaintext
, decrypted
);
133 // Crashes, Bug 55180.
135 #define MAYBE_ExportImport DISABLED_ExportImport
137 #define MAYBE_ExportImport ExportImport
139 TEST(SyncNigoriTest
, MAYBE_ExportImport
) {
141 EXPECT_TRUE(nigori1
.InitByDerivation("example.com", "username", "password"));
143 std::string user_key
;
144 std::string encryption_key
;
146 EXPECT_TRUE(nigori1
.ExportKeys(&user_key
, &encryption_key
, &mac_key
));
149 EXPECT_TRUE(nigori2
.InitByImport(user_key
, encryption_key
, mac_key
));
151 std::string
original("test");
152 std::string plaintext
;
153 std::string ciphertext
;
155 EXPECT_TRUE(nigori1
.Encrypt(original
, &ciphertext
));
156 EXPECT_TRUE(nigori2
.Decrypt(ciphertext
, &plaintext
));
157 EXPECT_EQ(original
, plaintext
);
159 EXPECT_TRUE(nigori2
.Encrypt(original
, &ciphertext
));
160 EXPECT_TRUE(nigori1
.Decrypt(ciphertext
, &plaintext
));
161 EXPECT_EQ(original
, plaintext
);
163 std::string permuted1
, permuted2
;
164 EXPECT_TRUE(nigori1
.Permute(Nigori::Password
, original
, &permuted1
));
165 EXPECT_TRUE(nigori2
.Permute(Nigori::Password
, original
, &permuted2
));
166 EXPECT_EQ(permuted1
, permuted2
);
169 } // anonymous namespace
170 } // namespace syncer