| 1 | // |
| 2 | // HTTPCookieTest.cpp |
| 3 | // |
| 4 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
| 5 | // and Contributors. |
| 6 | // |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | // |
| 9 | |
| 10 | |
| 11 | #include "HTTPCookieTest.h" |
| 12 | #include "Poco/CppUnit/TestCaller.h" |
| 13 | #include "Poco/CppUnit/TestSuite.h" |
| 14 | #include "Poco/Net/HTTPCookie.h" |
| 15 | #include "Poco/Timestamp.h" |
| 16 | #include "Poco/Timespan.h" |
| 17 | #include "Poco/DateTime.h" |
| 18 | #include "Poco/DateTimeFormatter.h" |
| 19 | #include "Poco/DateTimeParser.h" |
| 20 | #include "Poco/DateTimeFormat.h" |
| 21 | #include "Poco/Net/NameValueCollection.h" |
| 22 | #include <cstdlib> |
| 23 | #include <sstream> |
| 24 | |
| 25 | |
| 26 | using Poco::Timestamp; |
| 27 | using Poco::Timespan; |
| 28 | using Poco::DateTimeFormatter; |
| 29 | using Poco::DateTimeFormat; |
| 30 | using Poco::DateTimeParser; |
| 31 | using Poco::DateTime; |
| 32 | using Poco::Net::NameValueCollection; |
| 33 | using Poco::Net::HTTPCookie; |
| 34 | |
| 35 | |
| 36 | HTTPCookieTest::HTTPCookieTest(const std::string& name): CppUnit::TestCase(name) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | |
| 41 | HTTPCookieTest::~HTTPCookieTest() |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | |
| 46 | void HTTPCookieTest::testCookie() |
| 47 | { |
| 48 | HTTPCookie cookie("name" , "value" ); |
| 49 | assertTrue (cookie.getName() == "name" ); |
| 50 | assertTrue (cookie.getValue() == "value" ); |
| 51 | assertTrue (cookie.toString() == "name=value" ); |
| 52 | cookie.setPath("/" ); |
| 53 | assertTrue (cookie.toString() == "name=value; path=/" ); |
| 54 | cookie.setComment("comment" ); |
| 55 | assertTrue (cookie.toString() == "name=value; path=/" ); |
| 56 | cookie.setDomain("appinf.com" ); |
| 57 | assertTrue (cookie.toString() == "name=value; domain=appinf.com; path=/" ); |
| 58 | cookie.setSecure(true); |
| 59 | assertTrue (cookie.toString() == "name=value; domain=appinf.com; path=/; secure" ); |
| 60 | cookie.setHttpOnly(true); |
| 61 | assertTrue (cookie.toString() == "name=value; domain=appinf.com; path=/; secure; HttpOnly" ); |
| 62 | cookie.setPriority("Low" ); |
| 63 | assertTrue (cookie.toString() == "name=value; domain=appinf.com; path=/; Priority=Low; secure; HttpOnly" ); |
| 64 | cookie.setPriority("Medium" ); |
| 65 | assertTrue (cookie.toString() == "name=value; domain=appinf.com; path=/; Priority=Medium; secure; HttpOnly" ); |
| 66 | cookie.setPriority("High" ); |
| 67 | assertTrue (cookie.toString() == "name=value; domain=appinf.com; path=/; Priority=High; secure; HttpOnly" ); |
| 68 | cookie.setPriority("" ); |
| 69 | cookie.setHttpOnly(false); |
| 70 | |
| 71 | cookie.setVersion(1); |
| 72 | assertTrue (cookie.toString() == "name=\"value\"; Comment=\"comment\"; Domain=\"appinf.com\"; Path=\"/\"; secure; Version=\"1\"" ); |
| 73 | |
| 74 | cookie.setSecure(false); |
| 75 | cookie.setMaxAge(100); |
| 76 | assertTrue (cookie.toString() == "name=\"value\"; Comment=\"comment\"; Domain=\"appinf.com\"; Path=\"/\"; Max-Age=\"100\"; Version=\"1\"" ); |
| 77 | |
| 78 | cookie.setHttpOnly(true); |
| 79 | assertTrue (cookie.toString() == "name=\"value\"; Comment=\"comment\"; Domain=\"appinf.com\"; Path=\"/\"; Max-Age=\"100\"; HttpOnly; Version=\"1\"" ); |
| 80 | |
| 81 | cookie.setPriority("Low" ); |
| 82 | assertTrue (cookie.toString() == "name=\"value\"; Comment=\"comment\"; Domain=\"appinf.com\"; Path=\"/\"; Priority=\"Low\"; Max-Age=\"100\"; HttpOnly; Version=\"1\"" ); |
| 83 | cookie.setPriority("Medium" ); |
| 84 | assertTrue (cookie.toString() == "name=\"value\"; Comment=\"comment\"; Domain=\"appinf.com\"; Path=\"/\"; Priority=\"Medium\"; Max-Age=\"100\"; HttpOnly; Version=\"1\"" ); |
| 85 | cookie.setPriority("High" ); |
| 86 | assertTrue (cookie.toString() == "name=\"value\"; Comment=\"comment\"; Domain=\"appinf.com\"; Path=\"/\"; Priority=\"High\"; Max-Age=\"100\"; HttpOnly; Version=\"1\"" ); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | void HTTPCookieTest::testEscape() |
| 91 | { |
| 92 | std::string escaped = HTTPCookie::escape("this is a test." ); |
| 93 | assertTrue (escaped == "this%20is%20a%20test." ); |
| 94 | |
| 95 | escaped = HTTPCookie::escape("\n\t@,;\"'" ); |
| 96 | assertTrue (escaped == "%0A%09@%2C%3B%22%27" ); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | void HTTPCookieTest::testUnescape() |
| 101 | { |
| 102 | std::string unescaped = HTTPCookie::unescape("this%20is%20a%20test!" ); |
| 103 | assertTrue (unescaped == "this is a test!" ); |
| 104 | |
| 105 | unescaped = HTTPCookie::unescape("%0a%09@%2c%3b%22%27" ); |
| 106 | assertTrue (unescaped == "\n\t@,;\"'" ); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | void HTTPCookieTest::testExpiryFuture() |
| 111 | { |
| 112 | DateTime future; |
| 113 | //1 year from now |
| 114 | future.assign(future.year() + 1, |
| 115 | future.month(), |
| 116 | (future.month() == 2 && future.day() == 29) ? 28 : future.day(), |
| 117 | future.hour(), |
| 118 | future.minute(), |
| 119 | future.second(), |
| 120 | future.millisecond(), |
| 121 | future.microsecond()); |
| 122 | testCookieExpiry(future); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | void HTTPCookieTest::testExpiryPast() |
| 127 | { |
| 128 | DateTime past; |
| 129 | // 1 year ago |
| 130 | past.assign(past.year() - 1, |
| 131 | past.month(), |
| 132 | (past.month() == 2 && past.day() == 29) ? 28 : past.day(), |
| 133 | past.hour(), |
| 134 | past.minute(), |
| 135 | past.second(), |
| 136 | past.millisecond(), |
| 137 | past.microsecond()); |
| 138 | testCookieExpiry(past); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | void HTTPCookieTest::testCookieExpiry(DateTime expiryTime) |
| 143 | { |
| 144 | NameValueCollection nvc; |
| 145 | nvc.add("name" , "value" ); |
| 146 | std::string expiryString = DateTimeFormatter::format(expiryTime.timestamp(),DateTimeFormat::HTTP_FORMAT); |
| 147 | nvc.add("expires" , expiryString); |
| 148 | |
| 149 | Timestamp before; //start of cookie lifetime |
| 150 | HTTPCookie cookie(nvc); //cookie created |
| 151 | std::string cookieStringV0 = cookie.toString(); |
| 152 | cookie.setVersion(1); |
| 153 | std::string cookieStringV1 = cookie.toString(); |
| 154 | Timestamp now; |
| 155 | //expected number of seconds until expiryTime - should be close to cookie._maxAge |
| 156 | int expectedMaxAge = (int) ((expiryTime.timestamp() - now) / Timestamp::resolution()); //expected number of seconds until expiryTime |
| 157 | Timestamp after; //end of cookie lifetime |
| 158 | |
| 159 | //length of lifetime of the cookie |
| 160 | Timespan delta = after - before; |
| 161 | |
| 162 | //pull out cookie expire time string |
| 163 | size_t startPos = cookieStringV0.find("expires=" ) + 8; |
| 164 | std::string cookieExpireTimeStr = cookieStringV0.substr(startPos, cookieStringV0.find(";" , startPos)); |
| 165 | //convert to a DateTime |
| 166 | int tzd; |
| 167 | DateTime cookieExpireTime = DateTimeParser::parse(cookieExpireTimeStr, tzd); |
| 168 | //pull out cookie max age |
| 169 | int cookieMaxAge; |
| 170 | startPos = cookieStringV1.find("Max-Age=\"" ) + 9; |
| 171 | std::string cookieMaxAgeStr = cookieStringV1.substr(startPos, cookieStringV1.find("\"" , startPos)); |
| 172 | //convert to integer |
| 173 | std::istringstream(cookieMaxAgeStr) >> cookieMaxAge; |
| 174 | |
| 175 | //assert that the cookie's expiry time reflects the time passed to |
| 176 | //its constructor, within a delta of the lifetime of the cookie |
| 177 | assertTrue (cookieExpireTime - expiryTime <= delta); |
| 178 | //assert that the cookie's max age is the number of seconds between |
| 179 | //the creation of the cookie and the expiry time passed to its |
| 180 | //constuctor, within a delta of the lifetime of the cookie |
| 181 | assertTrue (cookieMaxAge - expectedMaxAge <= delta.seconds()); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | void HTTPCookieTest::setUp() |
| 186 | { |
| 187 | } |
| 188 | |
| 189 | |
| 190 | void HTTPCookieTest::tearDown() |
| 191 | { |
| 192 | } |
| 193 | |
| 194 | |
| 195 | CppUnit::Test* HTTPCookieTest::suite() |
| 196 | { |
| 197 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPCookieTest" ); |
| 198 | |
| 199 | CppUnit_addTest(pSuite, HTTPCookieTest, testCookie); |
| 200 | CppUnit_addTest(pSuite, HTTPCookieTest, testEscape); |
| 201 | CppUnit_addTest(pSuite, HTTPCookieTest, testUnescape); |
| 202 | CppUnit_addTest(pSuite, HTTPCookieTest, testExpiryFuture); |
| 203 | CppUnit_addTest(pSuite, HTTPCookieTest, testExpiryPast); |
| 204 | |
| 205 | return pSuite; |
| 206 | } |
| 207 | |