blob: 39a50061cc5ee41bff269d5c7a0b7d7d3300ef2c [file] [log] [blame]
Mengjin Yanaec70742014-08-25 10:37:45 -07001#include "chatroom-info.hpp"
2#include <boost/test/unit_test.hpp>
3#include <ndn-cxx/encoding/block.hpp>
4
5namespace chronos {
6
7namespace test {
8
9using std::string;
10
11BOOST_AUTO_TEST_SUITE(TestChatroomInfo)
12
13const uint8_t chatroomInfo[] = {
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070014 0x80, 0x5d, // ChatroomInfo
15 0x81, 0x06, // ChatroomName
16 0x08, 0x04,
17 0x06e, 0x64, 0x6e, 0x64,
Mengjin Yanaec70742014-08-25 10:37:45 -070018 0x82, 0x01, // TrustModel
19 0x01,
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070020 0x83, 0x12, // ChatroomPrefix
21 0x07, 0x10,
22 0x08, 0x03,
23 0x6e, 0x64, 0x6e,
24 0x08, 0x09,
25 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
26 0x84, 0x14, // ManagerPrefix
Mengjin Yanaec70742014-08-25 10:37:45 -070027 0x07, 0x12,
28 0x08, 0x03,
29 0x6e, 0x64, 0x6e,
30 0x08, 0x04,
31 0x75, 0x63, 0x6c, 0x61,
32 0x08, 0x05,
33 0x61, 0x6c, 0x69, 0x63, 0x65,
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070034 0x85, 0x26,// Participants
35 0x07, 0x12,
36 0x08, 0x03,
37 0x6e, 0x64, 0x6e,
38 0x08, 0x04,
39 0x75, 0x63, 0x6c, 0x61,
40 0x08, 0x05,
41 0x61, 0x6c, 0x69, 0x63, 0x65,
Mengjin Yanaec70742014-08-25 10:37:45 -070042 0x07, 0x10,
43 0x08, 0x03,
44 0x6e, 0x64, 0x6e,
45 0x08, 0x04,
46 0x75, 0x63, 0x6c, 0x61,
47 0x08, 0x03,
48 0x79, 0x6d, 0x6a
49};
50
51BOOST_AUTO_TEST_CASE(EncodeChatroom)
52{
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070053
54 // ChatroomInfo := CHATROOM-INFO-TYPE TLV-LENGTH
55 // ChatroomName
56 // TrustModel
57 // ChatroomPrefix
58 // ManagerPrefix
59 // Participants
60 //
61 // ChatroomName := CHATROOM-NAME-TYPE TLV-LENGTH
62 // NameComponent
63 //
64 // TrustModel := TRUST-MODEL-TYPE TLV-LENGTH
65 // nonNegativeInteger
66 //
67 // ChatroomPrefix := CHATROOM-PREFIX-TYPE TLV-LENGTH
68 // Name
69 //
70 // ManagerPrefix := MANAGER-PREFIX-TYPE TLV-LENGTH
71 // Name
72 //
73 // Participants := PARTICIPANTS-TYPE TLV-LENGTH
74 // Name+
Mengjin Yanaec70742014-08-25 10:37:45 -070075
76 ChatroomInfo chatroom;
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070077 chatroom.setName(ndn::Name::Component("ndnd"));
78 chatroom.setManager("/ndn/ucla/alice");
79 chatroom.setSyncPrefix("/ndn/broadcast");
Mengjin Yanaec70742014-08-25 10:37:45 -070080 chatroom.addParticipant(Name("/ndn/ucla/alice"));
81 chatroom.addParticipant(Name("/ndn/ucla/ymj"));
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070082 chatroom.addParticipant(Name("/ndn/ucla"));
83 chatroom.removeParticipant(Name("/ndn/ucla"));
Mengjin Yanaec70742014-08-25 10:37:45 -070084 chatroom.setTrustModel(ChatroomInfo::TRUST_MODEL_WEBOFTRUST);
85
86 const Block& encoded = chatroom.wireEncode();
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070087
Mengjin Yanaec70742014-08-25 10:37:45 -070088 Block chatroomInfoBlock(chatroomInfo, sizeof(chatroomInfo));
89
90 BOOST_CHECK_EQUAL_COLLECTIONS(chatroomInfoBlock.wire(),
91 chatroomInfoBlock.wire() + chatroomInfoBlock.size(),
92 encoded.wire(),
93 encoded.wire() + encoded.size());
94}
95
96BOOST_AUTO_TEST_CASE(DecodeChatroomCorrect)
97{
98 ChatroomInfo chatroom;
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070099 chatroom.setName(ndn::Name::Component("ndnd"));
100 chatroom.setManager("/ndn/ucla/alice");
101 chatroom.setSyncPrefix("/ndn/broadcast");
Mengjin Yanaec70742014-08-25 10:37:45 -0700102 chatroom.addParticipant(Name("/ndn/ucla/alice"));
103 chatroom.addParticipant(Name("/ndn/ucla/ymj"));
104 chatroom.setTrustModel(ChatroomInfo::TRUST_MODEL_WEBOFTRUST);
105
106 Block chatroomInfoBlock(chatroomInfo, sizeof(chatroomInfo));
107 ChatroomInfo dechatroom;
108 dechatroom.wireDecode(chatroomInfoBlock);
Mengjin Yanaec70742014-08-25 10:37:45 -0700109
110 BOOST_CHECK_EQUAL(chatroom.getName(), dechatroom.getName());
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700111 BOOST_CHECK_EQUAL(chatroom.getSyncPrefix().toUri(), dechatroom.getSyncPrefix().toUri());
112 BOOST_CHECK_EQUAL(chatroom.getManagerPrefix().toUri(), dechatroom.getManagerPrefix().toUri());
Mengjin Yanaec70742014-08-25 10:37:45 -0700113 BOOST_CHECK_EQUAL(chatroom.getParticipants().size(), dechatroom.getParticipants().size());
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700114 BOOST_CHECK_EQUAL(chatroom.getParticipants().begin()->toUri(),
115 dechatroom.getParticipants().begin()->toUri());
116 BOOST_CHECK_EQUAL(chatroom.getParticipants().begin()->toUri(),
117 dechatroom.getParticipants().begin()->toUri());
Mengjin Yanaec70742014-08-25 10:37:45 -0700118}
119
120BOOST_AUTO_TEST_CASE(DecodeChatroomError)
121{
122 const uint8_t error1[] = {
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700123 0x81, 0x5d, // ChatroomInfo Type Error
124 0x81, 0x06, // ChatroomName
125 0x08, 0x04,
126 0x06e, 0x64, 0x6e, 0x64,
Mengjin Yanaec70742014-08-25 10:37:45 -0700127 0x82, 0x01, // TrustModel
128 0x01,
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700129 0x83, 0x12, // ChatroomPrefix
130 0x07, 0x10,
131 0x08, 0x03,
132 0x6e, 0x64, 0x6e,
133 0x08, 0x09,
134 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
135 0x84, 0x14, // ManagerPrefix
Mengjin Yanaec70742014-08-25 10:37:45 -0700136 0x07, 0x12,
137 0x08, 0x03,
138 0x6e, 0x64, 0x6e,
139 0x08, 0x04,
140 0x75, 0x63, 0x6c, 0x61,
141 0x08, 0x05,
142 0x61, 0x6c, 0x69, 0x63, 0x65,
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700143 0x85, 0x26,// Participants
144 0x07, 0x12,
145 0x08, 0x03,
146 0x6e, 0x64, 0x6e,
147 0x08, 0x04,
148 0x75, 0x63, 0x6c, 0x61,
149 0x08, 0x05,
150 0x61, 0x6c, 0x69, 0x63, 0x65,
Mengjin Yanaec70742014-08-25 10:37:45 -0700151 0x07, 0x10,
152 0x08, 0x03,
153 0x6e, 0x64, 0x6e,
154 0x08, 0x04,
155 0x75, 0x63, 0x6c, 0x61,
156 0x08, 0x03,
157 0x79, 0x6d, 0x6a
158 };
159
160 Block errorBlock1(error1, sizeof(error1));
161 BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock1), ChatroomInfo::Error);
162
163 const uint8_t error2[] = {
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700164 0x80, 0x5d, // ChatroomInfo
165 0x81, 0x06, // ChatroomName
166 0x08, 0x04,
167 0x06e, 0x64, 0x6e, 0x64,
168 0x83, 0x01, // TrustModel Type Error
Mengjin Yanaec70742014-08-25 10:37:45 -0700169 0x01,
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700170 0x83, 0x12, // ChatroomPrefix
171 0x07, 0x10,
172 0x08, 0x03,
173 0x6e, 0x64, 0x6e,
174 0x08, 0x09,
175 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
176 0x84, 0x14, // ManagerPrefix
Mengjin Yanaec70742014-08-25 10:37:45 -0700177 0x07, 0x12,
178 0x08, 0x03,
179 0x6e, 0x64, 0x6e,
180 0x08, 0x04,
181 0x75, 0x63, 0x6c, 0x61,
182 0x08, 0x05,
183 0x61, 0x6c, 0x69, 0x63, 0x65,
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700184 0x85, 0x26,// Participants
185 0x07, 0x12,
186 0x08, 0x03,
187 0x6e, 0x64, 0x6e,
188 0x08, 0x04,
189 0x75, 0x63, 0x6c, 0x61,
190 0x08, 0x05,
191 0x61, 0x6c, 0x69, 0x63, 0x65,
Mengjin Yanaec70742014-08-25 10:37:45 -0700192 0x07, 0x10,
193 0x08, 0x03,
194 0x6e, 0x64, 0x6e,
195 0x08, 0x04,
196 0x75, 0x63, 0x6c, 0x61,
197 0x08, 0x03,
198 0x79, 0x6d, 0x6a
199 };
200
201 Block errorBlock2(error2, sizeof(error2));
202 BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock2), ChatroomInfo::Error);
203
204 const uint8_t error3[] = {
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700205 0x80, 0x5d, // ChatroomInfo
206 0x81, 0x06, // ChatroomName
207 0x08, 0x04,
208 0x06e, 0x64, 0x6e, 0x64,
Mengjin Yanaec70742014-08-25 10:37:45 -0700209 0x82, 0x01, // TrustModel
210 0x01,
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700211 0x80, 0x12, // ChatroomPrefix Type Error
212 0x07, 0x10,
213 0x08, 0x03,
214 0x6e, 0x64, 0x6e,
215 0x08, 0x09,
216 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
217 0x84, 0x14, // ManagerPrefix
Mengjin Yanaec70742014-08-25 10:37:45 -0700218 0x07, 0x12,
219 0x08, 0x03,
220 0x6e, 0x64, 0x6e,
221 0x08, 0x04,
222 0x75, 0x63, 0x6c, 0x61,
223 0x08, 0x05,
224 0x61, 0x6c, 0x69, 0x63, 0x65,
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700225 0x85, 0x26,// Participants
226 0x07, 0x12,
227 0x08, 0x03,
228 0x6e, 0x64, 0x6e,
229 0x08, 0x04,
230 0x75, 0x63, 0x6c, 0x61,
231 0x08, 0x05,
232 0x61, 0x6c, 0x69, 0x63, 0x65,
Mengjin Yanaec70742014-08-25 10:37:45 -0700233 0x07, 0x10,
234 0x08, 0x03,
235 0x6e, 0x64, 0x6e,
236 0x08, 0x04,
237 0x75, 0x63, 0x6c, 0x61,
238 0x08, 0x03,
239 0x79, 0x6d, 0x6a
240 };
241
242 Block errorBlock3(error3, sizeof(error3));
243 BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock3), ChatroomInfo::Error);
244
245 const uint8_t error4[] = {
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700246 0x80, 0x5d, // ChatroomInfo
247 0x81, 0x06, // ChatroomName
248 0x08, 0x04,
249 0x06e, 0x64, 0x6e, 0x64,
250 0x82, 0x01, // TrustModel
251 0x01,
252 0x83, 0x12, // ChatroomPrefix
253 0x07, 0x10,
254 0x08, 0x03,
255 0x6e, 0x64, 0x6e,
256 0x08, 0x09,
257 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
258 0x80, 0x14, // ManagerPrefix Error Type
259 0x07, 0x12,
260 0x08, 0x03,
261 0x6e, 0x64, 0x6e,
262 0x08, 0x04,
263 0x75, 0x63, 0x6c, 0x61,
264 0x08, 0x05,
265 0x61, 0x6c, 0x69, 0x63, 0x65,
266 0x85, 0x26,// Participants
267 0x07, 0x12,
268 0x08, 0x03,
269 0x6e, 0x64, 0x6e,
270 0x08, 0x04,
271 0x75, 0x63, 0x6c, 0x61,
272 0x08, 0x05,
273 0x61, 0x6c, 0x69, 0x63, 0x65,
274 0x07, 0x10,
275 0x08, 0x03,
276 0x6e, 0x64, 0x6e,
277 0x08, 0x04,
278 0x75, 0x63, 0x6c, 0x61,
279 0x08, 0x03,
280 0x79, 0x6d, 0x6a
Mengjin Yanaec70742014-08-25 10:37:45 -0700281 };
282
283 Block errorBlock4(error4, sizeof(error4));
284 BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock4), ChatroomInfo::Error);
285
286 const uint8_t error5[] = {
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700287 0x80, 0x5d, // ChatroomInfo
288 0x81, 0x06, // ChatroomName
289 0x08, 0x04,
290 0x06e, 0x64, 0x6e, 0x64,
Mengjin Yanaec70742014-08-25 10:37:45 -0700291 0x82, 0x01, // TrustModel
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700292 0x01,
293 0x83, 0x12, // ChatroomPrefix
294 0x07, 0x10,
295 0x08, 0x03,
296 0x6e, 0x64, 0x6e,
297 0x08, 0x09,
298 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
299 0x84, 0x14, // ManagerPrefix
300 0x07, 0x12,
301 0x08, 0x03,
302 0x6e, 0x64, 0x6e,
303 0x08, 0x04,
304 0x75, 0x63, 0x6c, 0x61,
305 0x08, 0x05,
306 0x61, 0x6c, 0x69, 0x63, 0x65,
307 0x80, 0x26,// Participants Error Type
308 0x07, 0x12,
309 0x08, 0x03,
310 0x6e, 0x64, 0x6e,
311 0x08, 0x04,
312 0x75, 0x63, 0x6c, 0x61,
313 0x08, 0x05,
314 0x61, 0x6c, 0x69, 0x63, 0x65,
315 0x07, 0x10,
316 0x08, 0x03,
317 0x6e, 0x64, 0x6e,
318 0x08, 0x04,
319 0x75, 0x63, 0x6c, 0x61,
320 0x08, 0x03,
321 0x79, 0x6d, 0x6a
Mengjin Yanaec70742014-08-25 10:37:45 -0700322 };
323
324 Block errorBlock5(error5, sizeof(error5));
325 BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock5), ChatroomInfo::Error);
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700326
327 const uint8_t error6[] = {
328 0x80, 0x00 // Empty ChatroomInfo
329 };
330
331 Block errorBlock6(error6, sizeof(error6));
332 BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock6), ChatroomInfo::Error);
333
334 const uint8_t error7[] = {
335 0x80, 0x1f, // ChatroomInfo
336 0x81, 0x06, // ChatroomName
337 0x08, 0x04,
338 0x06e, 0x64, 0x6e, 0x64,
339 0x82, 0x01, // TrustModel
340 0x01,
341 0x83, 0x12, // ChatroomPrefix
342 0x07, 0x10,
343 0x08, 0x03,
344 0x6e, 0x64, 0x6e,
345 0x08, 0x09,
346 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
347 // no Participant
348 };
349
350 Block errorBlock7(error7, sizeof(error7));
351 BOOST_CHECK_THROW(ChatroomInfo chatroom(errorBlock7), ChatroomInfo::Error);
Mengjin Yanaec70742014-08-25 10:37:45 -0700352}
353
354BOOST_AUTO_TEST_SUITE_END()
355
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700356} // namespace test
Mengjin Yanaec70742014-08-25 10:37:45 -0700357
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700358} // namespace chronos