From d3a61a0b8a36237ee2539445578201215920e3b8 Mon Sep 17 00:00:00 2001 From: Karl Reid Date: Tue, 18 Jun 2013 15:12:04 +0100 Subject: [PATCH 1/3] Reworked cookie expiry testsuite to be less brittle. --- Net/testsuite/src/HTTPCookieTest.cpp | 91 +++++++++++++++------------- Net/testsuite/src/HTTPCookieTest.h | 3 + 2 files changed, 51 insertions(+), 43 deletions(-) diff --git a/Net/testsuite/src/HTTPCookieTest.cpp b/Net/testsuite/src/HTTPCookieTest.cpp index 18ad9beb5..77cc1b04e 100644 --- a/Net/testsuite/src/HTTPCookieTest.cpp +++ b/Net/testsuite/src/HTTPCookieTest.cpp @@ -36,16 +36,18 @@ #include "Poco/Net/HTTPCookie.h" #include "Poco/Timestamp.h" #include "Poco/Timespan.h" -#include "Poco/DateTime.h" #include "Poco/DateTimeFormatter.h" +#include "Poco/DateTimeParser.h" #include "Poco/DateTimeFormat.h" #include "Poco/Net/NameValueCollection.h" #include +#include using Poco::Timestamp; using Poco::Timespan; using Poco::DateTimeFormatter; using Poco::DateTimeFormat; +using Poco::DateTimeParser; using Poco::DateTime; using Poco::Net::NameValueCollection; using Poco::Net::HTTPCookie; @@ -111,55 +113,58 @@ void HTTPCookieTest::testUnescape() } void HTTPCookieTest::testExpiry() - { - NameValueCollection nvc; - nvc.add("name", "value"); - +{ //----Test expiry time in the future---- - Timestamp before; - DateTime future; + DateTime future; //now //now + 1 year future.assign(future.year()+1, future.month(),future.day(),future.hour(),future.minute(),future.second(), future.millisecond(), future.microsecond()); - std::string futureExpiryString = DateTimeFormatter::format(future.timestamp(),DateTimeFormat::HTTP_FORMAT); - nvc.add("expires", futureExpiryString); - HTTPCookie cookie(nvc); - //take one second off the future time since HTTPCookie is always one second off - future.assign(future.year(), future.month(),future.day(),future.hour(),future.minute(),future.second()-1, future.millisecond(), future.microsecond()); - futureExpiryString = DateTimeFormatter::format(future.timestamp(),DateTimeFormat::HTTP_FORMAT); - - // assert that the cookie bears the same expiry time as the string - // its constructor was passed. - assert (cookie.toString() == "name=value; expires=" + futureExpiryString); - - cookie.setVersion(1); - Timestamp now; - int futureAge = (int) ((future.timestamp() - now)/Timestamp::resolution()); - Timestamp after; - Timespan diff = after - before; //time taken between creation of 'future' and 'now' - int margin = diff.seconds() + 1; - // assert that the cookie's max age is the number of seconds between now - // and the time indicated in the 'expires' value passed to its - // constructor, within a margin of error - assert (abs(cookie.getMaxAge() - futureAge) <= margin); - - //-----Test expiry time in the past---- - before = Timestamp(); - DateTime past; + testCookieExpiry(future); + //----Test expiry time in the future---- + DateTime past; //now //now - 1 year past.assign(past.year()-1, past.month(),past.day(),past.hour(),past.minute(),past.second(), past.millisecond(), past.microsecond()); - std::string pastExpiryString = DateTimeFormatter::format(past.timestamp(),DateTimeFormat::HTTP_FORMAT); - nvc.erase("expires"); - nvc.add("expires", pastExpiryString); - cookie = HTTPCookie(nvc); - assert (cookie.toString() == "name=value; expires=" + pastExpiryString); + testCookieExpiry(past); +} +void HTTPCookieTest::testCookieExpiry(DateTime expiryTime){ + NameValueCollection nvc; + nvc.add("name", "value"); + std::string expiryString = DateTimeFormatter::format(expiryTime.timestamp(),DateTimeFormat::HTTP_FORMAT); + nvc.add("expires", expiryString); + + Timestamp before; //start of cookie lifetime + HTTPCookie cookie(nvc); //cookie created + std::string cookieStringV0 = cookie.toString(); cookie.setVersion(1); - now = Timestamp(); - int pastAge = (int) ((past.timestamp() - now)/Timestamp::resolution()); - after = Timestamp(); - diff = after - before; - margin = diff.seconds(); - assert (abs(cookie.getMaxAge() - pastAge) <= margin); + std::string cookieStringV1 = cookie.toString(); + Timestamp now; + //expected number of seconds until future - should be close to cookie._maxAge + int expectedMaxAge = (int) ((expiryTime.timestamp() - now)/Timestamp::resolution()); //expected number of seconds until expiryTime + Timestamp after; //end of cookie lifetime + + //length of lifetime of the cookie + Timespan delta = after - before; + + //pull out cookie expire time string + size_t startPos = cookieStringV0.find("expires=") + 8; + std::string cookieExpireTimeStr = cookieStringV0.substr(startPos, cookieStringV0.find(";", startPos)); + //convert to a DateTime + int tzd; + DateTime cookieExpireTime = DateTimeParser::parse(cookieExpireTimeStr, tzd); + //pull out cookie max age + int cookieMaxAge; + startPos = cookieStringV1.find("Max-Age=\"") + 9; + std::string cookieMaxAgeStr = cookieStringV1.substr(startPos, cookieStringV1.find("\"", startPos)); + //convert to integer + std::istringstream(cookieMaxAgeStr) >> cookieMaxAge; + + //assert that the cookie's expiry time reflects the time passed to + //its constructor, within a delta of the lifetime of the cookie + assert(cookieExpireTime - expiryTime <= delta); + //assert that the cookie's max age is the number of seconds between + //the creation of the cookie and the expiry time passed to its + //constuctor, within a delta of the lifetime of the cookie + assert(abs(cookieMaxAge - expectedMaxAge) <= delta.seconds()); } diff --git a/Net/testsuite/src/HTTPCookieTest.h b/Net/testsuite/src/HTTPCookieTest.h index 4c4d19f25..6d5e2864e 100644 --- a/Net/testsuite/src/HTTPCookieTest.h +++ b/Net/testsuite/src/HTTPCookieTest.h @@ -37,9 +37,11 @@ #include "Poco/Net/Net.h" +#include "Poco/DateTime.h" #include "CppUnit/TestCase.h" + class HTTPCookieTest: public CppUnit::TestCase { public: @@ -50,6 +52,7 @@ public: void testEscape(); void testUnescape(); void testExpiry(); + void testCookieExpiry(Poco::DateTime expiryTime); void setUp(); void tearDown(); From 5a709716917fefc8dab0f5309287e0f39bf09ba2 Mon Sep 17 00:00:00 2001 From: karlr42 Date: Tue, 18 Jun 2013 15:37:30 +0100 Subject: [PATCH 2/3] Fixed typo in comments --- Net/testsuite/src/HTTPCookieTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Net/testsuite/src/HTTPCookieTest.cpp b/Net/testsuite/src/HTTPCookieTest.cpp index 77cc1b04e..3e3e87e16 100644 --- a/Net/testsuite/src/HTTPCookieTest.cpp +++ b/Net/testsuite/src/HTTPCookieTest.cpp @@ -138,7 +138,7 @@ void HTTPCookieTest::testCookieExpiry(DateTime expiryTime){ cookie.setVersion(1); std::string cookieStringV1 = cookie.toString(); Timestamp now; - //expected number of seconds until future - should be close to cookie._maxAge + //expected number of seconds until expiryTime - should be close to cookie._maxAge int expectedMaxAge = (int) ((expiryTime.timestamp() - now)/Timestamp::resolution()); //expected number of seconds until expiryTime Timestamp after; //end of cookie lifetime From fb9e90dacd0ef14cf10274595d6e7072faaa067e Mon Sep 17 00:00:00 2001 From: karlr42 Date: Tue, 18 Jun 2013 16:27:28 +0100 Subject: [PATCH 3/3] Synced HTTPCookieTest --- Net/testsuite/src/HTTPCookieTest.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Net/testsuite/src/HTTPCookieTest.cpp b/Net/testsuite/src/HTTPCookieTest.cpp index 524fab1a1..6f79b8409 100644 --- a/Net/testsuite/src/HTTPCookieTest.cpp +++ b/Net/testsuite/src/HTTPCookieTest.cpp @@ -140,8 +140,6 @@ void HTTPCookieTest::testExpiryPast() testCookieExpiry(past); } - - void HTTPCookieTest::testCookieExpiry(DateTime expiryTime){ NameValueCollection nvc; nvc.add("name", "value"); @@ -155,7 +153,7 @@ void HTTPCookieTest::testCookieExpiry(DateTime expiryTime){ std::string cookieStringV1 = cookie.toString(); Timestamp now; //expected number of seconds until expiryTime - should be close to cookie._maxAge - int expectedMaxAge = (int) ((expiryTime.timestamp() - now)/Timestamp::resolution()); //expected number of seconds until expiryTime + int expectedMaxAge = (int) ((expiryTime.timestamp() - now) / Timestamp::resolution()); //expected number of seconds until expiryTime Timestamp after; //end of cookie lifetime //length of lifetime of the cookie @@ -176,11 +174,11 @@ void HTTPCookieTest::testCookieExpiry(DateTime expiryTime){ //assert that the cookie's expiry time reflects the time passed to //its constructor, within a delta of the lifetime of the cookie - assert(cookieExpireTime - expiryTime <= delta); + assert (cookieExpireTime - expiryTime <= delta); //assert that the cookie's max age is the number of seconds between //the creation of the cookie and the expiry time passed to its //constuctor, within a delta of the lifetime of the cookie - assert(abs(cookieMaxAge - expectedMaxAge) <= delta.seconds()); + assert (abs(cookieMaxAge - expectedMaxAge) <= delta.seconds()); } @@ -201,7 +199,8 @@ CppUnit::Test* HTTPCookieTest::suite() CppUnit_addTest(pSuite, HTTPCookieTest, testCookie); CppUnit_addTest(pSuite, HTTPCookieTest, testEscape); CppUnit_addTest(pSuite, HTTPCookieTest, testUnescape); - CppUnit_addTest(pSuite, HTTPCookieTest, testExpiry); + CppUnit_addTest(pSuite, HTTPCookieTest, testExpiryFuture); + CppUnit_addTest(pSuite, HTTPCookieTest, testExpiryPast); return pSuite; }