CookieHelperTest.cc 8.0 KB

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