blob: 878d0e9080850f61386191141b52143b916b06f1 [file] [log] [blame]
Prashanthc0029b62015-04-27 14:00:08 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California
4 *
5 * This file is part of ndn-group-encrypt (Group-based Encryption Protocol for NDN).
6 * See AUTHORS.md for complete list of ndn-group-encrypt authors and contributors.
7 *
8 * ndn-group-encrypt is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * ndn-group-encrypt is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ndn-group-encrypt, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "encrypted-content.hpp"
21
22#include "boost-test.hpp"
23#include <algorithm>
24
25namespace ndn {
26namespace gep {
27namespace tests {
28
29BOOST_AUTO_TEST_SUITE(TestEncryptedContent)
30
31const uint8_t encrypted[] = {
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700320x82, 0x30, // EncryptedContent
33 0x1c, 0x16, // KeyLocator
34 0x07, 0x14, // Name
35 0x08, 0x04,
36 0x74, 0x65, 0x73, 0x74, // 'test'
37 0x08, 0x03,
38 0x6b, 0x65, 0x79, // 'key'
39 0x08, 0x07,
40 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, // 'locator'
41 0x83, 0x01, // EncryptedAlgorithm
42 0x00,
43 0x85, 0x0a, // InitialVector
44 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73,
45 0x84, 0x07, // EncryptedPayload
46 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
47};
48
49const uint8_t encrypted_noiv[] = {
Prashanthc0029b62015-04-27 14:00:08 -0700500x82, 0x24, // EncryptedContent
51 0x1c, 0x16, // KeyLocator
52 0x07, 0x14, // Name
53 0x08, 0x04,
54 0x74, 0x65, 0x73, 0x74, // 'test'
55 0x08, 0x03,
56 0x6b, 0x65, 0x79, // 'key'
57 0x08, 0x07,
58 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, // 'locator'
59 0x83, 0x01, // EncryptedAlgorithm
60 0x00,
61 0x84, 0x07, // EncryptedPayload
62 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
63};
64
65const uint8_t message[] = {
66 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
67};
68
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -070069const uint8_t iv[] = {
70 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73
71};
72
Prashanthc0029b62015-04-27 14:00:08 -070073BOOST_AUTO_TEST_CASE(Constructor)
74{
75 EncryptedContent content;
76 BOOST_CHECK_EQUAL(content.getAlgorithmType(), -1);
77 BOOST_CHECK_EQUAL(content.getPayload() == nullptr, true);
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -070078 BOOST_CHECK_EQUAL(content.getInitialVector() == nullptr, true);
Prashanthc0029b62015-04-27 14:00:08 -070079 BOOST_CHECK_EQUAL(content.hasKeyLocator(), false);
80 BOOST_CHECK_THROW(content.getKeyLocator(), EncryptedContent::Error);
81
82 ConstBufferPtr payload = make_shared<Buffer>(message, sizeof(message));
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -070083 ConstBufferPtr initialVector = make_shared<Buffer>(iv, sizeof(iv));
84
Prashanthc0029b62015-04-27 14:00:08 -070085 KeyLocator keyLocator("test/key/locator");
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -070086 EncryptedContent sha256RsaContent(tlv::AlgorithmSha256WithRsa, keyLocator, payload, initialVector);
Prashanthc0029b62015-04-27 14:00:08 -070087 ConstBufferPtr contentPayload = sha256RsaContent.getPayload();
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -070088 ConstBufferPtr contentInitialVector = sha256RsaContent.getInitialVector();
Prashanthc0029b62015-04-27 14:00:08 -070089
90 BOOST_CHECK_EQUAL(sha256RsaContent.getAlgorithmType(), tlv::AlgorithmSha256WithRsa);
91 BOOST_CHECK_EQUAL_COLLECTIONS(contentPayload->begin(),
92 contentPayload->end(),
93 payload->begin(),
94 payload->end());
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -070095 BOOST_CHECK_EQUAL_COLLECTIONS(contentInitialVector->begin(),
96 contentInitialVector->end(),
97 initialVector->begin(),
98 initialVector->end());
Prashanthc0029b62015-04-27 14:00:08 -070099 BOOST_CHECK_EQUAL(sha256RsaContent.hasKeyLocator(), true);
100 BOOST_CHECK_NO_THROW(sha256RsaContent.getKeyLocator());
101 BOOST_CHECK_EQUAL(sha256RsaContent.getKeyLocator().getName(), Name("test/key/locator"));
102
103 Block encryptedBlock(encrypted, sizeof(encrypted));
104 const Block& encoded = sha256RsaContent.wireEncode();
105
106 BOOST_CHECK_EQUAL_COLLECTIONS(encryptedBlock.wire(),
107 encryptedBlock.wire() + encryptedBlock.size(),
108 encoded.wire(),
109 encoded.wire() + encoded.size());
110
111 sha256RsaContent = EncryptedContent(encryptedBlock);
112 contentPayload = sha256RsaContent.getPayload();
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700113 contentInitialVector = sha256RsaContent.getInitialVector();
Prashanthc0029b62015-04-27 14:00:08 -0700114
115 BOOST_CHECK_EQUAL(sha256RsaContent.getAlgorithmType(), tlv::AlgorithmSha256WithRsa);
116 BOOST_CHECK_EQUAL(sha256RsaContent.hasKeyLocator(), true);
117 BOOST_CHECK_EQUAL_COLLECTIONS(contentPayload->begin(),
118 contentPayload->end(),
119 payload->begin(),
120 payload->end());
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700121 BOOST_CHECK_EQUAL_COLLECTIONS(contentInitialVector->begin(),
122 contentInitialVector->end(),
123 initialVector->begin(),
124 initialVector->end());
Prashanthc0029b62015-04-27 14:00:08 -0700125 BOOST_CHECK_NO_THROW(sha256RsaContent.getKeyLocator());
126 BOOST_CHECK_EQUAL(sha256RsaContent.getKeyLocator().getName(), Name("test/key/locator"));
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700127
128 sha256RsaContent = EncryptedContent(tlv::AlgorithmSha256WithRsa, keyLocator, payload);
129
130 BOOST_CHECK_EQUAL(sha256RsaContent.getAlgorithmType(), tlv::AlgorithmSha256WithRsa);
131 BOOST_CHECK_EQUAL_COLLECTIONS(contentPayload->begin(),
132 contentPayload->end(),
133 payload->begin(),
134 payload->end());
135 BOOST_CHECK_EQUAL(sha256RsaContent.getInitialVector() == nullptr, true);
136 BOOST_CHECK_EQUAL(sha256RsaContent.hasKeyLocator(), true);
137 BOOST_CHECK_NO_THROW(sha256RsaContent.getKeyLocator());
138 BOOST_CHECK_EQUAL(sha256RsaContent.getKeyLocator().getName(), Name("test/key/locator"));
139
140 encryptedBlock = Block(encrypted_noiv, sizeof(encrypted_noiv));
141 const Block& encodedNoIV = sha256RsaContent.wireEncode();
142
143 BOOST_CHECK_EQUAL_COLLECTIONS(encryptedBlock.wire(),
144 encryptedBlock.wire() + encryptedBlock.size(),
145 encodedNoIV.wire(),
146 encodedNoIV.wire() + encodedNoIV.size());
147
148 sha256RsaContent = EncryptedContent(encryptedBlock);
149 contentPayload = sha256RsaContent.getPayload();
150
151 BOOST_CHECK_EQUAL(sha256RsaContent.getAlgorithmType(), tlv::AlgorithmSha256WithRsa);
152 BOOST_CHECK_EQUAL(sha256RsaContent.hasKeyLocator(), true);
153 BOOST_CHECK_EQUAL_COLLECTIONS(contentPayload->begin(),
154 contentPayload->end(),
155 payload->begin(),
156 payload->end());
157 BOOST_CHECK_EQUAL(sha256RsaContent.getInitialVector() == nullptr, true);
158 BOOST_CHECK_NO_THROW(sha256RsaContent.getKeyLocator());
159 BOOST_CHECK_EQUAL(sha256RsaContent.getKeyLocator().getName(), Name("test/key/locator"));
160
Prashanthc0029b62015-04-27 14:00:08 -0700161}
162
163BOOST_AUTO_TEST_CASE(ConstructorError)
164{
165 const uint8_t error1[] = {
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700166 0x1f, 0x30, // Wrong EncryptedContent (0x82, 0x24)
Prashanthc0029b62015-04-27 14:00:08 -0700167 0x1c, 0x16, // KeyLocator
168 0x07, 0x14, // Name
169 0x08, 0x04,
170 0x74, 0x65, 0x73, 0x74,
171 0x08, 0x03,
172 0x6b, 0x65, 0x79,
173 0x08, 0x07,
174 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
175 0x83, 0x01, // EncryptedAlgorithm
176 0x00,
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700177 0x85, 0x0a, // InitialVector
178 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73,
Prashanthc0029b62015-04-27 14:00:08 -0700179 0x84, 0x07, // EncryptedPayload
180 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
181 };
182 Block errorBlock1(error1, sizeof(error1));
183 BOOST_CHECK_THROW(EncryptedContent info(errorBlock1), EncryptedContent::Error);
184
185 const uint8_t error2[] = {
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700186 0x82, 0x30, // EncryptedContent
Prashanthc0029b62015-04-27 14:00:08 -0700187 0x1d, 0x16, // Wrong KeyLocator (0x1c, 0x16)
188 0x07, 0x14, // Name
189 0x08, 0x04,
190 0x74, 0x65, 0x73, 0x74,
191 0x08, 0x03,
192 0x6b, 0x65, 0x79,
193 0x08, 0x07,
194 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
195 0x83, 0x01, // EncryptedAlgorithm
196 0x00,
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700197 0x85, 0x0a, // InitialVector
198 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73,
Prashanthc0029b62015-04-27 14:00:08 -0700199 0x84, 0x07, // EncryptedPayload
200 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
201 };
202 Block errorBlock2(error2, sizeof(error2));
203 BOOST_CHECK_THROW(EncryptedContent info(errorBlock2), EncryptedContent::Error);
204
205 const uint8_t error3[] = {
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700206 0x82, 0x30, // EncryptedContent
Prashanthc0029b62015-04-27 14:00:08 -0700207 0x1c, 0x16, // KeyLocator
208 0x07, 0x14, // Name
209 0x08, 0x04,
210 0x74, 0x65, 0x73, 0x74,
211 0x08, 0x03,
212 0x6b, 0x65, 0x79,
213 0x08, 0x07,
214 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
215 0x1d, 0x01, // Wrong EncryptedAlgorithm (0x83, 0x01)
216 0x00,
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700217 0x85, 0x0a, // InitialVector
218 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73,
Prashanthc0029b62015-04-27 14:00:08 -0700219 0x84, 0x07, // EncryptedPayload
220 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
221 };
222 Block errorBlock3(error3, sizeof(error3));
223 BOOST_CHECK_THROW(EncryptedContent info(errorBlock3), EncryptedContent::Error);
224
225 const uint8_t error4[] = {
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700226 0x82, 0x30, // EncryptedContent
Prashanthc0029b62015-04-27 14:00:08 -0700227 0x1c, 0x16, // KeyLocator
228 0x07, 0x14, // Name
229 0x08, 0x04,
230 0x74, 0x65, 0x73, 0x74, // 'test'
231 0x08, 0x03,
232 0x6b, 0x65, 0x79, // 'key'
233 0x08, 0x07,
234 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, // 'locator'
235 0x83, 0x01, // EncryptedAlgorithm
236 0x00,
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700237 0x1f, 0x0a, // InitialVector (0x84, 0x0a)
238 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73,
239 0x84, 0x07, // EncryptedPayload
Prashanthc0029b62015-04-27 14:00:08 -0700240 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
241 };
242 Block errorBlock4(error4, sizeof(error4));
243 BOOST_CHECK_THROW(EncryptedContent info(errorBlock4), EncryptedContent::Error);
244
245 const uint8_t error5[] = {
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700246 0x82, 0x30, // EncryptedContent
247 0x1c, 0x16, // KeyLocator
248 0x07, 0x14, // Name
249 0x08, 0x04,
250 0x74, 0x65, 0x73, 0x74, // 'test'
251 0x08, 0x03,
252 0x6b, 0x65, 0x79, // 'key'
253 0x08, 0x07,
254 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, // 'locator'
255 0x83, 0x01, // EncryptedAlgorithm
256 0x00,
257 0x85, 0x0a, // InitialVector
258 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73,
259 0x21, 0x07, // EncryptedPayload (0x85, 0x07)
260 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
Prashanthc0029b62015-04-27 14:00:08 -0700261 };
262 Block errorBlock5(error5, sizeof(error5));
263 BOOST_CHECK_THROW(EncryptedContent info(errorBlock5), EncryptedContent::Error);
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700264
265 const uint8_t error6[] = {
266 0x82, 0x00 // Empty EncryptedContent
267 };
268 Block errorBlock6(error6, sizeof(error6));
269 BOOST_CHECK_THROW(EncryptedContent info(errorBlock6), EncryptedContent::Error);
Prashanthc0029b62015-04-27 14:00:08 -0700270}
271
272BOOST_AUTO_TEST_CASE(SetterGetter)
273{
274 EncryptedContent content;
275 BOOST_CHECK_EQUAL(content.getAlgorithmType(), -1);
276 BOOST_CHECK_EQUAL(content.getPayload() == nullptr, true);
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700277 BOOST_CHECK_EQUAL(content.getInitialVector() == nullptr, true);
Prashanthc0029b62015-04-27 14:00:08 -0700278 BOOST_CHECK_EQUAL(content.hasKeyLocator(), false);
279 BOOST_CHECK_THROW(content.getKeyLocator(), EncryptedContent::Error);
280
281 content.setAlgorithmType(tlv::AlgorithmSha256WithRsa);
282 BOOST_CHECK_EQUAL(content.getAlgorithmType(), tlv::AlgorithmSha256WithRsa);
283 BOOST_CHECK_EQUAL(content.getPayload() == nullptr, true);
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700284 BOOST_CHECK_EQUAL(content.getInitialVector() == nullptr, true);
Prashanthc0029b62015-04-27 14:00:08 -0700285 BOOST_CHECK_EQUAL(content.hasKeyLocator(), false);
286
287 KeyLocator keyLocator("/test/key/locator");
288 content.setKeyLocator(keyLocator);
289 BOOST_CHECK_EQUAL(content.hasKeyLocator(), true);
290 BOOST_CHECK_NO_THROW(content.getKeyLocator());
291 BOOST_CHECK_EQUAL(content.getKeyLocator().getName(), Name("/test/key/locator"));
292 BOOST_CHECK_EQUAL(content.getPayload() == nullptr, true);
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700293 BOOST_CHECK_EQUAL(content.getInitialVector() == nullptr, true);
Prashanthc0029b62015-04-27 14:00:08 -0700294
295 ConstBufferPtr payload = make_shared<Buffer>(message, sizeof(message));
296 content.setPayload(payload);
297
298 ConstBufferPtr contentPayload = content.getPayload();
299 BOOST_CHECK_EQUAL_COLLECTIONS(contentPayload->begin(),
300 contentPayload->end(),
301 payload->begin(),
302 payload->end());
303
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700304 ConstBufferPtr initialVector = make_shared<Buffer>(iv, sizeof(iv));
305 content.setInitialVector(initialVector);
306
307 ConstBufferPtr contentInitialVector = content.getInitialVector();
308 BOOST_CHECK_EQUAL_COLLECTIONS(contentInitialVector->begin(),
309 contentInitialVector->end(),
310 initialVector->begin(),
311 initialVector->end());
312
Prashanthc0029b62015-04-27 14:00:08 -0700313 const Block& encoded = content.wireEncode();
314 Block contentBlock(encrypted, sizeof(encrypted));
315
316 BOOST_CHECK_EQUAL_COLLECTIONS(contentBlock.wire(),
317 contentBlock.wire() + contentBlock.size(),
318 encoded.wire(),
319 encoded.wire() + encoded.size());
320}
321BOOST_AUTO_TEST_SUITE_END()
322
323} // namespace tests
324} // namespace gep
325} // namespace ndn