Sqlite3MozCookieParserTest.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "Sqlite3MozCookieParser.h"
  2. #include <iostream>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "RecoverableException.h"
  5. #include "Util.h"
  6. namespace aria2 {
  7. class Sqlite3MozCookieParserTest:public CppUnit::TestFixture {
  8. CPPUNIT_TEST_SUITE(Sqlite3MozCookieParserTest);
  9. CPPUNIT_TEST(testParse);
  10. CPPUNIT_TEST(testParse_fileNotFound);
  11. CPPUNIT_TEST(testParse_badfile);
  12. CPPUNIT_TEST_SUITE_END();
  13. public:
  14. void setUp() {}
  15. void tearDown() {}
  16. void testParse();
  17. void testParse_fileNotFound();
  18. void testParse_badfile();
  19. };
  20. CPPUNIT_TEST_SUITE_REGISTRATION(Sqlite3MozCookieParserTest);
  21. void Sqlite3MozCookieParserTest::testParse()
  22. {
  23. Sqlite3MozCookieParser parser;
  24. std::deque<Cookie> cookies = parser.parse("cookies.sqlite");
  25. CPPUNIT_ASSERT_EQUAL((size_t)3, cookies.size());
  26. const Cookie& localhost = cookies[0];
  27. CPPUNIT_ASSERT_EQUAL(std::string("localhost"), localhost.getDomain());
  28. CPPUNIT_ASSERT_EQUAL(std::string("/"), localhost.getPath());
  29. CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), localhost.getName());
  30. CPPUNIT_ASSERT_EQUAL(std::string("123456789"), localhost.getValue());
  31. CPPUNIT_ASSERT_EQUAL((time_t)INT32_MAX, localhost.getExpiry());
  32. CPPUNIT_ASSERT_EQUAL(true, localhost.isSecureCookie());
  33. const Cookie& nullValue = cookies[1];
  34. CPPUNIT_ASSERT_EQUAL(std::string("null_value"), nullValue.getDomain());
  35. CPPUNIT_ASSERT_EQUAL(std::string("/path/to"), nullValue.getPath());
  36. CPPUNIT_ASSERT_EQUAL(std::string("uid"), nullValue.getName());
  37. CPPUNIT_ASSERT_EQUAL(std::string(""), nullValue.getValue());
  38. CPPUNIT_ASSERT_EQUAL((time_t)0, nullValue.getExpiry());
  39. CPPUNIT_ASSERT_EQUAL(false, nullValue.isSecureCookie());
  40. // See row id=3 has no name, so it is skipped.
  41. const Cookie& overflowTime = cookies[2];
  42. CPPUNIT_ASSERT_EQUAL(std::string("overflow_time_t"),
  43. overflowTime.getDomain());
  44. CPPUNIT_ASSERT_EQUAL(std::string("/path/to"), overflowTime.getPath());
  45. CPPUNIT_ASSERT_EQUAL(std::string("foo"), overflowTime.getName());
  46. CPPUNIT_ASSERT_EQUAL(std::string("bar"), overflowTime.getValue());
  47. CPPUNIT_ASSERT_EQUAL((time_t)INT32_MAX, overflowTime.getExpiry());
  48. CPPUNIT_ASSERT_EQUAL(false, overflowTime.isSecureCookie());
  49. }
  50. void Sqlite3MozCookieParserTest::testParse_fileNotFound()
  51. {
  52. Sqlite3MozCookieParser parser;
  53. try {
  54. parser.parse("fileNotFound");
  55. CPPUNIT_FAIL("exception must be thrown.");
  56. } catch(RecoverableException& e) {
  57. // SUCCESS
  58. CPPUNIT_ASSERT(Util::startsWith(e.what(),
  59. "Failed to open SQLite3 database:"));
  60. }
  61. }
  62. void Sqlite3MozCookieParserTest::testParse_badfile()
  63. {
  64. Sqlite3MozCookieParser parser;
  65. try {
  66. parser.parse("badcookies.sqlite");
  67. CPPUNIT_FAIL("exception must be thrown.");
  68. } catch(RecoverableException& e) {
  69. // SUCCESS
  70. }
  71. }
  72. } // namespace aria2