summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2013-06-28 00:25:06 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2013-06-29 23:09:09 +0900
commit5a223115e09cd568169fe2efd71d09689a980335 (patch)
tree61250fc5f89229e4ecc63d70ae2069207388fea0 /test
parent0b1e05b13bfd7cfbf75b299c1f32cdd6e6aed953 (diff)
Rewrite CookieStorage
Diffstat (limited to 'test')
-rw-r--r--test/CookieHelperTest.cc150
-rw-r--r--test/CookieStorageTest.cc481
-rw-r--r--test/CookieTest.cc109
-rw-r--r--test/HttpRequestTest.cc31
-rw-r--r--test/HttpResponseTest.cc8
-rw-r--r--test/NsCookieParserTest.cc100
-rw-r--r--test/Sqlite3CookieParserTest.cc131
-rw-r--r--test/TestUtil.cc8
-rw-r--r--test/TestUtil.h25
9 files changed, 563 insertions, 480 deletions
diff --git a/test/CookieHelperTest.cc b/test/CookieHelperTest.cc
index 7574b3fe..a4c46612 100644
--- a/test/CookieHelperTest.cc
+++ b/test/CookieHelperTest.cc
@@ -88,145 +88,145 @@ void CookieHelperTest::testParse()
{
std::string str = "ID=123456789; expires=Sun, 10-Jun-2007 11:00:00 GMT;"
"path=/foo; domain=localhost; secure;httpOnly ";
- Cookie c;
- CPPUNIT_ASSERT(cookie::parse(c, str, "localhost", "/", creationDate));
- 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());
+ 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;";
- Cookie c;
- CPPUNIT_ASSERT(cookie::parse(c, str, "localhost", "/", creationDate));
- 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());
+ 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;";
- Cookie c;
- CPPUNIT_ASSERT(cookie::parse(c, str, "localhost", "/", creationDate));
- CPPUNIT_ASSERT_EQUAL((time_t)0, c.getExpiryTime());
- CPPUNIT_ASSERT(c.getPersistent());
+ 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;";
- Cookie c;
- CPPUNIT_ASSERT(cookie::parse(c, str, "localhost", "/", creationDate));
- CPPUNIT_ASSERT_EQUAL((time_t)creationDate+100, c.getExpiryTime());
- CPPUNIT_ASSERT(c.getPersistent());
+ 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;";
- Cookie c;
- CPPUNIT_ASSERT(cookie::parse(c, str, "localhost", "/", creationDate));
- CPPUNIT_ASSERT_EQUAL(std::numeric_limits<time_t>::max(), c.getExpiryTime());
- CPPUNIT_ASSERT(c.getPersistent());
+ 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;";
- Cookie c;
- CPPUNIT_ASSERT(!cookie::parse(c, str, "localhost", "/", creationDate));
+ CPPUNIT_ASSERT(!cookie::parse(str, "localhost", "/", creationDate));
}
{
std::string str = "id=; Max-Age=100garbage;";
- Cookie c;
- CPPUNIT_ASSERT(!cookie::parse(c, str, "localhost", "/", creationDate));
+ CPPUNIT_ASSERT(!cookie::parse(str, "localhost", "/", creationDate));
}
{
std::string str = "id=; Max-Age=100;expires=Sun, 10-Jun-2007 11:00:00 GMT;";
- Cookie c;
- CPPUNIT_ASSERT(cookie::parse(c, str, "localhost", "/", creationDate));
- CPPUNIT_ASSERT_EQUAL((time_t)creationDate+100, c.getExpiryTime());
- CPPUNIT_ASSERT(c.getPersistent());
+ 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;";
- Cookie c;
- CPPUNIT_ASSERT(!cookie::parse(c, str, "localhost", "/", creationDate));
+ CPPUNIT_ASSERT(!cookie::parse(str, "localhost", "/", creationDate));
}
{
std::string str = "id=;";
- Cookie c;
- CPPUNIT_ASSERT(cookie::parse(c, str, "localhost", "/", creationDate));
- CPPUNIT_ASSERT(!c.getPersistent());
+ auto c = cookie::parse(str, "localhost", "/", creationDate);
+ CPPUNIT_ASSERT(c);
+ CPPUNIT_ASSERT(!c->getPersistent());
}
{
std::string str = "id=; path=abc";
- Cookie c;
- CPPUNIT_ASSERT(cookie::parse(c, str, "localhost", "/", creationDate));
- CPPUNIT_ASSERT_EQUAL(std::string("/"), c.getPath());
+ auto c = cookie::parse(str, "localhost", "/", creationDate);
+ CPPUNIT_ASSERT(c);
+ CPPUNIT_ASSERT_EQUAL(std::string("/"), c->getPath());
}
{
std::string str = "id=; domain=.example.org";
- Cookie c;
- CPPUNIT_ASSERT(cookie::parse(c, str, "www.example.org", "/",creationDate));
+ 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";
- Cookie c;
- CPPUNIT_ASSERT(!cookie::parse(c, str, "example.org", "/", creationDate));
+ CPPUNIT_ASSERT(!cookie::parse(str, "example.org", "/", creationDate));
}
{
std::string str = "id=; domain=.";
- Cookie c;
- CPPUNIT_ASSERT(!cookie::parse(c, str, "localhost", "/",creationDate));
+ CPPUNIT_ASSERT(!cookie::parse(str, "localhost", "/",creationDate));
}
{
std::string str = "";
- Cookie c;
- CPPUNIT_ASSERT(!cookie::parse(c, str, "localhost", "/",creationDate));
+ CPPUNIT_ASSERT(!cookie::parse(str, "localhost", "/",creationDate));
}
{
std::string str = "=";
- Cookie c;
- CPPUNIT_ASSERT(!cookie::parse(c, str, "localhost", "/",creationDate));
+ CPPUNIT_ASSERT(!cookie::parse(str, "localhost", "/",creationDate));
}
{
// Use domain last time seen.
std::string str = "id=;domain=a.example.org;domain=.example.org";
- Cookie c;
- CPPUNIT_ASSERT(cookie::parse(c, str, "b.example.org", "/",creationDate));
- CPPUNIT_ASSERT_EQUAL(std::string("example.org"), c.getDomain());
+ 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=;";
- Cookie c;
- CPPUNIT_ASSERT(cookie::parse(c, str, "192.168.0.1", "/",creationDate));
- CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), c.getDomain());
- CPPUNIT_ASSERT(c.getHostOnly());
+ 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";
- Cookie c;
- CPPUNIT_ASSERT(cookie::parse(c, str, "192.168.0.1", "/",creationDate));
- CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), c.getDomain());
- CPPUNIT_ASSERT(c.getHostOnly());
+ 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\";";
- Cookie c;
- CPPUNIT_ASSERT(cookie::parse(c, str, "localhost", "/", creationDate));
- CPPUNIT_ASSERT_EQUAL(std::string("foo"), c.getValue());
+ 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());
}
}
diff --git a/test/CookieStorageTest.cc b/test/CookieStorageTest.cc
index 4740f969..e88332b8 100644
--- a/test/CookieStorageTest.cc
+++ b/test/CookieStorageTest.cc
@@ -12,6 +12,7 @@
#include "RecoverableException.h"
#include "File.h"
#include "TestUtil.h"
+#include "TimerA2.h"
namespace aria2 {
@@ -29,14 +30,13 @@ class CookieStorageTest:public CppUnit::TestFixture {
CPPUNIT_TEST(testSaveNsFormat_fail);
CPPUNIT_TEST(testCookieIsFull);
CPPUNIT_TEST(testDomainIsFull);
+ CPPUNIT_TEST(testEviction);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {}
void tearDown() {}
- void dumpCookie(std::vector<Cookie>& cookies, const CookieStorage& cs);
-
void testStore();
void testParseAndStore();
void testCriteriaFind();
@@ -48,56 +48,67 @@ public:
void testSaveNsFormat_fail();
void testCookieIsFull();
void testDomainIsFull();
+ void testEviction();
};
CPPUNIT_TEST_SUITE_REGISTRATION(CookieStorageTest);
-void CookieStorageTest::dumpCookie
-(std::vector<Cookie>& cookies, const CookieStorage& st)
+namespace {
+std::vector<const Cookie*> dumpCookie(const CookieStorage& st)
{
- st.dumpCookie(std::back_inserter(cookies));
- std::sort(cookies.begin(), cookies.end(), CookieSorter());
+ 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 = 1000;
- CookieStorage st;
- Cookie goodCookie(createCookie("k", "v", "localhost", true, "/", false));
- CPPUNIT_ASSERT(st.store(goodCookie, now));
+ auto st = CookieStorage{};
+ auto goodCookie = []() {
+ return createCookie("k", "v", "localhost", true, "/", false);
+ };
+ CPPUNIT_ASSERT(st.store(goodCookie(), now));
CPPUNIT_ASSERT_EQUAL((size_t)1, st.size());
- CPPUNIT_ASSERT(st.contains(goodCookie));
+ CPPUNIT_ASSERT(st.contains(*goodCookie()));
- Cookie anotherCookie(createCookie("k", "v", "mirror", true, "/", true));
- CPPUNIT_ASSERT(st.store(anotherCookie, now));
+ 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));
+ CPPUNIT_ASSERT(st.contains(*anotherCookie()));
+ CPPUNIT_ASSERT(st.contains(*goodCookie()));
- Cookie updateGoodCookie(createCookie("k", "v2", "localhost", true,
- "/", false));
- CPPUNIT_ASSERT(st.store(updateGoodCookie, now));
+ auto updateGoodCookie = []() {
+ return createCookie("k", "v2", "localhost", true, "/", false);
+ };
+ CPPUNIT_ASSERT(st.store(updateGoodCookie(), now));
CPPUNIT_ASSERT_EQUAL((size_t)2, st.size());
- CPPUNIT_ASSERT(st.contains(updateGoodCookie));
- CPPUNIT_ASSERT(st.contains(anotherCookie));
+ CPPUNIT_ASSERT(st.contains(*updateGoodCookie()));
+ CPPUNIT_ASSERT(st.contains(*anotherCookie()));
- Cookie expireGoodCookie(createCookie("k", "v3", 0, "localhost", true,
- "/", false));
- CPPUNIT_ASSERT(!st.store(expireGoodCookie, now));
+ 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));
+ CPPUNIT_ASSERT(st.contains(*anotherCookie()));
- Cookie fromNumericHost(createCookie("k", "v", "192.168.1.1", true,
- "/", false));
- CPPUNIT_ASSERT(st.store(fromNumericHost, now));
+ 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));
+ CPPUNIT_ASSERT(st.contains(*fromNumericHost()));
}
void CookieStorageTest::testParseAndStore()
{
- CookieStorage st;
+ auto st = CookieStorage{};
time_t now = 1000;
std::string localhostCookieStr = "k=v;"
" expires=Fri, 01 Jan 2038 00:00:00 GMT; path=/; domain=localhost;";
@@ -122,221 +133,233 @@ void CookieStorageTest::testParseAndStore()
void CookieStorageTest::testCriteriaFind()
{
- CookieStorage st;
+ auto st = CookieStorage{};
time_t now = 1000;
- Cookie alpha(createCookie("alpha", "ALPHA", "aria2.org", false, "/", false));
- Cookie bravo(createCookie("bravo", "BRAVO", 1060, "aria2.org", false,
- "/foo", false));
- Cookie charlie(createCookie("charlie", "CHARLIE", "aria2.org", false,
- "/", true));
- Cookie delta(createCookie("delta", "DELTA", "aria2.org", false,
- "/foo/bar", false));
- Cookie echo(createCookie("echo", "ECHO", "www.dl.aria2.org", false,
- "/", false));
- Cookie foxtrot(createCookie("foxtrot", "FOXTROT", "sf.net", false,
- "/", false));
- Cookie golf(createCookie("golf", "GOLF", "192.168.1.1", true,
- "/", false));
- Cookie hotel1(createCookie("hotel", "HOTEL1", "samename.x", false,
- "/", false));
- Cookie hotel2(createCookie("hotel", "HOTEL2", "samename.x", false,
- "/hotel", false));
- Cookie hotel3(createCookie("hotel", "HOTEL3", "samename.x", false,
- "/bar/wine", false));
- Cookie hotel4(createCookie("hotel", "HOTEL4", "samename.x", false,
- "/bar/", false));
- Cookie india1(createCookie("india", "INDIA1", "default.domain", true,
- "/foo", false));
- Cookie india2(createCookie("india", "INDIA2", "default.domain", false,
- "/", false));
- Cookie juliet1(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));
-
- std::vector<Cookie> aria2Slash = st.criteriaFind("www.dl.aria2.org", "/",
- 0, false);
+ 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(std::find(aria2Slash.begin(), aria2Slash.end(), alpha)
- != aria2Slash.end());
- CPPUNIT_ASSERT(std::find(aria2Slash.begin(), aria2Slash.end(), echo)
- != aria2Slash.end());
+ CPPUNIT_ASSERT(derefFind(aria2Slash, alpha()));
+ CPPUNIT_ASSERT(derefFind(aria2Slash, echo()));
- std::vector<Cookie> aria2SlashFoo = st.criteriaFind("www.dl.aria2.org","/foo",
- 0, false);
+ 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(std::find(aria2SlashFoo.begin(), aria2SlashFoo.end(), alpha)
- != aria2SlashFoo.end());
- CPPUNIT_ASSERT(std::find(aria2SlashFoo.begin(), aria2SlashFoo.end(), echo)
- != aria2SlashFoo.end());
-
- std::vector<Cookie> aria2Expires = st.criteriaFind("www.dl.aria2.org", "/foo",
- 1120,
- false);
+ 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(std::find(aria2Expires.begin(), aria2Expires.end(), alpha)
- != aria2Expires.end());
- CPPUNIT_ASSERT(std::find(aria2Expires.begin(), aria2Expires.end(), echo)
- != aria2Expires.end());
+ CPPUNIT_ASSERT(derefFind(aria2Expires, alpha()));
+ CPPUNIT_ASSERT(derefFind(aria2Expires, echo()));
- std::vector<Cookie> dlAria2 = st.criteriaFind("dl.aria2.org", "/", 0, false);
+ 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());
+ CPPUNIT_ASSERT_EQUAL(std::string("alpha"), dlAria2[0]->getName());
- std::vector<Cookie> tailmatchAria2 = st.criteriaFind("myaria2.org", "/", 0,
- false);
+ auto tailmatchAria2 = st.criteriaFind("myaria2.org", "/", 0, false);
CPPUNIT_ASSERT(tailmatchAria2.empty());
- std::vector<Cookie> numericHostCookies = st.criteriaFind("192.168.1.1", "/",0,
- false);
+ 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());
+ CPPUNIT_ASSERT_EQUAL(std::string("golf"), numericHostCookies[0]->getName());
- std::vector<Cookie> sameNameCookies =
- st.criteriaFind("samename.x", "/bar/wine", 0, false);
+ 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());
+ 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());
- std::vector<Cookie> defaultDomainCookies =
- st.criteriaFind("default.domain", "/foo", 0, false);
+ 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());
+ defaultDomainCookies[0]->getValue());
CPPUNIT_ASSERT_EQUAL(std::string("INDIA2"),
- defaultDomainCookies[1].getValue());
+ 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());
+ defaultDomainCookies[0]->getValue());
// localhost.local case
- std::vector<Cookie> localDomainCookies =
- st.criteriaFind("localhost", "/foo", 0, false);
+ 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());
+ localDomainCookies[0]->getValue());
}
void CookieStorageTest::testCriteriaFind_cookieOrder()
{
- CookieStorage st;
- Cookie a(createCookie("a", "0", "host", true, "/", false));
- a.setCreationTime(1000);
- Cookie b(createCookie("b", "0", "host", true, "/foo", false));
- b.setCreationTime(5000);
- Cookie c(createCookie("c", "0", "host", true, "/foo", false));
- c.setCreationTime(4000);
- Cookie d(createCookie("d", "0", "host", true, "/foo/bar", false));
- d.setCreationTime(6000);
-
- st.store(a, 0);
- st.store(b, 0);
- st.store(c, 0);
- st.store(d, 0);
-
- std::vector<Cookie> cookies = st.criteriaFind("host", "/foo/bar", 0, false);
+ 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());
+ 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()
{
- CookieStorage st;
+ auto st = CookieStorage{};
st.load(A2_TEST_DIR"/nscookietest.txt", 1001);
CPPUNIT_ASSERT_EQUAL((size_t)4, st.size());
- std::vector<Cookie> cookies;
- dumpCookie(cookies, st);
+ auto cookies = dumpCookie(st);
- Cookie 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());
+ 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());
+ 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());
+ 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((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());
+ 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());
}
void CookieStorageTest::testLoad_sqlite3()
{
- CookieStorage st;
+ auto st = CookieStorage{};
#ifdef HAVE_SQLITE3
st.load(A2_TEST_DIR"/cookies.sqlite", 1000);
CPPUNIT_ASSERT_EQUAL((size_t)2, st.size());
- std::vector<Cookie> cookies;
- dumpCookie(cookies, st);
- Cookie 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());
+ 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());
+ 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));
@@ -345,7 +368,7 @@ void CookieStorageTest::testLoad_sqlite3()
void CookieStorageTest::testLoad_fileNotfound()
{
- CookieStorage st;
+ auto st = CookieStorage{};
CPPUNIT_ASSERT(!st.load("./aria2_CookieStorageTest_testLoad_fileNotfound",0));
}
@@ -354,22 +377,22 @@ void CookieStorageTest::testSaveNsFormat()
// TODO add cookie with default domain
std::string filename = A2_TEST_OUT_DIR"/aria2_CookieStorageTest_testSaveNsFormat";
File(filename).remove();
- CookieStorage st;
+ auto st = CookieStorage{};
time_t now = 1000;
- st.store(Cookie(createCookie("favorite", "classic", "domain.org", false,
- "/config",true)), now);
- st.store(Cookie(createCookie("uid", "tujikawa", now, "domain.org", true,
- "/",false)), now);
+ 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));
- CookieStorage loadst;
+ auto loadst = CookieStorage{};
loadst.load(filename, now);
CPPUNIT_ASSERT_EQUAL((size_t)2, loadst.size());
- std::vector<Cookie> cookies;
- dumpCookie(cookies, loadst);
+ auto cookies = dumpCookie(loadst);
- CPPUNIT_ASSERT_EQUAL(std::string("favorite"), cookies[0].getName());
- CPPUNIT_ASSERT_EQUAL(std::string("uid"), cookies[1].getName());
+ 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()
@@ -380,17 +403,16 @@ void CookieStorageTest::testSaveNsFormat_fail()
f.remove();
f.mkdirs();
CPPUNIT_ASSERT(f.isDir());
- CookieStorage st;
+ auto st = CookieStorage{};
CPPUNIT_ASSERT(!st.saveNsFormat(filename));
}
void CookieStorageTest::testCookieIsFull()
{
- CookieStorage st;
+ auto st = CookieStorage{};
for(size_t i = 0; i < CookieStorage::MAX_COOKIE_PER_DOMAIN+1; ++i) {
- Cookie c(createCookie("k"+util::itos(i), "v", "aria2.org", false,
- "/", false));
- st.store(c, 0);
+ 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());
}
@@ -399,13 +421,58 @@ void CookieStorageTest::testDomainIsFull()
{
// See DOMAIN_EVICTION_TRIGGER and DOMAIN_EVICTION_RATE in
// CookieStorage.cc
- CookieStorage st;
+ auto st = CookieStorage{};
for(size_t i = 0; i < 2001; ++i) {
- Cookie c(createCookie("k", "v", "domain"+util::itos(i), true,
- "/", false));
- st.store(c, 0);
+ 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
diff --git a/test/CookieTest.cc b/test/CookieTest.cc
index bf8f6595..d4d0c09e 100644
--- a/test/CookieTest.cc
+++ b/test/CookieTest.cc
@@ -35,67 +35,66 @@ CPPUNIT_TEST_SUITE_REGISTRATION(CookieTest);
void CookieTest::testOperatorEqual()
{
- Cookie a(createCookie("k", "v", "localhost", true, "/", false));
- Cookie b(createCookie("k", "v", "localhost", true, "/", true));
- Cookie wrongPath(createCookie("k", "v", "localhost", true, "/a", false));
- Cookie wrongDomain(createCookie("k", "v", "mydomain", true, "/", false));
- Cookie wrongName(createCookie("h", "v", "localhost", true, "/a", false));
- Cookie caseSensitiveName(createCookie("K", "v", "localhost", true,
- "/a", false));
- CPPUNIT_ASSERT(a == b);
- CPPUNIT_ASSERT(!(a == wrongPath));
- CPPUNIT_ASSERT(!(a == wrongDomain));
- CPPUNIT_ASSERT(!(a == wrongName));
- CPPUNIT_ASSERT(!(a == caseSensitiveName));
+ auto a = createCookie("k", "v", "localhost", true, "/", false);
+ auto b = createCookie("k", "v", "localhost", true, "/", true);
+ auto wrongPath = createCookie("k", "v", "localhost", true, "/a", false);
+ auto wrongDomain = createCookie("k", "v", "mydomain", true, "/", false);
+ auto wrongName = createCookie("h", "v", "localhost", true, "/a", false);
+ auto caseSensitiveName = createCookie("K", "v", "localhost", true,
+ "/a", false);
+ CPPUNIT_ASSERT(*a == *b);
+ CPPUNIT_ASSERT(!(*a == *wrongPath));
+ CPPUNIT_ASSERT(!(*a == *wrongDomain));
+ CPPUNIT_ASSERT(!(*a == *wrongName));
+ CPPUNIT_ASSERT(!(*a == *caseSensitiveName));
}
void CookieTest::testMatch()
{
- Cookie c(createCookie("k", "v", "aria2.org", false, "/downloads", false));
- Cookie c2(createCookie("k", "v", "aria2.org", false, "/downloads", false));
- Cookie c3(createCookie("k", "v", "aria2.org", true, "/downloads", false));
- Cookie c4(createCookie("k", "v", "localhost", true, "/downloads", false));
- CPPUNIT_ASSERT(c.match("www.aria2.org", "/downloads", 0, false));
- CPPUNIT_ASSERT(c2.match("www.aria2.org", "/downloads", 0, false));
- CPPUNIT_ASSERT(!c.match("www.aria.org", "/downloads", 0, false));
- CPPUNIT_ASSERT(!c.match("www.aria2.org", "/examples", 0, false));
- CPPUNIT_ASSERT(c.match("www.aria2.org", "/downloads", 0, true));
- CPPUNIT_ASSERT(c.match("www.aria2.org", "/downloads/latest", 0, false));
- CPPUNIT_ASSERT(!c.match("www.aria2.org", "/downloadss/latest", 0, false));
- CPPUNIT_ASSERT(!c.match("www.aria2.org", "/DOWNLOADS", 0, false));
- CPPUNIT_ASSERT(!c3.match("www.aria2.org", "/downloads", 0, false));
- CPPUNIT_ASSERT(c4.match("localhost", "/downloads", 0, false));
-
- Cookie secureCookie(createCookie("k", "v", "secure.aria2.org", false,
- "/", true));
- CPPUNIT_ASSERT(secureCookie.match("secure.aria2.org", "/", 0, true));
- CPPUNIT_ASSERT(!secureCookie.match("secure.aria2.org", "/", 0, false));
- CPPUNIT_ASSERT(!secureCookie.match("ssecure.aria2.org", "/", 0, true));
- CPPUNIT_ASSERT(secureCookie.match("www.secure.aria2.org", "/", 0, true));
-
- Cookie expireTest(createCookie("k", "v", 1000, "aria2.org", false,
- "/", false));
- CPPUNIT_ASSERT(expireTest.match("www.aria2.org", "/", 999, false));
- CPPUNIT_ASSERT(expireTest.match("www.aria2.org", "/", 1000, false));
- CPPUNIT_ASSERT(!expireTest.match("www.aria2.org", "/", 1001, false));
-
- Cookie fromNumericHost(createCookie("k", "v", "192.168.1.1", true,
- "/foo", false));
- CPPUNIT_ASSERT(fromNumericHost.match("192.168.1.1", "/foo", 0, false));
- CPPUNIT_ASSERT(!fromNumericHost.match("www.aria2.org", "/foo", 0, false));
- CPPUNIT_ASSERT(!fromNumericHost.match("1.192.168.1.1", "/foo", 0, false));
- CPPUNIT_ASSERT(!fromNumericHost.match("192.168.1.1", "/", 0, false));
+ auto c = createCookie("k", "v", "aria2.org", false, "/downloads", false);
+ auto c2 = createCookie("k", "v", "aria2.org", false, "/downloads", false);
+ auto c3 = createCookie("k", "v", "aria2.org", true, "/downloads", false);
+ auto c4 = createCookie("k", "v", "localhost", true, "/downloads", false);
+ CPPUNIT_ASSERT(c->match("www.aria2.org", "/downloads", 0, false));
+ CPPUNIT_ASSERT(c2->match("www.aria2.org", "/downloads", 0, false));
+ CPPUNIT_ASSERT(!c->match("www.aria.org", "/downloads", 0, false));
+ CPPUNIT_ASSERT(!c->match("www.aria2.org", "/examples", 0, false));
+ CPPUNIT_ASSERT(c->match("www.aria2.org", "/downloads", 0, true));
+ CPPUNIT_ASSERT(c->match("www.aria2.org", "/downloads/latest", 0, false));
+ CPPUNIT_ASSERT(!c->match("www.aria2.org", "/downloadss/latest", 0, false));
+ CPPUNIT_ASSERT(!c->match("www.aria2.org", "/DOWNLOADS", 0, false));
+ CPPUNIT_ASSERT(!c3->match("www.aria2.org", "/downloads", 0, false));
+ CPPUNIT_ASSERT(c4->match("localhost", "/downloads", 0, false));
+
+ auto secureCookie = createCookie("k", "v", "secure.aria2.org", false,
+ "/", true);
+ CPPUNIT_ASSERT(secureCookie->match("secure.aria2.org", "/", 0, true));
+ CPPUNIT_ASSERT(!secureCookie->match("secure.aria2.org", "/", 0, false));
+ CPPUNIT_ASSERT(!secureCookie->match("ssecure.aria2.org", "/", 0, true));
+ CPPUNIT_ASSERT(secureCookie->match("www.secure.aria2.org", "/", 0, true));
+
+ auto expireTest = createCookie("k", "v", 1000, "aria2.org", false,
+ "/", false);
+ CPPUNIT_ASSERT(expireTest->match("www.aria2.org", "/", 999, false));
+ CPPUNIT_ASSERT(expireTest->match("www.aria2.org", "/", 1000, false));
+ CPPUNIT_ASSERT(!expireTest->match("www.aria2.org", "/", 1001, false));
+
+ auto fromNumericHost = createCookie("k", "v", "192.168.1.1", true,
+ "/foo", false);
+ CPPUNIT_ASSERT(fromNumericHost->match("192.168.1.1", "/foo", 0, false));
+ CPPUNIT_ASSERT(!fromNumericHost->match("www.aria2.org", "/foo", 0, false));
+ CPPUNIT_ASSERT(!fromNumericHost->match("1.192.168.1.1", "/foo", 0, false));
+ CPPUNIT_ASSERT(!fromNumericHost->match("192.168.1.1", "/", 0, false));
}
void CookieTest::testIsExpired()
{
- Cookie cookie(createCookie("k", "v", 1000, "localhost", true,
- "/", false));
- CPPUNIT_ASSERT(cookie.isExpired(1001));
- CPPUNIT_ASSERT(!cookie.isExpired(1000));
- CPPUNIT_ASSERT(!cookie.isExpired(999));
- Cookie sessionCookie(createCookie("k", "v", "localhost", true, "/", false));
- CPPUNIT_ASSERT(!sessionCookie.isExpired(INT32_MAX));
+ auto cookie = createCookie("k", "v", 1000, "localhost", true, "/", false);
+ CPPUNIT_ASSERT(cookie->isExpired(1001));
+ CPPUNIT_ASSERT(!cookie->isExpired(1000));
+ CPPUNIT_ASSERT(!cookie->isExpired(999));
+ auto sessionCookie = createCookie("k", "v", "localhost", true, "/", false);
+ CPPUNIT_ASSERT(!sessionCookie->isExpired(INT32_MAX));
}
void CookieTest::testToNsCookieFormat()
@@ -103,12 +102,12 @@ void CookieTest::testToNsCookieFormat()
CPPUNIT_ASSERT_EQUAL
(std::string(".domain.org\tTRUE\t/\tFALSE\t12345678\thello\tworld"),
createCookie("hello", "world", 12345678, "domain.org", false, "/",false)
- .toNsCookieFormat());
+ ->toNsCookieFormat());
// Session cookie
CPPUNIT_ASSERT_EQUAL
(std::string("domain.org\tFALSE\t/\tTRUE\t0\thello\tworld"),
createCookie("hello", "world", "domain.org", true, "/", true)
- .toNsCookieFormat());
+ ->toNsCookieFormat());
}
} // namespace aria2
diff --git a/test/HttpRequestTest.cc b/test/HttpRequestTest.cc
index 5beb239b..6d93d4ae 100644
--- a/test/HttpRequestTest.cc
+++ b/test/HttpRequestTest.cc
@@ -368,6 +368,13 @@ void HttpRequestTest::testCreateRequest_ftp()
CPPUNIT_ASSERT_EQUAL(expectedText, httpRequest.createRequest());
}
+template<typename InputIterator>
+void foo(CookieStorage& st, InputIterator first, InputIterator last, time_t t)
+{
+ for(; first != last; ++first) {
+ st.store(*first, t);
+ }
+}
void HttpRequestTest::testCreateRequest_with_cookie()
{
auto request = std::make_shared<Request>();
@@ -376,19 +383,17 @@ void HttpRequestTest::testCreateRequest_with_cookie()
auto segment = std::make_shared<PiecedSegment>(1024*1024, p);
auto fileEntry = std::make_shared<FileEntry>("file", 1024*1024*10, 0);
- std::vector<Cookie> cookies {
- createCookie("name1", "value1", "localhost", true, "/archives", false),
- createCookie("name2", "value2", "localhost", true,
- "/archives/download", false),
- createCookie("name3", "value3", "aria2.org", false,
- "/archives/download", false),
- createCookie("name4", "value4", "aria2.org", false, "/archives/", true),
- createCookie("name5", "value5", "example.org", false, "/", false)
- };
- CookieStorage st;
- for(auto c : cookies) {
- CPPUNIT_ASSERT(st.store(c, 0));
- }
+ auto st = CookieStorage{};
+ CPPUNIT_ASSERT(st.store(createCookie("name1", "value1", "localhost", true,
+ "/archives", false), 0));
+ CPPUNIT_ASSERT(st.store(createCookie("name2", "value2", "localhost", true,
+ "/archives/download", false), 0));
+ CPPUNIT_ASSERT(st.store(createCookie("name3", "value3", "aria2.org", false,
+ "/archives/download", false), 0));
+ CPPUNIT_ASSERT(st.store(createCookie("name4", "value4", "aria2.org", false,
+ "/archives/", true), 0));
+ CPPUNIT_ASSERT(st.store(createCookie("name5", "value5", "example.org", false,
+ "/", false), 0));
HttpRequest httpRequest;
diff --git a/test/HttpResponseTest.cc b/test/HttpResponseTest.cc
index 08892144..ce6589f4 100644
--- a/test/HttpResponseTest.cc
+++ b/test/HttpResponseTest.cc
@@ -4,6 +4,7 @@
#include <cppunit/extensions/HelperMacros.h>
+#include "TestUtil.h"
#include "prefs.h"
#include "PiecedSegment.h"
#include "Piece.h"
@@ -511,10 +512,11 @@ void HttpResponseTest::testRetrieveCookie()
CPPUNIT_ASSERT_EQUAL((size_t)2, st.size());
- std::vector<Cookie> cookies;
+ auto cookies = std::vector<const Cookie*>{};
st.dumpCookie(std::back_inserter(cookies));
- CPPUNIT_ASSERT_EQUAL(std::string("k2=v2"), cookies[0].toString());
- CPPUNIT_ASSERT_EQUAL(std::string("k3=v3"), cookies[1].toString());
+ std::sort(std::begin(cookies), std::end(cookies), CookieSorter());
+ CPPUNIT_ASSERT_EQUAL(std::string("k2=v2"), cookies[0]->toString());
+ CPPUNIT_ASSERT_EQUAL(std::string("k3=v3"), cookies[1]->toString());
}
void HttpResponseTest::testSupportsPersistentConnection()
diff --git a/test/NsCookieParserTest.cc b/test/NsCookieParserTest.cc
index 832badca..33003fc7 100644
--- a/test/NsCookieParserTest.cc
+++ b/test/NsCookieParserTest.cc
@@ -33,58 +33,58 @@ void NsCookieParserTest::testParse()
{
NsCookieParser parser;
time_t now = 0;
- std::vector<Cookie> cookies = parser.parse(A2_TEST_DIR"/nscookietest.txt", now);
+ auto cookies = parser.parse(A2_TEST_DIR"/nscookietest.txt", now);
CPPUNIT_ASSERT_EQUAL((size_t)5, cookies.size());
- Cookie 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("user"), c.getName());
- CPPUNIT_ASSERT_EQUAL(std::string("me"), c.getValue());
- CPPUNIT_ASSERT_EQUAL((time_t)1000, c.getExpiryTime());
- CPPUNIT_ASSERT(c.getPersistent());
- CPPUNIT_ASSERT_EQUAL(std::string("expired"), c.getDomain());
- CPPUNIT_ASSERT(c.getHostOnly());
- CPPUNIT_ASSERT_EQUAL(std::string("/"), c.getPath());
- CPPUNIT_ASSERT(!c.getSecure());
-
- c = cookies[2];
- 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("192.168.0.1"), c.getDomain());
- CPPUNIT_ASSERT(c.getHostOnly());
- CPPUNIT_ASSERT_EQUAL(std::string("/cgi-bin"), c.getPath());
- 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((time_t)INT32_MAX <= c.getExpiryTime());
- CPPUNIT_ASSERT(c.getPersistent());
- CPPUNIT_ASSERT_EQUAL(std::string("overflow"), c.getDomain());
- CPPUNIT_ASSERT(c.getHostOnly());
- CPPUNIT_ASSERT_EQUAL(std::string("/"), c.getPath());
- CPPUNIT_ASSERT(!c.getSecure());
-
- c = cookies[4];
- CPPUNIT_ASSERT_EQUAL(std::string("novalue"), c.getName());
- CPPUNIT_ASSERT_EQUAL(std::string(""), c.getValue());
- CPPUNIT_ASSERT_EQUAL((time_t)INT32_MAX, c.getExpiryTime());
- CPPUNIT_ASSERT(c.getPersistent());
- CPPUNIT_ASSERT_EQUAL(std::string("example.org"), c.getDomain());
- CPPUNIT_ASSERT(!c.getHostOnly());
- CPPUNIT_ASSERT_EQUAL(std::string("/"), c.getPath());
- CPPUNIT_ASSERT(!c.getSecure());
+ auto c = cookies[0].get();
+ 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].get();
+ CPPUNIT_ASSERT_EQUAL(std::string("user"), c->getName());
+ CPPUNIT_ASSERT_EQUAL(std::string("me"), c->getValue());
+ CPPUNIT_ASSERT_EQUAL((time_t)1000, c->getExpiryTime());
+ CPPUNIT_ASSERT(c->getPersistent());
+ CPPUNIT_ASSERT_EQUAL(std::string("expired"), c->getDomain());
+ CPPUNIT_ASSERT(c->getHostOnly());
+ CPPUNIT_ASSERT_EQUAL(std::string("/"), c->getPath());
+ CPPUNIT_ASSERT(!c->getSecure());
+
+ c = cookies[2].get();
+ 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("192.168.0.1"), c->getDomain());
+ CPPUNIT_ASSERT(c->getHostOnly());
+ CPPUNIT_ASSERT_EQUAL(std::string("/cgi-bin"), c->getPath());
+ CPPUNIT_ASSERT(!c->getSecure());
+
+ c = cookies[3].get();
+ 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("overflow"), c->getDomain());
+ CPPUNIT_ASSERT(c->getHostOnly());
+ CPPUNIT_ASSERT_EQUAL(std::string("/"), c->getPath());
+ CPPUNIT_ASSERT(!c->getSecure());
+
+ c = cookies[4].get();
+ CPPUNIT_ASSERT_EQUAL(std::string("novalue"), c->getName());
+ CPPUNIT_ASSERT_EQUAL(std::string(""), c->getValue());
+ CPPUNIT_ASSERT_EQUAL((time_t)INT32_MAX, c->getExpiryTime());
+ CPPUNIT_ASSERT(c->getPersistent());
+ CPPUNIT_ASSERT_EQUAL(std::string("example.org"), c->getDomain());
+ CPPUNIT_ASSERT(!c->getHostOnly());
+ CPPUNIT_ASSERT_EQUAL(std::string("/"), c->getPath());
+ CPPUNIT_ASSERT(!c->getSecure());
}
void NsCookieParserTest::testParse_fileNotFound()
diff --git a/test/Sqlite3CookieParserTest.cc b/test/Sqlite3CookieParserTest.cc
index 6f492082..b9998ee6 100644
--- a/test/Sqlite3CookieParserTest.cc
+++ b/test/Sqlite3CookieParserTest.cc
@@ -35,55 +35,53 @@ CPPUNIT_TEST_SUITE_REGISTRATION(Sqlite3CookieParserTest);
void Sqlite3CookieParserTest::testMozParse()
{
- Sqlite3MozCookieParser parser(A2_TEST_DIR"/cookies.sqlite");
- std::vector<Cookie> cookies;
- parser.parse(cookies);
+ auto parser = Sqlite3MozCookieParser{A2_TEST_DIR"/cookies.sqlite"};
+ auto cookies = parser.parse();
CPPUNIT_ASSERT_EQUAL((size_t)3, cookies.size());
- const Cookie& localhost = cookies[0];
- CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), localhost.getName());
- CPPUNIT_ASSERT_EQUAL(std::string("123456789"), localhost.getValue());
- CPPUNIT_ASSERT_EQUAL((time_t)INT32_MAX, localhost.getExpiryTime());
- CPPUNIT_ASSERT(localhost.getPersistent());
- CPPUNIT_ASSERT_EQUAL(std::string("localhost"), localhost.getDomain());
- CPPUNIT_ASSERT(localhost.getHostOnly());
- CPPUNIT_ASSERT_EQUAL(std::string("/"), localhost.getPath());
- CPPUNIT_ASSERT(localhost.getSecure());
- CPPUNIT_ASSERT_EQUAL((time_t)3000, localhost.getLastAccessTime());
- CPPUNIT_ASSERT_EQUAL((time_t)3000, localhost.getCreationTime());
-
- const Cookie& nullValue = cookies[1];
- CPPUNIT_ASSERT_EQUAL(std::string("uid"), nullValue.getName());
- CPPUNIT_ASSERT_EQUAL(std::string(""), nullValue.getValue());
- CPPUNIT_ASSERT_EQUAL((time_t)0, nullValue.getExpiryTime());
- CPPUNIT_ASSERT(nullValue.getPersistent());
- CPPUNIT_ASSERT_EQUAL(std::string("null_value.com"), nullValue.getDomain());
- CPPUNIT_ASSERT(!nullValue.getHostOnly());
- CPPUNIT_ASSERT_EQUAL(std::string("/path/to"), nullValue.getPath());
- CPPUNIT_ASSERT(!nullValue.getSecure());
+ auto& localhost = cookies[0];
+ CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), localhost->getName());
+ CPPUNIT_ASSERT_EQUAL(std::string("123456789"), localhost->getValue());
+ CPPUNIT_ASSERT_EQUAL((time_t)INT32_MAX, localhost->getExpiryTime());
+ CPPUNIT_ASSERT(localhost->getPersistent());
+ CPPUNIT_ASSERT_EQUAL(std::string("localhost"), localhost->getDomain());
+ CPPUNIT_ASSERT(localhost->getHostOnly());
+ CPPUNIT_ASSERT_EQUAL(std::string("/"), localhost->getPath());
+ CPPUNIT_ASSERT(localhost->getSecure());
+ CPPUNIT_ASSERT_EQUAL((time_t)3000, localhost->getLastAccessTime());
+ CPPUNIT_ASSERT_EQUAL((time_t)3000, localhost->getCreationTime());
+
+ auto& nullValue = cookies[1];
+ CPPUNIT_ASSERT_EQUAL(std::string("uid"), nullValue->getName());
+ CPPUNIT_ASSERT_EQUAL(std::string(""), nullValue->getValue());
+ CPPUNIT_ASSERT_EQUAL((time_t)0, nullValue->getExpiryTime());
+ CPPUNIT_ASSERT(nullValue->getPersistent());
+ CPPUNIT_ASSERT_EQUAL(std::string("null_value.com"), nullValue->getDomain());
+ CPPUNIT_ASSERT(!nullValue->getHostOnly());
+ CPPUNIT_ASSERT_EQUAL(std::string("/path/to"), nullValue->getPath());
+ CPPUNIT_ASSERT(!nullValue->getSecure());
// See row id=3 has no name, so it is skipped.
- const Cookie& overflowTime = cookies[2];
- CPPUNIT_ASSERT_EQUAL(std::string("foo"), overflowTime.getName());
- CPPUNIT_ASSERT_EQUAL(std::string("bar"), overflowTime.getValue());
- CPPUNIT_ASSERT((time_t)INT32_MAX <= overflowTime.getExpiryTime());
- CPPUNIT_ASSERT(overflowTime.getPersistent());
+ auto& overflowTime = cookies[2];
+ CPPUNIT_ASSERT_EQUAL(std::string("foo"), overflowTime->getName());
+ CPPUNIT_ASSERT_EQUAL(std::string("bar"), overflowTime->getValue());
+ CPPUNIT_ASSERT((time_t)INT32_MAX <= overflowTime->getExpiryTime());
+ CPPUNIT_ASSERT(overflowTime->getPersistent());
CPPUNIT_ASSERT_EQUAL(std::string("overflow.time_t.org"),
- overflowTime.getDomain());
- CPPUNIT_ASSERT(!overflowTime.getHostOnly());
- CPPUNIT_ASSERT_EQUAL(std::string("/path/to"), overflowTime.getPath());
- CPPUNIT_ASSERT(!overflowTime.getSecure());
+ overflowTime->getDomain());
+ CPPUNIT_ASSERT(!overflowTime->getHostOnly());
+ CPPUNIT_ASSERT_EQUAL(std::string("/path/to"), overflowTime->getPath());
+ CPPUNIT_ASSERT(!overflowTime->getSecure());
// See row id=5 has bad path, so it is skipped.
}
void Sqlite3CookieParserTest::testMozParse_fileNotFound()
{
- Sqlite3MozCookieParser parser("fileNotFound");
+ auto parser = Sqlite3MozCookieParser{"fileNotFound"};
try {
- std::vector<Cookie> cookies;
- parser.parse(cookies);
+ parser.parse();
CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) {
// SUCCESS
@@ -95,10 +93,9 @@ void Sqlite3CookieParserTest::testMozParse_fileNotFound()
void Sqlite3CookieParserTest::testMozParse_badfile()
{
- Sqlite3MozCookieParser parser(A2_TEST_DIR"/badcookies.sqlite");
+ auto parser = Sqlite3MozCookieParser{A2_TEST_DIR"/badcookies.sqlite"};
try {
- std::vector<Cookie> cookies;
- parser.parse(cookies);
+ parser.parse();
CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) {
// SUCCESS
@@ -107,37 +104,37 @@ void Sqlite3CookieParserTest::testMozParse_badfile()
void Sqlite3CookieParserTest::testChromumParse()
{
- Sqlite3ChromiumCookieParser parser(A2_TEST_DIR"/chromium_cookies.sqlite");
- std::vector<Cookie> cookies;
- parser.parse(cookies);
+ auto parser = Sqlite3ChromiumCookieParser
+ {A2_TEST_DIR"/chromium_cookies.sqlite"};
+ auto cookies = parser.parse();
CPPUNIT_ASSERT_EQUAL((size_t)3, cookies.size());
- const Cookie& sfnet = cookies[0];
- CPPUNIT_ASSERT_EQUAL(std::string("mykey"), sfnet.getName());
- CPPUNIT_ASSERT_EQUAL(std::string("pass"), sfnet.getValue());
- CPPUNIT_ASSERT_EQUAL((time_t)12345679, sfnet.getExpiryTime());
- CPPUNIT_ASSERT(sfnet.getPersistent());
+ auto& sfnet = cookies[0];
+ CPPUNIT_ASSERT_EQUAL(std::string("mykey"), sfnet->getName());
+ CPPUNIT_ASSERT_EQUAL(std::string("pass"), sfnet->getValue());
+ CPPUNIT_ASSERT_EQUAL((time_t)12345679, sfnet->getExpiryTime());
+ CPPUNIT_ASSERT(sfnet->getPersistent());
CPPUNIT_ASSERT_EQUAL(std::string("aria2.sourceforge.net"),
- sfnet.getDomain());
- CPPUNIT_ASSERT(!sfnet.getHostOnly());
- CPPUNIT_ASSERT_EQUAL(std::string("/"), sfnet.getPath());
- CPPUNIT_ASSERT(!sfnet.getSecure());
-
- const Cookie& sfjp = cookies[1];
- CPPUNIT_ASSERT_EQUAL(std::string("myseckey"), sfjp.getName());
- CPPUNIT_ASSERT_EQUAL(std::string("pass2"), sfjp.getValue());
- CPPUNIT_ASSERT_EQUAL((time_t)0, sfjp.getExpiryTime());
- CPPUNIT_ASSERT(sfjp.getPersistent());
- CPPUNIT_ASSERT_EQUAL(std::string("aria2.sourceforge.jp"), sfjp.getDomain());
- CPPUNIT_ASSERT(sfjp.getHostOnly());
- CPPUNIT_ASSERT_EQUAL(std::string("/profile"), sfjp.getPath());
- CPPUNIT_ASSERT(sfjp.getSecure());
-
- const Cookie& localnet = cookies[2];
- CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), localnet.getDomain());
- CPPUNIT_ASSERT(sfjp.getHostOnly());
- CPPUNIT_ASSERT_EQUAL((time_t)3000, localnet.getLastAccessTime());
- CPPUNIT_ASSERT_EQUAL((time_t)3000, localnet.getCreationTime());
+ sfnet->getDomain());
+ CPPUNIT_ASSERT(!sfnet->getHostOnly());
+ CPPUNIT_ASSERT_EQUAL(std::string("/"), sfnet->getPath());
+ CPPUNIT_ASSERT(!sfnet->getSecure());
+
+ auto& sfjp = cookies[1];
+ CPPUNIT_ASSERT_EQUAL(std::string("myseckey"), sfjp->getName());
+ CPPUNIT_ASSERT_EQUAL(std::string("pass2"), sfjp->getValue());
+ CPPUNIT_ASSERT_EQUAL((time_t)0, sfjp->getExpiryTime());
+ CPPUNIT_ASSERT(sfjp->getPersistent());
+ CPPUNIT_ASSERT_EQUAL(std::string("aria2.sourceforge.jp"), sfjp->getDomain());
+ CPPUNIT_ASSERT(sfjp->getHostOnly());
+ CPPUNIT_ASSERT_EQUAL(std::string("/profile"), sfjp->getPath());
+ CPPUNIT_ASSERT(sfjp->getSecure());
+
+ auto& localnet = cookies[2];
+ CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), localnet->getDomain());
+ CPPUNIT_ASSERT(localnet->getHostOnly());
+ CPPUNIT_ASSERT_EQUAL((time_t)3000, localnet->getLastAccessTime());
+ CPPUNIT_ASSERT_EQUAL((time_t)3000, localnet->getCreationTime());
}
} // namespace aria2
diff --git a/test/TestUtil.cc b/test/TestUtil.cc
index 74d2800c..f3ca8e03 100644
--- a/test/TestUtil.cc
+++ b/test/TestUtil.cc
@@ -50,7 +50,7 @@ std::string readFile(const std::string& path)
return ss.str();
}
-Cookie createCookie
+std::unique_ptr<Cookie> createCookie
(const std::string& name,
const std::string& value,
const std::string& domain,
@@ -58,11 +58,11 @@ Cookie createCookie
const std::string& path,
bool secure)
{
- return Cookie
+ return make_unique<Cookie>
(name, value, 0, false, domain, hostOnly, path, secure, false, 0);
}
-Cookie createCookie
+std::unique_ptr<Cookie> createCookie
(const std::string& name,
const std::string& value,
time_t expiryTime,
@@ -71,7 +71,7 @@ Cookie createCookie
const std::string& path,
bool secure)
{
- return Cookie
+ return make_unique<Cookie>
(name, value, expiryTime, true, domain, hostOnly, path, secure, false, 0);
}
diff --git a/test/TestUtil.h b/test/TestUtil.h
index 51bff38d..b1e95ad8 100644
--- a/test/TestUtil.h
+++ b/test/TestUtil.h
@@ -21,17 +21,17 @@ std::string readFile(const std::string& path);
class CookieSorter {
public:
- bool operator()(const Cookie& lhs, const Cookie& rhs) const
+ bool operator()(const Cookie* lhs, const Cookie* rhs) const
{
- if(lhs.getDomain() == rhs.getDomain()) {
- return lhs.getName() < rhs.getName();
+ if(lhs->getDomain() == rhs->getDomain()) {
+ return lhs->getName() < rhs->getName();
} else {
- return lhs.getDomain() < rhs.getDomain();
+ return lhs->getDomain() < rhs->getDomain();
}
}
};
-Cookie createCookie
+std::unique_ptr<Cookie> createCookie
(const std::string& name,
const std::string& value,
const std::string& domain,
@@ -39,7 +39,7 @@ Cookie createCookie
const std::string& path,
bool secure);
-Cookie createCookie
+std::unique_ptr<Cookie> createCookie
(const std::string& name,
const std::string& value,
time_t expiryTime,
@@ -75,4 +75,17 @@ std::shared_ptr<RequestGroup> createRequestGroup(int32_t pieceLength,
std::shared_ptr<DownloadResult> createDownloadResult
(error_code::Value result, const std::string& uri);
+namespace {
+template<typename V, typename T>
+bool derefFind(const V& v, const T& t)
+{
+ for(auto i : v) {
+ if(*i == *t) {
+ return true;
+ }
+ }
+ return false;
+}
+} // namespace
+
} // namespace aria2