summaryrefslogtreecommitdiffstats
path: root/net/cookies/cookie_monster_unittest.cc
diff options
context:
space:
mode:
authorbattre@chromium.org <battre@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-07-12 12:21:41 +0000
committerbattre@chromium.org <battre@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-07-12 12:21:41 +0000
commitebfe3173617e62ccbda071f8ca367a50a5ba4ee0 (patch)
treedc4d1b827721341b8329859e1c2242370c31ea29 /net/cookies/cookie_monster_unittest.cc
parent5a76f9b471e5ef367280fb92662bdef9f83cfd57 (diff)
downloadchromium_src-ebfe3173617e62ccbda071f8ca367a50a5ba4ee0.zip
chromium_src-ebfe3173617e62ccbda071f8ca367a50a5ba4ee0.tar.gz
chromium_src-ebfe3173617e62ccbda071f8ca367a50a5ba4ee0.tar.bz2
Extract ParsedCookie into a top level class
BUG=137014,112155 TEST=no TBR=sky@chromium.org, rdsmith@chromium.org, jochen@chromium.org Review URL: https://chromiumcodereview.appspot.com/10689158 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@146324 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/cookies/cookie_monster_unittest.cc')
-rw-r--r--net/cookies/cookie_monster_unittest.cc279
1 files changed, 2 insertions, 277 deletions
diff --git a/net/cookies/cookie_monster_unittest.cc b/net/cookies/cookie_monster_unittest.cc
index 4f76439..c69c805 100644
--- a/net/cookies/cookie_monster_unittest.cc
+++ b/net/cookies/cookie_monster_unittest.cc
@@ -21,6 +21,7 @@
#include "net/cookies/cookie_monster.h"
#include "net/cookies/cookie_monster_store_test.h" // For CookieStore mock
#include "net/cookies/cookie_util.h"
+#include "net/cookies/parsed_cookie.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -75,281 +76,6 @@ class GetCookieListCallback : public CookieCallback {
CookieList cookies_;
};
-class ParsedCookieTest : public testing::Test { };
-
-} // namespace
-
-TEST(ParsedCookieTest, TestBasic) {
- CookieMonster::ParsedCookie pc("a=b");
- EXPECT_TRUE(pc.IsValid());
- EXPECT_FALSE(pc.IsSecure());
- EXPECT_EQ("a", pc.Name());
- EXPECT_EQ("b", pc.Value());
-}
-
-TEST(ParsedCookieTest, TestQuoted) {
- // These are some quoting cases which the major browsers all
- // handle differently. I've tested Internet Explorer 6, Opera 9.6,
- // Firefox 3, and Safari Windows 3.2.1. We originally tried to match
- // Firefox closely, however we now match Internet Explorer and Safari.
- const char* values[] = {
- // Trailing whitespace after a quoted value. The whitespace after
- // the quote is stripped in all browsers.
- "\"zzz \" ", "\"zzz \"",
- // Handling a quoted value with a ';', like FOO="zz;pp" ;
- // IE and Safari: "zz;
- // Firefox and Opera: "zz;pp"
- "\"zz;pp\" ;", "\"zz",
- // Handling a value with multiple quoted parts, like FOO="zzz " "ppp" ;
- // IE and Safari: "zzz " "ppp";
- // Firefox: "zzz ";
- // Opera: <rejects cookie>
- "\"zzz \" \"ppp\" ", "\"zzz \" \"ppp\"",
- // A quote in a value that didn't start quoted. like FOO=A"B ;
- // IE, Safari, and Firefox: A"B;
- // Opera: <rejects cookie>
- "A\"B", "A\"B",
- };
-
- for (size_t i = 0; i < arraysize(values); i += 2) {
- std::string input(values[i]);
- std::string expected(values[i + 1]);
-
- CookieMonster::ParsedCookie pc(
- "aBc=" + input + " ; path=\"/\" ; httponly ");
- EXPECT_TRUE(pc.IsValid());
- EXPECT_FALSE(pc.IsSecure());
- EXPECT_TRUE(pc.IsHttpOnly());
- EXPECT_TRUE(pc.HasPath());
- EXPECT_EQ("aBc", pc.Name());
- EXPECT_EQ(expected, pc.Value());
-
- // If a path was quoted, the path attribute keeps the quotes. This will
- // make the cookie effectively useless, but path parameters aren't supposed
- // to be quoted. Bug 1261605.
- EXPECT_EQ("\"/\"", pc.Path());
- }
-}
-
-TEST(ParsedCookieTest, TestNameless) {
- CookieMonster::ParsedCookie pc("BLAHHH; path=/; secure;");
- EXPECT_TRUE(pc.IsValid());
- EXPECT_TRUE(pc.IsSecure());
- EXPECT_TRUE(pc.HasPath());
- EXPECT_EQ("/", pc.Path());
- EXPECT_EQ("", pc.Name());
- EXPECT_EQ("BLAHHH", pc.Value());
-}
-
-TEST(ParsedCookieTest, TestAttributeCase) {
- CookieMonster::ParsedCookie pc("BLAHHH; Path=/; sECuRe; httpONLY");
- EXPECT_TRUE(pc.IsValid());
- EXPECT_TRUE(pc.IsSecure());
- EXPECT_TRUE(pc.IsHttpOnly());
- EXPECT_TRUE(pc.HasPath());
- EXPECT_EQ("/", pc.Path());
- EXPECT_EQ("", pc.Name());
- EXPECT_EQ("BLAHHH", pc.Value());
- EXPECT_EQ(3U, pc.NumberOfAttributes());
-}
-
-TEST(ParsedCookieTest, TestDoubleQuotedNameless) {
- CookieMonster::ParsedCookie pc("\"BLA\\\"HHH\"; path=/; secure;");
- EXPECT_TRUE(pc.IsValid());
- EXPECT_TRUE(pc.IsSecure());
- EXPECT_TRUE(pc.HasPath());
- EXPECT_EQ("/", pc.Path());
- EXPECT_EQ("", pc.Name());
- EXPECT_EQ("\"BLA\\\"HHH\"", pc.Value());
- EXPECT_EQ(2U, pc.NumberOfAttributes());
-}
-
-TEST(ParsedCookieTest, QuoteOffTheEnd) {
- CookieMonster::ParsedCookie pc("a=\"B");
- EXPECT_TRUE(pc.IsValid());
- EXPECT_EQ("a", pc.Name());
- EXPECT_EQ("\"B", pc.Value());
- EXPECT_EQ(0U, pc.NumberOfAttributes());
-}
-
-TEST(ParsedCookieTest, MissingName) {
- CookieMonster::ParsedCookie pc("=ABC");
- EXPECT_TRUE(pc.IsValid());
- EXPECT_EQ("", pc.Name());
- EXPECT_EQ("ABC", pc.Value());
- EXPECT_EQ(0U, pc.NumberOfAttributes());
-}
-
-TEST(ParsedCookieTest, MissingValue) {
- CookieMonster::ParsedCookie pc("ABC=; path = /wee");
- EXPECT_TRUE(pc.IsValid());
- EXPECT_EQ("ABC", pc.Name());
- EXPECT_EQ("", pc.Value());
- EXPECT_TRUE(pc.HasPath());
- EXPECT_EQ("/wee", pc.Path());
- EXPECT_EQ(1U, pc.NumberOfAttributes());
-}
-
-TEST(ParsedCookieTest, Whitespace) {
- CookieMonster::ParsedCookie pc(" A = BC ;secure;;; httponly");
- EXPECT_TRUE(pc.IsValid());
- EXPECT_EQ("A", pc.Name());
- EXPECT_EQ("BC", pc.Value());
- EXPECT_FALSE(pc.HasPath());
- EXPECT_FALSE(pc.HasDomain());
- EXPECT_TRUE(pc.IsSecure());
- EXPECT_TRUE(pc.IsHttpOnly());
- // We parse anything between ; as attributes, so we end up with two
- // attributes with an empty string name and value.
- EXPECT_EQ(4U, pc.NumberOfAttributes());
-}
-TEST(ParsedCookieTest, MultipleEquals) {
- CookieMonster::ParsedCookie pc(" A=== BC ;secure;;; httponly");
- EXPECT_TRUE(pc.IsValid());
- EXPECT_EQ("A", pc.Name());
- EXPECT_EQ("== BC", pc.Value());
- EXPECT_FALSE(pc.HasPath());
- EXPECT_FALSE(pc.HasDomain());
- EXPECT_TRUE(pc.IsSecure());
- EXPECT_TRUE(pc.IsHttpOnly());
- EXPECT_EQ(4U, pc.NumberOfAttributes());
-}
-
-TEST(ParsedCookieTest, MACKey) {
- CookieMonster::ParsedCookie pc("foo=bar; MAC-Key=3900ac9anw9incvw9f");
- EXPECT_TRUE(pc.IsValid());
- EXPECT_EQ("foo", pc.Name());
- EXPECT_EQ("bar", pc.Value());
- EXPECT_EQ("3900ac9anw9incvw9f", pc.MACKey());
- EXPECT_EQ(1U, pc.NumberOfAttributes());
-}
-
-TEST(ParsedCookieTest, MACAlgorithm) {
- CookieMonster::ParsedCookie pc("foo=bar; MAC-Algorithm=hmac-sha-1");
- EXPECT_TRUE(pc.IsValid());
- EXPECT_EQ("foo", pc.Name());
- EXPECT_EQ("bar", pc.Value());
- EXPECT_EQ("hmac-sha-1", pc.MACAlgorithm());
- EXPECT_EQ(1U, pc.NumberOfAttributes());
-}
-
-TEST(ParsedCookieTest, MACKeyAndMACAlgorithm) {
- CookieMonster::ParsedCookie pc(
- "foo=bar; MAC-Key=voiae-09fj0302nfqf; MAC-Algorithm=hmac-sha-256");
- EXPECT_TRUE(pc.IsValid());
- EXPECT_EQ("foo", pc.Name());
- EXPECT_EQ("bar", pc.Value());
- EXPECT_EQ("voiae-09fj0302nfqf", pc.MACKey());
- EXPECT_EQ("hmac-sha-256", pc.MACAlgorithm());
- EXPECT_EQ(2U, pc.NumberOfAttributes());
-}
-
-TEST(ParsedCookieTest, QuotedTrailingWhitespace) {
- CookieMonster::ParsedCookie pc("ANCUUID=\"zohNumRKgI0oxyhSsV3Z7D\" ; "
- "expires=Sun, 18-Apr-2027 21:06:29 GMT ; "
- "path=/ ; ");
- EXPECT_TRUE(pc.IsValid());
- EXPECT_EQ("ANCUUID", pc.Name());
- // Stripping whitespace after the quotes matches all other major browsers.
- EXPECT_EQ("\"zohNumRKgI0oxyhSsV3Z7D\"", pc.Value());
- EXPECT_TRUE(pc.HasExpires());
- EXPECT_TRUE(pc.HasPath());
- EXPECT_EQ("/", pc.Path());
- EXPECT_EQ(2U, pc.NumberOfAttributes());
-}
-
-TEST(ParsedCookieTest, TrailingWhitespace) {
- CookieMonster::ParsedCookie pc("ANCUUID=zohNumRKgI0oxyhSsV3Z7D ; "
- "expires=Sun, 18-Apr-2027 21:06:29 GMT ; "
- "path=/ ; ");
- EXPECT_TRUE(pc.IsValid());
- EXPECT_EQ("ANCUUID", pc.Name());
- EXPECT_EQ("zohNumRKgI0oxyhSsV3Z7D", pc.Value());
- EXPECT_TRUE(pc.HasExpires());
- EXPECT_TRUE(pc.HasPath());
- EXPECT_EQ("/", pc.Path());
- EXPECT_EQ(2U, pc.NumberOfAttributes());
-}
-
-TEST(ParsedCookieTest, TooManyPairs) {
- std::string blankpairs;
- blankpairs.resize(CookieMonster::ParsedCookie::kMaxPairs - 1, ';');
-
- CookieMonster::ParsedCookie pc1(blankpairs + "secure");
- EXPECT_TRUE(pc1.IsValid());
- EXPECT_TRUE(pc1.IsSecure());
-
- CookieMonster::ParsedCookie pc2(blankpairs + ";secure");
- EXPECT_TRUE(pc2.IsValid());
- EXPECT_FALSE(pc2.IsSecure());
-}
-
-// TODO(erikwright): some better test cases for invalid cookies.
-TEST(ParsedCookieTest, InvalidWhitespace) {
- CookieMonster::ParsedCookie pc(" ");
- EXPECT_FALSE(pc.IsValid());
-}
-
-TEST(ParsedCookieTest, InvalidTooLong) {
- std::string maxstr;
- maxstr.resize(CookieMonster::ParsedCookie::kMaxCookieSize, 'a');
-
- CookieMonster::ParsedCookie pc1(maxstr);
- EXPECT_TRUE(pc1.IsValid());
-
- CookieMonster::ParsedCookie pc2(maxstr + "A");
- EXPECT_FALSE(pc2.IsValid());
-}
-
-TEST(ParsedCookieTest, InvalidEmpty) {
- CookieMonster::ParsedCookie pc("");
- EXPECT_FALSE(pc.IsValid());
-}
-
-TEST(ParsedCookieTest, EmbeddedTerminator) {
- CookieMonster::ParsedCookie pc1("AAA=BB\0ZYX");
- CookieMonster::ParsedCookie pc2("AAA=BB\rZYX");
- CookieMonster::ParsedCookie pc3("AAA=BB\nZYX");
- EXPECT_TRUE(pc1.IsValid());
- EXPECT_EQ("AAA", pc1.Name());
- EXPECT_EQ("BB", pc1.Value());
- EXPECT_TRUE(pc2.IsValid());
- EXPECT_EQ("AAA", pc2.Name());
- EXPECT_EQ("BB", pc2.Value());
- EXPECT_TRUE(pc3.IsValid());
- EXPECT_EQ("AAA", pc3.Name());
- EXPECT_EQ("BB", pc3.Value());
-}
-
-TEST(ParsedCookieTest, ParseTokensAndValues) {
- EXPECT_EQ("hello",
- CookieMonster::ParsedCookie::ParseTokenString(
- "hello\nworld"));
- EXPECT_EQ("fs!!@",
- CookieMonster::ParsedCookie::ParseTokenString(
- "fs!!@;helloworld"));
- EXPECT_EQ("hello world\tgood",
- CookieMonster::ParsedCookie::ParseTokenString(
- "hello world\tgood\rbye"));
- EXPECT_EQ("A",
- CookieMonster::ParsedCookie::ParseTokenString(
- "A=B=C;D=E"));
- EXPECT_EQ("hello",
- CookieMonster::ParsedCookie::ParseValueString(
- "hello\nworld"));
- EXPECT_EQ("fs!!@",
- CookieMonster::ParsedCookie::ParseValueString(
- "fs!!@;helloworld"));
- EXPECT_EQ("hello world\tgood",
- CookieMonster::ParsedCookie::ParseValueString(
- "hello world\tgood\rbye"));
- EXPECT_EQ("A=B=C",
- CookieMonster::ParsedCookie::ParseValueString(
- "A=B=C;D=E"));
-}
-
-namespace {
-
struct CookieMonsterTestTraits {
static scoped_refptr<CookieStore> Create() {
return new CookieMonster(NULL, NULL);
@@ -2574,8 +2300,7 @@ TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckDeleteCanonicalCookie) {
}
TEST_F(CookieMonsterTest, InvalidExpiryTime) {
- CookieMonster::ParsedCookie pc(
- std::string(kValidCookieLine) + "; expires=Blarg arg arg");
+ ParsedCookie pc(std::string(kValidCookieLine) + "; expires=Blarg arg arg");
scoped_ptr<CookieMonster::CanonicalCookie> cookie(
CookieMonster::CanonicalCookie::Create(url_google_, pc));