blob: a3543a316cf6b91bc947f52e548b07dd210c711c [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -04002/*
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/key.hpp"
23#include "security/pib/pib.hpp"
24#include "security/pib/pib-memory.hpp"
Yingdi Yucbe72b02015-11-25 17:35:37 -080025#include "security/pib/detail/key-impl.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070026
27#include "boost-test.hpp"
Davide Pesaventoeee3e822016-11-26 19:19:34 +010028#include "pib-data-fixture.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070029
30namespace ndn {
31namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070032namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070033namespace tests {
34
Yingdi Yu6ee2d362015-07-16 21:48:05 -070035using namespace ndn::security::tests;
36
Davide Pesaventoeee3e822016-11-26 19:19:34 +010037BOOST_AUTO_TEST_SUITE(Security)
Yingdi Yu6ee2d362015-07-16 21:48:05 -070038BOOST_AUTO_TEST_SUITE(Pib)
Davide Pesaventoeee3e822016-11-26 19:19:34 +010039BOOST_FIXTURE_TEST_SUITE(TestKey, PibDataFixture)
Yingdi Yub8f8b342015-04-27 11:06:42 -070040
Yingdi Yu6ee2d362015-07-16 21:48:05 -070041using pib::Pib;
42
Davide Pesaventoeee3e822016-11-26 19:19:34 +010043BOOST_AUTO_TEST_CASE(ValidityChecking)
Yingdi Yub8f8b342015-04-27 11:06:42 -070044{
Yingdi Yucbe72b02015-11-25 17:35:37 -080045 using security::pib::detail::KeyImpl;
46
Yingdi Yub8f8b342015-04-27 11:06:42 -070047 Key key;
48
49 BOOST_CHECK_EQUAL(static_cast<bool>(key), false);
50 BOOST_CHECK_EQUAL(!key, true);
51
52 if (key)
53 BOOST_CHECK(false);
54 else
55 BOOST_CHECK(true);
56
Yingdi Yucbe72b02015-11-25 17:35:37 -080057 auto keyImpl = make_shared<KeyImpl>(id1Key1Name, id1Key1.buf(), id1Key1.size(), make_shared<pib::PibMemory>());
58 key = Key(keyImpl);
Yingdi Yub8f8b342015-04-27 11:06:42 -070059
60 BOOST_CHECK_EQUAL(static_cast<bool>(key), true);
61 BOOST_CHECK_EQUAL(!key, false);
62
63 if (key)
64 BOOST_CHECK(true);
65 else
66 BOOST_CHECK(false);
67}
68
Yingdi Yucbe72b02015-11-25 17:35:37 -080069/**
70 * pib::Key is a wrapper of pib::detail::KeyImpl. Since the functionalities of KeyImpl
71 * have already been tested in detail/key-impl.t.cpp, we only test the shared property
72 * of pib::Key in this test case.
73 */
74
75BOOST_AUTO_TEST_CASE(Share)
Yingdi Yub8f8b342015-04-27 11:06:42 -070076{
Yingdi Yucbe72b02015-11-25 17:35:37 -080077 using security::pib::detail::KeyImpl;
Yingdi Yub8f8b342015-04-27 11:06:42 -070078
Yingdi Yucbe72b02015-11-25 17:35:37 -080079 auto keyImpl = make_shared<KeyImpl>(id1Key1Name, id1Key1.buf(), id1Key1.size(), make_shared<pib::PibMemory>());
80 Key key1(keyImpl);
81 Key key2(keyImpl);
Junxiao Shi5759be32017-10-15 00:00:52 +000082 BOOST_CHECK_EQUAL(key1, key2);
83 BOOST_CHECK_NE(key1, Key());
84 BOOST_CHECK_EQUAL(Key(), Key());
Yingdi Yub8f8b342015-04-27 11:06:42 -070085
Yingdi Yucbe72b02015-11-25 17:35:37 -080086 key1.addCertificate(id1Key1Cert1);
87 BOOST_CHECK_NO_THROW(key2.getCertificate(id1Key1Cert1.getName()));
88 key2.removeCertificate(id1Key1Cert1.getName());
89 BOOST_CHECK_THROW(key1.getCertificate(id1Key1Cert1.getName()), Pib::Error);
Yingdi Yub8f8b342015-04-27 11:06:42 -070090
Yingdi Yucbe72b02015-11-25 17:35:37 -080091 key1.setDefaultCertificate(id1Key1Cert1);
92 BOOST_CHECK_NO_THROW(key2.getDefaultCertificate());
Yingdi Yub8f8b342015-04-27 11:06:42 -070093}
94
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -040095BOOST_AUTO_TEST_CASE(Helpers)
96{
97 BOOST_CHECK_EQUAL(v2::constructKeyName("/hello", name::Component("world")), "/hello/KEY/world");
98
99 BOOST_CHECK_EQUAL(v2::isValidKeyName("/hello"), false);
100 BOOST_CHECK_EQUAL(v2::isValidKeyName("/hello/KEY"), false);
101 BOOST_CHECK_EQUAL(v2::isValidKeyName("/hello/KEY/world"), true);
102
103 BOOST_CHECK_EQUAL(v2::isValidKeyName("/KEY/hello"), true);
104 BOOST_CHECK_EQUAL(v2::isValidKeyName("/hello/world/KEY/!"), true);
105
106 BOOST_CHECK_EQUAL(v2::extractIdentityFromKeyName("/KEY/hello"), "/");
107 BOOST_CHECK_EQUAL(v2::extractIdentityFromKeyName("/hello/KEY/world"), "/hello");
108 BOOST_CHECK_EQUAL(v2::extractIdentityFromKeyName("/hello/world/KEY/!"), "/hello/world");
109
110 BOOST_CHECK_THROW(v2::extractIdentityFromKeyName("/hello"), std::invalid_argument);
111}
112
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100113BOOST_AUTO_TEST_SUITE_END() // TestKey
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700114BOOST_AUTO_TEST_SUITE_END() // Pib
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100115BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yub8f8b342015-04-27 11:06:42 -0700116
117} // namespace tests
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700118} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700119} // namespace security
120} // namespace ndn