blob: 4bc69bd7a6c49bec84aacdbddd09ff6b3586a89b [file] [log] [blame]
Mickey Sweatt3b0bea62016-01-25 22:12:27 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3* Copyright (c) 2016 Regents of the University of California.
4*
5* This file is part of the nTorrent codebase.
6*
7* nTorrent 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* nTorrent 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 nTorrent, e.g., in COPYING.md file. If not, see
17* <http://www.gnu.org/licenses/>.
18*
19* See AUTHORS for complete list of nTorrent authors and contributors.
20*/
21
22#include "file-manifest.hpp"
23#include "boost-test.hpp"
24
25#include <vector>
26
27#include <ndn-cxx/data.hpp>
28#include <ndn-cxx/signature.hpp>
29#include <ndn-cxx/security/key-chain.hpp>
30
31BOOST_TEST_DONT_PRINT_LOG_VALUE(std::nullptr_t)
32BOOST_TEST_DONT_PRINT_LOG_VALUE(std::vector<ndn::Name>)
33
34namespace ndn {
35namespace ntorrent {
36namespace tests {
37
38using std::vector;
39using ndn::Name;
40
41BOOST_AUTO_TEST_SUITE(TestFileManifest)
42
43BOOST_AUTO_TEST_CASE(CheckPrimaryAccessorsAndManipulators)
44{
45 FileManifest m1("/file0/1A2B3C4D", 256, "/foo/");
46
47 // Check the values on most simply constructed m1
48 BOOST_CHECK_EQUAL("/file0/1A2B3C4D", m1.name());
49 BOOST_CHECK_EQUAL(256, m1.data_packet_size());
50 BOOST_CHECK_EQUAL(nullptr, m1.submanifest_ptr());
51 BOOST_CHECK_EQUAL("/foo/", m1.catalog_prefix());
52 BOOST_CHECK_EQUAL(vector<Name>({}), m1.catalog());
53
54 // Append names to catalog and recheck all salient attributes
55 m1.push_back("/foo/0/ABC123");
56 m1.push_back("/foo/1/DEADBEFF");
57 m1.push_back("/foo/2/CAFEBABE");
58
59 BOOST_CHECK_EQUAL("/file0/1A2B3C4D", m1.name());
60 BOOST_CHECK_EQUAL(256, m1.data_packet_size());
61 BOOST_CHECK_EQUAL(nullptr, m1.submanifest_ptr());
62 BOOST_CHECK_EQUAL("/foo/", m1.catalog_prefix());
63 BOOST_CHECK_EQUAL(vector<Name>({"/foo/0/ABC123", "/foo/1/DEADBEFF", "/foo/2/CAFEBABE"}),
64 m1.catalog());
65
66 // Remove a value from the catalog and recheck all salient attributes
67 BOOST_CHECK_EQUAL(true, m1.remove("/foo/0/ABC123"));
68
69 BOOST_CHECK_EQUAL("/file0/1A2B3C4D", m1.name());
70 BOOST_CHECK_EQUAL(256, m1.data_packet_size());
71 BOOST_CHECK_EQUAL(nullptr, m1.submanifest_ptr());
72 BOOST_CHECK_EQUAL("/foo/", m1.catalog_prefix());
73 BOOST_CHECK_EQUAL(vector<Name>({"/foo/1/DEADBEFF", "/foo/2/CAFEBABE"}), m1.catalog());
74
75 // Try to remove a value no longer in the catalog, and recheck that all salient attributes
76 BOOST_CHECK_EQUAL(false, m1.remove("/foo/0/ABC123"));
77
78 BOOST_CHECK_EQUAL("/file0/1A2B3C4D", m1.name());
79 BOOST_CHECK_EQUAL(256, m1.data_packet_size());
80 BOOST_CHECK_EQUAL(nullptr, m1.submanifest_ptr());
81 BOOST_CHECK_EQUAL("/foo/", m1.catalog_prefix());
82 BOOST_CHECK_EQUAL(vector<Name>({"/foo/1/DEADBEFF", "/foo/2/CAFEBABE"}), m1.catalog());
83
84 // Try to remove a value never in the catalog, and recheck that all salient attributes
85 BOOST_CHECK_EQUAL(false, m1.remove("/bar/0/ABC123"));
86 BOOST_CHECK_EQUAL("/file0/1A2B3C4D", m1.name());
87 BOOST_CHECK_EQUAL(256, m1.data_packet_size());
88 BOOST_CHECK_EQUAL(nullptr, m1.submanifest_ptr());
89 BOOST_CHECK_EQUAL("/foo/", m1.catalog_prefix());
90 BOOST_CHECK_EQUAL(vector<Name>({"/foo/1/DEADBEFF", "/foo/2/CAFEBABE"}),
91 m1.catalog());
92
93 // Remove a value from the end of the list
94 BOOST_CHECK_EQUAL(true, m1.remove("/foo/2/CAFEBABE"));
95
96 BOOST_CHECK_EQUAL("/file0/1A2B3C4D", m1.name());
97 BOOST_CHECK_EQUAL(256, m1.data_packet_size());
98 BOOST_CHECK_EQUAL(nullptr, m1.submanifest_ptr());
99 BOOST_CHECK_EQUAL("/foo/", m1.catalog_prefix());
100 BOOST_CHECK_EQUAL(vector<Name>({"/foo/1/DEADBEFF"}),
101 m1.catalog());
102}
103
104BOOST_AUTO_TEST_CASE(CheckValueCtors)
105{
106 FileManifest m1("/file0/1A2B3C4D",
107 256,
108 "/foo/",
109 {"/foo/0/ABC123", "/foo/1/DEADBEFF", "/foo/2/CAFEBABE"},
110 std::make_shared<Name>("/file0/1/5E6F7G8H"));
111
112 BOOST_CHECK_EQUAL("/file0/1A2B3C4D", m1.name());
113 BOOST_CHECK_EQUAL(256, m1.data_packet_size());
114 BOOST_CHECK_EQUAL("/file0/1/5E6F7G8H", *m1.submanifest_ptr());
115 BOOST_CHECK_EQUAL("/foo/", m1.catalog_prefix());
116 BOOST_CHECK_EQUAL(vector<Name>({"/foo/0/ABC123", "/foo/1/DEADBEFF", "/foo/2/CAFEBABE"}),
117 m1.catalog());
118
119 // Remove a value from the catalog and recheck all salient attributes
120 BOOST_CHECK_EQUAL(true, m1.remove("/foo/0/ABC123"));
121
122 BOOST_CHECK_EQUAL("/file0/1A2B3C4D", m1.name());
123 BOOST_CHECK_EQUAL(256, m1.data_packet_size());
124 BOOST_CHECK_EQUAL("/file0/1/5E6F7G8H", *m1.submanifest_ptr());
125 BOOST_CHECK_EQUAL("/foo/", m1.catalog_prefix());
126 BOOST_CHECK_EQUAL(vector<Name>({"/foo/1/DEADBEFF", "/foo/2/CAFEBABE"}), m1.catalog());
127
128 // Try to remove a value no longer in the catalog, and recheck that all salient attributes
129 BOOST_CHECK_EQUAL(false, m1.remove("/foo/0/ABC123"));
130
131 BOOST_CHECK_EQUAL("/file0/1A2B3C4D", m1.name());
132 BOOST_CHECK_EQUAL(256, m1.data_packet_size());
133 BOOST_CHECK_EQUAL("/file0/1/5E6F7G8H", *m1.submanifest_ptr());
134 BOOST_CHECK_EQUAL("/foo/", m1.catalog_prefix());
135 BOOST_CHECK_EQUAL(vector<Name>({"/foo/1/DEADBEFF", "/foo/2/CAFEBABE"}), m1.catalog());
136
137 // Try to remove a value never in the catalog, and recheck that all salient attributes
138 BOOST_CHECK_EQUAL(false, m1.remove("/bar/0/ABC123"));
139 BOOST_CHECK_EQUAL("/file0/1A2B3C4D", m1.name());
140 BOOST_CHECK_EQUAL(256, m1.data_packet_size());
141 BOOST_CHECK_EQUAL("/file0/1/5E6F7G8H", *m1.submanifest_ptr());
142 BOOST_CHECK_EQUAL("/foo/", m1.catalog_prefix());
143 BOOST_CHECK_EQUAL(vector<Name>({"/foo/1/DEADBEFF", "/foo/2/CAFEBABE"}), m1.catalog());
144
145 // Remove a value from the end of the list
146 BOOST_CHECK_EQUAL(true, m1.remove("/foo/2/CAFEBABE"));
147
148 BOOST_CHECK_EQUAL("/file0/1A2B3C4D", m1.name());
149 BOOST_CHECK_EQUAL(256, m1.data_packet_size());
150 BOOST_CHECK_EQUAL("/file0/1/5E6F7G8H", *m1.submanifest_ptr());
151 BOOST_CHECK_EQUAL("/foo/", m1.catalog_prefix());
152 BOOST_CHECK_EQUAL(vector<Name>({"/foo/1/DEADBEFF"}), m1.catalog());
153}
154
155BOOST_AUTO_TEST_CASE(CheckAssignmentOperatorEqaulityAndCopyCtor)
156{
157 // Construct two manifests with the same attributes, and check that they related equal
158 {
159 FileManifest m1("/file0/1A2B3C4D", 256, "/foo/", vector<Name>({}), std::make_shared<Name>("/file0/1/5E6F7G8H"));
160 FileManifest m2("/file0/1A2B3C4D", 256, "/foo/", vector<Name>({}), std::make_shared<Name>("/file0/1/5E6F7G8H"));
161
162 BOOST_CHECK_EQUAL(m1, m2);
163 BOOST_CHECK(!(m1 != m2));
164
165 // Change value of one
166 m1.push_back("/foo/0/ABC123");
167 BOOST_CHECK_NE(m1, m2);
168 BOOST_CHECK(!(m1 == m2));
169
170 // Update other
171 m2.push_back("/foo/0/ABC123");
172 BOOST_CHECK_EQUAL(m1, m2);
173 BOOST_CHECK(!(m1 != m2));
174
175 // Change value again
176 m1.remove("/foo/0/ABC123");
177 BOOST_CHECK_NE(m1, m2);
178 BOOST_CHECK(!(m1 == m2));
179
180 m2.remove("/foo/0/ABC123");
181 BOOST_CHECK_EQUAL(m1, m2);
182 BOOST_CHECK(!(m1 != m2));
183 }
184
185 // Set sub-manifest pointer in one and not the other
186 {
187 FileManifest m1("/file0/1A2B3C4D",
188 256,
189 "/foo/",
190 vector<Name>({}),
191 std::make_shared<Name>("/file0/1/5E6F7G8H"));
192
193 FileManifest m2("/file0/1A2B3C4D", 256, "/foo/");
194 BOOST_CHECK_NE(m1, m2);
195 BOOST_CHECK(!(m1 == m2));
196
197 std::swap(m1, m2);
198 BOOST_CHECK_NE(m1, m2);
199 BOOST_CHECK(!(m1 == m2));
200 }
201
202 // Construct two manifests using copy ctor for one
203 {
204 FileManifest m1("/file0/1A2B3C4D", 256, "/foo/");
205 FileManifest m2(m1);
206
207 BOOST_CHECK_EQUAL(m1, m2);
208 BOOST_CHECK(!(m1 != m2));
209
210 // Change value of one
211 m1.push_back("/foo/0/ABC123");
212 BOOST_CHECK_NE(m1, m2);
213 BOOST_CHECK(!(m1 == m2));
214
215 // Update other
216 m2.push_back("/foo/0/ABC123");
217 BOOST_CHECK_EQUAL(m1, m2);
218 BOOST_CHECK(!(m1 != m2));
219
220 // Change value again
221 m1.remove("/foo/0/ABC123");
222 BOOST_CHECK_NE(m1, m2);
223 BOOST_CHECK(!(m1 == m2));
224
225 m2.remove("/foo/0/ABC123");
226 BOOST_CHECK_EQUAL(m1, m2);
227 BOOST_CHECK(!(m1 != m2));
228 }
229
230 // Use assignment operator
231 {
232 FileManifest m1("/file1/1A2B3C4D", 256, "/foo/");
233 FileManifest m2("/file1/5E6F7G8H", 256, "/foo/");
234
235 BOOST_CHECK_NE(m1, m2);
236 BOOST_CHECK(!(m1 == m2));
237
238 m2 = m1;
239 BOOST_CHECK_EQUAL(m1, m2);
240 BOOST_CHECK(!(m1 != m2));
241
242 // Change value of one
243 m1.push_back("/foo/0/ABC123");
244 BOOST_CHECK_NE(m1, m2);
245 BOOST_CHECK(!(m1 == m2));
246
247 // Update other
248 m2.push_back("/foo/0/ABC123");
249 BOOST_CHECK_EQUAL(m1, m2);
250 BOOST_CHECK(!(m1 != m2));
251
252 // Change value again
253 m1.remove("/foo/0/ABC123");
254 BOOST_CHECK_NE(m1, m2);
255 BOOST_CHECK(!(m1 == m2));
256
257 m2.remove("/foo/0/ABC123");
258 BOOST_CHECK_EQUAL(m1, m2);
259 BOOST_CHECK(!(m1 != m2));
260 }
261}
262
263BOOST_AUTO_TEST_CASE(CheckEncodeDecode)
264{
265 // Construct new FileManifest from wire encoding of another FileManifest
266 FileManifest m1("/file0/1A2B3C4D",
267 256,
268 "/foo/",
269 {"/foo/0/ABC123", "/foo/1/DEADBEFF", "/foo/2/CAFEBABE"},
270 std::make_shared<Name>("/file0/1/5E6F7G8H"));
271
272 KeyChain keyChain;
273 keyChain.sign(m1);
274 BOOST_CHECK_EQUAL(m1, FileManifest(m1.wireEncode()));
275
276 // Change value and be sure that wireEncoding still works
277 m1.remove("/foo/2/CAFEBABE");
278 keyChain.sign(m1);
279
280 BOOST_CHECK_EQUAL(m1, FileManifest(m1.wireEncode()));
281
282 // Explicitly call wireEncode and ensure the value works
283 FileManifest m2 = m1;
284 m1.remove("/foo/0/ABC123");
285 keyChain.sign(m1);
286 m2.wireDecode(m1.wireEncode());
287 BOOST_CHECK_EQUAL(m1, m2);
288}
289
290BOOST_AUTO_TEST_SUITE_END()
291
292} // namespace tests
293} // namespace nTorrent
294} // namespace ndn