Sqlite3CookieParserTest.cc 4.8 KB

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