blob: f947fede6ad1575b4fdd9092841ad8dc2d093bda [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu6ee2d362015-07-16 21:48:05 -07003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yub8f8b342015-04-27 11:06:42 -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
Alexander Afanasyev97709c02016-08-25 19:58:30 -070022#include "security/pib/pib.hpp"
23#include "security/pib/pib-memory.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070024
25#include "boost-test.hpp"
Davide Pesaventoeee3e822016-11-26 19:19:34 +010026#include "pib-data-fixture.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070027
28namespace ndn {
29namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070030namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070031namespace tests {
32
Yingdi Yu6ee2d362015-07-16 21:48:05 -070033using namespace ndn::security::tests;
34
Davide Pesaventoeee3e822016-11-26 19:19:34 +010035BOOST_AUTO_TEST_SUITE(Security)
Yingdi Yu6ee2d362015-07-16 21:48:05 -070036BOOST_AUTO_TEST_SUITE(Pib)
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070037BOOST_FIXTURE_TEST_SUITE(TestPib, PibDataFixture)
Yingdi Yub8f8b342015-04-27 11:06:42 -070038
Yingdi Yu6ee2d362015-07-16 21:48:05 -070039using pib::Pib;
40
Davide Pesaventoeee3e822016-11-26 19:19:34 +010041BOOST_AUTO_TEST_CASE(ValidityChecking)
Yingdi Yub8f8b342015-04-27 11:06:42 -070042{
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070043 Pib pib("pib-memory", "", make_shared<PibMemory>());
Yingdi Yub8f8b342015-04-27 11:06:42 -070044
45 Identity id = pib.addIdentity(id1);
46
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070047 BOOST_CHECK_EQUAL(static_cast<bool>(id), true);
Yingdi Yub8f8b342015-04-27 11:06:42 -070048 BOOST_CHECK_EQUAL(!id, false);
49
50 if (id)
51 BOOST_CHECK(true);
52 else
53 BOOST_CHECK(false);
54
55 // key
Yingdi Yu6ee2d362015-07-16 21:48:05 -070056 Key key = id.addKey(id1Key1.buf(), id1Key1.size(), id1Key1Name);
Yingdi Yub8f8b342015-04-27 11:06:42 -070057
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070058 BOOST_CHECK_EQUAL(static_cast<bool>(key), true);
Yingdi Yub8f8b342015-04-27 11:06:42 -070059 BOOST_CHECK_EQUAL(!key, false);
60
61 if (key)
62 BOOST_CHECK(true);
63 else
64 BOOST_CHECK(false);
65}
66
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070067BOOST_AUTO_TEST_CASE(TpmLocator)
68{
69 Pib pib("pib-memory", "", make_shared<PibMemory>());
70
71 BOOST_CHECK_EQUAL(pib.getPibLocator(), "pib-memory:");
72 BOOST_CHECK_THROW(pib.getTpmLocator(), Pib::Error);
73
74 pib.setTpmLocator("test-tpm-locator");
75 BOOST_CHECK_NO_THROW(pib.getTpmLocator());
76
77 BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
78 pib.addIdentity(id1);
79 BOOST_CHECK_NO_THROW(pib.getIdentity(id1));
80
81 pib.setTpmLocator("another-tpm-locator");
82 BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
83
84 pib.addIdentity(id1);
85 BOOST_CHECK_NO_THROW(pib.getIdentity(id1));
86 pib.reset();
87 BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
88 BOOST_CHECK_THROW(pib.getTpmLocator(), Pib::Error);
89}
90
Davide Pesaventoeee3e822016-11-26 19:19:34 +010091BOOST_AUTO_TEST_CASE(IdentityOperations)
Yingdi Yub8f8b342015-04-27 11:06:42 -070092{
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070093 Pib pib("pib-memory", "", make_shared<PibMemory>());
Yingdi Yub8f8b342015-04-27 11:06:42 -070094
95 BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
96 Identity identity1 = pib.addIdentity(id1);
97 BOOST_CHECK_NO_THROW(pib.getIdentity(id1));
98 pib.removeIdentity(id1);
99 BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
100
101 BOOST_CHECK_THROW(pib.getDefaultIdentity(), Pib::Error);
102 BOOST_REQUIRE_NO_THROW(pib.setDefaultIdentity(id1));
103 BOOST_REQUIRE_NO_THROW(pib.getDefaultIdentity());
104 BOOST_CHECK_EQUAL(pib.getDefaultIdentity().getName(), id1);
105 pib.removeIdentity(id1);
106 BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
107 BOOST_CHECK_THROW(pib.getDefaultIdentity(), Pib::Error);
108}
109
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700110BOOST_AUTO_TEST_SUITE_END() // TestPib
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700111BOOST_AUTO_TEST_SUITE_END() // Pib
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100112BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yub8f8b342015-04-27 11:06:42 -0700113
114} // namespace tests
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700115} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700116} // namespace security
117} // namespace ndn