blob: 1eaa665cb7901eddd241ec62a32fc04a4601985a [file] [log] [blame]
Yingdi Yu4a557052014-07-09 16:40:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventoeee3e822016-11-26 19:19:34 +01003 * Copyright (c) 2013-2016 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"
25
26namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080027namespace tests {
Yingdi Yu4a557052014-07-09 16:40:37 -070028
29BOOST_AUTO_TEST_SUITE(TestSignatureInfo)
30
31const uint8_t sigInfoRsa[] = {
320x16, 0x1b, // SignatureInfo
33 0x1b, 0x01, // SignatureType
34 0x01, // Sha256WithRsa
35 0x1c, 0x16, // KeyLocator
36 0x07, 0x14, // Name
37 0x08, 0x04,
38 0x74, 0x65, 0x73, 0x74,
39 0x08, 0x03,
40 0x6b, 0x65, 0x79,
41 0x08, 0x07,
42 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
43};
44
45BOOST_AUTO_TEST_CASE(Constructor)
46{
47 SignatureInfo info;
48 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
49 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
50 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
51
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060052 SignatureInfo sha256Info(tlv::DigestSha256);
53 BOOST_CHECK_EQUAL(sha256Info.getSignatureType(), tlv::DigestSha256);
Yingdi Yu4a557052014-07-09 16:40:37 -070054 BOOST_CHECK_EQUAL(sha256Info.hasKeyLocator(), false);
55 BOOST_CHECK_THROW(sha256Info.getKeyLocator(), SignatureInfo::Error);
56
57 KeyLocator keyLocator("/test/key/locator");
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060058 SignatureInfo sha256RsaInfo(tlv::SignatureSha256WithRsa, keyLocator);
59 BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa);
Yingdi Yu4a557052014-07-09 16:40:37 -070060 BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true);
61 BOOST_CHECK_NO_THROW(sha256RsaInfo.getKeyLocator());
62 BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator"));
63
64 const Block& encoded = sha256RsaInfo.wireEncode();
65 Block sigInfoBlock(sigInfoRsa, sizeof(sigInfoRsa));
66
67 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(),
68 sigInfoBlock.wire() + sigInfoBlock.size(),
69 encoded.wire(),
70 encoded.wire() + encoded.size());
71
72 sha256RsaInfo = SignatureInfo(sigInfoBlock);
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060073 BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa);
Yingdi Yu4a557052014-07-09 16:40:37 -070074 BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true);
75 BOOST_CHECK_NO_THROW(sha256RsaInfo.getKeyLocator());
76 BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator"));
77}
78
79BOOST_AUTO_TEST_CASE(ConstructorError)
80{
81 const uint8_t error1[] = {
82 0x15, 0x1b, // Wrong SignatureInfo (0x16, 0x1b)
83 0x1b, 0x01, // SignatureType
84 0x01, // Sha256WithRsa
85 0x1c, 0x16, // KeyLocator
86 0x07, 0x14, // Name
87 0x08, 0x04,
88 0x74, 0x65, 0x73, 0x74,
89 0x08, 0x03,
90 0x6b, 0x65, 0x79,
91 0x08, 0x07,
92 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
93 };
94 Block errorBlock1(error1, sizeof(error1));
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060095 BOOST_CHECK_THROW(SignatureInfo info(errorBlock1), tlv::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -070096
97 const uint8_t error2[] = {
98 0x16, 0x01, // SignatureInfo
99 0x01 // Wrong SignatureInfo value
100 };
101 Block errorBlock2(error2, sizeof(error2));
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600102 BOOST_CHECK_THROW(SignatureInfo info(errorBlock2), tlv::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -0700103
104 const uint8_t error3[] = {
105 0x16, 0x01, // SignatureInfo
106 0x1a, 0x01, // Wrong SignatureType (0x1b, 0x1b)
107 0x01, // Sha256WithRsa
108 0x1c, 0x16, // KeyLocator
109 0x07, 0x14, // Name
110 0x08, 0x04,
111 0x74, 0x65, 0x73, 0x74,
112 0x08, 0x03,
113 0x6b, 0x65, 0x79,
114 0x08, 0x07,
115 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
116 };
117 Block errorBlock3(error3, sizeof(error3));
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600118 BOOST_CHECK_THROW(SignatureInfo info(errorBlock3), tlv::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -0700119
120 const uint8_t error4[] = {
121 0x16, 0x00 // Empty SignatureInfo
122 };
123 Block errorBlock4(error4, sizeof(error4));
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600124 BOOST_CHECK_THROW(SignatureInfo info(errorBlock4), tlv::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -0700125
126}
127
128BOOST_AUTO_TEST_CASE(SetterGetter)
129{
130 SignatureInfo info;
131 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
132 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
133 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
134
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600135 info.setSignatureType(tlv::SignatureSha256WithRsa);
136 BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithRsa);
Yingdi Yu4a557052014-07-09 16:40:37 -0700137 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
138
139 KeyLocator keyLocator("/test/key/locator");
140 info.setKeyLocator(keyLocator);
141 BOOST_CHECK_EQUAL(info.hasKeyLocator(), true);
142 BOOST_CHECK_NO_THROW(info.getKeyLocator());
143 BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator"));
144
145 const Block& encoded = info.wireEncode();
146 Block sigInfoBlock(sigInfoRsa, sizeof(sigInfoRsa));
147
148 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(),
149 sigInfoBlock.wire() + sigInfoBlock.size(),
150 encoded.wire(),
151 encoded.wire() + encoded.size());
Alexander Afanasyeva7c7f9d2014-07-13 11:51:52 -0700152
153 info.unsetKeyLocator();
154 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
155 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -0700156}
157
Yingdi Yu6be43f32015-06-09 14:19:54 -0700158BOOST_AUTO_TEST_CASE(ValidityPeriodExtension)
159{
160 const uint8_t sigInfo[] = {
161 0x16, 0x45, // SignatureInfo
162 0x1b, 0x01, // SignatureType
163 0x01, // Sha256WithRsa
164 0x1c, 0x16, // KeyLocator
165 0x07, 0x14, // Name
166 0x08, 0x04,
167 0x74, 0x65, 0x73, 0x74,
168 0x08, 0x03,
169 0x6b, 0x65, 0x79,
170 0x08, 0x07,
171 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
172 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
173 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
174 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
175 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
176 0xfd, 0x00, 0xff, 0x0f, // NotAfter
177 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
178 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
179 };
180
181 time::system_clock::TimePoint notBefore = time::getUnixEpoch();
182 time::system_clock::TimePoint notAfter = notBefore + time::days(1);
183 security::ValidityPeriod vp1(notBefore, notAfter);
184
185 // encode
186 SignatureInfo info;
187 info.setSignatureType(tlv::SignatureSha256WithRsa);
188 info.setKeyLocator(KeyLocator("/test/key/locator"));
189 info.setValidityPeriod(vp1);
190
191 BOOST_CHECK(info.getValidityPeriod() == vp1);
192
193 const Block& encoded = info.wireEncode();
194
195 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfo, sigInfo + sizeof(sigInfo),
196 encoded.wire(), encoded.wire() + encoded.size());
197
198 // decode
199 Block block(sigInfo, sizeof(sigInfo));
200 SignatureInfo info2;
201 info2.wireDecode(block);
202 BOOST_CHECK(info2.getValidityPeriod() == vp1);
203
204 const security::ValidityPeriod& validityPeriod = info2.getValidityPeriod();
205 BOOST_CHECK(validityPeriod.getPeriod() == std::make_pair(notBefore, notAfter));
206}
207
Yingdi Yu4a557052014-07-09 16:40:37 -0700208BOOST_AUTO_TEST_CASE(OtherTlvs)
209{
210 SignatureInfo info;
211 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
212 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
213 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
214
215 const uint8_t tlv1[] = {
216 0x81, // T
217 0x01, // L
218 0x01, // V
219 };
220 Block block1(tlv1, sizeof(tlv1));
221
222 info.appendTypeSpecificTlv(block1);
223 BOOST_CHECK_THROW(info.getTypeSpecificTlv(0x82), SignatureInfo::Error);
224 BOOST_REQUIRE_NO_THROW(info.getTypeSpecificTlv(0x81));
225}
226
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100227BOOST_AUTO_TEST_SUITE_END() // TestSignatureInfo
Yingdi Yu4a557052014-07-09 16:40:37 -0700228
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800229} // namespace tests
Yingdi Yu4a557052014-07-09 16:40:37 -0700230} // namespace ndn