summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2008-08-27 16:04:36 +0000
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2008-08-27 16:04:36 +0000
commit9cdcbf32ff69afc4c2c8f220df90aabed62dafde (patch)
tree0388730a58d56f83b829c193f3a44fcc5b98c52e
parent58ad225655b27370e0a3ebe5f603ec9890360b90 (diff)
2008-08-28 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added CookieStorage class which is based on RFC2109 and manages cookies more strictly than CookieBox and CookieBoxFactory class. * src/CookieStorage.cc * src/CookieStorage.h * src/Cookie.cc * src/Cookie.h * test/CookieStorageTest.cc * test/CookieTest.cc
-rw-r--r--ChangeLog11
-rw-r--r--src/Cookie.cc93
-rw-r--r--src/Cookie.h10
-rw-r--r--src/CookieStorage.cc175
-rw-r--r--src/CookieStorage.h95
-rw-r--r--test/CookieStorageTest.cc128
-rw-r--r--test/CookieTest.cc142
-rw-r--r--test/Makefile.am4
-rw-r--r--test/Makefile.in13
9 files changed, 660 insertions, 11 deletions
diff --git a/ChangeLog b/ChangeLog
index a8bf24c3..3ef98417 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2008-08-28 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
+
+ Added CookieStorage class which is based on RFC2109 and manages cookies
+ more strictly than CookieBox and CookieBoxFactory class.
+ * src/CookieStorage.cc
+ * src/CookieStorage.h
+ * src/Cookie.cc
+ * src/Cookie.h
+ * test/CookieStorageTest.cc
+ * test/CookieTest.cc
+
2008-08-27 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Fixed the bug that commands are created more than the number of pieces.
diff --git a/src/Cookie.cc b/src/Cookie.cc
index dcd95a9f..46693934 100644
--- a/src/Cookie.cc
+++ b/src/Cookie.cc
@@ -35,6 +35,8 @@
#include "Cookie.h"
#include "Util.h"
#include "A2STR.h"
+#include "TimeA2.h"
+#include <algorithm>
namespace aria2 {
@@ -48,7 +50,7 @@ Cookie::Cookie(const std::string& name,
value(value),
expires(expires),
path(path),
- domain(domain),
+ domain(Util::toLower(domain)),
secure(secure),
onetime(false) {}
@@ -60,7 +62,7 @@ Cookie::Cookie(const std::string& name,
name(name),
value(value),
path(path),
- domain(domain),
+ domain(Util::toLower(domain)),
secure(secure),
onetime(true) {}
@@ -85,11 +87,40 @@ bool Cookie::good() const
return !name.empty();
}
-bool Cookie::match(const std::string& host, const std::string& dir, time_t date, bool secure) const
+static bool pathInclude(const std::string& requestPath, const std::string& path)
{
+ if(requestPath == path) {
+ return true;
+ }
+ if(Util::startsWith(requestPath, path)) {
+ if(*path.rbegin() != '/' && requestPath[path.size()] != '/') {
+ return false;
+ }
+ } else if(*path.rbegin() != '/' || *requestPath.rbegin() == '/' ||
+ !Util::startsWith(requestPath+"/", path)) {
+ return false;
+ }
+ return true;
+}
+
+static bool domainMatch(const std::string& requestHost,
+ const std::string& domain)
+{
+ if(*domain.begin() == '.') {
+ return Util::endsWith("."+requestHost, domain);
+ } else {
+ return requestHost == domain;
+ }
+}
+
+bool Cookie::match(const std::string& requestHost,
+ const std::string& requestPath,
+ time_t date, bool secure) const
+{
+ std::string lowerRequestHost = Util::toLower(requestHost);
if((secure || (!this->secure && !secure)) &&
- Util::endsWith("."+host, this->domain) &&
- Util::startsWith(dir, this->path) &&
+ domainMatch(lowerRequestHost, this->domain) &&
+ pathInclude(requestPath, path) &&
(this->onetime || (date < this->expires))) {
return true;
} else {
@@ -97,4 +128,56 @@ bool Cookie::match(const std::string& host, const std::string& dir, time_t date,
}
}
+bool Cookie::validate(const std::string& requestHost,
+ const std::string& requestPath) const
+{
+ std::string lowerRequestHost = Util::toLower(requestHost);
+ if(lowerRequestHost != domain) {
+ // domain must start with '.'
+ if(*domain.begin() != '.') {
+ return false;
+ }
+ // domain must not end with '.'
+ if(*domain.rbegin() == '.') {
+ return false;
+ }
+ // domain must include at least one embeded '.'
+ if(domain.size() < 4 || domain.find(".", 1) == std::string::npos) {
+ return false;
+ }
+ if(!Util::endsWith(lowerRequestHost, domain)) {
+ return false;
+ }
+ // From RFC2109
+ // * The request-host is a FQDN (not IP address) and has the form HD,
+ // where D is the value of the Domain attribute, and H is a string
+ // that contains one or more dots.
+ if(std::count(lowerRequestHost.begin(),
+ lowerRequestHost.begin()+
+ (lowerRequestHost.size()-domain.size()), '.')
+ > 0) {
+ return false;
+ }
+ }
+ if(requestPath != path) {
+ // From RFC2109
+ // * The value for the Path attribute is not a prefix of the request-
+ // URI.
+ if(!pathInclude(requestPath, path)) {
+ return false;
+ }
+ }
+ return !name.empty();
+}
+
+bool Cookie::operator==(const Cookie& cookie) const
+{
+ return domain == cookie.domain && path == cookie.path && name == cookie.name;
+}
+
+bool Cookie::isExpired() const
+{
+ return !onetime && Time().getTime() >= expires;
+}
+
} // namespace aria2
diff --git a/src/Cookie.h b/src/Cookie.h
index a4529749..eeafb1f8 100644
--- a/src/Cookie.h
+++ b/src/Cookie.h
@@ -75,7 +75,15 @@ public:
bool good() const;
- bool match(const std::string& host, const std::string& dir, time_t date, bool secure) const;
+ bool match(const std::string& requestHost, const std::string& requestPath,
+ time_t date, bool secure) const;
+
+ bool validate(const std::string& requestHost,
+ const std::string& requestPath) const;
+
+ bool operator==(const Cookie& cookie) const;
+
+ bool isExpired() const;
};
typedef std::deque<Cookie> Cookies;
diff --git a/src/CookieStorage.cc b/src/CookieStorage.cc
new file mode 100644
index 00000000..6f2ccaf5
--- /dev/null
+++ b/src/CookieStorage.cc
@@ -0,0 +1,175 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2006 Tatsuhiro Tsujikawa
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you
+ * do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#include "CookieStorage.h"
+#include "Util.h"
+#include "LogFactory.h"
+#include "Logger.h"
+#include "RecoverableException.h"
+#include "StringFormat.h"
+#include "NsCookieParser.h"
+#ifdef HAVE_SQLITE3
+# include "Sqlite3MozCookieParser.h"
+#endif // HAVE_SQLITE3
+#include <algorithm>
+#include <fstream>
+
+namespace aria2 {
+
+CookieStorage::CookieStorage():_logger(LogFactory::getInstance()) {}
+
+CookieStorage::~CookieStorage() {}
+
+bool CookieStorage::store(const Cookie& cookie)
+{
+ if(!cookie.good()) {
+ return false;
+ }
+ std::deque<Cookie>::iterator i = std::find(_cookies.begin(), _cookies.end(),
+ cookie);
+ if(i == _cookies.end()) {
+ if(cookie.isExpired()) {
+ return false;
+ } else {
+ _cookies.push_back(cookie);
+ return true;
+ }
+ } else if(cookie.isExpired()) {
+ _cookies.erase(i);
+ return false;
+ } else {
+ *i = cookie;
+ return true;
+ }
+}
+
+void CookieStorage::storeCookies(const std::deque<Cookie>& cookies)
+{
+ for(std::deque<Cookie>::const_iterator i = cookies.begin();
+ i != cookies.end(); ++i) {
+ store(*i);
+ }
+}
+
+bool CookieStorage::parseAndStore(const std::string& setCookieString,
+ const std::string& requestHost,
+ const std::string& requestPath)
+{
+ if(Util::isNumbersAndDotsNotation(requestHost)) {
+ return false;
+ }
+ Cookie cookie = _parser.parse(setCookieString, requestHost, requestPath);
+ if(cookie.validate(requestHost, requestPath)) {
+ return store(cookie);
+ } else {
+ return false;
+ }
+}
+
+class CriteriaMatch:public std::unary_function<Cookie, bool> {
+private:
+ std::string _requestHost;
+ std::string _requestPath;
+ time_t _date;
+ bool _secure;
+public:
+ CriteriaMatch(const std::string& requestHost, const std::string& requestPath,
+ time_t date, bool secure):
+ _requestHost(requestHost),
+ _requestPath(requestPath),
+ _date(date),
+ _secure(secure) {}
+
+ bool operator()(const Cookie& cookie) const
+ {
+ return cookie.match(_requestHost, _requestPath, _date, _secure);
+ }
+};
+
+class OrderByPathDesc:public std::binary_function<Cookie, Cookie, bool> {
+public:
+ bool operator()(const Cookie& lhs, const Cookie& rhs) const
+ {
+ return lhs.path > rhs.path;
+ }
+};
+
+std::deque<Cookie> CookieStorage::criteriaFind(const std::string& requestHost,
+ const std::string& requestPath,
+ time_t date, bool secure) const
+{
+ std::deque<Cookie> res;
+ std::remove_copy_if(_cookies.begin(), _cookies.end(), std::back_inserter(res),
+ std::not1(CriteriaMatch(requestHost, requestPath, date, secure)));
+ std::sort(res.begin(), res.end(), OrderByPathDesc());
+ return res;
+}
+
+size_t CookieStorage::size() const
+{
+ return _cookies.size();
+}
+
+void CookieStorage::load(const std::string& filename)
+{
+ char header[16]; // "SQLite format 3" plus \0
+ {
+ std::ifstream s(filename.c_str());
+ s.get(header, sizeof(header));
+ if(s.bad()) {
+ throw RecoverableException
+ (StringFormat("Failed to read header of cookie file %s",
+ filename.c_str()).str());
+ }
+ }
+ try {
+ if(std::string(header) == "SQLite format 3") {
+#ifdef HAVE_SQLITE3
+ storeCookies(Sqlite3MozCookieParser().parse(filename));
+#else // !HAVE_SQLITE3
+ throw RecoverableException
+ ("Cannot read SQLite3 database because SQLite3 support is disabled by"
+ " configuration.");
+#endif // !HAVE_SQLITE3
+ } else {
+ storeCookies(NsCookieParser().parse(filename));
+ }
+ } catch(RecoverableException& e) {
+ throw RecoverableException
+ (StringFormat("Failed to load cookies from %s", filename.c_str()).str(),
+ e);
+ }
+}
+
+} // namespace aria2
diff --git a/src/CookieStorage.h b/src/CookieStorage.h
new file mode 100644
index 00000000..d8ea6149
--- /dev/null
+++ b/src/CookieStorage.h
@@ -0,0 +1,95 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2006 Tatsuhiro Tsujikawa
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you
+ * do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_COOKIE_STORAGE_H_
+#define _D_COOKIE_STORAGE_H_
+
+#include "common.h"
+#include "a2time.h"
+#include "Cookie.h"
+#include "CookieParser.h"
+#include <string>
+#include <deque>
+
+namespace aria2 {
+
+class Logger;
+
+class CookieStorage {
+private:
+ std::deque<Cookie> _cookies;
+
+ CookieParser _parser;
+
+ Logger* _logger;
+
+ void storeCookies(const std::deque<Cookie>& cookies);
+public:
+ CookieStorage();
+
+ ~CookieStorage();
+
+ // Returns true if cookie is stored or updated existing cookie.
+ // Returns false if cookie is expired.
+ bool store(const Cookie& cookie);
+
+ // Returns true if cookie is stored or updated existing cookie.
+ // Otherwise, returns false.
+ bool parseAndStore(const std::string& setCookieString,
+ const std::string& requestHost,
+ const std::string& requestPath);
+
+ std::deque<Cookie> criteriaFind(const std::string& requestHost,
+ const std::string& requestPath,
+ time_t date, bool secure) const;
+
+ void load(const std::string& filename);
+
+ size_t size() const;
+
+ std::deque<Cookie>::const_iterator begin() const
+ {
+ return _cookies.begin();
+ }
+
+ std::deque<Cookie>::const_iterator end() const
+ {
+ return _cookies.end();
+ }
+
+};
+
+} // namespace aria2
+
+#endif // _D_COOKIE_STORAGE_H_
diff --git a/test/CookieStorageTest.cc b/test/CookieStorageTest.cc
new file mode 100644
index 00000000..9f1a156b
--- /dev/null
+++ b/test/CookieStorageTest.cc
@@ -0,0 +1,128 @@
+#include "CookieStorage.h"
+#include "Exception.h"
+#include "Util.h"
+#include "TimeA2.h"
+#include "CookieParser.h"
+#include <iostream>
+#include <algorithm>
+#include <cppunit/extensions/HelperMacros.h>
+
+namespace aria2 {
+
+class CookieStorageTest:public CppUnit::TestFixture {
+
+ CPPUNIT_TEST_SUITE(CookieStorageTest);
+ CPPUNIT_TEST(testStore);
+ CPPUNIT_TEST(testParseAndStore);
+ CPPUNIT_TEST(testCriteriaFind);
+ CPPUNIT_TEST_SUITE_END();
+public:
+ void setUp() {}
+
+ void tearDown() {}
+
+ void testStore();
+ void testParseAndStore();
+ void testCriteriaFind();
+};
+
+
+CPPUNIT_TEST_SUITE_REGISTRATION(CookieStorageTest);
+
+void CookieStorageTest::testStore()
+{
+ CookieStorage st;
+ Cookie goodCookie("k", "v", "/", "localhost", false);
+ CPPUNIT_ASSERT(st.store(goodCookie));
+ CPPUNIT_ASSERT_EQUAL((size_t)1, st.size());
+ CPPUNIT_ASSERT(std::find(st.begin(), st.end(), goodCookie) != st.end());
+
+ Cookie anotherCookie("k", "v", "/", "mirror", true);
+ CPPUNIT_ASSERT(st.store(anotherCookie));
+ CPPUNIT_ASSERT_EQUAL((size_t)2, st.size());
+ CPPUNIT_ASSERT(std::find(st.begin(), st.end(), anotherCookie) != st.end());
+ CPPUNIT_ASSERT(std::find(st.begin(), st.end(), goodCookie) != st.end());
+
+ Cookie updateGoodCookie("k", "v2", "/", "localhost", false);
+ CPPUNIT_ASSERT(st.store(goodCookie));
+ CPPUNIT_ASSERT_EQUAL((size_t)2, st.size());
+ CPPUNIT_ASSERT(std::find(st.begin(), st.end(), updateGoodCookie) != st.end());
+ CPPUNIT_ASSERT(std::find(st.begin(), st.end(), anotherCookie) != st.end());
+
+ Cookie expireGoodCookie("k", "v3", 0, "/", "localhost", false);
+ CPPUNIT_ASSERT(!st.store(expireGoodCookie));
+ CPPUNIT_ASSERT_EQUAL((size_t)1, st.size());
+ CPPUNIT_ASSERT(std::find(st.begin(), st.end(), anotherCookie) != st.end());
+
+ Cookie badCookie("", "", "/", "localhost", false);
+ CPPUNIT_ASSERT(!st.store(badCookie));
+ CPPUNIT_ASSERT_EQUAL((size_t)1, st.size());
+ CPPUNIT_ASSERT(std::find(st.begin(), st.end(), anotherCookie) != st.end());
+}
+
+void CookieStorageTest::testParseAndStore()
+{
+ CookieStorage st;
+
+ std::string localhostCookieStr = "k=v;"
+ " expires=Fri, 2038-01-01 00:00:00 GMT; path=/; domain=localhost;";
+
+ CPPUNIT_ASSERT(st.parseAndStore(localhostCookieStr,
+ "localhost", "/downloads"));
+ CPPUNIT_ASSERT(!st.parseAndStore(localhostCookieStr,
+ "mirror", "/downloads"));
+
+ CPPUNIT_ASSERT(!st.parseAndStore(localhostCookieStr,
+ "127.0.0.1", "/downloads"));
+}
+
+void CookieStorageTest::testCriteriaFind()
+{
+ CookieStorage st;
+
+ Cookie alpha("alpha", "ALPHA", "/", ".aria2.org", false);
+ Cookie bravo("bravo", "BRAVO", Time().getTime()+60, "/foo", ".aria2.org",
+ false);
+ Cookie charlie("charlie", "CHARLIE", "/", ".aria2.org", true);
+ Cookie delta("delta", "DELTA", "/foo/bar", ".aria2.org", false);
+ Cookie echo("echo", "ECHO", "/", "www.aria2.org", false);
+ Cookie foxtrot("foxtrot", "FOXTROT", "/", ".sf.net", false);
+ CPPUNIT_ASSERT(st.store(alpha));
+ CPPUNIT_ASSERT(st.store(bravo));
+ CPPUNIT_ASSERT(st.store(charlie));
+ CPPUNIT_ASSERT(st.store(delta));
+ CPPUNIT_ASSERT(st.store(echo));
+ CPPUNIT_ASSERT(st.store(foxtrot));
+
+ std::deque<Cookie> aria2Slash = st.criteriaFind("www.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());
+
+ std::deque<Cookie> aria2SlashFoo = st.criteriaFind("www.aria2.org", "/foo",
+ 0, false);
+ CPPUNIT_ASSERT_EQUAL((size_t)3, aria2SlashFoo.size());
+ CPPUNIT_ASSERT_EQUAL(std::string("bravo"), aria2SlashFoo[0].name);
+ CPPUNIT_ASSERT(std::find(aria2SlashFoo.begin(), aria2SlashFoo.end(), alpha)
+ != aria2SlashFoo.end());
+ CPPUNIT_ASSERT(std::find(aria2SlashFoo.begin(), aria2SlashFoo.end(), echo)
+ != aria2SlashFoo.end());
+
+ std::deque<Cookie> aria2Expires = st.criteriaFind("www.aria2.org", "/foo",
+ Time().getTime()+120,
+ 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());
+
+ std::deque<Cookie> dlAria2 = st.criteriaFind("dl.aria2.org", "/", 0, false);
+ CPPUNIT_ASSERT_EQUAL((size_t)1, dlAria2.size());
+ CPPUNIT_ASSERT_EQUAL(std::string("alpha"), dlAria2[0].name);
+}
+
+} // namespace aria2
diff --git a/test/CookieTest.cc b/test/CookieTest.cc
new file mode 100644
index 00000000..0221ae05
--- /dev/null
+++ b/test/CookieTest.cc
@@ -0,0 +1,142 @@
+#include "Cookie.h"
+#include "Exception.h"
+#include "Util.h"
+#include "TimeA2.h"
+#include <iostream>
+#include <cppunit/extensions/HelperMacros.h>
+
+namespace aria2 {
+
+class CookieTest:public CppUnit::TestFixture {
+
+ CPPUNIT_TEST_SUITE(CookieTest);
+ CPPUNIT_TEST(testValidate);
+ CPPUNIT_TEST(testOperatorEqual);
+ CPPUNIT_TEST(testMatch);
+ CPPUNIT_TEST(testIsExpired);
+ CPPUNIT_TEST_SUITE_END();
+public:
+ void setUp() {}
+
+ void tearDown() {}
+
+ void testValidate();
+ void testOperatorEqual();
+ void testMatch();
+ void testIsExpired();
+};
+
+
+CPPUNIT_TEST_SUITE_REGISTRATION(CookieTest);
+
+void CookieTest::testValidate()
+{
+ {
+ Cookie defaultDomainPath("k", "v", "/", "localhost", false);
+ CPPUNIT_ASSERT(defaultDomainPath.validate("localhost", "/"));
+ }
+ {
+ Cookie domainStartsWithDot("k", "v", "/", "aria2.org", false);
+ CPPUNIT_ASSERT(!domainStartsWithDot.validate("www.aria2.org", "/"));
+ Cookie success("k", "v", "/", ".aria2.org", false);
+ CPPUNIT_ASSERT(success.validate("www.aria2.org", "/"));
+ }
+ {
+ Cookie domainWithoutEmbeddedDot("k", "v", "/", ".org", false);
+ CPPUNIT_ASSERT(!domainWithoutEmbeddedDot.validate("aria2.org", "/"));
+ }
+ {
+ Cookie domainEndsWithDot("k", "v", "/", ".aria2.org.", false);
+ CPPUNIT_ASSERT(!domainEndsWithDot.validate("www.aria2.org", "/"));
+ }
+ {
+ Cookie domainHD("k", "v", "/", ".aria2.org", false);
+ CPPUNIT_ASSERT(!domainHD.validate("aria2.www.aria2.org", "/"));
+ }
+ {
+ Cookie pathNotStartsWith("k", "v", "/downloads", "localhost", false);
+ CPPUNIT_ASSERT(!pathNotStartsWith.validate("localhost", "/examples"));
+ }
+ {
+ Cookie pathStartsWith("k", "v", "/downloads", "localhost", false);
+ CPPUNIT_ASSERT(pathStartsWith.validate("localhost", "/downloads/latest/"));
+ }
+ {
+ Cookie pathSlash("k", "v", "/downloads", "localhost", false);
+ CPPUNIT_ASSERT(pathSlash.validate("localhost", "/downloads/"));
+ }
+ {
+ Cookie pathSlash("k", "v", "/downloads/", "localhost", false);
+ CPPUNIT_ASSERT(pathSlash.validate("localhost", "/downloads"));
+ }
+ {
+ Cookie pathNotMatch("k", "v", "/downloads", "localhost", false);
+ CPPUNIT_ASSERT(!pathNotMatch.validate("localhost", "/downloadss"));
+ }
+ {
+ Cookie pathNotMatch("k", "v", "/downloads/", "localhost", false);
+ CPPUNIT_ASSERT(!pathNotMatch.validate("localhost", "/downloadss"));
+ }
+ {
+ Cookie pathNotMatch("k", "v", "/downloads", "localhost", false);
+ CPPUNIT_ASSERT(!pathNotMatch.validate("localhost", "/downloadss/"));
+ }
+ {
+ Cookie nameEmpty("", "v", "/", "localhost", false);
+ CPPUNIT_ASSERT(!nameEmpty.validate("localhost", "/"));
+ }
+}
+
+void CookieTest::testOperatorEqual()
+{
+ Cookie a("k", "v", "/", "localhost", false);
+ Cookie b("k", "v", "/", "LOCALHOST", true);
+ Cookie wrongPath("k", "v", "/a", "localhost", false);
+ Cookie wrongDomain("k", "v", "/", "mydomain", false);
+ Cookie wrongName("h", "v", "/a", "localhost", false);
+ Cookie caseSensitiveName("K", "v", "/a", "localhost", 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("k", "v", "/downloads", ".aria2.org", false);
+ Cookie c2("k", "v", "/downloads/", ".aria2.org", false);
+ CPPUNIT_ASSERT(c.match("www.aria2.org", "/downloads", 0, 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.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));
+
+ Cookie secureCookie("k", "v", "/", "secure.aria2.org", 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("k", "v", 1000, "/", ".aria2.org", 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));
+}
+
+void CookieTest::testIsExpired()
+{
+ Cookie expiredCookie("k", "v", 1000, "/", "localhost", false);
+ CPPUNIT_ASSERT(expiredCookie.isExpired());
+ Cookie validCookie("k", "v", Time().getTime()+60, "/", "localhost", false);
+ CPPUNIT_ASSERT(!validCookie.isExpired());
+ Cookie sessionCookie("k", "v", "/", "localhost", false);
+ CPPUNIT_ASSERT(!sessionCookie.isExpired());
+}
+
+} // namespace aria2
diff --git a/test/Makefile.am b/test/Makefile.am
index c68c7a64..2d6b528f 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -60,7 +60,9 @@ aria2c_SOURCES = AllTest.cc\
InOrderURISelectorTest.cc\
ServerStatTest.cc\
NsCookieParserTest.cc\
- DirectDiskAdaptorTest.cc
+ DirectDiskAdaptorTest.cc\
+ CookieTest.cc\
+ CookieStorageTest.cc
if HAVE_LIBZ
aria2c_SOURCES += GZipDecoderTest.cc
diff --git a/test/Makefile.in b/test/Makefile.in
index d85eb075..279b31b2 100644
--- a/test/Makefile.in
+++ b/test/Makefile.in
@@ -193,8 +193,9 @@ am__aria2c_SOURCES_DIST = AllTest.cc TestUtil.cc TestUtil.h \
SignatureTest.cc ServerStatManTest.cc \
ServerStatURISelectorTest.cc InOrderURISelectorTest.cc \
ServerStatTest.cc NsCookieParserTest.cc \
- DirectDiskAdaptorTest.cc GZipDecoderTest.cc \
- Sqlite3MozCookieParserTest.cc MessageDigestHelperTest.cc \
+ DirectDiskAdaptorTest.cc CookieTest.cc CookieStorageTest.cc \
+ GZipDecoderTest.cc Sqlite3MozCookieParserTest.cc \
+ MessageDigestHelperTest.cc \
IteratableChunkChecksumValidatorTest.cc \
IteratableChecksumValidatorTest.cc BtAllowedFastMessageTest.cc \
BtBitfieldMessageTest.cc BtCancelMessageTest.cc \
@@ -365,6 +366,7 @@ am_aria2c_OBJECTS = AllTest.$(OBJEXT) TestUtil.$(OBJEXT) \
ServerStatURISelectorTest.$(OBJEXT) \
InOrderURISelectorTest.$(OBJEXT) ServerStatTest.$(OBJEXT) \
NsCookieParserTest.$(OBJEXT) DirectDiskAdaptorTest.$(OBJEXT) \
+ CookieTest.$(OBJEXT) CookieStorageTest.$(OBJEXT) \
$(am__objects_1) $(am__objects_2) $(am__objects_3) \
$(am__objects_4) $(am__objects_5)
aria2c_OBJECTS = $(am_aria2c_OBJECTS)
@@ -587,8 +589,9 @@ aria2c_SOURCES = AllTest.cc TestUtil.cc TestUtil.h SocketCoreTest.cc \
SignatureTest.cc ServerStatManTest.cc \
ServerStatURISelectorTest.cc InOrderURISelectorTest.cc \
ServerStatTest.cc NsCookieParserTest.cc \
- DirectDiskAdaptorTest.cc $(am__append_1) $(am__append_2) \
- $(am__append_3) $(am__append_4) $(am__append_5)
+ DirectDiskAdaptorTest.cc CookieTest.cc CookieStorageTest.cc \
+ $(am__append_1) $(am__append_2) $(am__append_3) \
+ $(am__append_4) $(am__append_5)
#aria2c_CXXFLAGS = ${CPPUNIT_CFLAGS} -I../src -I../lib -Wall -D_FILE_OFFSET_BITS=64
#aria2c_LDFLAGS = ${CPPUNIT_LIBS}
@@ -710,6 +713,8 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CookieBoxFactoryTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CookieBoxTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CookieParserTest.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CookieStorageTest.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CookieTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DHKeyExchangeTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DHTAnnouncePeerMessageTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DHTAnnouncePeerReplyMessageTest.Po@am__quote@