#include "cookie_helper.h"

#include <limits>

#include <cppunit/extensions/HelperMacros.h>

#include "Exception.h"
#include "util.h"
#include "Cookie.h"

namespace aria2 {

class CookieHelperTest:public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(CookieHelperTest);
  CPPUNIT_TEST(testParseDate);
  CPPUNIT_TEST(testDomainMatch);
  CPPUNIT_TEST(testPathMatch);
  CPPUNIT_TEST(testParse);
  CPPUNIT_TEST(testReverseDomainLevel);
  CPPUNIT_TEST_SUITE_END();
public:
  void testParseDate();
  void testDomainMatch();
  void testPathMatch();
  void testParse();
  void testReverseDomainLevel();
};


CPPUNIT_TEST_SUITE_REGISTRATION(CookieHelperTest);

void CookieHelperTest::testParseDate()
{
  // RFC1123
  time_t time = 0;
  CPPUNIT_ASSERT(cookie::parseDate(time, "Sat, 06 Sep 2008 15:26:33 GMT"));
  CPPUNIT_ASSERT_EQUAL((time_t)1220714793, time);
  // RFC850
  CPPUNIT_ASSERT(cookie::parseDate(time, "Saturday, 06-Sep-08 15:26:33 GMT"));
  CPPUNIT_ASSERT_EQUAL((time_t)1220714793, time);
  // ANSI C's asctime()
  CPPUNIT_ASSERT(cookie::parseDate(time, "Sun Sep  6 15:26:33 2008"));
  CPPUNIT_ASSERT_EQUAL((time_t)1220714793, time);

  CPPUNIT_ASSERT(cookie::parseDate(time, "Thu Jan 1 0:0:0 1970"));
  CPPUNIT_ASSERT_EQUAL((time_t)0, time);

  CPPUNIT_ASSERT(!cookie::parseDate(time, "Thu Jan 1 1970 0:")); 
  CPPUNIT_ASSERT(!cookie::parseDate(time, "Thu Jan 1 1970 0:0")); 
  CPPUNIT_ASSERT(!cookie::parseDate(time, "Thu Jan 1 1970 0:0:")); 

  // Leap year
  CPPUNIT_ASSERT(cookie::parseDate(time, "Tue, 29 Feb 2000 00:00:00 GMT"));
  CPPUNIT_ASSERT(!cookie::parseDate(time, "Thu, 29 Feb 2001 00:00:00 GMT"));
}

void CookieHelperTest::testDomainMatch()
{
  CPPUNIT_ASSERT(cookie::domainMatch("localhost", "localhost"));
  CPPUNIT_ASSERT(cookie::domainMatch("192.168.0.1", "192.168.0.1"));
  CPPUNIT_ASSERT(cookie::domainMatch("www.example.org", "example.org"));
  CPPUNIT_ASSERT(!cookie::domainMatch("192.168.0.1", "0.1"));
  CPPUNIT_ASSERT(!cookie::domainMatch("example.org", "example.com"));
  CPPUNIT_ASSERT(!cookie::domainMatch("example.org", "www.example.org"));  
}

void CookieHelperTest::testPathMatch()
{
  CPPUNIT_ASSERT(cookie::pathMatch("/", "/"));
  CPPUNIT_ASSERT(cookie::pathMatch("/foo/", "/foo"));
  CPPUNIT_ASSERT(!cookie::pathMatch("/bar/", "/foo"));
  CPPUNIT_ASSERT(!cookie::pathMatch("/foo", "/bar/foo"));
  CPPUNIT_ASSERT(cookie::pathMatch("/foo/bar", "/foo/"));
}

