OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "net/quic/crypto/channel_id.h" | 5 #include "net/quic/crypto/channel_id.h" |
6 | 6 |
7 #include "net/quic/test_tools/crypto_test_utils.h" | 7 #include "net/quic/test_tools/crypto_test_utils.h" |
8 #include "net/quic/test_tools/quic_test_utils.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
9 | 10 |
10 using base::StringPiece; | 11 using base::StringPiece; |
11 using std::string; | 12 using std::string; |
12 | 13 |
13 namespace net { | 14 namespace net { |
14 namespace test { | 15 namespace test { |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 "03c7e0d628e50736c7512df0acfa9f2320bd102229f46495ae6d0857cc452a84", | 179 "03c7e0d628e50736c7512df0acfa9f2320bd102229f46495ae6d0857cc452a84", |
179 "2d98ea01f754d34bbc3003df5050200abf445ec728556d7ed7d5c54c55552b6d", | 180 "2d98ea01f754d34bbc3003df5050200abf445ec728556d7ed7d5c54c55552b6d", |
180 "9b52672742d637a32add056dfd6d8792f2a33c2e69dafabea09b960bc61e230a", | 181 "9b52672742d637a32add056dfd6d8792f2a33c2e69dafabea09b960bc61e230a", |
181 "06108e525f845d0155bf60193222b3219c98e3d49424c2fb2a0987f825c17959", | 182 "06108e525f845d0155bf60193222b3219c98e3d49424c2fb2a0987f825c17959", |
182 "62b5cdd591e5b507e560167ba8f6f7cda74673eb315680cb89ccbc4eec477dce", | 183 "62b5cdd591e5b507e560167ba8f6f7cda74673eb315680cb89ccbc4eec477dce", |
183 true // P (0 ) | 184 true // P (0 ) |
184 }, | 185 }, |
185 { NULL } | 186 { NULL } |
186 }; | 187 }; |
187 | 188 |
188 // Returns true if |ch| is a lowercase hexadecimal digit. | |
189 bool IsHexDigit(char ch) { | |
190 return ('0' <= ch && ch <= '9') || ('a' <= ch && ch <= 'f'); | |
191 } | |
192 | |
193 // Converts a lowercase hexadecimal digit to its integer value. | |
194 int HexDigitToInt(char ch) { | |
195 if ('0' <= ch && ch <= '9') { | |
196 return ch - '0'; | |
197 } | |
198 return ch - 'a' + 10; | |
199 } | |
200 | |
201 // |in| is a string consisting of lowercase hexadecimal digits, where | |
202 // every two digits represent one byte. |out| is a buffer of size |max_len|. | |
203 // Converts |in| to bytes and stores the bytes in the |out| buffer. The | |
204 // number of bytes converted is returned in |*out_len|. Returns true on | |
205 // success, false on failure. | |
206 bool DecodeHexString(const char* in, | |
207 char* out, | |
208 size_t* out_len, | |
209 size_t max_len) { | |
210 if (!in) { | |
211 *out_len = (size_t)-1; | |
212 return true; | |
213 } | |
214 *out_len = 0; | |
215 while (*in != '\0') { | |
216 if (!IsHexDigit(*in) || !IsHexDigit(*(in + 1))) { | |
217 return false; | |
218 } | |
219 if (*out_len >= max_len) { | |
220 return false; | |
221 } | |
222 out[*out_len] = HexDigitToInt(*in) * 16 + HexDigitToInt(*(in + 1)); | |
223 (*out_len)++; | |
224 in += 2; | |
225 } | |
226 return true; | |
227 } | |
228 | |
229 } // namespace | 189 } // namespace |
230 | 190 |
231 // A known answer test for ChannelIDVerifier. | 191 // A known answer test for ChannelIDVerifier. |
232 TEST(ChannelIDTest, VerifyKnownAnswerTest) { | 192 TEST(ChannelIDTest, VerifyKnownAnswerTest) { |
233 char msg[1024]; | 193 string msg; |
234 size_t msg_len; | 194 string qx; |
235 char key[64]; | 195 string qy; |
236 size_t qx_len; | 196 string r; |
237 size_t qy_len; | 197 string s; |
238 char signature[64]; | |
239 size_t r_len; | |
240 size_t s_len; | |
241 | 198 |
242 for (size_t i = 0; test_vector[i].msg != NULL; i++) { | 199 for (size_t i = 0; test_vector[i].msg != NULL; i++) { |
243 SCOPED_TRACE(i); | 200 SCOPED_TRACE(i); |
244 // Decode the test vector. | 201 // Decode the test vector. |
245 ASSERT_TRUE( | 202 ASSERT_TRUE(DecodeHexString(test_vector[i].msg, &msg)); |
246 DecodeHexString(test_vector[i].msg, msg, &msg_len, sizeof(msg))); | 203 ASSERT_TRUE(DecodeHexString(test_vector[i].qx, &qx)); |
247 ASSERT_TRUE(DecodeHexString(test_vector[i].qx, key, &qx_len, sizeof(key))); | 204 ASSERT_TRUE(DecodeHexString(test_vector[i].qy, &qy)); |
248 ASSERT_TRUE(DecodeHexString(test_vector[i].qy, key + qx_len, &qy_len, | 205 ASSERT_TRUE(DecodeHexString(test_vector[i].r, &r)); |
249 sizeof(key) - qx_len)); | 206 ASSERT_TRUE(DecodeHexString(test_vector[i].s, &s)); |
250 ASSERT_TRUE(DecodeHexString(test_vector[i].r, signature, &r_len, | 207 |
251 sizeof(signature))); | 208 string key = qx + qy; |
252 ASSERT_TRUE(DecodeHexString(test_vector[i].s, signature + r_len, &s_len, | 209 string signature = r + s; |
253 sizeof(signature) - r_len)); | |
254 | 210 |
255 // The test vector's lengths should look sane. | 211 // The test vector's lengths should look sane. |
256 EXPECT_EQ(sizeof(key) / 2, qx_len); | 212 EXPECT_EQ(key.size() / 2, qx.size()); |
257 EXPECT_EQ(sizeof(key) / 2, qy_len); | 213 EXPECT_EQ(key.size() / 2, qy.size()); |
258 EXPECT_EQ(sizeof(signature) / 2, r_len); | 214 EXPECT_EQ(signature.size() / 2, r.size()); |
259 EXPECT_EQ(sizeof(signature) / 2, s_len); | 215 EXPECT_EQ(signature.size() / 2, s.size()); |
wtc
2013/11/04 01:58:27
The new tests are weaker than the original tests.
eroman
2013/11/04 19:39:58
Done.
| |
260 | 216 |
261 EXPECT_EQ(test_vector[i].result, | 217 EXPECT_EQ(test_vector[i].result, |
262 ChannelIDVerifier::VerifyRaw( | 218 ChannelIDVerifier::VerifyRaw(key, msg, signature, false)); |
263 StringPiece(key, sizeof(key)), | |
264 StringPiece(msg, msg_len), | |
265 StringPiece(signature, sizeof(signature)), | |
266 false)); | |
267 } | 219 } |
268 } | 220 } |
269 | 221 |
270 TEST(ChannelIDTest, SignAndVerify) { | 222 TEST(ChannelIDTest, SignAndVerify) { |
271 scoped_ptr<ChannelIDSigner> signer( | 223 scoped_ptr<ChannelIDSigner> signer( |
272 CryptoTestUtils::ChannelIDSignerForTesting()); | 224 CryptoTestUtils::ChannelIDSignerForTesting()); |
273 | 225 |
274 const string signed_data = "signed data"; | 226 const string signed_data = "signed data"; |
275 const string hostname = "foo.example.com"; | 227 const string hostname = "foo.example.com"; |
276 string key, signature; | 228 string key, signature; |
(...skipping 17 matching lines...) Expand all Loading... | |
294 bad_signature[1] ^= 0x80; | 246 bad_signature[1] ^= 0x80; |
295 EXPECT_FALSE(ChannelIDVerifier::Verify( | 247 EXPECT_FALSE(ChannelIDVerifier::Verify( |
296 key, signed_data, string(bad_signature.get(), signature.size()))); | 248 key, signed_data, string(bad_signature.get(), signature.size()))); |
297 | 249 |
298 EXPECT_FALSE(ChannelIDVerifier::Verify( | 250 EXPECT_FALSE(ChannelIDVerifier::Verify( |
299 key, "wrong signed data", signature)); | 251 key, "wrong signed data", signature)); |
300 } | 252 } |
301 | 253 |
302 } // namespace test | 254 } // namespace test |
303 } // namespace net | 255 } // namespace net |
OLD | NEW |