2007-06-10 07:55:43 +00:00
|
|
|
#include "CookieParser.h"
|
|
|
|
#include <fstream>
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2007-06-10 07:55:43 +00:00
|
|
|
|
|
|
|
class CookieParserTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(CookieParserTest);
|
|
|
|
CPPUNIT_TEST(testParse);
|
|
|
|
CPPUNIT_TEST(testParse_file);
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
private:
|
|
|
|
|
|
|
|
public:
|
|
|
|
void setUp() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void testParse();
|
|
|
|
|
|
|
|
void testParse_file();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( CookieParserTest );
|
|
|
|
|
|
|
|
void CookieParserTest::testParse()
|
|
|
|
{
|
2008-09-07 04:36:41 +00:00
|
|
|
std::string str = "JSESSIONID=123456789; expires=Sun, 10-Jun-2007 11:00:00 GMT; path=/; domain=localhost; secure";
|
2007-06-10 07:55:43 +00:00
|
|
|
Cookie c = CookieParser().parse(str);
|
|
|
|
CPPUNIT_ASSERT(c.good());
|
2008-09-01 15:00:41 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), c.getName());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("123456789"), c.getValue());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((time_t)1181473200, c.getExpiry());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("/"), c.getPath());
|
2008-12-15 15:48:48 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(".localhost.local"), c.getDomain());
|
2008-09-01 15:00:41 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(true, c.isSecureCookie());
|
2008-10-26 14:22:58 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, c.isSessionCookie());
|
2007-06-10 07:55:43 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string str2 = "JSESSIONID=123456789";
|
2008-01-11 15:20:35 +00:00
|
|
|
c = CookieParser().parse(str2, "default.domain", "/default/path");
|
2007-06-10 07:55:43 +00:00
|
|
|
CPPUNIT_ASSERT(c.good());
|
2008-09-01 15:00:41 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), c.getName());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("123456789"), c.getValue());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((time_t)0, c.getExpiry());
|
2008-12-15 15:48:48 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(".default.domain"), c.getDomain());
|
2008-09-01 15:00:41 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("/default/path"), c.getPath());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, c.isSecureCookie());
|
2008-10-26 14:22:58 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(true, c.isSessionCookie());
|
2007-06-10 07:55:43 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string str3 = "";
|
2007-06-10 07:55:43 +00:00
|
|
|
c = CookieParser().parse(str3);
|
|
|
|
CPPUNIT_ASSERT(!c.good());
|
2008-08-17 14:09:03 +00:00
|
|
|
|
2008-11-22 20:28:50 +00:00
|
|
|
#ifndef __MINGW32__
|
2008-11-10 16:18:39 +00:00
|
|
|
std::string str4 = "UID=300; expires=Wed, 01-Jan-1960 00:00:00 GMT";
|
2008-11-22 20:28:50 +00:00
|
|
|
time_t expire_time = (time_t) -315619200;
|
|
|
|
#else
|
|
|
|
std::string str4 = "UID=300; expires=Wed, 01-Jan-1970 00:00:01 GMT";
|
|
|
|
time_t expire_time = (time_t) 1;
|
|
|
|
#endif
|
2008-08-17 14:09:03 +00:00
|
|
|
c = CookieParser().parse(str4, "localhost", "/");
|
|
|
|
CPPUNIT_ASSERT(c.good());
|
2008-11-05 10:30:43 +00:00
|
|
|
CPPUNIT_ASSERT(!c.isSessionCookie());
|
2008-11-22 20:28:50 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(expire_time, c.getExpiry());
|
2008-09-07 04:36:41 +00:00
|
|
|
|
|
|
|
std::string str5 = "k=v; expires=Sun, 10-Jun-07 11:00:00 GMT";
|
|
|
|
c = CookieParser().parse(str5);
|
|
|
|
CPPUNIT_ASSERT(c.good());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("k"), c.getName());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("v"), c.getValue());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((time_t)1181473200, c.getExpiry());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), c.getDomain());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), c.getPath());
|
|
|
|
CPPUNIT_ASSERT(!c.isSecureCookie());
|
2008-10-26 14:22:58 +00:00
|
|
|
CPPUNIT_ASSERT(!c.isSessionCookie());
|
2007-06-10 07:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CookieParserTest::testParse_file()
|
|
|
|
{
|
2008-02-08 15:53:45 +00:00
|
|
|
std::ifstream f("cookietest.txt");
|
2007-06-10 07:55:43 +00:00
|
|
|
|
|
|
|
Cookies cookies = CookieParser().parse(f);
|
|
|
|
|
2007-07-23 14:09:46 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((int32_t)3, (int32_t)cookies.size());
|
2007-06-10 07:55:43 +00:00
|
|
|
|
|
|
|
Cookie c = cookies[0];
|
2008-09-01 15:00:41 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), c.getName());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("123456789"), c.getValue());
|
2007-06-10 07:55:43 +00:00
|
|
|
|
|
|
|
c = cookies[1];
|
2008-09-01 15:00:41 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("user"), c.getName());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("me"), c.getValue());
|
2007-06-10 07:55:43 +00:00
|
|
|
|
|
|
|
c = cookies[2];
|
2008-09-01 15:00:41 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("passwd"), c.getName());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("secret"), c.getValue());
|
2007-06-10 07:55:43 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|