Sqlite3CookieParserTest.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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, 0);
  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. const Cookie& nullValue = cookies[1];
  39. CPPUNIT_ASSERT_EQUAL(std::string("uid"), nullValue.getName());
  40. CPPUNIT_ASSERT_EQUAL(std::string(""), nullValue.getValue());
  41. CPPUNIT_ASSERT_EQUAL((time_t)0, nullValue.getExpiryTime());
  42. CPPUNIT_ASSERT(nullValue.getPersistent());
  43. CPPUNIT_ASSERT_EQUAL(std::string("null_value.com"), nullValue.getDomain());
  44. CPPUNIT_ASSERT(!nullValue.getHostOnly());
  45. CPPUNIT_ASSERT_EQUAL(std::string("/path/to"), nullValue.getPath());
  46. CPPUNIT_ASSERT(!nullValue.getSecure());
  47. // See row id=3 has no name, so it is skipped.
  48. const Cookie& overflowTime = cookies[2];
  49. CPPUNIT_ASSERT_EQUAL(std::string("foo"), overflowTime.getName());
  50. CPPUNIT_ASSERT_EQUAL(std::string("bar"), overflowTime.getValue());
  51. CPPUNIT_ASSERT((time_t)INT32_MAX <= overflowTime.getExpiryTime());
  52. CPPUNIT_ASSERT(overflowTime.getPersistent());
  53. CPPUNIT_ASSERT_EQUAL(std::string("overflow.time_t.org"),
  54. overflowTime.getDomain());
  55. CPPUNIT_ASSERT(!overflowTime.getHostOnly());
  56. CPPUNIT_ASSERT_EQUAL(std::string("/path/to"), overflowTime.getPath());
  57. CPPUNIT_ASSERT(!overflowTime.getSecure());
  58. // See row id=5 has bad path, so it is skipped.
  59. }
  60. void Sqlite3CookieParserTest::testMozParse_fileNotFound()
  61. {
  62. Sqlite3MozCookieParser parser("fileNotFound");
  63. try {
  64. std::vector<Cookie> cookies;
  65. parser.parse(cookies, 0);
  66. CPPUNIT_FAIL("exception must be thrown.");
  67. } catch(RecoverableException& e) {
  68. // SUCCESS
  69. CPPUNIT_ASSERT(util::startsWith(e.what(),
  70. "SQLite3 database is not opened"));
  71. }
  72. }
  73. void Sqlite3CookieParserTest::testMozParse_badfile()
  74. {
  75. Sqlite3MozCookieParser parser("badcookies.sqlite");
  76. try {
  77. std::vector<Cookie> cookies;
  78. parser.parse(cookies, 0);
  79. CPPUNIT_FAIL("exception must be thrown.");
  80. } catch(RecoverableException& e) {
  81. // SUCCESS
  82. }
  83. }
  84. void Sqlite3CookieParserTest::testChromumParse()
  85. {
  86. Sqlite3ChromiumCookieParser parser("chromium_cookies.sqlite");
  87. std::vector<Cookie> cookies;
  88. parser.parse(cookies, 0);
  89. CPPUNIT_ASSERT_EQUAL((size_t)3, cookies.size());
  90. const Cookie& sfnet = cookies[0];
  91. CPPUNIT_ASSERT_EQUAL(std::string("mykey"), sfnet.getName());
  92. CPPUNIT_ASSERT_EQUAL(std::string("pass"), sfnet.getValue());
  93. CPPUNIT_ASSERT_EQUAL((time_t)12345679, sfnet.getExpiryTime());
  94. CPPUNIT_ASSERT(sfnet.getPersistent());
  95. CPPUNIT_ASSERT_EQUAL(std::string("aria2.sourceforge.net"),
  96. sfnet.getDomain());
  97. CPPUNIT_ASSERT(!sfnet.getHostOnly());
  98. CPPUNIT_ASSERT_EQUAL(std::string("/"), sfnet.getPath());
  99. CPPUNIT_ASSERT(!sfnet.getSecure());
  100. const Cookie& sfjp = cookies[1];
  101. CPPUNIT_ASSERT_EQUAL(std::string("myseckey"), sfjp.getName());
  102. CPPUNIT_ASSERT_EQUAL(std::string("pass2"), sfjp.getValue());
  103. CPPUNIT_ASSERT_EQUAL((time_t)0, sfjp.getExpiryTime());
  104. CPPUNIT_ASSERT(sfjp.getPersistent());
  105. CPPUNIT_ASSERT_EQUAL(std::string("aria2.sourceforge.jp"), sfjp.getDomain());
  106. CPPUNIT_ASSERT(sfjp.getHostOnly());
  107. CPPUNIT_ASSERT_EQUAL(std::string("/profile"), sfjp.getPath());
  108. CPPUNIT_ASSERT(sfjp.getSecure());
  109. const Cookie& localnet = cookies[2];
  110. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), localnet.getDomain());
  111. CPPUNIT_ASSERT(sfjp.getHostOnly());
  112. }
  113. } // namespace aria2