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

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: Fixing tests 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 std::string initiator_encode_key() const override {
39 return SessionKeys::responder_encode_key();
40 }
41 std::string responder_encode_key() const override {
42 return SessionKeys::initiator_encode_key();
43 }
44 };
45
30 } // namespace 46 } // namespace
31 47
32 class ProximityAuthDeviceToDeviceSecureContextTest : public testing::Test { 48 class ProximityAuthDeviceToDeviceSecureContextTest : public testing::Test {
33 protected: 49 protected:
34 ProximityAuthDeviceToDeviceSecureContextTest() 50 ProximityAuthDeviceToDeviceSecureContextTest()
35 : secure_context_( 51 : secure_context_(base::MakeUnique<FakeSecureMessageDelegate>(),
36 base::MakeUnique<FakeSecureMessageDelegate>(), 52 base::MakeUnique<SessionKeys>(kSymmetricKey),
37 kSymmetricKey, 53 kResponderAuthMessage,
38 kResponderAuthMessage, 54 kProtocolVersion) {}
39 kProtocolVersion) {}
40 55
41 DeviceToDeviceSecureContext secure_context_; 56 DeviceToDeviceSecureContext secure_context_;
42 }; 57 };
43 58
44 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest, GetProperties) { 59 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest, GetProperties) {
45 EXPECT_EQ(kResponderAuthMessage, secure_context_.GetChannelBindingData()); 60 EXPECT_EQ(kResponderAuthMessage, secure_context_.GetChannelBindingData());
46 EXPECT_EQ(kProtocolVersion, secure_context_.GetProtocolVersion()); 61 EXPECT_EQ(kProtocolVersion, secure_context_.GetProtocolVersion());
47 } 62 }
48 63
49 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest, CheckEncodedHeader) { 64 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest, CheckEncodedHeader) {
(...skipping 18 matching lines...) Expand all
68 std::string encoded_message = "invalidly encoded message"; 83 std::string encoded_message = "invalidly encoded message";
69 std::string decoded_message = "not empty"; 84 std::string decoded_message = "not empty";
70 secure_context_.Decode(encoded_message, 85 secure_context_.Decode(encoded_message,
71 base::Bind(&SaveResult, &decoded_message)); 86 base::Bind(&SaveResult, &decoded_message));
72 EXPECT_TRUE(decoded_message.empty()); 87 EXPECT_TRUE(decoded_message.empty());
73 } 88 }
74 89
75 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest, EncodeAndDecode) { 90 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest, EncodeAndDecode) {
76 // Initialize second secure channel with the same parameters as the first. 91 // Initialize second secure channel with the same parameters as the first.
77 DeviceToDeviceSecureContext secure_context2( 92 DeviceToDeviceSecureContext secure_context2(
78 base::MakeUnique<FakeSecureMessageDelegate>(), kSymmetricKey, 93 base::MakeUnique<FakeSecureMessageDelegate>(),
94 base::MakeUnique<InvertedSessionKeys>(kSymmetricKey),
79 kResponderAuthMessage, kProtocolVersion); 95 kResponderAuthMessage, kProtocolVersion);
80 std::string message = "encrypt this message"; 96 std::string message = "encrypt this message";
81 97
82 // Pass some messages between the two secure contexts. 98 // Pass some messages between the two secure contexts.
83 for (int i = 0; i < 3; ++i) { 99 for (int i = 0; i < 3; ++i) {
84 std::string encoded_message; 100 std::string encoded_message;
85 secure_context_.Encode(message, base::Bind(&SaveResult, &encoded_message)); 101 secure_context_.Encode(message, base::Bind(&SaveResult, &encoded_message));
86 EXPECT_NE(message, encoded_message); 102 EXPECT_NE(message, encoded_message);
87 103
88 std::string decoded_message; 104 std::string decoded_message;
89 secure_context2.Decode(encoded_message, 105 secure_context2.Decode(encoded_message,
90 base::Bind(&SaveResult, &decoded_message)); 106 base::Bind(&SaveResult, &decoded_message));
91 EXPECT_EQ(message, decoded_message); 107 EXPECT_EQ(message, decoded_message);
92 } 108 }
93 } 109 }
94 110
95 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest, 111 TEST_F(ProximityAuthDeviceToDeviceSecureContextTest,
96 DecodeInvalidSequenceNumber) { 112 DecodeInvalidSequenceNumber) {
97 // Initialize second secure channel with the same parameters as the first. 113 // Initialize second secure channel with the same parameters as the first.
98 DeviceToDeviceSecureContext secure_context2( 114 DeviceToDeviceSecureContext secure_context2(
99 base::MakeUnique<FakeSecureMessageDelegate>(), kSymmetricKey, 115 base::MakeUnique<FakeSecureMessageDelegate>(),
116 base::MakeUnique<InvertedSessionKeys>(kSymmetricKey),
100 kResponderAuthMessage, kProtocolVersion); 117 kResponderAuthMessage, kProtocolVersion);
101 118
102 // Send a few messages over the first secure context. 119 // Send a few messages over the first secure context.
103 std::string message = "encrypt this message"; 120 std::string message = "encrypt this message";
104 std::string encoded1; 121 std::string encoded1;
105 for (int i = 0; i < 3; ++i) { 122 for (int i = 0; i < 3; ++i) {
106 secure_context_.Encode(message, base::Bind(&SaveResult, &encoded1)); 123 secure_context_.Encode(message, base::Bind(&SaveResult, &encoded1));
107 } 124 }
108 125
109 // Second secure channel should not decode the message with an invalid 126 // Second secure channel should not decode the message with an invalid
110 // sequence number. 127 // sequence number.
111 std::string decoded_message = "not empty"; 128 std::string decoded_message = "not empty";
112 secure_context_.Decode(encoded1, base::Bind(&SaveResult, &decoded_message)); 129 secure_context_.Decode(encoded1, base::Bind(&SaveResult, &decoded_message));
113 EXPECT_TRUE(decoded_message.empty()); 130 EXPECT_TRUE(decoded_message.empty());
114 } 131 }
115 132
116 } // cryptauth 133 } // cryptauth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698