blob: 605b325b8d1af3a152614547db003b24114224cf [file] [log] [blame]
Yingdi Yu4a557052014-07-09 16:40:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 Regents of the University of California.
4 *
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 {
27
28BOOST_AUTO_TEST_SUITE(TestSignatureInfo)
29
30const uint8_t sigInfoRsa[] = {
310x16, 0x1b, // SignatureInfo
32 0x1b, 0x01, // SignatureType
33 0x01, // Sha256WithRsa
34 0x1c, 0x16, // KeyLocator
35 0x07, 0x14, // Name
36 0x08, 0x04,
37 0x74, 0x65, 0x73, 0x74,
38 0x08, 0x03,
39 0x6b, 0x65, 0x79,
40 0x08, 0x07,
41 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
42};
43
44BOOST_AUTO_TEST_CASE(Constructor)
45{
46 SignatureInfo info;
47 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
48 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
49 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
50
51 SignatureInfo sha256Info(Tlv::DigestSha256);
52 BOOST_CHECK_EQUAL(sha256Info.getSignatureType(), Tlv::DigestSha256);
53 BOOST_CHECK_EQUAL(sha256Info.hasKeyLocator(), false);
54 BOOST_CHECK_THROW(sha256Info.getKeyLocator(), SignatureInfo::Error);
55
56 KeyLocator keyLocator("/test/key/locator");
57 SignatureInfo sha256RsaInfo(Tlv::SignatureSha256WithRsa, keyLocator);
58 BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), Tlv::SignatureSha256WithRsa);
59 BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true);
60 BOOST_CHECK_NO_THROW(sha256RsaInfo.getKeyLocator());
61 BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator"));
62
63 const Block& encoded = sha256RsaInfo.wireEncode();
64 Block sigInfoBlock(sigInfoRsa, sizeof(sigInfoRsa));
65
66 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(),
67 sigInfoBlock.wire() + sigInfoBlock.size(),
68 encoded.wire(),
69 encoded.wire() + encoded.size());
70
71 sha256RsaInfo = SignatureInfo(sigInfoBlock);
72 BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), Tlv::SignatureSha256WithRsa);
73 BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true);
74 BOOST_CHECK_NO_THROW(sha256RsaInfo.getKeyLocator());
75 BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator"));
76}
77
78BOOST_AUTO_TEST_CASE(ConstructorError)
79{
80 const uint8_t error1[] = {
81 0x15, 0x1b, // Wrong SignatureInfo (0x16, 0x1b)
82 0x1b, 0x01, // SignatureType
83 0x01, // Sha256WithRsa
84 0x1c, 0x16, // KeyLocator
85 0x07, 0x14, // Name
86 0x08, 0x04,
87 0x74, 0x65, 0x73, 0x74,
88 0x08, 0x03,
89 0x6b, 0x65, 0x79,
90 0x08, 0x07,
91 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
92 };
93 Block errorBlock1(error1, sizeof(error1));
94 BOOST_CHECK_THROW(SignatureInfo info(errorBlock1), Tlv::Error);
95
96 const uint8_t error2[] = {
97 0x16, 0x01, // SignatureInfo
98 0x01 // Wrong SignatureInfo value
99 };
100 Block errorBlock2(error2, sizeof(error2));
101 BOOST_CHECK_THROW(SignatureInfo info(errorBlock2), Tlv::Error);
102
103 const uint8_t error3[] = {
104 0x16, 0x01, // SignatureInfo
105 0x1a, 0x01, // Wrong SignatureType (0x1b, 0x1b)
106 0x01, // Sha256WithRsa
107 0x1c, 0x16, // KeyLocator
108 0x07, 0x14, // Name
109 0x08, 0x04,
110 0x74, 0x65, 0x73, 0x74,
111 0x08, 0x03,
112 0x6b, 0x65, 0x79,
113 0x08, 0x07,
114 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
115 };
116 Block errorBlock3(error3, sizeof(error3));
117 BOOST_CHECK_THROW(SignatureInfo info(errorBlock3), Tlv::Error);
118
119 const uint8_t error4[] = {
120 0x16, 0x00 // Empty SignatureInfo
121 };
122 Block errorBlock4(error4, sizeof(error4));
123 BOOST_CHECK_THROW(SignatureInfo info(errorBlock4), Tlv::Error);
124
125}
126
127BOOST_AUTO_TEST_CASE(SetterGetter)
128{
129 SignatureInfo info;
130 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
131 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
132 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
133
134 info.setSignatureType(Tlv::SignatureSha256WithRsa);
135 BOOST_CHECK_EQUAL(info.getSignatureType(), Tlv::SignatureSha256WithRsa);
136 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
137
138 KeyLocator keyLocator("/test/key/locator");
139 info.setKeyLocator(keyLocator);
140 BOOST_CHECK_EQUAL(info.hasKeyLocator(), true);
141 BOOST_CHECK_NO_THROW(info.getKeyLocator());
142 BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator"));
143
144 const Block& encoded = info.wireEncode();
145 Block sigInfoBlock(sigInfoRsa, sizeof(sigInfoRsa));
146
147 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(),
148 sigInfoBlock.wire() + sigInfoBlock.size(),
149 encoded.wire(),
150 encoded.wire() + encoded.size());
151}
152
153BOOST_AUTO_TEST_CASE(OtherTlvs)
154{
155 SignatureInfo info;
156 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
157 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
158 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
159
160 const uint8_t tlv1[] = {
161 0x81, // T
162 0x01, // L
163 0x01, // V
164 };
165 Block block1(tlv1, sizeof(tlv1));
166
167 info.appendTypeSpecificTlv(block1);
168 BOOST_CHECK_THROW(info.getTypeSpecificTlv(0x82), SignatureInfo::Error);
169 BOOST_REQUIRE_NO_THROW(info.getTypeSpecificTlv(0x81));
170}
171
172BOOST_AUTO_TEST_SUITE_END()
173
174} // namespace ndn