Sqlite3MozCookieParserTest.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "Sqlite3MozCookieParser.h"
  2. #include "RecoverableException.h"
  3. #include "Util.h"
  4. #include <iostream>
  5. #include <cppunit/extensions/HelperMacros.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. }
  59. }
  60. void Sqlite3MozCookieParserTest::testParse_badfile()
  61. {
  62. Sqlite3MozCookieParser parser;
  63. try {
  64. parser.parse("badcookies.sqlite");
  65. CPPUNIT_FAIL("exception must be thrown.");
  66. } catch(RecoverableException& e) {
  67. // SUCCESS
  68. }
  69. }
  70. } // namespace aria2