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-certificate-cache.cpp b/tests/security/test-certificate-cache.cpp
index 3718170..ee7fd6f 100644
--- a/tests/security/test-certificate-cache.cpp
+++ b/tests/security/test-certificate-cache.cpp
@@ -25,17 +25,17 @@
BOOST_AUTO_TEST_CASE (Ttl)
{
shared_ptr<boost::asio::io_service> io = make_shared<boost::asio::io_service>();
- shared_ptr<CertificateCacheTtl> cache = make_shared<CertificateCacheTtl>(io, 1);
+ shared_ptr<CertificateCacheTtl> cache = make_shared<CertificateCacheTtl>(io, time::seconds(1));
Scheduler scheduler(*io);
shared_ptr<IdentityCertificate> cert1 = make_shared<IdentityCertificate>();
Name certName1("/tmp/KEY/ksk-1/ID-CERT/1");
cert1->setName(certName1);
- cert1->setFreshnessPeriod(500);
+ cert1->setFreshnessPeriod(time::milliseconds(500));
shared_ptr<IdentityCertificate> cert2 = make_shared<IdentityCertificate>();
Name certName2("/tmp/KEY/ksk-2/ID-CERT/2");
cert2->setName(certName2);
- cert2->setFreshnessPeriod(1000);
+ cert2->setFreshnessPeriod(time::milliseconds(1000));
Name name1 = certName1.getPrefix(-1);
Name name2 = certName2.getPrefix(-1);
@@ -43,13 +43,13 @@
cache->insertCertificate(cert1);
cache->insertCertificate(cert2);
- scheduler.scheduleEvent(time::seconds(0.3), bind(&getCertificateTtl, cache, name1, true));
- scheduler.scheduleEvent(time::seconds(0.3), bind(&getCertificateTtl, cache, name2, true));
- scheduler.scheduleEvent(time::seconds(0.6), bind(&getCertificateTtl, cache, name1, false));
- scheduler.scheduleEvent(time::seconds(0.6), bind(&getCertificateTtl, cache, name2, true));
- scheduler.scheduleEvent(time::seconds(0.6), bind(&CertificateCache::insertCertificate, &*cache, cert2));
- scheduler.scheduleEvent(time::seconds(1.3), bind(&getCertificateTtl, cache, name2, true));
- scheduler.scheduleEvent(time::seconds(1.7), bind(&getCertificateTtl, cache, name2, false));
+ scheduler.scheduleEvent(time::milliseconds(300), bind(&getCertificateTtl, cache, name1, true));
+ scheduler.scheduleEvent(time::milliseconds(300), bind(&getCertificateTtl, cache, name2, true));
+ scheduler.scheduleEvent(time::milliseconds(600), bind(&getCertificateTtl, cache, name1, false));
+ scheduler.scheduleEvent(time::milliseconds(600), bind(&getCertificateTtl, cache, name2, true));
+ scheduler.scheduleEvent(time::milliseconds(600), bind(&CertificateCache::insertCertificate, &*cache, cert2));
+ scheduler.scheduleEvent(time::milliseconds(1300), bind(&getCertificateTtl, cache, name2, true));
+ scheduler.scheduleEvent(time::milliseconds(1700), bind(&getCertificateTtl, cache, name2, false));
io->run();
}
diff --git a/tests/security/test-encode-decode-certificate.cpp b/tests/security/test-encode-decode-certificate.cpp
index 409e757..3b8cc83 100644
--- a/tests/security/test-encode-decode-certificate.cpp
+++ b/tests/security/test-encode-decode-certificate.cpp
@@ -83,8 +83,8 @@
const std::string CERT_INFO = "Certificate name:\n"
" /\n"
"Validity:\n"
- " NotBefore: 20131226T232254.000000\n"
- " NotAfter: 20131226T232254.000000\n"
+ " NotBefore: 20131226T232254\n"
+ " NotAfter: 20131226T232254\n"
"Subject Description:\n"
" 2.5.4.41: TEST NAME\n"
"Public key bits:\n"
@@ -98,8 +98,8 @@
ndn::Certificate c;
// validity
- c.setNotBefore(1388100174000); // 12/26/2013 @ 11:22pm
- c.setNotAfter(1388100174000); // 12/26/2013 @ 11:22pm
+ c.setNotBefore(time::fromUnixTimestamp(time::milliseconds(1388100174000))); // 12/26/2013 @ 11:22pm
+ c.setNotAfter(time::fromUnixTimestamp(time::milliseconds(1388100174000))); // 12/26/2013 @ 11:22pm
// subject
c.addSubjectDescription(CertificateSubjectDescription("2.5.4.41", "TEST NAME"));
@@ -131,19 +131,19 @@
// p.Load(source);
BOOST_REQUIRE_NO_THROW(c.encode());
-
+
// ofstream of("cert.out");
// of.write((const char*)c.getContent().value(), c.getContent().value_size());
// const Block &wire = i.wireEncode();
BOOST_REQUIRE_EQUAL_COLLECTIONS(CERT, CERT+sizeof(CERT),
- c.getContent().value_begin(), c.getContent().value_end());
+ c.getContent().value_begin(), c.getContent().value_end());
std::ostringstream os;
os << c << std::endl;
std::string info(os.str());
- BOOST_REQUIRE_EQUAL_COLLECTIONS(CERT_INFO.begin(), CERT_INFO.end(),
- info.begin(), info.end());
+
+ BOOST_CHECK_EQUAL(CERT_INFO, info);
}
const unsigned char REAL_CERT[] = {
@@ -153,8 +153,8 @@
const std::string REAL_CERT_INFO = "Certificate name:\n"
" /tmp\n"
"Validity:\n"
-" NotBefore: 20131101T171122.000000\n"
-" NotAfter: 20141101T171122.000000\n"
+" NotBefore: 20131101T171122\n"
+" NotAfter: 20141101T171122\n"
"Subject Description:\n"
" 2.5.4.41: NDN Testbed Root\n"
"Public key bits:\n"
@@ -176,8 +176,7 @@
std::ostringstream os;
os << c << std::endl;
std::string info(os.str());
- BOOST_REQUIRE_EQUAL_COLLECTIONS(REAL_CERT_INFO.begin(), REAL_CERT_INFO.end(),
- info.begin(), info.end());
+ BOOST_CHECK_EQUAL(REAL_CERT_INFO, info);
}
BOOST_AUTO_TEST_SUITE_END()
diff --git a/tests/security/test-keychain.cpp b/tests/security/test-keychain.cpp
index 3e9b4fc..8a76b79 100644
--- a/tests/security/test-keychain.cpp
+++ b/tests/security/test-keychain.cpp
@@ -7,7 +7,6 @@
#include <boost/test/unit_test.hpp>
#include "security/key-chain.hpp"
-#include "util/time.hpp"
using namespace std;
@@ -19,7 +18,8 @@
{
KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
- Name identity(string("/TestKeyChain/ExportIdentity/") + boost::lexical_cast<std::string>(time::now()));
+ Name identity("/TestKeyChain/ExportIdentity/");
+ identity.appendVersion();
keyChain.createIdentity(identity);
shared_ptr<SecuredBag> exported = keyChain.exportIdentity(identity, "1234");
@@ -60,28 +60,30 @@
{
KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
- Name identity(string("/TestKeyChain/PrepareIdentityCertificate/") + boost::lexical_cast<std::string>(time::now()));
+ Name identity("/TestKeyChain/PrepareIdentityCertificate/");
+ identity.appendVersion();
keyChain.createIdentity(identity);
vector<CertificateSubjectDescription> subjectDescription;
Name lowerIdentity = identity;
- lowerIdentity.append("Lower").append(boost::lexical_cast<std::string>(time::now()));
+ lowerIdentity.append("Lower").appendVersion();
Name lowerKeyName = keyChain.generateRSAKeyPair(lowerIdentity, true);
shared_ptr<IdentityCertificate> idCert
= keyChain.prepareUnsignedIdentityCertificate(lowerKeyName, identity,
- time::now() / 1000000,
- time::now() / 1000000 + 630720000,
+ time::system_clock::now(),
+ time::system_clock::now() + time::days(365),
subjectDescription);
BOOST_CHECK(static_cast<bool>(idCert));
BOOST_CHECK(idCert->getName().getPrefix(5) == Name().append(identity).append("KEY").append("Lower"));
- Name anotherIdentity(string("/TestKeyChain/PrepareIdentityCertificate/Another/") + boost::lexical_cast<std::string>(time::now()));
+ Name anotherIdentity("/TestKeyChain/PrepareIdentityCertificate/Another/");
+ anotherIdentity.appendVersion();
Name anotherKeyName = keyChain.generateRSAKeyPair(anotherIdentity, true);
shared_ptr<IdentityCertificate> idCert2
= keyChain.prepareUnsignedIdentityCertificate(anotherKeyName, identity,
- time::now() / 1000000,
- time::now() / 1000000 + 630720000,
+ time::system_clock::now(),
+ time::system_clock::now() + time::days(365),
subjectDescription);
BOOST_CHECK(static_cast<bool>(idCert2));
BOOST_CHECK(idCert2->getName().getPrefix(5) == Name().append(anotherIdentity).append("KEY"));
@@ -90,8 +92,8 @@
Name wrongKeyName1;
shared_ptr<IdentityCertificate> idCert3
= keyChain.prepareUnsignedIdentityCertificate(wrongKeyName1, identity,
- time::now() / 1000000,
- time::now() / 1000000 + 630720000,
+ time::system_clock::now(),
+ time::system_clock::now() + time::days(365),
subjectDescription);
BOOST_CHECK(!static_cast<bool>(idCert3));
@@ -99,8 +101,8 @@
Name wrongKeyName2("/TestKeyChain/PrepareIdentityCertificate");
shared_ptr<IdentityCertificate> idCert4
= keyChain.prepareUnsignedIdentityCertificate(wrongKeyName2, identity,
- time::now() / 1000000,
- time::now() / 1000000 + 630720000,
+ time::system_clock::now(),
+ time::system_clock::now() + time::days(365),
subjectDescription);
BOOST_CHECK(!static_cast<bool>(idCert4));
@@ -108,8 +110,8 @@
Name wrongKeyName3("/TestKeyChain/PrepareIdentityCertificate/ksk-1234");
shared_ptr<IdentityCertificate> idCert5
= keyChain.prepareUnsignedIdentityCertificate(wrongKeyName3, identity,
- time::now() / 1000000,
- time::now() / 1000000 + 630720000,
+ time::system_clock::now(),
+ time::system_clock::now() + time::days(365),
subjectDescription);
BOOST_CHECK(!static_cast<bool>(idCert5));
diff --git a/tests/security/test-sec-public-info-sqlite3.cpp b/tests/security/test-sec-public-info-sqlite3.cpp
index 8928ebf..c50183d 100644
--- a/tests/security/test-sec-public-info-sqlite3.cpp
+++ b/tests/security/test-sec-public-info-sqlite3.cpp
@@ -22,7 +22,9 @@
{
KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
- Name identity("/TestSecPublicInfoSqlite3/Delete/" + boost::lexical_cast<string>(time::now()));
+ Name identity("/TestSecPublicInfoSqlite3/Delete");
+ identity.appendVersion();
+
Name certName1;
BOOST_REQUIRE_NO_THROW(certName1 = keyChain.createIdentity(identity));
diff --git a/tests/security/test-sec-tpm-file.cpp b/tests/security/test-sec-tpm-file.cpp
index 146a1e1..70fada8 100644
--- a/tests/security/test-sec-tpm-file.cpp
+++ b/tests/security/test-sec-tpm-file.cpp
@@ -24,7 +24,7 @@
{
SecTpmFile tpm;
- Name keyName("/TestSecTpmFile/Delete/ksk-" + boost::lexical_cast<string>(time::now()));
+ Name keyName("/TestSecTpmFile/Delete/ksk-" + boost::lexical_cast<string>(time::toUnixTimestamp(time::system_clock::now())));
BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true);
@@ -40,7 +40,7 @@
{
SecTpmFile tpm;
- Name keyName("/TestSecTpmFile/SignVerify/ksk-" + boost::lexical_cast<string>(time::now()));
+ Name keyName("/TestSecTpmFile/SignVerify/ksk-" + boost::lexical_cast<string>(time::toUnixTimestamp(time::system_clock::now())));
BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
Data data("/tmp/test/1");
@@ -107,7 +107,7 @@
SecTpmFile tpm;
- Name keyName("/TestSecTpmFile/ImportKey/ksk-" + boost::lexical_cast<string>(time::now()));
+ Name keyName("/TestSecTpmFile/ImportKey/ksk-" + boost::lexical_cast<string>(time::toUnixTimestamp(time::system_clock::now())));
BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false);
BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false);
diff --git a/tests/security/test-sec-tpm-osx.cpp b/tests/security/test-sec-tpm-osx.cpp
index d75eec1..a08dc48 100644
--- a/tests/security/test-sec-tpm-osx.cpp
+++ b/tests/security/test-sec-tpm-osx.cpp
@@ -25,7 +25,8 @@
{
SecTpmOsx tpm;
- Name keyName("/TestSecTpmOsx/Delete/ksk-" + boost::lexical_cast<string>(time::now()));
+ Name keyName("/TestSecTpmOsx/Delete/ksk-" + boost::lexical_cast<string>(
+ time::toUnixTimestamp(time::system_clock::now()).count()));
BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true);
@@ -41,7 +42,8 @@
{
SecTpmOsx tpm;
- Name keyName("/TestSecTpmOsx/SignVerify/ksk-" + boost::lexical_cast<string>(time::now()));
+ Name keyName("/TestSecTpmOsx/SignVerify/ksk-" + boost::lexical_cast<string>(
+ time::toUnixTimestamp(time::system_clock::now()).count()));
BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
Data data("/TestSecTpmOsx/SignVaerify/Data/1");
@@ -102,7 +104,8 @@
SecTpmOsx tpm;
- Name keyName("/TestSecTpmOsx/ExportImportKey/ksk-" + boost::lexical_cast<string>(time::now()));
+ Name keyName("/TestSecTpmOsx/ExportImportKey/ksk-" + boost::lexical_cast<string>(
+ time::toUnixTimestamp(time::system_clock::now()).count()));
BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
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));
diff --git a/tests/security/test-validator.cpp b/tests/security/test-validator.cpp
index bf55bda..c909c06 100644
--- a/tests/security/test-validator.cpp
+++ b/tests/security/test-validator.cpp
@@ -31,7 +31,9 @@
{
KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
- Name identity("/TestValidator/Null/" + boost::lexical_cast<std::string>(time::now()));
+ Name identity("/TestValidator/Null");
+ identity.appendVersion();
+
BOOST_REQUIRE_NO_THROW(keyChain.createIdentity(identity));
Name dataName = identity;