blob: 936e6417ad6bb7ea03b5528250077b98be8eb653 [file] [log] [blame]
Alexander Afanasyev8495a4a2016-12-25 15:27:25 -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, originally written as part of NFD (Named Data Networking Forwarding Daemon),
12 * is a part of ChronoShare, a decentralized file sharing application over NDN.
13 *
14 * ChronoShare 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 * ChronoShare 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 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * See AUTHORS.md for complete list of ChronoShare 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 chronoshare {
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, const std::string& filename, bool wantAdd)
70{
71 shared_ptr<ndn::IdentityCertificate> cert;
72 try {
73 cert = m_keyChain.getCertificate(m_keyChain.getDefaultCertificateNameForIdentity(identity));
74 }
75 catch (const ndn::SecPublicInfo::Error&) {
76 if (wantAdd && this->addIdentity(identity)) {
77 return this->saveIdentityCertificate(identity, filename, false);
78 }
79 return false;
80 }
81
82 m_certFiles.push_back(filename);
83 try {
84 ndn::io::save(*cert, filename);
85 return true;
86 }
87 catch (const ndn::io::Error&) {
88 return false;
89 }
90}
91
92} // namespace tests
93} // namespace chronoshare
94} // namespace ndn