~ubuntu-branches/ubuntu/karmic/gears/karmic

« back to all changes in this revision

Viewing changes to third_party/googleurl/src/url_parse_unittest.cc

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Lesicnik
  • Date: 2009-04-30 19:15:25 UTC
  • Revision ID: james.westby@ubuntu.com-20090430191525-0790sb5wzg8ou0xb
Tags: upstream-0.5.21.0~svn3334+dfsg
ImportĀ upstreamĀ versionĀ 0.5.21.0~svn3334+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2007, Google Inc.
 
2
// 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 are
 
6
// met:
 
7
//
 
8
//     * Redistributions of source code must retain the above copyright
 
9
// notice, this list of conditions and the following disclaimer.
 
10
//     * Redistributions in binary form must reproduce the above
 
11
// copyright notice, this list of conditions and the following disclaimer
 
12
// in the documentation and/or other materials provided with the
 
13
// distribution.
 
14
//     * Neither the name of Google Inc. nor the names of its
 
15
// contributors may be used to endorse or promote products derived from
 
16
// this software without specific prior written permission.
 
17
//
 
18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
19
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
20
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
21
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
22
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
24
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
25
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
26
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
28
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 
 
30
#include "base/basictypes.h"
 
31
#include "googleurl/src/url_parse.h"
 
32
#include "testing/gtest/include/gtest/gtest.h"
 
33
 
 
34
// Some implementations of base/basictypes.h may define ARRAYSIZE.
 
35
// If it's not defined, we define it to the ARRAYSIZE_UNSAFE macro
 
36
// which is in our version of basictypes.h.
 
37
#ifndef ARRAYSIZE
 
38
#define ARRAYSIZE ARRAYSIZE_UNSAFE
 
39
#endif
 
40
 
 
41
// Interesting IE file:isms...
 
42
//
 
43
//  file:/foo/bar              file:///foo/bar
 
44
//      The result here seems totally invalid!?!? This isn't UNC.
 
45
//
 
46
//  file:/
 
47
//  file:// or any other number of slashes
 
48
//      IE6 doesn't do anything at all if you click on this link. No error:
 
49
//      nothing. IE6's history system seems to always color this link, so I'm
 
50
//      guessing that it maps internally to the empty URL.
 
51
//
 
52
//  C:\                        file:///C:/
 
53
//  /                          file:///C:/
 
54
//  /foo                       file:///C:/foo
 
55
//      Interestingly, IE treats "/" as an alias for "c:\", which makes sense,
 
56
//      but is weird to think about on Windows.
 
57
//
 
58
//  file:foo/                  file:foo/  (invalid?!?!?)
 
59
//  file:/foo/                 file:///foo/  (invalid?!?!?)
 
60
//  file://foo/                file://foo/   (UNC to server "foo")
 
61
//  file:///foo/               file:///foo/  (invalid)
 
62
//  file:////foo/              file://foo/   (UNC to server "foo")
 
63
//      Any more than four slashes is also treated as UNC.
 
64
//
 
65
//  file:C:/                   file://C:/
 
66
//  file:/C:/                  file://C:/
 
67
//      The number of slashes after "file:" don't matter if the thing following
 
68
//      it looks like an absolute drive path. Also, slashes and backslashes are
 
69
//      equally valid here.
 
70
 
 
71
namespace {
 
72
 
 
73
// Used for regular URL parse cases.
 
74
struct URLParseCase {
 
75
  const char* input;
 
76
 
 
77
  const char* scheme;
 
78
  const char* username;
 
79
  const char* password;
 
80
  const char* host;
 
81
  int port;
 
82
  const char* path;
 
83
  const char* query;
 
84
  const char* ref;
 
85
};
 
86
 
 
87
// Simpler version of URLParseCase for testing path URLs.
 
88
struct PathURLParseCase {
 
89
  const char* input;
 
90
 
 
91
  const char* scheme;
 
92
  const char* path;
 
93
};
 
94
 
 
95
// Simpler version of URLParseCase for testing mailto URLs.
 
96
struct MailtoURLParseCase {
 
97
  const char* input;
 
98
 
 
99
  const char* scheme;
 
100
  const char* path;
 
101
  const char* query;
 
102
};
 
103
 
 
104
 
 
105
bool ComponentMatches(const char* input,
 
106
                      const char* reference,
 
107
                      const url_parse::Component& component) {
 
108
  // If the component is nonexistant (length == -1), it should begin at 0.
 
109
  EXPECT_TRUE(component.len >= 0 || component.len == -1);
 
110
 
 
111
  // Begin should be valid.
 
112
  EXPECT_LE(0, component.begin);
 
113
 
 
114
  // A NULL reference means the component should be nonexistant.
 
115
  if (!reference)
 
116
    return component.len == -1;
 
117
  if (component.len < 0)
 
118
    return false;  // Reference is not NULL but we don't have anything
 
119
 
 
120
  if (strlen(reference) != static_cast<size_t>(component.len))
 
121
    return false;  // Lengths don't match
 
122
 
 
123
  // Now check the actual characters.
 
124
  return strncmp(reference, &input[component.begin], component.len) == 0;
 
125
}
 
126
 
 
127
void ExpectInvalidComponent(const url_parse::Component& component) {
 
128
  EXPECT_EQ(0, component.begin);
 
129
  EXPECT_EQ(-1, component.len);
 
130
}
 
131
 
 
132
}  // namespace
 
