NsCookieParserTest.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "NsCookieParser.h"
  2. #include <iostream>
  3. #include <limits>
  4. #include <cppunit/extensions/HelperMacros.h>
  5. #include "RecoverableException.h"
  6. #include "util.h"
  7. #include "Cookie.h"
  8. namespace aria2 {
  9. class NsCookieParserTest : public CppUnit::TestFixture {
  10. CPPUNIT_TEST_SUITE(NsCookieParserTest);
  11. CPPUNIT_TEST(testParse);
  12. CPPUNIT_TEST(testParse_fileNotFound);
  13. CPPUNIT_TEST_SUITE_END();
  14. public:
  15. void setUp() {}
  16. void tearDown() {}
  17. void testParse();
  18. void testParse_fileNotFound();
  19. };
  20. CPPUNIT_TEST_SUITE_REGISTRATION(NsCookieParserTest);
  21. void NsCookieParserTest::testParse()
  22. {
  23. NsCookieParser parser;
  24. time_t now = 0;
  25. auto cookies = parser.parse(A2_TEST_DIR "/nscookietest.txt", now);
  26. CPPUNIT_ASSERT_EQUAL((size_t)5, cookies.size());
  27. auto c = cookies[0].get();
  28. CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), c->getName());
  29. CPPUNIT_ASSERT_EQUAL(std::string("123456789"), c->getValue());
  30. CPPUNIT_ASSERT_EQUAL((time_t)INT32_MAX, c->getExpiryTime());
  31. CPPUNIT_ASSERT(c->getPersistent());
  32. CPPUNIT_ASSERT_EQUAL(std::string("localhost"), c->getDomain());
  33. CPPUNIT_ASSERT(c->getHostOnly());
  34. CPPUNIT_ASSERT_EQUAL(std::string("/"), c->getPath());
  35. CPPUNIT_ASSERT(c->getSecure());
  36. c = cookies[1].get();
  37. CPPUNIT_ASSERT_EQUAL(std::string("user"), c->getName());
  38. CPPUNIT_ASSERT_EQUAL(std::string("me"), c->getValue());
  39. CPPUNIT_ASSERT_EQUAL((time_t)1000, c->getExpiryTime());
  40. CPPUNIT_ASSERT(c->getPersistent());
  41. CPPUNIT_ASSERT_EQUAL(std::string("expired"), c->getDomain());
  42. CPPUNIT_ASSERT(c->getHostOnly());
  43. CPPUNIT_ASSERT_EQUAL(std::string("/"), c->getPath());
  44. CPPUNIT_ASSERT(!c->getSecure());
  45. c = cookies[2].get();
  46. CPPUNIT_ASSERT_EQUAL(std::string("passwd"), c->getName());
  47. CPPUNIT_ASSERT_EQUAL(std::string("secret"), c->getValue());
  48. CPPUNIT_ASSERT_EQUAL(std::numeric_limits<time_t>::max(), c->getExpiryTime());
  49. CPPUNIT_ASSERT(!c->getPersistent());
  50. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), c->getDomain());
  51. CPPUNIT_ASSERT(c->getHostOnly());
  52. CPPUNIT_ASSERT_EQUAL(std::string("/cgi-bin"), c->getPath());
  53. CPPUNIT_ASSERT(!c->getSecure());
  54. c = cookies[3].get();
  55. CPPUNIT_ASSERT_EQUAL(std::string("TAX"), c->getName());
  56. CPPUNIT_ASSERT_EQUAL(std::string("1000"), c->getValue());
  57. CPPUNIT_ASSERT((time_t)INT32_MAX <= c->getExpiryTime());
  58. CPPUNIT_ASSERT(c->getPersistent());
  59. CPPUNIT_ASSERT_EQUAL(std::string("overflow"), c->getDomain());
  60. CPPUNIT_ASSERT(c->getHostOnly());
  61. CPPUNIT_ASSERT_EQUAL(std::string("/"), c->getPath());
  62. CPPUNIT_ASSERT(!c->getSecure());
  63. c = cookies[4].get();
  64. CPPUNIT_ASSERT_EQUAL(std::string("novalue"), c->getName());
  65. CPPUNIT_ASSERT_EQUAL(std::string(""), c->getValue());
  66. CPPUNIT_ASSERT_EQUAL((time_t)INT32_MAX, c->getExpiryTime());
  67. CPPUNIT_ASSERT(c->getPersistent());
  68. CPPUNIT_ASSERT_EQUAL(std::string("example.org"), c->getDomain());
  69. CPPUNIT_ASSERT(!c->getHostOnly());
  70. CPPUNIT_ASSERT_EQUAL(std::string("/"), c->getPath());
  71. CPPUNIT_ASSERT(!c->getSecure());
  72. }
  73. void NsCookieParserTest::testParse_fileNotFound()
  74. {
  75. NsCookieParser parser;
  76. try {
  77. time_t now = 0;
  78. parser.parse("fileNotFound", now);
  79. CPPUNIT_FAIL("exception must be thrown.");
  80. }
  81. catch (RecoverableException& e) {
  82. // SUCCESS
  83. }
  84. }
  85. } // namespace aria2