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

Side by Side Diff: net/quic/crypto/crypto_utils_test.cc

Issue 2193073003: Move shared files in net/quic/ into net/quic/core/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: io_thread_unittest.cc Created 4 years, 4 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
« no previous file with comments | « net/quic/crypto/crypto_utils.cc ('k') | net/quic/crypto/curve25519_key_exchange.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 "net/quic/crypto/crypto_utils.h"
6
7 #include "net/quic/test_tools/quic_test_utils.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 using std::string;
11
12 namespace net {
13 namespace test {
14 namespace {
15
16 TEST(CryptoUtilsTest, IsValidSNI) {
17 // IP as SNI.
18 EXPECT_FALSE(CryptoUtils::IsValidSNI("192.168.0.1"));
19 // SNI without any dot.
20 EXPECT_FALSE(CryptoUtils::IsValidSNI("somedomain"));
21 // Invalid RFC2396 hostname
22 // TODO(rtenneti): Support RFC2396 hostname.
23 // EXPECT_FALSE(CryptoUtils::IsValidSNI("some_domain.com"));
24 // An empty string must be invalid otherwise the QUIC client will try sending
25 // it.
26 EXPECT_FALSE(CryptoUtils::IsValidSNI(""));
27
28 // Valid SNI
29 EXPECT_TRUE(CryptoUtils::IsValidSNI("test.google.com"));
30 }
31
32 TEST(CryptoUtilsTest, NormalizeHostname) {
33 struct {
34 const char *input, *expected;
35 } tests[] = {
36 {
37 "www.google.com", "www.google.com",
38 },
39 {
40 "WWW.GOOGLE.COM", "www.google.com",
41 },
42 {
43 "www.google.com.", "www.google.com",
44 },
45 {
46 "www.google.COM.", "www.google.com",
47 },
48 {
49 "www.google.com..", "www.google.com",
50 },
51 {
52 "www.google.com........", "www.google.com",
53 },
54 };
55
56 for (size_t i = 0; i < arraysize(tests); ++i) {
57 char buf[256];
58 snprintf(buf, sizeof(buf), "%s", tests[i].input);
59 EXPECT_EQ(string(tests[i].expected), CryptoUtils::NormalizeHostname(buf));
60 }
61 }
62
63 TEST(CryptoUtilsTest, TestExportKeyingMaterial) {
64 const struct TestVector {
65 // Input (strings of hexadecimal digits):
66 const char* subkey_secret;
67 const char* label;
68 const char* context;
69 size_t result_len;
70
71 // Expected output (string of hexadecimal digits):
72 const char* expected; // Null if it should fail.
73 } test_vector[] = {
74 // Try a typical input
75 {"4823c1189ecc40fce888fbb4cf9ae6254f19ba12e6d9af54788f195a6f509ca3",
76 "e934f78d7a71dd85420fceeb8cea0317",
77 "b8d766b5d3c8aba0009c7ed3de553eba53b4de1030ea91383dcdf724cd8b7217", 32,
78 "a9979da0d5f1c1387d7cbe68f5c4163ddb445a03c4ad6ee72cb49d56726d679e"},
79 // Don't let the label contain nulls
80 {"14fe51e082ffee7d1b4d8d4ab41f8c55", "3132333435363700",
81 "58585858585858585858585858585858", 16, nullptr},
82 // Make sure nulls in the context are fine
83 {"d862c2e36b0a42f7827c67ebc8d44df7", "7a5b95e4e8378123",
84 "4142434445464700", 16, "12d418c6d0738a2e4d85b2d0170f76e1"},
85 // ... and give a different result than without
86 {"d862c2e36b0a42f7827c67ebc8d44df7", "7a5b95e4e8378123", "41424344454647",
87 16, "abfa1c479a6e3ffb98a11dee7d196408"},
88 // Try weird lengths
89 {"d0ec8a34f6cc9a8c96", "49711798cc6251",
90 "933d4a2f30d22f089cfba842791116adc121e0", 23,
91 "c9a46ed0757bd1812f1f21b4d41e62125fec8364a21db7"},
92 };
93
94 for (size_t i = 0; i < arraysize(test_vector); i++) {
95 // Decode the test vector.
96 string subkey_secret = QuicUtils::HexDecode(test_vector[i].subkey_secret);
97 string label = QuicUtils::HexDecode(test_vector[i].label);
98 string context = QuicUtils::HexDecode(test_vector[i].context);
99 size_t result_len = test_vector[i].result_len;
100 bool expect_ok = test_vector[i].expected != nullptr;
101 string expected;
102 if (expect_ok) {
103 expected = QuicUtils::HexDecode(test_vector[i].expected);
104 }
105
106 string result;
107 bool ok = CryptoUtils::ExportKeyingMaterial(subkey_secret, label, context,
108 result_len, &result);
109 EXPECT_EQ(expect_ok, ok);
110 if (expect_ok) {
111 EXPECT_EQ(result_len, result.length());
112 test::CompareCharArraysWithHexError("HKDF output", result.data(),
113 result.length(), expected.data(),
114 expected.length());
115 }
116 }
117 }
118
119 TEST(CryptoUtilsTest, HandshakeFailureReasonToString) {
120 EXPECT_STREQ("HANDSHAKE_OK",
121 CryptoUtils::HandshakeFailureReasonToString(HANDSHAKE_OK));
122 EXPECT_STREQ("CLIENT_NONCE_UNKNOWN_FAILURE",
123 CryptoUtils::HandshakeFailureReasonToString(
124 CLIENT_NONCE_UNKNOWN_FAILURE));
125 EXPECT_STREQ("CLIENT_NONCE_INVALID_FAILURE",
126 CryptoUtils::HandshakeFailureReasonToString(
127 CLIENT_NONCE_INVALID_FAILURE));
128 EXPECT_STREQ("CLIENT_NONCE_NOT_UNIQUE_FAILURE",
129 CryptoUtils::HandshakeFailureReasonToString(
130 CLIENT_NONCE_NOT_UNIQUE_FAILURE));
131 EXPECT_STREQ("CLIENT_NONCE_INVALID_ORBIT_FAILURE",
132 CryptoUtils::HandshakeFailureReasonToString(
133 CLIENT_NONCE_INVALID_ORBIT_FAILURE));
134 EXPECT_STREQ("CLIENT_NONCE_INVALID_TIME_FAILURE",
135 CryptoUtils::HandshakeFailureReasonToString(
136 CLIENT_NONCE_INVALID_TIME_FAILURE));
137 EXPECT_STREQ("CLIENT_NONCE_STRIKE_REGISTER_TIMEOUT",
138 CryptoUtils::HandshakeFailureReasonToString(
139 CLIENT_NONCE_STRIKE_REGISTER_TIMEOUT));
140 EXPECT_STREQ("CLIENT_NONCE_STRIKE_REGISTER_FAILURE",
141 CryptoUtils::HandshakeFailureReasonToString(
142 CLIENT_NONCE_STRIKE_REGISTER_FAILURE));
143 EXPECT_STREQ("SERVER_NONCE_DECRYPTION_FAILURE",
144 CryptoUtils::HandshakeFailureReasonToString(
145 SERVER_NONCE_DECRYPTION_FAILURE));
146 EXPECT_STREQ("SERVER_NONCE_INVALID_FAILURE",
147 CryptoUtils::HandshakeFailureReasonToString(
148 SERVER_NONCE_INVALID_FAILURE));
149 EXPECT_STREQ("SERVER_NONCE_NOT_UNIQUE_FAILURE",
150 CryptoUtils::HandshakeFailureReasonToString(
151 SERVER_NONCE_NOT_UNIQUE_FAILURE));
152 EXPECT_STREQ("SERVER_NONCE_INVALID_TIME_FAILURE",
153 CryptoUtils::HandshakeFailureReasonToString(
154 SERVER_NONCE_INVALID_TIME_FAILURE));
155 EXPECT_STREQ("SERVER_NONCE_REQUIRED_FAILURE",
156 CryptoUtils::HandshakeFailureReasonToString(
157 SERVER_NONCE_REQUIRED_FAILURE));
158 EXPECT_STREQ("SERVER_CONFIG_INCHOATE_HELLO_FAILURE",
159 CryptoUtils::HandshakeFailureReasonToString(
160 SERVER_CONFIG_INCHOATE_HELLO_FAILURE));
161 EXPECT_STREQ("SERVER_CONFIG_UNKNOWN_CONFIG_FAILURE",
162 CryptoUtils::HandshakeFailureReasonToString(
163 SERVER_CONFIG_UNKNOWN_CONFIG_FAILURE));
164 EXPECT_STREQ("SOURCE_ADDRESS_TOKEN_INVALID_FAILURE",
165 CryptoUtils::HandshakeFailureReasonToString(
166 SOURCE_ADDRESS_TOKEN_INVALID_FAILURE));
167 EXPECT_STREQ("SOURCE_ADDRESS_TOKEN_DECRYPTION_FAILURE",
168 CryptoUtils::HandshakeFailureReasonToString(
169 SOURCE_ADDRESS_TOKEN_DECRYPTION_FAILURE));
170 EXPECT_STREQ("SOURCE_ADDRESS_TOKEN_PARSE_FAILURE",
171 CryptoUtils::HandshakeFailureReasonToString(
172 SOURCE_ADDRESS_TOKEN_PARSE_FAILURE));
173 EXPECT_STREQ("SOURCE_ADDRESS_TOKEN_DIFFERENT_IP_ADDRESS_FAILURE",
174 CryptoUtils::HandshakeFailureReasonToString(
175 SOURCE_ADDRESS_TOKEN_DIFFERENT_IP_ADDRESS_FAILURE));
176 EXPECT_STREQ("SOURCE_ADDRESS_TOKEN_CLOCK_SKEW_FAILURE",
177 CryptoUtils::HandshakeFailureReasonToString(
178 SOURCE_ADDRESS_TOKEN_CLOCK_SKEW_FAILURE));
179 EXPECT_STREQ("SOURCE_ADDRESS_TOKEN_EXPIRED_FAILURE",
180 CryptoUtils::HandshakeFailureReasonToString(
181 SOURCE_ADDRESS_TOKEN_EXPIRED_FAILURE));
182 EXPECT_STREQ("INVALID_EXPECTED_LEAF_CERTIFICATE",
183 CryptoUtils::HandshakeFailureReasonToString(
184 INVALID_EXPECTED_LEAF_CERTIFICATE));
185 EXPECT_STREQ("MAX_FAILURE_REASON",
186 CryptoUtils::HandshakeFailureReasonToString(MAX_FAILURE_REASON));
187 EXPECT_STREQ(
188 "INVALID_HANDSHAKE_FAILURE_REASON",
189 CryptoUtils::HandshakeFailureReasonToString(
190 static_cast<HandshakeFailureReason>(MAX_FAILURE_REASON + 1)));
191 }
192
193 } // namespace
194 } // namespace test
195 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/crypto/crypto_utils.cc ('k') | net/quic/crypto/curve25519_key_exchange.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698