blob: 828bcf86085b0f6410ff664068d538ea7f231ef6 [file] [log] [blame]
Yingdi Yu4a557052014-07-09 16:40:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi605671d2017-08-26 13:41:06 +00002/*
Alexander Afanasyev41469752017-01-10 21:51:55 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yu4a557052014-07-09 16:40: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 "signature-info.hpp"
23
24#include "boost-test.hpp"
Alexander Afanasyev41469752017-01-10 21:51:55 -080025#include <boost/lexical_cast.hpp>
Yingdi Yu4a557052014-07-09 16:40:37 -070026
27namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080028namespace tests {
Yingdi Yu4a557052014-07-09 16:40:37 -070029
30BOOST_AUTO_TEST_SUITE(TestSignatureInfo)
31
32const uint8_t sigInfoRsa[] = {
330x16, 0x1b, // SignatureInfo
34 0x1b, 0x01, // SignatureType
35 0x01, // Sha256WithRsa
36 0x1c, 0x16, // KeyLocator
37 0x07, 0x14, // Name
38 0x08, 0x04,
39 0x74, 0x65, 0x73, 0x74,
40 0x08, 0x03,
41 0x6b, 0x65, 0x79,
42 0x08, 0x07,
43 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
44};
45
46BOOST_AUTO_TEST_CASE(Constructor)
47{
48 SignatureInfo info;
49 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
50 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
51 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
52
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060053 SignatureInfo sha256Info(tlv::DigestSha256);
54 BOOST_CHECK_EQUAL(sha256Info.getSignatureType(), tlv::DigestSha256);
Yingdi Yu4a557052014-07-09 16:40:37 -070055 BOOST_CHECK_EQUAL(sha256Info.hasKeyLocator(), false);
56 BOOST_CHECK_THROW(sha256Info.getKeyLocator(), SignatureInfo::Error);
57
Alexander Afanasyev41469752017-01-10 21:51:55 -080058 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(sha256Info), "DigestSha256");
59
Yingdi Yu4a557052014-07-09 16:40:37 -070060 KeyLocator keyLocator("/test/key/locator");
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060061 SignatureInfo sha256RsaInfo(tlv::SignatureSha256WithRsa, keyLocator);
62 BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa);
Yingdi Yu4a557052014-07-09 16:40:37 -070063 BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true);
64 BOOST_CHECK_NO_THROW(sha256RsaInfo.getKeyLocator());
65 BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator"));
66
Alexander Afanasyev41469752017-01-10 21:51:55 -080067 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(sha256RsaInfo), "SignatureSha256WithRsa Name=/test/key/locator");
68
Yingdi Yu4a557052014-07-09 16:40:37 -070069 const Block& encoded = sha256RsaInfo.wireEncode();
70 Block sigInfoBlock(sigInfoRsa, sizeof(sigInfoRsa));
71
72 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(),
73 sigInfoBlock.wire() + sigInfoBlock.size(),
74 encoded.wire(),
75 encoded.wire() + encoded.size());
76
77 sha256RsaInfo = SignatureInfo(sigInfoBlock);
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060078 BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa);
Yingdi Yu4a557052014-07-09 16:40:37 -070079 BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true);
80 BOOST_CHECK_NO_THROW(sha256RsaInfo.getKeyLocator());
81 BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator"));
82}
83
84BOOST_AUTO_TEST_CASE(ConstructorError)
85{
86 const uint8_t error1[] = {
87 0x15, 0x1b, // Wrong SignatureInfo (0x16, 0x1b)
88 0x1b, 0x01, // SignatureType
89 0x01, // Sha256WithRsa
90 0x1c, 0x16, // KeyLocator
91 0x07, 0x14, // Name
92 0x08, 0x04,
93 0x74, 0x65, 0x73, 0x74,
94 0x08, 0x03,
95 0x6b, 0x65, 0x79,
96 0x08, 0x07,
97 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
98 };
99 Block errorBlock1(error1, sizeof(error1));
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600100 BOOST_CHECK_THROW(SignatureInfo info(errorBlock1), tlv::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -0700101
102 const uint8_t error2[] = {
103 0x16, 0x01, // SignatureInfo
104 0x01 // Wrong SignatureInfo value
105 };
106 Block errorBlock2(error2, sizeof(error2));
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600107 BOOST_CHECK_THROW(SignatureInfo info(errorBlock2), tlv::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -0700108
109 const uint8_t error3[] = {
110 0x16, 0x01, // SignatureInfo
111 0x1a, 0x01, // Wrong SignatureType (0x1b, 0x1b)
112 0x01, // Sha256WithRsa
113 0x1c, 0x16, // KeyLocator
114 0x07, 0x14, // Name
115 0x08, 0x04,
116 0x74, 0x65, 0x73, 0x74,
117 0x08, 0x03,
118 0x6b, 0x65, 0x79,
119 0x08, 0x07,
120 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
121 };
122 Block errorBlock3(error3, sizeof(error3));
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600123 BOOST_CHECK_THROW(SignatureInfo info(errorBlock3), tlv::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -0700124
125 const uint8_t error4[] = {
126 0x16, 0x00 // Empty SignatureInfo
127 };
128 Block errorBlock4(error4, sizeof(error4));
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600129 BOOST_CHECK_THROW(SignatureInfo info(errorBlock4), tlv::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -0700130
131}
132
133BOOST_AUTO_TEST_CASE(SetterGetter)
134{
135 SignatureInfo info;
136 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
137 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
138 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
139
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600140 info.setSignatureType(tlv::SignatureSha256WithRsa);
141 BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithRsa);
Yingdi Yu4a557052014-07-09 16:40:37 -0700142 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
143
144 KeyLocator keyLocator("/test/key/locator");
145 info.setKeyLocator(keyLocator);
146 BOOST_CHECK_EQUAL(info.hasKeyLocator(), true);
147 BOOST_CHECK_NO_THROW(info.getKeyLocator());
148 BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator"));
149
150 const Block& encoded = info.wireEncode();
151 Block sigInfoBlock(sigInfoRsa, sizeof(sigInfoRsa));
152
153 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(),
154 sigInfoBlock.wire() + sigInfoBlock.size(),
155 encoded.wire(),
156 encoded.wire() + encoded.size());
Alexander Afanasyeva7c7f9d2014-07-13 11:51:52 -0700157
158 info.unsetKeyLocator();
159 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
160 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -0700161}
162
Yingdi Yu6be43f32015-06-09 14:19:54 -0700163BOOST_AUTO_TEST_CASE(ValidityPeriodExtension)
164{
165 const uint8_t sigInfo[] = {
166 0x16, 0x45, // SignatureInfo
167 0x1b, 0x01, // SignatureType
168 0x01, // Sha256WithRsa
169 0x1c, 0x16, // KeyLocator
170 0x07, 0x14, // Name
171 0x08, 0x04,
172 0x74, 0x65, 0x73, 0x74,
173 0x08, 0x03,
174 0x6b, 0x65, 0x79,
175 0x08, 0x07,
176 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
177 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
178 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
179 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
180 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
181 0xfd, 0x00, 0xff, 0x0f, // NotAfter
182 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
183 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
184 };
185
186 time::system_clock::TimePoint notBefore = time::getUnixEpoch();
187 time::system_clock::TimePoint notAfter = notBefore + time::days(1);
188 security::ValidityPeriod vp1(notBefore, notAfter);
189
190 // encode
191 SignatureInfo info;
192 info.setSignatureType(tlv::SignatureSha256WithRsa);
193 info.setKeyLocator(KeyLocator("/test/key/locator"));
194 info.setValidityPeriod(vp1);
195
196 BOOST_CHECK(info.getValidityPeriod() == vp1);
197
198 const Block& encoded = info.wireEncode();
199
200 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfo, sigInfo + sizeof(sigInfo),
201 encoded.wire(), encoded.wire() + encoded.size());
202
203 // decode
204 Block block(sigInfo, sizeof(sigInfo));
205 SignatureInfo info2;
206 info2.wireDecode(block);
207 BOOST_CHECK(info2.getValidityPeriod() == vp1);
208
209 const security::ValidityPeriod& validityPeriod = info2.getValidityPeriod();
210 BOOST_CHECK(validityPeriod.getPeriod() == std::make_pair(notBefore, notAfter));
211}
212
Yingdi Yu4a557052014-07-09 16:40:37 -0700213BOOST_AUTO_TEST_CASE(OtherTlvs)
214{
215 SignatureInfo info;
216 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
217 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
218 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
219
220 const uint8_t tlv1[] = {
221 0x81, // T
222 0x01, // L
223 0x01, // V
224 };
225 Block block1(tlv1, sizeof(tlv1));
226
227 info.appendTypeSpecificTlv(block1);
228 BOOST_CHECK_THROW(info.getTypeSpecificTlv(0x82), SignatureInfo::Error);
229 BOOST_REQUIRE_NO_THROW(info.getTypeSpecificTlv(0x81));
230}
231
Alexander Afanasyev41469752017-01-10 21:51:55 -0800232BOOST_AUTO_TEST_CASE(OtherTlvsEncoding) // Bug #3914
233{
Junxiao Shi605671d2017-08-26 13:41:06 +0000234 SignatureInfo info1(tlv::SignatureSha256WithRsa);
Alexander Afanasyev41469752017-01-10 21:51:55 -0800235 info1.appendTypeSpecificTlv(makeStringBlock(101, "First"));
236 info1.appendTypeSpecificTlv(makeStringBlock(102, "Second"));
237 info1.appendTypeSpecificTlv(makeStringBlock(103, "Third"));
238
Junxiao Shi605671d2017-08-26 13:41:06 +0000239 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info1), "SignatureSha256WithRsa { 101 102 103 }");
Alexander Afanasyev41469752017-01-10 21:51:55 -0800240
241 SignatureInfo info2;
242 info2.wireDecode(info1.wireEncode());
243 BOOST_CHECK_EQUAL(info1, info2);
244
Alexander Afanasyev41469752017-01-10 21:51:55 -0800245 const uint8_t infoBytes[] = {
Junxiao Shi605671d2017-08-26 13:41:06 +0000246 0x16, 0x19, // SignatureInfo
247 0x1b, 0x01, 0x01, // SignatureType=1
248 0x65, 0x05, 0x46, 0x69, 0x72, 0x73, 0x74, // 101 "First"
249 0x66, 0x06, 0x53, 0x65, 0x63, 0x6f, 0x6e, 0x64, // 102 "Second"
250 0x67, 0x05, 0x54, 0x68, 0x69, 0x72, 0x64 // 103 "Third"
Alexander Afanasyev41469752017-01-10 21:51:55 -0800251 };
252
253 SignatureInfo info3(Block(infoBytes, sizeof(infoBytes)));
254 BOOST_CHECK_EQUAL(info3, info1);
255 BOOST_CHECK_EQUAL_COLLECTIONS(infoBytes, infoBytes + sizeof(infoBytes),
256 info1.wireEncode().begin(), info1.wireEncode().end());
257}
258
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100259BOOST_AUTO_TEST_SUITE_END() // TestSignatureInfo
Yingdi Yu4a557052014-07-09 16:40:37 -0700260
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800261} // namespace tests
Yingdi Yu4a557052014-07-09 16:40:37 -0700262} // namespace ndn