blob: ee8c826100e1e2ae366b24cbf5d8ca438d74c82f [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/*
Davide Pesavento14c56cd2020-05-21 01:44:03 -04003 * Copyright (c) 2013-2020 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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/signature-info.hpp"
Yingdi Yu4a557052014-07-09 16:40:37 -070023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
Davide Pesavento14c56cd2020-05-21 01:44:03 -040025
Alexander Afanasyev41469752017-01-10 21:51:55 -080026#include <boost/lexical_cast.hpp>
Yingdi Yu4a557052014-07-09 16:40:37 -070027
28namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080029namespace tests {
Yingdi Yu4a557052014-07-09 16:40:37 -070030
31BOOST_AUTO_TEST_SUITE(TestSignatureInfo)
32
Eric Newberryf165bb42020-05-22 11:28:45 -070033const uint8_t sigInfoDataRsa[] = {
Davide Pesavento14c56cd2020-05-21 01:44:03 -040034 0x16, 0x1b, // SignatureInfo
35 0x1b, 0x01, // SignatureType
36 0x01, // Sha256WithRsa
37 0x1c, 0x16, // KeyLocator
38 0x07, 0x14, // Name
39 0x08, 0x04,
40 0x74, 0x65, 0x73, 0x74,
41 0x08, 0x03,
42 0x6b, 0x65, 0x79,
43 0x08, 0x07,
44 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
Yingdi Yu4a557052014-07-09 16:40:37 -070045};
46
Eric Newberryf165bb42020-05-22 11:28:45 -070047const uint8_t sigInfoInterestRsa[] = {
48 0x2c, 0x33, // InterestSignatureInfo
49 0x1b, 0x01, // SignatureType
50 0x01, // Sha256WithRsa
51 0x1c, 0x16, // KeyLocator
52 0x07, 0x14, // Name
53 0x08, 0x04,
54 0x74, 0x65, 0x73, 0x74,
55 0x08, 0x03,
56 0x6b, 0x65, 0x79,
57 0x08, 0x07,
58 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
59 0x26, 0x08, // SignatureNonce
60 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
61 0x28, 0x08, // SignatureTime
62 0x00, 0x00, 0x01, 0x72, 0x3d, 0x77, 0x00, 0x00,
63 0x2a, 0x02, // SignatureSeqNum
64 0x10, 0x20,
65};
66
Yingdi Yu4a557052014-07-09 16:40:37 -070067BOOST_AUTO_TEST_CASE(Constructor)
68{
69 SignatureInfo info;
70 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
71 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
72 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
Davide Pesavento14c56cd2020-05-21 01:44:03 -040073 BOOST_CHECK_THROW(info.getValidityPeriod(), SignatureInfo::Error);
Eric Newberryf165bb42020-05-22 11:28:45 -070074 BOOST_CHECK(!info.getNonce());
75 BOOST_CHECK(!info.getTime());
76 BOOST_CHECK(!info.getSeqNum());
Davide Pesavento7e6f6f82017-10-31 18:05:28 -040077
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060078 SignatureInfo sha256Info(tlv::DigestSha256);
79 BOOST_CHECK_EQUAL(sha256Info.getSignatureType(), tlv::DigestSha256);
Yingdi Yu4a557052014-07-09 16:40:37 -070080 BOOST_CHECK_THROW(sha256Info.getKeyLocator(), SignatureInfo::Error);
Eric Newberryf165bb42020-05-22 11:28:45 -070081 BOOST_CHECK(!info.getNonce());
82 BOOST_CHECK(!info.getTime());
83 BOOST_CHECK(!info.getSeqNum());
Alexander Afanasyev41469752017-01-10 21:51:55 -080084
Yingdi Yu4a557052014-07-09 16:40:37 -070085 KeyLocator keyLocator("/test/key/locator");
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060086 SignatureInfo sha256RsaInfo(tlv::SignatureSha256WithRsa, keyLocator);
87 BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa);
Yingdi Yu4a557052014-07-09 16:40:37 -070088 BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true);
Yingdi Yu4a557052014-07-09 16:40:37 -070089 BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator"));
Davide Pesavento14c56cd2020-05-21 01:44:03 -040090 BOOST_CHECK_THROW(sha256RsaInfo.getValidityPeriod(), SignatureInfo::Error);
Eric Newberryf165bb42020-05-22 11:28:45 -070091 BOOST_CHECK(!info.getNonce());
92 BOOST_CHECK(!info.getTime());
93 BOOST_CHECK(!info.getSeqNum());
Yingdi Yu4a557052014-07-09 16:40:37 -070094}
95
Eric Newberryf165bb42020-05-22 11:28:45 -070096BOOST_AUTO_TEST_CASE(SignatureType)
Yingdi Yu4a557052014-07-09 16:40:37 -070097{
98 SignatureInfo info;
99 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400100 BOOST_CHECK_THROW(info.wireEncode(), SignatureInfo::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -0700101
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400102 info.setSignatureType(tlv::SignatureSha256WithEcdsa);
103 BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithEcdsa);
104 BOOST_CHECK_EQUAL(info.hasWire(), false);
Yingdi Yu4a557052014-07-09 16:40:37 -0700105
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400106 info.wireEncode();
107 BOOST_CHECK_EQUAL(info.hasWire(), true);
108 info.setSignatureType(tlv::SignatureSha256WithEcdsa);
109 BOOST_CHECK_EQUAL(info.hasWire(), true);
Yingdi Yu4a557052014-07-09 16:40:37 -0700110
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400111 info.setSignatureType(static_cast<tlv::SignatureTypeValue>(1234));
112 BOOST_CHECK_EQUAL(info.getSignatureType(), 1234);
113 BOOST_CHECK_EQUAL(info.hasWire(), false);
Yingdi Yu4a557052014-07-09 16:40:37 -0700114}
115
Eric Newberryf165bb42020-05-22 11:28:45 -0700116// We must name this test case differently to avoid naming conflicts
117BOOST_AUTO_TEST_CASE(KeyLocatorField)
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400118{
119 SignatureInfo info(tlv::SignatureSha256WithEcdsa);
120 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
121 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
122
123 info.setKeyLocator(Name("/test/key/locator"));
124 BOOST_CHECK_EQUAL(info.hasKeyLocator(), true);
125 BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), "/test/key/locator");
126 BOOST_CHECK_EQUAL(info.hasWire(), false);
127
128 info.wireEncode();
129 BOOST_CHECK_EQUAL(info.hasWire(), true);
130 info.setKeyLocator(Name("/test/key/locator"));
131 BOOST_CHECK_EQUAL(info.hasWire(), true);
132 info.setKeyLocator(Name("/another/key/locator"));
133 BOOST_CHECK_EQUAL(info.hasWire(), false);
134
135 info.wireEncode();
136 BOOST_CHECK_EQUAL(info.hasWire(), true);
137 info.setKeyLocator(nullopt);
138 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
139 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
140 BOOST_CHECK_EQUAL(info.hasWire(), false);
141}
142
Eric Newberryf165bb42020-05-22 11:28:45 -0700143BOOST_AUTO_TEST_CASE(SignatureNonce)
144{
145 SignatureInfo info(tlv::SignatureSha256WithEcdsa);
146 BOOST_CHECK(!info.getNonce());
147 info.wireEncode();
148 BOOST_CHECK_EQUAL(info.hasWire(), true);
149 std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
150 info.setNonce(nonce);
151 BOOST_CHECK_EQUAL(info.hasWire(), false);
152 BOOST_CHECK(info.getNonce() == nonce);
153
154 info.wireEncode();
155 info.setNonce(nonce);
156 BOOST_CHECK_EQUAL(info.hasWire(), true);
157 nonce[2] = 0xFF;
158 info.setNonce(nonce);
159 BOOST_CHECK_EQUAL(info.hasWire(), false);
160 BOOST_CHECK(info.getNonce() == nonce);
161
162 info.wireEncode();
163 BOOST_CHECK_EQUAL(info.hasWire(), true);
164 info.setNonce(nullopt);
165 BOOST_CHECK_EQUAL(info.hasWire(), false);
166 BOOST_CHECK(!info.getNonce());
167}
168
169BOOST_AUTO_TEST_CASE(SignatureTime)
170{
171 SignatureInfo info(tlv::SignatureSha256WithEcdsa);
172 BOOST_CHECK(!info.getTime());
173 info.wireEncode();
174 BOOST_CHECK_EQUAL(info.hasWire(), true);
175 time::system_clock::time_point timePoint(1590169108480_ms);
176 info.setTime(timePoint);
177 BOOST_CHECK_EQUAL(info.hasWire(), false);
178 BOOST_CHECK(info.getTime() == timePoint);
179
180 info.wireEncode();
181 info.setTime(timePoint);
182 BOOST_CHECK_EQUAL(info.hasWire(), true);
183 info.setTime(timePoint + 2_s);
184 BOOST_CHECK_EQUAL(info.hasWire(), false);
185 BOOST_CHECK(info.getTime() == timePoint + 2_s);
186
187 info.wireEncode();
188 BOOST_CHECK_EQUAL(info.hasWire(), true);
189 info.setTime(nullopt);
190 BOOST_CHECK_EQUAL(info.hasWire(), false);
191 BOOST_CHECK(!info.getTime());
192}
193
194BOOST_AUTO_TEST_CASE(SignatureSeqNum)
195{
196 SignatureInfo info(tlv::SignatureSha256WithEcdsa);
197 BOOST_CHECK(!info.getSeqNum());
198 info.wireEncode();
199 BOOST_CHECK_EQUAL(info.hasWire(), true);
200 info.setSeqNum(256);
201 BOOST_CHECK_EQUAL(info.hasWire(), false);
202 BOOST_CHECK(info.getSeqNum() == 256UL);
203
204 info.wireEncode();
205 info.setSeqNum(256);
206 BOOST_CHECK_EQUAL(info.hasWire(), true);
207 info.setSeqNum(512);
208 BOOST_CHECK_EQUAL(info.hasWire(), false);
209 BOOST_CHECK(info.getSeqNum() == 512UL);
210
211 info.wireEncode();
212 BOOST_CHECK_EQUAL(info.hasWire(), true);
213 info.setSeqNum(nullopt);
214 BOOST_CHECK_EQUAL(info.hasWire(), false);
215 BOOST_CHECK(!info.getSeqNum());
216}
217
218BOOST_AUTO_TEST_CASE(EncodeDecode)
219{
220 KeyLocator keyLocator("/test/key/locator");
221 SignatureInfo info(tlv::SignatureSha256WithRsa, keyLocator);
222
223 // Encode as SignatureInfo (for Data packets)
224 auto encodedData = info.wireEncode(SignatureInfo::Type::Data);
225 Block sigInfoDataBlock(sigInfoDataRsa, sizeof(sigInfoDataRsa));
226
227 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoDataBlock.wire(), sigInfoDataBlock.wire() + sigInfoDataBlock.size(),
228 encodedData.wire(), encodedData.wire() + encodedData.size());
229
230 // Decode as SignatureInfo (for Data packets)
231 info = SignatureInfo(sigInfoDataBlock, SignatureInfo::Type::Data);
232 BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithRsa);
233 BOOST_CHECK_EQUAL(info.hasKeyLocator(), true);
234 BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator"));
235
236 BOOST_CHECK(!info.getNonce());
237 BOOST_CHECK(!info.getTime());
238 BOOST_CHECK(!info.getSeqNum());
239
240 // Encode as InterestSignatureInfo
241 std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
242 info.setNonce(nonce);
243 time::system_clock::time_point timePoint(1590169108480_ms);
244 info.setTime(timePoint);
245 info.setSeqNum(0x1020);
246 auto encodedInterest = info.wireEncode(SignatureInfo::Type::Interest);
247 Block sigInfoInterestBlock(sigInfoInterestRsa, sizeof(sigInfoInterestRsa));
248
249 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoInterestBlock.wire(),
250 sigInfoInterestBlock.wire() + sigInfoInterestBlock.size(),
251 encodedInterest.wire(),
252 encodedInterest.wire() + encodedInterest.size());
253
254 // Decode as InterestSignatureInfo
255 info = SignatureInfo(sigInfoInterestBlock, SignatureInfo::Type::Interest);
256 BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithRsa);
257 BOOST_CHECK_EQUAL(info.hasKeyLocator(), true);
258 BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator"));
259 BOOST_CHECK(info.getNonce() == nonce);
260 BOOST_CHECK(info.getTime() == timePoint);
261 BOOST_CHECK(info.getSeqNum() == 0x1020UL);
262}
263
264BOOST_AUTO_TEST_CASE(DecodeError)
265{
266 const uint8_t error1[] = {
267 0x15, 0x1b, // Wrong SignatureInfo (0x16, 0x1b)
268 0x1b, 0x01, // SignatureType
269 0x01, // Sha256WithRsa
270 0x1c, 0x16, // KeyLocator
271 0x07, 0x14, // Name
272 0x08, 0x04,
273 0x74, 0x65, 0x73, 0x74,
274 0x08, 0x03,
275 0x6b, 0x65, 0x79,
276 0x08, 0x07,
277 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
278 };
279 Block errorBlock1(error1, sizeof(error1));
280 BOOST_CHECK_THROW(SignatureInfo(errorBlock1, SignatureInfo::Type::Data), tlv::Error);
281
282 const uint8_t error2[] = {
283 0x16, 0x05, // SignatureInfo
284 0x1b, 0x01, // SignatureType
285 0x01, // Sha256WithRsa
286 0x83, 0x00, // Unrecognized critical TLV
287 };
288 Block errorBlock2(error2, sizeof(error2));
289 BOOST_CHECK_THROW(SignatureInfo(errorBlock2, SignatureInfo::Type::Data), tlv::Error);
290
291 const uint8_t error3[] = {
292 0x16, 0x00 // Empty SignatureInfo
293 };
294 Block errorBlock3(error3, sizeof(error3));
295 BOOST_CHECK_THROW(SignatureInfo(errorBlock3, SignatureInfo::Type::Data), tlv::Error);
296
297 // Encoding is correct for SignatureInfo, but decoder is expecting InterestSignatureInfo
298 const uint8_t error4[] = {
299 0x16, 0x1b, // SignatureInfo
300 0x1b, 0x01, // SignatureType
301 0x01, // Sha256WithRsa
302 0x1c, 0x16, // KeyLocator
303 0x07, 0x14, // Name
304 0x08, 0x04,
305 0x74, 0x65, 0x73, 0x74,
306 0x08, 0x03,
307 0x6b, 0x65, 0x79,
308 0x08, 0x07,
309 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
310 };
311 Block errorBlock4(error4, sizeof(error4));
312 BOOST_CHECK_THROW(SignatureInfo(errorBlock4, SignatureInfo::Type::Interest), tlv::Error);
313
314 // SignatureType and KeyLocator out-of-order
315 const uint8_t error5[] = {
316 0x2c, 0x1b, // SignatureInfo
317 0x1c, 0x16, // KeyLocator
318 0x07, 0x14, // Name
319 0x08, 0x04,
320 0x74, 0x65, 0x73, 0x74,
321 0x08, 0x03,
322 0x6b, 0x65, 0x79,
323 0x08, 0x07,
324 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
325 0x1b, 0x01, // SignatureType
326 0x01, // Sha256WithRsa
327 };
328 Block errorBlock5(error5, sizeof(error5));
329 BOOST_CHECK_THROW(SignatureInfo(errorBlock5, SignatureInfo::Type::Interest), tlv::Error);
330
331 // Repeated KeyLocator
332 const uint8_t error6[] = {
333 0x2c, 0x33, // SignatureInfo
334 0x1b, 0x01, // SignatureType
335 0x01, // Sha256WithRsa
336 0x1c, 0x16, // KeyLocator
337 0x07, 0x14, // Name
338 0x08, 0x04,
339 0x74, 0x65, 0x73, 0x74,
340 0x08, 0x03,
341 0x6b, 0x65, 0x79,
342 0x08, 0x07,
343 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
344 0x1c, 0x16, // KeyLocator
345 0x07, 0x14, // Name
346 0x08, 0x04,
347 0x74, 0x65, 0x73, 0x74,
348 0x08, 0x03,
349 0x6b, 0x65, 0x79,
350 0x08, 0x07,
351 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
352 };
353 Block errorBlock6(error6, sizeof(error6));
354 BOOST_CHECK_THROW(SignatureInfo(errorBlock6, SignatureInfo::Type::Interest), tlv::Error);
355
356 // Zero-length SignatureNonce
357 const uint8_t error7[] = {
358 0x2c, 0x05, // SignatureInfo
359 0x1b, 0x01, // SignatureType
360 0x01, // Sha256WithRsa
361 0x26, 0x00 // SignatureNonce
362 };
363 Block errorBlock7(error7, sizeof(error7));
364 BOOST_CHECK_THROW(SignatureInfo(errorBlock7, SignatureInfo::Type::Interest), tlv::Error);
365}
366
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400367BOOST_AUTO_TEST_CASE(ValidityPeriod)
Yingdi Yu6be43f32015-06-09 14:19:54 -0700368{
369 const uint8_t sigInfo[] = {
370 0x16, 0x45, // SignatureInfo
371 0x1b, 0x01, // SignatureType
372 0x01, // Sha256WithRsa
373 0x1c, 0x16, // KeyLocator
374 0x07, 0x14, // Name
375 0x08, 0x04,
376 0x74, 0x65, 0x73, 0x74,
377 0x08, 0x03,
378 0x6b, 0x65, 0x79,
379 0x08, 0x07,
380 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
381 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
382 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
383 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
384 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
385 0xfd, 0x00, 0xff, 0x0f, // NotAfter
386 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
387 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
388 };
389
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400390 const auto notBefore = time::getUnixEpoch();
391 const auto notAfter = notBefore + 1_day;
Yingdi Yu6be43f32015-06-09 14:19:54 -0700392 security::ValidityPeriod vp1(notBefore, notAfter);
393
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400394 SignatureInfo info(tlv::SignatureSha256WithRsa, KeyLocator("/test/key/locator"));
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400395 BOOST_CHECK_THROW(info.getValidityPeriod(), SignatureInfo::Error);
396
397 info.wireEncode();
Yingdi Yu6be43f32015-06-09 14:19:54 -0700398 info.setValidityPeriod(vp1);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000399 BOOST_CHECK_EQUAL(info.getValidityPeriod(), vp1);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400400 BOOST_CHECK_EQUAL(info.hasWire(), false);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700401
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400402 // encode
403 auto encoded = info.wireEncode();
404 BOOST_CHECK_EQUAL(info.hasWire(), true);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700405 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfo, sigInfo + sizeof(sigInfo),
406 encoded.wire(), encoded.wire() + encoded.size());
407
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400408 info.setValidityPeriod(vp1);
409 BOOST_CHECK_EQUAL(info.hasWire(), true);
410
Yingdi Yu6be43f32015-06-09 14:19:54 -0700411 // decode
412 Block block(sigInfo, sizeof(sigInfo));
Eric Newberryf165bb42020-05-22 11:28:45 -0700413 SignatureInfo info2(block, SignatureInfo::Type::Data);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000414 BOOST_CHECK_EQUAL(info2.getValidityPeriod(), vp1);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400415 BOOST_CHECK_EQUAL(info2.hasWire(), true);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700416
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400417 info2.setValidityPeriod(security::ValidityPeriod(notBefore, notBefore + 42_days));
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400418 BOOST_CHECK_NE(info2.getValidityPeriod(), vp1);
419 BOOST_CHECK(info2.getValidityPeriod().getPeriod() == std::make_pair(notBefore, notBefore + 42_days));
420 BOOST_CHECK_EQUAL(info2.hasWire(), false);
421
422 info2.wireEncode();
423 BOOST_CHECK_EQUAL(info2.hasWire(), true);
424 info2.setValidityPeriod(nullopt);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400425 BOOST_CHECK_THROW(info2.getValidityPeriod(), SignatureInfo::Error);
426 BOOST_CHECK_EQUAL(info2.hasWire(), false);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700427}
428
Eric Newberryf165bb42020-05-22 11:28:45 -0700429BOOST_AUTO_TEST_CASE(CustomTlvs)
Yingdi Yu4a557052014-07-09 16:40:37 -0700430{
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400431 SignatureInfo info(tlv::SignatureSha256WithEcdsa);
Eric Newberryf165bb42020-05-22 11:28:45 -0700432 info.addCustomTlv("810101"_block);
433 BOOST_CHECK(!info.getCustomTlv(0x82));
434 BOOST_REQUIRE(info.getCustomTlv(0x81));
435 BOOST_CHECK_EQUAL(info.getCustomTlv(0x81)->type(), 0x81);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400436
437 info.wireEncode();
438 BOOST_CHECK_EQUAL(info.hasWire(), true);
Eric Newberryf165bb42020-05-22 11:28:45 -0700439 info.addCustomTlv("82020202"_block);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400440 BOOST_CHECK_EQUAL(info.hasWire(), false);
Eric Newberryf165bb42020-05-22 11:28:45 -0700441
442 info.wireEncode();
443 BOOST_CHECK_EQUAL(info.hasWire(), true);
444 info.removeCustomTlv(0x81);
445 BOOST_CHECK_EQUAL(info.hasWire(), false);
446 BOOST_CHECK(!info.getCustomTlv(0x81));
Yingdi Yu4a557052014-07-09 16:40:37 -0700447}
448
Eric Newberryf165bb42020-05-22 11:28:45 -0700449BOOST_AUTO_TEST_CASE(CustomTlvsEncoding) // Bug #3914
Alexander Afanasyev41469752017-01-10 21:51:55 -0800450{
Junxiao Shi605671d2017-08-26 13:41:06 +0000451 SignatureInfo info1(tlv::SignatureSha256WithRsa);
Eric Newberryf165bb42020-05-22 11:28:45 -0700452 info1.appendTypeSpecificTlv(makeStringBlock(102, "First"));
453 info1.appendTypeSpecificTlv(makeStringBlock(104, "Second"));
454 info1.appendTypeSpecificTlv(makeStringBlock(106, "Third"));
Alexander Afanasyev41469752017-01-10 21:51:55 -0800455
Eric Newberryf165bb42020-05-22 11:28:45 -0700456 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info1), "SignatureSha256WithRsa { 102 104 106 }");
Alexander Afanasyev41469752017-01-10 21:51:55 -0800457
458 SignatureInfo info2;
Eric Newberryf165bb42020-05-22 11:28:45 -0700459 info2.wireDecode(info1.wireEncode(), SignatureInfo::Type::Data);
Alexander Afanasyev41469752017-01-10 21:51:55 -0800460 BOOST_CHECK_EQUAL(info1, info2);
461
Alexander Afanasyev41469752017-01-10 21:51:55 -0800462 const uint8_t infoBytes[] = {
Junxiao Shi605671d2017-08-26 13:41:06 +0000463 0x16, 0x19, // SignatureInfo
464 0x1b, 0x01, 0x01, // SignatureType=1
Eric Newberryf165bb42020-05-22 11:28:45 -0700465 0x66, 0x05, 0x46, 0x69, 0x72, 0x73, 0x74, // 102 "First"
466 0x68, 0x06, 0x53, 0x65, 0x63, 0x6f, 0x6e, 0x64, // 104 "Second"
467 0x6a, 0x05, 0x54, 0x68, 0x69, 0x72, 0x64 // 106 "Third"
Alexander Afanasyev41469752017-01-10 21:51:55 -0800468 };
469
Eric Newberryf165bb42020-05-22 11:28:45 -0700470 SignatureInfo info3(Block(infoBytes, sizeof(infoBytes)), SignatureInfo::Type::Data);
Alexander Afanasyev41469752017-01-10 21:51:55 -0800471 BOOST_CHECK_EQUAL(info3, info1);
472 BOOST_CHECK_EQUAL_COLLECTIONS(infoBytes, infoBytes + sizeof(infoBytes),
473 info1.wireEncode().begin(), info1.wireEncode().end());
474}
475
Eric Newberryf165bb42020-05-22 11:28:45 -0700476BOOST_AUTO_TEST_CASE(OutputStream)
477{
478 SignatureInfo info;
479 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), "Invalid SignatureInfo");
480
481 info.setSignatureType(tlv::DigestSha256);
482 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), "DigestSha256");
483
484 info.setSignatureType(tlv::SignatureSha256WithRsa);
485 info.setKeyLocator(KeyLocator("/test/key/locator"));
486 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info),
487 "SignatureSha256WithRsa Name=/test/key/locator");
488
489 info.setNonce(std::vector<uint8_t>{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08});
490 info.setTime(time::system_clock::time_point(1590169108480_ms));
491 info.setSeqNum(0x1020);
492
493 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info),
494 "SignatureSha256WithRsa Name=/test/key/locator "
495 "{ Nonce=0102030405060708 Time=1590169108480 SeqNum=4128 }");
496
497 info.setValidityPeriod(security::ValidityPeriod(time::getUnixEpoch(), time::getUnixEpoch() + 31_days));
498
499 info.addCustomTlv("82020102"_block);
500 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info),
501 "SignatureSha256WithRsa Name=/test/key/locator "
502 "{ Nonce=0102030405060708 Time=1590169108480 SeqNum=4128 "
503 "ValidityPeriod=(19700101T000000, 19700201T000000) 130 }");
504
505 info.addCustomTlv("84020102"_block);
506 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info),
507 "SignatureSha256WithRsa Name=/test/key/locator "
508 "{ Nonce=0102030405060708 Time=1590169108480 SeqNum=4128 "
509 "ValidityPeriod=(19700101T000000, 19700201T000000) 130 132 }");
510}
511
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100512BOOST_AUTO_TEST_SUITE_END() // TestSignatureInfo
Yingdi Yu4a557052014-07-09 16:40:37 -0700513
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800514} // namespace tests
Yingdi Yu4a557052014-07-09 16:40:37 -0700515} // namespace ndn