blob: 3c93e5876a61fcc7fdb2a6cb2865f956a24a1bd4 [file] [log] [blame]
Zhiyi Zhang3e62a832015-07-20 18:36:31 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016 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 * @author Zhiyi Zhang <dreamerbarrychang@gmail.com>
22 */
Davide Pesaventoeee3e822016-11-26 19:19:34 +010023
Zhiyi Zhang3e62a832015-07-20 18:36:31 -070024#include "security/safe-bag.hpp"
25
26#include "boost-test.hpp"
27
28namespace ndn {
29namespace security {
30namespace tests {
31
32BOOST_AUTO_TEST_SUITE(Security)
33BOOST_AUTO_TEST_SUITE(TestSafeBag)
34
35const uint8_t CERT[] = {
36 0x06, 0xc8, // Data
37 0x07, 0x14, // Name
38 0x08, 0x05,
39 0x6c, 0x6f, 0x63, 0x61, 0x6c,
40 0x08, 0x03,
41 0x6e, 0x64, 0x6e,
42 0x08, 0x06,
43 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
44 0x14, 0x07, // MetaInfo
45 0x18, 0x01, // ContentType
46 0x02,
47 0x19, 0x02, // FreshnessPeriod
48 0x27, 0x10,
49 0x15, 0x08, // Content
50 0x53, 0x55, 0x43, 0x43, 0x45, 0x53, 0x53, 0x21,
51 0x16, 0x1b, // SignatureInfo
52 0x1b, 0x01, // SignatureType
53 0x01,
54 0x1c, 0x16, // KeyLocator
55 0x07, 0x14, // Name
56 0x08, 0x04,
57 0x74, 0x65, 0x73, 0x74,
58 0x08, 0x03,
59 0x6b, 0x65, 0x79,
60 0x08, 0x07,
61 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
62 0x17, 0x80, // SignatureValue
63 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
64 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
65 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
66 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
67 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
68 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
69 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
70 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
71 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
72 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1
73};
74
75const uint8_t ENCRYPTED_KEY_BAG[] = {
76 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe
77};
78
79const uint8_t SAFE_BAG[] = {
80 0x80, 0xd4, // SafeBag
81 0x06, 0xc8, // Data
82 0x07, 0x14, // Name
83 0x08, 0x05,
84 0x6c, 0x6f, 0x63, 0x61, 0x6c,
85 0x08, 0x03,
86 0x6e, 0x64, 0x6e,
87 0x08, 0x06,
88 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
89 0x14, 0x07, // MetaInfo
90 0x18, 0x01, // ContentType
91 0x02,
92 0x19, 0x02, // FreshnessPeriod
93 0x27, 0x10,
94 0x15, 0x08, // Content
95 0x53, 0x55, 0x43, 0x43, 0x45, 0x53, 0x53, 0x21,
96 0x16, 0x1b, // SignatureInfo
97 0x1b, 0x01, // SignatureType
98 0x01,
99 0x1c, 0x16, // KeyLocator
100 0x07, 0x14, // Name
101 0x08, 0x04,
102 0x74, 0x65, 0x73, 0x74,
103 0x08, 0x03,
104 0x6b, 0x65, 0x79,
105 0x08, 0x07,
106 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
107 0x17, 0x80, // SignatureValue
108 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
109 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
110 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
111 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
112 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
113 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
114 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
115 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
116 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
117 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1,
118 0x81, 0x08, // EncryptedKeyBag
119 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe
120};
121
122BOOST_AUTO_TEST_CASE(Constructor)
123{
124 Block dataBlock(CERT, sizeof(CERT));
125 Data data(dataBlock);
126 SafeBag safeBag1(data, ENCRYPTED_KEY_BAG, sizeof(ENCRYPTED_KEY_BAG));
127
128 Block safeBagBlock(SAFE_BAG, sizeof(SAFE_BAG));
129 SafeBag safeBag2(safeBagBlock);
130
131 Buffer buffer(ENCRYPTED_KEY_BAG, sizeof(ENCRYPTED_KEY_BAG));
132 SafeBag safeBag3(data, buffer);
133
134 BOOST_CHECK(safeBag1.getCertificate() == data);
135 BOOST_CHECK(safeBag1.getEncryptedKeyBag() == buffer);
136 BOOST_CHECK(safeBag2.getCertificate() == data);
137 BOOST_CHECK(safeBag2.getEncryptedKeyBag() == buffer);
138 BOOST_CHECK(safeBag3.getCertificate() == data);
139 BOOST_CHECK(safeBag3.getEncryptedKeyBag() == buffer);
140}
141
142BOOST_AUTO_TEST_CASE(EncoderAndDecoder)
143{
144 Block dataBlock(CERT, sizeof(CERT));
145 Data data(dataBlock);
146 SafeBag safeBag(data, ENCRYPTED_KEY_BAG, sizeof(ENCRYPTED_KEY_BAG));
147
148 // wire encode
149 Block wireBlock = safeBag.wireEncode();
150 Block block(SAFE_BAG, sizeof(SAFE_BAG));
151
152 // check safe bag block
153 BOOST_CHECK(wireBlock == block);
154
155 // wire decode
156 SafeBag safeBag2;
157 safeBag2.wireDecode(wireBlock);
158
159 // check equal
160 Buffer buffer1 = safeBag2.getEncryptedKeyBag();
161 Buffer buffer2(ENCRYPTED_KEY_BAG, sizeof(ENCRYPTED_KEY_BAG));
162 BOOST_CHECK(buffer1 == buffer2);
163}
164
165BOOST_AUTO_TEST_SUITE_END() // TestSafeBag
166BOOST_AUTO_TEST_SUITE_END() // Security
167
168} // namespace tests
169} // namespace security
170} // namespace ndn