CookieHelperTest.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include "cookie_helper.h"
  2. #include <limits>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "Exception.h"
  5. #include "util.h"
  6. #include "Cookie.h"
  7. namespace aria2 {
  8. class CookieHelperTest : public CppUnit::TestFixture {
  9. CPPUNIT_TEST_SUITE(CookieHelperTest);
  10. CPPUNIT_TEST(testParseDate);
  11. CPPUNIT_TEST(testDomainMatch);
  12. CPPUNIT_TEST(testPathMatch);
  13. CPPUNIT_TEST(testParse);
  14. CPPUNIT_TEST(testReverseDomainLevel);
  15. CPPUNIT_TEST_SUITE_END();
  16. public:
  17. void testParseDate();
  18. void testDomainMatch();
  19. void testPathMatch();
  20. void testParse();
  21. void testReverseDomainLevel();
  22. };
  23. CPPUNIT_TEST_SUITE_REGISTRATION(CookieHelperTest);
  24. void CookieHelperTest::testParseDate()
  25. {
  26. // RFC1123
  27. time_t time = 0;
  28. std::string s = "Sat, 06 Sep 2008 15:26:33 GMT";
  29. CPPUNIT_ASSERT(cookie::parseDate(time, s.begin(), s.end()));
  30. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, time);
  31. // RFC850
  32. s = "Saturday, 06-Sep-08 15:26:33 GMT";
  33. CPPUNIT_ASSERT(cookie::parseDate(time, s.begin(), s.end()));
  34. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, time);
  35. // ANSI C's asctime()
  36. s = "Sun Sep 6 15:26:33 2008";
  37. CPPUNIT_ASSERT(cookie::parseDate(time, s.begin(), s.end()));
  38. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, time);
  39. s = "Thu Jan 1 0:0:0 1970";
  40. CPPUNIT_ASSERT(cookie::parseDate(time, s.begin(), s.end()));
  41. CPPUNIT_ASSERT_EQUAL((time_t)0, time);
  42. s = "Thu Jan 1 1970 0:";
  43. CPPUNIT_ASSERT(!cookie::parseDate(time, s.begin(), s.end()));
  44. s = "Thu Jan 1 1970 0:0";
  45. CPPUNIT_ASSERT(!cookie::parseDate(time, s.begin(), s.end()));
  46. s = "Thu Jan 1 1970 0:0:";
  47. CPPUNIT_ASSERT(!cookie::parseDate(time, s.begin(), s.end()));
  48. // Leap year
  49. s = "Tue, 29 Feb 2000 00:00:00 GMT";
  50. CPPUNIT_ASSERT(cookie::parseDate(time, s.begin(), s.end()));
  51. s = "Thu, 29 Feb 2001 00:00:00 GMT";
  52. CPPUNIT_ASSERT(!cookie::parseDate(time, s.begin(), s.end()));
  53. }
  54. void CookieHelperTest::testDomainMatch()
  55. {
  56. CPPUNIT_ASSERT(cookie::domainMatch("localhost", "localhost"));
  57. CPPUNIT_ASSERT(cookie::domainMatch("192.168.0.1", "192.168.0.1"));
  58. CPPUNIT_ASSERT(cookie::domainMatch("www.example.org", "example.org"));
  59. CPPUNIT_ASSERT(!cookie::domainMatch("192.168.0.1", "0.1"));
  60. CPPUNIT_ASSERT(!cookie::domainMatch("example.org", "example.com"));
  61. CPPUNIT_ASSERT(!cookie::domainMatch("example.org", "www.example.org"));
  62. }
  63. void CookieHelperTest::testPathMatch()
  64. {
  65. CPPUNIT_ASSERT(cookie::pathMatch("/", "/"));
  66. CPPUNIT_ASSERT(cookie::pathMatch("/foo/", "/foo"));
  67. CPPUNIT_ASSERT(!cookie::pathMatch("/bar/", "/foo"));
  68. CPPUNIT_ASSERT(!cookie::pathMatch("/foo", "/bar/foo"));
  69. CPPUNIT_ASSERT(cookie::pathMatch("/foo/bar", "/foo/"));
  70. }
  71. void CookieHelperTest::testParse()
  72. {
  73. time_t creationDate = 141;
  74. {
  75. std::string str = "ID=123456789; expires=Sun, 10-Jun-2007 11:00:00 GMT;"
  76. "path=/foo; domain=localhost; secure;httpOnly ";
  77. auto c = cookie::parse(str, "localhost", "/", creationDate);
  78. CPPUNIT_ASSERT(c);
  79. CPPUNIT_ASSERT_EQUAL(std::string("ID"), c->getName());
  80. CPPUNIT_ASSERT_EQUAL(std::string("123456789"), c->getValue());
  81. CPPUNIT_ASSERT_EQUAL((time_t)1181473200, c->getExpiryTime());
  82. CPPUNIT_ASSERT(c->getPersistent());
  83. CPPUNIT_ASSERT_EQUAL(std::string("localhost"), c->getDomain());
  84. CPPUNIT_ASSERT(!c->getHostOnly());
  85. CPPUNIT_ASSERT_EQUAL(std::string("/foo"), c->getPath());
  86. CPPUNIT_ASSERT(c->getSecure());
  87. CPPUNIT_ASSERT(c->getHttpOnly());
  88. CPPUNIT_ASSERT_EQUAL((time_t)141, c->getCreationTime());
  89. CPPUNIT_ASSERT_EQUAL((time_t)141, c->getLastAccessTime());
  90. }
  91. {
  92. std::string str = "id=; Max-Age=0;";
  93. auto c = cookie::parse(str, "localhost", "/", creationDate);
  94. CPPUNIT_ASSERT(c);
  95. CPPUNIT_ASSERT_EQUAL(std::string("id"), c->getName());
  96. CPPUNIT_ASSERT_EQUAL((time_t)0, c->getExpiryTime());
  97. CPPUNIT_ASSERT(c->getPersistent());
  98. CPPUNIT_ASSERT_EQUAL(std::string("localhost"), c->getDomain());
  99. CPPUNIT_ASSERT(c->getHostOnly());
  100. CPPUNIT_ASSERT_EQUAL(std::string("/"), c->getPath());
  101. CPPUNIT_ASSERT(!c->getSecure());
  102. CPPUNIT_ASSERT(!c->getHttpOnly());
  103. }
  104. {
  105. std::string str = "id=; Max-Age=-100;";
  106. auto c = cookie::parse(str, "localhost", "/", creationDate);
  107. CPPUNIT_ASSERT(c);
  108. CPPUNIT_ASSERT_EQUAL((time_t)0, c->getExpiryTime());
  109. CPPUNIT_ASSERT(c->getPersistent());
  110. }
  111. {
  112. std::string str = "id=; Max-Age=100;";
  113. auto c = cookie::parse(str, "localhost", "/", creationDate);
  114. CPPUNIT_ASSERT(c);
  115. CPPUNIT_ASSERT_EQUAL((time_t)creationDate + 100, c->getExpiryTime());
  116. CPPUNIT_ASSERT(c->getPersistent());
  117. }
  118. {
  119. std::string str = "id=; Max-Age=9223372036854775807;";
  120. auto c = cookie::parse(str, "localhost", "/", creationDate);
  121. CPPUNIT_ASSERT(c);
  122. CPPUNIT_ASSERT_EQUAL(std::numeric_limits<time_t>::max(),
  123. c->getExpiryTime());
  124. CPPUNIT_ASSERT(c->getPersistent());
  125. }
  126. {
  127. std::string str = "id=; Max-Age=X;";
  128. CPPUNIT_ASSERT(!cookie::parse(str, "localhost", "/", creationDate));
  129. }
  130. {
  131. std::string str = "id=; Max-Age=100garbage;";
  132. CPPUNIT_ASSERT(!cookie::parse(str, "localhost", "/", creationDate));
  133. }
  134. {
  135. std::string str = "id=; Max-Age=100;expires=Sun, 10-Jun-2007 11:00:00 GMT;";
  136. auto c = cookie::parse(str, "localhost", "/", creationDate);
  137. CPPUNIT_ASSERT(c);
  138. CPPUNIT_ASSERT_EQUAL((time_t)creationDate + 100, c->getExpiryTime());
  139. CPPUNIT_ASSERT(c->getPersistent());
  140. }
  141. {
  142. // Cookie data cannot be parsed.
  143. std::string str = "id=; expires=2007-10-01 11:00:00 GMT;";
  144. CPPUNIT_ASSERT(!cookie::parse(str, "localhost", "/", creationDate));
  145. }
  146. {
  147. std::string str = "id=;";
  148. auto c = cookie::parse(str, "localhost", "/", creationDate);
  149. CPPUNIT_ASSERT(c);
  150. CPPUNIT_ASSERT(!c->getPersistent());
  151. }
  152. {
  153. std::string str = "id=; path=abc";
  154. auto c = cookie::parse(str, "localhost", "/", creationDate);
  155. CPPUNIT_ASSERT(c);
  156. CPPUNIT_ASSERT_EQUAL(std::string("/"), c->getPath());
  157. }
  158. {
  159. std::string str = "id=; domain=.example.org";
  160. CPPUNIT_ASSERT(cookie::parse(str, "www.example.org", "/", creationDate));
  161. }
  162. {
  163. // Fails because request host does not domain-match with cookie
  164. // domain.
  165. std::string str = "id=; domain=www.example.org";
  166. CPPUNIT_ASSERT(!cookie::parse(str, "example.org", "/", creationDate));
  167. }
  168. {
  169. std::string str = "id=; domain=.";
  170. CPPUNIT_ASSERT(!cookie::parse(str, "localhost", "/", creationDate));
  171. }
  172. {
  173. std::string str = "";
  174. CPPUNIT_ASSERT(!cookie::parse(str, "localhost", "/", creationDate));
  175. }
  176. {
  177. std::string str = "=";
  178. CPPUNIT_ASSERT(!cookie::parse(str, "localhost", "/", creationDate));
  179. }
  180. {
  181. // Use domain last time seen.
  182. std::string str = "id=;domain=a.example.org;domain=.example.org";
  183. auto c = cookie::parse(str, "b.example.org", "/", creationDate);
  184. CPPUNIT_ASSERT(c);
  185. CPPUNIT_ASSERT_EQUAL(std::string("example.org"), c->getDomain());
  186. }
  187. {
  188. // numeric host
  189. std::string str = "id=;";
  190. auto c = cookie::parse(str, "192.168.0.1", "/", creationDate);
  191. CPPUNIT_ASSERT(c);
  192. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), c->getDomain());
  193. CPPUNIT_ASSERT(c->getHostOnly());
  194. }
  195. {
  196. // numeric host
  197. std::string str = "id=; domain=192.168.0.1";
  198. auto c = cookie::parse(str, "192.168.0.1", "/", creationDate);
  199. CPPUNIT_ASSERT(c);
  200. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), c->getDomain());
  201. CPPUNIT_ASSERT(c->getHostOnly());
  202. }
  203. {
  204. // DQUOTE around cookie-value
  205. std::string str = "id=\"foo\";";
  206. auto c = cookie::parse(str, "localhost", "/", creationDate);
  207. CPPUNIT_ASSERT(c);
  208. CPPUNIT_ASSERT_EQUAL(std::string("foo"), c->getValue());
  209. }
  210. {
  211. // Default path
  212. std::string str = "id=;";
  213. auto c = cookie::parse(str, "localhost", "/foo", creationDate);
  214. CPPUNIT_ASSERT(c);
  215. CPPUNIT_ASSERT_EQUAL(std::string("/foo"), c->getPath());
  216. }
  217. }
  218. void CookieHelperTest::testReverseDomainLevel()
  219. {
  220. CPPUNIT_ASSERT_EQUAL(std::string("net.sourceforge.aria2"),
  221. cookie::reverseDomainLevel("aria2.sourceforge.net"));
  222. CPPUNIT_ASSERT_EQUAL(std::string("localhost"),
  223. cookie::reverseDomainLevel("localhost"));
  224. // Behavior check
  225. CPPUNIT_ASSERT_EQUAL(std::string(""), cookie::reverseDomainLevel(""));
  226. CPPUNIT_ASSERT_EQUAL(std::string(""), cookie::reverseDomainLevel("."));
  227. CPPUNIT_ASSERT_EQUAL(std::string("foo."), cookie::reverseDomainLevel(".foo"));
  228. CPPUNIT_ASSERT_EQUAL(std::string("foo"), cookie::reverseDomainLevel("foo."));
  229. }
  230. } // namespace aria2