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 "components/proximity_auth/device_to_device_secure_context.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "components/proximity_auth/cryptauth/fake_secure_message_delegate.h"
10 #include "components/proximity_auth/cryptauth/proto/cryptauth_api.pb.h"
11 #include "components/proximity_auth/cryptauth/proto/securemessage.pb.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace proximity_auth
{
18 const char kSymmetricKey
[] = "symmetric key";
19 const char kResponderAuthMessage
[] = "responder_auth_message";
20 const SecureContext::ProtocolVersion kProtocolVersion
=
21 SecureContext::PROTOCOL_VERSION_THREE_ONE
;
23 // Callback saving |result| to |result_out|.
24 void SaveResult(std::string
* result_out
, const std::string
& result
) {
30 class ProximityAuthDeviceToDeviceSecureContextTest
: public testing::Test
{
32 ProximityAuthDeviceToDeviceSecureContextTest()
33 : secure_context_(make_scoped_ptr(new FakeSecureMessageDelegate()),
35 kResponderAuthMessage
,
38 DeviceToDeviceSecureContext secure_context_
;
41 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest
, GetProperties
) {
42 EXPECT_EQ(kResponderAuthMessage
, secure_context_
.GetReceivedAuthMessage());
43 EXPECT_EQ(kProtocolVersion
, secure_context_
.GetProtocolVersion());
46 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest
, CheckEncodedHeader
) {
47 std::string message
= "encrypt this message";
48 std::string encoded_message
;
49 secure_context_
.Encode(message
, base::Bind(&SaveResult
, &encoded_message
));
51 securemessage::SecureMessage secure_message
;
52 ASSERT_TRUE(secure_message
.ParseFromString(encoded_message
));
53 securemessage::HeaderAndBody header_and_body
;
55 header_and_body
.ParseFromString(secure_message
.header_and_body()));
57 cryptauth::GcmMetadata gcm_metadata
;
59 gcm_metadata
.ParseFromString(header_and_body
.header().public_metadata()));
60 EXPECT_EQ(1, gcm_metadata
.version());
61 EXPECT_EQ(cryptauth::DEVICE_TO_DEVICE_MESSAGE
, gcm_metadata
.type());
64 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest
, DecodeInvalidMessage
) {
65 std::string encoded_message
= "invalidly encoded message";
66 std::string decoded_message
= "not empty";
67 secure_context_
.Decode(encoded_message
,
68 base::Bind(&SaveResult
, &decoded_message
));
69 EXPECT_TRUE(decoded_message
.empty());
72 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest
, EncodeAndDecode
) {
73 // Initialize second secure channel with the same parameters as the first.
74 DeviceToDeviceSecureContext
secure_context2(
75 make_scoped_ptr(new FakeSecureMessageDelegate()), kSymmetricKey
,
76 kResponderAuthMessage
, kProtocolVersion
);
77 std::string message
= "encrypt this message";
79 // Pass some messages between the two secure contexts.
80 for (int i
= 0; i
< 3; ++i
) {
81 std::string encoded_message
;
82 secure_context_
.Encode(message
, base::Bind(&SaveResult
, &encoded_message
));
83 EXPECT_NE(message
, encoded_message
);
85 std::string decoded_message
;
86 secure_context2
.Decode(encoded_message
,
87 base::Bind(&SaveResult
, &decoded_message
));
88 EXPECT_EQ(message
, decoded_message
);
92 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest
,
93 DecodeInvalidSequenceNumber
) {
94 // Initialize second secure channel with the same parameters as the first.
95 DeviceToDeviceSecureContext
secure_context2(
96 make_scoped_ptr(new FakeSecureMessageDelegate()), kSymmetricKey
,
97 kResponderAuthMessage
, kProtocolVersion
);
99 // Send a few messages over the first secure context.
100 std::string message
= "encrypt this message";
101 std::string encoded1
;
102 for (int i
= 0; i
< 3; ++i
) {
103 secure_context_
.Encode(message
, base::Bind(&SaveResult
, &encoded1
));
106 // Second secure channel should not decode the message with an invalid
108 std::string decoded_message
= "not empty";
109 secure_context_
.Decode(encoded1
, base::Bind(&SaveResult
, &decoded_message
));
110 EXPECT_TRUE(decoded_message
.empty());