Refactor Windows MessageBox fallback code.
[chromium-blink-merge.git] / sync / util / nigori_unittest.cc
blob88c2461231a14c03964dde691bcadd0eb17dc734
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 // Crashes, Bug 55180.
134 #if defined(OS_WIN)
135 #define MAYBE_ExportImport DISABLED_ExportImport
136 #else
137 #define MAYBE_ExportImport ExportImport
138 #endif
139 TEST(SyncNigoriTest, MAYBE_ExportImport) {
140 Nigori nigori1;
141 EXPECT_TRUE(nigori1.InitByDerivation("example.com", "username", "password"));
143 std::string user_key;
144 std::string encryption_key;
145 std::string mac_key;
146 EXPECT_TRUE(nigori1.ExportKeys(&user_key, &encryption_key, &mac_key));
148 Nigori nigori2;
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