Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: components/cryptauth/device_to_device_secure_context_unittest.cc

Issue 2899863002: Updating D2D protocol to v1 to support separate sequence numbers. (Closed)
Patch Set: Addressing comments Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/cryptauth/device_to_device_secure_context.h" 5 #include "components/cryptauth/device_to_device_secure_context.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "components/cryptauth/fake_secure_message_delegate.h" 11 #include "components/cryptauth/fake_secure_message_delegate.h"
12 #include "components/cryptauth/proto/cryptauth_api.pb.h" 12 #include "components/cryptauth/proto/cryptauth_api.pb.h"
13 #include "components/cryptauth/proto/securemessage.pb.h" 13 #include "components/cryptauth/proto/securemessage.pb.h"
14 #include "components/cryptauth/session_keys.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 16
16 namespace cryptauth { 17 namespace cryptauth {
17 18
18 namespace { 19 namespace {
19 20
20 const char kSymmetricKey[] = "symmetric key"; 21 const char kSymmetricKey[] = "symmetric key";
21 const char kResponderAuthMessage[] = "responder_auth_message"; 22 const char kResponderAuthMessage[] = "responder_auth_message";
22 const SecureContext::ProtocolVersion kProtocolVersion = 23 const SecureContext::ProtocolVersion kProtocolVersion =
23 SecureContext::PROTOCOL_VERSION_THREE_ONE; 24 SecureContext::PROTOCOL_VERSION_THREE_ONE;
24 25
25 // Callback saving |result| to |result_out|. 26 // Callback saving |result| to |result_out|.
26 void SaveResult(std::string* result_out, const std::string& result) { 27 void SaveResult(std::string* result_out, const std::string& result) {
27 *result_out = result; 28 *result_out = result;
28 } 29 }
29 30
31 // The responder's secure context will have the encoding / decoding keys
32 // inverted.
33 class InvertedSessionKeys : public SessionKeys {
34 public:
35 explicit InvertedSessionKeys(const std::string& master_symmetric_key)
36 : SessionKeys(master_symmetric_key) {}
37
38 InvertedSessionKeys() : SessionKeys() {}
39
40 InvertedSessionKeys(const InvertedSessionKeys& other) : SessionKeys(other) {}
41
42 std::string initiator_encode_key() const override {
43 return SessionKeys::responder_encode_key();
44 }
45 std::string responder_encode_key() const override {
46 return SessionKeys::initiator_encode_key();
47 }
48 };
49
30 } // namespace 50 } // namespace
31 51
32 class ProximityAuthDeviceToDeviceSecureContextTest : public testing::Test { 52 class ProximityAuthDeviceToDeviceSecureContextTest : public testing::Test {
33 protected: 53 protected:
34 ProximityAuthDeviceToDeviceSecureContextTest() 54 ProximityAuthDeviceToDeviceSecureContextTest()
35 : secure_context_( 55 : secure_context_(base::MakeUnique<FakeSecureMessageDelegate>(),
36 base::MakeUnique<FakeSecureMessageDelegate>(), 56 SessionKeys(kSymmetricKey),
37 kSymmetricKey, 57 kResponderAuthMessage,
38 kResponderAuthMessage, 58 kProtocolVersion) {}
39 kProtocolVersion) {}
40 59
41 DeviceToDeviceSecureContext secure_context_; 60 DeviceToDeviceSecureContext secure_context_;
42 }; 61 };
43 62
44 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest, GetProperties) { 63 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest, GetProperties) {
45 EXPECT_EQ(kResponderAuthMessage, secure_context_.GetChannelBindingData()); 64 EXPECT_EQ(kResponderAuthMessage, secure_context_.GetChannelBindingData());
46 EXPECT_EQ(kProtocolVersion, secure_context_.GetProtocolVersion()); 65 EXPECT_EQ(kProtocolVersion, secure_context_.GetProtocolVersion());
47 } 66 }
48 67
49 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest, CheckEncodedHeader) { 68 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest, CheckEncodedHeader) {
(...skipping 17 matching lines...) Expand all
67 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest, DecodeInvalidMessage) { 86 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest, DecodeInvalidMessage) {
68 std::string encoded_message = "invalidly encoded message"; 87 std::string encoded_message = "invalidly encoded message";
69 std::string decoded_message = "not empty"; 88 std::string decoded_message = "not empty";
70 secure_context_.Decode(encoded_message, 89 secure_context_.Decode(encoded_message,
71 base::Bind(&SaveResult, &decoded_message)); 90 base::Bind(&SaveResult, &decoded_message));
72 EXPECT_TRUE(decoded_message.empty()); 91 EXPECT_TRUE(decoded_message.empty());
73 } 92 }
74 93
75 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest, EncodeAndDecode) { 94 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest, EncodeAndDecode) {
76 // Initialize second secure channel with the same parameters as the first. 95 // Initialize second secure channel with the same parameters as the first.
96 InvertedSessionKeys inverted_session_keys(kSymmetricKey);
77 DeviceToDeviceSecureContext secure_context2( 97 DeviceToDeviceSecureContext secure_context2(
78 base::MakeUnique<FakeSecureMessageDelegate>(), kSymmetricKey, 98 base::MakeUnique<FakeSecureMessageDelegate>(), inverted_session_keys,
79 kResponderAuthMessage, kProtocolVersion); 99 kResponderAuthMessage, kProtocolVersion);
80 std::string message = "encrypt this message"; 100 std::string message = "encrypt this message";
81 101
102 SessionKeys session_keys(kSymmetricKey);
103 EXPECT_EQ(session_keys.initiator_encode_key(),
104 inverted_session_keys.responder_encode_key());
105 EXPECT_EQ(session_keys.responder_encode_key(),
106 inverted_session_keys.initiator_encode_key());
107
82 // Pass some messages between the two secure contexts. 108 // Pass some messages between the two secure contexts.
83 for (int i = 0; i < 3; ++i) { 109 for (int i = 0; i < 3; ++i) {
84 std::string encoded_message; 110 std::string encoded_message;
85 secure_context_.Encode(message, base::Bind(&SaveResult, &encoded_message)); 111 secure_context_.Encode(message, base::Bind(&SaveResult, &encoded_message));
86 EXPECT_NE(message, encoded_message); 112 EXPECT_NE(message, encoded_message);
87 113
88 std::string decoded_message; 114 std::string decoded_message;
89 secure_context2.Decode(encoded_message, 115 secure_context2.Decode(encoded_message,
90 base::Bind(&SaveResult, &decoded_message)); 116 base::Bind(&SaveResult, &decoded_message));
91 EXPECT_EQ(message, decoded_message); 117 EXPECT_EQ(message, decoded_message);
92 } 118 }
93 } 119 }
94 120
95 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest, 121 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest,
96 DecodeInvalidSequenceNumber) { 122 DecodeInvalidSequenceNumber) {
97 // Initialize second secure channel with the same parameters as the first. 123 // Initialize second secure channel with the same parameters as the first.
98 DeviceToDeviceSecureContext secure_context2( 124 DeviceToDeviceSecureContext secure_context2(
99 base::MakeUnique<FakeSecureMessageDelegate>(), kSymmetricKey, 125 base::MakeUnique<FakeSecureMessageDelegate>(),
100 kResponderAuthMessage, kProtocolVersion); 126 InvertedSessionKeys(kSymmetricKey), kResponderAuthMessage,
127 kProtocolVersion);
101 128
102 // Send a few messages over the first secure context. 129 // Send a few messages over the first secure context.
103 std::string message = "encrypt this message"; 130 std::string message = "encrypt this message";
104 std::string encoded1; 131 std::string encoded1;
105 for (int i = 0; i < 3; ++i) { 132 for (int i = 0; i < 3; ++i) {
106 secure_context_.Encode(message, base::Bind(&SaveResult, &encoded1)); 133 secure_context_.Encode(message, base::Bind(&SaveResult, &encoded1));
107 } 134 }
108 135
109 // Second secure channel should not decode the message with an invalid 136 // Second secure channel should not decode the message with an invalid
110 // sequence number. 137 // sequence number.
111 std::string decoded_message = "not empty"; 138 std::string decoded_message = "not empty";
112 secure_context_.Decode(encoded1, base::Bind(&SaveResult, &decoded_message)); 139 secure_context_.Decode(encoded1, base::Bind(&SaveResult, &decoded_message));
113 EXPECT_TRUE(decoded_message.empty()); 140 EXPECT_TRUE(decoded_message.empty());
114 } 141 }
115 142
116 } // cryptauth 143 } // cryptauth
OLDNEW
« no previous file with comments | « components/cryptauth/device_to_device_secure_context.cc ('k') | components/cryptauth/proto/securemessage.proto » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698