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