2008-08-27 16:04:36 +00:00
|
|
|
#include "CookieStorage.h"
|
2008-12-15 15:48:48 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <algorithm>
|
2010-10-27 14:54:25 +00:00
|
|
|
#include <limits>
|
2008-12-15 15:48:48 +00:00
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2008-08-27 16:04:36 +00:00
|
|
|
#include "Exception.h"
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2008-08-27 16:04:36 +00:00
|
|
|
#include "TimeA2.h"
|
2008-08-27 16:31:43 +00:00
|
|
|
#include "RecoverableException.h"
|
2009-06-29 08:42:58 +00:00
|
|
|
#include "File.h"
|
2010-01-28 14:01:50 +00:00
|
|
|
#include "TestUtil.h"
|
2013-06-27 15:25:06 +00:00
|
|
|
#include "TimerA2.h"
|
2008-08-27 16:04:36 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
class CookieStorageTest : public CppUnit::TestFixture {
|
2008-08-27 16:04:36 +00:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(CookieStorageTest);
|
|
|
|
CPPUNIT_TEST(testStore);
|
|
|
|
CPPUNIT_TEST(testParseAndStore);
|
|
|
|
CPPUNIT_TEST(testCriteriaFind);
|
2010-10-09 14:22:49 +00:00
|
|
|
CPPUNIT_TEST(testCriteriaFind_cookieOrder);
|
2008-08-27 16:31:43 +00:00
|
|
|
CPPUNIT_TEST(testLoad);
|
|
|
|
CPPUNIT_TEST(testLoad_sqlite3);
|
|
|
|
CPPUNIT_TEST(testLoad_fileNotfound);
|
2009-05-22 14:51:57 +00:00
|
|
|
CPPUNIT_TEST(testSaveNsFormat);
|
|
|
|
CPPUNIT_TEST(testSaveNsFormat_fail);
|
2010-01-28 14:01:50 +00:00
|
|
|
CPPUNIT_TEST(testCookieIsFull);
|
|
|
|
CPPUNIT_TEST(testDomainIsFull);
|
2013-06-27 15:25:06 +00:00
|
|
|
CPPUNIT_TEST(testEviction);
|
2008-08-27 16:04:36 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
2015-12-27 09:39:47 +00:00
|
|
|
|
2008-08-27 16:04:36 +00:00
|
|
|
public:
|
|
|
|
void setUp() {}
|
|
|
|
|
|
|
|
void tearDown() {}
|
|
|
|
|
|
|
|
void testStore();
|
|
|
|
void testParseAndStore();
|
|
|
|
void testCriteriaFind();
|
2010-10-09 14:22:49 +00:00
|
|
|
void testCriteriaFind_cookieOrder();
|
2008-08-27 16:31:43 +00:00
|
|
|
void testLoad();
|
|
|
|
void testLoad_sqlite3();
|
|
|
|
void testLoad_fileNotfound();
|
2009-05-22 14:51:57 +00:00
|
|
|
void testSaveNsFormat();
|
|
|
|
void testSaveNsFormat_fail();
|
2010-01-28 14:01:50 +00:00
|
|
|
void testCookieIsFull();
|
|
|
|
void testDomainIsFull();
|
2013-06-27 15:25:06 +00:00
|
|
|
void testEviction();
|
2008-08-27 16:04:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(CookieStorageTest);
|
|
|
|
|
2013-06-27 15:25:06 +00:00
|
|
|
namespace {
|
|
|
|
std::vector<const Cookie*> dumpCookie(const CookieStorage& st)
|
2010-01-28 14:01:50 +00:00
|
|
|
{
|
2013-06-27 15:25:06 +00:00
|
|
|
auto res = std::vector<const Cookie*>{};
|
|
|
|
st.dumpCookie(std::back_inserter(res));
|
|
|
|
std::sort(res.begin(), res.end(), CookieSorter());
|
|
|
|
return res;
|
2010-01-28 14:01:50 +00:00
|
|
|
}
|
2013-06-27 15:25:06 +00:00
|
|
|
} // namespace
|
2010-01-28 14:01:50 +00:00
|
|
|
|
2008-08-27 16:04:36 +00:00
|
|
|
void CookieStorageTest::testStore()
|
|
|
|
{
|
2013-09-19 15:24:03 +00:00
|
|
|
time_t now = 999;
|
2013-06-27 15:25:06 +00:00
|
|
|
auto st = CookieStorage{};
|
2013-09-19 15:24:03 +00:00
|
|
|
auto goodCookie = [&]() {
|
|
|
|
auto c = createCookie("k", "v", "localhost", true, "/", false);
|
|
|
|
c->setCreationTime(now);
|
|
|
|
return c;
|
2013-06-27 15:25:06 +00:00
|
|
|
};
|
|
|
|
CPPUNIT_ASSERT(st.store(goodCookie(), now));
|
2008-08-27 16:04:36 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, st.size());
|
2013-06-27 15:25:06 +00:00
|
|
|
CPPUNIT_ASSERT(st.contains(*goodCookie()));
|
2008-08-27 16:04:36 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
auto anotherCookie =
|
|
|
|
[]() { return createCookie("k", "v", "mirror", true, "/", true); };
|
2013-06-27 15:25:06 +00:00
|
|
|
CPPUNIT_ASSERT(st.store(anotherCookie(), now));
|
2008-08-27 16:04:36 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, st.size());
|
2013-06-27 15:25:06 +00:00
|
|
|
CPPUNIT_ASSERT(st.contains(*anotherCookie()));
|
|
|
|
CPPUNIT_ASSERT(st.contains(*goodCookie()));
|
2008-08-27 16:04:36 +00:00
|
|
|
|
2013-09-19 15:24:03 +00:00
|
|
|
++now;
|
|
|
|
auto updateGoodCookie = [&]() {
|
2015-12-27 09:39:47 +00:00
|
|
|
auto c = createCookie("k", "v2", "localhost", true, "/", false);
|
2013-09-19 15:24:03 +00:00
|
|
|
c->setCreationTime(now);
|
|
|
|
return c;
|
2013-06-27 15:25:06 +00:00
|
|
|
};
|
|
|
|
CPPUNIT_ASSERT(st.store(updateGoodCookie(), now));
|
2008-08-27 16:04:36 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, st.size());
|
2013-06-27 15:25:06 +00:00
|
|
|
CPPUNIT_ASSERT(st.contains(*updateGoodCookie()));
|
|
|
|
CPPUNIT_ASSERT(st.contains(*anotherCookie()));
|
2013-09-19 15:24:03 +00:00
|
|
|
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());
|
2008-08-27 16:04:36 +00:00
|
|
|
|
2013-06-27 15:25:06 +00:00
|
|
|
auto expireGoodCookie = []() {
|
|
|
|
return createCookie("k", "v3", 0, "localhost", true, "/", false);
|
|
|
|
};
|
|
|
|
CPPUNIT_ASSERT(!st.store(expireGoodCookie(), now));
|
2008-08-27 16:04:36 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, st.size());
|
2013-06-27 15:25:06 +00:00
|
|
|
CPPUNIT_ASSERT(st.contains(*anotherCookie()));
|
2008-08-27 16:04:36 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
auto fromNumericHost =
|
|
|
|
[]() { return createCookie("k", "v", "192.168.1.1", true, "/", false); };
|
2013-06-27 15:25:06 +00:00
|
|
|
CPPUNIT_ASSERT(st.store(fromNumericHost(), now));
|
2008-09-01 13:46:03 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, st.size());
|
2013-06-27 15:25:06 +00:00
|
|
|
CPPUNIT_ASSERT(st.contains(*fromNumericHost()));
|
2008-08-27 16:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CookieStorageTest::testParseAndStore()
|
|
|
|
{
|
2013-06-27 15:25:06 +00:00
|
|
|
auto st = CookieStorage{};
|
2010-10-09 14:22:49 +00:00
|
|
|
time_t now = 1000;
|
2015-12-27 09:39:47 +00:00
|
|
|
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));
|
2008-09-01 13:46:03 +00:00
|
|
|
|
2010-01-28 14:01:50 +00:00
|
|
|
// No domain and no path are specified.
|
|
|
|
std::string noDomainPathCookieStr = "k=v";
|
2015-12-27 09:39:47 +00:00
|
|
|
CPPUNIT_ASSERT(st.parseAndStore(noDomainPathCookieStr, "aria2.sf.net",
|
|
|
|
"/downloads", now));
|
2008-08-27 16:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CookieStorageTest::testCriteriaFind()
|
|
|
|
{
|
2013-06-27 15:25:06 +00:00
|
|
|
auto st = CookieStorage{};
|
2010-10-09 14:22:49 +00:00
|
|
|
time_t now = 1000;
|
|
|
|
|
2013-06-27 15:25:06 +00:00
|
|
|
auto alpha = []() {
|
2015-12-27 09:39:47 +00:00
|
|
|
return createCookie("alpha", "ALPHA", "aria2.org", false, "/", false);
|
2013-06-27 15:25:06 +00:00
|
|
|
};
|
|
|
|
auto bravo = []() {
|
2015-12-27 09:39:47 +00:00
|
|
|
return createCookie("bravo", "BRAVO", 1060, "aria2.org", false, "/foo",
|
|
|
|
false);
|
2013-06-27 15:25:06 +00:00
|
|
|
};
|
|
|
|
auto charlie = []() {
|
2015-12-27 09:39:47 +00:00
|
|
|
return createCookie("charlie", "CHARLIE", "aria2.org", false, "/", true);
|
2013-06-27 15:25:06 +00:00
|
|
|
};
|
|
|
|
auto delta = []() {
|
2015-12-27 09:39:47 +00:00
|
|
|
return createCookie("delta", "DELTA", "aria2.org", false, "/foo/bar",
|
|
|
|
false);
|
2013-06-27 15:25:06 +00:00
|
|
|
};
|
|
|
|
auto echo = []() {
|
2015-12-27 09:39:47 +00:00
|
|
|
return createCookie("echo", "ECHO", "www.dl.aria2.org", false, "/", false);
|
2013-06-27 15:25:06 +00:00
|
|
|
};
|
|
|
|
auto foxtrot = []() {
|
2015-12-27 09:39:47 +00:00
|
|
|
return createCookie("foxtrot", "FOXTROT", "sf.net", false, "/", false);
|
2013-06-27 15:25:06 +00:00
|
|
|
};
|
|
|
|
auto golf = []() {
|
2015-12-27 09:39:47 +00:00
|
|
|
return createCookie("golf", "GOLF", "192.168.1.1", true, "/", false);
|
2013-06-27 15:25:06 +00:00
|
|
|
};
|
|
|
|
auto hotel1 = []() {
|
2015-12-27 09:39:47 +00:00
|
|
|
return createCookie("hotel", "HOTEL1", "samename.x", false, "/", false);
|
2013-06-27 15:25:06 +00:00
|
|
|
};
|
|
|
|
auto hotel2 = []() {
|
2015-12-27 09:39:47 +00:00
|
|
|
return createCookie("hotel", "HOTEL2", "samename.x", false, "/hotel",
|
|
|
|
false);
|
2013-06-27 15:25:06 +00:00
|
|
|
};
|
|
|
|
auto hotel3 = []() {
|
2015-12-27 09:39:47 +00:00
|
|
|
return createCookie("hotel", "HOTEL3", "samename.x", false, "/bar/wine",
|
|
|
|
false);
|
2013-06-27 15:25:06 +00:00
|
|
|
};
|
|
|
|
auto hotel4 = []() {
|
2015-12-27 09:39:47 +00:00
|
|
|
return createCookie("hotel", "HOTEL4", "samename.x", false, "/bar/", false);
|
2013-06-27 15:25:06 +00:00
|
|
|
};
|
|
|
|
auto india1 = []() {
|
2015-12-27 09:39:47 +00:00
|
|
|
return createCookie("india", "INDIA1", "default.domain", true, "/foo",
|
|
|
|
false);
|
2013-06-27 15:25:06 +00:00
|
|
|
};
|
|
|
|
auto india2 = []() {
|
2015-12-27 09:39:47 +00:00
|
|
|
return createCookie("india", "INDIA2", "default.domain", false, "/", false);
|
2013-06-27 15:25:06 +00:00
|
|
|
};
|
|
|
|
auto juliet1 = []() {
|
2015-12-27 09:39:47 +00:00
|
|
|
return createCookie("juliet", "JULIET1", "localhost", true, "/foo", false);
|
2013-06-27 15:25:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2008-08-27 16:04:36 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, aria2Slash.size());
|
2013-06-27 15:25:06 +00:00
|
|
|
CPPUNIT_ASSERT(derefFind(aria2Slash, alpha()));
|
|
|
|
CPPUNIT_ASSERT(derefFind(aria2Slash, echo()));
|
2008-08-27 16:04:36 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
auto aria2SlashFoo = st.criteriaFind("www.dl.aria2.org", "/foo", 0, false);
|
2008-08-27 16:04:36 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, aria2SlashFoo.size());
|
2013-06-27 15:25:06 +00:00
|
|
|
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);
|
2008-08-27 16:04:36 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, aria2Expires.size());
|
2013-06-27 15:25:06 +00:00
|
|
|
CPPUNIT_ASSERT(derefFind(aria2Expires, alpha()));
|
|
|
|
CPPUNIT_ASSERT(derefFind(aria2Expires, echo()));
|
2008-08-27 16:04:36 +00:00
|
|
|
|
2013-06-27 15:25:06 +00:00
|
|
|
auto dlAria2 = st.criteriaFind("dl.aria2.org", "/", 0, false);
|
2008-08-27 16:04:36 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, dlAria2.size());
|
2013-06-27 15:25:06 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("alpha"), dlAria2[0]->getName());
|
2008-09-01 13:46:03 +00:00
|
|
|
|
2013-06-27 15:25:06 +00:00
|
|
|
auto tailmatchAria2 = st.criteriaFind("myaria2.org", "/", 0, false);
|
2013-04-15 12:30:15 +00:00
|
|
|
CPPUNIT_ASSERT(tailmatchAria2.empty());
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
auto numericHostCookies = st.criteriaFind("192.168.1.1", "/", 0, false);
|
2008-09-01 13:46:03 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, numericHostCookies.size());
|
2013-06-27 15:25:06 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("golf"), numericHostCookies[0]->getName());
|
2010-01-28 14:01:50 +00:00
|
|
|
|
2013-06-27 15:25:06 +00:00
|
|
|
auto sameNameCookies = st.criteriaFind("samename.x", "/bar/wine", 0, false);
|
2010-01-28 14:01:50 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, sameNameCookies.size());
|
2013-06-27 15:25:06 +00:00
|
|
|
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());
|
2010-01-28 14:01:50 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
auto defaultDomainCookies =
|
|
|
|
st.criteriaFind("default.domain", "/foo", 0, false);
|
2010-01-28 14:01:50 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, defaultDomainCookies.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("INDIA1"),
|
2013-06-27 15:25:06 +00:00
|
|
|
defaultDomainCookies[0]->getValue());
|
2010-01-28 14:01:50 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("INDIA2"),
|
2013-06-27 15:25:06 +00:00
|
|
|
defaultDomainCookies[1]->getValue());
|
2010-01-29 12:04:36 +00:00
|
|
|
defaultDomainCookies =
|
2015-12-27 09:39:47 +00:00
|
|
|
st.criteriaFind("sub.default.domain", "/foo", 0, false);
|
2010-01-29 12:04:36 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, defaultDomainCookies.size());
|
2010-10-09 14:22:49 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("INDIA2"),
|
2013-06-27 15:25:06 +00:00
|
|
|
defaultDomainCookies[0]->getValue());
|
2010-01-28 14:01:50 +00:00
|
|
|
|
|
|
|
// localhost.local case
|
2013-06-27 15:25:06 +00:00
|
|
|
auto localDomainCookies = st.criteriaFind("localhost", "/foo", 0, false);
|
2010-10-09 14:22:49 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, localDomainCookies.size());
|
2010-01-28 14:01:50 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("JULIET1"),
|
2013-06-27 15:25:06 +00:00
|
|
|
localDomainCookies[0]->getValue());
|
2010-10-09 14:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CookieStorageTest::testCriteriaFind_cookieOrder()
|
|
|
|
{
|
2013-06-27 15:25:06 +00:00
|
|
|
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);
|
2010-10-09 14:22:49 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)4, cookies.size());
|
2013-06-27 15:25:06 +00:00
|
|
|
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());
|
2008-08-27 16:04:36 +00:00
|
|
|
}
|
|
|
|
|
2008-08-27 16:31:43 +00:00
|
|
|
void CookieStorageTest::testLoad()
|
|
|
|
{
|
2013-06-27 15:25:06 +00:00
|
|
|
auto st = CookieStorage{};
|
2008-08-27 16:31:43 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
st.load(A2_TEST_DIR "/nscookietest.txt", 1001);
|
2008-08-27 16:31:43 +00:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)4, st.size());
|
|
|
|
|
2013-06-27 15:25:06 +00:00
|
|
|
auto cookies = dumpCookie(st);
|
2010-01-28 14:01:50 +00:00
|
|
|
|
2013-06-27 15:25:06 +00:00
|
|
|
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());
|
2010-10-09 14:22:49 +00:00
|
|
|
|
|
|
|
c = cookies[1];
|
2013-06-27 15:25:06 +00:00
|
|
|
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());
|
2008-08-27 16:31:43 +00:00
|
|
|
|
2010-10-09 14:22:49 +00:00
|
|
|
c = cookies[2];
|
2013-06-27 15:25:06 +00:00
|
|
|
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());
|
2008-08-27 16:31:43 +00:00
|
|
|
|
2010-01-28 14:01:50 +00:00
|
|
|
c = cookies[3];
|
2013-06-27 15:25:06 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("TAX"), c->getName());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("1000"), c->getValue());
|
|
|
|
CPPUNIT_ASSERT((time_t)INT32_MAX <= c->getExpiryTime());
|
|
|
|
CPPUNIT_ASSERT(c->getPersistent());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("/"), c->getPath());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("overflow"), c->getDomain());
|
|
|
|
CPPUNIT_ASSERT(!c->getSecure());
|
2008-08-27 16:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CookieStorageTest::testLoad_sqlite3()
|
|
|
|
{
|
2013-06-27 15:25:06 +00:00
|
|
|
auto st = CookieStorage{};
|
2008-08-27 16:31:43 +00:00
|
|
|
#ifdef HAVE_SQLITE3
|
2015-12-27 09:39:47 +00:00
|
|
|
st.load(A2_TEST_DIR "/cookies.sqlite", 1000);
|
2010-10-09 14:22:49 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, st.size());
|
2013-06-27 15:25:06 +00:00
|
|
|
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());
|
2008-10-26 14:22:58 +00:00
|
|
|
|
2010-01-29 12:04:36 +00:00
|
|
|
c = cookies[1];
|
2013-06-27 15:25:06 +00:00
|
|
|
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());
|
2012-10-01 14:52:22 +00:00
|
|
|
|
2008-08-27 16:31:43 +00:00
|
|
|
#else // !HAVE_SQLITE3
|
2015-12-27 09:39:47 +00:00
|
|
|
CPPUNIT_ASSERT(!st.load(A2_TEST_DIR "/cookies.sqlite", 1000));
|
2008-08-27 16:31:43 +00:00
|
|
|
#endif // !HAVE_SQLITE3
|
|
|
|
}
|
|
|
|
|
|
|
|
void CookieStorageTest::testLoad_fileNotfound()
|
|
|
|
{
|
2013-06-27 15:25:06 +00:00
|
|
|
auto st = CookieStorage{};
|
2015-12-27 09:39:47 +00:00
|
|
|
CPPUNIT_ASSERT(
|
|
|
|
!st.load("./aria2_CookieStorageTest_testLoad_fileNotfound", 0));
|
2008-08-27 16:31:43 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 14:51:57 +00:00
|
|
|
void CookieStorageTest::testSaveNsFormat()
|
|
|
|
{
|
2010-01-28 14:01:50 +00:00
|
|
|
// TODO add cookie with default domain
|
2015-12-27 09:39:47 +00:00
|
|
|
std::string filename =
|
|
|
|
A2_TEST_OUT_DIR "/aria2_CookieStorageTest_testSaveNsFormat";
|
2009-05-22 14:51:57 +00:00
|
|
|
File(filename).remove();
|
2013-06-27 15:25:06 +00:00
|
|
|
auto st = CookieStorage{};
|
2010-10-09 14:22:49 +00:00
|
|
|
time_t now = 1000;
|
2015-12-27 09:39:47 +00:00
|
|
|
st.store(
|
|
|
|
createCookie("favorite", "classic", "domain.org", false, "/config", true),
|
|
|
|
now);
|
|
|
|
st.store(createCookie("uid", "tujikawa", now, "domain.org", true, "/", false),
|
|
|
|
now);
|
2011-08-05 13:44:54 +00:00
|
|
|
CPPUNIT_ASSERT(st.saveNsFormat(filename));
|
2013-06-27 15:25:06 +00:00
|
|
|
auto loadst = CookieStorage{};
|
2010-10-09 14:22:49 +00:00
|
|
|
loadst.load(filename, now);
|
2009-05-22 14:51:57 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, loadst.size());
|
2010-01-28 14:01:50 +00:00
|
|
|
|
2013-06-27 15:25:06 +00:00
|
|
|
auto cookies = dumpCookie(loadst);
|
2010-01-28 14:01:50 +00:00
|
|
|
|
2013-06-27 15:25:06 +00:00
|
|
|
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());
|
2009-05-22 14:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CookieStorageTest::testSaveNsFormat_fail()
|
|
|
|
{
|
2010-12-02 13:38:36 +00:00
|
|
|
std::string filename =
|
2015-12-27 09:39:47 +00:00
|
|
|
A2_TEST_OUT_DIR "/aria2_CookieStorageTest_testSaveNsFormat_fail";
|
2009-05-22 14:51:57 +00:00
|
|
|
File f(filename);
|
2010-02-15 14:01:11 +00:00
|
|
|
f.remove();
|
|
|
|
f.mkdirs();
|
|
|
|
CPPUNIT_ASSERT(f.isDir());
|
2013-06-27 15:25:06 +00:00
|
|
|
auto st = CookieStorage{};
|
2009-06-23 15:12:08 +00:00
|
|
|
CPPUNIT_ASSERT(!st.saveNsFormat(filename));
|
2009-05-22 14:51:57 +00:00
|
|
|
}
|
|
|
|
|
2010-01-28 14:01:50 +00:00
|
|
|
void CookieStorageTest::testCookieIsFull()
|
|
|
|
{
|
2013-06-27 15:25:06 +00:00
|
|
|
auto st = CookieStorage{};
|
2015-12-27 09:39:47 +00:00
|
|
|
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);
|
2010-01-28 14:01:50 +00:00
|
|
|
}
|
|
|
|
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
|
2013-06-27 15:25:06 +00:00
|
|
|
auto st = CookieStorage{};
|
2015-12-27 09:39:47 +00:00
|
|
|
for (size_t i = 0; i < 2001; ++i) {
|
|
|
|
st.store(createCookie("k", "v", "domain" + util::itos(i), true, "/", false),
|
|
|
|
0);
|
2010-01-28 14:01:50 +00:00
|
|
|
}
|
2010-07-08 15:20:21 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1801, st.size());
|
2010-01-28 14:01:50 +00:00
|
|
|
}
|
|
|
|
|
2013-06-27 15:25:06 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2008-08-27 16:04:36 +00:00
|
|
|
} // namespace aria2
|