CookieTest.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. auto a = createCookie("k", "v", "localhost", true, "/", false);
  28. auto b = createCookie("k", "v", "localhost", true, "/", true);
  29. auto wrongPath = createCookie("k", "v", "localhost", true, "/a", false);
  30. auto wrongDomain = createCookie("k", "v", "mydomain", true, "/", false);
  31. auto wrongName = createCookie("h", "v", "localhost", true, "/a", false);
  32. auto caseSensitiveName =
  33. createCookie("K", "v", "localhost", true, "/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. auto c = createCookie("k", "v", "aria2.org", false, "/downloads", false);
  43. auto c2 = createCookie("k", "v", "aria2.org", false, "/downloads", false);
  44. auto c3 = createCookie("k", "v", "aria2.org", true, "/downloads", false);
  45. auto 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. auto secureCookie =
  57. createCookie("k", "v", "secure.aria2.org", false, "/", 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. auto expireTest =
  63. createCookie("k", "v", 1000, "aria2.org", false, "/", 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. auto fromNumericHost =
  68. createCookie("k", "v", "192.168.1.1", true, "/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. auto cookie = createCookie("k", "v", 1000, "localhost", true, "/", false);
  77. CPPUNIT_ASSERT(cookie->isExpired(1001));
  78. CPPUNIT_ASSERT(!cookie->isExpired(1000));
  79. CPPUNIT_ASSERT(!cookie->isExpired(999));
  80. auto sessionCookie = createCookie("k", "v", "localhost", true, "/", false);
  81. CPPUNIT_ASSERT(!sessionCookie->isExpired(INT32_MAX));
  82. }
  83. void CookieTest::testToNsCookieFormat()
  84. {
  85. CPPUNIT_ASSERT_EQUAL(
  86. std::string(".domain.org\tTRUE\t/\tFALSE\t12345678\thello\tworld"),
  87. createCookie("hello", "world", 12345678, "domain.org", false, "/", false)
  88. ->toNsCookieFormat());
  89. // Session cookie
  90. CPPUNIT_ASSERT_EQUAL(
  91. std::string("domain.org\tFALSE\t/\tTRUE\t0\thello\tworld"),
  92. createCookie("hello", "world", "domain.org", true, "/", true)
  93. ->toNsCookieFormat());
  94. }
  95. } // namespace aria2