blob: b1f20477c879e7d8df7206acd189cd400d0b7d5d [file] [log] [blame]
Yingdi Yu4a557052014-07-09 16:40:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Spyridon Mastorakis429634f2015-02-19 17:35:33 -08003 * Copyright (c) 2013-2015 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
158BOOST_AUTO_TEST_CASE(OtherTlvs)
159{
160 SignatureInfo info;
161 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
162 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
163 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
164
165 const uint8_t tlv1[] = {
166 0x81, // T
167 0x01, // L
168 0x01, // V
169 };
170 Block block1(tlv1, sizeof(tlv1));
171
172 info.appendTypeSpecificTlv(block1);
173 BOOST_CHECK_THROW(info.getTypeSpecificTlv(0x82), SignatureInfo::Error);
174 BOOST_REQUIRE_NO_THROW(info.getTypeSpecificTlv(0x81));
175}
176
177BOOST_AUTO_TEST_SUITE_END()
178
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800179} // namespace tests
Yingdi Yu4a557052014-07-09 16:40:37 -0700180} // namespace ndn