Files
aria2/test/NsCookieParserTest.cc
Tatsuhiro Tsujikawa 8b17d4b276 2010-10-09 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
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
2010-10-09 14:22:49 +00:00

102 lines
3.1 KiB
C++

#include "NsCookieParser.h"
#include <iostream>
#include <cppunit/extensions/HelperMacros.h>
#include "RecoverableException.h"
#include "util.h"
#include "Cookie.h"
namespace aria2 {
class NsCookieParserTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(NsCookieParserTest);
CPPUNIT_TEST(testParse);
CPPUNIT_TEST(testParse_fileNotFound);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {}
void tearDown() {}
void testParse();
void testParse_fileNotFound();
};
CPPUNIT_TEST_SUITE_REGISTRATION(NsCookieParserTest);
void NsCookieParserTest::testParse()
{
NsCookieParser parser;
time_t now = 0;
std::vector<Cookie> cookies = parser.parse("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((time_t)INT32_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());
}
void NsCookieParserTest::testParse_fileNotFound()
{
NsCookieParser parser;
try {
time_t now = 0;
parser.parse("fileNotFound", now);
CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) {
// SUCCESS
}
}
} // namespace aria2