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