Sqlite3CookieParserTest.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "Sqlite3CookieParserImpl.h"
  2. #include <cstring>
  3. #include <iostream>
  4. #include <cppunit/extensions/HelperMacros.h>
  5. #include "RecoverableException.h"
  6. #include "util.h"
  7. #include "array_fun.h"
  8. namespace aria2 {
  9. class Sqlite3CookieParserTest : public CppUnit::TestFixture {
  10. CPPUNIT_TEST_SUITE(Sqlite3CookieParserTest);
  11. CPPUNIT_TEST(testMozParse);
  12. CPPUNIT_TEST(testMozParse_fileNotFound);
  13. CPPUNIT_TEST(testMozParse_badfile);
  14. CPPUNIT_TEST(testChromumParse);
  15. CPPUNIT_TEST_SUITE_END();
  16. public:
  17. void setUp() {}
  18. void tearDown() {}
  19. void testMozParse();
  20. void testMozParse_fileNotFound();
  21. void testMozParse_badfile();
  22. void testChromumParse();
  23. };
  24. CPPUNIT_TEST_SUITE_REGISTRATION(Sqlite3CookieParserTest);
  25. void Sqlite3CookieParserTest::testMozParse()
  26. {
  27. auto parser = Sqlite3MozCookieParser{A2_TEST_DIR "/cookies.sqlite"};
  28. auto cookies = parser.parse();
  29. CPPUNIT_ASSERT_EQUAL((size_t)3, cookies.size());
  30. auto& localhost = cookies[0];
  31. CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), localhost->getName());
  32. CPPUNIT_ASSERT_EQUAL(std::string("123456789"), localhost->getValue());
  33. CPPUNIT_ASSERT_EQUAL((time_t)INT32_MAX, localhost->getExpiryTime());
  34. CPPUNIT_ASSERT(localhost->getPersistent());
  35. CPPUNIT_ASSERT_EQUAL(std::string("localhost"), localhost->getDomain());
  36. CPPUNIT_ASSERT(localhost->getHostOnly());
  37. CPPUNIT_ASSERT_EQUAL(std::string("/"), localhost->getPath());
  38. CPPUNIT_ASSERT(localhost->getSecure());
  39. CPPUNIT_ASSERT_EQUAL((time_t)3000, localhost->getLastAccessTime());
  40. CPPUNIT_ASSERT_EQUAL((time_t)3000, localhost->getCreationTime());
  41. auto& nullValue = cookies[1];
  42. CPPUNIT_ASSERT_EQUAL(std::string("uid"), nullValue->getName());
  43. CPPUNIT_ASSERT_EQUAL(std::string(""), nullValue->getValue());
  44. CPPUNIT_ASSERT_EQUAL((time_t)0, nullValue->getExpiryTime());
  45. CPPUNIT_ASSERT(nullValue->getPersistent());
  46. CPPUNIT_ASSERT_EQUAL(std::string("null_value.com"), nullValue->getDomain());
  47. CPPUNIT_ASSERT(!nullValue->getHostOnly());
  48. CPPUNIT_ASSERT_EQUAL(std::string("/path/to"), nullValue->getPath());
  49. CPPUNIT_ASSERT(!nullValue->getSecure());
  50. // See row id=3 has no name, so it is skipped.
  51. auto& overflowTime = cookies[2];
  52. CPPUNIT_ASSERT_EQUAL(std::string("foo"), overflowTime->getName());
  53. CPPUNIT_ASSERT_EQUAL(std::string("bar"), overflowTime->getValue());
  54. CPPUNIT_ASSERT((time_t)INT32_MAX <= overflowTime->getExpiryTime());
  55. CPPUNIT_ASSERT(overflowTime->getPersistent());
  56. CPPUNIT_ASSERT_EQUAL(std::string("overflow.time_t.org"),
  57. overflowTime->getDomain());
  58. CPPUNIT_ASSERT(!overflowTime->getHostOnly());
  59. CPPUNIT_ASSERT_EQUAL(std::string("/path/to"), overflowTime->getPath());
  60. CPPUNIT_ASSERT(!overflowTime->getSecure());
  61. // See row id=5 has bad path, so it is skipped.
  62. }
  63. void Sqlite3CookieParserTest::testMozParse_fileNotFound()
  64. {
  65. auto parser = Sqlite3MozCookieParser{"fileNotFound"};
  66. try {
  67. parser.parse();
  68. CPPUNIT_FAIL("exception must be thrown.");
  69. }
  70. catch (RecoverableException& e) {
  71. // SUCCESS
  72. const char A2_SQLITE_ERR[] = "SQLite3 database is not opened";
  73. CPPUNIT_ASSERT(util::startsWith(e.what(), e.what() + strlen(e.what()),
  74. A2_SQLITE_ERR,
  75. std::end(A2_SQLITE_ERR) - 1));
  76. }
  77. }
  78. void Sqlite3CookieParserTest::testMozParse_badfile()
  79. {
  80. auto parser = Sqlite3MozCookieParser{A2_TEST_DIR "/badcookies.sqlite"};
  81. try {
  82. parser.parse();
  83. CPPUNIT_FAIL("exception must be thrown.");
  84. }
  85. catch (RecoverableException& e) {
  86. // SUCCESS
  87. }
  88. }
  89. void Sqlite3CookieParserTest::testChromumParse()
  90. {
  91. auto parser =
  92. Sqlite3ChromiumCookieParser{A2_TEST_DIR "/chromium_cookies.sqlite"};
  93. auto cookies = parser.parse();
  94. CPPUNIT_ASSERT_EQUAL((size_t)3, cookies.size());
  95. auto& sfnet = cookies[0];
  96. CPPUNIT_ASSERT_EQUAL(std::string("mykey"), sfnet->getName());
  97. CPPUNIT_ASSERT_EQUAL(std::string("pass"), sfnet->getValue());
  98. CPPUNIT_ASSERT_EQUAL((time_t)12345679, sfnet->getExpiryTime());
  99. CPPUNIT_ASSERT(sfnet->getPersistent());
  100. CPPUNIT_ASSERT_EQUAL(std::string("aria2.sourceforge.net"),
  101. sfnet->getDomain());
  102. CPPUNIT_ASSERT(!sfnet->getHostOnly());
  103. CPPUNIT_ASSERT_EQUAL(std::string("/"), sfnet->getPath());
  104. CPPUNIT_ASSERT(!sfnet->getSecure());
  105. auto& sfjp = cookies[1];
  106. CPPUNIT_ASSERT_EQUAL(std::string("myseckey"), sfjp->getName());
  107. CPPUNIT_ASSERT_EQUAL(std::string("pass2"), sfjp->getValue());
  108. CPPUNIT_ASSERT_EQUAL((time_t)0, sfjp->getExpiryTime());
  109. CPPUNIT_ASSERT(sfjp->getPersistent());
  110. CPPUNIT_ASSERT_EQUAL(std::string("aria2.sourceforge.jp"), sfjp->getDomain());
  111. CPPUNIT_ASSERT(sfjp->getHostOnly());
  112. CPPUNIT_ASSERT_EQUAL(std::string("/profile"), sfjp->getPath());
  113. CPPUNIT_ASSERT(sfjp->getSecure());
  114. auto& localnet = cookies[2];
  115. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), localnet->getDomain());
  116. CPPUNIT_ASSERT(localnet->getHostOnly());
  117. CPPUNIT_ASSERT_EQUAL((time_t)3000, localnet->getLastAccessTime());
  118. CPPUNIT_ASSERT_EQUAL((time_t)3000, localnet->getCreationTime());
  119. }
  120. } // namespace aria2