all: Refactoring work with time using boost::chrono
Now the library has two clocks: time::steady_clock and
time::system_clock, following (boost|std)::chrono. In addition to
standard now() method, the library contains several helpers to convert
to/from UnixTimestamp (microsecond resolution) and IsoString (optional
microsecond resolution). The IsoString conversions use
boost::posix_time routines, since boost::chrono supports extended IO
support only starting boost version 1.52 (Boost Chrono V2).
This commit breaks compatibility with previous API. All time-related
Data/Interest calls must explicitly use time units to specify
FreshnessPeriod/InterestLifetime.
Brief usage/conversion guide:
- creation of time units does not support double/float types. If
necessary to create time unit from double, ``ndn::duration<double>`` (for
seconds) needs to be used instead. In some cases, this would also
require ``ndn::duration_cast``, if the target is not ``ndn::nanoseconds``.
- ndn::getNow, ndn::ndn_getNowMilliseconds, ndn::time::now are all
removed in favor of the now() method in a specific clock:
* time::system_clock::now();
* time::steady_clock::now();
- When necessary to convert system_clock::TimePoint to unix timestamp,
``time::toUnixTimestamp`` can be used. This method return number of
milliseconds since UNIX epoch as ``ndn::time::milliseconds`` type.
Use count() method to obtain number as an integral value.
Change-Id: Icd688bc6766e008d60c3d2888173627874526e47
Refs: #1152
diff --git a/tests/security/test-signed-interest.cpp b/tests/security/test-signed-interest.cpp
index f6898b9..db051c3 100644
--- a/tests/security/test-signed-interest.cpp
+++ b/tests/security/test-signed-interest.cpp
@@ -21,22 +21,24 @@
{
KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
- Name identityName("/TestSignedInterest/SignVerify/" + boost::lexical_cast<string>(time::now()));
+ Name identityName("/TestSignedInterest/SignVerify");
+ identityName.appendVersion();
+
Name certificateName;
BOOST_REQUIRE_NO_THROW(certificateName = keyChain.createIdentity(identityName));
Interest interest("/TestSignedInterest/SignVerify/Interest1");
BOOST_CHECK_NO_THROW(keyChain.signByIdentity(interest, identityName));
-
+
Block interestBlock(interest.wireEncode().wire(), interest.wireEncode().size());
Interest interest2;
interest2.wireDecode(interestBlock);
-
+
shared_ptr<PublicKey> publicKey;
BOOST_REQUIRE_NO_THROW(publicKey = keyChain.getPublicKeyFromTpm(keyChain.getDefaultKeyNameForIdentity(identityName)));
bool result = Validator::verifySignature(interest2, *publicKey);
-
+
BOOST_CHECK_EQUAL(result, true);
keyChain.deleteIdentity(identityName);
@@ -48,7 +50,7 @@
CommandInterestFixture()
: m_validity(false)
{}
-
+
void
validated(const shared_ptr<const Interest>& interest)
{ m_validity = true; }
@@ -56,7 +58,7 @@
void
validationFailed(const shared_ptr<const Interest>& interest, const string& failureInfo)
{
- m_validity = false;
+ m_validity = false;
}
void
@@ -64,12 +66,14 @@
{ m_validity = false; }
bool m_validity;
-};
+};
BOOST_FIXTURE_TEST_CASE (CommandInterest, CommandInterestFixture)
{
KeyChain keyChain;
- Name identity("/TestCommandInterest/Validation/" + boost::lexical_cast<string>(time::now()));
+ Name identity("/TestCommandInterest/Validation");
+ identity.appendVersion();
+
Name certName;
BOOST_REQUIRE_NO_THROW(certName = keyChain.createIdentity(identity));
@@ -84,38 +88,39 @@
validator.validate(*commandInterest1,
bind(&CommandInterestFixture::validated, this, _1),
bind(&CommandInterestFixture::validationFailed, this, _1, _2));
-
+
BOOST_CHECK_EQUAL(m_validity, true);
-
+
//Test an outdated command
reset();
shared_ptr<Interest> commandInterest2 = make_shared<Interest>("/TestCommandInterest/Validation/Command2");
- int64_t timestamp = time::now() / 1000000;
- timestamp -= 5000;
+ time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now());
+ timestamp -= time::seconds(5);
+
Name commandName = commandInterest2->getName();
commandName
- .append(name::Component::fromNumber(timestamp))
- .append(name::Component::fromNumber(random::generateWord64()));
+ .appendNumber(timestamp.count())
+ .appendNumber(random::generateWord64());
commandInterest2->setName(commandName);
-
+
keyChain.signByIdentity(*commandInterest2, identity);
validator.validate(*commandInterest2,
bind(&CommandInterestFixture::validated, this, _1),
bind(&CommandInterestFixture::validationFailed, this, _1, _2));
-
+
BOOST_CHECK_EQUAL(m_validity, false);
-
+
//Test an unauthorized command
Name identity2("/TestCommandInterest/Validation2");
Name certName2;
BOOST_REQUIRE_NO_THROW(certName2 = keyChain.createIdentity(identity2));
-
+
shared_ptr<Interest> commandInterest3 = make_shared<Interest>("/TestCommandInterest/Validation/Command3");
generator.generateWithIdentity(*commandInterest3, identity2);
validator.validate(*commandInterest3,
bind(&CommandInterestFixture::validated, this, _1),
bind(&CommandInterestFixture::validationFailed, this, _1, _2));
-
+
BOOST_CHECK_EQUAL(m_validity, false);
//Test another unauthorized command
@@ -124,7 +129,7 @@
validator.validate(*commandInterest4,
bind(&CommandInterestFixture::validated, this, _1),
bind(&CommandInterestFixture::validationFailed, this, _1, _2));
-
+
BOOST_CHECK_EQUAL(m_validity, false);
BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity));