blob: f11105969fd6c754c04856fe1229546c5c4c127d [file] [log] [blame]
Yingdi Yu1b0311c2015-06-10 14:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyevc95f5642017-01-04 17:34:26 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yu1b0311c2015-06-10 14:58:47 -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/signing-info.hpp"
Spencer Lee308bc442015-11-24 02:59:55 -070023
Yingdi Yu1b0311c2015-06-10 14:58:47 -070024#include "boost-test.hpp"
25
Davide Pesaventoeee3e822016-11-26 19:19:34 +010026#include <boost/lexical_cast.hpp>
27#include <sstream>
28
Yingdi Yu1b0311c2015-06-10 14:58:47 -070029namespace ndn {
30namespace security {
31namespace tests {
32
Davide Pesaventoeee3e822016-11-26 19:19:34 +010033BOOST_AUTO_TEST_SUITE(Security)
34BOOST_AUTO_TEST_SUITE(TestSigningInfo)
Yingdi Yu1b0311c2015-06-10 14:58:47 -070035
36BOOST_AUTO_TEST_CASE(Basic)
37{
Spencer Lee308bc442015-11-24 02:59:55 -070038 Name id("/my-identity");
39 Name key("/my-key");
40 Name cert("/my-cert");
Yingdi Yu1b0311c2015-06-10 14:58:47 -070041
42 SigningInfo info;
43
44 BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_NULL);
Yingdi Yufe4733a2015-10-22 14:24:12 -070045 BOOST_CHECK_EQUAL(info.getSignerName(), SigningInfo::getEmptyName());
Yingdi Yu99b2a002015-08-12 12:47:44 -070046 BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070047
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);
Yingdi Yu99b2a002015-08-12 12:47:44 -070055 BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070056
57 SigningInfo infoId(SigningInfo::SIGNER_TYPE_ID, id);
58 BOOST_CHECK_EQUAL(infoId.getSignerType(), SigningInfo::SIGNER_TYPE_ID);
59 BOOST_CHECK_EQUAL(infoId.getSignerName(), id);
Yingdi Yu99b2a002015-08-12 12:47:44 -070060 BOOST_CHECK_EQUAL(infoId.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070061
62 info.setSigningKeyName(key);
63 BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_KEY);
64 BOOST_CHECK_EQUAL(info.getSignerName(), key);
Yingdi Yu99b2a002015-08-12 12:47:44 -070065 BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070066
67 SigningInfo infoKey(SigningInfo::SIGNER_TYPE_KEY, key);
68 BOOST_CHECK_EQUAL(infoKey.getSignerType(), SigningInfo::SIGNER_TYPE_KEY);
69 BOOST_CHECK_EQUAL(infoKey.getSignerName(), key);
Yingdi Yu99b2a002015-08-12 12:47:44 -070070 BOOST_CHECK_EQUAL(infoKey.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070071
72 info.setSigningCertName(cert);
73 BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_CERT);
74 BOOST_CHECK_EQUAL(info.getSignerName(), cert);
Yingdi Yu99b2a002015-08-12 12:47:44 -070075 BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070076
77 SigningInfo infoCert(SigningInfo::SIGNER_TYPE_CERT, cert);
78 BOOST_CHECK_EQUAL(infoCert.getSignerType(), SigningInfo::SIGNER_TYPE_CERT);
79 BOOST_CHECK_EQUAL(infoCert.getSignerName(), cert);
Yingdi Yu99b2a002015-08-12 12:47:44 -070080 BOOST_CHECK_EQUAL(infoCert.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070081
82 info.setSha256Signing();
83 BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_SHA256);
Yingdi Yufe4733a2015-10-22 14:24:12 -070084 BOOST_CHECK_EQUAL(info.getSignerName(), SigningInfo::getEmptyName());
Yingdi Yu99b2a002015-08-12 12:47:44 -070085 BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070086
87 SigningInfo infoSha(SigningInfo::SIGNER_TYPE_SHA256);
88 BOOST_CHECK_EQUAL(infoSha.getSignerType(), SigningInfo::SIGNER_TYPE_SHA256);
Yingdi Yufe4733a2015-10-22 14:24:12 -070089 BOOST_CHECK_EQUAL(infoSha.getSignerName(), SigningInfo::getEmptyName());
Yingdi Yu99b2a002015-08-12 12:47:44 -070090 BOOST_CHECK_EQUAL(infoSha.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070091}
92
93BOOST_AUTO_TEST_CASE(CustomSignatureInfo)
94{
95 SigningInfo info1;
96 BOOST_CHECK(info1.getSignatureInfo() == SignatureInfo());
97
98 SignatureInfo si;
99 si.setKeyLocator(Name("ndn:/test/key/locator"));
100 info1.setSignatureInfo(si);
101
102 BOOST_CHECK(info1.getSignatureInfo() == si);
103
Yingdi Yufe4733a2015-10-22 14:24:12 -0700104 SigningInfo info2(SigningInfo::SIGNER_TYPE_NULL, SigningInfo::getEmptyName(), si);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700105 BOOST_CHECK(info2.getSignatureInfo() == si);
106}
107
Spencer Lee308bc442015-11-24 02:59:55 -0700108BOOST_AUTO_TEST_CASE(FromString)
109{
110 SigningInfo infoDefault("");
111 BOOST_CHECK_EQUAL(infoDefault.getSignerType(), SigningInfo::SIGNER_TYPE_NULL);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700112 BOOST_CHECK_EQUAL(infoDefault.getSignerName(), SigningInfo::getEmptyName());
Yingdi Yu99b2a002015-08-12 12:47:44 -0700113 BOOST_CHECK_EQUAL(infoDefault.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Spencer Lee308bc442015-11-24 02:59:55 -0700114
115 SigningInfo infoId("id:/my-identity");
116 BOOST_CHECK_EQUAL(infoId.getSignerType(), SigningInfo::SIGNER_TYPE_ID);
117 BOOST_CHECK_EQUAL(infoId.getSignerName(), "/my-identity");
Yingdi Yu99b2a002015-08-12 12:47:44 -0700118 BOOST_CHECK_EQUAL(infoId.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Spencer Lee308bc442015-11-24 02:59:55 -0700119
120 SigningInfo infoKey("key:/my-key");
121 BOOST_CHECK_EQUAL(infoKey.getSignerType(), SigningInfo::SIGNER_TYPE_KEY);
122 BOOST_CHECK_EQUAL(infoKey.getSignerName(), "/my-key");
Yingdi Yu99b2a002015-08-12 12:47:44 -0700123 BOOST_CHECK_EQUAL(infoKey.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Spencer Lee308bc442015-11-24 02:59:55 -0700124
125 SigningInfo infoCert("cert:/my-cert");
126 BOOST_CHECK_EQUAL(infoCert.getSignerType(), SigningInfo::SIGNER_TYPE_CERT);
127 BOOST_CHECK_EQUAL(infoCert.getSignerName(), "/my-cert");
Yingdi Yu99b2a002015-08-12 12:47:44 -0700128 BOOST_CHECK_EQUAL(infoCert.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Spencer Lee308bc442015-11-24 02:59:55 -0700129
130 SigningInfo infoSha("id:/localhost/identity/digest-sha256");
131 BOOST_CHECK_EQUAL(infoSha.getSignerType(), SigningInfo::SIGNER_TYPE_SHA256);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700132 BOOST_CHECK_EQUAL(infoSha.getSignerName(), SigningInfo::getEmptyName());
Yingdi Yu99b2a002015-08-12 12:47:44 -0700133 BOOST_CHECK_EQUAL(infoSha.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Spencer Lee308bc442015-11-24 02:59:55 -0700134}
135
136BOOST_AUTO_TEST_CASE(ToString)
137{
138 // We can't use lexical_cast due to Boost Bug 6298.
139 std::stringstream ss;
140 ss << SigningInfo();
141 BOOST_CHECK_EQUAL(ss.str(), "");
142
143 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(
144 SigningInfo(SigningInfo::SIGNER_TYPE_ID, "/my-identity")), "id:/my-identity");
145 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(
146 SigningInfo(SigningInfo::SIGNER_TYPE_KEY, "/my-key")), "key:/my-key");
147 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(
148 SigningInfo(SigningInfo::SIGNER_TYPE_CERT, "/my-cert")), "cert:/my-cert");
149 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(
150 SigningInfo(SigningInfo::SIGNER_TYPE_SHA256)),
151 "id:/localhost/identity/digest-sha256");
152}
153
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800154BOOST_AUTO_TEST_CASE(Chaining)
155{
156 SigningInfo info = SigningInfo()
157 .setSigningIdentity("/identity")
158 .setSigningKeyName("/key/name")
159 .setSigningCertName("/cert/name")
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800160 .setPibIdentity(Identity())
161 .setPibKey(Key())
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800162 .setSha256Signing()
163 .setDigestAlgorithm(DigestAlgorithm::SHA256)
164 .setSignatureInfo(SignatureInfo());
165
166 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), "id:/localhost/identity/digest-sha256");
167}
168
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100169BOOST_AUTO_TEST_SUITE_END() // TestSigningInfo
170BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700171
172} // namespace tests
173} // namespace security
174} // namespace ndn