xref: /aosp_15_r20/external/cronet/net/base/apple/url_conversions_unittest.mm (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1// Copyright 2012 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#import <Foundation/Foundation.h>
6
7#include "base/strings/sys_string_conversions.h"
8#import "net/base/apple/url_conversions.h"
9#include "testing/gtest/include/gtest/gtest.h"
10#include "testing/gtest_mac.h"
11#include "url/gurl.h"
12
13namespace net {
14
15namespace {
16
17class URLConversionTest : public ::testing::Test {
18 protected:
19  void SetUp() override {
20    test_data_ = [NSArray
21        arrayWithObjects:
22            // Simple URL with protocol
23            @"https://www.google.com/", @"https://www.google.com/",
24
25            // Simple URL with protocol and query string
26            @"https://www.google.com/search?q=gtest",
27            @"https://www.google.com/search?q=gtest",
28
29            // Simple URL with protocol and query string multiple params
30            @"https://www.google.com/search?hl=en&q=gtest",
31            @"https://www.google.com/search?hl=en&q=gtest",
32
33            // Simple URL with protocol and query string and fragment
34            @"https://www.google.com/search?q=gtest#123",
35            @"https://www.google.com/search?q=gtest#123",
36
37            // URL with ~
38            @"http://www.mysite.com/~user", @"http://www.mysite.com/~user",
39
40            // URL with #
41            @"http://www.mysite.com/#123456", @"http://www.mysite.com/#123456",
42
43            // URL with # before ?
44            @"http://www.mysite.com/test#test?test",
45            @"http://www.mysite.com/test#test?test",
46
47            // URL with ? before #
48            @"http://www.mysite.com/test?test#test",
49            @"http://www.mysite.com/test?test#test",
50
51            // URL with two #s
52            @"http://www.mysite.com/test#test#test",
53            @"http://www.mysite.com/test#test%23test",
54
55            // URL with two ?s
56            @"http://www.mysite.com/test?test?test",
57            @"http://www.mysite.com/test?test?test",
58
59            // URL with pattern ? # ?
60            @"http://www.mysite.com/test?test#test?test",
61            @"http://www.mysite.com/test?test#test?test",
62
63            // URL with pattern # ? #
64            @"http://www.mysite.com/test#test?test#test",
65            @"http://www.mysite.com/test#test?test%23test",
66
67            // URL with %
68            @"http://www.mysite.com/%123", @"http://www.mysite.com/%123",
69
70            // URL with [
71            @"http://www.mysite.com/[123", @"http://www.mysite.com/%5B123",
72
73            // URL with ]
74            @"http://www.mysite.com/]123", @"http://www.mysite.com/%5D123",
75
76            // URL with `
77            @"http://www.mysite.com/`123", @"http://www.mysite.com/%60123",
78
79            // URL with ^
80            @"http://www.mysite.com/^123", @"http://www.mysite.com/%5E123",
81
82            // URL with backslash (GURL canonicallizes unescaped \ to /)
83            @"http://www.mysite.com/\\123", @"http://www.mysite.com//123",
84
85            // URL with space
86            @"http://www.mysite.com/~user name",
87            @"http://www.mysite.com/~user%20name",
88
89            // URL with <
90            @"http://www.mysite.com/123<456",
91            @"http://www.mysite.com/123%3C456",
92
93            // URL with >
94            @"http://www.mysite.com/456>123",
95            @"http://www.mysite.com/456%3E123",
96
97            // URL with |
98            @"http://www.mysite.com/|123", @"http://www.mysite.com/%7C123",
99
100            // URL with !
101            @"http://www.mysite.com/!123", @"http://www.mysite.com/!123",
102
103            // URL with ~
104            @"http://www.mysite.com/~user", @"http://www.mysite.com/~user",
105
106            // URL with & (no ?)
107            @"http://www.mysite.com/&123", @"http://www.mysite.com/&123",
108
109            // URL with '
110            @"http://www.mysite.com/'user", @"http://www.mysite.com/'user",
111
112            // URL with "
113            @"http://www.mysite.com/\"user", @"http://www.mysite.com/%22user",
114
115            // URL with (
116            @"http://www.mysite.com/(123", @"http://www.mysite.com/(123",
117
118            // URL with )
119            @"http://www.mysite.com/)123", @"http://www.mysite.com/)123",
120
121            // URL with +
122            @"http://www.mysite.com/+123", @"http://www.mysite.com/+123",
123
124            // URL with *
125            @"http://www.mysite.com/*123", @"http://www.mysite.com/*123",
126
127            // URL with space
128            @"http://www.mysite.com/user name",
129            @"http://www.mysite.com/user%20name",
130
131            // URL with unescaped European accented characters
132            @"http://fr.news.yahoo.com/bactérie-e-coli-ajouter-vinaigre-leau-"
133             "rinçage-légumes-061425535.html",
134            @"http://fr.news.yahoo.com/bact%C3%A9rie-e-coli-ajouter-vinaigre-"
135             "leau-rin%C3%A7age-l%C3%A9gumes-061425535.html",
136
137            // URL with mix of unescaped European accented characters
138            @"http://fr.news.yahoo.com/bactérie-e-coli-ajouter-vinaigre-leau-"
139             "rinçage-l%C3%A9gumes-061425535.html",
140            @"http://fr.news.yahoo.com/bact%C3%A9rie-e-coli-ajouter-vinaigre-"
141             "leau-rin%C3%A7age-l%C3%A9gumes-061425535.html",
142
143            // URL with unescaped Asian unicode characters
144            @"http://www.baidu.com/s?cl=3&fr=tb01000&wd=鍜嬩箞鍠傞",
145            @"http://www.baidu.com/s?cl=3&fr=tb01000&wd="
146             "%E9%8D%9C%E5%AC%A9%E7%AE%9E%E9%8D%A0%E5%82%9E",
147
148            // URL containing every character in the range 20->7F
149            @"http://google.com/ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLM"
150             "NOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
151            @"http://google.com/%20!%22#$%25&'()*+,-./0123456789:;%3C=%3E?@ABCD"
152             "EFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz"
153             "%7B%7C%7D~",
154
155            // URL containing every accented character from the range 80->FF
156            @"http://google.com/¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìí"
157             "îïðñòóôõö÷øùúûüýþÿ",
158            @"http://google.com/%C2%BF%C3%80%C3%81%C3%82%C3%83%C3%84%C3%85%C3"
159             "%86%C3%87%C3%88%C3%89%C3%8A%C3%8B%C3%8C%C3%8D%C3%8E%C3%8F%C3%90"
160             "%C3%91%C3%92%C3%93%C3%94%C3%95%C3%96%C3%97%C3%98%C3%99%C3%9A%C3"
161             "%9B%C3%9C%C3%9D%C3%9E%C3%9F%C3%A0%C3%A1%C3%A2%C3%A3%C3%A4%C3%A5"
162             "%C3%A6%C3%A7%C3%A8%C3%A9%C3%AA%C3%AB%C3%AC%C3%AD%C3%AE%C3%AF%C3"
163             "%B0%C3%B1%C3%B2%C3%B3%C3%B4%C3%B5%C3%B6%C3%B7%C3%B8%C3%B9%C3%BA"
164             "%C3%BB%C3%BC%C3%BD%C3%BE%C3%BF",
165
166            // URL containing every character in the range 20->7F repeated twice
167            @"http://google.com/ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLM"
168             "NOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ !\"#$%&'()*+,-"
169             "./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm"
170             "nopqrstuvwxyz{|}~",
171            @"http://google.com/%20!%22#$%25&'()*+,-./0123456789:;%3C=%3E?@ABCD"
172             "EFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz"
173             "%7B%7C%7D~%20!%22%23$%25&'()*+,-./0123456789:;%3C=%3E?@ABCDEFGHIJ"
174             "KLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C"
175             "%7D~",
176
177            // Special case 7F. the weird out of place control character for DEL
178            @"data:,\177", @"data:,%7F",
179
180            // URL with some common control characters.
181            // GURL simply removes most control characters.
182            @"data:,\a\b\t\r\n", @"data:,",
183
184            // All control characters but \000.
185            @"data:,\001\002\003\004\005\006\007\010\011\012\013\014\015\016"
186             "\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036"
187             "\037",
188            @"data:,",
189
190            // Square brackets shouldn't be escaped in IPv6 literal.
191            @"http://[::1]/path/", @"http://[::1]/path/",
192
193            // Test all possible features of a URL.
194            @"http://foobar:[email protected]:8080/some/path/"
195             "file.html;params-here?foo=bar#baz",
196            @"http://foobar:[email protected]:8080/some/path/"
197             "file.html;params-here?foo=bar#baz",
198
199            // Test a username and password that require escaping.
200            @"http://john!<]:bar[@foo.com/path",
201            @"http://john!%3C%5D:bar%[email protected]/path",
202
203            nil];
204  }
205
206  // NSArray of NSString pairs used for running the tests.
207  // Each pair is in the form <input value, expected result>.
208  NSArray<NSString*>* __strong test_data_;
209};
210
211TEST_F(URLConversionTest, TestNSURLCreationFromStrings) {
212  for (NSUInteger i = 0; i < test_data_.count; i += 2) {
213    NSString* input = test_data_[i];
214    NSString* expected = test_data_[i + 1];
215    NSURL* url = NSURLWithGURL(GURL(base::SysNSStringToUTF8(input)));
216    EXPECT_NSEQ(expected, url.absoluteString);
217  }
218}
219
220TEST_F(URLConversionTest, TestURLWithStringDoesNotModifyAlreadyEscapedURLs) {
221  for (NSUInteger i = 0; i < test_data_.count; i += 2) {
222    NSString* input = test_data_[i + 1];
223    NSURL* url = NSURLWithGURL(GURL(base::SysNSStringToUTF8(input)));
224    NSString* expected = test_data_[i + 1];
225    // Test the expected URL is created.
226    EXPECT_NSEQ(expected, url.absoluteString);
227  }
228}
229
230}  // namespace
231
232}  // namespace net
233