2008-08-27 16:04:36 +00:00
|
|
|
#include "Cookie.h"
|
2008-12-15 15:48:48 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2008-08-27 16:04:36 +00:00
|
|
|
#include "Exception.h"
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2008-08-27 16:04:36 +00:00
|
|
|
#include "TimeA2.h"
|
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
|
|
|
class CookieTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(CookieTest);
|
|
|
|
CPPUNIT_TEST(testValidate);
|
|
|
|
CPPUNIT_TEST(testOperatorEqual);
|
|
|
|
CPPUNIT_TEST(testMatch);
|
|
|
|
CPPUNIT_TEST(testIsExpired);
|
2008-12-15 15:48:48 +00:00
|
|
|
CPPUNIT_TEST(testNormalizeDomain);
|
2009-05-22 14:51:57 +00:00
|
|
|
CPPUNIT_TEST(testToNsCookieFormat);
|
2010-01-28 14:01:50 +00:00
|
|
|
CPPUNIT_TEST(testMarkOriginServerOnly);
|
2008-08-27 16:04:36 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
public:
|
|
|
|
void setUp() {}
|
|
|
|
|
|
|
|
void tearDown() {}
|
|
|
|
|
|
|
|
void testValidate();
|
|
|
|
void testOperatorEqual();
|
|
|
|
void testMatch();
|
|
|
|
void testIsExpired();
|
2008-12-15 15:48:48 +00:00
|
|
|
void testNormalizeDomain();
|
2009-05-22 14:51:57 +00:00
|
|
|
void testToNsCookieFormat();
|
2010-01-28 14:01:50 +00:00
|
|
|
void testMarkOriginServerOnly();
|
2008-08-27 16:04:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(CookieTest);
|
|
|
|
|
|
|
|
void CookieTest::testValidate()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Cookie defaultDomainPath("k", "v", "/", "localhost", false);
|
|
|
|
CPPUNIT_ASSERT(defaultDomainPath.validate("localhost", "/"));
|
|
|
|
}
|
|
|
|
{
|
2008-12-15 15:48:48 +00:00
|
|
|
Cookie domainStartsWithoutDot("k", "v", "/", "aria2.org", false);
|
|
|
|
CPPUNIT_ASSERT(domainStartsWithoutDot.validate("www.aria2.org", "/"));
|
|
|
|
Cookie domainStartsWithDot("k", "v", "/", ".aria2.org", false);
|
|
|
|
CPPUNIT_ASSERT(domainStartsWithDot.validate("www.aria2.org", "/"));
|
2008-08-27 16:04:36 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
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", "/"));
|
|
|
|
}
|
2008-09-01 13:46:03 +00:00
|
|
|
{
|
|
|
|
Cookie fromNumericHost("k", "v", "/", "192.168.1.1", false);
|
|
|
|
CPPUNIT_ASSERT(fromNumericHost.validate("192.168.1.1", "/"));
|
|
|
|
CPPUNIT_ASSERT(!fromNumericHost.validate("www.aria2.org", "/"));
|
|
|
|
}
|
2008-08-27 16:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2008-12-15 15:48:48 +00:00
|
|
|
// normalize
|
|
|
|
Cookie n1("k", "v", "/", "localhost", false);
|
|
|
|
Cookie n2("k", "v", "/", ".localhost", false);
|
|
|
|
Cookie n3("k", "v", "/", "localhost.local", false);
|
|
|
|
Cookie n4("k", "v", "/", ".localhost.local", false);
|
|
|
|
CPPUNIT_ASSERT(n1 == n2);
|
|
|
|
CPPUNIT_ASSERT(n1 == n3);
|
|
|
|
CPPUNIT_ASSERT(n1 == n4);
|
2008-08-27 16:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CookieTest::testMatch()
|
|
|
|
{
|
|
|
|
Cookie c("k", "v", "/downloads", ".aria2.org", false);
|
|
|
|
Cookie c2("k", "v", "/downloads/", ".aria2.org", false);
|
2008-12-15 15:48:48 +00:00
|
|
|
Cookie c3("k", "v", "/downloads/", "aria2.org", false);
|
|
|
|
Cookie c4("k", "v", "/downloads/", "localhost", false);
|
2008-08-27 16:04:36 +00:00
|
|
|
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));
|
2008-12-15 15:48:48 +00:00
|
|
|
CPPUNIT_ASSERT(c3.match("www.aria2.org", "/downloads", 0, false));
|
|
|
|
CPPUNIT_ASSERT(c4.match("localhost", "/downloads", 0, false));
|
2008-08-27 16:04:36 +00:00
|
|
|
|
|
|
|
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));
|
2008-12-15 15:48:48 +00:00
|
|
|
CPPUNIT_ASSERT(secureCookie.match("www.secure.aria2.org", "/", 0, true));
|
2008-08-27 16:04:36 +00:00
|
|
|
|
|
|
|
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));
|
2008-09-01 13:46:03 +00:00
|
|
|
|
|
|
|
Cookie fromNumericHost("k", "v", "/", "192.168.1.1", false);
|
|
|
|
CPPUNIT_ASSERT(fromNumericHost.match("192.168.1.1", "/", 0, false));
|
|
|
|
CPPUNIT_ASSERT(!fromNumericHost.match("www.aria2.org", "/", 0, false));
|
2008-08-27 16:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
2008-10-26 14:22:58 +00:00
|
|
|
Cookie sessionCookie2("k", "v", 0, "/", "localhost", false);
|
2008-12-15 15:48:48 +00:00
|
|
|
CPPUNIT_ASSERT(!sessionCookie2.isExpired());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CookieTest::testNormalizeDomain()
|
|
|
|
{
|
|
|
|
Cookie dot("k", "v", "/", ".", false);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("..local"), dot.getDomain());
|
|
|
|
Cookie ip("k", "v", "/", "192.168.1.1", false);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("192.168.1.1"), ip.getDomain());
|
2010-01-28 14:25:16 +00:00
|
|
|
Cookie ipv6("k", "v", "/", "::1", false);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("::1"), ipv6.getDomain());
|
2008-08-27 16:04:36 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 14:51:57 +00:00
|
|
|
void CookieTest::testToNsCookieFormat()
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string(".domain.org\tTRUE\t/\tFALSE\t12345678\thello\tworld"),
|
|
|
|
Cookie("hello","world",12345678,"/",".domain.org",false).toNsCookieFormat());
|
|
|
|
// Session cookie's expiry is 0
|
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string(".domain.org\tTRUE\t/\tTRUE\t0\thello\tworld"),
|
|
|
|
Cookie("hello","world","/","domain.org",true).toNsCookieFormat());
|
|
|
|
}
|
|
|
|
|
2010-01-28 14:01:50 +00:00
|
|
|
void CookieTest::testMarkOriginServerOnly()
|
|
|
|
{
|
|
|
|
Cookie c("k", "v", "/", "aria2.sf.net", false);
|
|
|
|
c.markOriginServerOnly();
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2.sf.net"), c.getDomain());
|
|
|
|
Cookie ip("k", "v", "/", "192.168.0.1", false);
|
|
|
|
ip.markOriginServerOnly();
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), ip.getDomain());
|
|
|
|
}
|
|
|
|
|
2008-08-27 16:04:36 +00:00
|
|
|
} // namespace aria2
|