tests: sync common testing infrastructure with ndn-cxx
Change-Id: I6feab5247231abc35b8daa96bca21ad17c9cc4b3
diff --git a/tests/key-chain-fixture.cpp b/tests/key-chain-fixture.cpp
index 9e20dd3..c71f175 100644
--- a/tests/key-chain-fixture.cpp
+++ b/tests/key-chain-fixture.cpp
@@ -25,11 +25,6 @@
#include "tests/key-chain-fixture.hpp"
-#include <ndn-cxx/security/certificate.hpp>
-#include <ndn-cxx/security/pib/identity.hpp>
-#include <ndn-cxx/security/pib/key.hpp>
-#include <ndn-cxx/security/pib/pib.hpp>
-#include <ndn-cxx/security/transform.hpp>
#include <ndn-cxx/util/io.hpp>
#include <boost/filesystem.hpp>
@@ -37,10 +32,11 @@
namespace nfd {
namespace tests {
+using namespace ndn::security;
+
KeyChainFixture::KeyChainFixture()
: m_keyChain("pib-memory:", "tpm-memory:")
{
- m_keyChain.createIdentity("/DEFAULT");
}
KeyChainFixture::~KeyChainFixture()
@@ -51,32 +47,33 @@
}
}
-bool
-KeyChainFixture::addIdentity(const Name& identity, const ndn::KeyParams& params)
+Certificate
+KeyChainFixture::makeCert(const Key& key, const std::string& issuer, const Key& signingKey)
{
- try {
- m_keyChain.createIdentity(identity, params);
- return true;
- }
- catch (const std::runtime_error&) {
- return false;
- }
+ Certificate cert;
+ cert.setName(Name(key.getName())
+ .append(issuer)
+ .appendVersion());
+
+ // set metainfo
+ cert.setContentType(tlv::ContentType_Key);
+ cert.setFreshnessPeriod(1_h);
+
+ // set content
+ cert.setContent(key.getPublicKey().data(), key.getPublicKey().size());
+
+ // set signature info
+ ndn::SignatureInfo info;
+ auto now = time::system_clock::now();
+ info.setValidityPeriod(ValidityPeriod(now - 30_days, now + 30_days));
+
+ m_keyChain.sign(cert, signingByKey(signingKey ? signingKey : key).setSignatureInfo(info));
+ return cert;
}
bool
-KeyChainFixture::saveIdentityCertificate(const Name& identity, const std::string& filename, bool allowAdd)
+KeyChainFixture::saveCert(const Data& cert, const std::string& filename)
{
- ndn::security::Certificate cert;
- try {
- cert = m_keyChain.getPib().getIdentity(identity).getDefaultKey().getDefaultCertificate();
- }
- catch (const ndn::security::Pib::Error&) {
- if (allowAdd && addIdentity(identity)) {
- return saveIdentityCertificate(identity, filename, false);
- }
- return false;
- }
-
m_certFiles.push_back(filename);
try {
ndn::io::save(cert, filename);
@@ -87,26 +84,39 @@
}
}
-std::string
-KeyChainFixture::getIdentityCertificateBase64(const Name& identity, bool allowAdd)
+bool
+KeyChainFixture::saveIdentityCert(const Identity& identity, const std::string& filename)
{
- ndn::security::Certificate cert;
+ Certificate cert;
try {
- cert = m_keyChain.getPib().getIdentity(identity).getDefaultKey().getDefaultCertificate();
+ cert = identity.getDefaultKey().getDefaultCertificate();
}
- catch (const ndn::security::Pib::Error&) {
- if (!allowAdd) {
- NDN_THROW_NESTED(std::runtime_error("Identity does not exist"));
+ catch (const Pib::Error&) {
+ return false;
+ }
+
+ return saveCert(cert, filename);
+}
+
+bool
+KeyChainFixture::saveIdentityCert(const Name& identityName, const std::string& filename,
+ bool allowCreate)
+{
+ Identity id;
+ try {
+ id = m_keyChain.getPib().getIdentity(identityName);
+ }
+ catch (const Pib::Error&) {
+ if (allowCreate) {
+ id = m_keyChain.createIdentity(identityName);
}
- cert = m_keyChain.createIdentity(identity).getDefaultKey().getDefaultCertificate();
}
- const auto& block = cert.wireEncode();
+ if (!id) {
+ return false;
+ }
- namespace tr = ndn::security::transform;
- std::ostringstream oss;
- tr::bufferSource(block.wire(), block.size()) >> tr::base64Encode(false) >> tr::streamSink(oss);
- return oss.str();
+ return saveIdentityCert(id, filename);
}
} // namespace tests