1 // Copyright 2012 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.
5 #import <Foundation/Foundation.h>
7 #include "base/mac/scoped_nsobject.h"
8 #include "base/strings/sys_string_conversions.h"
9 #import "net/base/mac/url_conversions.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/gtest_mac.h"
18 class URLConversionTest : public ::testing::Test {
20 void SetUp() override {
21 testData_.reset([[NSArray alloc]
23 // Simple URL with protocol
24 @"https://www.google.com/", @"https://www.google.com/",
26 // Simple URL with protocol and query string
27 @"https://www.google.com/search?q=gtest",
28 @"https://www.google.com/search?q=gtest",
30 // Simple URL with protocol and query string multiple params
31 @"https://www.google.com/search?hl=en&q=gtest",
32 @"https://www.google.com/search?hl=en&q=gtest",
34 // Simple URL with protocol and query string and fragment
35 @"https://www.google.com/search?q=gtest#123",
36 @"https://www.google.com/search?q=gtest#123",
39 @"http://www.mysite.com/~user", @"http://www.mysite.com/~user",
42 @"http://www.mysite.com/#123456", @"http://www.mysite.com/#123456",
44 // URL with # before ?
45 @"http://www.mysite.com/test#test?test",
46 @"http://www.mysite.com/test#test?test",
48 // URL with ? before #
49 @"http://www.mysite.com/test?test#test",
50 @"http://www.mysite.com/test?test#test",
53 @"http://www.mysite.com/test#test#test",
54 @"http://www.mysite.com/test#test%23test",
57 @"http://www.mysite.com/test?test?test",
58 @"http://www.mysite.com/test?test?test",
60 // URL with pattern ? # ?
61 @"http://www.mysite.com/test?test#test?test",
62 @"http://www.mysite.com/test?test#test?test",
64 // URL with pattern # ? #
65 @"http://www.mysite.com/test#test?test#test",
66 @"http://www.mysite.com/test#test?test%23test",
69 @"http://www.mysite.com/%123", @"http://www.mysite.com/%123",
72 @"http://www.mysite.com/[123", @"http://www.mysite.com/%5B123",
75 @"http://www.mysite.com/]123", @"http://www.mysite.com/%5D123",
78 @"http://www.mysite.com/`123", @"http://www.mysite.com/%60123",
81 @"http://www.mysite.com/^123", @"http://www.mysite.com/%5E123",
83 // URL with backslash (GURL canonicallizes unescaped \ to /)
84 @"http://www.mysite.com/\\123", @"http://www.mysite.com//123",
87 @"http://www.mysite.com/~user name",
88 @"http://www.mysite.com/~user%20name",
91 @"http://www.mysite.com/123<456",
92 @"http://www.mysite.com/123%3C456",
95 @"http://www.mysite.com/456>123",
96 @"http://www.mysite.com/456%3E123",
99 @"http://www.mysite.com/|123", @"http://www.mysite.com/%7C123",
102 @"http://www.mysite.com/!123", @"http://www.mysite.com/!123",
105 @"http://www.mysite.com/~user", @"http://www.mysite.com/~user",
108 @"http://www.mysite.com/&123", @"http://www.mysite.com/&123",
111 @"http://www.mysite.com/'user", @"http://www.mysite.com/'user",
114 @"http://www.mysite.com/\"user", @"http://www.mysite.com/%22user",
117 @"http://www.mysite.com/(123", @"http://www.mysite.com/(123",
120 @"http://www.mysite.com/)123", @"http://www.mysite.com/)123",
123 @"http://www.mysite.com/+123", @"http://www.mysite.com/+123",
126 @"http://www.mysite.com/*123", @"http://www.mysite.com/*123",
129 @"http://www.mysite.com/user name",
130 @"http://www.mysite.com/user%20name",
132 // URL with unescaped European accented characters
133 @"http://fr.news.yahoo.com/bactérie-e-coli-ajouter-vinaigre-leau-"
134 "rinçage-légumes-061425535.html",
135 @"http://fr.news.yahoo.com/bact%C3%A9rie-e-coli-ajouter-vinaigre-"
136 "leau-rin%C3%A7age-l%C3%A9gumes-061425535.html",
138 // URL with mix of unescaped European accented characters
139 @"http://fr.news.yahoo.com/bactérie-e-coli-ajouter-vinaigre-leau-"
140 "rinçage-l%C3%A9gumes-061425535.html",
141 @"http://fr.news.yahoo.com/bact%C3%A9rie-e-coli-ajouter-vinaigre-"
142 "leau-rin%C3%A7age-l%C3%A9gumes-061425535.html",
144 // URL with unescaped Asian unicode characters
145 @"http://www.baidu.com/s?cl=3&fr=tb01000&wd=鍜嬩箞鍠傞",
146 @"http://www.baidu.com/s?cl=3&fr=tb01000&wd="
147 "%E9%8D%9C%E5%AC%A9%E7%AE%9E%E9%8D%A0%E5%82%9E",
149 // URL containing every character in the range 20->7F
150 @"http://google.com/ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLM"
151 "NOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
152 @"http://google.com/%20!%22#$%25&'()*+,-./0123456789:;%3C=%3E?@ABCD"
153 "EFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz"
156 // URL containing every accented character from the range 80->FF
157 @"http://google.com/¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìí"
158 "îïðñòóôõö÷øùúûüýþÿ",
159 @"http://google.com/%C2%BF%C3%80%C3%81%C3%82%C3%83%C3%84%C3%85%C3"
160 "%86%C3%87%C3%88%C3%89%C3%8A%C3%8B%C3%8C%C3%8D%C3%8E%C3%8F%C3%90"
161 "%C3%91%C3%92%C3%93%C3%94%C3%95%C3%96%C3%97%C3%98%C3%99%C3%9A%C3"
162 "%9B%C3%9C%C3%9D%C3%9E%C3%9F%C3%A0%C3%A1%C3%A2%C3%A3%C3%A4%C3%A5"
163 "%C3%A6%C3%A7%C3%A8%C3%A9%C3%AA%C3%AB%C3%AC%C3%AD%C3%AE%C3%AF%C3"
164 "%B0%C3%B1%C3%B2%C3%B3%C3%B4%C3%B5%C3%B6%C3%B7%C3%B8%C3%B9%C3%BA"
165 "%C3%BB%C3%BC%C3%BD%C3%BE%C3%BF",
167 // URL containing every character in the range 20->7F repeated twice
168 @"http://google.com/ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLM"
169 "NOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ !\"#$%&'()*+,-"
170 "./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm"
172 @"http://google.com/%20!%22#$%25&'()*+,-./0123456789:;%3C=%3E?@ABCD"
173 "EFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz"
174 "%7B%7C%7D~%20!%22%23$%25&'()*+,-./0123456789:;%3C=%3E?@ABCDEFGHIJ"
175 "KLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C"
178 // Special case 7F. the weird out of place control character for DEL
179 @"data:,\177", @"data:,%7F",
181 // URL with some common control characters.
182 // GURL simply removes most control characters.
183 @"data:,\a\b\t\r\n", @"data:,",
185 // All control characters but \000.
186 @"data:,\001\002\003\004\005\006\007\010\011\012\013\014\015\016"
187 "\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036"
191 // Square brackets shouldn't be escaped in IPv6 literal.
192 @"http://[::1]/path/", @"http://[::1]/path/",
194 // Test all possible features of a URL.
195 @"http://foobar:nicate@example.com:8080/some/path/"
196 "file.html;params-here?foo=bar#baz",
197 @"http://foobar:nicate@example.com:8080/some/path/"
198 "file.html;params-here?foo=bar#baz",
200 // Test a username and password that require escaping.
201 @"http://john!<]:bar[@foo.com/path",
202 @"http://john!%3C%5D:bar%5B@foo.com/path",
207 // NSArray of NSString pairs used for running the tests.
208 // Each pair is in the form <input value, expected result>.
209 base::scoped_nsobject<NSArray> testData_;
212 TEST_F(URLConversionTest, TestNSURLCreationFromStrings) {
213 for (NSUInteger i = 0; i < [testData_ count]; i += 2) {
214 NSString* inputStr = [testData_ objectAtIndex:i];
215 NSString* expected = [testData_ objectAtIndex:(i + 1)];
216 NSURL* url = NSURLWithGURL(GURL(base::SysNSStringToUTF8(inputStr)));
217 EXPECT_NSEQ(expected, [url absoluteString]);
221 TEST_F(URLConversionTest, TestURLWithStringDoesNotModifyAlreadyEscapedURLs) {
222 for (NSUInteger i = 0; i < [testData_ count]; i += 2) {
223 NSString* inputStr = [testData_ objectAtIndex:i + 1];
224 NSURL* url = NSURLWithGURL(GURL(base::SysNSStringToUTF8(inputStr)));
225 NSString* expected = [testData_ objectAtIndex:i + 1];
226 // Test the expected URL is created.
227 EXPECT_NSEQ(expected, [url absoluteString]);