OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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 "chromeos/dbus/fake_easy_unlock_client.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/bind.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 namespace { | |
13 | |
14 void RecordKeyPair(std::string* private_key_target, | |
satorux1
2014/08/29 01:10:50
function comment is missing.
tbarzic
2014/08/29 02:47:05
Done.
| |
15 std::string* public_key_target, | |
16 const std::string& private_key_source, | |
17 const std::string& public_key_source) { | |
18 *private_key_target = private_key_source; | |
19 *public_key_target = public_key_source; | |
20 } | |
21 | |
22 void RecordData(std::string* data_target, | |
satorux1
2014/08/29 01:10:50
ditto. just a one liner would suffice
tbarzic
2014/08/29 02:47:05
Done.
| |
23 const std::string& data_source) { | |
24 *data_target = data_source; | |
25 } | |
26 | |
27 TEST(FakeEasyUnlockClientTest, GenerateEcP256KeyPair) { | |
28 chromeos::FakeEasyUnlockClient client; | |
29 | |
30 std::string private_key_1; | |
31 std::string public_key_1; | |
32 client.GenerateEcP256KeyPair( | |
33 base::Bind(&RecordKeyPair, &private_key_1, &public_key_1)); | |
34 ASSERT_EQ("{\"ec_p256_private_key\": 1}", private_key_1); | |
35 ASSERT_EQ("{\"ec_p256_public_key\": 1}", public_key_1); | |
36 | |
37 std::string private_key_2; | |
38 std::string public_key_2; | |
39 client.GenerateEcP256KeyPair( | |
40 base::Bind(&RecordKeyPair, &private_key_2, &public_key_2)); | |
41 ASSERT_EQ("{\"ec_p256_private_key\": 2}", private_key_2); | |
42 ASSERT_EQ("{\"ec_p256_public_key\": 2}", public_key_2); | |
43 | |
44 EXPECT_NE(private_key_1, private_key_2); | |
45 EXPECT_NE(public_key_1, public_key_2); | |
46 } | |
47 | |
48 TEST(FakeEasyUnlockClientTest, IsEcP256KeyPair) { | |
49 ASSERT_TRUE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair( | |
50 "{\"ec_p256_private_key\": 12}", | |
51 "{\"ec_p256_public_key\": 12}")); | |
52 } | |
53 | |
54 TEST(FakeEasyUnlockClientTest, IsEcP256KeyPair_KeysFromDiffrentPairs) { | |
55 ASSERT_FALSE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair( | |
56 "{\"ec_p256_private_key\": 12}", | |
57 "{\"ec_p256_public_key\": 34}")); | |
58 } | |
59 | |
60 TEST(FakeEasyUnlockClientTest, IsEcP256KeyPair_KeyOrderSwitched) { | |
61 ASSERT_FALSE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair( | |
62 "{\"ec_p256_public_key\": 34}", | |
63 "{\"ec_p256_private_key\": 34}")); | |
64 } | |
65 | |
66 TEST(FakeEasyUnlockClientTest, IsEcP256KeyPair_PrivateKeyInvalidFormat) { | |
67 ASSERT_FALSE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair( | |
68 "\"ec_p256_private_key\": 12", | |
69 "{\"ec_p256_public_key\": 12}")); | |
70 } | |
71 | |
72 TEST(FakeEasyUnlockClientTest, IsEcP256KeyPair_PublicKeyInvalidFormat) { | |
73 ASSERT_FALSE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair( | |
74 "{\"ec_p256_private_key\": 12}", | |
75 "\"ec_p256_public_key\": 12")); | |
76 } | |
77 | |
78 TEST(FakeEasyUnlockClientTest, IsEcP256KeyPair_PrivateKeyInvalidDictKey) { | |
79 ASSERT_FALSE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair( | |
80 "{\"invalid\": 12}", | |
81 "{\"ec_p256_public_key\": 12}")); | |
82 } | |
83 | |
84 TEST(FakeEasyUnlockClientTest, IsEcP256KeyPair_PublicKeyInvalidDictKey) { | |
85 ASSERT_FALSE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair( | |
86 "{\"ec_p256_private_key\": 12}", | |
87 "{\"invalid\": 12}")); | |
88 } | |
89 | |
90 TEST(FakeEasyUnlockClientTest, IsEcP256KeyPair_InvalidDictValues) { | |
91 ASSERT_FALSE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair( | |
92 "{\"ec_p256_private_key\": \"12\"}", | |
93 "{\"ec_p256_public_key\": \"12\"}")); | |
94 } | |
95 | |
96 TEST(FakeEasyUnlockClientTest, ECDHKeyAgreementSuccess) { | |
97 chromeos::FakeEasyUnlockClient client; | |
98 | |
99 const std::string private_key_1 = "{\"ec_p256_private_key\": 32}"; | |
100 const std::string public_key_1 = "{\"ec_p256_public_key\": 32}"; | |
101 const std::string private_key_2 = "{\"ec_p256_private_key\": 352}"; | |
102 const std::string public_key_2 = "{\"ec_p256_public_key\": 352}"; | |
103 const std::string private_key_3 = "{\"ec_p256_private_key\": 432}"; | |
104 const std::string public_key_3 = "{\"ec_p256_public_key\": 432}"; | |
105 | |
106 std::string shared_key_1; | |
satorux1
2014/08/29 01:10:50
could you add some comments in the test? it was ha
tbarzic
2014/08/29 02:47:05
Done.
satorux1
2014/08/29 03:48:14
Thank you for adding. These are helpful.
| |
107 client.PerformECDHKeyAgreement(private_key_2, | |
108 public_key_1, | |
109 base::Bind(&RecordData, &shared_key_1)); | |
110 EXPECT_FALSE(shared_key_1.empty()); | |
111 | |
112 std::string shared_key_2; | |
113 client.PerformECDHKeyAgreement(private_key_1, | |
114 public_key_2, | |
115 base::Bind(&RecordData, &shared_key_2)); | |
116 EXPECT_FALSE(shared_key_2.empty()); | |
117 | |
118 std::string shared_key_3; | |
119 client.PerformECDHKeyAgreement(private_key_1, | |
120 public_key_3, | |
121 base::Bind(&RecordData, &shared_key_3)); | |
122 EXPECT_FALSE(shared_key_3.empty()); | |
123 | |
124 EXPECT_EQ(shared_key_1, shared_key_2); | |
125 EXPECT_NE(shared_key_1, shared_key_3); | |
126 EXPECT_NE(shared_key_2, shared_key_3); | |
127 } | |
128 | |
129 TEST(FakeEasyUnlockClientTest, ECDHKeyAgreementFailsIfKeyOrderSwitched) { | |
130 chromeos::FakeEasyUnlockClient client; | |
131 | |
132 const std::string private_key = "{\"ec_p256_private_key\": 415}"; | |
133 const std::string public_key = "{\"ec_p256_public_key\": 345}"; | |
134 | |
135 std::string shared_key; | |
136 client.PerformECDHKeyAgreement(public_key, | |
137 private_key, | |
138 base::Bind(&RecordData, &shared_key)); | |
139 EXPECT_TRUE(shared_key.empty()); | |
140 } | |
141 | |
142 TEST(FakeEasyUnlockClientTest, ECDHKeyAgreementFailsIfKeyDictKeyInvalid) { | |
143 chromeos::FakeEasyUnlockClient client; | |
144 | |
145 const std::string private_key = "{\"ec_p256_private_key_invalid\": 415}"; | |
146 const std::string public_key = "{\"ec_p256_public_key_invalid\": 345}"; | |
147 | |
148 std::string shared_key; | |
149 client.PerformECDHKeyAgreement(private_key, | |
150 public_key, | |
151 base::Bind(&RecordData, &shared_key)); | |
152 EXPECT_TRUE(shared_key.empty()); | |
153 } | |
154 | |
155 TEST(FakeEasyUnlockClientTest, ECDHKeyAgreementFailsIfKeyDictValueInvalid) { | |
156 chromeos::FakeEasyUnlockClient client; | |
157 | |
158 const std::string private_key = "{\"ec_p256_private_key\": 415}"; | |
159 const std::string public_key = "{\"ec_p256_public_key\": \"345__\"}"; | |
160 | |
161 std::string shared_key; | |
162 client.PerformECDHKeyAgreement(private_key, | |
163 public_key, | |
164 base::Bind(&RecordData, &shared_key)); | |
165 EXPECT_TRUE(shared_key.empty()); | |
166 } | |
167 | |
168 TEST(FakeEasyUnlockClientTest, ECDHKeyAgreementFailsIfKeyFormatInvalid) { | |
169 chromeos::FakeEasyUnlockClient client; | |
170 | |
171 const std::string private_key = "invalid"; | |
172 const std::string public_key = "{\"ec_p256_public_key\": 345}"; | |
173 | |
174 std::string shared_key; | |
175 client.PerformECDHKeyAgreement(private_key, | |
176 public_key, | |
177 base::Bind(&RecordData, &shared_key)); | |
178 EXPECT_TRUE(shared_key.empty()); | |
179 } | |
180 | |
181 TEST(FakeEasyUnlockClientTest, CreateSecureMessage) { | |
182 chromeos::FakeEasyUnlockClient client; | |
183 | |
184 std::string message; | |
185 client.CreateSecureMessage( | |
186 "PAYLOAD", | |
187 "KEY", | |
188 "ASSOCIATED_DATA", | |
189 "PUBLIC_METADATA", | |
190 "VERIFICATION_KEY_ID", | |
191 "ENCRYPTION_TYPE", | |
192 "SIGNATURE_TYPE", | |
193 base::Bind(&RecordData, &message)); | |
194 | |
195 const std::string expected_message( | |
196 "{\"securemessage\": {" | |
197 "\"payload\": \"PAYLOAD\"," | |
198 "\"key\": \"KEY\"," | |
199 "\"associated_data\": \"ASSOCIATED_DATA\"," | |
200 "\"public_metadata\": \"PUBLIC_METADATA\"," | |
201 "\"verification_key_id\": \"VERIFICATION_KEY_ID\"," | |
202 "\"encryption_type\": \"ENCRYPTION_TYPE\"," | |
203 "\"signature_type\": \"SIGNATURE_TYPE\"}" | |
204 "}"); | |
205 ASSERT_EQ(expected_message, message); | |
206 } | |
207 | |
208 TEST(FakeEasyUnlockClientTest, UnwrapSecureMessage) { | |
209 chromeos::FakeEasyUnlockClient client; | |
210 | |
211 std::string message; | |
212 client.UnwrapSecureMessage( | |
213 "MESSAGE", | |
214 "KEY", | |
215 "ASSOCIATED_DATA", | |
216 "ENCRYPTION_TYPE", | |
217 "SIGNATURE_TYPE", | |
218 base::Bind(&RecordData, &message)); | |
219 | |
220 const std::string expected_message( | |
221 "{\"unwrapped_securemessage\": {" | |
222 "\"message\": \"MESSAGE\"," | |
223 "\"key\": \"KEY\"," | |
224 "\"associated_data\": \"ASSOCIATED_DATA\"," | |
225 "\"encryption_type\": \"ENCRYPTION_TYPE\"," | |
226 "\"signature_type\": \"SIGNATURE_TYPE\"}" | |
227 "}"); | |
228 ASSERT_EQ(expected_message, message); | |
229 } | |
230 | |
231 } // namespace | |
232 | |
OLD | NEW |