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

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

Issue 2502343003: Moved //components/proximity_auth/cryptauth to //components/cryptauth. (Closed)
Patch Set: Fixed proto #includes. 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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/proximity_auth/cryptauth/fake_secure_message_delegate.h" 5 #include "components/cryptauth/fake_secure_message_delegate.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
11 namespace proximity_auth { 11 namespace cryptauth {
12 12
13 namespace { 13 namespace {
14 14
15 const char kTestPublicKey[] = "the private key is in another castle"; 15 const char kTestPublicKey[] = "the private key is in another castle";
16 const char kPayload[] = "500 tons of uranium"; 16 const char kPayload[] = "500 tons of uranium";
17 const char kSymmetricKey[] = "hunter2"; 17 const char kSymmetricKey[] = "hunter2";
18 const char kPublicMetadata[] = "brought to you by our sponsors"; 18 const char kPublicMetadata[] = "brought to you by our sponsors";
19 const char kAssociatedData[] = "save 20% bytes on your nonce insurance"; 19 const char kAssociatedData[] = "save 20% bytes on your nonce insurance";
20 const char kVerificationKeyId[] = "the one with the red stripes"; 20 const char kVerificationKeyId[] = "the one with the red stripes";
21 const char kDecryptionKeyId[] = "it's in your pocket somewhere"; 21 const char kDecryptionKeyId[] = "it's in your pocket somewhere";
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 const securemessage::Header& header = header_and_body.header(); 82 const securemessage::Header& header = header_and_body.header();
83 EXPECT_EQ(create_options.signature_scheme, header.signature_scheme()); 83 EXPECT_EQ(create_options.signature_scheme, header.signature_scheme());
84 EXPECT_EQ(create_options.encryption_scheme, header.encryption_scheme()); 84 EXPECT_EQ(create_options.encryption_scheme, header.encryption_scheme());
85 EXPECT_EQ(create_options.verification_key_id, header.verification_key_id()); 85 EXPECT_EQ(create_options.verification_key_id, header.verification_key_id());
86 EXPECT_EQ(create_options.decryption_key_id, header.decryption_key_id()); 86 EXPECT_EQ(create_options.decryption_key_id, header.decryption_key_id());
87 EXPECT_EQ(create_options.public_metadata, header.public_metadata()); 87 EXPECT_EQ(create_options.public_metadata, header.public_metadata());
88 } 88 }
89 89
90 } // namespace 90 } // namespace
91 91
92 class ProximityAuthFakeSecureMessageDelegateTest : public testing::Test { 92 class CryptAuthFakeSecureMessageDelegateTest : public testing::Test {
93 protected: 93 protected:
94 ProximityAuthFakeSecureMessageDelegateTest() {} 94 CryptAuthFakeSecureMessageDelegateTest() {}
95 95
96 FakeSecureMessageDelegate delegate_; 96 FakeSecureMessageDelegate delegate_;
97 97
98 DISALLOW_COPY_AND_ASSIGN(ProximityAuthFakeSecureMessageDelegateTest); 98 DISALLOW_COPY_AND_ASSIGN(CryptAuthFakeSecureMessageDelegateTest);
99 }; 99 };
100 100
101 TEST_F(ProximityAuthFakeSecureMessageDelegateTest, GenerateKeyPair) { 101 TEST_F(CryptAuthFakeSecureMessageDelegateTest, GenerateKeyPair) {
102 std::string public_key1, private_key1; 102 std::string public_key1, private_key1;
103 delegate_.GenerateKeyPair( 103 delegate_.GenerateKeyPair(
104 base::Bind(&SaveKeyPair, &public_key1, &private_key1)); 104 base::Bind(&SaveKeyPair, &public_key1, &private_key1));
105 EXPECT_NE(private_key1, public_key1); 105 EXPECT_NE(private_key1, public_key1);
106 106
107 std::string public_key2, private_key2; 107 std::string public_key2, private_key2;
108 delegate_.GenerateKeyPair( 108 delegate_.GenerateKeyPair(
109 base::Bind(&SaveKeyPair, &public_key2, &private_key2)); 109 base::Bind(&SaveKeyPair, &public_key2, &private_key2));
110 EXPECT_NE(private_key2, public_key2); 110 EXPECT_NE(private_key2, public_key2);
111 111
112 EXPECT_NE(public_key1, public_key2); 112 EXPECT_NE(public_key1, public_key2);
113 EXPECT_NE(private_key1, private_key2); 113 EXPECT_NE(private_key1, private_key2);
114 114
115 delegate_.set_next_public_key(kTestPublicKey); 115 delegate_.set_next_public_key(kTestPublicKey);
116 std::string public_key3, private_key3; 116 std::string public_key3, private_key3;
117 delegate_.GenerateKeyPair( 117 delegate_.GenerateKeyPair(
118 base::Bind(&SaveKeyPair, &public_key3, &private_key3)); 118 base::Bind(&SaveKeyPair, &public_key3, &private_key3));
119 EXPECT_EQ(kTestPublicKey, public_key3); 119 EXPECT_EQ(kTestPublicKey, public_key3);
120 EXPECT_NE(private_key3, public_key3); 120 EXPECT_NE(private_key3, public_key3);
121 121
122 EXPECT_NE(public_key1, public_key3); 122 EXPECT_NE(public_key1, public_key3);
123 EXPECT_NE(private_key1, private_key3); 123 EXPECT_NE(private_key1, private_key3);
124 } 124 }
125 125
126 TEST_F(ProximityAuthFakeSecureMessageDelegateTest, DeriveKey) { 126 TEST_F(CryptAuthFakeSecureMessageDelegateTest, DeriveKey) {
127 delegate_.set_next_public_key("key_pair_1"); 127 delegate_.set_next_public_key("key_pair_1");
128 std::string public_key1, private_key1; 128 std::string public_key1, private_key1;
129 delegate_.GenerateKeyPair( 129 delegate_.GenerateKeyPair(
130 base::Bind(&SaveKeyPair, &public_key1, &private_key1)); 130 base::Bind(&SaveKeyPair, &public_key1, &private_key1));
131 131
132 delegate_.set_next_public_key("key_pair_2"); 132 delegate_.set_next_public_key("key_pair_2");
133 std::string public_key2, private_key2; 133 std::string public_key2, private_key2;
134 delegate_.GenerateKeyPair( 134 delegate_.GenerateKeyPair(
135 base::Bind(&SaveKeyPair, &public_key2, &private_key2)); 135 base::Bind(&SaveKeyPair, &public_key2, &private_key2));
136 136
137 std::string symmetric_key1, symmetric_key2; 137 std::string symmetric_key1, symmetric_key2;
138 delegate_.DeriveKey(private_key1, public_key2, 138 delegate_.DeriveKey(private_key1, public_key2,
139 base::Bind(&SaveString, &symmetric_key1)); 139 base::Bind(&SaveString, &symmetric_key1));
140 delegate_.DeriveKey(private_key2, public_key1, 140 delegate_.DeriveKey(private_key2, public_key1,
141 base::Bind(&SaveString, &symmetric_key2)); 141 base::Bind(&SaveString, &symmetric_key2));
142 142
143 EXPECT_EQ(symmetric_key1, symmetric_key2); 143 EXPECT_EQ(symmetric_key1, symmetric_key2);
144 } 144 }
145 145
146 TEST_F(ProximityAuthFakeSecureMessageDelegateTest, 146 TEST_F(CryptAuthFakeSecureMessageDelegateTest,
147 CreateAndUnwrapWithSymmetricKey) { 147 CreateAndUnwrapWithSymmetricKey) {
148 // Create SecureMessage using symmetric key. 148 // Create SecureMessage using symmetric key.
149 SecureMessageDelegate::CreateOptions create_options = 149 SecureMessageDelegate::CreateOptions create_options =
150 GetCreateOptions(securemessage::AES_256_CBC, securemessage::HMAC_SHA256); 150 GetCreateOptions(securemessage::AES_256_CBC, securemessage::HMAC_SHA256);
151 std::string serialized_message; 151 std::string serialized_message;
152 delegate_.CreateSecureMessage(kPayload, kSymmetricKey, create_options, 152 delegate_.CreateSecureMessage(kPayload, kSymmetricKey, create_options,
153 base::Bind(&SaveString, &serialized_message)); 153 base::Bind(&SaveString, &serialized_message));
154 154
155 CheckSerializedSecureMessage(serialized_message, create_options); 155 CheckSerializedSecureMessage(serialized_message, create_options);
156 156
157 // Unwrap SecureMessage using symmetric key. 157 // Unwrap SecureMessage using symmetric key.
158 SecureMessageDelegate::UnwrapOptions unwrap_options = 158 SecureMessageDelegate::UnwrapOptions unwrap_options =
159 GetUnwrapOptions(securemessage::AES_256_CBC, securemessage::HMAC_SHA256); 159 GetUnwrapOptions(securemessage::AES_256_CBC, securemessage::HMAC_SHA256);
160 std::string payload; 160 std::string payload;
161 securemessage::Header header; 161 securemessage::Header header;
162 delegate_.UnwrapSecureMessage( 162 delegate_.UnwrapSecureMessage(
163 serialized_message, kSymmetricKey, unwrap_options, 163 serialized_message, kSymmetricKey, unwrap_options,
164 base::Bind(&SaveUnwrapResults, &payload, &header)); 164 base::Bind(&SaveUnwrapResults, &payload, &header));
165 165
166 EXPECT_EQ(kPayload, payload); 166 EXPECT_EQ(kPayload, payload);
167 } 167 }
168 168
169 TEST_F(ProximityAuthFakeSecureMessageDelegateTest, 169 TEST_F(CryptAuthFakeSecureMessageDelegateTest,
170 CreateAndUnwrapWithAsymmetricKey) { 170 CreateAndUnwrapWithAsymmetricKey) {
171 delegate_.set_next_public_key(kTestPublicKey); 171 delegate_.set_next_public_key(kTestPublicKey);
172 std::string public_key, private_key; 172 std::string public_key, private_key;
173 delegate_.GenerateKeyPair( 173 delegate_.GenerateKeyPair(
174 base::Bind(&SaveKeyPair, &public_key, &private_key)); 174 base::Bind(&SaveKeyPair, &public_key, &private_key));
175 175
176 // Create SecureMessage using asymmetric key. 176 // Create SecureMessage using asymmetric key.
177 SecureMessageDelegate::CreateOptions create_options = 177 SecureMessageDelegate::CreateOptions create_options =
178 GetCreateOptions(securemessage::NONE, securemessage::ECDSA_P256_SHA256); 178 GetCreateOptions(securemessage::NONE, securemessage::ECDSA_P256_SHA256);
179 std::string serialized_message; 179 std::string serialized_message;
180 delegate_.CreateSecureMessage(kPayload, private_key, create_options, 180 delegate_.CreateSecureMessage(kPayload, private_key, create_options,
181 base::Bind(&SaveString, &serialized_message)); 181 base::Bind(&SaveString, &serialized_message));
182 182
183 CheckSerializedSecureMessage(serialized_message, create_options); 183 CheckSerializedSecureMessage(serialized_message, create_options);
184 184
185 // Unwrap SecureMessage using symmetric key. 185 // Unwrap SecureMessage using symmetric key.
186 SecureMessageDelegate::UnwrapOptions unwrap_options = 186 SecureMessageDelegate::UnwrapOptions unwrap_options =
187 GetUnwrapOptions(securemessage::NONE, securemessage::ECDSA_P256_SHA256); 187 GetUnwrapOptions(securemessage::NONE, securemessage::ECDSA_P256_SHA256);
188 std::string payload; 188 std::string payload;
189 securemessage::Header header; 189 securemessage::Header header;
190 delegate_.UnwrapSecureMessage( 190 delegate_.UnwrapSecureMessage(
191 serialized_message, public_key, unwrap_options, 191 serialized_message, public_key, unwrap_options,
192 base::Bind(&SaveUnwrapResults, &payload, &header)); 192 base::Bind(&SaveUnwrapResults, &payload, &header));
193 193
194 EXPECT_EQ(kPayload, payload); 194 EXPECT_EQ(kPayload, payload);
195 } 195 }
196 196
197 TEST_F(ProximityAuthFakeSecureMessageDelegateTest, GetPrivateKeyForPublicKey) { 197 TEST_F(CryptAuthFakeSecureMessageDelegateTest, GetPrivateKeyForPublicKey) {
198 delegate_.set_next_public_key(kTestPublicKey); 198 delegate_.set_next_public_key(kTestPublicKey);
199 std::string public_key, private_key; 199 std::string public_key, private_key;
200 delegate_.GenerateKeyPair( 200 delegate_.GenerateKeyPair(
201 base::Bind(&SaveKeyPair, &public_key, &private_key)); 201 base::Bind(&SaveKeyPair, &public_key, &private_key));
202 EXPECT_EQ(kTestPublicKey, public_key); 202 EXPECT_EQ(kTestPublicKey, public_key);
203 EXPECT_EQ(private_key, delegate_.GetPrivateKeyForPublicKey(kTestPublicKey)); 203 EXPECT_EQ(private_key, delegate_.GetPrivateKeyForPublicKey(kTestPublicKey));
204 } 204 }
205 205
206 } // proximity_auth 206 } // proximity_auth
OLDNEW
« no previous file with comments | « components/cryptauth/fake_secure_message_delegate.cc ('k') | components/cryptauth/mock_cryptauth_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698