forked from aria2/aria2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCookieStorageTest.cc
485 lines (428 loc) · 16.5 KB
/
CookieStorageTest.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include "CookieStorage.h"
#include <iostream>
#include <algorithm>
#include <limits>
#include <cppunit/extensions/HelperMacros.h>
#include "Exception.h"
#include "util.h"
#include "TimeA2.h"
#include "RecoverableException.h"
#include "File.h"
#include "TestUtil.h"
#include "TimerA2.h"
namespace aria2 {
class CookieStorageTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(CookieStorageTest);
CPPUNIT_TEST(testStore);
CPPUNIT_TEST(testParseAndStore);
CPPUNIT_TEST(testCriteriaFind);
CPPUNIT_TEST(testCriteriaFind_cookieOrder);
CPPUNIT_TEST(testLoad);
CPPUNIT_TEST(testLoad_sqlite3);
CPPUNIT_TEST(testLoad_fileNotfound);
CPPUNIT_TEST(testSaveNsFormat);
CPPUNIT_TEST(testSaveNsFormat_fail);
CPPUNIT_TEST(testCookieIsFull);
CPPUNIT_TEST(testDomainIsFull);
CPPUNIT_TEST(testEviction);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {}
void tearDown() {}
void testStore();
void testParseAndStore();
void testCriteriaFind();
void testCriteriaFind_cookieOrder();
void testLoad();
void testLoad_sqlite3();
void testLoad_fileNotfound();
void testSaveNsFormat();
void testSaveNsFormat_fail();
void testCookieIsFull();
void testDomainIsFull();
void testEviction();
};
CPPUNIT_TEST_SUITE_REGISTRATION(CookieStorageTest);
namespace {
std::vector<const Cookie*> dumpCookie(const CookieStorage& st)
{
auto res = std::vector<const Cookie*>{};
st.dumpCookie(std::back_inserter(res));
std::sort(res.begin(), res.end(), CookieSorter());
return res;
}
} // namespace
void CookieStorageTest::testStore()
{
time_t now = 999;
auto st = CookieStorage{};
auto goodCookie = [&]() {
auto c = createCookie("k", "v", "localhost", true, "/", false);
c->setCreationTime(now);
return c;
};
CPPUNIT_ASSERT(st.store(goodCookie(), now));
CPPUNIT_ASSERT_EQUAL((size_t)1, st.size());
CPPUNIT_ASSERT(st.contains(*goodCookie()));
auto anotherCookie = []() {
return createCookie("k", "v", "mirror", true, "/", true);
};
CPPUNIT_ASSERT(st.store(anotherCookie(), now));
CPPUNIT_ASSERT_EQUAL((size_t)2, st.size());
CPPUNIT_ASSERT(st.contains(*anotherCookie()));
CPPUNIT_ASSERT(st.contains(*goodCookie()));
++now;
auto updateGoodCookie = [&]() {
auto c = createCookie("k", "v2", "localhost", true, "/", false);
c->setCreationTime(now);
return c;
};
CPPUNIT_ASSERT(st.store(updateGoodCookie(), now));
CPPUNIT_ASSERT_EQUAL((size_t)2, st.size());
CPPUNIT_ASSERT(st.contains(*updateGoodCookie()));
CPPUNIT_ASSERT(st.contains(*anotherCookie()));
auto cookies = st.criteriaFind("localhost", "/", now, false);
CPPUNIT_ASSERT_EQUAL((size_t)1, cookies.size());
CPPUNIT_ASSERT_EQUAL(std::string("v2"), cookies[0]->getValue());
// New cookie's creation time must match old cookie's creation time.
CPPUNIT_ASSERT_EQUAL((time_t)999, cookies[0]->getCreationTime());
auto expireGoodCookie = []() {
return createCookie("k", "v3", 0, "localhost", true, "/", false);
};
CPPUNIT_ASSERT(!st.store(expireGoodCookie(), now));
CPPUNIT_ASSERT_EQUAL((size_t)1, st.size());
CPPUNIT_ASSERT(st.contains(*anotherCookie()));
auto fromNumericHost = []() {
return createCookie("k", "v", "192.168.1.1", true, "/", false);
};
CPPUNIT_ASSERT(st.store(fromNumericHost(), now));
CPPUNIT_ASSERT_EQUAL((size_t)2, st.size());
CPPUNIT_ASSERT(st.contains(*fromNumericHost()));
}
void CookieStorageTest::testParseAndStore()
{
auto st = CookieStorage{};
time_t now = 1000;
std::string localhostCookieStr =
"k=v;"
" expires=Fri, 01 Jan 2038 00:00:00 GMT; path=/; domain=localhost;";
CPPUNIT_ASSERT(
st.parseAndStore(localhostCookieStr, "localhost", "/downloads", now));
CPPUNIT_ASSERT(
!st.parseAndStore(localhostCookieStr, "mirror", "/downloads", now));
CPPUNIT_ASSERT(
!st.parseAndStore(localhostCookieStr, "127.0.0.1", "/downloads", now));
std::string numericHostCookieStr =
"k=v;"
" expires=Fri, 01 Jan 2038 00:00:00 GMT; path=/; domain=192.168.1.1;";
CPPUNIT_ASSERT(
st.parseAndStore(numericHostCookieStr, "192.168.1.1", "/", now));
// No domain and no path are specified.
std::string noDomainPathCookieStr = "k=v";
CPPUNIT_ASSERT(st.parseAndStore(noDomainPathCookieStr, "aria2.sf.net",
"/downloads", now));
}
void CookieStorageTest::testCriteriaFind()
{
auto st = CookieStorage{};
time_t now = 1000;
auto alpha = []() {
return createCookie("alpha", "ALPHA", "aria2.org", false, "/", false);
};
auto bravo = []() {
return createCookie("bravo", "BRAVO", 1060, "aria2.org", false, "/foo",
false);
};
auto charlie = []() {
return createCookie("charlie", "CHARLIE", "aria2.org", false, "/", true);
};
auto delta = []() {
return createCookie("delta", "DELTA", "aria2.org", false, "/foo/bar",
false);
};
auto echo = []() {
return createCookie("echo", "ECHO", "www.dl.aria2.org", false, "/", false);
};
auto foxtrot = []() {
return createCookie("foxtrot", "FOXTROT", "sf.net", false, "/", false);
};
auto golf = []() {
return createCookie("golf", "GOLF", "192.168.1.1", true, "/", false);
};
auto hotel1 = []() {
return createCookie("hotel", "HOTEL1", "samename.x", false, "/", false);
};
auto hotel2 = []() {
return createCookie("hotel", "HOTEL2", "samename.x", false, "/hotel",
false);
};
auto hotel3 = []() {
return createCookie("hotel", "HOTEL3", "samename.x", false, "/bar/wine",
false);
};
auto hotel4 = []() {
return createCookie("hotel", "HOTEL4", "samename.x", false, "/bar/", false);
};
auto india1 = []() {
return createCookie("india", "INDIA1", "default.domain", true, "/foo",
false);
};
auto india2 = []() {
return createCookie("india", "INDIA2", "default.domain", false, "/", false);
};
auto juliet1 = []() {
return createCookie("juliet", "JULIET1", "localhost", true, "/foo", false);
};
CPPUNIT_ASSERT(st.store(alpha(), now));
CPPUNIT_ASSERT(st.store(bravo(), now));
CPPUNIT_ASSERT(st.store(charlie(), now));
CPPUNIT_ASSERT(st.store(delta(), now));
CPPUNIT_ASSERT(st.store(echo(), now));
CPPUNIT_ASSERT(st.store(foxtrot(), now));
CPPUNIT_ASSERT(st.store(golf(), now));
CPPUNIT_ASSERT(st.store(hotel1(), now));
CPPUNIT_ASSERT(st.store(hotel2(), now));
CPPUNIT_ASSERT(st.store(hotel3(), now));
CPPUNIT_ASSERT(st.store(hotel4(), now));
CPPUNIT_ASSERT(st.store(india1(), now));
CPPUNIT_ASSERT(st.store(india2(), now));
CPPUNIT_ASSERT(st.store(juliet1(), now));
auto aria2Slash = st.criteriaFind("www.dl.aria2.org", "/", 0, false);
CPPUNIT_ASSERT_EQUAL((size_t)2, aria2Slash.size());
CPPUNIT_ASSERT(derefFind(aria2Slash, alpha()));
CPPUNIT_ASSERT(derefFind(aria2Slash, echo()));
auto aria2SlashFoo = st.criteriaFind("www.dl.aria2.org", "/foo", 0, false);
CPPUNIT_ASSERT_EQUAL((size_t)3, aria2SlashFoo.size());
CPPUNIT_ASSERT_EQUAL(std::string("bravo"), aria2SlashFoo[0]->getName());
CPPUNIT_ASSERT(derefFind(aria2SlashFoo, alpha()));
CPPUNIT_ASSERT(derefFind(aria2SlashFoo, echo()));
auto aria2Expires = st.criteriaFind("www.dl.aria2.org", "/foo", 1120, false);
CPPUNIT_ASSERT_EQUAL((size_t)2, aria2Expires.size());
CPPUNIT_ASSERT(derefFind(aria2Expires, alpha()));
CPPUNIT_ASSERT(derefFind(aria2Expires, echo()));
auto dlAria2 = st.criteriaFind("dl.aria2.org", "/", 0, false);
CPPUNIT_ASSERT_EQUAL((size_t)1, dlAria2.size());
CPPUNIT_ASSERT_EQUAL(std::string("alpha"), dlAria2[0]->getName());
auto tailmatchAria2 = st.criteriaFind("myaria2.org", "/", 0, false);
CPPUNIT_ASSERT(tailmatchAria2.empty());
auto numericHostCookies = st.criteriaFind("192.168.1.1", "/", 0, false);
CPPUNIT_ASSERT_EQUAL((size_t)1, numericHostCookies.size());
CPPUNIT_ASSERT_EQUAL(std::string("golf"), numericHostCookies[0]->getName());
auto sameNameCookies = st.criteriaFind("samename.x", "/bar/wine", 0, false);
CPPUNIT_ASSERT_EQUAL((size_t)3, sameNameCookies.size());
CPPUNIT_ASSERT_EQUAL(std::string("HOTEL3"), sameNameCookies[0]->getValue());
CPPUNIT_ASSERT_EQUAL(std::string("HOTEL4"), sameNameCookies[1]->getValue());
CPPUNIT_ASSERT_EQUAL(std::string("HOTEL1"), sameNameCookies[2]->getValue());
auto defaultDomainCookies =
st.criteriaFind("default.domain", "/foo", 0, false);
CPPUNIT_ASSERT_EQUAL((size_t)2, defaultDomainCookies.size());
CPPUNIT_ASSERT_EQUAL(std::string("INDIA1"),
defaultDomainCookies[0]->getValue());
CPPUNIT_ASSERT_EQUAL(std::string("INDIA2"),
defaultDomainCookies[1]->getValue());
defaultDomainCookies =
st.criteriaFind("sub.default.domain", "/foo", 0, false);
CPPUNIT_ASSERT_EQUAL((size_t)1, defaultDomainCookies.size());
CPPUNIT_ASSERT_EQUAL(std::string("INDIA2"),
defaultDomainCookies[0]->getValue());
// localhost.local case
auto localDomainCookies = st.criteriaFind("localhost", "/foo", 0, false);
CPPUNIT_ASSERT_EQUAL((size_t)1, localDomainCookies.size());
CPPUNIT_ASSERT_EQUAL(std::string("JULIET1"),
localDomainCookies[0]->getValue());
}
void CookieStorageTest::testCriteriaFind_cookieOrder()
{
auto st = CookieStorage{};
auto a = createCookie("a", "0", "host", true, "/", false);
a->setCreationTime(1000);
auto b = createCookie("b", "0", "host", true, "/foo", false);
b->setCreationTime(5000);
auto c = createCookie("c", "0", "host", true, "/foo", false);
c->setCreationTime(4000);
auto d = createCookie("d", "0", "host", true, "/foo/bar", false);
d->setCreationTime(6000);
st.store(std::move(a), 0);
st.store(std::move(b), 0);
st.store(std::move(c), 0);
st.store(std::move(d), 0);
auto cookies = st.criteriaFind("host", "/foo/bar", 0, false);
CPPUNIT_ASSERT_EQUAL((size_t)4, cookies.size());
CPPUNIT_ASSERT_EQUAL(std::string("d"), cookies[0]->getName());
CPPUNIT_ASSERT_EQUAL(std::string("c"), cookies[1]->getName());
CPPUNIT_ASSERT_EQUAL(std::string("b"), cookies[2]->getName());
CPPUNIT_ASSERT_EQUAL(std::string("a"), cookies[3]->getName());
}
void CookieStorageTest::testLoad()
{
auto st = CookieStorage{};
st.load(A2_TEST_DIR "/nscookietest.txt", 1001);
CPPUNIT_ASSERT_EQUAL((size_t)4, st.size());
auto cookies = dumpCookie(st);
auto c = cookies[0];
CPPUNIT_ASSERT_EQUAL(std::string("passwd"), c->getName());
CPPUNIT_ASSERT_EQUAL(std::string("secret"), c->getValue());
CPPUNIT_ASSERT_EQUAL(std::numeric_limits<time_t>::max(), c->getExpiryTime());
CPPUNIT_ASSERT(!c->getPersistent());
CPPUNIT_ASSERT_EQUAL(std::string("/cgi-bin"), c->getPath());
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), c->getDomain());
CPPUNIT_ASSERT(c->getHostOnly());
CPPUNIT_ASSERT(!c->getSecure());
c = cookies[1];
CPPUNIT_ASSERT_EQUAL(std::string("novalue"), c->getName());
CPPUNIT_ASSERT_EQUAL(std::string(""), c->getValue());
CPPUNIT_ASSERT_EQUAL((time_t)2147483647, c->getExpiryTime());
CPPUNIT_ASSERT(c->getPersistent());
CPPUNIT_ASSERT_EQUAL(std::string("/"), c->getPath());
CPPUNIT_ASSERT_EQUAL(std::string("example.org"), c->getDomain());
CPPUNIT_ASSERT(!c->getHostOnly());
CPPUNIT_ASSERT(!c->getSecure());
c = cookies[2];
CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), c->getName());
CPPUNIT_ASSERT_EQUAL(std::string("123456789"), c->getValue());
CPPUNIT_ASSERT_EQUAL((time_t)2147483647, c->getExpiryTime());
CPPUNIT_ASSERT_EQUAL(std::string("/"), c->getPath());
CPPUNIT_ASSERT_EQUAL(std::string("localhost"), c->getDomain());
CPPUNIT_ASSERT(c->getHostOnly());
CPPUNIT_ASSERT(c->getSecure());
c = cookies[3];
CPPUNIT_ASSERT_EQUAL(std::string("TAX"), c->getName());
CPPUNIT_ASSERT_EQUAL(std::string("1000"), c->getValue());
CPPUNIT_ASSERT_EQUAL((time_t)1463304912, c->getExpiryTime());
CPPUNIT_ASSERT(c->getPersistent());
CPPUNIT_ASSERT_EQUAL(std::string("/"), c->getPath());
CPPUNIT_ASSERT_EQUAL(std::string("something"), c->getDomain());
CPPUNIT_ASSERT(!c->getSecure());
}
void CookieStorageTest::testLoad_sqlite3()
{
auto st = CookieStorage{};
#ifdef HAVE_SQLITE3
st.load(A2_TEST_DIR "/cookies.sqlite", 1000);
CPPUNIT_ASSERT_EQUAL((size_t)2, st.size());
auto cookies = dumpCookie(st);
auto c = cookies[0];
CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), c->getName());
CPPUNIT_ASSERT_EQUAL(std::string("123456789"), c->getValue());
CPPUNIT_ASSERT_EQUAL((time_t)INT32_MAX, 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());
c = cookies[1];
CPPUNIT_ASSERT_EQUAL(std::string("foo"), c->getName());
CPPUNIT_ASSERT_EQUAL(std::string("bar"), c->getValue());
CPPUNIT_ASSERT((time_t)INT32_MAX <= c->getExpiryTime());
CPPUNIT_ASSERT(c->getPersistent());
CPPUNIT_ASSERT_EQUAL(std::string("overflow.time_t.org"), c->getDomain());
CPPUNIT_ASSERT(!c->getHostOnly());
CPPUNIT_ASSERT_EQUAL(std::string("/path/to"), c->getPath());
CPPUNIT_ASSERT(!c->getSecure());
#else // !HAVE_SQLITE3
CPPUNIT_ASSERT(!st.load(A2_TEST_DIR "/cookies.sqlite", 1000));
#endif // !HAVE_SQLITE3
}
void CookieStorageTest::testLoad_fileNotfound()
{
auto st = CookieStorage{};
CPPUNIT_ASSERT(
!st.load("./aria2_CookieStorageTest_testLoad_fileNotfound", 0));
}
void CookieStorageTest::testSaveNsFormat()
{
// TODO add cookie with default domain
std::string filename =
A2_TEST_OUT_DIR "/aria2_CookieStorageTest_testSaveNsFormat";
File(filename).remove();
auto st = CookieStorage{};
time_t now = 1000;
st.store(
createCookie("favorite", "classic", "domain.org", false, "/config", true),
now);
st.store(createCookie("uid", "tujikawa", now, "domain.org", true, "/", false),
now);
CPPUNIT_ASSERT(st.saveNsFormat(filename));
auto loadst = CookieStorage{};
loadst.load(filename, now);
CPPUNIT_ASSERT_EQUAL((size_t)2, loadst.size());
auto cookies = dumpCookie(loadst);
CPPUNIT_ASSERT_EQUAL((size_t)2, cookies.size());
CPPUNIT_ASSERT_EQUAL(std::string("favorite"), cookies[0]->getName());
CPPUNIT_ASSERT_EQUAL(std::string("uid"), cookies[1]->getName());
}
void CookieStorageTest::testSaveNsFormat_fail()
{
std::string filename =
A2_TEST_OUT_DIR "/aria2_CookieStorageTest_testSaveNsFormat_fail";
File f(filename);
f.remove();
f.mkdirs();
CPPUNIT_ASSERT(f.isDir());
auto st = CookieStorage{};
CPPUNIT_ASSERT(!st.saveNsFormat(filename));
}
void CookieStorageTest::testCookieIsFull()
{
auto st = CookieStorage{};
for (size_t i = 0; i < CookieStorage::MAX_COOKIE_PER_DOMAIN + 1; ++i) {
st.store(
createCookie("k" + util::itos(i), "v", "aria2.org", false, "/", false),
0);
}
CPPUNIT_ASSERT_EQUAL((size_t)CookieStorage::MAX_COOKIE_PER_DOMAIN, st.size());
}
void CookieStorageTest::testDomainIsFull()
{
// See DOMAIN_EVICTION_TRIGGER and DOMAIN_EVICTION_RATE in
// CookieStorage.cc
auto st = CookieStorage{};
for (size_t i = 0; i < 2001; ++i) {
st.store(createCookie("k", "v", "domain" + util::itos(i), true, "/", false),
0);
}
CPPUNIT_ASSERT_EQUAL((size_t)1801, st.size());
}
void CookieStorageTest::testEviction()
{
auto st = CookieStorage{};
auto alpha = []() {
return createCookie("a", "alpha", "aria2.sf.net", false, "/", false);
};
auto bravo = []() {
return createCookie("b", "bravo", "d.aria2.sf.net", false, "/", false);
};
auto charlie = []() {
return createCookie("c", "charlie", "a2.github.com", false, "/", false);
};
auto delta = []() {
return createCookie("d", "delta", "aria2.sf.net", false, "/", false);
};
st.store(alpha(), 0);
CPPUNIT_ASSERT_EQUAL((size_t)1, st.getLruTrackerSize());
st.store(bravo(), 1);
CPPUNIT_ASSERT_EQUAL((size_t)2, st.getLruTrackerSize());
st.store(charlie(), 2);
CPPUNIT_ASSERT_EQUAL((size_t)3, st.getLruTrackerSize());
st.store(delta(), 0);
CPPUNIT_ASSERT_EQUAL((size_t)3, st.getLruTrackerSize());
// aria2.sf.net will be evicted
st.evictNode(1);
CPPUNIT_ASSERT_EQUAL((size_t)2, st.getLruTrackerSize());
CPPUNIT_ASSERT(!st.contains(*alpha()));
CPPUNIT_ASSERT(st.contains(*bravo()));
CPPUNIT_ASSERT(st.contains(*charlie()));
CPPUNIT_ASSERT(!st.contains(*delta()));
// d.aria2.sf.net will be evicted
st.evictNode(1);
CPPUNIT_ASSERT_EQUAL((size_t)1, st.getLruTrackerSize());
CPPUNIT_ASSERT(!st.contains(*bravo()));
CPPUNIT_ASSERT(st.contains(*charlie()));
// a2.github.com will be evicted
st.evictNode(1);
CPPUNIT_ASSERT_EQUAL((size_t)0, st.getLruTrackerSize());
CPPUNIT_ASSERT(!st.contains(*charlie()));
CPPUNIT_ASSERT_EQUAL((size_t)0, st.size());
CPPUNIT_ASSERT(!st.getRootNode()->hasNext());
}
} // namespace aria2