CookieHelperTest.cc 7.7 KB

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