Sqlite3MozCookieParserTest.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.domain);
  28. CPPUNIT_ASSERT_EQUAL(std::string("/"), localhost.path);
  29. CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), localhost.name);
  30. CPPUNIT_ASSERT_EQUAL(std::string("123456789"), localhost.value);
  31. CPPUNIT_ASSERT_EQUAL((time_t)1200000000, localhost.expires);
  32. CPPUNIT_ASSERT_EQUAL(true, localhost.secure);
  33. const Cookie& nullValue = cookies[1];
  34. CPPUNIT_ASSERT_EQUAL(std::string("null_value"), nullValue.domain);
  35. CPPUNIT_ASSERT_EQUAL(std::string("/path/to"), nullValue.path);
  36. CPPUNIT_ASSERT_EQUAL(std::string("uid"), nullValue.name);
  37. CPPUNIT_ASSERT_EQUAL(std::string(""), nullValue.value);
  38. CPPUNIT_ASSERT_EQUAL((time_t)0, nullValue.expires);
  39. CPPUNIT_ASSERT_EQUAL(false, nullValue.secure);
  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"), overflowTime.domain);
  43. CPPUNIT_ASSERT_EQUAL(std::string("/path/to"), overflowTime.path);
  44. CPPUNIT_ASSERT_EQUAL(std::string("foo"), overflowTime.name);
  45. CPPUNIT_ASSERT_EQUAL(std::string("bar"), overflowTime.value);
  46. CPPUNIT_ASSERT_EQUAL((time_t)INT32_MAX, overflowTime.expires);
  47. CPPUNIT_ASSERT_EQUAL(false, overflowTime.secure);
  48. }
  49. void Sqlite3MozCookieParserTest::testParse_fileNotFound()
  50. {
  51. Sqlite3MozCookieParser parser;
  52. try {
  53. parser.parse("fileNotFound");
  54. CPPUNIT_FAIL("exception must be thrown.");
  55. } catch(RecoverableException& e) {
  56. // SUCCESS
  57. }
  58. }
  59. void Sqlite3MozCookieParserTest::testParse_badfile()
  60. {
  61. Sqlite3MozCookieParser parser;
  62. try {
  63. parser.parse("badcookies.sqlite");
  64. CPPUNIT_FAIL("exception must be thrown.");
  65. } catch(RecoverableException& e) {
  66. // SUCCESS
  67. }
  68. }
  69. } // namespace aria2