security: Correct ValidityPeriod::isValid check
Change-Id: I235773470d838f5842cdf595f83d744935d304bd
Refs: #2868
diff --git a/src/security/validity-period.cpp b/src/security/validity-period.cpp
index c6f6026..14ba0f2 100644
--- a/src/security/validity-period.cpp
+++ b/src/security/validity-period.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2013-2015 Regents of the University of California.
+ * Copyright (c) 2013-2016 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -39,6 +39,12 @@
using boost::chrono::time_point_cast;
+ValidityPeriod::ValidityPeriod()
+ : ValidityPeriod(time::system_clock::TimePoint() + time::nanoseconds(1),
+ time::system_clock::TimePoint())
+{
+}
+
ValidityPeriod::ValidityPeriod(const time::system_clock::TimePoint& notBefore,
const time::system_clock::TimePoint& notAfter)
: m_notBefore(time_point_cast<TimePoint::duration>(notBefore + TimePoint::duration(1) -
@@ -144,7 +150,7 @@
bool
ValidityPeriod::isValid(const time::system_clock::TimePoint& now) const
{
- return m_notBefore < now && now < m_notAfter;
+ return m_notBefore <= now && now <= m_notAfter;
}
bool
diff --git a/src/security/validity-period.hpp b/src/security/validity-period.hpp
index ab24d68..d09657f 100644
--- a/src/security/validity-period.hpp
+++ b/src/security/validity-period.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2013-2015 Regents of the University of California.
+ * Copyright (c) 2013-2016 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -48,16 +48,16 @@
};
public:
- /** @brief Set validity period (UNIX epoch, UNIX epoch) that is always invalid
+ /** @brief Set validity period [UNIX epoch + 1 nanosecond, UNIX epoch] that is always invalid
*/
- ValidityPeriod() = default;
+ ValidityPeriod();
/** @brief Create validity period from @p block
*/
explicit
ValidityPeriod(const Block& block);
- /** @brief Create validity period (@p notBefore, @p notAfter)
+ /** @brief Create validity period [@p notBefore, @p notAfter]
* @param notBefore exclusive beginning of the validity period range
* @param notAfter exclusive end of the validity period range
*
@@ -70,12 +70,12 @@
/** @brief Check if @p now falls within the validity period
* @param now Time point to check if it falls within the period
- * @return periodBegin < @p now and @p now < periodEnd
+ * @return periodBegin <= @p now and @p now <= periodEnd
*/
bool
isValid(const time::system_clock::TimePoint& now = time::system_clock::now()) const;
- /** @brief Set validity period (@p notBefore, @p notAfter)
+ /** @brief Set validity period [@p notBefore, @p notAfter]
* @param notBefore exclusive beginning of the validity period range
* @param notAfter exclusive end of the validity period range
*
diff --git a/tests/unit-tests/security/validity-period.t.cpp b/tests/unit-tests/security/validity-period.t.cpp
index db22b9e..1fc91cd 100644
--- a/tests/unit-tests/security/validity-period.t.cpp
+++ b/tests/unit-tests/security/validity-period.t.cpp
@@ -22,18 +22,23 @@
#include "security/validity-period.hpp"
#include "boost-test.hpp"
+#include "unit-tests/unit-test-time-fixture.hpp"
#include <boost/lexical_cast.hpp>
namespace ndn {
namespace security {
-namespace test {
+namespace tests {
+
+using namespace ndn::tests;
BOOST_AUTO_TEST_SUITE(Security)
BOOST_AUTO_TEST_SUITE(TestValidityPeriod)
-BOOST_AUTO_TEST_CASE(ConstructorSetter)
+BOOST_FIXTURE_TEST_CASE(ConstructorSetter, UnitTestTimeFixture)
{
- time::system_clock::TimePoint notBefore = time::system_clock::now() - time::days(1);
+ time::system_clock::TimePoint now = this->systemClock->getNow();
+
+ time::system_clock::TimePoint notBefore = now - time::days(1);
time::system_clock::TimePoint notAfter = notBefore + time::days(2);
ValidityPeriod validity1 = ValidityPeriod(notBefore, notAfter);
@@ -46,13 +51,13 @@
BOOST_CHECK_GT(period.second, notAfter - time::seconds(1));
BOOST_CHECK_EQUAL(validity1.isValid(), true);
- BOOST_CHECK_EQUAL(ValidityPeriod(time::system_clock::now() - time::days(2),
- time::system_clock::now() - time::days(1)).isValid(),
+ BOOST_CHECK_EQUAL(ValidityPeriod(now - time::days(2),
+ now - time::days(1)).isValid(),
false);
BOOST_CHECK_NO_THROW((ValidityPeriod()));
ValidityPeriod validity2;
- BOOST_CHECK(validity2.getPeriod() == std::make_pair(time::getUnixEpoch(), time::getUnixEpoch()));
+ BOOST_CHECK_EQUAL(validity2.isValid(), false);
validity2.setPeriod(notBefore, notAfter);
BOOST_CHECK(validity2.getPeriod() != std::make_pair(time::getUnixEpoch(), time::getUnixEpoch()));
@@ -66,6 +71,9 @@
time::getUnixEpoch() + time::days(10 * 365) + time::nanoseconds(1));
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(validity1),
"(19700101T000001, 19791230T000000)");
+
+ BOOST_CHECK_EQUAL(ValidityPeriod(now, now).isValid(), true);
+ BOOST_CHECK_EQUAL(ValidityPeriod(now + time::seconds(1), now).isValid(), false);
}
const uint8_t VP1[] = {
@@ -188,6 +196,6 @@
BOOST_AUTO_TEST_SUITE_END() // TestValidityPeriod
BOOST_AUTO_TEST_SUITE_END() // Security
-} // namespace test
+} // namespace tests
} // namespace security
} // namespace ndn