blob: 8c9f410e6c7b59f2a33c68542639aa80e8a749a9 [file] [log] [blame]
Yingdi Yud9715e32014-06-27 08:48:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07003 * Copyright (c) 2013-2016 Regents of the University of California.
Yingdi Yud9715e32014-06-27 08:48:47 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "identity-management-fixture.hpp"
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080023#include "util/io.hpp"
24
25#include <boost/filesystem.hpp>
Yingdi Yud9715e32014-06-27 08:48:47 -070026
27namespace ndn {
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070028namespace tests {
Yingdi Yud9715e32014-06-27 08:48:47 -070029
30IdentityManagementFixture::IdentityManagementFixture()
Yingdi Yud9715e32014-06-27 08:48:47 -070031{
32}
33
34IdentityManagementFixture::~IdentityManagementFixture()
35{
Yingdi Yu41546342014-11-30 23:37:53 -080036 for (const auto& identity : m_identities) {
37 m_keyChain.deleteIdentity(identity);
Yingdi Yud9715e32014-06-27 08:48:47 -070038 }
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080039
40 boost::system::error_code ec;
41 for (const auto& certFile : m_certFiles) {
42 boost::filesystem::remove(certFile, ec); // ignore error
43 }
Yingdi Yud9715e32014-06-27 08:48:47 -070044}
45
46bool
47IdentityManagementFixture::addIdentity(const Name& identity, const KeyParams& params)
48{
49 try {
50 m_keyChain.createIdentity(identity, params);
51 m_identities.push_back(identity);
52 return true;
53 }
Yingdi Yu41546342014-11-30 23:37:53 -080054 catch (std::runtime_error&) {
Yingdi Yud9715e32014-06-27 08:48:47 -070055 return false;
56 }
57}
58
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080059bool
60IdentityManagementFixture::saveIdentityCertificate(const Name& identity,
61 const std::string& filename, bool wantAdd)
62{
63 shared_ptr<ndn::IdentityCertificate> cert;
64 try {
65 cert = m_keyChain.getCertificate(m_keyChain.getDefaultCertificateNameForIdentity(identity));
66 }
67 catch (const ndn::SecPublicInfo::Error&) {
68 if (wantAdd && this->addIdentity(identity)) {
69 return this->saveIdentityCertificate(identity, filename, false);
70 }
71 return false;
72 }
73
74 m_certFiles.push_back(filename);
75 try {
76 ndn::io::save(*cert, filename);
77 return true;
78 }
79 catch (const ndn::io::Error&) {
80 return false;
81 }
82}
83
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070084} // namespace tests
Yingdi Yud9715e32014-06-27 08:48:47 -070085} // namespace ndn