File: NsCookieParserTest.cc

package info (click to toggle)
aria2 1.18.8-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 19,392 kB
  • ctags: 16,036
  • sloc: cpp: 115,823; sh: 12,015; ansic: 7,394; makefile: 1,445; ruby: 462; python: 216; xml: 176; asm: 58; sed: 16
file content (102 lines) | stat: -rw-r--r-- 3,258 bytes parent folder | download
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
#include "NsCookieParser.h"

#include <iostream>
#include <limits>

#include <cppunit/extensions/HelperMacros.h>

#include "RecoverableException.h"
#include "util.h"
#include "Cookie.h"

namespace aria2 {

class NsCookieParserTest:public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(NsCookieParserTest);
  CPPUNIT_TEST(testParse);
  CPPUNIT_TEST(testParse_fileNotFound);
  CPPUNIT_TEST_SUITE_END();
public:
  void setUp() {}

  void tearDown() {}

  void testParse();
  void testParse_fileNotFound();
};


CPPUNIT_TEST_SUITE_REGISTRATION(NsCookieParserTest);

void NsCookieParserTest::testParse()
{
  NsCookieParser parser;
  time_t now = 0;
  auto cookies = parser.parse(A2_TEST_DIR"/nscookietest.txt", now);
  CPPUNIT_ASSERT_EQUAL((size_t)5, cookies.size());

  auto c = cookies[0].get();
  CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), c->getName());
  CPPUNIT_ASSERT_EQUAL(std::string("123456789"), c->getValue());
  CPPUNIT_ASSERT_EQUAL((time_t)INT32_MAX, c->getExpiryTime());
  CPPUNIT_ASSERT(c->getPersistent());
  CPPUNIT_ASSERT_EQUAL(std::string("localhost"), c->getDomain());
  CPPUNIT_ASSERT(c->getHostOnly());
  CPPUNIT_ASSERT_EQUAL(std::string("/"), c->getPath());
  CPPUNIT_ASSERT(c->getSecure());

  c = cookies[1].get();
  CPPUNIT_ASSERT_EQUAL(std::string("user"), c->getName());
  CPPUNIT_ASSERT_EQUAL(std::string("me"), c->getValue());
  CPPUNIT_ASSERT_EQUAL((time_t)1000, c->getExpiryTime());
  CPPUNIT_ASSERT(c->getPersistent());
  CPPUNIT_ASSERT_EQUAL(std::string("expired"), c->getDomain());
  CPPUNIT_ASSERT(c->getHostOnly());
  CPPUNIT_ASSERT_EQUAL(std::string("/"), c->getPath());
  CPPUNIT_ASSERT(!c->getSecure());

  c = cookies[2].get();
  CPPUNIT_ASSERT_EQUAL(std::string("passwd"), c->getName());
  CPPUNIT_ASSERT_EQUAL(std::string("secret"), c->getValue());
  CPPUNIT_ASSERT_EQUAL(std::numeric_limits<time_t>::max(), c->getExpiryTime());
  CPPUNIT_ASSERT(!c->getPersistent());
  CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), c->getDomain());
  CPPUNIT_ASSERT(c->getHostOnly());
  CPPUNIT_ASSERT_EQUAL(std::string("/cgi-bin"), c->getPath());
  CPPUNIT_ASSERT(!c->getSecure());

  c = cookies[3].get();
  CPPUNIT_ASSERT_EQUAL(std::string("TAX"), c->getName());
  CPPUNIT_ASSERT_EQUAL(std::string("1000"), c->getValue());
  CPPUNIT_ASSERT((time_t)INT32_MAX <= c->getExpiryTime());
  CPPUNIT_ASSERT(c->getPersistent());
  CPPUNIT_ASSERT_EQUAL(std::string("overflow"), c->getDomain());
  CPPUNIT_ASSERT(c->getHostOnly());
  CPPUNIT_ASSERT_EQUAL(std::string("/"), c->getPath());
  CPPUNIT_ASSERT(!c->getSecure());

  c = cookies[4].get();
  CPPUNIT_ASSERT_EQUAL(std::string("novalue"), c->getName());
  CPPUNIT_ASSERT_EQUAL(std::string(""), c->getValue());
  CPPUNIT_ASSERT_EQUAL((time_t)INT32_MAX, c->getExpiryTime());
  CPPUNIT_ASSERT(c->getPersistent());
  CPPUNIT_ASSERT_EQUAL(std::string("example.org"), c->getDomain());
  CPPUNIT_ASSERT(!c->getHostOnly());
  CPPUNIT_ASSERT_EQUAL(std::string("/"), c->getPath());
  CPPUNIT_ASSERT(!c->getSecure());
}

void NsCookieParserTest::testParse_fileNotFound()
{
  NsCookieParser parser;
  try {
    time_t now = 0;
    parser.parse("fileNotFound", now);
    CPPUNIT_FAIL("exception must be thrown.");
  } catch(RecoverableException& e) {
    // SUCCESS
  }
}

} // namespace aria2