blob: 01754ba2f45782fb80759c943d42e0b9011d046f [file] [log] [blame]
Davide Pesavento013de9b2016-09-01 12:06:56 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2016, Regents of the University of California.
4 *
5 * This file is part of ndn-tools (Named Data Networking Essential Tools).
6 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
7 *
8 * ndn-tools is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "identity-management-fixture.hpp"
21#include <ndn-cxx/util/io.hpp>
22#include <boost/filesystem.hpp>
23
24namespace ndn {
25namespace tests {
26
27IdentityManagementFixture::IdentityManagementFixture()
28 : m_keyChainPath((boost::filesystem::path(TMP_TESTS_PATH) / "IdentityManagementFixture").string())
29 , m_keyChain("pib-sqlite3:" + m_keyChainPath, "tpm-file:" + m_keyChainPath)
30{
31}
32
33IdentityManagementFixture::~IdentityManagementFixture()
34{
35 for (const auto& id : m_identities) {
36 m_keyChain.deleteIdentity(id);
37 }
38
39 boost::system::error_code ec; // ignore error
40
41 boost::filesystem::remove_all(m_keyChainPath, ec);
42
43 for (const auto& certFile : m_certFiles) {
44 boost::filesystem::remove(certFile, ec);
45 }
46}
47
48bool
49IdentityManagementFixture::addIdentity(const Name& identity, const KeyParams& params)
50{
51 try {
52 m_keyChain.createIdentity(identity, params);
53 m_identities.push_back(identity);
54 return true;
55 }
56 catch (const std::runtime_error&) {
57 return false;
58 }
59}
60
61bool
62IdentityManagementFixture::saveIdentityCertificate(const Name& identity, const std::string& filename, bool wantAdd)
63{
64 shared_ptr<IdentityCertificate> cert;
65 try {
66 cert = m_keyChain.getCertificate(m_keyChain.getDefaultCertificateNameForIdentity(identity));
67 }
68 catch (const SecPublicInfo::Error&) {
69 if (wantAdd && this->addIdentity(identity)) {
70 return this->saveIdentityCertificate(identity, filename, false);
71 }
72 return false;
73 }
74
75 m_certFiles.push_back(filename);
76 try {
77 io::save(*cert, filename);
78 return true;
79 }
80 catch (const io::Error&) {
81 return false;
82 }
83}
84
85} // namespace tests
86} // namespace ndn