blob: 8903e468fea73a7938b060c7b6614e10677bdaa3 [file] [log] [blame]
Yingdi Yu7036ce22014-06-19 18:53:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -07003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yu7036ce22014-06-19 18:53:37 -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 "security/key-params.hpp"
23
24#include "boost-test.hpp"
25
Yingdi Yuc08d7d62015-07-16 21:05:11 -070026#include <boost/lexical_cast.hpp>
27
Yingdi Yu7036ce22014-06-19 18:53:37 -070028namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080029namespace tests {
Yingdi Yu7036ce22014-06-19 18:53:37 -070030
Davide Pesaventoeee3e822016-11-26 19:19:34 +010031BOOST_AUTO_TEST_SUITE(Security)
32BOOST_AUTO_TEST_SUITE(TestKeyParams)
Yingdi Yu7036ce22014-06-19 18:53:37 -070033
Davide Pesaventoeee3e822016-11-26 19:19:34 +010034BOOST_AUTO_TEST_CASE(Rsa)
Yingdi Yu7036ce22014-06-19 18:53:37 -070035{
36 RsaKeyParams params;
Yingdi Yu99b2a002015-08-12 12:47:44 -070037 BOOST_CHECK_EQUAL(params.getKeyType(), KeyType::RSA);
Yingdi Yu7036ce22014-06-19 18:53:37 -070038 BOOST_CHECK_EQUAL(params.getKeySize(), 2048);
Yingdi Yuc08d7d62015-07-16 21:05:11 -070039 BOOST_CHECK(params.getKeyIdType() == KeyIdType::RANDOM);
Yingdi Yu7036ce22014-06-19 18:53:37 -070040
Yingdi Yuc08d7d62015-07-16 21:05:11 -070041 RsaKeyParams params2(1024, KeyIdType::SHA256);
Yingdi Yu99b2a002015-08-12 12:47:44 -070042 BOOST_CHECK_EQUAL(params2.getKeyType(), KeyType::RSA);
Yingdi Yu7036ce22014-06-19 18:53:37 -070043 BOOST_CHECK_EQUAL(params2.getKeySize(), 1024);
Yingdi Yuc08d7d62015-07-16 21:05:11 -070044 BOOST_CHECK(params2.getKeyIdType() == KeyIdType::SHA256);
Yingdi Yu7036ce22014-06-19 18:53:37 -070045
Yingdi Yuc08d7d62015-07-16 21:05:11 -070046 BOOST_CHECK_THROW(RsaKeyParams(3), KeyParams::Error);
47
48 name::Component keyId("keyId");
49 RsaKeyParams params4(keyId);
50 BOOST_CHECK(params4.getKeyType() == KeyType::RSA);
51 BOOST_CHECK_EQUAL(params4.getKeySize(), 2048);
52 BOOST_CHECK(params4.getKeyIdType() == KeyIdType::USER_SPECIFIED);
53 BOOST_CHECK_EQUAL(params4.getKeyId(), keyId);
Yingdi Yu7036ce22014-06-19 18:53:37 -070054}
55
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -070056BOOST_AUTO_TEST_CASE(Ec)
Yingdi Yu7036ce22014-06-19 18:53:37 -070057{
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -070058 EcKeyParams params;
Yingdi Yu99b2a002015-08-12 12:47:44 -070059 BOOST_CHECK_EQUAL(params.getKeyType(), KeyType::EC);
Yingdi Yu7036ce22014-06-19 18:53:37 -070060 BOOST_CHECK_EQUAL(params.getKeySize(), 256);
Yingdi Yuc08d7d62015-07-16 21:05:11 -070061 BOOST_CHECK(params.getKeyIdType() == KeyIdType::RANDOM);
Yingdi Yu7036ce22014-06-19 18:53:37 -070062
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -070063 EcKeyParams params2(384, KeyIdType::SHA256);
Yingdi Yu99b2a002015-08-12 12:47:44 -070064 BOOST_CHECK_EQUAL(params2.getKeyType(), KeyType::EC);
Yingdi Yu7036ce22014-06-19 18:53:37 -070065 BOOST_CHECK_EQUAL(params2.getKeySize(), 384);
Yingdi Yuc08d7d62015-07-16 21:05:11 -070066 BOOST_CHECK(params2.getKeyIdType() == KeyIdType::SHA256);
Yingdi Yu7036ce22014-06-19 18:53:37 -070067
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -070068 BOOST_CHECK_THROW(EcKeyParams(3), KeyParams::Error);
Yingdi Yuc08d7d62015-07-16 21:05:11 -070069
70 name::Component keyId("keyId");
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -070071 EcKeyParams params4(keyId);
Yingdi Yuc08d7d62015-07-16 21:05:11 -070072 BOOST_CHECK(params4.getKeyType() == KeyType::EC);
73 BOOST_CHECK_EQUAL(params4.getKeySize(), 256);
74 BOOST_CHECK(params4.getKeyIdType() == KeyIdType::USER_SPECIFIED);
75 BOOST_CHECK_EQUAL(params4.getKeyId(), keyId);
Yingdi Yu7036ce22014-06-19 18:53:37 -070076}
77
Davide Pesaventoeee3e822016-11-26 19:19:34 +010078BOOST_AUTO_TEST_CASE(Aes)
Yingdi Yu7036ce22014-06-19 18:53:37 -070079{
Yingdi Yuc08d7d62015-07-16 21:05:11 -070080 name::Component keyId("keyId");
81 AesKeyParams params(keyId);
Yingdi Yu99b2a002015-08-12 12:47:44 -070082 BOOST_CHECK_EQUAL(params.getKeyType(), KeyType::AES);
Yingdi Yuc08d7d62015-07-16 21:05:11 -070083 BOOST_CHECK_EQUAL(params.getKeySize(), 128);
84 BOOST_CHECK_EQUAL(params.getKeyIdType(), KeyIdType::USER_SPECIFIED);
Yingdi Yu7036ce22014-06-19 18:53:37 -070085
Yingdi Yuc08d7d62015-07-16 21:05:11 -070086 AesKeyParams params2(keyId, 192);
87 BOOST_CHECK(params2.getKeyType() == KeyType::AES);
88 BOOST_CHECK_EQUAL(params2.getKeySize(), 192);
89 BOOST_CHECK(params.getKeyIdType() == KeyIdType::USER_SPECIFIED);
Yingdi Yu7036ce22014-06-19 18:53:37 -070090
Yingdi Yuc08d7d62015-07-16 21:05:11 -070091 AesKeyParams params3(keyId, 256);
Yingdi Yu99b2a002015-08-12 12:47:44 -070092 BOOST_CHECK_EQUAL(params3.getKeyType(), KeyType::AES);
Yingdi Yu7036ce22014-06-19 18:53:37 -070093 BOOST_CHECK_EQUAL(params3.getKeySize(), 256);
Yingdi Yuc08d7d62015-07-16 21:05:11 -070094 BOOST_CHECK(params.getKeyIdType() == KeyIdType::USER_SPECIFIED);
Yingdi Yu7036ce22014-06-19 18:53:37 -070095
Yingdi Yuc08d7d62015-07-16 21:05:11 -070096 BOOST_CHECK_THROW(AesKeyParams(keyId, 4), KeyParams::Error);
97
98 AesKeyParams params5(keyId);
99 BOOST_CHECK_EQUAL(params5.getKeyType(), KeyType::AES);
100 BOOST_CHECK_EQUAL(params5.getKeySize(), 128);
101 BOOST_CHECK_EQUAL(params5.getKeyIdType(), KeyIdType::USER_SPECIFIED);
102 BOOST_CHECK_EQUAL(params5.getKeyId(), keyId);
Alexander Afanasyev1709aa72017-03-08 10:16:40 -0800103
104 AesKeyParams params6(192);
105 BOOST_CHECK(params6.getKeyType() == KeyType::AES);
106 BOOST_CHECK_EQUAL(params6.getKeySize(), 192);
107 BOOST_CHECK(params6.getKeyIdType() == KeyIdType::RANDOM);
Yingdi Yu7036ce22014-06-19 18:53:37 -0700108}
109
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700110BOOST_AUTO_TEST_CASE(KeyIdTypeInfo)
Yingdi Yu7036ce22014-06-19 18:53:37 -0700111{
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700112 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(KeyIdType::USER_SPECIFIED), "USER_SPECIFIED");
113 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(KeyIdType::SHA256), "SHA256");
114 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(KeyIdType::RANDOM), "RANDOM");
115 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<KeyIdType>(12345)), "12345");
Yingdi Yu7036ce22014-06-19 18:53:37 -0700116}
117
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700118
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100119BOOST_AUTO_TEST_SUITE_END() // TestKeyParams
120BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu7036ce22014-06-19 18:53:37 -0700121
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800122} // namespace tests
Yingdi Yu7036ce22014-06-19 18:53:37 -0700123} // namespace ndn