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

Side by Side Diff: components/proximity_auth/cryptauth/fake_secure_message_delegate_unittest.cc

Issue 2502343003: Moved //components/proximity_auth/cryptauth to //components/cryptauth. (Closed)
Patch Set: Prefixed two test names with "CryptAuth" so that they do not collide with other test names. Created 4 years, 1 month 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
(Empty)
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.
4
5 #include "components/proximity_auth/cryptauth/fake_secure_message_delegate.h"
6
7 #include "base/bind.h"
8 #include "base/macros.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace proximity_auth {
12
13 namespace {
14
15 const char kTestPublicKey[] = "the private key is in another castle";
16 const char kPayload[] = "500 tons of uranium";
17 const char kSymmetricKey[] = "hunter2";
18 const char kPublicMetadata[] = "brought to you by our sponsors";
19 const char kAssociatedData[] = "save 20% bytes on your nonce insurance";
20 const char kVerificationKeyId[] = "the one with the red stripes";
21 const char kDecryptionKeyId[] = "it's in your pocket somewhere";
22
23 // Callback for saving the result of GenerateKeys().
24 void SaveKeyPair(std::string* private_key_out,
25 std::string* public_key_out,
26 const std::string& private_key,
27 const std::string& public_key) {
28 *private_key_out = private_key;
29 *public_key_out = public_key;
30 }
31
32 // Callback for saving the result of DeriveKey() and CreateSecureMessage().
33 void SaveString(std::string* out, const std::string& value) {
34 *out = value;
35 }
36
37 // Callback for saving the result of UnwrapSecureMessage().
38 void SaveUnwrapResults(std::string* payload_out,
39 securemessage::Header* header_out,
40 bool verified,
41 const std::string& payload,
42 const securemessage::Header& header) {
43 ASSERT_TRUE(verified);
44 *payload_out = payload;
45 *header_out = header;
46 }
47
48 // Returns the CreateOptions struct to create the test message.
49 SecureMessageDelegate::CreateOptions GetCreateOptions(
50 securemessage::EncScheme encryption_scheme,
51 securemessage::SigScheme signature_scheme) {
52 SecureMessageDelegate::CreateOptions create_options;
53 create_options.encryption_scheme = encryption_scheme;
54 create_options.signature_scheme = signature_scheme;
55 create_options.public_metadata = kPublicMetadata;
56 create_options.associated_data = kAssociatedData;
57 create_options.verification_key_id = kVerificationKeyId;
58 create_options.decryption_key_id = kDecryptionKeyId;
59 return create_options;
60 }
61
62 // Returns the UnwrapOptions struct to unwrap the test message.
63 SecureMessageDelegate::UnwrapOptions GetUnwrapOptions(
64 securemessage::EncScheme encryption_scheme,
65 securemessage::SigScheme signature_scheme) {
66 SecureMessageDelegate::UnwrapOptions unwrap_options;
67 unwrap_options.encryption_scheme = encryption_scheme;
68 unwrap_options.signature_scheme = signature_scheme;
69 unwrap_options.associated_data = kAssociatedData;
70 return unwrap_options;
71 }
72
73 void CheckSerializedSecureMessage(
74 const std::string& serialized_message,
75 const SecureMessageDelegate::CreateOptions& create_options) {
76 securemessage::SecureMessage secure_message;
77 ASSERT_TRUE(secure_message.ParseFromString(serialized_message));
78 securemessage::HeaderAndBody header_and_body;
79 ASSERT_TRUE(
80 header_and_body.ParseFromString(secure_message.header_and_body()));
81
82 const securemessage::Header& header = header_and_body.header();
83 EXPECT_EQ(create_options.signature_scheme, header.signature_scheme());
84 EXPECT_EQ(create_options.encryption_scheme, header.encryption_scheme());
85 EXPECT_EQ(create_options.verification_key_id, header.verification_key_id());
86 EXPECT_EQ(create_options.decryption_key_id, header.decryption_key_id());
87 EXPECT_EQ(create_options.public_metadata, header.public_metadata());
88 }
89
90 } // namespace
91
92 class ProximityAuthFakeSecureMessageDelegateTest : public testing::Test {
93 protected:
94 ProximityAuthFakeSecureMessageDelegateTest() {}
95
96 FakeSecureMessageDelegate delegate_;
97
98 DISALLOW_COPY_AND_ASSIGN(ProximityAuthFakeSecureMessageDelegateTest);
99 };
100
101 TEST_F(ProximityAuthFakeSecureMessageDelegateTest, GenerateKeyPair) {
102 std::string public_key1, private_key1;
103 delegate_.GenerateKeyPair(
104 base::Bind(&SaveKeyPair, &public_key1, &private_key1));
105 EXPECT_NE(private_key1, public_key1);
106
107 std::string public_key2, private_key2;
108 delegate_.GenerateKeyPair(
109 base::Bind(&SaveKeyPair, &public_key2, &private_key2));
110 EXPECT_NE(private_key2, public_key2);
111
112 EXPECT_NE(public_key1, public_key2);
113 EXPECT_NE(private_key1, private_key2);
114
115 delegate_.set_next_public_key(kTestPublicKey);
116 std::string public_key3, private_key3;
117 delegate_.GenerateKeyPair(
118 base::Bind(&SaveKeyPair, &public_key3, &private_key3));
119 EXPECT_EQ(kTestPublicKey, public_key3);
120 EXPECT_NE(private_key3, public_key3);
121
122 EXPECT_NE(public_key1, public_key3);
123 EXPECT_NE(private_key1, private_key3);
124 }
125
126 TEST_F(ProximityAuthFakeSecureMessageDelegateTest, DeriveKey) {
127 delegate_.set_next_public_key("key_pair_1");
128 std::string public_key1, private_key1;
129 delegate_.GenerateKeyPair(
130 base::Bind(&SaveKeyPair, &public_key1, &private_key1));
131
132 delegate_.set_next_public_key("key_pair_2");
133 std::string public_key2, private_key2;
134 delegate_.GenerateKeyPair(
135 base::Bind(&SaveKeyPair, &public_key2, &private_key2));
136
137 std::string symmetric_key1, symmetric_key2;
138 delegate_.DeriveKey(private_key1, public_key2,
139 base::Bind(&SaveString, &symmetric_key1));
140 delegate_.DeriveKey(private_key2, public_key1,
141 base::Bind(&SaveString, &symmetric_key2));
142
143 EXPECT_EQ(symmetric_key1, symmetric_key2);
144 }
145
146 TEST_F(ProximityAuthFakeSecureMessageDelegateTest,
147 CreateAndUnwrapWithSymmetricKey) {
148 // Create SecureMessage using symmetric key.
149 SecureMessageDelegate::CreateOptions create_options =
150 GetCreateOptions(securemessage::AES_256_CBC, securemessage::HMAC_SHA256);
151 std::string serialized_message;
152 delegate_.CreateSecureMessage(kPayload, kSymmetricKey, create_options,
153 base::Bind(&SaveString, &serialized_message));
154
155 CheckSerializedSecureMessage(serialized_message, create_options);
156
157 // Unwrap SecureMessage using symmetric key.
158 SecureMessageDelegate::UnwrapOptions unwrap_options =
159 GetUnwrapOptions(securemessage::AES_256_CBC, securemessage::HMAC_SHA256);
160 std::string payload;
161 securemessage::Header header;
162 delegate_.UnwrapSecureMessage(
163 serialized_message, kSymmetricKey, unwrap_options,
164 base::Bind(&SaveUnwrapResults, &payload, &header));
165
166 EXPECT_EQ(kPayload, payload);
167 }
168
169 TEST_F(ProximityAuthFakeSecureMessageDelegateTest,
170 CreateAndUnwrapWithAsymmetricKey) {
171 delegate_.set_next_public_key(kTestPublicKey);
172 std::string public_key, private_key;
173 delegate_.GenerateKeyPair(
174 base::Bind(&SaveKeyPair, &public_key, &private_key));
175
176 // Create SecureMessage using asymmetric key.
177 SecureMessageDelegate::CreateOptions create_options =
178 GetCreateOptions(securemessage::NONE, securemessage::ECDSA_P256_SHA256);
179 std::string serialized_message;
180 delegate_.CreateSecureMessage(kPayload, private_key, create_options,
181 base::Bind(&SaveString, &serialized_message));
182
183 CheckSerializedSecureMessage(serialized_message, create_options);
184
185 // Unwrap SecureMessage using symmetric key.
186 SecureMessageDelegate::UnwrapOptions unwrap_options =
187 GetUnwrapOptions(securemessage::NONE, securemessage::ECDSA_P256_SHA256);
188 std::string payload;
189 securemessage::Header header;
190 delegate_.UnwrapSecureMessage(
191 serialized_message, public_key, unwrap_options,
192 base::Bind(&SaveUnwrapResults, &payload, &header));
193
194 EXPECT_EQ(kPayload, payload);
195 }
196
197 TEST_F(ProximityAuthFakeSecureMessageDelegateTest, GetPrivateKeyForPublicKey) {
198 delegate_.set_next_public_key(kTestPublicKey);
199 std::string public_key, private_key;
200 delegate_.GenerateKeyPair(
201 base::Bind(&SaveKeyPair, &public_key, &private_key));
202 EXPECT_EQ(kTestPublicKey, public_key);
203 EXPECT_EQ(private_key, delegate_.GetPrivateKeyForPublicKey(kTestPublicKey));
204 }
205
206 } // proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698