Add a way to check if a host is an IP address
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WebCore / URL.cpp
1 /*
2  * Copyright (C) 2011, 2012 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "WTFStringUtilities.h"
28 #include <WebCore/URL.h>
29 #include <WebCore/URLParser.h>
30 #include <wtf/MainThread.h>
31
32 using namespace WebCore;
33
34 namespace TestWebKitAPI {
35
36 class URLTest : public testing::Test {
37 public:
38     virtual void SetUp()
39     {
40         WTF::initializeMainThread();
41     }
42 };
43
44 TEST_F(URLTest, URLConstructorDefault)
45 {
46     URL kurl;
47
48     EXPECT_TRUE(kurl.isEmpty());
49     EXPECT_TRUE(kurl.isNull());
50     EXPECT_FALSE(kurl.isValid());
51 }
52
53 TEST_F(URLTest, URLConstructorConstChar)
54 {
55     URL kurl(ParsedURLString, "http://username:password@www.example.com:8080/index.html?var=val#fragment");
56
57     EXPECT_FALSE(kurl.isEmpty());
58     EXPECT_FALSE(kurl.isNull());
59     EXPECT_TRUE(kurl.isValid());
60
61     EXPECT_EQ(kurl.protocol() == "http", true);
62     EXPECT_EQ(String("www.example.com"), kurl.host());
63     EXPECT_TRUE(!!kurl.port());
64     EXPECT_EQ(8080, kurl.port().value());
65     EXPECT_EQ(String("username"), kurl.user());
66     EXPECT_EQ(String("password"), kurl.pass());
67     EXPECT_EQ(String("/index.html"), kurl.path());
68     EXPECT_EQ(String("index.html"), kurl.lastPathComponent());
69     EXPECT_EQ(String("var=val"), kurl.query());
70     EXPECT_TRUE(kurl.hasFragmentIdentifier());
71     EXPECT_EQ(String("fragment"), kurl.fragmentIdentifier());
72 }
73
74 static URL createURL(const char* urlAsString)
75 {
76     URLParser parser(urlAsString);
77     return parser.result();
78 };
79
80 TEST_F(URLTest, URLProtocolHostAndPort)
81 {
82     auto url = createURL("http://username:password@www.example.com:8080/index.html?var=val#fragment");
83     EXPECT_EQ(String("http://www.example.com:8080"), url.protocolHostAndPort());
84
85     url = createURL("http://username:@www.example.com:8080/index.html?var=val#fragment");
86     EXPECT_EQ(String("http://www.example.com:8080"), url.protocolHostAndPort());
87
88     url = createURL("http://:password@www.example.com:8080/index.html?var=val#fragment");
89     EXPECT_EQ(String("http://www.example.com:8080"), url.protocolHostAndPort());
90
91     url = createURL("http://username@www.example.com:8080/index.html?var=val#fragment");
92     EXPECT_EQ(String("http://www.example.com:8080"), url.protocolHostAndPort());
93
94     url = createURL("http://www.example.com:8080/index.html?var=val#fragment");
95     EXPECT_EQ(String("http://www.example.com:8080"), url.protocolHostAndPort());
96
97     url = createURL("http://www.example.com:/index.html?var=val#fragment");
98     EXPECT_EQ(String("http://www.example.com"), url.protocolHostAndPort());
99
100     url = createURL("http://www.example.com/index.html?var=val#fragment");
101     EXPECT_EQ(String("http://www.example.com"), url.protocolHostAndPort());
102
103     url = createURL("file:///a/b/c");
104     EXPECT_EQ(String("file://"), url.protocolHostAndPort());
105
106     url = createURL("file:///a/b");
107     EXPECT_EQ(String("file://"), url.protocolHostAndPort());
108
109     url = createURL("file:///a");
110     EXPECT_EQ(String("file://"), url.protocolHostAndPort());
111
112     url = createURL("file:///a");
113     EXPECT_EQ(String("file://"), url.protocolHostAndPort());
114
115     url = createURL("asdf://username:password@www.example.com:8080/index.html?var=val#fragment");
116     EXPECT_EQ(String("asdf://www.example.com:8080"), url.protocolHostAndPort());
117
118     url = createURL("asdf:///a/b/c");
119     EXPECT_EQ(String("asdf://"), url.protocolHostAndPort());
120 }
121
122 TEST_F(URLTest, URLDataURIStringSharing)
123 {
124     URL baseURL(ParsedURLString, "http://www.webkit.org/");
125     String threeApples = "data:text/plain;charset=utf-8;base64,76O/76O/76O/";
126
127     URL url(baseURL, threeApples);
128     EXPECT_EQ(threeApples.impl(), url.string().impl());
129 }
130
131 TEST_F(URLTest, URLSetQuery)
132 {
133     URL url = createURL("http://www.webkit.org/?test");
134     URL url1 = createURL("http://www.webkit.org/");
135     URL url2 = createURL("http://www.webkit.org/?");
136     URL url3 = createURL("http://www.webkit.org/?test");
137     URL url4 = createURL("http://www.webkit.org/?test1");
138
139     url1.setQuery("test");
140     url2.setQuery("test");
141     url3.setQuery("test");
142     url4.setQuery("test");
143
144     EXPECT_EQ(url.string(), url1.string());
145     EXPECT_EQ(url.string(), url2.string());
146     EXPECT_EQ(url.string(), url3.string());
147     EXPECT_EQ(url.string(), url4.string());
148
149     URL urlWithFragmentIdentifier = createURL("http://www.webkit.org/?test%C3%83%C2%A5#newFragment");
150     URL urlWithFragmentIdentifier1 = createURL("http://www.webkit.org/#newFragment");
151     URL urlWithFragmentIdentifier2 = createURL("http://www.webkit.org/?#newFragment");
152     URL urlWithFragmentIdentifier3 = createURL("http://www.webkit.org/?test1#newFragment");
153
154     urlWithFragmentIdentifier1.setQuery("test\xc3\xa5");
155     urlWithFragmentIdentifier2.setQuery("test\xc3\xa5");
156     urlWithFragmentIdentifier3.setQuery("test\xc3\xa5");
157
158     EXPECT_EQ(urlWithFragmentIdentifier.string(), urlWithFragmentIdentifier1.string());
159     EXPECT_EQ(urlWithFragmentIdentifier.string(), urlWithFragmentIdentifier2.string());
160     EXPECT_EQ(urlWithFragmentIdentifier.string(), urlWithFragmentIdentifier3.string());
161 }
162
163 TEST_F(URLTest, URLSetFragmentIdentifier)
164 {
165     URL url = createURL("http://www.webkit.org/#newFragment%C3%83%C2%A5");
166     URL url1 = createURL("http://www.webkit.org/");
167     URL url2 = createURL("http://www.webkit.org/#test2");
168     URL url3 = createURL("http://www.webkit.org/#");
169
170     url1.setFragmentIdentifier("newFragment\xc3\xa5");
171     url2.setFragmentIdentifier("newFragment\xc3\xa5");
172     url3.setFragmentIdentifier("newFragment\xc3\xa5");
173
174     EXPECT_EQ(url.string(), url1.string());
175     EXPECT_EQ(url.string(), url2.string());
176     EXPECT_EQ(url.string(), url3.string());
177
178     URL urlWithQuery = createURL("http://www.webkit.org/?test1#newFragment");
179     URL urlWithQuery1 = createURL("http://www.webkit.org/?test1");
180     URL urlWithQuery2 = createURL("http://www.webkit.org/?test1#");
181     URL urlWithQuery3 = createURL("http://www.webkit.org/?test1#test2");
182
183     urlWithQuery1.setFragmentIdentifier("newFragment");
184     urlWithQuery2.setFragmentIdentifier("newFragment");
185     urlWithQuery3.setFragmentIdentifier("newFragment");
186
187     EXPECT_EQ(urlWithQuery.string(), urlWithQuery1.string());
188     EXPECT_EQ(urlWithQuery.string(), urlWithQuery2.string());
189     EXPECT_EQ(urlWithQuery.string(), urlWithQuery3.string());
190 }
191
192 TEST_F(URLTest, URLRemoveQueryAndFragmentIdentifier)
193 {
194     URL url = createURL("http://www.webkit.org/");
195     URL url1 = createURL("http://www.webkit.org/?");
196     URL url2 = createURL("http://www.webkit.org/?test1");
197     URL url3 = createURL("http://www.webkit.org/?test1#test2");
198     URL url4 = createURL("http://www.webkit.org/#test2");
199     URL url5 = createURL("http://www.webkit.org/#");
200
201     url.removeQueryAndFragmentIdentifier();
202     url1.removeQueryAndFragmentIdentifier();
203     url2.removeQueryAndFragmentIdentifier();
204     url3.removeQueryAndFragmentIdentifier();
205     url4.removeQueryAndFragmentIdentifier();
206     url5.removeQueryAndFragmentIdentifier();
207
208     EXPECT_EQ(url.string(), url.string());
209     EXPECT_EQ(url.string(), url1.string());
210     EXPECT_EQ(url.string(), url2.string());
211     EXPECT_EQ(url.string(), url3.string());
212     EXPECT_EQ(url.string(), url4.string());
213     EXPECT_EQ(url.string(), url5.string());
214 }
215
216 TEST_F(URLTest, ProtocolIsInHTTPFamily)
217 {
218     EXPECT_FALSE(protocolIsInHTTPFamily({}));
219     EXPECT_FALSE(protocolIsInHTTPFamily(""));
220     EXPECT_FALSE(protocolIsInHTTPFamily("a"));
221     EXPECT_FALSE(protocolIsInHTTPFamily("ab"));
222     EXPECT_FALSE(protocolIsInHTTPFamily("abc"));
223     EXPECT_FALSE(protocolIsInHTTPFamily("abcd"));
224     EXPECT_FALSE(protocolIsInHTTPFamily("abcde"));
225     EXPECT_FALSE(protocolIsInHTTPFamily("abcdef"));
226     EXPECT_FALSE(protocolIsInHTTPFamily("abcdefg"));
227     EXPECT_TRUE(protocolIsInHTTPFamily("http:"));
228     EXPECT_FALSE(protocolIsInHTTPFamily("http"));
229     EXPECT_TRUE(protocolIsInHTTPFamily("https:"));
230     EXPECT_FALSE(protocolIsInHTTPFamily("https"));
231     EXPECT_TRUE(protocolIsInHTTPFamily("https://!@#$%^&*()"));
232 }
233
234 TEST_F(URLTest, HostIsIPAddress)
235 {
236     EXPECT_FALSE(URL::hostIsIPAddress({ }));
237     EXPECT_FALSE(URL::hostIsIPAddress(""));
238     EXPECT_FALSE(URL::hostIsIPAddress("localhost"));
239     EXPECT_FALSE(URL::hostIsIPAddress("127.localhost"));
240     EXPECT_FALSE(URL::hostIsIPAddress("localhost.127"));
241     EXPECT_FALSE(URL::hostIsIPAddress("127.0.0"));
242     EXPECT_FALSE(URL::hostIsIPAddress("127.0 .0.1"));
243     EXPECT_FALSE(URL::hostIsIPAddress(" 127.0.0.1"));
244     EXPECT_FALSE(URL::hostIsIPAddress("127..0.0.1"));
245     EXPECT_FALSE(URL::hostIsIPAddress("127.0.0."));
246     EXPECT_FALSE(URL::hostIsIPAddress("0123:4567:89AB:cdef:3210:7654:ba98"));
247     EXPECT_FALSE(URL::hostIsIPAddress("012x:4567:89AB:cdef:3210:7654:ba98:FeDc"));
248     EXPECT_FALSE(URL::hostIsIPAddress("00123:4567:89AB:cdef:3210:7654:ba98:FeDc"));
249     EXPECT_FALSE(URL::hostIsIPAddress("0123:4567:89AB:cdef:3210:123.45.67.89"));
250     EXPECT_FALSE(URL::hostIsIPAddress(":::"));
251
252     EXPECT_TRUE(URL::hostIsIPAddress("127.0.0.1"));
253     EXPECT_TRUE(URL::hostIsIPAddress("123.45.67.89"));
254     EXPECT_TRUE(URL::hostIsIPAddress("0.0.0.0"));
255     EXPECT_TRUE(URL::hostIsIPAddress("::1"));
256     EXPECT_TRUE(URL::hostIsIPAddress("::"));
257     EXPECT_TRUE(URL::hostIsIPAddress("0123:4567:89AB:cdef:3210:7654:ba98:FeDc"));
258     EXPECT_TRUE(URL::hostIsIPAddress("0123:4567:89AB:cdef:3210:7654:ba98::"));
259     EXPECT_TRUE(URL::hostIsIPAddress("::4567:89AB:cdef:3210:7654:ba98:FeDc"));
260     EXPECT_TRUE(URL::hostIsIPAddress("0123:4567:89AB:cdef:3210:7654:123.45.67.89"));
261     EXPECT_TRUE(URL::hostIsIPAddress("::123.45.67.89"));
262 }
263
264 } // namespace TestWebKitAPI