blob: af223d4ab39e932fc28d8490bdfb0977feb72b9f [file] [log] [blame]
Varun Patila24bd3e2020-11-24 10:08:33 +05301/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2020, Regents of the University of California
4 *
5 * BSD license, See the LICENSE file for more information
6 */
7
Mengjin Yanaec70742014-08-25 10:37:45 -07008#include "chatroom-info.hpp"
9#include <boost/test/unit_test.hpp>
10#include <ndn-cxx/encoding/block.hpp>
11
Qiuhan Ding52f13832015-03-06 14:05:59 -080012namespace chronochat {
Mengjin Yanaec70742014-08-25 10:37:45 -070013
Qiuhan Ding52f13832015-03-06 14:05:59 -080014namespace tests {
Mengjin Yanaec70742014-08-25 10:37:45 -070015
16using std::string;
17
18BOOST_AUTO_TEST_SUITE(TestChatroomInfo)
19
20const uint8_t chatroomInfo[] = {
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070021 0x80, 0x5d, // ChatroomInfo
22 0x81, 0x06, // ChatroomName
23 0x08, 0x04,
24 0x06e, 0x64, 0x6e, 0x64,
Mengjin Yanaec70742014-08-25 10:37:45 -070025 0x82, 0x01, // TrustModel
26 0x01,
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070027 0x83, 0x12, // ChatroomPrefix
28 0x07, 0x10,
29 0x08, 0x03,
30 0x6e, 0x64, 0x6e,
31 0x08, 0x09,
32 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
33 0x84, 0x14, // ManagerPrefix
Mengjin Yanaec70742014-08-25 10:37:45 -070034 0x07, 0x12,
35 0x08, 0x03,
36 0x6e, 0x64, 0x6e,
37 0x08, 0x04,
38 0x75, 0x63, 0x6c, 0x61,
39 0x08, 0x05,
40 0x61, 0x6c, 0x69, 0x63, 0x65,
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070041 0x85, 0x26,// Participants
42 0x07, 0x12,
43 0x08, 0x03,
44 0x6e, 0x64, 0x6e,
45 0x08, 0x04,
46 0x75, 0x63, 0x6c, 0x61,
47 0x08, 0x05,
48 0x61, 0x6c, 0x69, 0x63, 0x65,
Mengjin Yanaec70742014-08-25 10:37:45 -070049 0x07, 0x10,
50 0x08, 0x03,
51 0x6e, 0x64, 0x6e,
52 0x08, 0x04,
53 0x75, 0x63, 0x6c, 0x61,
54 0x08, 0x03,
55 0x79, 0x6d, 0x6a
56};
57
58BOOST_AUTO_TEST_CASE(EncodeChatroom)
59{
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070060
61 // ChatroomInfo := CHATROOM-INFO-TYPE TLV-LENGTH
62 // ChatroomName
63 // TrustModel
64 // ChatroomPrefix
65 // ManagerPrefix
66 // Participants
67 //
68 // ChatroomName := CHATROOM-NAME-TYPE TLV-LENGTH
69 // NameComponent
70 //
71 // TrustModel := TRUST-MODEL-TYPE TLV-LENGTH
72 // nonNegativeInteger
73 //
74 // ChatroomPrefix := CHATROOM-PREFIX-TYPE TLV-LENGTH
75 // Name
76 //
77 // ManagerPrefix := MANAGER-PREFIX-TYPE TLV-LENGTH
78 // Name
79 //
80 // Participants := PARTICIPANTS-TYPE TLV-LENGTH
81 // Name+
Mengjin Yanaec70742014-08-25 10:37:45 -070082
83 ChatroomInfo chatroom;
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070084 chatroom.setName(ndn::Name::Component("ndnd"));
85 chatroom.setManager("/ndn/ucla/alice");
86 chatroom.setSyncPrefix("/ndn/broadcast");
Mengjin Yanaec70742014-08-25 10:37:45 -070087 chatroom.addParticipant(Name("/ndn/ucla/alice"));
88 chatroom.addParticipant(Name("/ndn/ucla/ymj"));
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070089 chatroom.addParticipant(Name("/ndn/ucla"));
90 chatroom.removeParticipant(Name("/ndn/ucla"));
Mengjin Yanaec70742014-08-25 10:37:45 -070091 chatroom.setTrustModel(ChatroomInfo::TRUST_MODEL_WEBOFTRUST);
92
93 const Block& encoded = chatroom.wireEncode();
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070094
Mengjin Yanaec70742014-08-25 10:37:45 -070095 Block chatroomInfoBlock(chatroomInfo, sizeof(chatroomInfo));
96
97 BOOST_CHECK_EQUAL_COLLECTIONS(chatroomInfoBlock.wire(),
98 chatroomInfoBlock.wire() + chatroomInfoBlock.size(),
99 encoded.wire(),
100 encoded.wire() + encoded.size());
101}
102
103BOOST_AUTO_TEST_CASE(DecodeChatroomCorrect)
104{
105 ChatroomInfo chatroom;
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700106 chatroom.setName(ndn::Name::Component("ndnd"));
107 chatroom.setManager("/ndn/ucla/alice");
108 chatroom.setSyncPrefix("/ndn/broadcast");
Mengjin Yanaec70742014-08-25 10:37:45 -0700109 chatroom.addParticipant(Name("/ndn/ucla/alice"));
110 chatroom.addParticipant(Name("/ndn/ucla/ymj"));
111 chatroom.setTrustModel(ChatroomInfo::TRUST_MODEL_WEBOFTRUST);
112
113 Block chatroomInfoBlock(chatroomInfo, sizeof(chatroomInfo));
114 ChatroomInfo dechatroom;
115 dechatroom.wireDecode(chatroomInfoBlock);
Mengjin Yanaec70742014-08-25 10:37:45 -0700116
117 BOOST_CHECK_EQUAL(chatroom.getName(), dechatroom.getName());
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700118 BOOST_CHECK_EQUAL(chatroom.getSyncPrefix().toUri(), dechatroom.getSyncPrefix().toUri());
119 BOOST_CHECK_EQUAL(chatroom.getManagerPrefix().toUri(), dechatroom.getManagerPrefix().toUri());
Mengjin Yanaec70742014-08-25 10:37:45 -0700120 BOOST_CHECK_EQUAL(chatroom.getParticipants().size(), dechatroom.getParticipants().size());
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700121 BOOST_CHECK_EQUAL(chatroom.getParticipants().begin()->toUri(),
122 dechatroom.getParticipants().begin()->toUri());
123 BOOST_CHECK_EQUAL(chatroom.getParticipants().begin()->toUri(),
124 dechatroom.getParticipants().begin()->toUri());
Mengjin Yanaec70742014-08-25 10:37:45 -0700125}
126
127BOOST_AUTO_TEST_CASE(DecodeChatroomError)
128{
129 const uint8_t error1[] = {
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700130 0x81, 0x5d, // ChatroomInfo Type Error
131 0x81, 0x06, // ChatroomName
132 0x08, 0x04,
133 0x06e, 0x64, 0x6e, 0x64,
Mengjin Yanaec70742014-08-25 10:37:45 -0700134 0x82, 0x01, // TrustModel
135 0x01,
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700136 0x83, 0x12, // ChatroomPrefix
137 0x07, 0x10,
138 0x08, 0x03,
139 0x6e, 0x64, 0x6e,
140 0x08, 0x09,
141 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
142 0x84, 0x14, // ManagerPrefix
Mengjin Yanaec70742014-08-25 10:37:45 -0700143 0x07, 0x12,
144 0x08, 0x03,
145 0x6e, 0x64, 0x6e,
146 0x08, 0x04,
147 0x75, 0x63, 0x6c, 0x61,
148 0x08, 0x05,
149 0x61, 0x6c, 0x69, 0x63, 0x65,
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700150 0x85, 0x26,// Participants
151 0x07, 0x12,
152 0x08, 0x03,
153 0x6e, 0x64, 0x6e,
154 0x08, 0x04,
155 0x75, 0x63, 0x6c, 0x61,
156 0x08, 0x05,
157 0x61, 0x6c, 0x69, 0x63, 0x65,
Mengjin Yanaec70742014-08-25 10:37:45 -0700158 0x07, 0x10,
159 0x08, 0x03,
160 0x6e, 0x64, 0x6e,
161 0x08, 0x04,
162 0x75, 0x63, 0x6c, 0x61,
163 0x08, 0x03,
164 0x79, 0x6d, 0x6a
165 };
166
167 Block errorBlock1(error1, sizeof(error1));
168 BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock1), ChatroomInfo::Error);
169
170 const uint8_t error2[] = {
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700171 0x80, 0x5d, // ChatroomInfo
172 0x81, 0x06, // ChatroomName
173 0x08, 0x04,
174 0x06e, 0x64, 0x6e, 0x64,
175 0x83, 0x01, // TrustModel Type Error
Mengjin Yanaec70742014-08-25 10:37:45 -0700176 0x01,
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700177 0x83, 0x12, // ChatroomPrefix
178 0x07, 0x10,
179 0x08, 0x03,
180 0x6e, 0x64, 0x6e,
181 0x08, 0x09,
182 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
183 0x84, 0x14, // ManagerPrefix
Mengjin Yanaec70742014-08-25 10:37:45 -0700184 0x07, 0x12,
185 0x08, 0x03,
186 0x6e, 0x64, 0x6e,
187 0x08, 0x04,
188 0x75, 0x63, 0x6c, 0x61,
189 0x08, 0x05,
190 0x61, 0x6c, 0x69, 0x63, 0x65,
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700191 0x85, 0x26,// Participants
192 0x07, 0x12,
193 0x08, 0x03,
194 0x6e, 0x64, 0x6e,
195 0x08, 0x04,
196 0x75, 0x63, 0x6c, 0x61,
197 0x08, 0x05,
198 0x61, 0x6c, 0x69, 0x63, 0x65,
Mengjin Yanaec70742014-08-25 10:37:45 -0700199 0x07, 0x10,
200 0x08, 0x03,
201 0x6e, 0x64, 0x6e,
202 0x08, 0x04,
203 0x75, 0x63, 0x6c, 0x61,
204 0x08, 0x03,
205 0x79, 0x6d, 0x6a
206 };
207
208 Block errorBlock2(error2, sizeof(error2));
209 BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock2), ChatroomInfo::Error);
210
211 const uint8_t error3[] = {
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700212 0x80, 0x5d, // ChatroomInfo
213 0x81, 0x06, // ChatroomName
214 0x08, 0x04,
215 0x06e, 0x64, 0x6e, 0x64,
Mengjin Yanaec70742014-08-25 10:37:45 -0700216 0x82, 0x01, // TrustModel
217 0x01,
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700218 0x80, 0x12, // ChatroomPrefix Type Error
219 0x07, 0x10,
220 0x08, 0x03,
221 0x6e, 0x64, 0x6e,
222 0x08, 0x09,
223 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
224 0x84, 0x14, // ManagerPrefix
Mengjin Yanaec70742014-08-25 10:37:45 -0700225 0x07, 0x12,
226 0x08, 0x03,
227 0x6e, 0x64, 0x6e,
228 0x08, 0x04,
229 0x75, 0x63, 0x6c, 0x61,
230 0x08, 0x05,
231 0x61, 0x6c, 0x69, 0x63, 0x65,
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700232 0x85, 0x26,// Participants
233 0x07, 0x12,
234 0x08, 0x03,
235 0x6e, 0x64, 0x6e,
236 0x08, 0x04,
237 0x75, 0x63, 0x6c, 0x61,
238 0x08, 0x05,
239 0x61, 0x6c, 0x69, 0x63, 0x65,
Mengjin Yanaec70742014-08-25 10:37:45 -0700240 0x07, 0x10,
241 0x08, 0x03,
242 0x6e, 0x64, 0x6e,
243 0x08, 0x04,
244 0x75, 0x63, 0x6c, 0x61,
245 0x08, 0x03,
246 0x79, 0x6d, 0x6a
247 };
248
249 Block errorBlock3(error3, sizeof(error3));
250 BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock3), ChatroomInfo::Error);
251
252 const uint8_t error4[] = {
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700253 0x80, 0x5d, // ChatroomInfo
254 0x81, 0x06, // ChatroomName
255 0x08, 0x04,
256 0x06e, 0x64, 0x6e, 0x64,
257 0x82, 0x01, // TrustModel
258 0x01,
259 0x83, 0x12, // ChatroomPrefix
260 0x07, 0x10,
261 0x08, 0x03,
262 0x6e, 0x64, 0x6e,
263 0x08, 0x09,
264 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
265 0x80, 0x14, // ManagerPrefix Error Type
266 0x07, 0x12,
267 0x08, 0x03,
268 0x6e, 0x64, 0x6e,
269 0x08, 0x04,
270 0x75, 0x63, 0x6c, 0x61,
271 0x08, 0x05,
272 0x61, 0x6c, 0x69, 0x63, 0x65,
273 0x85, 0x26,// Participants
274 0x07, 0x12,
275 0x08, 0x03,
276 0x6e, 0x64, 0x6e,
277 0x08, 0x04,
278 0x75, 0x63, 0x6c, 0x61,
279 0x08, 0x05,
280 0x61, 0x6c, 0x69, 0x63, 0x65,
281 0x07, 0x10,
282 0x08, 0x03,
283 0x6e, 0x64, 0x6e,
284 0x08, 0x04,
285 0x75, 0x63, 0x6c, 0x61,
286 0x08, 0x03,
287 0x79, 0x6d, 0x6a
Mengjin Yanaec70742014-08-25 10:37:45 -0700288 };
289
290 Block errorBlock4(error4, sizeof(error4));
291 BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock4), ChatroomInfo::Error);
292
293 const uint8_t error5[] = {
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700294 0x80, 0x5d, // ChatroomInfo
295 0x81, 0x06, // ChatroomName
296 0x08, 0x04,
297 0x06e, 0x64, 0x6e, 0x64,
Mengjin Yanaec70742014-08-25 10:37:45 -0700298 0x82, 0x01, // TrustModel
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700299 0x01,
300 0x83, 0x12, // ChatroomPrefix
301 0x07, 0x10,
302 0x08, 0x03,
303 0x6e, 0x64, 0x6e,
304 0x08, 0x09,
305 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
306 0x84, 0x14, // ManagerPrefix
307 0x07, 0x12,
308 0x08, 0x03,
309 0x6e, 0x64, 0x6e,
310 0x08, 0x04,
311 0x75, 0x63, 0x6c, 0x61,
312 0x08, 0x05,
313 0x61, 0x6c, 0x69, 0x63, 0x65,
314 0x80, 0x26,// Participants Error Type
315 0x07, 0x12,
316 0x08, 0x03,
317 0x6e, 0x64, 0x6e,
318 0x08, 0x04,
319 0x75, 0x63, 0x6c, 0x61,
320 0x08, 0x05,
321 0x61, 0x6c, 0x69, 0x63, 0x65,
322 0x07, 0x10,
323 0x08, 0x03,
324 0x6e, 0x64, 0x6e,
325 0x08, 0x04,
326 0x75, 0x63, 0x6c, 0x61,
327 0x08, 0x03,
328 0x79, 0x6d, 0x6a
Mengjin Yanaec70742014-08-25 10:37:45 -0700329 };
330
331 Block errorBlock5(error5, sizeof(error5));
332 BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock5), ChatroomInfo::Error);
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700333
334 const uint8_t error6[] = {
335 0x80, 0x00 // Empty ChatroomInfo
336 };
337
338 Block errorBlock6(error6, sizeof(error6));
339 BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock6), ChatroomInfo::Error);
340
341 const uint8_t error7[] = {
342 0x80, 0x1f, // ChatroomInfo
343 0x81, 0x06, // ChatroomName
344 0x08, 0x04,
345 0x06e, 0x64, 0x6e, 0x64,
346 0x82, 0x01, // TrustModel
347 0x01,
348 0x83, 0x12, // ChatroomPrefix
349 0x07, 0x10,
350 0x08, 0x03,
351 0x6e, 0x64, 0x6e,
352 0x08, 0x09,
353 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
354 // no Participant
355 };
356
357 Block errorBlock7(error7, sizeof(error7));
358 BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock7), ChatroomInfo::Error);
Mengjin Yanaec70742014-08-25 10:37:45 -0700359}
360
361BOOST_AUTO_TEST_SUITE_END()
362
Qiuhan Ding52f13832015-03-06 14:05:59 -0800363} // namespace tests
Mengjin Yanaec70742014-08-25 10:37:45 -0700364
Qiuhan Ding52f13832015-03-06 14:05:59 -0800365} // namespace chronochat