133
 
 
134
// Parsed ----------------------------------------------------------------------
 
135
 
 
136
TEST(URLParser, Length) {
 
137
  const char* length_cases[] = {
 
138
      // One with everything in it.
 
139
    "http://user:pass@host:99/foo?bar#baz",
 
140
      // One with nothing in it.
 
141
    "",
 
142
      // Working backwards, let's start taking off stuff from the full one.
 
143
    "http://user:pass@host:99/foo?bar#",
 
144
    "http://user:pass@host:99/foo?bar",
 
145
    "http://user:pass@host:99/foo?",
 
146
    "http://user:pass@host:99/foo",
 
147
    "http://user:pass@host:99/",
 
148
    "http://user:pass@host:99",
 
149
    "http://user:pass@host:",
 
150
    "http://user:pass@host",
 
151
    "http://host",
 
152
    "http://user@",
 
153
    "http:",
 
154
  };
 
155
  for (size_t i = 0; i < arraysize(length_cases); i++) {
 
156
    int true_length = static_cast<int>(strlen(length_cases[i]));
 
157
 
 
158
    url_parse::Parsed parsed;
 
159
    url_parse::ParseStandardURL(length_cases[i], true_length, &parsed);
 
160
    
 
161
    EXPECT_EQ(true_length, parsed.Length());
 
162
  }
 
163
}
 
