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