OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <time.h> | 5 #include <time.h> |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/platform_thread.h" | 10 #include "base/platform_thread.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 | 25 |
26 TEST(ParsedCookieTest, TestBasic) { | 26 TEST(ParsedCookieTest, TestBasic) { |
27 net::CookieMonster::ParsedCookie pc("a=b"); | 27 net::CookieMonster::ParsedCookie pc("a=b"); |
28 EXPECT_TRUE(pc.IsValid()); | 28 EXPECT_TRUE(pc.IsValid()); |
29 EXPECT_FALSE(pc.IsSecure()); | 29 EXPECT_FALSE(pc.IsSecure()); |
30 EXPECT_EQ("a", pc.Name()); | 30 EXPECT_EQ("a", pc.Name()); |
31 EXPECT_EQ("b", pc.Value()); | 31 EXPECT_EQ("b", pc.Value()); |
32 } | 32 } |
33 | 33 |
34 TEST(ParsedCookieTest, TestQuoted) { | 34 TEST(ParsedCookieTest, TestQuoted) { |
35 net::CookieMonster::ParsedCookie pc("a=\"b=;\"; path=\"/\""); | 35 // These are some quoting cases which the major browsers all |
36 EXPECT_TRUE(pc.IsValid()); | 36 // handle differently. I've tested Internet Explorer 6, Opera 9.6, |
37 EXPECT_FALSE(pc.IsSecure()); | 37 // Firefox 3, and Safari Windows 3.2.1. We originally tried to match |
38 EXPECT_TRUE(pc.HasPath()); | 38 // Firefox closely, however we now match Internet Explorer and Safari. |
39 EXPECT_EQ("a", pc.Name()); | 39 const char* values[] = { |
40 EXPECT_EQ("\"b=;\"", pc.Value()); | 40 // Trailing whitespace after a quoted value. The whitespace after |
41 // If a path was quoted, the path attribute keeps the quotes. This will | 41 // the quote is stripped in all browsers. |
42 // make the cookie effectively useless, but path parameters aren't supposed | 42 "\"zzz \" ", "\"zzz \"", |
43 // to be quoted. Bug 1261605. | 43 // Handling a quoted value with a ';', like FOO="zz;pp" ; |
44 EXPECT_EQ("\"/\"", pc.Path()); | 44 // IE and Safari: "zz; |
| 45 // Firefox and Opera: "zz;pp" |
| 46 "\"zz;pp\" ;", "\"zz", |
| 47 // Handling a value with multiple quoted parts, like FOO="zzz " "ppp" ; |
| 48 // IE and Safari: "zzz " "ppp"; |
| 49 // Firefox: "zzz "; |
| 50 // Opera: <rejects cookie> |
| 51 "\"zzz \" \"ppp\" ", "\"zzz \" \"ppp\"", |
| 52 // A quote in a value that didn't start quoted. like FOO=A"B ; |
| 53 // IE, Safari, and Firefox: A"B; |
| 54 // Opera: <rejects cookie> |
| 55 "A\"B", "A\"B", |
| 56 }; |
| 57 |
| 58 for (size_t i = 0; i < arraysize(values); i += 2) { |
| 59 std::string input(values[i]); |
| 60 std::string expected(values[i + 1]); |
| 61 |
| 62 net::CookieMonster::ParsedCookie pc( |
| 63 "aBc=" + input + " ; path=\"/\" ; httponly "); |
| 64 EXPECT_TRUE(pc.IsValid()); |
| 65 EXPECT_FALSE(pc.IsSecure()); |
| 66 EXPECT_TRUE(pc.IsHttpOnly()); |
| 67 EXPECT_TRUE(pc.HasPath()); |
| 68 EXPECT_EQ("aBc", pc.Name()); |
| 69 EXPECT_EQ(expected, pc.Value()); |
| 70 |
| 71 // If a path was quoted, the path attribute keeps the quotes. This will |
| 72 // make the cookie effectively useless, but path parameters aren't supposed |
| 73 // to be quoted. Bug 1261605. |
| 74 EXPECT_EQ("\"/\"", pc.Path()); |
| 75 } |
45 } | 76 } |
46 | 77 |
47 TEST(ParsedCookieTest, TestNameless) { | 78 TEST(ParsedCookieTest, TestNameless) { |
48 net::CookieMonster::ParsedCookie pc("BLAHHH; path=/; secure;"); | 79 net::CookieMonster::ParsedCookie pc("BLAHHH; path=/; secure;"); |
49 EXPECT_TRUE(pc.IsValid()); | 80 EXPECT_TRUE(pc.IsValid()); |
50 EXPECT_TRUE(pc.IsSecure()); | 81 EXPECT_TRUE(pc.IsSecure()); |
51 EXPECT_TRUE(pc.HasPath()); | 82 EXPECT_TRUE(pc.HasPath()); |
52 EXPECT_EQ("/", pc.Path()); | 83 EXPECT_EQ("/", pc.Path()); |
53 EXPECT_EQ("", pc.Name()); | 84 EXPECT_EQ("", pc.Name()); |
54 EXPECT_EQ("BLAHHH", pc.Value()); | 85 EXPECT_EQ("BLAHHH", pc.Value()); |
55 } | 86 } |
56 | 87 |
57 TEST(ParsedCookieTest, TestAttributeCase) { | 88 TEST(ParsedCookieTest, TestAttributeCase) { |
58 net::CookieMonster::ParsedCookie pc("BLAHHH; Path=/; sECuRe; httpONLY"); | 89 net::CookieMonster::ParsedCookie pc("BLAHHH; Path=/; sECuRe; httpONLY"); |
59 EXPECT_TRUE(pc.IsValid()); | 90 EXPECT_TRUE(pc.IsValid()); |
60 EXPECT_TRUE(pc.IsSecure()); | 91 EXPECT_TRUE(pc.IsSecure()); |
61 EXPECT_TRUE(pc.IsHttpOnly()); | 92 EXPECT_TRUE(pc.IsHttpOnly()); |
62 EXPECT_TRUE(pc.HasPath()); | 93 EXPECT_TRUE(pc.HasPath()); |
63 EXPECT_EQ("/", pc.Path()); | 94 EXPECT_EQ("/", pc.Path()); |
64 EXPECT_EQ("", pc.Name()); | 95 EXPECT_EQ("", pc.Name()); |
65 EXPECT_EQ("BLAHHH", pc.Value()); | 96 EXPECT_EQ("BLAHHH", pc.Value()); |
| 97 EXPECT_EQ(3U, pc.NumberOfAttributes()); |
66 } | 98 } |
67 | 99 |
68 TEST(ParsedCookieTest, TestDoubleQuotedNameless) { | 100 TEST(ParsedCookieTest, TestDoubleQuotedNameless) { |
69 net::CookieMonster::ParsedCookie pc("\"BLA\\\"HHH\"; path=/; secure;"); | 101 net::CookieMonster::ParsedCookie pc("\"BLA\\\"HHH\"; path=/; secure;"); |
70 EXPECT_TRUE(pc.IsValid()); | 102 EXPECT_TRUE(pc.IsValid()); |
71 EXPECT_TRUE(pc.IsSecure()); | 103 EXPECT_TRUE(pc.IsSecure()); |
72 EXPECT_TRUE(pc.HasPath()); | 104 EXPECT_TRUE(pc.HasPath()); |
73 EXPECT_EQ("/", pc.Path()); | 105 EXPECT_EQ("/", pc.Path()); |
74 EXPECT_EQ("", pc.Name()); | 106 EXPECT_EQ("", pc.Name()); |
75 EXPECT_EQ("\"BLA\\\"HHH\"", pc.Value()); | 107 EXPECT_EQ("\"BLA\\\"HHH\"", pc.Value()); |
| 108 EXPECT_EQ(2U, pc.NumberOfAttributes()); |
76 } | 109 } |
77 | 110 |
78 TEST(ParsedCookieTest, QuoteOffTheEnd) { | 111 TEST(ParsedCookieTest, QuoteOffTheEnd) { |
79 net::CookieMonster::ParsedCookie pc("a=\"B"); | 112 net::CookieMonster::ParsedCookie pc("a=\"B"); |
80 EXPECT_TRUE(pc.IsValid()); | 113 EXPECT_TRUE(pc.IsValid()); |
81 EXPECT_EQ("a", pc.Name()); | 114 EXPECT_EQ("a", pc.Name()); |
82 EXPECT_EQ("\"B", pc.Value()); | 115 EXPECT_EQ("\"B", pc.Value()); |
| 116 EXPECT_EQ(0U, pc.NumberOfAttributes()); |
83 } | 117 } |
84 | 118 |
85 TEST(ParsedCookieTest, MissingName) { | 119 TEST(ParsedCookieTest, MissingName) { |
86 net::CookieMonster::ParsedCookie pc("=ABC"); | 120 net::CookieMonster::ParsedCookie pc("=ABC"); |
87 EXPECT_TRUE(pc.IsValid()); | 121 EXPECT_TRUE(pc.IsValid()); |
88 EXPECT_EQ("", pc.Name()); | 122 EXPECT_EQ("", pc.Name()); |
89 EXPECT_EQ("ABC", pc.Value()); | 123 EXPECT_EQ("ABC", pc.Value()); |
| 124 EXPECT_EQ(0U, pc.NumberOfAttributes()); |
90 } | 125 } |
91 | 126 |
92 TEST(ParsedCookieTest, MissingValue) { | 127 TEST(ParsedCookieTest, MissingValue) { |
93 net::CookieMonster::ParsedCookie pc("ABC=; path = /wee"); | 128 net::CookieMonster::ParsedCookie pc("ABC=; path = /wee"); |
94 EXPECT_TRUE(pc.IsValid()); | 129 EXPECT_TRUE(pc.IsValid()); |
95 EXPECT_EQ("ABC", pc.Name()); | 130 EXPECT_EQ("ABC", pc.Name()); |
96 EXPECT_EQ("", pc.Value()); | 131 EXPECT_EQ("", pc.Value()); |
97 EXPECT_TRUE(pc.HasPath()); | 132 EXPECT_TRUE(pc.HasPath()); |
98 EXPECT_EQ("/wee", pc.Path()); | 133 EXPECT_EQ("/wee", pc.Path()); |
| 134 EXPECT_EQ(1U, pc.NumberOfAttributes()); |
99 } | 135 } |
100 | 136 |
101 TEST(ParsedCookieTest, Whitespace) { | 137 TEST(ParsedCookieTest, Whitespace) { |
102 net::CookieMonster::ParsedCookie pc(" A = BC ;secure;;; httponly"); | 138 net::CookieMonster::ParsedCookie pc(" A = BC ;secure;;; httponly"); |
103 EXPECT_TRUE(pc.IsValid()); | 139 EXPECT_TRUE(pc.IsValid()); |
104 EXPECT_EQ("A", pc.Name()); | 140 EXPECT_EQ("A", pc.Name()); |
105 EXPECT_EQ("BC", pc.Value()); | 141 EXPECT_EQ("BC", pc.Value()); |
106 EXPECT_FALSE(pc.HasPath()); | 142 EXPECT_FALSE(pc.HasPath()); |
107 EXPECT_FALSE(pc.HasDomain()); | 143 EXPECT_FALSE(pc.HasDomain()); |
108 EXPECT_TRUE(pc.IsSecure()); | 144 EXPECT_TRUE(pc.IsSecure()); |
109 EXPECT_TRUE(pc.IsHttpOnly()); | 145 EXPECT_TRUE(pc.IsHttpOnly()); |
| 146 // We parse anything between ; as attributes, so we end up with two |
| 147 // attributes with an empty string name and value. |
| 148 EXPECT_EQ(4U, pc.NumberOfAttributes()); |
110 } | 149 } |
111 TEST(ParsedCookieTest, MultipleEquals) { | 150 TEST(ParsedCookieTest, MultipleEquals) { |
112 net::CookieMonster::ParsedCookie pc(" A=== BC ;secure;;; httponly"); | 151 net::CookieMonster::ParsedCookie pc(" A=== BC ;secure;;; httponly"); |
113 EXPECT_TRUE(pc.IsValid()); | 152 EXPECT_TRUE(pc.IsValid()); |
114 EXPECT_EQ("A", pc.Name()); | 153 EXPECT_EQ("A", pc.Name()); |
115 EXPECT_EQ("== BC", pc.Value()); | 154 EXPECT_EQ("== BC", pc.Value()); |
116 EXPECT_FALSE(pc.HasPath()); | 155 EXPECT_FALSE(pc.HasPath()); |
117 EXPECT_FALSE(pc.HasDomain()); | 156 EXPECT_FALSE(pc.HasDomain()); |
118 EXPECT_TRUE(pc.IsSecure()); | 157 EXPECT_TRUE(pc.IsSecure()); |
119 EXPECT_TRUE(pc.IsHttpOnly()); | 158 EXPECT_TRUE(pc.IsHttpOnly()); |
| 159 EXPECT_EQ(4U, pc.NumberOfAttributes()); |
| 160 } |
| 161 |
| 162 TEST(ParsedCookieTest, QuotedTrailingWhitespace) { |
| 163 net::CookieMonster::ParsedCookie pc("ANCUUID=\"zohNumRKgI0oxyhSsV3Z7D\" ; " |
| 164 "expires=Sun, 18-Apr-2027 21:06:29 GMT ; " |
| 165 "path=/ ; "); |
| 166 EXPECT_TRUE(pc.IsValid()); |
| 167 EXPECT_EQ("ANCUUID", pc.Name()); |
| 168 // Stripping whitespace after the quotes matches all other major browsers. |
| 169 EXPECT_EQ("\"zohNumRKgI0oxyhSsV3Z7D\"", pc.Value()); |
| 170 EXPECT_TRUE(pc.HasExpires()); |
| 171 EXPECT_TRUE(pc.HasPath()); |
| 172 EXPECT_EQ("/", pc.Path()); |
| 173 EXPECT_EQ(2U, pc.NumberOfAttributes()); |
120 } | 174 } |
121 | 175 |
122 TEST(ParsedCookieTest, TrailingWhitespace) { | 176 TEST(ParsedCookieTest, TrailingWhitespace) { |
123 net::CookieMonster::ParsedCookie pc("ANCUUID=zohNumRKgI0oxyhSsV3Z7D; " | 177 net::CookieMonster::ParsedCookie pc("ANCUUID=zohNumRKgI0oxyhSsV3Z7D ; " |
124 "expires=Sun, 18-Apr-2027 21:06:29 GMT; " | 178 "expires=Sun, 18-Apr-2027 21:06:29 GMT ; " |
125 "path=/ ; "); | 179 "path=/ ; "); |
126 EXPECT_TRUE(pc.IsValid()); | 180 EXPECT_TRUE(pc.IsValid()); |
127 EXPECT_EQ("ANCUUID", pc.Name()); | 181 EXPECT_EQ("ANCUUID", pc.Name()); |
| 182 EXPECT_EQ("zohNumRKgI0oxyhSsV3Z7D", pc.Value()); |
128 EXPECT_TRUE(pc.HasExpires()); | 183 EXPECT_TRUE(pc.HasExpires()); |
129 EXPECT_TRUE(pc.HasPath()); | 184 EXPECT_TRUE(pc.HasPath()); |
130 EXPECT_EQ("/", pc.Path()); | 185 EXPECT_EQ("/", pc.Path()); |
131 // TODO should export like NumAttributes() and make sure that the | 186 EXPECT_EQ(2U, pc.NumberOfAttributes()); |
132 // trailing whitespace doesn't end up as an empty attribute or something. | |
133 } | 187 } |
134 | 188 |
135 TEST(ParsedCookieTest, TooManyPairs) { | 189 TEST(ParsedCookieTest, TooManyPairs) { |
136 std::string blankpairs; | 190 std::string blankpairs; |
137 blankpairs.resize(net::CookieMonster::ParsedCookie::kMaxPairs - 1, ';'); | 191 blankpairs.resize(net::CookieMonster::ParsedCookie::kMaxPairs - 1, ';'); |
138 | 192 |
139 net::CookieMonster::ParsedCookie pc1(blankpairs + "secure"); | 193 net::CookieMonster::ParsedCookie pc1(blankpairs + "secure"); |
140 EXPECT_TRUE(pc1.IsValid()); | 194 EXPECT_TRUE(pc1.IsValid()); |
141 EXPECT_TRUE(pc1.IsSecure()); | 195 EXPECT_TRUE(pc1.IsSecure()); |
142 | 196 |
(...skipping 720 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
863 | 917 |
864 EXPECT_TRUE(FindAndDeleteCookie(cm, url_google.host(), "C")); | 918 EXPECT_TRUE(FindAndDeleteCookie(cm, url_google.host(), "C")); |
865 EXPECT_EQ("A=B; E=F", cm.GetCookies(url_google)); | 919 EXPECT_EQ("A=B; E=F", cm.GetCookies(url_google)); |
866 | 920 |
867 EXPECT_FALSE(FindAndDeleteCookie(cm, "random.host", "E")); | 921 EXPECT_FALSE(FindAndDeleteCookie(cm, "random.host", "E")); |
868 EXPECT_EQ("A=B; E=F", cm.GetCookies(url_google)); | 922 EXPECT_EQ("A=B; E=F", cm.GetCookies(url_google)); |
869 } | 923 } |
870 | 924 |
871 // TODO test overwrite cookie | 925 // TODO test overwrite cookie |
872 | 926 |
OLD | NEW |