CookieTest.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "Cookie.h"
  2. #include <iostream>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "Exception.h"
  5. #include "util.h"
  6. #include "TimeA2.h"
  7. #include "TestUtil.h"
  8. namespace aria2 {
  9. class CookieTest:public CppUnit::TestFixture {
  10. CPPUNIT_TEST_SUITE(CookieTest);
  11. CPPUNIT_TEST(testOperatorEqual);
  12. CPPUNIT_TEST(testMatch);
  13. CPPUNIT_TEST(testIsExpired);
  14. CPPUNIT_TEST(testToNsCookieFormat);
  15. CPPUNIT_TEST_SUITE_END();
  16. public:
  17. void setUp() {}
  18. void tearDown() {}
  19. void testOperatorEqual();
  20. void testMatch();
  21. void testIsExpired();
  22. void testToNsCookieFormat();
  23. };
  24. CPPUNIT_TEST_SUITE_REGISTRATION(CookieTest);
  25. void CookieTest::testOperatorEqual()
  26. {
  27. Cookie a(createCookie("k", "v", "localhost", true, "/", false));
  28. Cookie b(createCookie("k", "v", "localhost", true, "/", true));
  29. Cookie wrongPath(createCookie("k", "v", "localhost", true, "/a", false));
  30. Cookie wrongDomain(createCookie("k", "v", "mydomain", true, "/", false));
  31. Cookie wrongName(createCookie("h", "v", "localhost", true, "/a", false));
  32. Cookie caseSensitiveName(createCookie("K", "v", "localhost", true,
  33. "/a", false));
  34. CPPUNIT_ASSERT(a == b);
  35. CPPUNIT_ASSERT(!(a == wrongPath));
  36. CPPUNIT_ASSERT(!(a == wrongDomain));
  37. CPPUNIT_ASSERT(!(a == wrongName));
  38. CPPUNIT_ASSERT(!(a == caseSensitiveName));
  39. }
  40. void CookieTest::testMatch()
  41. {
  42. Cookie c(createCookie("k", "v", "aria2.org", false, "/downloads", false));
  43. Cookie c2(createCookie("k", "v", "aria2.org", false, "/downloads", false));
  44. Cookie c3(createCookie("k", "v", "aria2.org", true, "/downloads", false));
  45. Cookie c4(createCookie("k", "v", "localhost", true, "/downloads", false));
  46. CPPUNIT_ASSERT(c.match("www.aria2.org", "/downloads", 0, false));
  47. CPPUNIT_ASSERT(c2.match("www.aria2.org", "/downloads", 0, false));
  48. CPPUNIT_ASSERT(!c.match("www.aria.org", "/downloads", 0, false));
  49. CPPUNIT_ASSERT(!c.match("www.aria2.org", "/examples", 0, false));
  50. CPPUNIT_ASSERT(c.match("www.aria2.org", "/downloads", 0, true));
  51. CPPUNIT_ASSERT(c.match("www.aria2.org", "/downloads/latest", 0, false));
  52. CPPUNIT_ASSERT(!c.match("www.aria2.org", "/downloadss/latest", 0, false));
  53. CPPUNIT_ASSERT(!c.match("www.aria2.org", "/DOWNLOADS", 0, false));
  54. CPPUNIT_ASSERT(!c3.match("www.aria2.org", "/downloads", 0, false));
  55. CPPUNIT_ASSERT(c4.match("localhost", "/downloads", 0, false));
  56. Cookie secureCookie(createCookie("k", "v", "secure.aria2.org", false,
  57. "/", true));
  58. CPPUNIT_ASSERT(secureCookie.match("secure.aria2.org", "/", 0, true));
  59. CPPUNIT_ASSERT(!secureCookie.match("secure.aria2.org", "/", 0, false));
  60. CPPUNIT_ASSERT(!secureCookie.match("ssecure.aria2.org", "/", 0, true));
  61. CPPUNIT_ASSERT(secureCookie.match("www.secure.aria2.org", "/", 0, true));
  62. Cookie expireTest(createCookie("k", "v", 1000, "aria2.org", false,
  63. "/", false));
  64. CPPUNIT_ASSERT(expireTest.match("www.aria2.org", "/", 999, false));
  65. CPPUNIT_ASSERT(expireTest.match("www.aria2.org", "/", 1000, false));
  66. CPPUNIT_ASSERT(!expireTest.match("www.aria2.org", "/", 1001, false));
  67. Cookie fromNumericHost(createCookie("k", "v", "192.168.1.1", true,
  68. "/foo", false));
  69. CPPUNIT_ASSERT(fromNumericHost.match("192.168.1.1", "/foo", 0, false));
  70. CPPUNIT_ASSERT(!fromNumericHost.match("www.aria2.org", "/foo", 0, false));
  71. CPPUNIT_ASSERT(!fromNumericHost.match("1.192.168.1.1", "/foo", 0, false));
  72. CPPUNIT_ASSERT(!fromNumericHost.match("192.168.1.1", "/", 0, false));
  73. }
  74. void CookieTest::testIsExpired()
  75. {
  76. Cookie cookie(createCookie("k", "v", 1000, "localhost", true,
  77. "/", false));
  78. CPPUNIT_ASSERT(cookie.isExpired(1001));
  79. CPPUNIT_ASSERT(!cookie.isExpired(1000));
  80. CPPUNIT_ASSERT(!cookie.isExpired(999));
  81. Cookie sessionCookie(createCookie("k", "v", "localhost", true, "/", false));
  82. CPPUNIT_ASSERT(!sessionCookie.isExpired(INT32_MAX));
  83. }
  84. void CookieTest::testToNsCookieFormat()
  85. {
  86. CPPUNIT_ASSERT_EQUAL
  87. (std::string(".domain.org\tTRUE\t/\tFALSE\t12345678\thello\tworld"),
  88. createCookie("hello", "world", 12345678, "domain.org", false, "/",false)
  89. .toNsCookieFormat());
  90. // Session cookie
  91. CPPUNIT_ASSERT_EQUAL
  92. (std::string("domain.org\tFALSE\t/\tTRUE\t0\thello\tworld"),
  93. createCookie("hello", "world", "domain.org", true, "/", true)
  94. .toNsCookieFormat());
  95. }
  96. } // namespace aria2