forked from aria2/aria2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCookieHelperTest.cc
246 lines (230 loc) · 8.35 KB
/
CookieHelperTest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "cookie_helper.h"
#include <limits>
#include <cppunit/extensions/HelperMacros.h>
#include "Exception.h"
#include "util.h"
#include "Cookie.h"
namespace aria2 {
class CookieHelperTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(CookieHelperTest);
CPPUNIT_TEST(testParseDate);
CPPUNIT_TEST(testDomainMatch);
CPPUNIT_TEST(testPathMatch);
CPPUNIT_TEST(testParse);
CPPUNIT_TEST(testReverseDomainLevel);
CPPUNIT_TEST_SUITE_END();
public:
void testParseDate();
void testDomainMatch();
void testPathMatch();
void testParse();
void testReverseDomainLevel();
};
CPPUNIT_TEST_SUITE_REGISTRATION(CookieHelperTest);
void CookieHelperTest::testParseDate()
{
// RFC1123
time_t time = 0;
std::string s = "Sat, 06 Sep 2008 15:26:33 GMT";
CPPUNIT_ASSERT(cookie::parseDate(time, s.begin(), s.end()));
CPPUNIT_ASSERT_EQUAL((time_t)1220714793, time);
// RFC850
s = "Saturday, 06-Sep-08 15:26:33 GMT";
CPPUNIT_ASSERT(cookie::parseDate(time, s.begin(), s.end()));
CPPUNIT_ASSERT_EQUAL((time_t)1220714793, time);
// ANSI C's asctime()
s = "Sun Sep 6 15:26:33 2008";
CPPUNIT_ASSERT(cookie::parseDate(time, s.begin(), s.end()));
CPPUNIT_ASSERT_EQUAL((time_t)1220714793, time);
s = "Thu Jan 1 0:0:0 1970";
CPPUNIT_ASSERT(cookie::parseDate(time, s.begin(), s.end()));
CPPUNIT_ASSERT_EQUAL((time_t)0, time);
s = "Thu Jan 1 1970 0:";
CPPUNIT_ASSERT(!cookie::parseDate(time, s.begin(), s.end()));
s = "Thu Jan 1 1970 0:0";
CPPUNIT_ASSERT(!cookie::parseDate(time, s.begin(), s.end()));
s = "Thu Jan 1 1970 0:0:";
CPPUNIT_ASSERT(!cookie::parseDate(time, s.begin(), s.end()));
// Leap year
s = "Tue, 29 Feb 2000 00:00:00 GMT";
CPPUNIT_ASSERT(cookie::parseDate(time, s.begin(), s.end()));
s = "Thu, 29 Feb 2001 00:00:00 GMT";
CPPUNIT_ASSERT(!cookie::parseDate(time, s.begin(), s.end()));
}
void CookieHelperTest::testDomainMatch()
{
CPPUNIT_ASSERT(cookie::domainMatch("localhost", "localhost"));
CPPUNIT_ASSERT(cookie::domainMatch("192.168.0.1", "192.168.0.1"));
CPPUNIT_ASSERT(cookie::domainMatch("www.example.org", "example.org"));
CPPUNIT_ASSERT(!cookie::domainMatch("192.168.0.1", "0.1"));
CPPUNIT_ASSERT(!cookie::domainMatch("example.org", "example.com"));
CPPUNIT_ASSERT(!cookie::domainMatch("example.org", "www.example.org"));
}
void CookieHelperTest::testPathMatch()
{
CPPUNIT_ASSERT(cookie::pathMatch("/", "/"));
CPPUNIT_ASSERT(cookie::pathMatch("/foo/", "/foo"));
CPPUNIT_ASSERT(!cookie::pathMatch("/bar/", "/foo"));
CPPUNIT_ASSERT(!cookie::pathMatch("/foo", "/bar/foo"));
CPPUNIT_ASSERT(cookie::pathMatch("/foo/bar", "/foo/"));
}
void CookieHelperTest::testParse()
{
time_t creationDate = 141;
{
std::string str = "ID=123456789; expires=Sun, 10-Jun-2007 11:00:00 GMT;"
"path=/foo; domain=localhost; secure;httpOnly ";
auto c = cookie::parse(str, "localhost", "/", creationDate);
CPPUNIT_ASSERT(c);
CPPUNIT_ASSERT_EQUAL(std::string("ID"), c->getName());
CPPUNIT_ASSERT_EQUAL(std::string("123456789"), c->getValue());
CPPUNIT_ASSERT_EQUAL((time_t)1181473200, c->getExpiryTime());
CPPUNIT_ASSERT(c->getPersistent());
CPPUNIT_ASSERT_EQUAL(std::string("localhost"), c->getDomain());
CPPUNIT_ASSERT(!c->getHostOnly());
CPPUNIT_ASSERT_EQUAL(std::string("/foo"), c->getPath());
CPPUNIT_ASSERT(c->getSecure());
CPPUNIT_ASSERT(c->getHttpOnly());
CPPUNIT_ASSERT_EQUAL((time_t)141, c->getCreationTime());
CPPUNIT_ASSERT_EQUAL((time_t)141, c->getLastAccessTime());
}
{
std::string str = "id=; Max-Age=0;";
auto c = cookie::parse(str, "localhost", "/", creationDate);
CPPUNIT_ASSERT(c);
CPPUNIT_ASSERT_EQUAL(std::string("id"), c->getName());
CPPUNIT_ASSERT_EQUAL((time_t)0, c->getExpiryTime());
CPPUNIT_ASSERT(c->getPersistent());
CPPUNIT_ASSERT_EQUAL(std::string("localhost"), c->getDomain());
CPPUNIT_ASSERT(c->getHostOnly());
CPPUNIT_ASSERT_EQUAL(std::string("/"), c->getPath());
CPPUNIT_ASSERT(!c->getSecure());
CPPUNIT_ASSERT(!c->getHttpOnly());
}
{
std::string str = "id=; Max-Age=-100;";
auto c = cookie::parse(str, "localhost", "/", creationDate);
CPPUNIT_ASSERT(c);
CPPUNIT_ASSERT_EQUAL((time_t)0, c->getExpiryTime());
CPPUNIT_ASSERT(c->getPersistent());
}
{
std::string str = "id=; Max-Age=100;";
auto c = cookie::parse(str, "localhost", "/", creationDate);
CPPUNIT_ASSERT(c);
CPPUNIT_ASSERT_EQUAL((time_t)creationDate + 100, c->getExpiryTime());
CPPUNIT_ASSERT(c->getPersistent());
}
{
std::string str = "id=; Max-Age=9223372036854775807;";
auto c = cookie::parse(str, "localhost", "/", creationDate);
CPPUNIT_ASSERT(c);
CPPUNIT_ASSERT_EQUAL(std::numeric_limits<time_t>::max(),
c->getExpiryTime());
CPPUNIT_ASSERT(c->getPersistent());
}
{
std::string str = "id=; Max-Age=X;";
CPPUNIT_ASSERT(!cookie::parse(str, "localhost", "/", creationDate));
}
{
std::string str = "id=; Max-Age=100garbage;";
CPPUNIT_ASSERT(!cookie::parse(str, "localhost", "/", creationDate));
}
{
std::string str = "id=; Max-Age=100;expires=Sun, 10-Jun-2007 11:00:00 GMT;";
auto c = cookie::parse(str, "localhost", "/", creationDate);
CPPUNIT_ASSERT(c);
CPPUNIT_ASSERT_EQUAL((time_t)creationDate + 100, c->getExpiryTime());
CPPUNIT_ASSERT(c->getPersistent());
}
{
// Cookie data cannot be parsed.
std::string str = "id=; expires=2007-10-01 11:00:00 GMT;";
CPPUNIT_ASSERT(!cookie::parse(str, "localhost", "/", creationDate));
}
{
std::string str = "id=;";
auto c = cookie::parse(str, "localhost", "/", creationDate);
CPPUNIT_ASSERT(c);
CPPUNIT_ASSERT(!c->getPersistent());
}
{
std::string str = "id=; path=abc";
auto c = cookie::parse(str, "localhost", "/", creationDate);
CPPUNIT_ASSERT(c);
CPPUNIT_ASSERT_EQUAL(std::string("/"), c->getPath());
}
{
std::string str = "id=; domain=.example.org";
CPPUNIT_ASSERT(cookie::parse(str, "www.example.org", "/", creationDate));
}
{
// Fails because request host does not domain-match with cookie
// domain.
std::string str = "id=; domain=www.example.org";
CPPUNIT_ASSERT(!cookie::parse(str, "example.org", "/", creationDate));
}
{
std::string str = "id=; domain=.";
CPPUNIT_ASSERT(!cookie::parse(str, "localhost", "/", creationDate));
}
{
std::string str = "";
CPPUNIT_ASSERT(!cookie::parse(str, "localhost", "/", creationDate));
}
{
std::string str = "=";
CPPUNIT_ASSERT(!cookie::parse(str, "localhost", "/", creationDate));
}
{
// Use domain last time seen.
std::string str = "id=;domain=a.example.org;domain=.example.org";
auto c = cookie::parse(str, "b.example.org", "/", creationDate);
CPPUNIT_ASSERT(c);
CPPUNIT_ASSERT_EQUAL(std::string("example.org"), c->getDomain());
}
{
// numeric host
std::string str = "id=;";
auto c = cookie::parse(str, "192.168.0.1", "/", creationDate);
CPPUNIT_ASSERT(c);
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), c->getDomain());
CPPUNIT_ASSERT(c->getHostOnly());
}
{
// numeric host
std::string str = "id=; domain=192.168.0.1";
auto c = cookie::parse(str, "192.168.0.1", "/", creationDate);
CPPUNIT_ASSERT(c);
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), c->getDomain());
CPPUNIT_ASSERT(c->getHostOnly());
}
{
// DQUOTE around cookie-value
std::string str = "id=\"foo\";";
auto c = cookie::parse(str, "localhost", "/", creationDate);
CPPUNIT_ASSERT(c);
CPPUNIT_ASSERT_EQUAL(std::string("foo"), c->getValue());
}
{
// Default path
std::string str = "id=;";
auto c = cookie::parse(str, "localhost", "/foo", creationDate);
CPPUNIT_ASSERT(c);
CPPUNIT_ASSERT_EQUAL(std::string("/foo"), c->getPath());
}
}
void CookieHelperTest::testReverseDomainLevel()
{
CPPUNIT_ASSERT_EQUAL(std::string("net.sourceforge.aria2"),
cookie::reverseDomainLevel("aria2.sourceforge.net"));
CPPUNIT_ASSERT_EQUAL(std::string("localhost"),
cookie::reverseDomainLevel("localhost"));
// Behavior check
CPPUNIT_ASSERT_EQUAL(std::string(""), cookie::reverseDomainLevel(""));
CPPUNIT_ASSERT_EQUAL(std::string(""), cookie::reverseDomainLevel("."));
CPPUNIT_ASSERT_EQUAL(std::string("foo."), cookie::reverseDomainLevel(".foo"));
CPPUNIT_ASSERT_EQUAL(std::string("foo"), cookie::reverseDomainLevel("foo."));
}
} // namespace aria2