164
 
 
165
TEST(URLParser, CountCharactersBefore) {
 
166
  using namespace url_parse;
 
167
  struct CountCase {
 
168
    const char* url;
 
169
    Parsed::ComponentType component;
 
170
    bool include_delimiter;
 
171
    int expected_count;
 
172
  } count_cases[] = {
 
173
      // Test each possibility in the case where all components are present.
 
174
//    0         1         2
 
175
//    0123456789012345678901
 
176
    {"http://u:p@h:8/p?q#r", Parsed::SCHEME, true, 0},
 
177
    {"http://u:p@h:8/p?q#r", Parsed::SCHEME, false, 0},
 
178
    {"http://u:p@h:8/p?q#r", Parsed::USERNAME, true, 7},
 
179
    {"http://u:p@h:8/p?q#r", Parsed::USERNAME, false, 7},
 
180
    {"http://u:p@h:8/p?q#r", Parsed::PASSWORD, true, 9},
 
181
    {"http://u:p@h:8/p?q#r", Parsed::PASSWORD, false, 9},
 
182
    {"http://u:p@h:8/p?q#r", Parsed::HOST, true, 11},
 
183
    {"http://u:p@h:8/p?q#r", Parsed::HOST, false, 11},
 
184
    {"http://u:p@h:8/p?q#r", Parsed::PORT, true, 12},
 
185
    {"http://u:p@h:8/p?q#r", Parsed::PORT, false, 13},
 
186
    {"http://u:p@h:8/p?q#r", Parsed::PATH, false, 14},
 
187
    {"http://u:p@h:8/p?q#r", Parsed::PATH, true, 14},
 
188
    {"http://u:p@h:8/p?q#r", Parsed::QUERY, true, 16},
 
189
    {"http://u:p@h:8/p?q#r", Parsed::QUERY, false, 17},
 
190
    {"http://u:p@h:8/p?q#r", Parsed::REF, true, 18},
 
191
    {"http://u:p@h:8/p?q#r", Parsed::REF, false, 19},
 
192
      // Now test when the requested component is missing.
 
193
    {"http://u:p@h:8/p?", Parsed::REF, true, 17},
 
194
    {"http://u:p@h:8/p?q", Parsed::REF, true, 18},
 
195
    {"http://u:p@h:8/p#r", Parsed::QUERY, true, 16},
 
196
    {"http://u:p@h:8#r", Parsed::PATH, true, 14},
 
197
    {"http://u:p@h/", Parsed::PORT, true, 12},
 
198
    {"http://u:p@/", Parsed::HOST, true, 11},
 
199
      // This case is a little weird. It will report that the password would
 
200
      // start where the host begins. This is arguably correct, although you
 
201
      // could also argue that it should start at the '@' sign. Doing it
 
202
      // starting with the '@' sign is actually harder, so we don't bother.
 
203
    {"http://u@h/", Parsed::PASSWORD, true, 9},
 
204
    {"http://h/", Parsed::USERNAME, true, 7},
 
205
    {"http:", Parsed::USERNAME, true, 5},
 
206
    {"", Parsed::SCHEME, true, 0},
 
207
      // Make sure a random component still works when there's nothing there.
 
208
    {"", Parsed::REF, true, 0},
 
209
      // File URLs are special with no host, so we test those.
 
210
    {"file:///c:/foo", Parsed::USERNAME, true, 7},
 
211
    {"file:///c:/foo", Parsed::PASSWORD, true, 7},
 
212
    {"file:///c:/foo", Parsed::HOST, true, 7},
 
213
    {"file:///c:/foo", Parsed::PATH, true, 7},
 
214
  };
 
215
  for (size_t i = 0; i < ARRAYSIZE(count_cases); i++) {
 
216
    int length = static_cast<int>(strlen(count_cases[i].url));
 
217
 
 
218
    // Simple test to distinguish file and standard URLs.
 
219
    url_parse::Parsed parsed;
 
220
    if (length > 0 && count_cases[i].url[0] == 'f')
 
221
      url_parse::ParseFileURL(count_cases[i].url, length, &parsed);
 
222
    else
 
223
      url_parse::ParseStandardURL(count_cases[i].url, length, &parsed);
 
224
    
 
225
    int chars_before = parsed.CountCharactersBefore(
 
226
        count_cases[i].component, count_cases[i].include_delimiter);
 
227
    EXPECT_EQ(count_cases[i].expected_count, chars_before);
 
228
  }
 
229
}
 
230
 
 
231
// Standard --------------------------------------------------------------------
 
232
 
 
233
// Input                               Scheme  Usrname Passwd     Host         Port Path       Query        Ref
 
234
// ------------------------------------ ------- ------- ---------- ------------ --- ---------- ------------ -----
 
