| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | |
| 6 #include <utility> | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 10 #include "net/cookies/cookie_util.h" | 6 #include "net/cookies/cookie_util.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 8 |
| 13 namespace { | |
| 14 | |
| 15 struct RequestCookieParsingTest { | |
| 16 std::string str; | |
| 17 std::vector<std::pair<std::string, std::string> > parsed; | |
| 18 }; | |
| 19 | |
| 20 net::cookie_util::ParsedRequestCookies MakeParsedRequestCookies( | |
| 21 const std::vector<std::pair<std::string, std::string> >& data) { | |
| 22 net::cookie_util::ParsedRequestCookies parsed; | |
| 23 for (size_t i = 0; i < data.size(); i++) { | |
| 24 parsed.push_back(std::make_pair(base::StringPiece(data[i].first), | |
| 25 base::StringPiece(data[i].second))); | |
| 26 } | |
| 27 return parsed; | |
| 28 } | |
| 29 | |
| 30 void CheckParse( | |
| 31 const std::string& str, | |
| 32 const std::vector<std::pair<std::string, std::string> >& parsed_expected) { | |
| 33 net::cookie_util::ParsedRequestCookies parsed; | |
| 34 net::cookie_util::ParseRequestCookieLine(str, &parsed); | |
| 35 EXPECT_EQ(MakeParsedRequestCookies(parsed_expected), parsed); | |
| 36 } | |
| 37 | |
| 38 void CheckSerialize( | |
| 39 const std::vector<std::pair<std::string, std::string> >& parsed, | |
| 40 const std::string& str_expected) { | |
| 41 net::cookie_util::ParsedRequestCookies prc = | |
| 42 MakeParsedRequestCookies(parsed); | |
| 43 EXPECT_EQ(str_expected, net::cookie_util::SerializeRequestCookieLine(prc)); | |
| 44 } | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 TEST(CookieUtilTest, TestDomainIsHostOnly) { | 9 TEST(CookieUtilTest, TestDomainIsHostOnly) { |
| 49 const struct { | 10 const struct { |
| 50 const char* str; | 11 const char* str; |
| 51 const bool is_host_only; | 12 const bool is_host_only; |
| 52 } tests[] = { | 13 } tests[] = { |
| 53 { "", true }, | 14 { "", true }, |
| 54 { "www.google.com", true }, | 15 { "www.google.com", true }, |
| 55 { ".google.com", false } | 16 { ".google.com", false } |
| 56 }; | 17 }; |
| 57 | 18 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 102 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 142 parsed_time = net::cookie_util::ParseCookieTime(tests[i].str); | 103 parsed_time = net::cookie_util::ParseCookieTime(tests[i].str); |
| 143 if (!tests[i].valid) { | 104 if (!tests[i].valid) { |
| 144 EXPECT_FALSE(!parsed_time.is_null()) << tests[i].str; | 105 EXPECT_FALSE(!parsed_time.is_null()) << tests[i].str; |
| 145 continue; | 106 continue; |
| 146 } | 107 } |
| 147 EXPECT_TRUE(!parsed_time.is_null()) << tests[i].str; | 108 EXPECT_TRUE(!parsed_time.is_null()) << tests[i].str; |
| 148 EXPECT_EQ(tests[i].epoch, parsed_time.ToTimeT()) << tests[i].str; | 109 EXPECT_EQ(tests[i].epoch, parsed_time.ToTimeT()) << tests[i].str; |
| 149 } | 110 } |
| 150 } | 111 } |
| 151 | |
| 152 TEST(CookieUtilTest, TestRequestCookieParsing) { | |
| 153 std::vector<RequestCookieParsingTest> tests; | |
| 154 | |
| 155 // Simple case. | |
| 156 tests.push_back(RequestCookieParsingTest()); | |
| 157 tests.back().str = "key=value"; | |
| 158 tests.back().parsed.push_back(std::make_pair(std::string("key"), | |
| 159 std::string("value"))); | |
| 160 // Multiple key/value pairs. | |
| 161 tests.push_back(RequestCookieParsingTest()); | |
| 162 tests.back().str = "key1=value1; key2=value2"; | |
| 163 tests.back().parsed.push_back(std::make_pair(std::string("key1"), | |
| 164 std::string("value1"))); | |
| 165 tests.back().parsed.push_back(std::make_pair(std::string("key2"), | |
| 166 std::string("value2"))); | |
| 167 // Empty value. | |
| 168 tests.push_back(RequestCookieParsingTest()); | |
| 169 tests.back().str = "key=; otherkey=1234"; | |
| 170 tests.back().parsed.push_back(std::make_pair(std::string("key"), | |
| 171 std::string())); | |
| 172 tests.back().parsed.push_back(std::make_pair(std::string("otherkey"), | |
| 173 std::string("1234"))); | |
| 174 // Special characters (including equals signs) in value. | |
| 175 tests.push_back(RequestCookieParsingTest()); | |
| 176 tests.back().str = "key=; a2=s=(./&t=:&u=a#$; a3=+~"; | |
| 177 tests.back().parsed.push_back(std::make_pair(std::string("key"), | |
| 178 std::string())); | |
| 179 tests.back().parsed.push_back(std::make_pair(std::string("a2"), | |
| 180 std::string("s=(./&t=:&u=a#$"))); | |
| 181 tests.back().parsed.push_back(std::make_pair(std::string("a3"), | |
| 182 std::string("+~"))); | |
| 183 // Quoted value. | |
| 184 tests.push_back(RequestCookieParsingTest()); | |
| 185 tests.back().str = "key=\"abcdef\"; otherkey=1234"; | |
| 186 tests.back().parsed.push_back(std::make_pair(std::string("key"), | |
| 187 std::string("\"abcdef\""))); | |
| 188 tests.back().parsed.push_back(std::make_pair(std::string("otherkey"), | |
| 189 std::string("1234"))); | |
| 190 | |
| 191 for (size_t i = 0; i < tests.size(); i++) { | |
| 192 SCOPED_TRACE(testing::Message() << "Test " << i); | |
| 193 CheckParse(tests[i].str, tests[i].parsed); | |
| 194 CheckSerialize(tests[i].parsed, tests[i].str); | |
| 195 } | |
| 196 } | |
| OLD | NEW |