Sqlite3CookieParserTest.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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("localhost.local"), localhost.getDomain());
  31. CPPUNIT_ASSERT_EQUAL(std::string("/"), localhost.getPath());
  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.getExpiry());
  35. CPPUNIT_ASSERT_EQUAL(true, localhost.isSecureCookie());
  36. const Cookie& nullValue = cookies[1];
  37. CPPUNIT_ASSERT_EQUAL(std::string(".null_value.com"), nullValue.getDomain());
  38. CPPUNIT_ASSERT_EQUAL(std::string("/path/to"), nullValue.getPath());
  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.getExpiry());
  42. CPPUNIT_ASSERT_EQUAL(false, nullValue.isSecureCookie());
  43. // See row id=3 has no name, so it is skipped.
  44. const Cookie& overflowTime = cookies[2];
  45. CPPUNIT_ASSERT_EQUAL(std::string(".overflow.time_t.org"),
  46. overflowTime.getDomain());
  47. CPPUNIT_ASSERT_EQUAL(std::string("/path/to"), overflowTime.getPath());
  48. CPPUNIT_ASSERT_EQUAL(std::string("foo"), overflowTime.getName());
  49. CPPUNIT_ASSERT_EQUAL(std::string("bar"), overflowTime.getValue());
  50. CPPUNIT_ASSERT_EQUAL((time_t)INT32_MAX, overflowTime.getExpiry());
  51. CPPUNIT_ASSERT_EQUAL(false, overflowTime.isSecureCookie());
  52. }
  53. void Sqlite3CookieParserTest::testMozParse_fileNotFound()
  54. {
  55. Sqlite3MozCookieParser parser("fileNotFound");
  56. try {
  57. std::vector<Cookie> cookies;
  58. parser.parse(cookies);
  59. CPPUNIT_FAIL("exception must be thrown.");
  60. } catch(RecoverableException& e) {
  61. // SUCCESS
  62. CPPUNIT_ASSERT(util::startsWith(e.what(),
  63. "SQLite3 database is not opened"));
  64. }
  65. }
  66. void Sqlite3CookieParserTest::testMozParse_badfile()
  67. {
  68. Sqlite3MozCookieParser parser("badcookies.sqlite");
  69. try {
  70. std::vector<Cookie> cookies;
  71. parser.parse(cookies);
  72. CPPUNIT_FAIL("exception must be thrown.");
  73. } catch(RecoverableException& e) {
  74. // SUCCESS
  75. }
  76. }
  77. void Sqlite3CookieParserTest::testChromumParse()
  78. {
  79. Sqlite3ChromiumCookieParser parser("chromium_cookies.sqlite");
  80. std::vector<Cookie> cookies;
  81. parser.parse(cookies);
  82. const Cookie& sfnet = cookies[0];
  83. CPPUNIT_ASSERT_EQUAL(std::string(".aria2.sourceforge.net"),
  84. sfnet.getDomain());
  85. CPPUNIT_ASSERT_EQUAL(std::string("/"), sfnet.getPath());
  86. CPPUNIT_ASSERT_EQUAL(std::string("mykey"), sfnet.getName());
  87. CPPUNIT_ASSERT_EQUAL(std::string("pass"), sfnet.getValue());
  88. CPPUNIT_ASSERT_EQUAL((time_t)12345679, sfnet.getExpiry());
  89. CPPUNIT_ASSERT_EQUAL(false, sfnet.isSecureCookie());
  90. const Cookie& sfjp = cookies[1];
  91. CPPUNIT_ASSERT_EQUAL(std::string(".aria2.sourceforge.jp"), sfjp.getDomain());
  92. CPPUNIT_ASSERT_EQUAL(std::string("/profile"), sfjp.getPath());
  93. CPPUNIT_ASSERT_EQUAL(std::string("myseckey"), sfjp.getName());
  94. CPPUNIT_ASSERT_EQUAL(std::string("pass2"), sfjp.getValue());
  95. CPPUNIT_ASSERT_EQUAL((time_t)0, sfjp.getExpiry());
  96. CPPUNIT_ASSERT_EQUAL(true, sfjp.isSecureCookie());
  97. }
  98. } // namespace aria2