1 // Copyright 2008, 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 "googleurl/src/url_canon.h"
31 #include "googleurl/src/url_canon_stdstring.h"
32 #include "googleurl/src/url_parse.h"
33 #include "googleurl/src/url_util.h"
34 #include "testing/gtest/include/gtest/gtest.h"
35
TEST(URLUtilTest,FindAndCompareScheme)36 TEST(URLUtilTest, FindAndCompareScheme) {
37 url_parse::Component found_scheme;
38
39 // Simple case where the scheme is found and matches.
40 const char kStr1[] = "http://www.com/";
41 EXPECT_TRUE(url_util::FindAndCompareScheme(
42 kStr1, static_cast<int>(strlen(kStr1)), "http", NULL));
43 EXPECT_TRUE(url_util::FindAndCompareScheme(
44 kStr1, static_cast<int>(strlen(kStr1)), "http", &found_scheme));
45 EXPECT_TRUE(found_scheme == url_parse::Component(0, 4));
46
47 // A case where the scheme is found and doesn't match.
48 EXPECT_FALSE(url_util::FindAndCompareScheme(
49 kStr1, static_cast<int>(strlen(kStr1)), "https", &found_scheme));
50 EXPECT_TRUE(found_scheme == url_parse::Component(0, 4));
51
52 // A case where there is no scheme.
53 const char kStr2[] = "httpfoobar";
54 EXPECT_FALSE(url_util::FindAndCompareScheme(
55 kStr2, static_cast<int>(strlen(kStr2)), "http", &found_scheme));
56 EXPECT_TRUE(found_scheme == url_parse::Component());
57
58 // When there is an empty scheme, it should match the empty scheme.
59 const char kStr3[] = ":foo.com/";
60 EXPECT_TRUE(url_util::FindAndCompareScheme(
61 kStr3, static_cast<int>(strlen(kStr3)), "", &found_scheme));
62 EXPECT_TRUE(found_scheme == url_parse::Component(0, 0));
63
64 // But when there is no scheme, it should fail.
65 EXPECT_FALSE(url_util::FindAndCompareScheme("", 0, "", &found_scheme));
66 EXPECT_TRUE(found_scheme == url_parse::Component());
67 }
68
TEST(URLUtilTest,ReplaceComponents)69 TEST(URLUtilTest, ReplaceComponents) {
70 url_parse::Parsed parsed;
71 url_canon::RawCanonOutputT<char> output;
72 url_parse::Parsed new_parsed;
73
74 // Check that the following calls do not cause crash
75 url_canon::Replacements<char> replacements;
76 replacements.SetRef("test", url_parse::Component(0, 4));
77 url_util::ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output,
78 &new_parsed);
79 url_util::ReplaceComponents("", 0, parsed, replacements, NULL, &output,
80 &new_parsed);
81 replacements.ClearRef();
82 replacements.SetHost("test", url_parse::Component(0, 4));
83 url_util::ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output,
84 &new_parsed);
85 url_util::ReplaceComponents("", 0, parsed, replacements, NULL, &output,
86 &new_parsed);
87
88 replacements.ClearHost();
89 url_util::ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output,
90 &new_parsed);
91 url_util::ReplaceComponents("", 0, parsed, replacements, NULL, &output,
92 &new_parsed);
93 url_util::ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output,
94 &new_parsed);
95 url_util::ReplaceComponents("", 0, parsed, replacements, NULL, &output,
96 &new_parsed);
97 }
98
99