Roll WebRTC 9745:9761, Libjingle 9742:9761
[chromium-blink-merge.git] / sync / util / nigori_unittest.cc
blobc7b8d6fc8d2119a88e802ccc7b684519e78a9e35
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"
7 #include <string>
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/string_util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace syncer {
14 namespace {
16 TEST(SyncNigoriTest, Permute) {
17 Nigori nigori;
18 EXPECT_TRUE(nigori.InitByDerivation("example.com", "username", "password"));
20 std::string permuted;
21 EXPECT_TRUE(nigori.Permute(Nigori::Password, "test name",
22 &permuted));
24 std::string expected =
25 "prewwdJj2PrGDczvmsHJEE5ndcCyVze8sY9kD5hjY/Tm"
26 "c5kOjXFK7zB3Ss4LlHjEDirMu+vh85JwHOnGrMVe+g==";
27 EXPECT_EQ(expected, permuted);
30 TEST(SyncNigoriTest, PermuteIsConstant) {
31 Nigori nigori1;
32 EXPECT_TRUE(nigori1.InitByDerivation("example.com", "username", "password"));
34 std::string permuted1;
35 EXPECT_TRUE(nigori1.Permute(Nigori::Password,
36 "name",
37 &permuted1));
39 Nigori nigori2;
40 EXPECT_TRUE(nigori2.InitByDerivation("example.com", "username", "password"));
42 std::string permuted2;
43 EXPECT_TRUE(nigori2.Permute(Nigori::Password,
44 "name",
45 &permuted2));
47 EXPECT_LT(0U, permuted1.size());
48 EXPECT_EQ(permuted1, permuted2);
51 TEST(SyncNigoriTest, EncryptDifferentIv) {
52 Nigori nigori;
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) {
67 Nigori nigori;
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) {
82 Nigori nigori;
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) {
97 Nigori nigori;
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) {
115 Nigori nigori;
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 TEST(SyncNigoriTest, ExportImport) {
134 Nigori nigori1;
135 EXPECT_TRUE(nigori1.InitByDerivation("example.com", "username", "password"));
137 std::string user_key;
138 std::string encryption_key;
139 std::string mac_key;
140 EXPECT_TRUE(nigori1.ExportKeys(&user_key, &encryption_key, &mac_key));
142 Nigori nigori2;
143 EXPECT_TRUE(nigori2.InitByImport(user_key, encryption_key, mac_key));
145 std::string original("test");
146 std::string plaintext;
147 std::string ciphertext;
149 EXPECT_TRUE(nigori1.Encrypt(original, &ciphertext));
150 EXPECT_TRUE(nigori2.Decrypt(ciphertext, &plaintext));
151 EXPECT_EQ(original, plaintext);
153 EXPECT_TRUE(nigori2.Encrypt(original, &ciphertext));
154 EXPECT_TRUE(nigori1.Decrypt(ciphertext, &plaintext));
155 EXPECT_EQ(original, plaintext);
157 std::string permuted1, permuted2;
158 EXPECT_TRUE(nigori1.Permute(Nigori::Password, original, &permuted1));
159 EXPECT_TRUE(nigori2.Permute(Nigori::Password, original, &permuted2));
160 EXPECT_EQ(permuted1, permuted2);
163 } // anonymous namespace
164 } // namespace syncer