void CookieHelperTest::testParse()
{
  time_t creationDate = 141;
  {
    std::string str = "ID=123456789; expires=Sun, 10-Jun-2007 11:00:00 GMT;"
      "path=/foo; domain=localhost; secure;httpOnly   ";
    Cookie c;
    CPPUNIT_ASSERT(cookie::parse(c, str, "localhost", "/", creationDate));
    CPPUNIT_ASSERT_EQUAL(std::string("ID"), c.getName());
    CPPUNIT_ASSERT_EQUAL(std::string("123456789"), c.getValue());
    CPPUNIT_ASSERT_EQUAL((time_t)1181473200, c.getExpiryTime());
    CPPUNIT_ASSERT(c.getPersistent());
    CPPUNIT_ASSERT_EQUAL(std::string("localhost"), c.getDomain());
    CPPUNIT_ASSERT(!c.getHostOnly());
    CPPUNIT_ASSERT_EQUAL(std::string("/foo"), c.getPath());
    CPPUNIT_ASSERT(c.getSecure());
    CPPUNIT_ASSERT(c.getHttpOnly());
    CPPUNIT_ASSERT_EQUAL((time_t)141, c.getCreationTime());
    CPPUNIT_ASSERT_EQUAL((time_t)141, c.getLastAccessTime());
  }
  {
    std::string str = "id=; Max-Age=0;";
    Cookie c;
    CPPUNIT_ASSERT(cookie::parse(c, str, "localhost", "/", creationDate));
    CPPUNIT_ASSERT_EQUAL(std::string("id"), c.getName());
    CPPUNIT_ASSERT_EQUAL((time_t)0, 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());
    CPPUNIT_ASSERT(!c.getHttpOnly());
  }
  {
    std::string str = "id=; Max-Age=-100;";
    Cookie c;
    CPPUNIT_ASSERT(cookie::parse(c, str, "localhost", "/", creationDate));
    CPPUNIT_ASSERT_EQUAL((time_t)0, c.getExpiryTime());
    CPPUNIT_ASSERT(c.getPersistent());
  }
  {
    std::string str = "id=; Max-Age=100;";
    Cookie c;
    CPPUNIT_ASSERT(cookie::parse(c, str, "localhost", "/", creationDate));
    CPPUNIT_ASSERT_EQUAL((time_t)creationDate+100, c.getExpiryTime());
    CPPUNIT_ASSERT(c.getPersistent());
  }
  {
    std::string str = "id=; Max-Age=9223372036854775807;";
    Cookie c;
    CPPUNIT_ASSERT(cookie::parse(c, str, "localhost", "/", creationDate));
    CPPUNIT_ASSERT_EQUAL(std::numeric_limits<time_t>::max(), c.getExpiryTime());
    CPPUNIT_ASSERT(c.getPersistent());
  }
  {
    std::string str = "id=; Max-Age=X;";
    Cookie c;
    CPPUNIT_ASSERT(!cookie::parse(c, str, "localhost", "/", creationDate));
  }
  {
    std::string str = "id=; Max-Age=100garbage;";
    Cookie c;
    CPPUNIT_ASSERT(!cookie::parse(c, str, "localhost", "/", creationDate));
  }
  {
    std::string str = "id=; Max-Age=100;expires=Sun, 10-Jun-2007 11:00:00 GMT;";
    Cookie c;
    CPPUNIT_ASSERT(cookie::parse(c, str, "localhost", "/", creationDate));
    CPPUNIT_ASSERT_EQUAL((time_t)creationDate+100, c.getExpiryTime());
    CPPUNIT_ASSERT(c.getPersistent());    
  }
  {
    // Cookie data cannot be parsed.
    std::string str = "id=; expires=2007-10-01 11:00:00 GMT;";
    Cookie c;
    CPPUNIT_ASSERT(!cookie::parse(c, str, "localhost", "/", creationDate));
  }
  {
    std::string str = "id=;";
    Cookie c;
    CPPUNIT_ASSERT(cookie::parse(c, str, "localhost", "/", creationDate));
    CPPUNIT_ASSERT(!c.getPersistent());
  }
  {
    std::string str = "id=; path=abc";
    Cookie c;
    CPPUNIT_ASSERT(cookie::parse(c, str, "localhost", "/", creationDate));
    CPPUNIT_ASSERT_EQUAL(std::string("/"), c.getPath());
  }
  {
    std::string str = "id=; domain=.example.org";
    Cookie c;
    CPPUNIT_ASSERT(cookie::parse(c, str, "www.example.org", "/",creationDate));
  }
  {
    // Fails because request host does not domain-match with cookie
    // domain.
    std::string str = "id=; domain=www.example.org";
    Cookie c;
    CPPUNIT_ASSERT(!cookie::parse(c, str, "example.org", "/", creationDate));
  }
  {
    std::string str = "id=; domain=.";
    Cookie c;
    CPPUNIT_ASSERT(!cookie::parse(c, str, "localhost", "/",creationDate));
  }
  {
    std::string str = "";
    Cookie c;
    CPPUNIT_ASSERT(!cookie::parse(c, str, "localhost", "/",creationDate));
  }
  {
    std::string str = "=";
    Cookie c;
    CPPUNIT_ASSERT(!cookie::parse(c, str, "localhost", "/",creationDate));
  }
  {
    // Use domain last time seen.
    std::string str = "id=;domain=a.example.org;domain=.example.org";
    Cookie c;
    CPPUNIT_ASSERT(cookie::parse(c, str, "b.example.org", "/",creationDate));
    CPPUNIT_ASSERT_EQUAL(std::string("example.org"), c.getDomain());
  }
  {
    // numeric host
    std::string str = "id=;";
    Cookie c;
    CPPUNIT_ASSERT(cookie::parse(c, str, "192.168.0.1", "/",creationDate));
    CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), c.getDomain());
    CPPUNIT_ASSERT(c.getHostOnly());
  }
  {
    // numeric host
    std::string str = "id=; domain=192.168.0.1";
    Cookie c;
    CPPUNIT_ASSERT(cookie::parse(c, str, "192.168.0.1", "/",creationDate));
    CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), c.getDomain());
    CPPUNIT_ASSERT(c.getHostOnly());
  }
  {
    // DQUOTE around cookie-value
    std::string str = "id=\"foo\";";
    Cookie c;
    CPPUNIT_ASSERT(cookie::parse(c, str, "localhost", "/", creationDate));
    CPPUNIT_ASSERT_EQUAL(std::string("foo"), c.getValue());
  }
}

void CookieHelperTest::testReverseDomainLevel()
{
  CPPUNIT_ASSERT_EQUAL(std::string("net.sourceforge.aria2"),
                       cookie::reverseDomainLevel("aria2.sourceforge.net"));
  CPPUNIT_ASSERT_EQUAL(std::string("localhost"),
                       cookie::reverseDomainLevel("localhost"));
  // Behavior check
  CPPUNIT_ASSERT_EQUAL(std::string(""), cookie::reverseDomainLevel(""));
  CPPUNIT_ASSERT_EQUAL(std::string(""), cookie::reverseDomainLevel("."));
  CPPUNIT_ASSERT_EQUAL(std::string("foo."), cookie::reverseDomainLevel(".foo"));
  CPPUNIT_ASSERT_EQUAL(std::string("foo"), cookie::reverseDomainLevel("foo."));
}

} // namespace aria2