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