Use kDefaultURL throughout SpdySessionTest.
[chromium-blink-merge.git] / components / proximity_auth / device_to_device_operations_unittest.cc
blob0ba34ad1d9e026e691218500e9d8720ff91d5cf3
1 // Copyright 2015 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 "base/bind.h"
6 #include "components/proximity_auth/cryptauth/base64url.h"
7 #include "components/proximity_auth/cryptauth/fake_secure_message_delegate.h"
8 #include "components/proximity_auth/device_to_device_initiator_operations.h"
9 #include "components/proximity_auth/device_to_device_responder_operations.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace proximity_auth {
14 namespace {
16 // The initiator's session public key in base64url form. Note that this is
17 // actually a serialized proto.
18 const char kInitiatorSessionPublicKeyBase64[] =
19 "CAESRQogOlH8DgPMQu7eAt-b6yoTXcazG8mAl6SPC5Ds-LTULIcSIQDZDMqsoYRO4tNMej1FB"
20 "El1sTiTiVDqrcGq-CkYCzDThw==";
22 // The responder's session public key in base64url form. Note that this is
23 // actually a serialized proto.
24 const char kResponderSessionPublicKeyBase64[] =
25 "CAESRgohAN9QYU5HySO14Gi9PDIClacBnC0C8wqPwXsNHUNG_vXlEiEAggzU80ZOd9DWuCBdp"
26 "6bzpGcC-oj1yrwdVCHGg_yeaAQ=";
28 // The long-term public key possessed by the responder device.
29 const char kResponderPersistentPublicKey[] = "responder persistent public key";
31 // Used as a callback for message creation operations to save |message| into
32 // |out_message|.
33 void SaveMessageResult(std::string* out_message, const std::string& message) {
34 *out_message = message;
37 // Used as a callback for validation operations to save |success| and into
38 // |out_success|.
39 void SaveValidationResult(bool* out_success, bool success) {
40 *out_success = success;
43 // Used as a callback for the ValidateResponderAuthMessage and
44 // ValidateHelloMessage operations, saving both the outcome and the returned
45 // key.
46 void SaveValidationResultWithKey(bool* out_success,
47 std::string* out_key,
48 bool success,
49 const std::string& key) {
50 *out_success = success;
51 *out_key = key;
54 } // namespace
56 class ProximityAuthDeviceToDeviceOperationsTest : public testing::Test {
57 protected:
58 ProximityAuthDeviceToDeviceOperationsTest() {}
59 ~ProximityAuthDeviceToDeviceOperationsTest() override {}
61 void SetUp() override {
62 ASSERT_TRUE(Base64UrlDecode(kInitiatorSessionPublicKeyBase64,
63 &local_session_public_key_));
64 local_session_private_key_ =
65 secure_message_delegate_.GetPrivateKeyForPublicKey(
66 local_session_public_key_);
68 ASSERT_TRUE(Base64UrlDecode(kResponderSessionPublicKeyBase64,
69 &remote_session_public_key_));
70 remote_session_private_key_ =
71 secure_message_delegate_.GetPrivateKeyForPublicKey(
72 remote_session_public_key_);
74 // Note: FakeSecureMessageDelegate functions are synchronous.
75 secure_message_delegate_.DeriveKey(
76 local_session_private_key_, remote_session_public_key_,
77 base::Bind(&SaveMessageResult, &session_symmetric_key_));
79 persistent_symmetric_key_ = "persistent symmetric key";
82 // Creates the initator's [Hello] message.
83 std::string CreateHelloMessage() {
84 std::string hello_message;
85 DeviceToDeviceInitiatorOperations::CreateHelloMessage(
86 local_session_public_key_, persistent_symmetric_key_,
87 &secure_message_delegate_,
88 base::Bind(&SaveMessageResult, &hello_message));
89 EXPECT_FALSE(hello_message.empty());
90 return hello_message;
93 // Creates the responder's [Remote Auth] message.
94 std::string CreateResponderAuthMessage(const std::string& hello_message) {
95 std::string persistent_responder_private_key =
96 secure_message_delegate_.GetPrivateKeyForPublicKey(
97 kResponderPersistentPublicKey);
99 std::string remote_auth_message;
100 DeviceToDeviceResponderOperations::CreateResponderAuthMessage(
101 hello_message, remote_session_public_key_, remote_session_private_key_,
102 persistent_responder_private_key, persistent_symmetric_key_,
103 &secure_message_delegate_,
104 base::Bind(&SaveMessageResult, &remote_auth_message));
105 EXPECT_FALSE(remote_auth_message.empty());
106 return remote_auth_message;
109 // Creates the initiator's [Initiator Auth] message.
110 std::string CreateInitiatorAuthMessage(
111 const std::string& remote_auth_message) {
112 std::string local_auth_message;
113 DeviceToDeviceInitiatorOperations::CreateInitiatorAuthMessage(
114 session_symmetric_key_, persistent_symmetric_key_, remote_auth_message,
115 &secure_message_delegate_,
116 base::Bind(&SaveMessageResult, &local_auth_message));
117 EXPECT_FALSE(local_auth_message.empty());
118 return local_auth_message;
121 FakeSecureMessageDelegate secure_message_delegate_;
123 std::string persistent_symmetric_key_;
124 std::string local_session_public_key_;
125 std::string local_session_private_key_;
126 std::string remote_session_public_key_;
127 std::string remote_session_private_key_;
128 std::string session_symmetric_key_;
130 DISALLOW_COPY_AND_ASSIGN(ProximityAuthDeviceToDeviceOperationsTest);
133 TEST_F(ProximityAuthDeviceToDeviceOperationsTest,
134 ValidateHelloMessage_Success) {
135 bool validation_success = false;
136 std::string hello_public_key;
137 DeviceToDeviceResponderOperations::ValidateHelloMessage(
138 CreateHelloMessage(), persistent_symmetric_key_,
139 &secure_message_delegate_,
140 base::Bind(&SaveValidationResultWithKey, &validation_success,
141 &hello_public_key));
143 EXPECT_TRUE(validation_success);
144 EXPECT_EQ(local_session_public_key_, hello_public_key);
147 TEST_F(ProximityAuthDeviceToDeviceOperationsTest,
148 ValidateHelloMessage_Failure) {
149 bool validation_success = true;
150 std::string hello_public_key = "non-empty string";
151 DeviceToDeviceResponderOperations::ValidateHelloMessage(
152 "some random string", persistent_symmetric_key_,
153 &secure_message_delegate_,
154 base::Bind(&SaveValidationResultWithKey, &validation_success,
155 &hello_public_key));
157 EXPECT_FALSE(validation_success);
158 EXPECT_TRUE(hello_public_key.empty());
161 TEST_F(ProximityAuthDeviceToDeviceOperationsTest,
162 ValidateResponderAuthMessage_Success) {
163 std::string hello_message = CreateHelloMessage();
164 std::string remote_auth_message = CreateResponderAuthMessage(hello_message);
166 bool validation_success = false;
167 std::string session_symmetric_key;
168 DeviceToDeviceInitiatorOperations::ValidateResponderAuthMessage(
169 remote_auth_message, kResponderPersistentPublicKey,
170 persistent_symmetric_key_, local_session_private_key_, hello_message,
171 &secure_message_delegate_,
172 base::Bind(&SaveValidationResultWithKey, &validation_success,
173 &session_symmetric_key));
175 EXPECT_TRUE(validation_success);
176 EXPECT_EQ(session_symmetric_key_, session_symmetric_key);
179 TEST_F(ProximityAuthDeviceToDeviceOperationsTest,
180 ValidateResponderAuthMessage_InvalidHelloMessage) {
181 std::string hello_message = CreateHelloMessage();
182 std::string remote_auth_message = CreateResponderAuthMessage(hello_message);
184 bool validation_success = true;
185 std::string session_symmetric_key = "non empty";
186 DeviceToDeviceInitiatorOperations::ValidateResponderAuthMessage(
187 remote_auth_message, kResponderPersistentPublicKey,
188 persistent_symmetric_key_, local_session_private_key_,
189 "invalid hello message", &secure_message_delegate_,
190 base::Bind(&SaveValidationResultWithKey, &validation_success,
191 &session_symmetric_key));
193 EXPECT_FALSE(validation_success);
194 EXPECT_TRUE(session_symmetric_key.empty());
197 TEST_F(ProximityAuthDeviceToDeviceOperationsTest,
198 ValidateResponderAuthMessage_InvalidPSK) {
199 std::string hello_message = CreateHelloMessage();
200 std::string remote_auth_message = CreateResponderAuthMessage(hello_message);
202 bool validation_success = true;
203 std::string session_symmetric_key = "non empty";
204 DeviceToDeviceInitiatorOperations::ValidateResponderAuthMessage(
205 remote_auth_message, kResponderPersistentPublicKey,
206 "invalid persistent symmetric key", local_session_private_key_,
207 hello_message, &secure_message_delegate_,
208 base::Bind(&SaveValidationResultWithKey, &validation_success,
209 &session_symmetric_key));
211 EXPECT_FALSE(validation_success);
212 EXPECT_TRUE(session_symmetric_key.empty());
215 TEST_F(ProximityAuthDeviceToDeviceOperationsTest,
216 ValidateInitiatorAuthMessage_Success) {
217 std::string hello_message = CreateHelloMessage();
218 std::string remote_auth_message = CreateResponderAuthMessage(hello_message);
219 std::string local_auth_message =
220 CreateInitiatorAuthMessage(remote_auth_message);
222 bool validation_success = false;
223 DeviceToDeviceResponderOperations::ValidateInitiatorAuthMessage(
224 local_auth_message, session_symmetric_key_, persistent_symmetric_key_,
225 remote_auth_message, &secure_message_delegate_,
226 base::Bind(&SaveValidationResult, &validation_success));
228 EXPECT_TRUE(validation_success);
231 TEST_F(ProximityAuthDeviceToDeviceOperationsTest,
232 ValidateInitiatorAuthMessage_InvalidRemoteAuth) {
233 std::string hello_message = CreateHelloMessage();
234 std::string remote_auth_message = CreateResponderAuthMessage(hello_message);
235 std::string local_auth_message =
236 CreateInitiatorAuthMessage(remote_auth_message);
238 bool validation_success = true;
239 DeviceToDeviceResponderOperations::ValidateInitiatorAuthMessage(
240 local_auth_message, session_symmetric_key_, persistent_symmetric_key_,
241 "invalid remote auth", &secure_message_delegate_,
242 base::Bind(&SaveValidationResult, &validation_success));
244 EXPECT_FALSE(validation_success);
247 TEST_F(ProximityAuthDeviceToDeviceOperationsTest,
248 ValidateInitiatorAuthMessage_InvalidPSK) {
249 std::string hello_message = CreateHelloMessage();
250 std::string remote_auth_message = CreateResponderAuthMessage(hello_message);
251 std::string local_auth_message =
252 CreateInitiatorAuthMessage(remote_auth_message);
254 bool validation_success = true;
255 DeviceToDeviceResponderOperations::ValidateInitiatorAuthMessage(
256 local_auth_message, session_symmetric_key_,
257 "invalid persistent symmetric key", remote_auth_message,
258 &secure_message_delegate_,
259 base::Bind(&SaveValidationResult, &validation_success));
261 EXPECT_FALSE(validation_success);
264 } // namespace proximity_auth