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 <errno.h> | 5 #include <errno.h> |
6 #include <stddef.h> | 6 #include <stddef.h> |
7 | 7 |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "url/third_party/mozilla/url_parse.h" | 11 #include "url/third_party/mozilla/url_parse.h" |
11 #include "url/url_canon.h" | 12 #include "url/url_canon.h" |
12 #include "url/url_canon_internal.h" | 13 #include "url/url_canon_internal.h" |
13 #include "url/url_canon_stdstring.h" | 14 #include "url/url_canon_stdstring.h" |
14 #include "url/url_test_utils.h" | 15 #include "url/url_test_utils.h" |
15 | 16 |
16 namespace url { | 17 namespace url { |
17 | 18 |
18 using test_utils::WStringToUTF16; | |
19 using test_utils::ConvertUTF8ToUTF16; | |
20 using test_utils::ConvertUTF16ToUTF8; | |
21 | |
22 namespace { | 19 namespace { |
23 | 20 |
24 struct ComponentCase { | 21 struct ComponentCase { |
25 const char* input; | 22 const char* input; |
26 const char* expected; | 23 const char* expected; |
27 Component expected_component; | 24 Component expected_component; |
28 bool expected_success; | 25 bool expected_success; |
29 }; | 26 }; |
30 | 27 |
31 // ComponentCase but with dual 8-bit/16-bit input. Generally, the unit tests | 28 // ComponentCase but with dual 8-bit/16-bit input. Generally, the unit tests |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 &output); | 185 &output); |
189 } | 186 } |
190 output.Complete(); | 187 output.Complete(); |
191 EXPECT_EQ(utf_cases[i].expected_success, success); | 188 EXPECT_EQ(utf_cases[i].expected_success, success); |
192 EXPECT_EQ(std::string(utf_cases[i].output), out_str); | 189 EXPECT_EQ(std::string(utf_cases[i].output), out_str); |
193 } | 190 } |
194 if (utf_cases[i].input16) { | 191 if (utf_cases[i].input16) { |
195 out_str.clear(); | 192 out_str.clear(); |
196 StdStringCanonOutput output(&out_str); | 193 StdStringCanonOutput output(&out_str); |
197 | 194 |
198 base::string16 input_str(WStringToUTF16(utf_cases[i].input16)); | 195 base::string16 input_str( |
| 196 test_utils::TruncateWStringToUTF16(utf_cases[i].input16)); |
199 int input_len = static_cast<int>(input_str.length()); | 197 int input_len = static_cast<int>(input_str.length()); |
200 bool success = true; | 198 bool success = true; |
201 for (int ch = 0; ch < input_len; ch++) { | 199 for (int ch = 0; ch < input_len; ch++) { |
202 success &= AppendUTF8EscapedChar(input_str.c_str(), &ch, input_len, | 200 success &= AppendUTF8EscapedChar(input_str.c_str(), &ch, input_len, |
203 &output); | 201 &output); |
204 } | 202 } |
205 output.Complete(); | 203 output.Complete(); |
206 EXPECT_EQ(utf_cases[i].expected_success, success); | 204 EXPECT_EQ(utf_cases[i].expected_success, success); |
207 EXPECT_EQ(std::string(utf_cases[i].output), out_str); | 205 EXPECT_EQ(std::string(utf_cases[i].output), out_str); |
208 } | 206 } |
209 | 207 |
210 if (utf_cases[i].input8 && utf_cases[i].input16 && | 208 if (utf_cases[i].input8 && utf_cases[i].input16 && |
211 utf_cases[i].expected_success) { | 209 utf_cases[i].expected_success) { |
212 // Check that the UTF-8 and UTF-16 inputs are equivalent. | 210 // Check that the UTF-8 and UTF-16 inputs are equivalent. |
213 | 211 |
214 // UTF-16 -> UTF-8 | 212 // UTF-16 -> UTF-8 |
215 std::string input8_str(utf_cases[i].input8); | 213 std::string input8_str(utf_cases[i].input8); |
216 base::string16 input16_str(WStringToUTF16(utf_cases[i].input16)); | 214 base::string16 input16_str( |
217 EXPECT_EQ(input8_str, ConvertUTF16ToUTF8(input16_str)); | 215 test_utils::TruncateWStringToUTF16(utf_cases[i].input16)); |
| 216 EXPECT_EQ(input8_str, base::UTF16ToUTF8(input16_str)); |
218 | 217 |
219 // UTF-8 -> UTF-16 | 218 // UTF-8 -> UTF-16 |
220 EXPECT_EQ(input16_str, ConvertUTF8ToUTF16(input8_str)); | 219 EXPECT_EQ(input16_str, base::UTF8ToUTF16(input8_str)); |
221 } | 220 } |
222 } | 221 } |
223 } | 222 } |
224 | 223 |
225 TEST(URLCanonTest, Scheme) { | 224 TEST(URLCanonTest, Scheme) { |
226 // Here, we're mostly testing that unusual characters are handled properly. | 225 // Here, we're mostly testing that unusual characters are handled properly. |
227 // The canonicalizer doesn't do any parsing or whitespace detection. It will | 226 // The canonicalizer doesn't do any parsing or whitespace detection. It will |
228 // also do its best on error, and will escape funny sequences (these won't be | 227 // also do its best on error, and will escape funny sequences (these won't be |
229 // valid schemes and it will return error). | 228 // valid schemes and it will return error). |
230 // | 229 // |
(...skipping 27 matching lines...) Expand all Loading... |
258 | 257 |
259 EXPECT_EQ(scheme_cases[i].expected_success, success); | 258 EXPECT_EQ(scheme_cases[i].expected_success, success); |
260 EXPECT_EQ(std::string(scheme_cases[i].expected), out_str); | 259 EXPECT_EQ(std::string(scheme_cases[i].expected), out_str); |
261 EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin); | 260 EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin); |
262 EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len); | 261 EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len); |
263 | 262 |
264 // Now try the wide version. | 263 // Now try the wide version. |
265 out_str.clear(); | 264 out_str.clear(); |
266 StdStringCanonOutput output2(&out_str); | 265 StdStringCanonOutput output2(&out_str); |
267 | 266 |
268 base::string16 wide_input(ConvertUTF8ToUTF16(scheme_cases[i].input)); | 267 base::string16 wide_input(base::UTF8ToUTF16(scheme_cases[i].input)); |
269 in_comp.len = static_cast<int>(wide_input.length()); | 268 in_comp.len = static_cast<int>(wide_input.length()); |
270 success = CanonicalizeScheme(wide_input.c_str(), in_comp, &output2, | 269 success = CanonicalizeScheme(wide_input.c_str(), in_comp, &output2, |
271 &out_comp); | 270 &out_comp); |
272 output2.Complete(); | 271 output2.Complete(); |
273 | 272 |
274 EXPECT_EQ(scheme_cases[i].expected_success, success); | 273 EXPECT_EQ(scheme_cases[i].expected_success, success); |
275 EXPECT_EQ(std::string(scheme_cases[i].expected), out_str); | 274 EXPECT_EQ(std::string(scheme_cases[i].expected), out_str); |
276 EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin); | 275 EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin); |
277 EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len); | 276 EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len); |
278 } | 277 } |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 EXPECT_EQ(std::string(host_cases[i].expected), out_str) << | 522 EXPECT_EQ(std::string(host_cases[i].expected), out_str) << |
524 "for input: " << host_cases[i].input8; | 523 "for input: " << host_cases[i].input8; |
525 EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin) << | 524 EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin) << |
526 "for input: " << host_cases[i].input8; | 525 "for input: " << host_cases[i].input8; |
527 EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len) << | 526 EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len) << |
528 "for input: " << host_cases[i].input8; | 527 "for input: " << host_cases[i].input8; |
529 } | 528 } |
530 | 529 |
531 // Wide version. | 530 // Wide version. |
532 if (host_cases[i].input16) { | 531 if (host_cases[i].input16) { |
533 base::string16 input16(WStringToUTF16(host_cases[i].input16)); | 532 base::string16 input16( |
| 533 test_utils::TruncateWStringToUTF16(host_cases[i].input16)); |
534 int host_len = static_cast<int>(input16.length()); | 534 int host_len = static_cast<int>(input16.length()); |
535 Component in_comp(0, host_len); | 535 Component in_comp(0, host_len); |
536 Component out_comp; | 536 Component out_comp; |
537 | 537 |
538 out_str.clear(); | 538 out_str.clear(); |
539 StdStringCanonOutput output(&out_str); | 539 StdStringCanonOutput output(&out_str); |
540 | 540 |
541 bool success = CanonicalizeHost(input16.c_str(), in_comp, &output, | 541 bool success = CanonicalizeHost(input16.c_str(), in_comp, &output, |
542 &out_comp); | 542 &out_comp); |
543 output.Complete(); | 543 output.Complete(); |
(...skipping 29 matching lines...) Expand all Loading... |
573 EXPECT_EQ(std::string(host_cases[i].expected_address_hex), | 573 EXPECT_EQ(std::string(host_cases[i].expected_address_hex), |
574 BytesToHexString(host_info.address, host_info.AddressLength())); | 574 BytesToHexString(host_info.address, host_info.AddressLength())); |
575 if (host_cases[i].expected_family == CanonHostInfo::IPV4) { | 575 if (host_cases[i].expected_family == CanonHostInfo::IPV4) { |
576 EXPECT_EQ(host_cases[i].expected_num_ipv4_components, | 576 EXPECT_EQ(host_cases[i].expected_num_ipv4_components, |
577 host_info.num_ipv4_components); | 577 host_info.num_ipv4_components); |
578 } | 578 } |
579 } | 579 } |
580 | 580 |
581 // Wide version. | 581 // Wide version. |
582 if (host_cases[i].input16) { | 582 if (host_cases[i].input16) { |
583 base::string16 input16(WStringToUTF16(host_cases[i].input16)); | 583 base::string16 input16( |
| 584 test_utils::TruncateWStringToUTF16(host_cases[i].input16)); |
584 int host_len = static_cast<int>(input16.length()); | 585 int host_len = static_cast<int>(input16.length()); |
585 Component in_comp(0, host_len); | 586 Component in_comp(0, host_len); |
586 | 587 |
587 out_str.clear(); | 588 out_str.clear(); |
588 StdStringCanonOutput output(&out_str); | 589 StdStringCanonOutput output(&out_str); |
589 CanonHostInfo host_info; | 590 CanonHostInfo host_info; |
590 | 591 |
591 CanonicalizeHostVerbose(input16.c_str(), in_comp, &output, &host_info); | 592 CanonicalizeHostVerbose(input16.c_str(), in_comp, &output, &host_info); |
592 output.Complete(); | 593 output.Complete(); |
593 | 594 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
695 BytesToHexString(host_info.address, host_info.AddressLength())); | 696 BytesToHexString(host_info.address, host_info.AddressLength())); |
696 if (host_info.family == CanonHostInfo::IPV4) { | 697 if (host_info.family == CanonHostInfo::IPV4) { |
697 EXPECT_STREQ(cases[i].expected, out_str1.c_str()); | 698 EXPECT_STREQ(cases[i].expected, out_str1.c_str()); |
698 EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin); | 699 EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin); |
699 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len); | 700 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len); |
700 EXPECT_EQ(cases[i].expected_num_ipv4_components, | 701 EXPECT_EQ(cases[i].expected_num_ipv4_components, |
701 host_info.num_ipv4_components); | 702 host_info.num_ipv4_components); |
702 } | 703 } |
703 | 704 |
704 // 16-bit version. | 705 // 16-bit version. |
705 base::string16 input16(WStringToUTF16(cases[i].input16)); | 706 base::string16 input16( |
| 707 test_utils::TruncateWStringToUTF16(cases[i].input16)); |
706 component = Component(0, static_cast<int>(input16.length())); | 708 component = Component(0, static_cast<int>(input16.length())); |
707 | 709 |
708 std::string out_str2; | 710 std::string out_str2; |
709 StdStringCanonOutput output2(&out_str2); | 711 StdStringCanonOutput output2(&out_str2); |
710 CanonicalizeIPAddress(input16.c_str(), component, &output2, &host_info); | 712 CanonicalizeIPAddress(input16.c_str(), component, &output2, &host_info); |
711 output2.Complete(); | 713 output2.Complete(); |
712 | 714 |
713 EXPECT_EQ(cases[i].expected_family, host_info.family); | 715 EXPECT_EQ(cases[i].expected_family, host_info.family); |
714 EXPECT_EQ(std::string(cases[i].expected_address_hex), | 716 EXPECT_EQ(std::string(cases[i].expected_address_hex), |
715 BytesToHexString(host_info.address, host_info.AddressLength())); | 717 BytesToHexString(host_info.address, host_info.AddressLength())); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
847 EXPECT_EQ(std::string(cases[i].expected_address_hex), | 849 EXPECT_EQ(std::string(cases[i].expected_address_hex), |
848 BytesToHexString(host_info.address, host_info.AddressLength())) <<
"iter " << i << " host " << cases[i].input8; | 850 BytesToHexString(host_info.address, host_info.AddressLength())) <<
"iter " << i << " host " << cases[i].input8; |
849 if (host_info.family == CanonHostInfo::IPV6) { | 851 if (host_info.family == CanonHostInfo::IPV6) { |
850 EXPECT_STREQ(cases[i].expected, out_str1.c_str()); | 852 EXPECT_STREQ(cases[i].expected, out_str1.c_str()); |
851 EXPECT_EQ(cases[i].expected_component.begin, | 853 EXPECT_EQ(cases[i].expected_component.begin, |
852 host_info.out_host.begin); | 854 host_info.out_host.begin); |
853 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len); | 855 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len); |
854 } | 856 } |
855 | 857 |
856 // 16-bit version. | 858 // 16-bit version. |
857 base::string16 input16(WStringToUTF16(cases[i].input16)); | 859 base::string16 input16( |
| 860 test_utils::TruncateWStringToUTF16(cases[i].input16)); |
858 component = Component(0, static_cast<int>(input16.length())); | 861 component = Component(0, static_cast<int>(input16.length())); |
859 | 862 |
860 std::string out_str2; | 863 std::string out_str2; |
861 StdStringCanonOutput output2(&out_str2); | 864 StdStringCanonOutput output2(&out_str2); |
862 CanonicalizeIPAddress(input16.c_str(), component, &output2, &host_info); | 865 CanonicalizeIPAddress(input16.c_str(), component, &output2, &host_info); |
863 output2.Complete(); | 866 output2.Complete(); |
864 | 867 |
865 EXPECT_EQ(cases[i].expected_family, host_info.family); | 868 EXPECT_EQ(cases[i].expected_family, host_info.family); |
866 EXPECT_EQ(std::string(cases[i].expected_address_hex), | 869 EXPECT_EQ(std::string(cases[i].expected_address_hex), |
867 BytesToHexString(host_info.address, host_info.AddressLength())); | 870 BytesToHexString(host_info.address, host_info.AddressLength())); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
899 Component(0, 12), &output)); | 902 Component(0, 12), &output)); |
900 output.Complete(); | 903 output.Complete(); |
901 EXPECT_EQ("xn--mnchen-3ya.com", out_str); | 904 EXPECT_EQ("xn--mnchen-3ya.com", out_str); |
902 } | 905 } |
903 | 906 |
904 // Failure case. | 907 // Failure case. |
905 { | 908 { |
906 std::string out_str; | 909 std::string out_str; |
907 StdStringCanonOutput output(&out_str); | 910 StdStringCanonOutput output(&out_str); |
908 EXPECT_FALSE(CanonicalizeHostSubstring( | 911 EXPECT_FALSE(CanonicalizeHostSubstring( |
909 WStringToUTF16(L"\xfdd0zyx.com").c_str(), Component(0, 8), &output)); | 912 test_utils::TruncateWStringToUTF16(L"\xfdd0zyx.com").c_str(), |
| 913 Component(0, 8), &output)); |
910 output.Complete(); | 914 output.Complete(); |
911 EXPECT_EQ("%EF%BF%BDzyx.com", out_str); | 915 EXPECT_EQ("%EF%BF%BDzyx.com", out_str); |
912 } | 916 } |
913 | 917 |
914 // Should return true for empty input strings. | 918 // Should return true for empty input strings. |
915 { | 919 { |
916 std::string out_str; | 920 std::string out_str; |
917 StdStringCanonOutput output(&out_str); | 921 StdStringCanonOutput output(&out_str); |
918 EXPECT_TRUE(CanonicalizeHostSubstring("", Component(0, 0), &output)); | 922 EXPECT_TRUE(CanonicalizeHostSubstring("", Component(0, 0), &output)); |
919 output.Complete(); | 923 output.Complete(); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
977 EXPECT_EQ(user_info_cases[i].expected_success, success); | 981 EXPECT_EQ(user_info_cases[i].expected_success, success); |
978 EXPECT_EQ(std::string(user_info_cases[i].expected), out_str); | 982 EXPECT_EQ(std::string(user_info_cases[i].expected), out_str); |
979 EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin); | 983 EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin); |
980 EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len); | 984 EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len); |
981 EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin); | 985 EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin); |
982 EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len); | 986 EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len); |
983 | 987 |
984 // Now try the wide version | 988 // Now try the wide version |
985 out_str.clear(); | 989 out_str.clear(); |
986 StdStringCanonOutput output2(&out_str); | 990 StdStringCanonOutput output2(&out_str); |
987 base::string16 wide_input(ConvertUTF8ToUTF16(user_info_cases[i].input)); | 991 base::string16 wide_input(base::UTF8ToUTF16(user_info_cases[i].input)); |
988 success = CanonicalizeUserInfo(wide_input.c_str(), | 992 success = CanonicalizeUserInfo(wide_input.c_str(), |
989 parsed.username, | 993 parsed.username, |
990 wide_input.c_str(), | 994 wide_input.c_str(), |
991 parsed.password, | 995 parsed.password, |
992 &output2, | 996 &output2, |
993 &out_user, | 997 &out_user, |
994 &out_pass); | 998 &out_pass); |
995 output2.Complete(); | 999 output2.Complete(); |
996 | 1000 |
997 EXPECT_EQ(user_info_cases[i].expected_success, success); | 1001 EXPECT_EQ(user_info_cases[i].expected_success, success); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1040 output1.Complete(); | 1044 output1.Complete(); |
1041 | 1045 |
1042 EXPECT_EQ(port_cases[i].expected_success, success); | 1046 EXPECT_EQ(port_cases[i].expected_success, success); |
1043 EXPECT_EQ(std::string(port_cases[i].expected), out_str); | 1047 EXPECT_EQ(std::string(port_cases[i].expected), out_str); |
1044 EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin); | 1048 EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin); |
1045 EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len); | 1049 EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len); |
1046 | 1050 |
1047 // Now try the wide version | 1051 // Now try the wide version |
1048 out_str.clear(); | 1052 out_str.clear(); |
1049 StdStringCanonOutput output2(&out_str); | 1053 StdStringCanonOutput output2(&out_str); |
1050 base::string16 wide_input(ConvertUTF8ToUTF16(port_cases[i].input)); | 1054 base::string16 wide_input(base::UTF8ToUTF16(port_cases[i].input)); |
1051 success = CanonicalizePort(wide_input.c_str(), | 1055 success = CanonicalizePort(wide_input.c_str(), |
1052 in_comp, | 1056 in_comp, |
1053 port_cases[i].default_port, | 1057 port_cases[i].default_port, |
1054 &output2, | 1058 &output2, |
1055 &out_comp); | 1059 &out_comp); |
1056 output2.Complete(); | 1060 output2.Complete(); |
1057 | 1061 |
1058 EXPECT_EQ(port_cases[i].expected_success, success); | 1062 EXPECT_EQ(port_cases[i].expected_success, success); |
1059 EXPECT_EQ(std::string(port_cases[i].expected), out_str); | 1063 EXPECT_EQ(std::string(port_cases[i].expected), out_str); |
1060 EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin); | 1064 EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1160 CanonicalizePath(path_cases[i].input8, in_comp, &output, &out_comp); | 1164 CanonicalizePath(path_cases[i].input8, in_comp, &output, &out_comp); |
1161 output.Complete(); | 1165 output.Complete(); |
1162 | 1166 |
1163 EXPECT_EQ(path_cases[i].expected_success, success); | 1167 EXPECT_EQ(path_cases[i].expected_success, success); |
1164 EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin); | 1168 EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin); |
1165 EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len); | 1169 EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len); |
1166 EXPECT_EQ(path_cases[i].expected, out_str); | 1170 EXPECT_EQ(path_cases[i].expected, out_str); |
1167 } | 1171 } |
1168 | 1172 |
1169 if (path_cases[i].input16) { | 1173 if (path_cases[i].input16) { |
1170 base::string16 input16(WStringToUTF16(path_cases[i].input16)); | 1174 base::string16 input16( |
| 1175 test_utils::TruncateWStringToUTF16(path_cases[i].input16)); |
1171 int len = static_cast<int>(input16.length()); | 1176 int len = static_cast<int>(input16.length()); |
1172 Component in_comp(0, len); | 1177 Component in_comp(0, len); |
1173 Component out_comp; | 1178 Component out_comp; |
1174 std::string out_str; | 1179 std::string out_str; |
1175 StdStringCanonOutput output(&out_str); | 1180 StdStringCanonOutput output(&out_str); |
1176 | 1181 |
1177 bool success = | 1182 bool success = |
1178 CanonicalizePath(input16.c_str(), in_comp, &output, &out_comp); | 1183 CanonicalizePath(input16.c_str(), in_comp, &output, &out_comp); |
1179 output.Complete(); | 1184 output.Complete(); |
1180 | 1185 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1235 | 1240 |
1236 StdStringCanonOutput output(&out_str); | 1241 StdStringCanonOutput output(&out_str); |
1237 CanonicalizeQuery(query_cases[i].input8, in_comp, NULL, &output, | 1242 CanonicalizeQuery(query_cases[i].input8, in_comp, NULL, &output, |
1238 &out_comp); | 1243 &out_comp); |
1239 output.Complete(); | 1244 output.Complete(); |
1240 | 1245 |
1241 EXPECT_EQ(query_cases[i].expected, out_str); | 1246 EXPECT_EQ(query_cases[i].expected, out_str); |
1242 } | 1247 } |
1243 | 1248 |
1244 if (query_cases[i].input16) { | 1249 if (query_cases[i].input16) { |
1245 base::string16 input16(WStringToUTF16(query_cases[i].input16)); | 1250 base::string16 input16( |
| 1251 test_utils::TruncateWStringToUTF16(query_cases[i].input16)); |
1246 int len = static_cast<int>(input16.length()); | 1252 int len = static_cast<int>(input16.length()); |
1247 Component in_comp(0, len); | 1253 Component in_comp(0, len); |
1248 std::string out_str; | 1254 std::string out_str; |
1249 | 1255 |
1250 StdStringCanonOutput output(&out_str); | 1256 StdStringCanonOutput output(&out_str); |
1251 CanonicalizeQuery(input16.c_str(), in_comp, NULL, &output, &out_comp); | 1257 CanonicalizeQuery(input16.c_str(), in_comp, NULL, &output, &out_comp); |
1252 output.Complete(); | 1258 output.Complete(); |
1253 | 1259 |
1254 EXPECT_EQ(query_cases[i].expected, out_str); | 1260 EXPECT_EQ(query_cases[i].expected, out_str); |
1255 } | 1261 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1297 CanonicalizeRef(ref_cases[i].input8, in_comp, &output, &out_comp); | 1303 CanonicalizeRef(ref_cases[i].input8, in_comp, &output, &out_comp); |
1298 output.Complete(); | 1304 output.Complete(); |
1299 | 1305 |
1300 EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin); | 1306 EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin); |
1301 EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len); | 1307 EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len); |
1302 EXPECT_EQ(ref_cases[i].expected, out_str); | 1308 EXPECT_EQ(ref_cases[i].expected, out_str); |
1303 } | 1309 } |
1304 | 1310 |
1305 // 16-bit input | 1311 // 16-bit input |
1306 if (ref_cases[i].input16) { | 1312 if (ref_cases[i].input16) { |
1307 base::string16 input16(WStringToUTF16(ref_cases[i].input16)); | 1313 base::string16 input16( |
| 1314 test_utils::TruncateWStringToUTF16(ref_cases[i].input16)); |
1308 int len = static_cast<int>(input16.length()); | 1315 int len = static_cast<int>(input16.length()); |
1309 Component in_comp(0, len); | 1316 Component in_comp(0, len); |
1310 Component out_comp; | 1317 Component out_comp; |
1311 | 1318 |
1312 std::string out_str; | 1319 std::string out_str; |
1313 StdStringCanonOutput output(&out_str); | 1320 StdStringCanonOutput output(&out_str); |
1314 CanonicalizeRef(input16.c_str(), in_comp, &output, &out_comp); | 1321 CanonicalizeRef(input16.c_str(), in_comp, &output, &out_comp); |
1315 output.Complete(); | 1322 output.Complete(); |
1316 | 1323 |
1317 EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin); | 1324 EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin); |
(...skipping 615 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1933 | 1940 |
1934 TEST(URLCanonTest, _itow_s) { | 1941 TEST(URLCanonTest, _itow_s) { |
1935 // We fill the buffer with 0xff to ensure that it's getting properly | 1942 // We fill the buffer with 0xff to ensure that it's getting properly |
1936 // null-terminated. We also allocate one byte more than what we tell | 1943 // null-terminated. We also allocate one byte more than what we tell |
1937 // _itoa_s about, and ensure that the extra byte is untouched. | 1944 // _itoa_s about, and ensure that the extra byte is untouched. |
1938 base::char16 buf[6]; | 1945 base::char16 buf[6]; |
1939 const char fill_mem = 0xff; | 1946 const char fill_mem = 0xff; |
1940 const base::char16 fill_char = 0xffff; | 1947 const base::char16 fill_char = 0xffff; |
1941 memset(buf, fill_mem, sizeof(buf)); | 1948 memset(buf, fill_mem, sizeof(buf)); |
1942 EXPECT_EQ(0, _itow_s(12, buf, sizeof(buf) / 2 - 1, 10)); | 1949 EXPECT_EQ(0, _itow_s(12, buf, sizeof(buf) / 2 - 1, 10)); |
1943 EXPECT_EQ(WStringToUTF16(L"12"), base::string16(buf)); | 1950 EXPECT_EQ(base::UTF8ToUTF16("12"), base::string16(buf)); |
1944 EXPECT_EQ(fill_char, buf[3]); | 1951 EXPECT_EQ(fill_char, buf[3]); |
1945 | 1952 |
1946 // Test the edge cases - exactly the buffer size and one over | 1953 // Test the edge cases - exactly the buffer size and one over |
1947 EXPECT_EQ(0, _itow_s(1234, buf, sizeof(buf) / 2 - 1, 10)); | 1954 EXPECT_EQ(0, _itow_s(1234, buf, sizeof(buf) / 2 - 1, 10)); |
1948 EXPECT_EQ(WStringToUTF16(L"1234"), base::string16(buf)); | 1955 EXPECT_EQ(base::UTF8ToUTF16("1234"), base::string16(buf)); |
1949 EXPECT_EQ(fill_char, buf[5]); | 1956 EXPECT_EQ(fill_char, buf[5]); |
1950 | 1957 |
1951 memset(buf, fill_mem, sizeof(buf)); | 1958 memset(buf, fill_mem, sizeof(buf)); |
1952 EXPECT_EQ(EINVAL, _itow_s(12345, buf, sizeof(buf) / 2 - 1, 10)); | 1959 EXPECT_EQ(EINVAL, _itow_s(12345, buf, sizeof(buf) / 2 - 1, 10)); |
1953 EXPECT_EQ(fill_char, buf[5]); // should never write to this location | 1960 EXPECT_EQ(fill_char, buf[5]); // should never write to this location |
1954 | 1961 |
1955 // Test the template overload (note that this will see the full buffer) | 1962 // Test the template overload (note that this will see the full buffer) |
1956 memset(buf, fill_mem, sizeof(buf)); | 1963 memset(buf, fill_mem, sizeof(buf)); |
1957 EXPECT_EQ(0, _itow_s(12, buf, 10)); | 1964 EXPECT_EQ(0, _itow_s(12, buf, 10)); |
1958 EXPECT_EQ(WStringToUTF16(L"12"), base::string16(buf)); | 1965 EXPECT_EQ(base::UTF8ToUTF16("12"), |
| 1966 base::string16(buf)); |
1959 EXPECT_EQ(fill_char, buf[3]); | 1967 EXPECT_EQ(fill_char, buf[3]); |
1960 | 1968 |
1961 memset(buf, fill_mem, sizeof(buf)); | 1969 memset(buf, fill_mem, sizeof(buf)); |
1962 EXPECT_EQ(0, _itow_s(12345, buf, 10)); | 1970 EXPECT_EQ(0, _itow_s(12345, buf, 10)); |
1963 EXPECT_EQ(WStringToUTF16(L"12345"), base::string16(buf)); | 1971 EXPECT_EQ(base::UTF8ToUTF16("12345"), base::string16(buf)); |
1964 | 1972 |
1965 EXPECT_EQ(EINVAL, _itow_s(123456, buf, 10)); | 1973 EXPECT_EQ(EINVAL, _itow_s(123456, buf, 10)); |
1966 } | 1974 } |
1967 | 1975 |
1968 #endif // !WIN32 | 1976 #endif // !WIN32 |
1969 | 1977 |
1970 // Returns true if the given two structures are the same. | 1978 // Returns true if the given two structures are the same. |
1971 static bool ParsedIsEqual(const Parsed& a, const Parsed& b) { | 1979 static bool ParsedIsEqual(const Parsed& a, const Parsed& b) { |
1972 return a.scheme.begin == b.scheme.begin && a.scheme.len == b.scheme.len && | 1980 return a.scheme.begin == b.scheme.begin && a.scheme.len == b.scheme.len && |
1973 a.username.begin == b.username.begin && a.username.len == b.username.le
n && | 1981 a.username.begin == b.username.begin && a.username.len == b.username.le
n && |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2189 Parsed parsed; | 2197 Parsed parsed; |
2190 ParseFileURL(src, src_len, &parsed); | 2198 ParseFileURL(src, src_len, &parsed); |
2191 | 2199 |
2192 // Override two components, the path with something short, and the query with | 2200 // Override two components, the path with something short, and the query with |
2193 // something long enough to trigger the bug. | 2201 // something long enough to trigger the bug. |
2194 Replacements<base::char16> repl; | 2202 Replacements<base::char16> repl; |
2195 base::string16 new_query; | 2203 base::string16 new_query; |
2196 for (int i = 0; i < 4800; i++) | 2204 for (int i = 0; i < 4800; i++) |
2197 new_query.push_back('a'); | 2205 new_query.push_back('a'); |
2198 | 2206 |
2199 base::string16 new_path(WStringToUTF16(L"/foo")); | 2207 base::string16 new_path(test_utils::TruncateWStringToUTF16(L"/foo")); |
2200 repl.SetPath(new_path.c_str(), Component(0, 4)); | 2208 repl.SetPath(new_path.c_str(), Component(0, 4)); |
2201 repl.SetQuery(new_query.c_str(), | 2209 repl.SetQuery(new_query.c_str(), |
2202 Component(0, static_cast<int>(new_query.length()))); | 2210 Component(0, static_cast<int>(new_query.length()))); |
2203 | 2211 |
2204 // Call ReplaceComponents on the string. It doesn't matter if we call it for | 2212 // Call ReplaceComponents on the string. It doesn't matter if we call it for |
2205 // standard URLs, file URLs, etc, since they will go to the same replacement | 2213 // standard URLs, file URLs, etc, since they will go to the same replacement |
2206 // function that was buggy. | 2214 // function that was buggy. |
2207 Parsed repl_parsed; | 2215 Parsed repl_parsed; |
2208 std::string repl_str; | 2216 std::string repl_str; |
2209 StdStringCanonOutput repl_output(&repl_str); | 2217 StdStringCanonOutput repl_output(&repl_str); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2242 }; | 2250 }; |
2243 | 2251 |
2244 for (auto& test_case : cases) { | 2252 for (auto& test_case : cases) { |
2245 SCOPED_TRACE(test_case.scheme); | 2253 SCOPED_TRACE(test_case.scheme); |
2246 EXPECT_EQ(test_case.expected_port, | 2254 EXPECT_EQ(test_case.expected_port, |
2247 DefaultPortForScheme(test_case.scheme, strlen(test_case.scheme))); | 2255 DefaultPortForScheme(test_case.scheme, strlen(test_case.scheme))); |
2248 } | 2256 } |
2249 } | 2257 } |
2250 | 2258 |
2251 } // namespace url | 2259 } // namespace url |
OLD | NEW |