1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
#include "Sqlite3CookieParserImpl.h"
#include <cstring>
#include <iostream>
#include <cppunit/extensions/HelperMacros.h>
#include "RecoverableException.h"
#include "util.h"
#include "array_fun.h"
namespace aria2 {
class Sqlite3CookieParserTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(Sqlite3CookieParserTest);
CPPUNIT_TEST(testMozParse);
CPPUNIT_TEST(testMozParse_fileNotFound);
CPPUNIT_TEST(testMozParse_badfile);
CPPUNIT_TEST(testChromumParse);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {}
void tearDown() {}
void testMozParse();
void testMozParse_fileNotFound();
void testMozParse_badfile();
void testChromumParse();
};
CPPUNIT_TEST_SUITE_REGISTRATION(Sqlite3CookieParserTest);
void Sqlite3CookieParserTest::testMozParse()
{
Sqlite3MozCookieParser parser(A2_TEST_DIR"/cookies.sqlite");
std::vector<Cookie> cookies;
parser.parse(cookies);
CPPUNIT_ASSERT_EQUAL((size_t)3, cookies.size());
const Cookie& localhost = cookies[0];
CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), localhost.getName());
CPPUNIT_ASSERT_EQUAL(std::string("123456789"), localhost.getValue());
CPPUNIT_ASSERT_EQUAL((time_t)INT32_MAX, localhost.getExpiryTime());
CPPUNIT_ASSERT(localhost.getPersistent());
CPPUNIT_ASSERT_EQUAL(std::string("localhost"), localhost.getDomain());
CPPUNIT_ASSERT(localhost.getHostOnly());
CPPUNIT_ASSERT_EQUAL(std::string("/"), localhost.getPath());
CPPUNIT_ASSERT(localhost.getSecure());
CPPUNIT_ASSERT_EQUAL((time_t)3000, localhost.getLastAccessTime());
CPPUNIT_ASSERT_EQUAL((time_t)3000, localhost.getCreationTime());
const Cookie& nullValue = cookies[1];
CPPUNIT_ASSERT_EQUAL(std::string("uid"), nullValue.getName());
CPPUNIT_ASSERT_EQUAL(std::string(""), nullValue.getValue());
CPPUNIT_ASSERT_EQUAL((time_t)0, nullValue.getExpiryTime());
CPPUNIT_ASSERT(nullValue.getPersistent());
CPPUNIT_ASSERT_EQUAL(std::string("null_value.com"), nullValue.getDomain());
CPPUNIT_ASSERT(!nullValue.getHostOnly());
CPPUNIT_ASSERT_EQUAL(std::string("/path/to"), nullValue.getPath());
CPPUNIT_ASSERT(!nullValue.getSecure());
// See row id=3 has no name, so it is skipped.
const Cookie& overflowTime = cookies[2];
CPPUNIT_ASSERT_EQUAL(std::string("foo"), overflowTime.getName());
CPPUNIT_ASSERT_EQUAL(std::string("bar"), overflowTime.getValue());
CPPUNIT_ASSERT((time_t)INT32_MAX <= overflowTime.getExpiryTime());
CPPUNIT_ASSERT(overflowTime.getPersistent());
CPPUNIT_ASSERT_EQUAL(std::string("overflow.time_t.org"),
overflowTime.getDomain());
CPPUNIT_ASSERT(!overflowTime.getHostOnly());
CPPUNIT_ASSERT_EQUAL(std::string("/path/to"), overflowTime.getPath());
CPPUNIT_ASSERT(!overflowTime.getSecure());
// See row id=5 has bad path, so it is skipped.
}
void Sqlite3CookieParserTest::testMozParse_fileNotFound()
{
Sqlite3MozCookieParser parser("fileNotFound");
try {
std::vector<Cookie> cookies;
parser.parse(cookies);
CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) {
// SUCCESS
const char A2_SQLITE_ERR[] = "SQLite3 database is not opened";
CPPUNIT_ASSERT(util::startsWith(e.what(), e.what()+strlen(e.what()),
A2_SQLITE_ERR, vend(A2_SQLITE_ERR)-1));
}
}
void Sqlite3CookieParserTest::testMozParse_badfile()
{
Sqlite3MozCookieParser parser(A2_TEST_DIR"/badcookies.sqlite");
try {
std::vector<Cookie> cookies;
parser.parse(cookies);
CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) {
// SUCCESS
}
}
void Sqlite3CookieParserTest::testChromumParse()
{
Sqlite3ChromiumCookieParser parser(A2_TEST_DIR"/chromium_cookies.sqlite");
std::vector<Cookie> cookies;
parser.parse(cookies);
CPPUNIT_ASSERT_EQUAL((size_t)3, cookies.size());
const Cookie& sfnet = cookies[0];
CPPUNIT_ASSERT_EQUAL(std::string("mykey"), sfnet.getName());
CPPUNIT_ASSERT_EQUAL(std::string("pass"), sfnet.getValue());
CPPUNIT_ASSERT_EQUAL((time_t)12345679, sfnet.getExpiryTime());
CPPUNIT_ASSERT(sfnet.getPersistent());
CPPUNIT_ASSERT_EQUAL(std::string("aria2.sourceforge.net"),
sfnet.getDomain());
CPPUNIT_ASSERT(!sfnet.getHostOnly());
CPPUNIT_ASSERT_EQUAL(std::string("/"), sfnet.getPath());
CPPUNIT_ASSERT(!sfnet.getSecure());
const Cookie& sfjp = cookies[1];
CPPUNIT_ASSERT_EQUAL(std::string("myseckey"), sfjp.getName());
CPPUNIT_ASSERT_EQUAL(std::string("pass2"), sfjp.getValue());
CPPUNIT_ASSERT_EQUAL((time_t)0, sfjp.getExpiryTime());
CPPUNIT_ASSERT(sfjp.getPersistent());
CPPUNIT_ASSERT_EQUAL(std::string("aria2.sourceforge.jp"), sfjp.getDomain());
CPPUNIT_ASSERT(sfjp.getHostOnly());
CPPUNIT_ASSERT_EQUAL(std::string("/profile"), sfjp.getPath());
CPPUNIT_ASSERT(sfjp.getSecure());
const Cookie& localnet = cookies[2];
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), localnet.getDomain());
CPPUNIT_ASSERT(sfjp.getHostOnly());
CPPUNIT_ASSERT_EQUAL((time_t)3000, localnet.getLastAccessTime());
CPPUNIT_ASSERT_EQUAL((time_t)3000, localnet.getCreationTime());
}
} // namespace aria2
|