Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 648ae9e | 2018-09-08 15:40:02 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 4 | * |
| 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 Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 26 | #include <boost/lexical_cast.hpp> |
| 27 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 28 | namespace ndn { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 29 | namespace tests { |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 31 | BOOST_AUTO_TEST_SUITE(Security) |
| 32 | BOOST_AUTO_TEST_SUITE(TestKeyParams) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 34 | BOOST_AUTO_TEST_CASE(Rsa) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 35 | { |
| 36 | RsaKeyParams params; |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 37 | BOOST_CHECK_EQUAL(params.getKeyType(), KeyType::RSA); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 38 | BOOST_CHECK_EQUAL(params.getKeySize(), 2048); |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 39 | BOOST_CHECK(params.getKeyIdType() == KeyIdType::RANDOM); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 40 | |
Davide Pesavento | 648ae9e | 2018-09-08 15:40:02 -0400 | [diff] [blame] | 41 | RsaKeyParams params2(4096, KeyIdType::SHA256); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 42 | BOOST_CHECK_EQUAL(params2.getKeyType(), KeyType::RSA); |
Davide Pesavento | 648ae9e | 2018-09-08 15:40:02 -0400 | [diff] [blame] | 43 | BOOST_CHECK_EQUAL(params2.getKeySize(), 4096); |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 44 | BOOST_CHECK(params2.getKeyIdType() == KeyIdType::SHA256); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 45 | |
Davide Pesavento | 648ae9e | 2018-09-08 15:40:02 -0400 | [diff] [blame] | 46 | BOOST_CHECK_THROW(RsaKeyParams(1024), KeyParams::Error); |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 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 Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 56 | BOOST_AUTO_TEST_CASE(Ec) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 57 | { |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 58 | EcKeyParams params; |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 59 | BOOST_CHECK_EQUAL(params.getKeyType(), KeyType::EC); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 60 | BOOST_CHECK_EQUAL(params.getKeySize(), 256); |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 61 | BOOST_CHECK(params.getKeyIdType() == KeyIdType::RANDOM); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 62 | |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 63 | EcKeyParams params2(384, KeyIdType::SHA256); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 64 | BOOST_CHECK_EQUAL(params2.getKeyType(), KeyType::EC); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 65 | BOOST_CHECK_EQUAL(params2.getKeySize(), 384); |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 66 | BOOST_CHECK(params2.getKeyIdType() == KeyIdType::SHA256); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 67 | |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 68 | BOOST_CHECK_THROW(EcKeyParams(3), KeyParams::Error); |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 69 | |
| 70 | name::Component keyId("keyId"); |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 71 | EcKeyParams params4(keyId); |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 72 | 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 Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 78 | BOOST_AUTO_TEST_CASE(Aes) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 79 | { |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 80 | name::Component keyId("keyId"); |
| 81 | AesKeyParams params(keyId); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 82 | BOOST_CHECK_EQUAL(params.getKeyType(), KeyType::AES); |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 83 | BOOST_CHECK_EQUAL(params.getKeySize(), 128); |
| 84 | BOOST_CHECK_EQUAL(params.getKeyIdType(), KeyIdType::USER_SPECIFIED); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 85 | |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 86 | 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 Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 90 | |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 91 | AesKeyParams params3(keyId, 256); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 92 | BOOST_CHECK_EQUAL(params3.getKeyType(), KeyType::AES); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 93 | BOOST_CHECK_EQUAL(params3.getKeySize(), 256); |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 94 | BOOST_CHECK(params.getKeyIdType() == KeyIdType::USER_SPECIFIED); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 95 | |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 96 | 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 Afanasyev | 1709aa7 | 2017-03-08 10:16:40 -0800 | [diff] [blame] | 103 | |
| 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 Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 110 | BOOST_AUTO_TEST_CASE(KeyIdTypeInfo) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 111 | { |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 112 | 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 Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 118 | BOOST_AUTO_TEST_SUITE_END() // TestKeyParams |
| 119 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 120 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 121 | } // namespace tests |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 122 | } // namespace ndn |