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.
5 #include "chromeos/dbus/fake_easy_unlock_client.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 // Callback for |GenerateEcP256KeyPair| method. Saves keys returned by the
15 // method in |private_key_target| and |public_key_target|.
16 void RecordKeyPair(std::string
* private_key_target
,
17 std::string
* public_key_target
,
18 const std::string
& private_key_source
,
19 const std::string
& public_key_source
) {
20 *private_key_target
= private_key_source
;
21 *public_key_target
= public_key_source
;
24 // Callback for |EasyUnlockClient| methods that return a single piece of data.
25 // It saves the returned data in |data_target|.
26 void RecordData(std::string
* data_target
,
27 const std::string
& data_source
) {
28 *data_target
= data_source
;
31 TEST(FakeEasyUnlockClientTest
, GenerateEcP256KeyPair
) {
32 chromeos::FakeEasyUnlockClient client
;
34 std::string private_key_1
;
35 std::string public_key_1
;
36 client
.GenerateEcP256KeyPair(
37 base::Bind(&RecordKeyPair
, &private_key_1
, &public_key_1
));
38 ASSERT_EQ("{\"ec_p256_private_key\": 1}", private_key_1
);
39 ASSERT_EQ("{\"ec_p256_public_key\": 1}", public_key_1
);
41 std::string private_key_2
;
42 std::string public_key_2
;
43 client
.GenerateEcP256KeyPair(
44 base::Bind(&RecordKeyPair
, &private_key_2
, &public_key_2
));
45 ASSERT_EQ("{\"ec_p256_private_key\": 2}", private_key_2
);
46 ASSERT_EQ("{\"ec_p256_public_key\": 2}", public_key_2
);
48 EXPECT_NE(private_key_1
, private_key_2
);
49 EXPECT_NE(public_key_1
, public_key_2
);
52 TEST(FakeEasyUnlockClientTest
, IsEcP256KeyPair
) {
53 ASSERT_TRUE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair(
54 "{\"ec_p256_private_key\": 12}",
55 "{\"ec_p256_public_key\": 12}"));
58 TEST(FakeEasyUnlockClientTest
, IsEcP256KeyPair_KeysFromDiffrentPairs
) {
59 ASSERT_FALSE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair(
60 "{\"ec_p256_private_key\": 12}",
61 "{\"ec_p256_public_key\": 34}"));
64 TEST(FakeEasyUnlockClientTest
, IsEcP256KeyPair_KeyOrderSwitched
) {
65 ASSERT_FALSE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair(
66 "{\"ec_p256_public_key\": 34}",
67 "{\"ec_p256_private_key\": 34}"));
70 TEST(FakeEasyUnlockClientTest
, IsEcP256KeyPair_PrivateKeyInvalidFormat
) {
71 ASSERT_FALSE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair(
72 "\"ec_p256_private_key\": 12",
73 "{\"ec_p256_public_key\": 12}"));
76 TEST(FakeEasyUnlockClientTest
, IsEcP256KeyPair_PublicKeyInvalidFormat
) {
77 ASSERT_FALSE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair(
78 "{\"ec_p256_private_key\": 12}",
79 "\"ec_p256_public_key\": 12"));
82 TEST(FakeEasyUnlockClientTest
, IsEcP256KeyPair_PrivateKeyInvalidDictKey
) {
83 ASSERT_FALSE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair(
85 "{\"ec_p256_public_key\": 12}"));
88 TEST(FakeEasyUnlockClientTest
, IsEcP256KeyPair_PublicKeyInvalidDictKey
) {
89 ASSERT_FALSE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair(
90 "{\"ec_p256_private_key\": 12}",
91 "{\"invalid\": 12}"));
94 TEST(FakeEasyUnlockClientTest
, IsEcP256KeyPair_InvalidDictValues
) {
95 ASSERT_FALSE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair(
96 "{\"ec_p256_private_key\": \"12\"}",
97 "{\"ec_p256_public_key\": \"12\"}"));
100 // Verifies the fake |PerformECDHKeyAgreement| method is symetric in respect to
101 // key pairs from which private and public key used in the key agreement
103 TEST(FakeEasyUnlockClientTest
, ECDHKeyAgreementSuccess
) {
104 chromeos::FakeEasyUnlockClient client
;
106 // (Fake) key pairs used in the test to generate fake shared keys.
107 const std::string private_key_1
= "{\"ec_p256_private_key\": 32}";
108 const std::string public_key_1
= "{\"ec_p256_public_key\": 32}";
110 const std::string private_key_2
= "{\"ec_p256_private_key\": 352}";
111 const std::string public_key_2
= "{\"ec_p256_public_key\": 352}";
113 const std::string private_key_3
= "{\"ec_p256_private_key\": 432}";
114 const std::string public_key_3
= "{\"ec_p256_public_key\": 432}";
116 // Generate shared key for key pairs 1 and 2, using private key from the
117 // second key pair and public key from the first key pair.
118 std::string shared_key_1
;
119 client
.PerformECDHKeyAgreement(private_key_2
,
121 base::Bind(&RecordData
, &shared_key_1
));
122 EXPECT_FALSE(shared_key_1
.empty());
124 // Generate shared key for key pairs 1 and 2, using private key from the
125 // first key pair and public key from the second key pair.
126 std::string shared_key_2
;
127 client
.PerformECDHKeyAgreement(private_key_1
,
129 base::Bind(&RecordData
, &shared_key_2
));
130 EXPECT_FALSE(shared_key_2
.empty());
132 // The generated keys should be equal. They were generated using keys from
133 // the same key pairs, even though key pairs from which private and public key
134 // originate were switched.
135 EXPECT_EQ(shared_key_1
, shared_key_2
);
137 // Generate a key using key pairs 1 and 3.
138 std::string shared_key_3
;
139 client
.PerformECDHKeyAgreement(private_key_1
,
141 base::Bind(&RecordData
, &shared_key_3
));
142 EXPECT_FALSE(shared_key_3
.empty());
144 // The new key should be different from the previously generated ones, since
145 // the used key pairs are different.
146 EXPECT_NE(shared_key_3
, shared_key_1
);
147 EXPECT_NE(shared_key_3
, shared_key_1
);
150 TEST(FakeEasyUnlockClientTest
, ECDHKeyAgreementFailsIfKeyOrderSwitched
) {
151 chromeos::FakeEasyUnlockClient client
;
153 const std::string private_key
= "{\"ec_p256_private_key\": 415}";
154 const std::string public_key
= "{\"ec_p256_public_key\": 345}";
156 std::string shared_key
;
157 client
.PerformECDHKeyAgreement(public_key
,
159 base::Bind(&RecordData
, &shared_key
));
160 EXPECT_TRUE(shared_key
.empty());
163 TEST(FakeEasyUnlockClientTest
, ECDHKeyAgreementFailsIfKeyDictKeyInvalid
) {
164 chromeos::FakeEasyUnlockClient client
;
166 const std::string private_key
= "{\"ec_p256_private_key_invalid\": 415}";
167 const std::string public_key
= "{\"ec_p256_public_key_invalid\": 345}";
169 std::string shared_key
;
170 client
.PerformECDHKeyAgreement(private_key
,
172 base::Bind(&RecordData
, &shared_key
));
173 EXPECT_TRUE(shared_key
.empty());
176 TEST(FakeEasyUnlockClientTest
, ECDHKeyAgreementFailsIfKeyDictValueInvalid
) {
177 chromeos::FakeEasyUnlockClient client
;
179 const std::string private_key
= "{\"ec_p256_private_key\": 415}";
180 const std::string public_key
= "{\"ec_p256_public_key\": \"345__\"}";
182 std::string shared_key
;
183 client
.PerformECDHKeyAgreement(private_key
,
185 base::Bind(&RecordData
, &shared_key
));
186 EXPECT_TRUE(shared_key
.empty());
189 TEST(FakeEasyUnlockClientTest
, ECDHKeyAgreementFailsIfKeyFormatInvalid
) {
190 chromeos::FakeEasyUnlockClient client
;
192 const std::string private_key
= "invalid";
193 const std::string public_key
= "{\"ec_p256_public_key\": 345}";
195 std::string shared_key
;
196 client
.PerformECDHKeyAgreement(private_key
,
198 base::Bind(&RecordData
, &shared_key
));
199 EXPECT_TRUE(shared_key
.empty());
202 TEST(FakeEasyUnlockClientTest
, CreateSecureMessage
) {
203 chromeos::FakeEasyUnlockClient client
;
206 client
.CreateSecureMessage(
211 "VERIFICATION_KEY_ID",
215 base::Bind(&RecordData
, &message
));
217 const std::string
expected_message(
218 "{\"securemessage\": {"
219 "\"payload\": \"PAYLOAD\","
221 "\"associated_data\": \"ASSOCIATED_DATA\","
222 "\"public_metadata\": \"PUBLIC_METADATA\","
223 "\"verification_key_id\": \"VERIFICATION_KEY_ID\","
224 "\"decryption_key_id\": \"DECRYPTION_KEY_ID\","
225 "\"encryption_type\": \"ENCRYPTION_TYPE\","
226 "\"signature_type\": \"SIGNATURE_TYPE\"}"
228 ASSERT_EQ(expected_message
, message
);
231 TEST(FakeEasyUnlockClientTest
, UnwrapSecureMessage
) {
232 chromeos::FakeEasyUnlockClient client
;
235 client
.UnwrapSecureMessage(
241 base::Bind(&RecordData
, &message
));
243 const std::string
expected_message(
244 "{\"unwrapped_securemessage\": {"
245 "\"message\": \"MESSAGE\","
247 "\"associated_data\": \"ASSOCIATED_DATA\","
248 "\"encryption_type\": \"ENCRYPTION_TYPE\","
249 "\"signature_type\": \"SIGNATURE_TYPE\"}"
251 ASSERT_EQ(expected_message
, message
);