mirror of https://github.com/aria2/aria2
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.ccpull/1/head
parent
58ad225655
commit
9cdcbf32ff
11
ChangeLog
11
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>
|
2008-08-27 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
||||||
|
|
||||||
Fixed the bug that commands are created more than the number of pieces.
|
Fixed the bug that commands are created more than the number of pieces.
|
||||||
|
|
|
@ -35,6 +35,8 @@
|
||||||
#include "Cookie.h"
|
#include "Cookie.h"
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
#include "A2STR.h"
|
#include "A2STR.h"
|
||||||
|
#include "TimeA2.h"
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
|
@ -48,7 +50,7 @@ Cookie::Cookie(const std::string& name,
|
||||||
value(value),
|
value(value),
|
||||||
expires(expires),
|
expires(expires),
|
||||||
path(path),
|
path(path),
|
||||||
domain(domain),
|
domain(Util::toLower(domain)),
|
||||||
secure(secure),
|
secure(secure),
|
||||||
onetime(false) {}
|
onetime(false) {}
|
||||||
|
|
||||||
|
@ -60,7 +62,7 @@ Cookie::Cookie(const std::string& name,
|
||||||
name(name),
|
name(name),
|
||||||
value(value),
|
value(value),
|
||||||
path(path),
|
path(path),
|
||||||
domain(domain),
|
domain(Util::toLower(domain)),
|
||||||
secure(secure),
|
secure(secure),
|
||||||
onetime(true) {}
|
onetime(true) {}
|
||||||
|
|
||||||
|
@ -85,11 +87,40 @@ bool Cookie::good() const
|
||||||
return !name.empty();
|
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)) &&
|
if((secure || (!this->secure && !secure)) &&
|
||||||
Util::endsWith("."+host, this->domain) &&
|
domainMatch(lowerRequestHost, this->domain) &&
|
||||||
Util::startsWith(dir, this->path) &&
|
pathInclude(requestPath, path) &&
|
||||||
(this->onetime || (date < this->expires))) {
|
(this->onetime || (date < this->expires))) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} 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
|
} // namespace aria2
|
||||||
|
|
10
src/Cookie.h
10
src/Cookie.h
|
@ -75,7 +75,15 @@ public:
|
||||||
|
|
||||||
bool good() const;
|
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;
|
typedef std::deque<Cookie> Cookies;
|
||||||
|
|
|
@ -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
|
|
@ -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_
|
|
@ -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
|
|
@ -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
|
|
@ -60,7 +60,9 @@ aria2c_SOURCES = AllTest.cc\
|
||||||
InOrderURISelectorTest.cc\
|
InOrderURISelectorTest.cc\
|
||||||
ServerStatTest.cc\
|
ServerStatTest.cc\
|
||||||
NsCookieParserTest.cc\
|
NsCookieParserTest.cc\
|
||||||
DirectDiskAdaptorTest.cc
|
DirectDiskAdaptorTest.cc\
|
||||||
|
CookieTest.cc\
|
||||||
|
CookieStorageTest.cc
|
||||||
|
|
||||||
if HAVE_LIBZ
|
if HAVE_LIBZ
|
||||||
aria2c_SOURCES += GZipDecoderTest.cc
|
aria2c_SOURCES += GZipDecoderTest.cc
|
||||||
|
|
|
@ -193,8 +193,9 @@ am__aria2c_SOURCES_DIST = AllTest.cc TestUtil.cc TestUtil.h \
|
||||||
SignatureTest.cc ServerStatManTest.cc \
|
SignatureTest.cc ServerStatManTest.cc \
|
||||||
ServerStatURISelectorTest.cc InOrderURISelectorTest.cc \
|
ServerStatURISelectorTest.cc InOrderURISelectorTest.cc \
|
||||||
ServerStatTest.cc NsCookieParserTest.cc \
|
ServerStatTest.cc NsCookieParserTest.cc \
|
||||||
DirectDiskAdaptorTest.cc GZipDecoderTest.cc \
|
DirectDiskAdaptorTest.cc CookieTest.cc CookieStorageTest.cc \
|
||||||
Sqlite3MozCookieParserTest.cc MessageDigestHelperTest.cc \
|
GZipDecoderTest.cc Sqlite3MozCookieParserTest.cc \
|
||||||
|
MessageDigestHelperTest.cc \
|
||||||
IteratableChunkChecksumValidatorTest.cc \
|
IteratableChunkChecksumValidatorTest.cc \
|
||||||
IteratableChecksumValidatorTest.cc BtAllowedFastMessageTest.cc \
|
IteratableChecksumValidatorTest.cc BtAllowedFastMessageTest.cc \
|
||||||
BtBitfieldMessageTest.cc BtCancelMessageTest.cc \
|
BtBitfieldMessageTest.cc BtCancelMessageTest.cc \
|
||||||
|
@ -365,6 +366,7 @@ am_aria2c_OBJECTS = AllTest.$(OBJEXT) TestUtil.$(OBJEXT) \
|
||||||
ServerStatURISelectorTest.$(OBJEXT) \
|
ServerStatURISelectorTest.$(OBJEXT) \
|
||||||
InOrderURISelectorTest.$(OBJEXT) ServerStatTest.$(OBJEXT) \
|
InOrderURISelectorTest.$(OBJEXT) ServerStatTest.$(OBJEXT) \
|
||||||
NsCookieParserTest.$(OBJEXT) DirectDiskAdaptorTest.$(OBJEXT) \
|
NsCookieParserTest.$(OBJEXT) DirectDiskAdaptorTest.$(OBJEXT) \
|
||||||
|
CookieTest.$(OBJEXT) CookieStorageTest.$(OBJEXT) \
|
||||||
$(am__objects_1) $(am__objects_2) $(am__objects_3) \
|
$(am__objects_1) $(am__objects_2) $(am__objects_3) \
|
||||||
$(am__objects_4) $(am__objects_5)
|
$(am__objects_4) $(am__objects_5)
|
||||||
aria2c_OBJECTS = $(am_aria2c_OBJECTS)
|
aria2c_OBJECTS = $(am_aria2c_OBJECTS)
|
||||||
|
@ -587,8 +589,9 @@ aria2c_SOURCES = AllTest.cc TestUtil.cc TestUtil.h SocketCoreTest.cc \
|
||||||
SignatureTest.cc ServerStatManTest.cc \
|
SignatureTest.cc ServerStatManTest.cc \
|
||||||
ServerStatURISelectorTest.cc InOrderURISelectorTest.cc \
|
ServerStatURISelectorTest.cc InOrderURISelectorTest.cc \
|
||||||
ServerStatTest.cc NsCookieParserTest.cc \
|
ServerStatTest.cc NsCookieParserTest.cc \
|
||||||
DirectDiskAdaptorTest.cc $(am__append_1) $(am__append_2) \
|
DirectDiskAdaptorTest.cc CookieTest.cc CookieStorageTest.cc \
|
||||||
$(am__append_3) $(am__append_4) $(am__append_5)
|
$(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_CXXFLAGS = ${CPPUNIT_CFLAGS} -I../src -I../lib -Wall -D_FILE_OFFSET_BITS=64
|
||||||
#aria2c_LDFLAGS = ${CPPUNIT_LIBS}
|
#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)/CookieBoxFactoryTest.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CookieBoxTest.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)/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)/DHKeyExchangeTest.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DHTAnnouncePeerMessageTest.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@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DHTAnnouncePeerReplyMessageTest.Po@am__quote@
|
||||||
|
|
Loading…
Reference in New Issue