OLD | NEW |
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 "net/cert/internal/verify_name_match.h" | 5 #include "net/cert/internal/verify_name_match.h" |
6 | 6 |
7 #include "base/base_paths.h" | |
8 #include "base/files/file_path.h" | |
9 #include "base/files/file_util.h" | |
10 #include "base/path_service.h" | |
11 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
13 #include "net/cert/pem_tokenizer.h" | 9 #include "net/cert/internal/test_helpers.h" |
14 #include "net/der/input.h" | 10 #include "net/der/input.h" |
15 #include "net/der/parser.h" | 11 #include "net/der/parser.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
17 | 13 |
18 namespace net { | 14 namespace net { |
19 namespace { | 15 namespace { |
20 | 16 |
21 der::Input SequenceValueFromString(const std::string& s) { | 17 der::Input SequenceValueFromString(const std::string* s) { |
22 der::Parser parser( | 18 der::Parser parser(InputFromString(s)); |
23 der::Input(reinterpret_cast<const uint8_t*>(s.data()), s.size())); | |
24 der::Input data; | 19 der::Input data; |
25 if (!parser.ReadTag(der::kSequence, &data)) { | 20 if (!parser.ReadTag(der::kSequence, &data)) { |
26 ADD_FAILURE(); | 21 ADD_FAILURE(); |
27 return der::Input(); | 22 return der::Input(); |
28 } | 23 } |
29 if (parser.HasMore()) { | 24 if (parser.HasMore()) { |
30 ADD_FAILURE(); | 25 ADD_FAILURE(); |
31 return der::Input(); | 26 return der::Input(); |
32 } | 27 } |
33 return data; | 28 return data; |
34 } | 29 } |
35 | 30 |
36 // Loads test data from file. The filename is constructed from the parameters: | 31 // Loads test data from file. The filename is constructed from the parameters: |
37 // |prefix| describes the type of data being tested, e.g. "ascii", | 32 // |prefix| describes the type of data being tested, e.g. "ascii", |
38 // "unicode_bmp", "unicode_supplementary", and "invalid". | 33 // "unicode_bmp", "unicode_supplementary", and "invalid". |
39 // |value_type| indicates what ASN.1 type is used to encode the data. | 34 // |value_type| indicates what ASN.1 type is used to encode the data. |
40 // |suffix| indicates any additional modifications, such as caseswapping, | 35 // |suffix| indicates any additional modifications, such as caseswapping, |
41 // whitespace adding, etc. | 36 // whitespace adding, etc. |
42 ::testing::AssertionResult LoadTestData(const std::string& prefix, | 37 ::testing::AssertionResult LoadTestData(const std::string& prefix, |
43 const std::string& value_type, | 38 const std::string& value_type, |
44 const std::string& suffix, | 39 const std::string& suffix, |
45 std::string* result) { | 40 std::string* result) { |
46 base::FilePath src_root; | 41 std::string path = "net/data/verify_name_match_unittest/names/" + prefix + |
47 PathService::Get(base::DIR_SOURCE_ROOT, &src_root); | 42 "-" + value_type + "-" + suffix + ".pem"; |
48 std::string filename = prefix + "-" + value_type + "-" + suffix + ".pem"; | |
49 base::FilePath filepath = | |
50 src_root.Append(FILE_PATH_LITERAL( | |
51 "net/data/verify_name_match_unittest/names")) | |
52 .AppendASCII(filename); | |
53 std::string file_data; | |
54 if (!base::ReadFileToString(filepath, &file_data)) { | |
55 return ::testing::AssertionFailure() | |
56 << "ReadFileToString returned false on " << filename; | |
57 } | |
58 | 43 |
59 std::vector<std::string> pem_headers; | 44 const PemBlockMapping mappings[] = { |
60 pem_headers.push_back("NAME"); | 45 {"NAME", result}, |
61 PEMTokenizer pem_tokenizer(file_data, pem_headers); | 46 }; |
62 if (!pem_tokenizer.GetNext()) { | |
63 return ::testing::AssertionFailure() << "PEM.GetNext returned false on " | |
64 << filename; | |
65 } | |
66 | 47 |
67 result->assign(pem_tokenizer.data()); | 48 return ReadTestDataFromPemFile(path, mappings); |
68 return ::testing::AssertionSuccess(); | |
69 } | 49 } |
70 | 50 |
71 bool TypesAreComparable(const std::string& type_1, const std::string& type_2) { | 51 bool TypesAreComparable(const std::string& type_1, const std::string& type_2) { |
72 if (type_1 == type_2) | 52 if (type_1 == type_2) |
73 return true; | 53 return true; |
74 if ((type_1 == "PRINTABLESTRING" || type_1 == "UTF8" || | 54 if ((type_1 == "PRINTABLESTRING" || type_1 == "UTF8" || |
75 type_1 == "BMPSTRING" || type_1 == "UNIVERSALSTRING") && | 55 type_1 == "BMPSTRING" || type_1 == "UNIVERSALSTRING") && |
76 (type_2 == "PRINTABLESTRING" || type_2 == "UTF8" || | 56 (type_2 == "PRINTABLESTRING" || type_2 == "UTF8" || |
77 type_2 == "BMPSTRING" || type_2 == "UNIVERSALSTRING")) { | 57 type_2 == "BMPSTRING" || type_2 == "UNIVERSALSTRING")) { |
78 return true; | 58 return true; |
(...skipping 22 matching lines...) Expand all Loading... |
101 public: | 81 public: |
102 std::string value_type() const { return ::testing::get<0>(GetParam()); } | 82 std::string value_type() const { return ::testing::get<0>(GetParam()); } |
103 std::string suffix() const { return ::testing::get<1>(GetParam()); } | 83 std::string suffix() const { return ::testing::get<1>(GetParam()); } |
104 }; | 84 }; |
105 | 85 |
106 // Compare each input against itself, verifies that all input data is parsed | 86 // Compare each input against itself, verifies that all input data is parsed |
107 // successfully. | 87 // successfully. |
108 TEST_P(VerifyNameMatchSimpleTest, ExactEquality) { | 88 TEST_P(VerifyNameMatchSimpleTest, ExactEquality) { |
109 std::string der; | 89 std::string der; |
110 ASSERT_TRUE(LoadTestData("ascii", value_type(), suffix(), &der)); | 90 ASSERT_TRUE(LoadTestData("ascii", value_type(), suffix(), &der)); |
111 EXPECT_TRUE(VerifyNameMatch(SequenceValueFromString(der), | 91 EXPECT_TRUE(VerifyNameMatch(SequenceValueFromString(&der), |
112 SequenceValueFromString(der))); | 92 SequenceValueFromString(&der))); |
113 | 93 |
114 std::string der_extra_attr; | 94 std::string der_extra_attr; |
115 ASSERT_TRUE(LoadTestData("ascii", value_type(), suffix() + "-extra_attr", | 95 ASSERT_TRUE(LoadTestData("ascii", value_type(), suffix() + "-extra_attr", |
116 &der_extra_attr)); | 96 &der_extra_attr)); |
117 EXPECT_TRUE(VerifyNameMatch(SequenceValueFromString(der_extra_attr), | 97 EXPECT_TRUE(VerifyNameMatch(SequenceValueFromString(&der_extra_attr), |
118 SequenceValueFromString(der_extra_attr))); | 98 SequenceValueFromString(&der_extra_attr))); |
119 | 99 |
120 std::string der_extra_rdn; | 100 std::string der_extra_rdn; |
121 ASSERT_TRUE(LoadTestData("ascii", value_type(), suffix() + "-extra_rdn", | 101 ASSERT_TRUE(LoadTestData("ascii", value_type(), suffix() + "-extra_rdn", |
122 &der_extra_rdn)); | 102 &der_extra_rdn)); |
123 EXPECT_TRUE(VerifyNameMatch(SequenceValueFromString(der_extra_rdn), | 103 EXPECT_TRUE(VerifyNameMatch(SequenceValueFromString(&der_extra_rdn), |
124 SequenceValueFromString(der_extra_rdn))); | 104 SequenceValueFromString(&der_extra_rdn))); |
125 } | 105 } |
126 | 106 |
127 // Ensure that a Name does not match another Name which is exactly the same but | 107 // Ensure that a Name does not match another Name which is exactly the same but |
128 // with an extra attribute in one Relative Distinguished Name. | 108 // with an extra attribute in one Relative Distinguished Name. |
129 TEST_P(VerifyNameMatchSimpleTest, ExtraAttrDoesNotMatch) { | 109 TEST_P(VerifyNameMatchSimpleTest, ExtraAttrDoesNotMatch) { |
130 std::string der; | 110 std::string der; |
131 ASSERT_TRUE(LoadTestData("ascii", value_type(), suffix(), &der)); | 111 ASSERT_TRUE(LoadTestData("ascii", value_type(), suffix(), &der)); |
132 std::string der_extra_attr; | 112 std::string der_extra_attr; |
133 ASSERT_TRUE(LoadTestData("ascii", value_type(), suffix() + "-extra_attr", | 113 ASSERT_TRUE(LoadTestData("ascii", value_type(), suffix() + "-extra_attr", |
134 &der_extra_attr)); | 114 &der_extra_attr)); |
135 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(der), | 115 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(&der), |
136 SequenceValueFromString(der_extra_attr))); | 116 SequenceValueFromString(&der_extra_attr))); |
137 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(der_extra_attr), | 117 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(&der_extra_attr), |
138 SequenceValueFromString(der))); | 118 SequenceValueFromString(&der))); |
139 } | 119 } |
140 | 120 |
141 // Ensure that a Name does not match another Name which is exactly the same but | 121 // Ensure that a Name does not match another Name which is exactly the same but |
142 // with an extra Relative Distinguished Name. | 122 // with an extra Relative Distinguished Name. |
143 TEST_P(VerifyNameMatchSimpleTest, ExtraRdnDoesNotMatch) { | 123 TEST_P(VerifyNameMatchSimpleTest, ExtraRdnDoesNotMatch) { |
144 std::string der; | 124 std::string der; |
145 ASSERT_TRUE(LoadTestData("ascii", value_type(), suffix(), &der)); | 125 ASSERT_TRUE(LoadTestData("ascii", value_type(), suffix(), &der)); |
146 std::string der_extra_rdn; | 126 std::string der_extra_rdn; |
147 ASSERT_TRUE(LoadTestData("ascii", value_type(), suffix() + "-extra_rdn", | 127 ASSERT_TRUE(LoadTestData("ascii", value_type(), suffix() + "-extra_rdn", |
148 &der_extra_rdn)); | 128 &der_extra_rdn)); |
149 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(der), | 129 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(&der), |
150 SequenceValueFromString(der_extra_rdn))); | 130 SequenceValueFromString(&der_extra_rdn))); |
151 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(der_extra_rdn), | 131 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(&der_extra_rdn), |
152 SequenceValueFromString(der))); | 132 SequenceValueFromString(&der))); |
153 } | 133 } |
154 | 134 |
155 // Runs VerifyNameMatchSimpleTest for all combinations of value_type and and | 135 // Runs VerifyNameMatchSimpleTest for all combinations of value_type and and |
156 // suffix. | 136 // suffix. |
157 INSTANTIATE_TEST_CASE_P(InstantiationName, | 137 INSTANTIATE_TEST_CASE_P(InstantiationName, |
158 VerifyNameMatchSimpleTest, | 138 VerifyNameMatchSimpleTest, |
159 ::testing::Combine(::testing::ValuesIn(kValueTypes), | 139 ::testing::Combine(::testing::ValuesIn(kValueTypes), |
160 ::testing::ValuesIn(kMangleTypes))); | 140 ::testing::ValuesIn(kMangleTypes))); |
161 | 141 |
162 class VerifyNameMatchNormalizationTest | 142 class VerifyNameMatchNormalizationTest |
163 : public ::testing::TestWithParam<::testing::tuple<bool, const char*>> { | 143 : public ::testing::TestWithParam<::testing::tuple<bool, const char*>> { |
164 public: | 144 public: |
165 bool expected_result() const { return ::testing::get<0>(GetParam()); } | 145 bool expected_result() const { return ::testing::get<0>(GetParam()); } |
166 std::string value_type() const { return ::testing::get<1>(GetParam()); } | 146 std::string value_type() const { return ::testing::get<1>(GetParam()); } |
167 }; | 147 }; |
168 | 148 |
169 // Verify matching is case insensitive (for the types which currently support | 149 // Verify matching is case insensitive (for the types which currently support |
170 // normalization). | 150 // normalization). |
171 TEST_P(VerifyNameMatchNormalizationTest, CaseInsensitivity) { | 151 TEST_P(VerifyNameMatchNormalizationTest, CaseInsensitivity) { |
172 std::string normal; | 152 std::string normal; |
173 ASSERT_TRUE(LoadTestData("ascii", value_type(), "unmangled", &normal)); | 153 ASSERT_TRUE(LoadTestData("ascii", value_type(), "unmangled", &normal)); |
174 std::string case_swap; | 154 std::string case_swap; |
175 ASSERT_TRUE(LoadTestData("ascii", value_type(), "case_swap", &case_swap)); | 155 ASSERT_TRUE(LoadTestData("ascii", value_type(), "case_swap", &case_swap)); |
176 EXPECT_EQ(expected_result(), | 156 EXPECT_EQ(expected_result(), |
177 VerifyNameMatch(SequenceValueFromString(normal), | 157 VerifyNameMatch(SequenceValueFromString(&normal), |
178 SequenceValueFromString(case_swap))); | 158 SequenceValueFromString(&case_swap))); |
179 EXPECT_EQ(expected_result(), | 159 EXPECT_EQ(expected_result(), |
180 VerifyNameMatch(SequenceValueFromString(case_swap), | 160 VerifyNameMatch(SequenceValueFromString(&case_swap), |
181 SequenceValueFromString(normal))); | 161 SequenceValueFromString(&normal))); |
182 } | 162 } |
183 | 163 |
184 // Verify matching folds whitespace (for the types which currently support | 164 // Verify matching folds whitespace (for the types which currently support |
185 // normalization). | 165 // normalization). |
186 TEST_P(VerifyNameMatchNormalizationTest, CollapseWhitespace) { | 166 TEST_P(VerifyNameMatchNormalizationTest, CollapseWhitespace) { |
187 std::string normal; | 167 std::string normal; |
188 ASSERT_TRUE(LoadTestData("ascii", value_type(), "unmangled", &normal)); | 168 ASSERT_TRUE(LoadTestData("ascii", value_type(), "unmangled", &normal)); |
189 std::string whitespace; | 169 std::string whitespace; |
190 ASSERT_TRUE( | 170 ASSERT_TRUE( |
191 LoadTestData("ascii", value_type(), "extra_whitespace", &whitespace)); | 171 LoadTestData("ascii", value_type(), "extra_whitespace", &whitespace)); |
192 EXPECT_EQ(expected_result(), | 172 EXPECT_EQ(expected_result(), |
193 VerifyNameMatch(SequenceValueFromString(normal), | 173 VerifyNameMatch(SequenceValueFromString(&normal), |
194 SequenceValueFromString(whitespace))); | 174 SequenceValueFromString(&whitespace))); |
195 EXPECT_EQ(expected_result(), | 175 EXPECT_EQ(expected_result(), |
196 VerifyNameMatch(SequenceValueFromString(whitespace), | 176 VerifyNameMatch(SequenceValueFromString(&whitespace), |
197 SequenceValueFromString(normal))); | 177 SequenceValueFromString(&normal))); |
198 } | 178 } |
199 | 179 |
200 // Runs VerifyNameMatchNormalizationTest for each (expected_result, value_type) | 180 // Runs VerifyNameMatchNormalizationTest for each (expected_result, value_type) |
201 // tuple. | 181 // tuple. |
202 INSTANTIATE_TEST_CASE_P( | 182 INSTANTIATE_TEST_CASE_P( |
203 InstantiationName, | 183 InstantiationName, |
204 VerifyNameMatchNormalizationTest, | 184 VerifyNameMatchNormalizationTest, |
205 ::testing::Values( | 185 ::testing::Values( |
206 ::testing::make_tuple(true, | 186 ::testing::make_tuple(true, |
207 static_cast<const char*>("PRINTABLESTRING")), | 187 static_cast<const char*>("PRINTABLESTRING")), |
(...skipping 10 matching lines...) Expand all Loading... |
218 std::string value_type_1() const { return ::testing::get<0>(GetParam()); } | 198 std::string value_type_1() const { return ::testing::get<0>(GetParam()); } |
219 std::string value_type_2() const { return ::testing::get<1>(GetParam()); } | 199 std::string value_type_2() const { return ::testing::get<1>(GetParam()); } |
220 }; | 200 }; |
221 | 201 |
222 TEST_P(VerifyNameMatchDifferingTypesTest, NormalizableTypesAreEqual) { | 202 TEST_P(VerifyNameMatchDifferingTypesTest, NormalizableTypesAreEqual) { |
223 std::string der_1; | 203 std::string der_1; |
224 ASSERT_TRUE(LoadTestData("ascii", value_type_1(), "unmangled", &der_1)); | 204 ASSERT_TRUE(LoadTestData("ascii", value_type_1(), "unmangled", &der_1)); |
225 std::string der_2; | 205 std::string der_2; |
226 ASSERT_TRUE(LoadTestData("ascii", value_type_2(), "unmangled", &der_2)); | 206 ASSERT_TRUE(LoadTestData("ascii", value_type_2(), "unmangled", &der_2)); |
227 if (TypesAreComparable(value_type_1(), value_type_2())) { | 207 if (TypesAreComparable(value_type_1(), value_type_2())) { |
228 EXPECT_TRUE(VerifyNameMatch(SequenceValueFromString(der_1), | 208 EXPECT_TRUE(VerifyNameMatch(SequenceValueFromString(&der_1), |
229 SequenceValueFromString(der_2))); | 209 SequenceValueFromString(&der_2))); |
230 } else { | 210 } else { |
231 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(der_1), | 211 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(&der_1), |
232 SequenceValueFromString(der_2))); | 212 SequenceValueFromString(&der_2))); |
233 } | 213 } |
234 } | 214 } |
235 | 215 |
236 // Runs VerifyNameMatchDifferingTypesTest for all combinations of value types in | 216 // Runs VerifyNameMatchDifferingTypesTest for all combinations of value types in |
237 // value_type1 and value_type_2. | 217 // value_type1 and value_type_2. |
238 INSTANTIATE_TEST_CASE_P(InstantiationName, | 218 INSTANTIATE_TEST_CASE_P(InstantiationName, |
239 VerifyNameMatchDifferingTypesTest, | 219 VerifyNameMatchDifferingTypesTest, |
240 ::testing::Combine(::testing::ValuesIn(kValueTypes), | 220 ::testing::Combine(::testing::ValuesIn(kValueTypes), |
241 ::testing::ValuesIn(kValueTypes))); | 221 ::testing::ValuesIn(kValueTypes))); |
242 | 222 |
243 class VerifyNameMatchUnicodeConversionTest | 223 class VerifyNameMatchUnicodeConversionTest |
244 : public ::testing::TestWithParam< | 224 : public ::testing::TestWithParam< |
245 ::testing::tuple<const char*, | 225 ::testing::tuple<const char*, |
246 ::testing::tuple<const char*, const char*>>> { | 226 ::testing::tuple<const char*, const char*>>> { |
247 public: | 227 public: |
248 std::string prefix() const { return ::testing::get<0>(GetParam()); } | 228 std::string prefix() const { return ::testing::get<0>(GetParam()); } |
249 std::string value_type_1() const { | 229 std::string value_type_1() const { |
250 return ::testing::get<0>(::testing::get<1>(GetParam())); | 230 return ::testing::get<0>(::testing::get<1>(GetParam())); |
251 } | 231 } |
252 std::string value_type_2() const { | 232 std::string value_type_2() const { |
253 return ::testing::get<1>(::testing::get<1>(GetParam())); | 233 return ::testing::get<1>(::testing::get<1>(GetParam())); |
254 } | 234 } |
255 }; | 235 }; |
256 | 236 |
257 TEST_P(VerifyNameMatchUnicodeConversionTest, UnicodeConversionsAreEqual) { | 237 TEST_P(VerifyNameMatchUnicodeConversionTest, UnicodeConversionsAreEqual) { |
258 std::string der_1; | 238 std::string der_1; |
259 ASSERT_TRUE(LoadTestData(prefix(), value_type_1(), "unmangled", &der_1)); | 239 ASSERT_TRUE(LoadTestData(prefix(), value_type_1(), "unmangled", &der_1)); |
260 std::string der_2; | 240 std::string der_2; |
261 ASSERT_TRUE(LoadTestData(prefix(), value_type_2(), "unmangled", &der_2)); | 241 ASSERT_TRUE(LoadTestData(prefix(), value_type_2(), "unmangled", &der_2)); |
262 EXPECT_TRUE(VerifyNameMatch(SequenceValueFromString(der_1), | 242 EXPECT_TRUE(VerifyNameMatch(SequenceValueFromString(&der_1), |
263 SequenceValueFromString(der_2))); | 243 SequenceValueFromString(&der_2))); |
264 } | 244 } |
265 | 245 |
266 // Runs VerifyNameMatchUnicodeConversionTest with prefix="unicode_bmp" for all | 246 // Runs VerifyNameMatchUnicodeConversionTest with prefix="unicode_bmp" for all |
267 // combinations of Basic Multilingual Plane-capable value types in value_type1 | 247 // combinations of Basic Multilingual Plane-capable value types in value_type1 |
268 // and value_type_2. | 248 // and value_type_2. |
269 INSTANTIATE_TEST_CASE_P( | 249 INSTANTIATE_TEST_CASE_P( |
270 BMPConversion, | 250 BMPConversion, |
271 VerifyNameMatchUnicodeConversionTest, | 251 VerifyNameMatchUnicodeConversionTest, |
272 ::testing::Combine( | 252 ::testing::Combine( |
273 ::testing::Values("unicode_bmp"), | 253 ::testing::Values("unicode_bmp"), |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 case '-': | 288 case '-': |
309 case '.': | 289 case '.': |
310 case '/': | 290 case '/': |
311 case ':': | 291 case ':': |
312 case '=': | 292 case '=': |
313 case '?': | 293 case '?': |
314 continue; | 294 continue; |
315 } | 295 } |
316 der.replace(replace_location, 1, 1, c); | 296 der.replace(replace_location, 1, 1, c); |
317 // Verification should fail due to the invalid character. | 297 // Verification should fail due to the invalid character. |
318 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(der), | 298 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(&der), |
319 SequenceValueFromString(der))); | 299 SequenceValueFromString(&der))); |
320 } | 300 } |
321 } | 301 } |
322 | 302 |
323 // Matching should fail if an IA5String contains invalid characters. | 303 // Matching should fail if an IA5String contains invalid characters. |
324 TEST(VerifyNameMatchInvalidDataTest, FailOnInvalidIA5StringChars) { | 304 TEST(VerifyNameMatchInvalidDataTest, FailOnInvalidIA5StringChars) { |
325 std::string der; | 305 std::string der; |
326 ASSERT_TRUE(LoadTestData("ascii", "mixed", "rdn_dupetype_sorting_1", &der)); | 306 ASSERT_TRUE(LoadTestData("ascii", "mixed", "rdn_dupetype_sorting_1", &der)); |
327 // Find a known location inside an IA5String in the DER-encoded data. | 307 // Find a known location inside an IA5String in the DER-encoded data. |
328 size_t replace_location = der.find("eXaMple"); | 308 size_t replace_location = der.find("eXaMple"); |
329 ASSERT_NE(std::string::npos, replace_location); | 309 ASSERT_NE(std::string::npos, replace_location); |
330 for (int c = 0; c < 256; ++c) { | 310 for (int c = 0; c < 256; ++c) { |
331 SCOPED_TRACE(base::IntToString(c)); | 311 SCOPED_TRACE(base::IntToString(c)); |
332 der.replace(replace_location, 1, 1, c); | 312 der.replace(replace_location, 1, 1, c); |
333 bool expected_result = (c <= 127); | 313 bool expected_result = (c <= 127); |
334 EXPECT_EQ(expected_result, VerifyNameMatch(SequenceValueFromString(der), | 314 EXPECT_EQ(expected_result, VerifyNameMatch(SequenceValueFromString(&der), |
335 SequenceValueFromString(der))); | 315 SequenceValueFromString(&der))); |
336 } | 316 } |
337 } | 317 } |
338 | 318 |
339 TEST(VerifyNameMatchInvalidDataTest, FailOnAttributeTypeAndValueExtraData) { | 319 TEST(VerifyNameMatchInvalidDataTest, FailOnAttributeTypeAndValueExtraData) { |
340 std::string invalid; | 320 std::string invalid; |
341 ASSERT_TRUE( | 321 ASSERT_TRUE( |
342 LoadTestData("invalid", "AttributeTypeAndValue", "extradata", &invalid)); | 322 LoadTestData("invalid", "AttributeTypeAndValue", "extradata", &invalid)); |
343 // Verification should fail due to extra element in AttributeTypeAndValue | 323 // Verification should fail due to extra element in AttributeTypeAndValue |
344 // sequence. | 324 // sequence. |
345 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(invalid), | 325 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(&invalid), |
346 SequenceValueFromString(invalid))); | 326 SequenceValueFromString(&invalid))); |
347 } | 327 } |
348 | 328 |
349 TEST(VerifyNameMatchInvalidDataTest, FailOnAttributeTypeAndValueShort) { | 329 TEST(VerifyNameMatchInvalidDataTest, FailOnAttributeTypeAndValueShort) { |
350 std::string invalid; | 330 std::string invalid; |
351 ASSERT_TRUE(LoadTestData("invalid", "AttributeTypeAndValue", "onlyOneElement", | 331 ASSERT_TRUE(LoadTestData("invalid", "AttributeTypeAndValue", "onlyOneElement", |
352 &invalid)); | 332 &invalid)); |
353 // Verification should fail due to AttributeTypeAndValue sequence having only | 333 // Verification should fail due to AttributeTypeAndValue sequence having only |
354 // one element. | 334 // one element. |
355 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(invalid), | 335 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(&invalid), |
356 SequenceValueFromString(invalid))); | 336 SequenceValueFromString(&invalid))); |
357 } | 337 } |
358 | 338 |
359 TEST(VerifyNameMatchInvalidDataTest, FailOnAttributeTypeAndValueEmpty) { | 339 TEST(VerifyNameMatchInvalidDataTest, FailOnAttributeTypeAndValueEmpty) { |
360 std::string invalid; | 340 std::string invalid; |
361 ASSERT_TRUE( | 341 ASSERT_TRUE( |
362 LoadTestData("invalid", "AttributeTypeAndValue", "empty", &invalid)); | 342 LoadTestData("invalid", "AttributeTypeAndValue", "empty", &invalid)); |
363 // Verification should fail due to empty AttributeTypeAndValue sequence. | 343 // Verification should fail due to empty AttributeTypeAndValue sequence. |
364 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(invalid), | 344 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(&invalid), |
365 SequenceValueFromString(invalid))); | 345 SequenceValueFromString(&invalid))); |
366 } | 346 } |
367 | 347 |
368 TEST(VerifyNameMatchInvalidDataTest, FailOnBadAttributeType) { | 348 TEST(VerifyNameMatchInvalidDataTest, FailOnBadAttributeType) { |
369 std::string invalid; | 349 std::string invalid; |
370 ASSERT_TRUE(LoadTestData("invalid", "AttributeTypeAndValue", | 350 ASSERT_TRUE(LoadTestData("invalid", "AttributeTypeAndValue", |
371 "badAttributeType", &invalid)); | 351 "badAttributeType", &invalid)); |
372 // Verification should fail due to Attribute Type not being an OID. | 352 // Verification should fail due to Attribute Type not being an OID. |
373 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(invalid), | 353 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(&invalid), |
374 SequenceValueFromString(invalid))); | 354 SequenceValueFromString(&invalid))); |
375 } | 355 } |
376 | 356 |
377 TEST(VerifyNameMatchInvalidDataTest, FailOnAttributeTypeAndValueNotSequence) { | 357 TEST(VerifyNameMatchInvalidDataTest, FailOnAttributeTypeAndValueNotSequence) { |
378 std::string invalid; | 358 std::string invalid; |
379 ASSERT_TRUE(LoadTestData("invalid", "AttributeTypeAndValue", "setNotSequence", | 359 ASSERT_TRUE(LoadTestData("invalid", "AttributeTypeAndValue", "setNotSequence", |
380 &invalid)); | 360 &invalid)); |
381 // Verification should fail due to AttributeTypeAndValue being a Set instead | 361 // Verification should fail due to AttributeTypeAndValue being a Set instead |
382 // of a Sequence. | 362 // of a Sequence. |
383 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(invalid), | 363 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(&invalid), |
384 SequenceValueFromString(invalid))); | 364 SequenceValueFromString(&invalid))); |
385 } | 365 } |
386 | 366 |
387 TEST(VerifyNameMatchInvalidDataTest, FailOnRdnNotSet) { | 367 TEST(VerifyNameMatchInvalidDataTest, FailOnRdnNotSet) { |
388 std::string invalid; | 368 std::string invalid; |
389 ASSERT_TRUE(LoadTestData("invalid", "RDN", "sequenceInsteadOfSet", &invalid)); | 369 ASSERT_TRUE(LoadTestData("invalid", "RDN", "sequenceInsteadOfSet", &invalid)); |
390 // Verification should fail due to RDN being a Sequence instead of a Set. | 370 // Verification should fail due to RDN being a Sequence instead of a Set. |
391 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(invalid), | 371 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(&invalid), |
392 SequenceValueFromString(invalid))); | 372 SequenceValueFromString(&invalid))); |
393 } | 373 } |
394 | 374 |
395 TEST(VerifyNameMatchInvalidDataTest, FailOnEmptyRdn) { | 375 TEST(VerifyNameMatchInvalidDataTest, FailOnEmptyRdn) { |
396 std::string invalid; | 376 std::string invalid; |
397 ASSERT_TRUE(LoadTestData("invalid", "RDN", "empty", &invalid)); | 377 ASSERT_TRUE(LoadTestData("invalid", "RDN", "empty", &invalid)); |
398 // Verification should fail due to RDN having zero AttributeTypeAndValues. | 378 // Verification should fail due to RDN having zero AttributeTypeAndValues. |
399 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(invalid), | 379 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(&invalid), |
400 SequenceValueFromString(invalid))); | 380 SequenceValueFromString(&invalid))); |
401 } | 381 } |
402 | 382 |
403 // Matching should fail if a BMPString contains surrogates. | 383 // Matching should fail if a BMPString contains surrogates. |
404 TEST(VerifyNameMatchInvalidDataTest, FailOnBmpStringSurrogates) { | 384 TEST(VerifyNameMatchInvalidDataTest, FailOnBmpStringSurrogates) { |
405 std::string normal; | 385 std::string normal; |
406 ASSERT_TRUE(LoadTestData("unicode_bmp", "BMPSTRING", "unmangled", &normal)); | 386 ASSERT_TRUE(LoadTestData("unicode_bmp", "BMPSTRING", "unmangled", &normal)); |
407 // Find a known location inside a BMPSTRING in the DER-encoded data. | 387 // Find a known location inside a BMPSTRING in the DER-encoded data. |
408 size_t replace_location = normal.find("\x67\x71\x4e\xac"); | 388 size_t replace_location = normal.find("\x67\x71\x4e\xac"); |
409 ASSERT_NE(std::string::npos, replace_location); | 389 ASSERT_NE(std::string::npos, replace_location); |
410 // Replace with U+1D400 MATHEMATICAL BOLD CAPITAL A, which requires surrogates | 390 // Replace with U+1D400 MATHEMATICAL BOLD CAPITAL A, which requires surrogates |
411 // to represent. | 391 // to represent. |
412 std::string invalid = | 392 std::string invalid = |
413 normal.replace(replace_location, 4, std::string("\xd8\x35\xdc\x00", 4)); | 393 normal.replace(replace_location, 4, std::string("\xd8\x35\xdc\x00", 4)); |
414 // Verification should fail due to the invalid codepoints. | 394 // Verification should fail due to the invalid codepoints. |
415 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(invalid), | 395 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(&invalid), |
416 SequenceValueFromString(invalid))); | 396 SequenceValueFromString(&invalid))); |
417 } | 397 } |
418 | 398 |
419 TEST(VerifyNameMatchTest, EmptyNameMatching) { | 399 TEST(VerifyNameMatchTest, EmptyNameMatching) { |
420 std::string empty; | 400 std::string empty; |
421 ASSERT_TRUE(LoadTestData("valid", "Name", "empty", &empty)); | 401 ASSERT_TRUE(LoadTestData("valid", "Name", "empty", &empty)); |
422 // Empty names are equal. | 402 // Empty names are equal. |
423 EXPECT_TRUE(VerifyNameMatch(SequenceValueFromString(empty), | 403 EXPECT_TRUE(VerifyNameMatch(SequenceValueFromString(&empty), |
424 SequenceValueFromString(empty))); | 404 SequenceValueFromString(&empty))); |
425 | 405 |
426 // An empty name is not equal to non-empty name. | 406 // An empty name is not equal to non-empty name. |
427 std::string non_empty; | 407 std::string non_empty; |
428 ASSERT_TRUE( | 408 ASSERT_TRUE( |
429 LoadTestData("ascii", "PRINTABLESTRING", "unmangled", &non_empty)); | 409 LoadTestData("ascii", "PRINTABLESTRING", "unmangled", &non_empty)); |
430 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(empty), | 410 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(&empty), |
431 SequenceValueFromString(non_empty))); | 411 SequenceValueFromString(&non_empty))); |
432 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(non_empty), | 412 EXPECT_FALSE(VerifyNameMatch(SequenceValueFromString(&non_empty), |
433 SequenceValueFromString(empty))); | 413 SequenceValueFromString(&empty))); |
434 } | 414 } |
435 | 415 |
436 // Matching should succeed when the RDNs are sorted differently but are still | 416 // Matching should succeed when the RDNs are sorted differently but are still |
437 // equal after normalizing. | 417 // equal after normalizing. |
438 TEST(VerifyNameMatchRDNSorting, Simple) { | 418 TEST(VerifyNameMatchRDNSorting, Simple) { |
439 std::string a; | 419 std::string a; |
440 ASSERT_TRUE(LoadTestData("ascii", "PRINTABLESTRING", "rdn_sorting_1", &a)); | 420 ASSERT_TRUE(LoadTestData("ascii", "PRINTABLESTRING", "rdn_sorting_1", &a)); |
441 std::string b; | 421 std::string b; |
442 ASSERT_TRUE(LoadTestData("ascii", "PRINTABLESTRING", "rdn_sorting_2", &b)); | 422 ASSERT_TRUE(LoadTestData("ascii", "PRINTABLESTRING", "rdn_sorting_2", &b)); |
443 EXPECT_TRUE( | 423 EXPECT_TRUE(VerifyNameMatch(SequenceValueFromString(&a), |
444 VerifyNameMatch(SequenceValueFromString(a), SequenceValueFromString(b))); | 424 SequenceValueFromString(&b))); |
445 EXPECT_TRUE( | 425 EXPECT_TRUE(VerifyNameMatch(SequenceValueFromString(&b), |
446 VerifyNameMatch(SequenceValueFromString(b), SequenceValueFromString(a))); | 426 SequenceValueFromString(&a))); |
447 } | 427 } |
448 | 428 |
449 // Matching should succeed when the RDNs are sorted differently but are still | 429 // Matching should succeed when the RDNs are sorted differently but are still |
450 // equal after normalizing, even in malformed RDNs that contain multiple | 430 // equal after normalizing, even in malformed RDNs that contain multiple |
451 // elements with the same type. | 431 // elements with the same type. |
452 TEST(VerifyNameMatchRDNSorting, DuplicateTypes) { | 432 TEST(VerifyNameMatchRDNSorting, DuplicateTypes) { |
453 std::string a; | 433 std::string a; |
454 ASSERT_TRUE(LoadTestData("ascii", "mixed", "rdn_dupetype_sorting_1", &a)); | 434 ASSERT_TRUE(LoadTestData("ascii", "mixed", "rdn_dupetype_sorting_1", &a)); |
455 std::string b; | 435 std::string b; |
456 ASSERT_TRUE(LoadTestData("ascii", "mixed", "rdn_dupetype_sorting_2", &b)); | 436 ASSERT_TRUE(LoadTestData("ascii", "mixed", "rdn_dupetype_sorting_2", &b)); |
457 EXPECT_TRUE( | 437 EXPECT_TRUE(VerifyNameMatch(SequenceValueFromString(&a), |
458 VerifyNameMatch(SequenceValueFromString(a), SequenceValueFromString(b))); | 438 SequenceValueFromString(&b))); |
459 EXPECT_TRUE( | 439 EXPECT_TRUE(VerifyNameMatch(SequenceValueFromString(&b), |
460 VerifyNameMatch(SequenceValueFromString(b), SequenceValueFromString(a))); | 440 SequenceValueFromString(&a))); |
461 } | 441 } |
462 | 442 |
463 } // namespace net | 443 } // namespace net |
OLD | NEW |