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. Sqlite3MozCookieParser parser(A2_TEST_DIR"/cookies.sqlite");
  28. std::vector<Cookie> cookies;
  29. parser.parse(cookies);
  30. CPPUNIT_ASSERT_EQUAL((size_t)3, cookies.size());
  31. const Cookie& localhost = cookies[0];
  32. CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), localhost.getName());
  33. CPPUNIT_ASSERT_EQUAL(std::string("123456789"), localhost.getValue());
  34. CPPUNIT_ASSERT_EQUAL((time_t)INT32_MAX, localhost.getExpiryTime());
  35. CPPUNIT_ASSERT(localhost.getPersistent());
  36. CPPUNIT_ASSERT_EQUAL(std::string("localhost"), localhost.getDomain());
  37. CPPUNIT_ASSERT(localhost.getHostOnly());
  38. CPPUNIT_ASSERT_EQUAL(std::string("/"), localhost.getPath());
  39. CPPUNIT_ASSERT(localhost.getSecure());
  40. CPPUNIT_ASSERT_EQUAL((time_t)3000, localhost.getLastAccessTime());
  41. CPPUNIT_ASSERT_EQUAL((time_t)3000, localhost.getCreationTime());
  42. const Cookie& nullValue = cookies[1];
  43. CPPUNIT_ASSERT_EQUAL(std::string("uid"), nullValue.getName());
  44. CPPUNIT_ASSERT_EQUAL(std::string(""), nullValue.getValue());
  45. CPPUNIT_ASSERT_EQUAL((time_t)0, nullValue.getExpiryTime());
  46. CPPUNIT_ASSERT(nullValue.getPersistent());
  47. CPPUNIT_ASSERT_EQUAL(std::string("null_value.com"), nullValue.getDomain());
  48. CPPUNIT_ASSERT(!nullValue.getHostOnly());
  49. CPPUNIT_ASSERT_EQUAL(std::string("/path/to"), nullValue.getPath());
  50. CPPUNIT_ASSERT(!nullValue.getSecure());
  51. // See row id=3 has no name, so it is skipped.
  52. const Cookie& overflowTime = cookies[2];
  53. CPPUNIT_ASSERT_EQUAL(std::string("foo"), overflowTime.getName());
  54. CPPUNIT_ASSERT_EQUAL(std::string("bar"), overflowTime.getValue());
  55. CPPUNIT_ASSERT((time_t)INT32_MAX <= overflowTime.getExpiryTime());
  56. CPPUNIT_ASSERT(overflowTime.getPersistent());
  57. CPPUNIT_ASSERT_EQUAL(std::string("overflow.time_t.org"),
  58. overflowTime.getDomain());
  59. CPPUNIT_ASSERT(!overflowTime.getHostOnly());
  60. CPPUNIT_ASSERT_EQUAL(std::string("/path/to"), overflowTime.getPath());
  61. CPPUNIT_ASSERT(!overflowTime.getSecure());
  62. // See row id=5 has bad path, so it is skipped.
  63. }
  64. void Sqlite3CookieParserTest::testMozParse_fileNotFound()
  65. {
  66. Sqlite3MozCookieParser parser("fileNotFound");
  67. try {
  68. std::vector<Cookie> cookies;
  69. parser.parse(cookies);
  70. CPPUNIT_FAIL("exception must be thrown.");
  71. } catch(RecoverableException& e) {
  72. // SUCCESS
  73. const char A2_SQLITE_ERR[] = "SQLite3 database is not opened";
  74. CPPUNIT_ASSERT(util::startsWith(e.what(), e.what()+strlen(e.what()),
  75. A2_SQLITE_ERR, vend(A2_SQLITE_ERR)-1));
  76. }
  77. }
  78. void Sqlite3CookieParserTest::testMozParse_badfile()
  79. {
  80. Sqlite3MozCookieParser parser(A2_TEST_DIR"/badcookies.sqlite");
  81. try {
  82. std::vector<Cookie> cookies;
  83. parser.parse(cookies);
  84. CPPUNIT_FAIL("exception must be thrown.");
  85. } catch(RecoverableException& e) {
  86. // SUCCESS
  87. }
  88. }
  89. void Sqlite3CookieParserTest::testChromumParse()
  90. {
  91. Sqlite3ChromiumCookieParser parser(A2_TEST_DIR"/chromium_cookies.sqlite");
  92. std::vector<Cookie> cookies;
  93. parser.parse(cookies);
  94. CPPUNIT_ASSERT_EQUAL((size_t)3, cookies.size());
  95. const Cookie& 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. const Cookie& 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. const Cookie& localnet = cookies[2];
  115. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), localnet.getDomain());
  116. CPPUNIT_ASSERT(sfjp.getHostOnly());
  117. CPPUNIT_ASSERT_EQUAL((time_t)3000, localnet.getLastAccessTime());
  118. CPPUNIT_ASSERT_EQUAL((time_t)3000, localnet.getCreationTime());
  119. }
  120. } // namespace aria2