blob: b9d6d41d7f3997c6d4649dbd97f649869cc6d6bb [file] [log] [blame]
Zhiyi Zhang8617a792017-01-17 16:45:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2017, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file, originally written as part of NFD (Named Data Networking Forwarding Daemon),
12 * is a part of ndncert, a certificate management system based on NDN.
13 *
14 * ndncert is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation, either
16 * version 3 of the License, or (at your option) any later version.
17 *
18 * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
20 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received copies of the GNU General Public License along with
23 * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * See AUTHORS.md for complete list of ndncert authors and contributors.
26 */
27
28#include "identity-management-fixture.hpp"
29
30#include <ndn-cxx/util/io.hpp>
31#include <boost/filesystem.hpp>
32
33namespace ndn {
34namespace ndncert {
35namespace tests {
36
37IdentityManagementFixture::IdentityManagementFixture()
38 : m_keyChain("sqlite3", "file")
39{
40 m_keyChain.getDefaultCertificate(); // side effect: create a default cert if it doesn't exist
41}
42
43IdentityManagementFixture::~IdentityManagementFixture()
44{
45 for (const auto& id : m_identities) {
46 m_keyChain.deleteIdentity(id);
47 }
48
49 boost::system::error_code ec;
50 for (const auto& certFile : m_certFiles) {
51 boost::filesystem::remove(certFile, ec); // ignore error
52 }
53}
54
55bool
56IdentityManagementFixture::addIdentity(const Name& identity, const ndn::KeyParams& params)
57{
58 try {
59 m_keyChain.createIdentity(identity, params);
60 m_identities.push_back(identity);
61 return true;
62 }
63 catch (std::runtime_error&) {
64 return false;
65 }
66}
67
68bool
69IdentityManagementFixture::saveIdentityCertificate(const Name& identity,
70 const std::string& filename, bool wantAdd)
71{
72 shared_ptr<ndn::IdentityCertificate> cert;
73 try {
74 cert = m_keyChain.getCertificate(m_keyChain.getDefaultCertificateNameForIdentity(identity));
75 }
76 catch (const ndn::SecPublicInfo::Error&) {
77 if (wantAdd && this->addIdentity(identity)) {
78 return this->saveIdentityCertificate(identity, filename, false);
79 }
80 return false;
81 }
82
83 m_certFiles.push_back(filename);
84 try {
85 ndn::io::save(*cert, filename);
86 return true;
87 }
88 catch (const ndn::io::Error&) {
89 return false;
90 }
91}
92
93} // namespace tests
94} // namespace ndncert
95} // namespace ndn