From 9e7579b475e322ec86ff986e3371fea3fd5c857e Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 20 Sep 2013 00:24:03 +0900 Subject: [PATCH] Set old cookie's creation-time to new cookie on replacement As described in http://tools.ietf.org/html/rfc6265#section-5.3 --- src/CookieStorage.cc | 1 + test/CookieStorageTest.cc | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/CookieStorage.cc b/src/CookieStorage.cc index e6f3fb09..c82cf7df 100644 --- a/src/CookieStorage.cc +++ b/src/CookieStorage.cc @@ -126,6 +126,7 @@ bool DomainNode::addCookie(std::unique_ptr cookie, time_t now) cookies_->erase(i); return false; } else { + cookie->setCreationTime((*i)->getCreationTime()); *i = std::move(cookie); return true; } diff --git a/test/CookieStorageTest.cc b/test/CookieStorageTest.cc index e88332b8..5da98374 100644 --- a/test/CookieStorageTest.cc +++ b/test/CookieStorageTest.cc @@ -66,10 +66,12 @@ std::vector dumpCookie(const CookieStorage& st) void CookieStorageTest::testStore() { - time_t now = 1000; + time_t now = 999; auto st = CookieStorage{}; - auto goodCookie = []() { - return createCookie("k", "v", "localhost", true, "/", false); + auto goodCookie = [&]() { + auto c = createCookie("k", "v", "localhost", true, "/", false); + c->setCreationTime(now); + return c; }; CPPUNIT_ASSERT(st.store(goodCookie(), now)); CPPUNIT_ASSERT_EQUAL((size_t)1, st.size()); @@ -83,13 +85,21 @@ void CookieStorageTest::testStore() CPPUNIT_ASSERT(st.contains(*anotherCookie())); CPPUNIT_ASSERT(st.contains(*goodCookie())); - auto updateGoodCookie = []() { - return createCookie("k", "v2", "localhost", true, "/", false); + ++now; + auto updateGoodCookie = [&]() { + auto c = createCookie("k", "v2", "localhost", true, "/", false); + c->setCreationTime(now); + return c; }; CPPUNIT_ASSERT(st.store(updateGoodCookie(), now)); CPPUNIT_ASSERT_EQUAL((size_t)2, st.size()); CPPUNIT_ASSERT(st.contains(*updateGoodCookie())); CPPUNIT_ASSERT(st.contains(*anotherCookie())); + auto cookies = st.criteriaFind("localhost", "/", now, false); + CPPUNIT_ASSERT_EQUAL((size_t)1, cookies.size()); + CPPUNIT_ASSERT_EQUAL(std::string("v2"), cookies[0]->getValue()); + // New cookie's creation time must match old cookie's creation time. + CPPUNIT_ASSERT_EQUAL((time_t)999, cookies[0]->getCreationTime()); auto expireGoodCookie = []() { return createCookie("k", "v3", 0, "localhost", true, "/", false);