Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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. | |
| 4 | |
| 5 #include "components/link_header_util/link_header_util.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 namespace link_header_util { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 void SplitLinkHeaderForTesting(const std::string& header, | |
| 16 std::vector<std::string>* values) { | |
| 17 std::vector<StringIteratorPair> values_iterators = SplitLinkHeader(header); | |
| 18 values->clear(); | |
| 19 for (const auto& pair : values_iterators) | |
| 20 values->push_back(std::string(pair.first, pair.second)); | |
| 21 } | |
| 22 | |
| 23 bool ParseLinkHeaderValueForTesting( | |
| 24 std::string value, | |
| 25 std::string* url, | |
| 26 std::unordered_map<std::string, base::Optional<std::string>>* params) { | |
| 27 return ParseLinkHeaderValue(value.begin(), value.end(), url, params); | |
| 28 } | |
| 29 | |
| 30 TEST(LinkHeaderTest, SplitEmpty) { | |
| 31 std::vector<std::string> values; | |
| 32 SplitLinkHeaderForTesting("", &values); | |
| 33 ASSERT_EQ(0u, values.size()); | |
| 34 } | |
| 35 | |
| 36 TEST(LinkHeaderTest, SplitSimple) { | |
| 37 std::vector<std::string> values; | |
| 38 SplitLinkHeaderForTesting("hello", &values); | |
| 39 ASSERT_EQ(1u, values.size()); | |
| 40 EXPECT_EQ("hello", values[0]); | |
| 41 | |
| 42 SplitLinkHeaderForTesting("foo, bar", &values); | |
| 43 ASSERT_EQ(2u, values.size()); | |
| 44 EXPECT_EQ("foo", values[0]); | |
| 45 EXPECT_EQ("bar", values[1]); | |
| 46 | |
| 47 SplitLinkHeaderForTesting(" 1\t,\t2,3", &values); | |
| 48 ASSERT_EQ(3u, values.size()); | |
| 49 EXPECT_EQ("1", values[0]); | |
| 50 EXPECT_EQ("2", values[1]); | |
| 51 EXPECT_EQ("3", values[2]); | |
| 52 } | |
| 53 | |
| 54 TEST(LinkHeaderTest, SplitSkipsEmpty) { | |
| 55 std::vector<std::string> values; | |
| 56 SplitLinkHeaderForTesting(", foo, , \t, bar", &values); | |
| 57 ASSERT_EQ(2u, values.size()); | |
| 58 EXPECT_EQ("foo", values[0]); | |
| 59 EXPECT_EQ("bar", values[1]); | |
| 60 } | |
| 61 | |
| 62 TEST(LinkHeaderTest, SplitQuotes) { | |
| 63 std::vector<std::string> values; | |
| 64 SplitLinkHeaderForTesting("\"foo,bar\", 'bar,foo', <hel,lo>", &values); | |
| 65 ASSERT_EQ(3u, values.size()); | |
| 66 EXPECT_EQ("\"foo,bar\"", values[0]); | |
| 67 EXPECT_EQ("'bar,foo'", values[1]); | |
| 68 EXPECT_EQ("<hel,lo>", values[2]); | |
| 69 } | |
| 70 | |
| 71 TEST(LinkHeaderTest, SplitEscapedQuotes) { | |
| 72 std::vector<std::string> values; | |
| 73 SplitLinkHeaderForTesting("\"f\\\"oo,bar\", 'b\\'ar,foo', <hel\\>,lo>", | |
| 74 &values); | |
| 75 ASSERT_EQ(4u, values.size()); | |
| 76 EXPECT_EQ("\"f\\\"oo,bar\"", values[0]); | |
| 77 EXPECT_EQ("'b\\'ar,foo'", values[1]); | |
| 78 EXPECT_EQ("<hel\\>", values[2]); | |
| 79 EXPECT_EQ("lo>", values[3]); | |
| 80 } | |
| 81 | |
| 82 struct SimpleParseTestData { | |
| 83 const char* link; | |
| 84 bool valid; | |
| 85 const char* url; | |
| 86 const char* rel; | |
| 87 const char* as; | |
| 88 }; | |
| 89 | |
| 90 void PrintTo(const SimpleParseTestData& test, std::ostream* os) { | |
| 91 *os << ::testing::PrintToString(test.link); | |
| 92 } | |
| 93 | |
| 94 class SimpleParseTest : public ::testing::TestWithParam<SimpleParseTestData> {}; | |
| 95 | |
| 96 TEST_P(SimpleParseTest, Simple) { | |
| 97 const SimpleParseTestData test = GetParam(); | |
| 98 | |
| 99 std::string url; | |
| 100 std::unordered_map<std::string, base::Optional<std::string>> params; | |
| 101 EXPECT_EQ(test.valid, | |
| 102 ParseLinkHeaderValueForTesting(test.link, &url, ¶ms)); | |
| 103 if (test.valid) { | |
| 104 EXPECT_EQ(test.url, url); | |
| 105 EXPECT_EQ(test.rel, params["rel"].value_or("")); | |
| 106 EXPECT_EQ(test.as, params["as"].value_or("")); | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 // Test data mostly copied from blink::LinkHeaderTest. Expectations for some | |
|
kinuko
2016/04/20 02:57:48
now that we're merging these this comment looks st
Marijn Kruisselbrink
2016/04/20 21:07:48
Good point. Updated the comment to actually match
| |
| 111 // test cases are different though. Mostly because blink::LinkHeader is stricter | |
| 112 // about validity while parsing (primarily things like mismatched quotes), and | |
| 113 // factors in knowledge about semantics of Link headers (parameters that are | |
| 114 // required to have a value if they occur, some parameters are auto-lower-cased, | |
| 115 // headers with an "anchor" parameter are rejected by base::LinkHeader). | |
| 116 // The code this tests purely parses without actually interpreting the data, as | |
| 117 // it is expected that another layer on top will do more specific validations. | |
| 118 const SimpleParseTestData simple_parse_tests[] = { | |
| 119 {"</images/cat.jpg>; rel=prefetch", true, "/images/cat.jpg", "prefetch", | |
| 120 ""}, | |
| 121 {"</images/cat.jpg>;rel=prefetch", true, "/images/cat.jpg", "prefetch", ""}, | |
| 122 {"</images/cat.jpg> ;rel=prefetch", true, "/images/cat.jpg", "prefetch", | |
| 123 ""}, | |
| 124 {"</images/cat.jpg> ; rel=prefetch", true, "/images/cat.jpg", | |
| 125 "prefetch", ""}, | |
| 126 {"< /images/cat.jpg> ; rel=prefetch", true, "/images/cat.jpg", | |
| 127 "prefetch", ""}, | |
| 128 {"</images/cat.jpg > ; rel=prefetch", true, "/images/cat.jpg", | |
| 129 "prefetch", ""}, | |
| 130 {"</images/cat.jpg wutwut> ; rel=prefetch", true, | |
| 131 "/images/cat.jpg wutwut", "prefetch", ""}, | |
| 132 {"</images/cat.jpg wutwut \t > ; rel=prefetch", true, | |
| 133 "/images/cat.jpg wutwut", "prefetch", ""}, | |
| 134 {"</images/cat.jpg>; rel=prefetch ", true, "/images/cat.jpg", "prefetch", | |
| 135 ""}, | |
| 136 {"</images/cat.jpg>; Rel=prefetch ", true, "/images/cat.jpg", "prefetch", | |
| 137 ""}, | |
| 138 {"</images/cat.jpg>; Rel=PReFetCh ", true, "/images/cat.jpg", "PReFetCh", | |
| 139 ""}, | |
| 140 {"</images/cat.jpg>; rel=prefetch; rel=somethingelse", true, | |
| 141 "/images/cat.jpg", "prefetch", ""}, | |
| 142 {"</images/cat.jpg>\t\t ; \trel=prefetch \t ", true, "/images/cat.jpg", | |
| 143 "prefetch", ""}, | |
| 144 {"</images/cat.jpg>; rel= prefetch", true, "/images/cat.jpg", "prefetch", | |
| 145 ""}, | |
| 146 {"<../images/cat.jpg?dog>; rel= prefetch", true, "../images/cat.jpg?dog", | |
| 147 "prefetch", ""}, | |
| 148 {"</images/cat.jpg>; rel =prefetch", true, "/images/cat.jpg", "prefetch", | |
| 149 ""}, | |
| 150 {"</images/cat.jpg>; rel pel=prefetch", false}, | |
| 151 {"< /images/cat.jpg>", true, "/images/cat.jpg", "", ""}, | |
| 152 {"</images/cat.jpg>; wut=sup; rel =prefetch", true, "/images/cat.jpg", | |
| 153 "prefetch", ""}, | |
| 154 {"</images/cat.jpg>; wut=sup ; rel =prefetch", true, "/images/cat.jpg", | |
| 155 "prefetch", ""}, | |
| 156 {"</images/cat.jpg>; wut=sup ; rel =prefetch \t ;", true, | |
| 157 "/images/cat.jpg", "prefetch", ""}, | |
| 158 {"</images/cat.jpg> wut=sup ; rel =prefetch \t ;", false}, | |
| 159 {"< /images/cat.jpg", false}, | |
| 160 {"< http://wut.com/ sdfsdf ?sd>; rel=dns-prefetch", true, | |
| 161 "http://wut.com/ sdfsdf ?sd", "dns-prefetch", ""}, | |
| 162 {"< http://wut.com/%20%20%3dsdfsdf?sd>; rel=dns-prefetch", true, | |
| 163 "http://wut.com/%20%20%3dsdfsdf?sd", "dns-prefetch", ""}, | |
| 164 {"< http://wut.com/dfsdf?sdf=ghj&wer=rty>; rel=prefetch", true, | |
| 165 "http://wut.com/dfsdf?sdf=ghj&wer=rty", "prefetch", ""}, | |
| 166 {"< http://wut.com/dfsdf?sdf=ghj&wer=rty>;;;;; rel=prefetch", true, | |
| 167 "http://wut.com/dfsdf?sdf=ghj&wer=rty", "prefetch", ""}, | |
| 168 {"< http://wut.com/%20%20%3dsdfsdf?sd>; rel=preload;as=image", true, | |
| 169 "http://wut.com/%20%20%3dsdfsdf?sd", "preload", "image"}, | |
| 170 {"< http://wut.com/%20%20%3dsdfsdf?sd>; rel=preload;as=whatever", true, | |
| 171 "http://wut.com/%20%20%3dsdfsdf?sd", "preload", "whatever"}, | |
| 172 {"</images/cat.jpg>; rel=prefetch;", true, "/images/cat.jpg", "prefetch", | |
| 173 ""}, | |
| 174 {"</images/cat.jpg>; rel=prefetch ;", true, "/images/cat.jpg", | |
| 175 "prefetch", ""}, | |
| 176 {"</images/ca,t.jpg>; rel=prefetch ;", true, "/images/ca,t.jpg", | |
| 177 "prefetch", ""}, | |
| 178 {"<simple.css>; rel=stylesheet; title=\"title with a DQUOTE and " | |
| 179 "backslash\"", | |
| 180 true, "simple.css", "stylesheet", ""}, | |
| 181 {"<simple.css>; rel=stylesheet; title=\"title with a DQUOTE \\\" and " | |
| 182 "backslash: \\\"", | |
| 183 false}, | |
| 184 {"<simple.css>; title=\"title with a DQUOTE \\\" and backslash: \"; " | |
| 185 "rel=stylesheet; ", | |
| 186 true, "simple.css", "stylesheet", ""}, | |
| 187 {"<simple.css>; title=\'title with a DQUOTE \\\' and backslash: \'; " | |
| 188 "rel=stylesheet; ", | |
| 189 true, "simple.css", "stylesheet", ""}, | |
| 190 {"<simple.css>; title=\"title with a DQUOTE \\\" and ;backslash,: \"; " | |
| 191 "rel=stylesheet; ", | |
| 192 true, "simple.css", "stylesheet", ""}, | |
| 193 {"<simple.css>; title=\"title with a DQUOTE \' and ;backslash,: \"; " | |
| 194 "rel=stylesheet; ", | |
| 195 true, "simple.css", "stylesheet", ""}, | |
| 196 {"<simple.css>; title=\"\"; rel=stylesheet; ", true, "simple.css", | |
| 197 "stylesheet", ""}, | |
| 198 {"<simple.css>; title=\"\"; rel=\"stylesheet\"; ", true, "simple.css", | |
| 199 "stylesheet", ""}, | |
| 200 {"<simple.css>; rel=stylesheet; title=\"", false}, | |
| 201 {"<simple.css>; rel=stylesheet; title=\"\"", true, "simple.css", | |
| 202 "stylesheet", ""}, | |
| 203 {"<simple.css>; rel=\"stylesheet\"; title=\"", false}, | |
| 204 {"<simple.css>; rel=\";style,sheet\"; title=\"", false}, | |
| 205 {"<simple.css>; rel=\"bla'sdf\"; title=\"", false}, | |
| 206 {"<simple.css>; rel=\"\"; title=\"\"", true, "simple.css", "", ""}, | |
| 207 {"<simple.css>; rel=''; title=\"\"", true, "simple.css", "", ""}, | |
| 208 {"<simple.css>; rel=''; bla", true, "simple.css", "", ""}, | |
| 209 {"<simple.css>; rel='prefetch", false}, | |
| 210 {"<simple.css>; rel=\"prefetch", false}, | |
| 211 {"<simple.css>; rel=\"", false}, | |
| 212 {"simple.css; rel=prefetch", false}, | |
| 213 {"<simple.css>; rel=prefetch; rel=foobar", true, "simple.css", "prefetch", | |
| 214 ""}, | |
| 215 }; | |
| 216 | |
| 217 INSTANTIATE_TEST_CASE_P(LinkHeaderTest, | |
| 218 SimpleParseTest, | |
| 219 testing::ValuesIn(simple_parse_tests)); | |
| 220 | |
| 221 } // namespace | |
| 222 | |
| 223 } // namespace link_header_util | |
| OLD | NEW |