mirror of
https://github.com/aria2/aria2.git
synced 2025-01-24 02:31:29 +00:00
8b17d4b276
Rewritten Cookie class and Cookie parser based on http://tools.ietf.org/html/draft-ietf-httpstate-cookie-14 with some modifications. When parsing cookie date, match time first so that it parses asctime() format. The request-path must be ends with '/' so that request-path '/foo/' path-matches cookie-path '/foo' and '/foo/' in the proposed algorithm. * src/Cookie.cc * src/Cookie.h * src/CookieParser.cc: Removed * src/CookieParser.h: Removed * src/CookieStorage.cc * src/CookieStorage.h * src/HttpResponse.cc * src/Makefile.am * src/Makefile.in * src/MultiUrlRequestInfo.cc * src/NsCookieParser.cc * src/NsCookieParser.h * src/Sqlite3CookieParser.cc * src/Sqlite3CookieParser.h * src/a2functional.h * src/cookie_helper.cc * src/cookie_helper.h * src/util.cc * src/util.h * test/CookieBoxFactoryTest.cc: Removed * test/CookieHelperTest.cc * test/CookieParserTest.cc: Removed * test/CookieStorageTest.cc * test/CookieTest.cc * test/HttpRequestTest.cc * test/Makefile.am * test/Makefile.in * test/NsCookieParserTest.cc * test/Sqlite3CookieParserTest.cc * test/TestUtil.cc * test/TestUtil.h * test/a2functionalTest.cc * test/chromium_cookies.sqlite * test/cookies.sqlite * test/nscookietest.txt
137 lines
4.6 KiB
C++
137 lines
4.6 KiB
C++
#include "Sqlite3CookieParserImpl.h"
|
|
|
|
#include <iostream>
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
#include "RecoverableException.h"
|
|
#include "util.h"
|
|
|
|
namespace aria2 {
|
|
|
|
class Sqlite3CookieParserTest:public CppUnit::TestFixture {
|
|
|
|
CPPUNIT_TEST_SUITE(Sqlite3CookieParserTest);
|
|
CPPUNIT_TEST(testMozParse);
|
|
CPPUNIT_TEST(testMozParse_fileNotFound);
|
|
CPPUNIT_TEST(testMozParse_badfile);
|
|
CPPUNIT_TEST(testChromumParse);
|
|
CPPUNIT_TEST_SUITE_END();
|
|
public:
|
|
void setUp() {}
|
|
|
|
void tearDown() {}
|
|
|
|
void testMozParse();
|
|
void testMozParse_fileNotFound();
|
|
void testMozParse_badfile();
|
|
void testChromumParse();
|
|
};
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(Sqlite3CookieParserTest);
|
|
|
|
void Sqlite3CookieParserTest::testMozParse()
|
|
{
|
|
Sqlite3MozCookieParser parser("cookies.sqlite");
|
|
std::vector<Cookie> cookies;
|
|
parser.parse(cookies, 0);
|
|
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());
|
|
|
|
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());
|
|
|
|
// 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());
|
|
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());
|
|
|
|
// See row id=5 has bad path, so it is skipped.
|
|
}
|
|
|
|
void Sqlite3CookieParserTest::testMozParse_fileNotFound()
|
|
{
|
|
Sqlite3MozCookieParser parser("fileNotFound");
|
|
try {
|
|
std::vector<Cookie> cookies;
|
|
parser.parse(cookies, 0);
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
|
} catch(RecoverableException& e) {
|
|
// SUCCESS
|
|
CPPUNIT_ASSERT(util::startsWith(e.what(),
|
|
"SQLite3 database is not opened"));
|
|
}
|
|
}
|
|
|
|
void Sqlite3CookieParserTest::testMozParse_badfile()
|
|
{
|
|
Sqlite3MozCookieParser parser("badcookies.sqlite");
|
|
try {
|
|
std::vector<Cookie> cookies;
|
|
parser.parse(cookies, 0);
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
|
} catch(RecoverableException& e) {
|
|
// SUCCESS
|
|
}
|
|
}
|
|
|
|
void Sqlite3CookieParserTest::testChromumParse()
|
|
{
|
|
Sqlite3ChromiumCookieParser parser("chromium_cookies.sqlite");
|
|
std::vector<Cookie> cookies;
|
|
parser.parse(cookies, 0);
|
|
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());
|
|
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());
|
|
}
|
|
|
|
} // namespace aria2
|