Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -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/signing-info.hpp" |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 23 | #include "security/key-chain.hpp" |
| 24 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 25 | #include "boost-test.hpp" |
| 26 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 27 | #include <boost/lexical_cast.hpp> |
| 28 | #include <sstream> |
| 29 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 30 | namespace ndn { |
| 31 | namespace security { |
| 32 | namespace tests { |
| 33 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(Security) |
| 35 | BOOST_AUTO_TEST_SUITE(TestSigningInfo) |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 36 | |
| 37 | BOOST_AUTO_TEST_CASE(Basic) |
| 38 | { |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 39 | Name id("/my-identity"); |
| 40 | Name key("/my-key"); |
| 41 | Name cert("/my-cert"); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 42 | |
| 43 | SigningInfo info; |
| 44 | |
| 45 | BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_NULL); |
| 46 | BOOST_CHECK_EQUAL(info.getSignerName(), SigningInfo::EMPTY_NAME); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 47 | BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 48 | |
| 49 | const SignatureInfo& sigInfo = info.getSignatureInfo(); |
| 50 | BOOST_CHECK_EQUAL(sigInfo.getSignatureType(), -1); |
| 51 | BOOST_CHECK_EQUAL(sigInfo.hasKeyLocator(), false); |
| 52 | |
| 53 | info.setSigningIdentity(id); |
| 54 | BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_ID); |
| 55 | BOOST_CHECK_EQUAL(info.getSignerName(), id); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 56 | BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 57 | |
| 58 | SigningInfo infoId(SigningInfo::SIGNER_TYPE_ID, id); |
| 59 | BOOST_CHECK_EQUAL(infoId.getSignerType(), SigningInfo::SIGNER_TYPE_ID); |
| 60 | BOOST_CHECK_EQUAL(infoId.getSignerName(), id); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 61 | BOOST_CHECK_EQUAL(infoId.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 62 | |
| 63 | info.setSigningKeyName(key); |
| 64 | BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_KEY); |
| 65 | BOOST_CHECK_EQUAL(info.getSignerName(), key); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 66 | BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 67 | |
| 68 | SigningInfo infoKey(SigningInfo::SIGNER_TYPE_KEY, key); |
| 69 | BOOST_CHECK_EQUAL(infoKey.getSignerType(), SigningInfo::SIGNER_TYPE_KEY); |
| 70 | BOOST_CHECK_EQUAL(infoKey.getSignerName(), key); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 71 | BOOST_CHECK_EQUAL(infoKey.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 72 | |
| 73 | info.setSigningCertName(cert); |
| 74 | BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_CERT); |
| 75 | BOOST_CHECK_EQUAL(info.getSignerName(), cert); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 76 | BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 77 | |
| 78 | SigningInfo infoCert(SigningInfo::SIGNER_TYPE_CERT, cert); |
| 79 | BOOST_CHECK_EQUAL(infoCert.getSignerType(), SigningInfo::SIGNER_TYPE_CERT); |
| 80 | BOOST_CHECK_EQUAL(infoCert.getSignerName(), cert); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 81 | BOOST_CHECK_EQUAL(infoCert.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 82 | |
| 83 | info.setSha256Signing(); |
| 84 | BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_SHA256); |
| 85 | BOOST_CHECK_EQUAL(info.getSignerName(), SigningInfo::EMPTY_NAME); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 86 | BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 87 | |
| 88 | SigningInfo infoSha(SigningInfo::SIGNER_TYPE_SHA256); |
| 89 | BOOST_CHECK_EQUAL(infoSha.getSignerType(), SigningInfo::SIGNER_TYPE_SHA256); |
| 90 | BOOST_CHECK_EQUAL(infoSha.getSignerName(), SigningInfo::EMPTY_NAME); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 91 | BOOST_CHECK_EQUAL(infoSha.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | BOOST_AUTO_TEST_CASE(CustomSignatureInfo) |
| 95 | { |
| 96 | SigningInfo info1; |
| 97 | BOOST_CHECK(info1.getSignatureInfo() == SignatureInfo()); |
| 98 | |
| 99 | SignatureInfo si; |
| 100 | si.setKeyLocator(Name("ndn:/test/key/locator")); |
| 101 | info1.setSignatureInfo(si); |
| 102 | |
| 103 | BOOST_CHECK(info1.getSignatureInfo() == si); |
| 104 | |
| 105 | SigningInfo info2(SigningInfo::SIGNER_TYPE_NULL, SigningInfo::EMPTY_NAME, si); |
| 106 | BOOST_CHECK(info2.getSignatureInfo() == si); |
| 107 | } |
| 108 | |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 109 | BOOST_AUTO_TEST_CASE(FromString) |
| 110 | { |
| 111 | SigningInfo infoDefault(""); |
| 112 | BOOST_CHECK_EQUAL(infoDefault.getSignerType(), SigningInfo::SIGNER_TYPE_NULL); |
| 113 | BOOST_CHECK_EQUAL(infoDefault.getSignerName(), SigningInfo::EMPTY_NAME); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 114 | BOOST_CHECK_EQUAL(infoDefault.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 115 | |
| 116 | SigningInfo infoId("id:/my-identity"); |
| 117 | BOOST_CHECK_EQUAL(infoId.getSignerType(), SigningInfo::SIGNER_TYPE_ID); |
| 118 | BOOST_CHECK_EQUAL(infoId.getSignerName(), "/my-identity"); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 119 | BOOST_CHECK_EQUAL(infoId.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 120 | |
| 121 | SigningInfo infoKey("key:/my-key"); |
| 122 | BOOST_CHECK_EQUAL(infoKey.getSignerType(), SigningInfo::SIGNER_TYPE_KEY); |
| 123 | BOOST_CHECK_EQUAL(infoKey.getSignerName(), "/my-key"); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 124 | BOOST_CHECK_EQUAL(infoKey.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 125 | |
| 126 | SigningInfo infoCert("cert:/my-cert"); |
| 127 | BOOST_CHECK_EQUAL(infoCert.getSignerType(), SigningInfo::SIGNER_TYPE_CERT); |
| 128 | BOOST_CHECK_EQUAL(infoCert.getSignerName(), "/my-cert"); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 129 | BOOST_CHECK_EQUAL(infoCert.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 130 | |
| 131 | SigningInfo infoSha("id:/localhost/identity/digest-sha256"); |
| 132 | BOOST_CHECK_EQUAL(infoSha.getSignerType(), SigningInfo::SIGNER_TYPE_SHA256); |
| 133 | BOOST_CHECK_EQUAL(infoSha.getSignerName(), SigningInfo::EMPTY_NAME); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 134 | BOOST_CHECK_EQUAL(infoSha.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | BOOST_AUTO_TEST_CASE(ToString) |
| 138 | { |
| 139 | // We can't use lexical_cast due to Boost Bug 6298. |
| 140 | std::stringstream ss; |
| 141 | ss << SigningInfo(); |
| 142 | BOOST_CHECK_EQUAL(ss.str(), ""); |
| 143 | |
| 144 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>( |
| 145 | SigningInfo(SigningInfo::SIGNER_TYPE_ID, "/my-identity")), "id:/my-identity"); |
| 146 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>( |
| 147 | SigningInfo(SigningInfo::SIGNER_TYPE_KEY, "/my-key")), "key:/my-key"); |
| 148 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>( |
| 149 | SigningInfo(SigningInfo::SIGNER_TYPE_CERT, "/my-cert")), "cert:/my-cert"); |
| 150 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>( |
| 151 | SigningInfo(SigningInfo::SIGNER_TYPE_SHA256)), |
| 152 | "id:/localhost/identity/digest-sha256"); |
| 153 | } |
| 154 | |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame^] | 155 | BOOST_AUTO_TEST_CASE(Chaining) |
| 156 | { |
| 157 | SigningInfo info = SigningInfo() |
| 158 | .setSigningIdentity("/identity") |
| 159 | .setSigningKeyName("/key/name") |
| 160 | .setSigningCertName("/cert/name") |
| 161 | .setSha256Signing() |
| 162 | .setDigestAlgorithm(DigestAlgorithm::SHA256) |
| 163 | .setSignatureInfo(SignatureInfo()); |
| 164 | |
| 165 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), "id:/localhost/identity/digest-sha256"); |
| 166 | } |
| 167 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 168 | BOOST_AUTO_TEST_SUITE_END() // TestSigningInfo |
| 169 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 170 | |
| 171 | } // namespace tests |
| 172 | } // namespace security |
| 173 | } // namespace ndn |