235
static URLParseCase cases[] = {
 
236
  // Regular URL with all the parts
 
237
{"http://user:pass@foo:21/bar;par?b#c", "http", "user", "pass",    "foo",       21, "/bar;par","b",          "c"},
 
238
 
 
239
  // Known schemes should lean towards authority identification
 
240
{"http:foo.com",                        "http", NULL,  NULL,      "foo.com",    -1, NULL,      NULL,        NULL},
 
241
 
 
242
  // Spaces!
 
243
{"\t   :foo.com   \n",                  "",     NULL,  NULL,      "foo.com",    -1, NULL,      NULL,        NULL},
 
244
{" foo.com  ",                          NULL,   NULL,  NULL,      "foo.com",    -1, NULL,      NULL,        NULL},
 
245
{"a:\t foo.com",                        "a",    NULL,  NULL,      "\t foo.com", -1, NULL,      NULL,        NULL},
 
246
{"http://f:21/ b ? d # e ",             "http", NULL,  NULL,      "f",          21, "/ b ",    " d ",       " e"},
 
247
 
 
248
  // Invalid port numbers should be identified and turned into -2, empty port
 
249
  // numbers should be -1. Spaces aren't allowed in port numbers
 
250
{"http://f:/c",                         "http", NULL,  NULL,      "f",          -1, "/c",      NULL,        NULL},
 
251
{"http://f:0/c",                        "http", NULL,  NULL,      "f",           0, "/c",      NULL,        NULL},
 
252
{"http://f:00000000000000/c",           "http", NULL,  NULL,      "f",           0, "/c",      NULL,        NULL},
 
253
{"http://f:00000000000000000000080/c",  "http", NULL,  NULL,      "f",          80, "/c",      NULL,        NULL},
 
254
{"http://f:b/c",                        "http", NULL,  NULL,      "f",          -2, "/c",      NULL,        NULL},
 
255
{"http://f: /c",                        "http", NULL,  NULL,      "f",          -2, "/c",      NULL,        NULL},
 
256
{"http://f:\n/c",                       "http", NULL,  NULL,      "f",          -2, "/c",      NULL,        NULL},
 
257
{"http://f:fifty-two/c",                "http", NULL,  NULL,      "f",          -2, "/c",      NULL,        NULL},
 
258
{"http://f:999999/c",                   "http", NULL,  NULL,      "f",          -2, "/c",      NULL,        NULL},
 
259
{"http://f: 21 / b ? d # e ",           "http", NULL,  NULL,      "f",          -2, "/ b ",    " d ",       " e"},
 
260
 
 
261
  // Creative URLs missing key elements
 
262
{"",                                    NULL,   NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
 
263
{"  \t",                                NULL,   NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
 
264
{":foo.com/",                           "",     NULL,  NULL,      "foo.com",    -1, "/",       NULL,        NULL},
 
265
{":foo.com\\",                          "",     NULL,  NULL,      "foo.com",    -1, "\\",      NULL,        NULL},
 
266
{":",                                   "",     NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
 
267
{":a",                                  "",     NULL,  NULL,      "a",          -1, NULL,      NULL,        NULL},
 
268
{":/",                                  "",     NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
 
269
{":\\",                                 "",     NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
 
270
{":#",                                  "",     NULL,  NULL,      NULL,         -1, NULL,      NULL,        ""},
 
271
{"#",                                   NULL,   NULL,  NULL,      NULL,         -1, NULL,      NULL,        ""},
 
272
{"#/",                                  NULL,   NULL,  NULL,      NULL,         -1, NULL,      NULL,        "/"},
 
273
{"#\\",                                 NULL,   NULL,  NULL,      NULL,         -1, NULL,      NULL,        "\\"},
 
274
{"#;?",                                 NULL,   NULL,  NULL,      NULL,         -1, NULL,      NULL,        ";?"},
 
275
{"?",                                   NULL,   NULL,  NULL,      NULL,         -1, NULL,      "",          NULL},
 
276
{"/",                                   NULL,   NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
 
277
{":23",                                 "",     NULL,  NULL,      "23",         -1, NULL,      NULL,        NULL},
 
278
{"/:23",                                "/",    NULL,  NULL,      "23",         -1, NULL,      NULL,        NULL},
 
279
{"//",                                  NULL,   NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
 
280
{"::",                                  "",     NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
 
281
{"::23",                                "",     NULL,  NULL,      NULL,         23, NULL,      NULL,        NULL},
 
282
{"foo://",                              "foo",  NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
 
283
 
 
284
  // Username/passwords and things that look like them
 
285
{"http://a:b@c:29/d",                   "http", "a",   "b",       "c",          29, "/d",      NULL,        NULL},
 
286
{"http::@c:29",                         "http", "",    "",        "c",          29, NULL,      NULL,        NULL},
 
287
  // ... "]" in the password field isn't allowed, but we tolerate it here...
 
288
{"http://&a:foo(b]c@d:2/",              "http", "&a",  "foo(b]c", "d",           2, "/",       NULL,        NULL},
 
289
{"http://::@c@d:2",                     "http", "",    ":@c",     "d",           2, NULL,      NULL,        NULL},
 
290
{"http://foo.com:b@d/",                 "http", "foo.com", "b",   "d",          -1, "/",       NULL,        NULL},
 
291
 
 
292
{"http://foo.com/\\@",                  "http", NULL,  NULL,      "foo.com",    -1, "/\\@",    NULL,        NULL},
 
293
{"http:\\\\foo.com\\",                  "http", NULL,  NULL,      "foo.com",    -1, "\\",      NULL,        NULL},
 
294
{"http:\\\\a\\b:c\\d@foo.com\\",        "http", NULL,  NULL,      "a",          -1, "\\b:c\\d@foo.com\\", NULL,   NULL},
 
295
 
 
296
  // Tolerate different numbers of slashes.
 
297
{"foo:/",                               "foo",  NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
 
298
{"foo:/bar.com/",                       "foo",  NULL,  NULL,      "bar.com",    -1, "/",       NULL,        NULL},
 
299
{"foo://///////",                       "foo",  NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
 
300
{"foo://///////bar.com/",               "foo",  NULL,  NULL,      "bar.com",    -1, "/",       NULL,        NULL},
 
301
{"foo:////://///",                      "foo",  NULL,  NULL,      NULL,         -1, "/////",   NULL,        NULL},
 
302
 
 
303
  // Raw file paths on Windows aren't handled by the parser.
 
304
{"c:/foo",                              "c",    NULL,  NULL,      "foo",        -1, NULL,      NULL,        NULL},
 
305
{"//foo/bar",                           NULL,   NULL,  NULL,      "foo",        -1, "/bar",    NULL,        NULL},
 
306
 
 
307
  // Use the first question mark for the query and the ref.
 
308
{"http://foo/path;a??e#f#g",            "http", NULL,  NULL,      "foo",        -1, "/path;a", "?e",      "f#g"},
 
309
{"http://foo/abcd?efgh?ijkl",           "http", NULL,  NULL,      "foo",        -1, "/abcd",   "efgh?ijkl", NULL},
 
310
{"http://foo/abcd#foo?bar",             "http", NULL,  NULL,      "foo",        -1, "/abcd",   NULL,        "foo?bar"},
 
311
 
 
312
  // IPV6, check also interesting uses of colons.
 
313
{"[61:24:74]:98",                       "[61",  NULL,  NULL,      "24:74]",     98, NULL,      NULL,        NULL},
 
314
{"http://[61:27]:98",                   "http", NULL,  NULL,      "[61:27]",    98, NULL,      NULL,        NULL},
 
315
{"http:[61:27]/:foo",                   "http", NULL,  NULL,      "[61:27]",    -1, "/:foo",   NULL,        NULL},
 
316
 
 
317
};
 
318
 
 
319
TEST(URLParser, Standard) {
 
320
  // Declared outside for loop to try to catch cases in init() where we forget
 
321
  // to reset something that is reset by the construtor.
 
322
  url_parse::Parsed parsed;
 
323
  for (size_t i = 0; i < arraysize(cases); i++) {
 
324
    const char* url = cases[i].input;
 
325
    url_parse::ParseStandardURL(url, static_cast<int>(strlen(url)), &parsed);
 
326
    int port = url_parse::ParsePort(url, parsed.port);
 
327
 
 
328
    EXPECT_TRUE(ComponentMatches(url, cases[i].scheme, parsed.scheme));
 
329
    EXPECT_TRUE(ComponentMatches(url, cases[i].username, parsed.username));
 
330
    EXPECT_TRUE(ComponentMatches(url, cases[i].password, parsed.password));
 
331
    EXPECT_TRUE(ComponentMatches(url, cases[i].host, parsed.host));
 
332
    EXPECT_EQ(cases[i].port, port);
 
333
    EXPECT_TRUE(ComponentMatches(url, cases[i].path, parsed.path));
 
334
    EXPECT_TRUE(ComponentMatches(url, cases[i].query, parsed.query));
 
335
    EXPECT_TRUE(ComponentMatches(url, cases[i].ref, parsed.ref));
 
336
  }
 
337
}
 
338
 
 
339
// PathURL --------------------------------------------------------------------
 
340
 
 
341
// Various incarnations of path URLs.
 
342
static PathURLParseCase path_cases[] = {
 
343
{"",                                        NULL,          NULL},
 
344
{":",                                       "",            NULL},
 
345
{":/",                                      "",            "/"},
 
346
{"/",                                       NULL,          "/"},
 
347
{" This is \\interesting// \t",             NULL,          "This is \\interesting//"},
 
348
{"about:",                                  "about",       NULL},
 
349
{"about:blank",                             "about",       "blank"},
 
350
{"  about: blank ",                         "about",       " blank"},
 
351
{"javascript :alert(\"He:/l\\l#o?foo\"); ", "javascript ", "alert(\"He:/l\\l#o?foo\");"},
 
352
};
 
353
 
 
354
TEST(URLParser, PathURL) {
 
355
  // Declared outside for loop to try to catch cases in init() where we forget
 
356
  // to reset something that is reset by the construtor.
 
357
  url_parse::Parsed parsed;
 
358
  for (size_t i = 0; i < arraysize(path_cases); i++) {
 
359
    const char* url = path_cases[i].input;
 
360
    url_parse::ParsePathURL(url, static_cast<int>(strlen(url)), &parsed);
 
361
 
 
362
    EXPECT_TRUE(ComponentMatches(url, path_cases[i].scheme, parsed.scheme));
 
363
    EXPECT_TRUE(ComponentMatches(url, path_cases[i].path, parsed.path));
 
364
 
 
365
    // The remaining components are never used for path urls.
 
366
    ExpectInvalidComponent(parsed.username);
 
367
    ExpectInvalidComponent(parsed.password);
 
368
    ExpectInvalidComponent(parsed.host);
 
369
    ExpectInvalidComponent(parsed.port);
 
370
    ExpectInvalidComponent(parsed.query);
 
371
    ExpectInvalidComponent(parsed.ref);
 
372
  }
 
373
}
 
374
 
 
375
#ifdef WIN32
 
376
 
 
377
// WindowsFile ----------------------------------------------------------------
 
378
 
 
379
// Various incarnations of file URLs. These are for Windows only.
 
380
static URLParseCase file_cases[] = {
 
381
{"file:server",              "file", NULL, NULL, "server", -1, NULL,          NULL, NULL},
 
382
{"  file: server  \t",       "file", NULL, NULL, " server",-1, NULL,          NULL, NULL},
 
383
{"FiLe:c|",                  "FiLe", NULL, NULL, NULL,     -1, "c|",          NULL, NULL},
 
384
{"FILE:/\\\\/server/file",   "FILE", NULL, NULL, "server", -1, "/file",       NULL, NULL},
 
385
{"file://server/",           "file", NULL, NULL, "server", -1, "/",           NULL, NULL},
 
386
{"file://localhost/c:/",     "file", NULL, NULL, NULL,     -1, "/c:/",        NULL, NULL},
 
387
{"file://127.0.0.1/c|\\",    "file", NULL, NULL, NULL,     -1, "/c|\\",       NULL, NULL},
 
388
{"file:/",                   "file", NULL, NULL, NULL,     -1, NULL,          NULL, NULL},
 
389
{"file:",                    "file", NULL, NULL, NULL,     -1, NULL,          NULL, NULL},
 
390
  // If there is a Windows drive letter, treat any number of slashes as the
 
391
  // path part.
 
392
{"file:c:\\fo\\b",           "file", NULL, NULL, NULL,     -1, "c:\\fo\\b",   NULL, NULL},
 
393
{"file:/c:\\foo/bar",        "file", NULL, NULL, NULL,     -1, "/c:\\foo/bar",NULL, NULL},
 
394
{"file://c:/f\\b",           "file", NULL, NULL, NULL,     -1, "/c:/f\\b",    NULL, NULL},
 
395
{"file:///C:/foo",           "file", NULL, NULL, NULL,     -1, "/C:/foo",     NULL, NULL},
 
396
{"file://///\\/\\/c:\\f\\b", "file", NULL, NULL, NULL,     -1, "/c:\\f\\b",   NULL, NULL},
 
397
  // If there is not a drive letter, we should treat is as UNC EXCEPT for
 
398
  // three slashes, which we treat as a Unix style path.
 
399
{"file:server/file",         "file", NULL, NULL, "server", -1, "/file",       NULL, NULL},
 
400
{"file:/server/file",        "file", NULL, NULL, "server", -1, "/file",       NULL, NULL},
 
401
{"file://server/file",       "file", NULL, NULL, "server", -1, "/file",       NULL, NULL},
 
402
{"file:///server/file",      "file", NULL, NULL, NULL,     -1, "/server/file",NULL, NULL},
 
403
{"file://\\server/file",     "file", NULL, NULL, NULL,     -1, "\\server/file",NULL, NULL},
 
404
{"file:////server/file",     "file", NULL, NULL, "server", -1, "/file",       NULL, NULL},
 
405
  // Queries and refs are valid for file URLs as well.
 
406
{"file:///C:/foo.html?#",   "file", NULL, NULL,  NULL,     -1, "/C:/foo.html",  "",   ""},
 
407
{"file:///C:/foo.html?query=yes#ref", "file", NULL, NULL, NULL, -1, "/C:/foo.html", "query=yes", "ref"},
 
408
};
 
409
 
 
410
TEST(URLParser, WindowsFile) {
 
411
  // Declared outside for loop to try to catch cases in init() where we forget
 
412
  // to reset something that is reset by the construtor.
 
413
  url_parse::Parsed parsed;
 
414
  for (int i = 0; i < arraysize(file_cases); i++) {
 
415
    const char* url = file_cases[i].input;
 
416
    url_parse::ParseFileURL(url, static_cast<int>(strlen(url)), &parsed);
 
417
    int port = url_parse::ParsePort(url, parsed.port);
 
418
 
 
419
    EXPECT_TRUE(ComponentMatches(url, file_cases[i].scheme, parsed.scheme));
 
420
    EXPECT_TRUE(ComponentMatches(url, file_cases[i].username, parsed.username));
 
421
    EXPECT_TRUE(ComponentMatches(url, file_cases[i].password, parsed.password));
 
422
    EXPECT_TRUE(ComponentMatches(url, file_cases[i].host, parsed.host));
 
423
    EXPECT_EQ(file_cases[i].port, port);
 
424
    EXPECT_TRUE(ComponentMatches(url, file_cases[i].path, parsed.path));
 
425
    EXPECT_TRUE(ComponentMatches(url, file_cases[i].query, parsed.query));
 
426
    EXPECT_TRUE(ComponentMatches(url, file_cases[i].ref, parsed.ref));
 
427
  }
 
428
}
 
429
 
 
430
#endif  // WIN32
 
431
 
 
432
TEST(URLParser, ExtractFileName) {
 
433
  struct FileCase {
 
434
    const char* input;
 
435
    const char* expected;
 
436
  } file_cases[] = {
 
437
    {"http://www.google.com", NULL},
 
438
    {"http://www.google.com/", ""},
 
439
    {"http://www.google.com/search", "search"},
 
440
    {"http://www.google.com/search/", ""},
 
441
    {"http://www.google.com/foo/bar.html?baz=22", "bar.html"},
 
442
    {"http://www.google.com/foo/bar.html#ref", "bar.html"},
 
443
    {"http://www.google.com/search/;param", ""},
 
444
    {"http://www.google.com/foo/bar.html;param#ref", "bar.html"},
 
445
    {"http://www.google.com/foo/bar.html;foo;param#ref", "bar.html;foo"},
 
446
    {"http://www.google.com/foo/bar.html?query#ref", "bar.html"},
 
447
  };
 
448
 
 
449
  for (size_t i = 0; i < ARRAYSIZE(file_cases); i++) {
 
450
    const char* url = file_cases[i].input;
 
451
    int len = static_cast<int>(strlen(url));
 
452
 
 
453
    url_parse::Parsed parsed;
 
454
    url_parse::ParseStandardURL(url, len, &parsed);
 
455
 
 
456
    url_parse::Component file_name;
 
457
    url_parse::ExtractFileName(url, parsed.path, &file_name);
 
458
 
 
459
    EXPECT_TRUE(ComponentMatches(url, file_cases[i].expected, file_name));
 
460
  }
 
461
}
 
462
 
 
463
// Returns true if the parameter with index |parameter| in the given URL's
 
464
// query string. The expected key can be NULL to indicate no such key index
 
465
// should exist. The parameter number is 1-based.
 
466
static bool NthParameterIs(const char* url,
 
467
                           int parameter,
 
468
                           const char* expected_key,
 
469
                           const char* expected_value) {
 
470
  url_parse::Parsed parsed;
 
471
  url_parse::ParseStandardURL(url, static_cast<int>(strlen(url)), &parsed);
 
472
 
 
473
  url_parse::Component query = parsed.query;
 
474
 
 
475
  for (int i = 1; i <= parameter; i++) {
 
476
    url_parse::Component key, value;
 
477
    if (!url_parse::ExtractQueryKeyValue(url, &query, &key, &value)) {
 
478
      if (parameter >= i && !expected_key)
 
479
        return true;  // Expected nonexistant key, got one.
 
480
      return false;  // Not enough keys.
 
481
    }
 
482
 
 
483
    if (i == parameter) {
 
484
      if (!expected_key)
 
485
        return false;
 
486
      
 
487
      if (strncmp(&url[key.begin], expected_key, key.len) != 0)
 
488
        return false;
 
489
      if (strncmp(&url[value.begin], expected_value, value.len) != 0)
 
490
        return false;
 
491
      return true;
 
492
    }
 
493
  }
 
494
  return expected_key == NULL;  // We didn't find that many parameters.
 
495
}
 
496
 
 
497
TEST(URLParser, ExtractQueryKeyValue) {
 
498
  EXPECT_TRUE(NthParameterIs("http://www.google.com", 1, NULL, NULL));
 
499
 
 
500
  // Basic case.
 
501
  char a[] = "http://www.google.com?arg1=1&arg2=2&bar";
 
502
  EXPECT_TRUE(NthParameterIs(a, 1, "arg1", "1"));
 
503
  EXPECT_TRUE(NthParameterIs(a, 2, "arg2", "2"));
 
504
  EXPECT_TRUE(NthParameterIs(a, 3, "bar", ""));
 
505
  EXPECT_TRUE(NthParameterIs(a, 4, NULL, NULL));
 
506
 
 
507
  // Empty param at the end.
 
508
  char b[] = "http://www.google.com?foo=bar&";
 
509
  EXPECT_TRUE(NthParameterIs(b, 1, "foo", "bar"));
 
510
  EXPECT_TRUE(NthParameterIs(b, 2, NULL, NULL));
 
511
 
 
512
  // Empty param at the beginning.
 
513
  char c[] = "http://www.google.com?&foo=bar";
 
514
  EXPECT_TRUE(NthParameterIs(c, 1, "", ""));
 
515
  EXPECT_TRUE(NthParameterIs(c, 2, "foo", "bar"));
 
516
  EXPECT_TRUE(NthParameterIs(c, 3, NULL, NULL));
 
517
 
 
518
  // Empty key with value.
 
519
  char d[] = "http://www.google.com?=foo";
 
520
  EXPECT_TRUE(NthParameterIs(d, 1, "", "foo"));
 
521
  EXPECT_TRUE(NthParameterIs(d, 2, NULL, NULL));
 
522
 
 
523
  // Empty value with key.
 
524
  char e[] = "http://www.google.com?foo=";
 
525
  EXPECT_TRUE(NthParameterIs(e, 1, "foo", ""));
 
526
  EXPECT_TRUE(NthParameterIs(e, 2, NULL, NULL));
 
527
 
 
528
  // Empty key and values.
 
529
  char f[] = "http://www.google.com?&&==&=";
 
530
  EXPECT_TRUE(NthParameterIs(f, 1, "", ""));
 
531
  EXPECT_TRUE(NthParameterIs(f, 2, "", ""));
 
532
  EXPECT_TRUE(NthParameterIs(f, 3, "", "="));
 
533
  EXPECT_TRUE(NthParameterIs(f, 4, "", ""));
 
534
  EXPECT_TRUE(NthParameterIs(f, 5, NULL, NULL));
 
535
}
 
536
 
 
537
// MailtoURL --------------------------------------------------------------------
 
538
 
 
539
static MailtoURLParseCase mailto_cases[] = {
 
540
//|input                       |scheme   |path               |query
 
541
{"mailto:foo@gmail.com",        "mailto", "foo@gmail.com",    NULL},
 
542
{"  mailto: to  \t",            "mailto", " to",              NULL},
 
543
{"mailto:addr1%2C%20addr2 ",    "mailto", "addr1%2C%20addr2", NULL},
 
544
{"Mailto:addr1, addr2 ",        "Mailto", "addr1, addr2",     NULL},
 
545
{"mailto:addr1:addr2 ",         "mailto", "addr1:addr2",      NULL},
 
546
{"mailto:?to=addr1,addr2",      "mailto", NULL,               "to=addr1,addr2"},
 
547
{"mailto:?to=addr1%2C%20addr2", "mailto", NULL,               "to=addr1%2C%20addr2"},
 
548
{"mailto:addr1?to=addr2",       "mailto", "addr1",            "to=addr2"},
 
549
{"mailto:?body=#foobar#",       "mailto", NULL,               "body=#foobar#",},
 
550
{"mailto:#?body=#foobar#",      "mailto", "#",                "body=#foobar#"},
 
551
};
 
552
 
 
553
TEST(URLParser, MailtoUrl) {
 
554
  // Declared outside for loop to try to catch cases in init() where we forget
 
555
  // to reset something that is reset by the construtor.
 
556
  url_parse::Parsed parsed;
 
557
  for (size_t i = 0; i < arraysize(mailto_cases); ++i) {
 
558
    const char* url = mailto_cases[i].input;
 
559
    url_parse::ParseMailtoURL(url, static_cast<int>(strlen(url)), &parsed);
 
560
    int port = url_parse::ParsePort(url, parsed.port);
 
561
 
 
562
    EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].scheme, parsed.scheme));
 
563
    EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].path, parsed.path));
 
564
    EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].query, parsed.query));
 
565
    EXPECT_EQ(url_parse::PORT_UNSPECIFIED, port);
 
566
 
 
567
    // The remaining components are never used for mailto urls.
 
568
    ExpectInvalidComponent(parsed.username);
 
569
    ExpectInvalidComponent(parsed.password);
 
570
    ExpectInvalidComponent(parsed.port);
 
571
    ExpectInvalidComponent(parsed.ref);
 
572
  }
 
573
}