security: Add certfile cleanup to IdentityManagementFixture
Change-Id: I7e2c779e7ae6006e7df0d810d5bbf1cc11dede3f
Refs: #3855
diff --git a/tests/identity-management-fixture.cpp b/tests/identity-management-fixture.cpp
index 227f686..8c9f410 100644
--- a/tests/identity-management-fixture.cpp
+++ b/tests/identity-management-fixture.cpp
@@ -20,6 +20,9 @@
*/
#include "identity-management-fixture.hpp"
+#include "util/io.hpp"
+
+#include <boost/filesystem.hpp>
namespace ndn {
namespace tests {
@@ -33,6 +36,11 @@
for (const auto& identity : m_identities) {
m_keyChain.deleteIdentity(identity);
}
+
+ boost::system::error_code ec;
+ for (const auto& certFile : m_certFiles) {
+ boost::filesystem::remove(certFile, ec); // ignore error
+ }
}
bool
@@ -48,5 +56,30 @@
}
}
+bool
+IdentityManagementFixture::saveIdentityCertificate(const Name& identity,
+ const std::string& filename, bool wantAdd)
+{
+ shared_ptr<ndn::IdentityCertificate> cert;
+ try {
+ cert = m_keyChain.getCertificate(m_keyChain.getDefaultCertificateNameForIdentity(identity));
+ }
+ catch (const ndn::SecPublicInfo::Error&) {
+ if (wantAdd && this->addIdentity(identity)) {
+ return this->saveIdentityCertificate(identity, filename, false);
+ }
+ return false;
+ }
+
+ m_certFiles.push_back(filename);
+ try {
+ ndn::io::save(*cert, filename);
+ return true;
+ }
+ catch (const ndn::io::Error&) {
+ return false;
+ }
+}
+
} // namespace tests
} // namespace ndn
diff --git a/tests/identity-management-fixture.hpp b/tests/identity-management-fixture.hpp
index 23eb91f..4df61bf 100644
--- a/tests/identity-management-fixture.hpp
+++ b/tests/identity-management-fixture.hpp
@@ -47,9 +47,22 @@
bool
addIdentity(const Name& identity, const KeyParams& params = KeyChain::DEFAULT_KEY_PARAMS);
+ /**
+ * @brief save identity certificate to a file
+ * @param identity identity name
+ * @param filename file name, should be writable
+ * @param wantAdd if true, add new identity when necessary
+ * @return whether successful
+ */
+ bool
+ saveIdentityCertificate(const Name& identity, const std::string& filename, bool wantAdd = false);
+
protected:
KeyChain m_keyChain;
+
+private:
std::vector<Name> m_identities;
+ std::vector<std::string> m_certFiles;
};
} // namespace tests