Line data Source code
1 : /*
2 : COLLECTION LIBRARY
3 :
4 : Collection unit test.
5 :
6 : Copyright (C) Dmitri Pal <dpal@redhat.com> 2009
7 :
8 : Collection Library is free software: you can redistribute it and/or modify
9 : it under the terms of the GNU Lesser General Public License as published by
10 : the Free Software Foundation, either version 3 of the License, or
11 : (at your option) any later version.
12 :
13 : Collection Library is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : GNU Lesser General Public License for more details.
17 :
18 : You should have received a copy of the GNU Lesser General Public License
19 : along with Collection Library. If not, see <http://www.gnu.org/licenses/>.
20 : */
21 :
22 : #include "config.h"
23 : #include <stdio.h>
24 : #include <string.h>
25 : #include <errno.h>
26 : #define TRACE_HOME
27 : #include "trace.h"
28 : #include "collection.h"
29 : #include "collection_tools.h"
30 :
31 : typedef int (*test_fn)(void);
32 :
33 : int verbose = 0;
34 :
35 : #define COLOUT(foo) \
36 : do { \
37 : if (verbose) foo; \
38 : } while(0)
39 :
40 :
41 :
42 1 : static int ref_collection_test(void)
43 : {
44 1 : struct collection_item *peer = NULL;
45 1 : struct collection_item *socket = NULL;
46 1 : struct collection_item *socket2 = NULL;
47 1 : char binary_dump[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
48 :
49 1 : int error = EOK;
50 :
51 : TRACE_FLOW_STRING("ref_collection_test", "Entry.");
52 :
53 1 : COLOUT(printf("\n\nREF TEST!!!.\n\n\n"));
54 1 : COLOUT(printf("Creating PEER collection.\n"));
55 :
56 2 : if ((error = col_create_collection(&peer, "peer", 0)) ||
57 2 : (error = col_add_str_property(peer, NULL, "hostname", "peerhost.mytest.com", 0)) ||
58 : /* Expect trailing zero to be truncated */
59 2 : (error = col_add_str_property(peer, NULL, "IPv4", "10.10.10.10", 12)) ||
60 1 : (error = col_add_str_property(peer, NULL, "IPv6", "bla:bla:bla:bla:bla:bla", 0))) {
61 0 : printf("Failed to add property. Error %d\n", error);
62 0 : col_destroy_collection(peer);
63 0 : return error;
64 : }
65 :
66 1 : COLOUT(printf("Creating SOCKET collection.\n"));
67 :
68 2 : if ((error = col_create_collection(&socket, "socket", 0)) ||
69 2 : (error = col_add_int_property(socket, NULL, "id", 1)) ||
70 2 : (error = col_add_long_property(socket, NULL, "packets", 100000000L)) ||
71 1 : (error = col_add_binary_property(socket, NULL, "stack", binary_dump, sizeof(binary_dump)))) {
72 0 : col_destroy_collection(peer);
73 0 : col_destroy_collection(socket);
74 0 : printf("Failed to add property. Error %d\n", error);
75 0 : return error;
76 : }
77 :
78 1 : COLOUT(col_debug_collection(socket, COL_TRAVERSE_DEFAULT));
79 1 : COLOUT(col_debug_collection(peer, COL_TRAVERSE_DEFAULT));
80 :
81 1 : COLOUT(printf("Adding PEER collection to SOCKET collection as a reference named PEER\n"));
82 :
83 : /* Embed peer host into the socket2 as reference */
84 1 : error = col_add_collection_to_collection(socket, NULL, "peer", peer, COL_ADD_MODE_REFERENCE);
85 1 : if (error) {
86 0 : col_destroy_collection(peer);
87 0 : col_destroy_collection(socket);
88 0 : printf("Failed to add collection to collection. Error %d\n", error);
89 0 : return error;
90 : }
91 :
92 1 : COLOUT(col_debug_collection(socket, COL_TRAVERSE_DEFAULT));
93 1 : COLOUT(col_debug_collection(peer, COL_TRAVERSE_DEFAULT));
94 :
95 1 : COLOUT(printf("About to destroy PEER\n"));
96 1 : col_destroy_collection(peer);
97 1 : COLOUT(col_debug_collection(socket, COL_TRAVERSE_DEFAULT));
98 :
99 1 : COLOUT(printf("About to extract PEER\n"));
100 1 : error = col_get_collection_reference(socket, &peer, "peer");
101 1 : if (error) {
102 0 : col_destroy_collection(socket);
103 0 : printf("Failed to extract collection. Error %d\n", error);
104 0 : return error;
105 : }
106 :
107 1 : COLOUT(col_debug_collection(socket, COL_TRAVERSE_DEFAULT));
108 1 : COLOUT(col_debug_collection(peer, COL_TRAVERSE_DEFAULT));
109 1 : col_destroy_collection(peer);
110 :
111 1 : COLOUT(col_debug_collection(socket, COL_TRAVERSE_DEFAULT));
112 :
113 1 : error = col_get_collection_reference(socket, &socket2, NULL);
114 1 : if (error) {
115 0 : col_destroy_collection(socket);
116 0 : printf("Failed to extract collection. Error %d\n", error);
117 0 : return error;
118 : }
119 :
120 1 : COLOUT(col_debug_collection(socket2, COL_TRAVERSE_DEFAULT));
121 1 : col_destroy_collection(socket);
122 1 : COLOUT(col_debug_collection(socket2, COL_TRAVERSE_DEFAULT));
123 1 : col_destroy_collection(socket2);
124 :
125 : TRACE_FLOW_NUMBER("ref_collection_test. Returning", error);
126 :
127 1 : COLOUT(printf("\n\nEND OF REF TEST!!!.\n\n\n"));
128 :
129 1 : return error;
130 :
131 : }
132 :
133 :
134 1 : static int single_collection_test(void)
135 : {
136 1 : struct collection_item *handle = NULL;
137 1 : int error = EOK;
138 :
139 : TRACE_FLOW_STRING("single_collection_test", "Entry.");
140 :
141 2 : if ((error = col_create_collection(&handle, "string_test", 0)) ||
142 2 : (error = col_add_str_property(handle, NULL, "property_1", "some data", 0)) ||
143 2 : (error = col_add_str_property(handle, NULL, "property_2", "some other data", 2)) ||
144 1 : (error = col_add_str_property(handle, NULL, "property_3", "more data", 7))) {
145 0 : printf("Failed to add property. Error %d\n", error);
146 0 : col_destroy_collection(handle);
147 0 : return error;
148 : }
149 :
150 1 : error = col_add_str_property(handle, NULL, "property 1!", "some data", 0);
151 1 : if (error) {
152 1 : COLOUT(printf("Expected error adding bad property to collection %d\n", error));
153 : }
154 : else {
155 0 : printf("Expected error but got success\n");
156 0 : return -1;
157 : }
158 :
159 1 : error = col_add_double_property(handle, NULL, "double", 0.253545);
160 1 : if (error) {
161 0 : printf("Failed to add double property. Error %d\n", error);
162 0 : col_destroy_collection(handle);
163 0 : return error;
164 : }
165 :
166 1 : error = col_update_double_property(handle, "double", COL_TRAVERSE_DEFAULT, 1.999999);
167 1 : if (error) {
168 0 : printf("Failed to update double property. Error %d\n", error);
169 0 : col_destroy_collection(handle);
170 0 : return error;
171 : }
172 :
173 1 : COLOUT(printf("Created collection\n"));
174 :
175 : /* Traverse collection */
176 1 : if (verbose) {
177 0 : error = col_debug_collection(handle, COL_TRAVERSE_DEFAULT);
178 0 : if (error) {
179 0 : printf("Error debugging collection %d\n", error);
180 0 : return error;
181 : }
182 0 : error = col_print_collection(handle);
183 0 : if (error) {
184 0 : printf("Error printing collection %d\n", error);
185 0 : return error;
186 : }
187 : }
188 :
189 1 : col_destroy_collection(handle);
190 :
191 : TRACE_FLOW_NUMBER("single_collection_test. Error: ", error);
192 1 : return error;
193 : }
194 :
195 1 : static int add_collection_test(void)
196 : {
197 1 : struct collection_item *peer = NULL;
198 1 : struct collection_item *socket = NULL;
199 1 : char binary_dump[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
200 :
201 1 : int error = EOK;
202 :
203 : TRACE_FLOW_STRING("add_collection_test", "Entry.");
204 :
205 1 : COLOUT(printf("\n\nADD TEST!!!.\n\n\n"));
206 1 : COLOUT(printf("Creating PEER collection.\n"));
207 :
208 2 : if ((error = col_create_collection(&peer, "peer", 0)) ||
209 2 : (error = col_add_str_property(peer, NULL, "hostname", "peerhost.mytest.com", 0)) ||
210 : /* Expect trailing zero to be truncated */
211 2 : (error = col_add_str_property(peer, NULL, "IPv4", "10.10.10.10", 12)) ||
212 1 : (error = col_add_str_property(peer, NULL, "IPv6", "bla:bla:bla:bla:bla:bla", 0))) {
213 0 : printf("Failed to add property. Error %d", error);
214 0 : col_destroy_collection(peer);
215 0 : return error;
216 : }
217 :
218 1 : COLOUT(printf("Creating SOCKET collection.\n"));
219 :
220 2 : if ((error = col_create_collection(&socket, "socket", 0)) ||
221 2 : (error = col_add_int_property(socket, NULL, "id", 1)) ||
222 2 : (error = col_add_long_property(socket, NULL, "packets", 100000000L)) ||
223 1 : (error = col_add_binary_property(socket, NULL, "stack", binary_dump, sizeof(binary_dump)))) {
224 0 : col_destroy_collection(peer);
225 0 : col_destroy_collection(socket);
226 0 : printf("Failed to add property. Error %d\n", error);
227 0 : return error;
228 : }
229 :
230 1 : COLOUT(col_debug_collection(socket, COL_TRAVERSE_DEFAULT));
231 1 : COLOUT(col_debug_collection(peer, COL_TRAVERSE_DEFAULT));
232 :
233 1 : COLOUT(printf("Adding PEER collection to SOCKET collection as a reference named PEER\n"));
234 :
235 : /* Embed peer host into the socket2 as reference */
236 1 : error = col_add_collection_to_collection(socket, NULL, "peer", peer, COL_ADD_MODE_REFERENCE);
237 1 : if (error) {
238 0 : col_destroy_collection(peer);
239 0 : col_destroy_collection(socket);
240 0 : printf("Failed to create collection. Error %d\n", error);
241 0 : return error;
242 : }
243 :
244 1 : COLOUT(col_debug_collection(socket, COL_TRAVERSE_DEFAULT));
245 1 : COLOUT(col_debug_collection(peer, COL_TRAVERSE_DEFAULT));
246 1 : col_destroy_collection(peer);
247 1 : COLOUT(col_debug_collection(socket, COL_TRAVERSE_DEFAULT));
248 1 : col_destroy_collection(socket);
249 : TRACE_FLOW_NUMBER("add_collection_test. Returning", error);
250 1 : return error;
251 : }
252 :
253 3 : static int copy_cb(struct collection_item *item,
254 : void *ext_data,
255 : int *skip)
256 : {
257 3 : COLOUT(printf("INSIDE Copy Callback\n"));
258 3 : COLOUT(col_debug_item(item));
259 3 : COLOUT(printf("Passed in data: %s\n", (char *) ext_data));
260 3 : if (strcmp(col_get_item_property(item, NULL), "id") == 0) *skip = 1;
261 3 : return EOK;
262 : }
263 :
264 :
265 1 : static int mixed_collection_test(void)
266 : {
267 : struct collection_item *peer;
268 : struct collection_item *socket1;
269 : struct collection_item *socket2;
270 : struct collection_item *socket3;
271 : struct collection_item *event;
272 : struct collection_item *host;
273 1 : char binary_dump[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
274 1 : int found = 0;
275 1 : unsigned int class = 0;
276 1 : char foo[] = "foo";
277 :
278 1 : int error = EOK;
279 :
280 : TRACE_FLOW_STRING("mixed_collection_test", "Entry.");
281 :
282 1 : COLOUT(printf("\n\nMIXED TEST!!!.\n\n\n"));
283 1 : COLOUT(printf("Creating PEER collection.\n"));
284 :
285 2 : if ((error = col_create_collection(&peer, "peer", 0)) ||
286 2 : (error = col_add_str_property(peer, NULL, "hostname", "peerhost.mytest.com", 0)) ||
287 : /* Expect trailing zero to be truncated */
288 2 : (error = col_add_str_property(peer, NULL, "IPv4", "10.10.10.10", 12)) ||
289 1 : (error = col_add_str_property(peer, NULL, "IPv6", "bla:bla:bla:bla:bla:bla", 0))) {
290 0 : printf("Failed to add property. Error %d", error);
291 0 : col_destroy_collection(peer);
292 0 : return error;
293 : }
294 :
295 1 : COLOUT(col_debug_collection(peer, COL_TRAVERSE_DEFAULT));
296 :
297 1 : COLOUT(printf("Creating HOST collection.\n"));
298 :
299 2 : if ((error = col_create_collection(&host, "host", 0)) ||
300 2 : (error = col_add_str_property(host, NULL, "hostname", "myhost.mytest.com", 0)) ||
301 2 : (error = col_add_str_property(host, NULL, "IPv4", "20.20.20.20", 13)) ||
302 2 : (error = col_add_str_property(host, NULL, "IPv6", "bla:bla:bla:bla:bla:bla", 0)) ||
303 1 : (error = col_add_double_property(host, NULL, "double", 0.253545))) {
304 0 : printf("Failed to add property. Error %d", error);
305 0 : col_destroy_collection(peer);
306 0 : col_destroy_collection(host);
307 0 : return error;
308 : }
309 :
310 1 : COLOUT(col_debug_collection(host, COL_TRAVERSE_DEFAULT));
311 :
312 1 : COLOUT(printf("Creating SOCKET1 collection.\n"));
313 :
314 2 : if ((error = col_create_collection(&socket1, "socket1", 0)) ||
315 2 : (error = col_add_int_property(socket1, NULL, "id", 1)) ||
316 2 : (error = col_add_long_property(socket1, NULL, "packets", 100000000L)) ||
317 1 : (error = col_add_binary_property(socket1, NULL, "stack", binary_dump, sizeof(binary_dump)))) {
318 0 : col_destroy_collection(peer);
319 0 : col_destroy_collection(host);
320 0 : col_destroy_collection(socket1);
321 0 : printf("Failed to add property. Error %d\n", error);
322 0 : return error;
323 : }
324 :
325 1 : COLOUT(col_debug_collection(socket1, COL_TRAVERSE_DEFAULT));
326 1 : COLOUT(printf("Creating a copy of SOCKET1 collection named SOCKET2.\n"));
327 :
328 1 : error = col_copy_collection(&socket2, socket1, "socket2", COL_COPY_NORMAL);
329 1 : if (error) {
330 0 : col_destroy_collection(peer);
331 0 : col_destroy_collection(host);
332 0 : col_destroy_collection(socket1);
333 0 : printf("Failed to copy collection. Error %d\n", error);
334 0 : return error;
335 : }
336 :
337 1 : COLOUT(col_debug_collection(socket2, COL_TRAVERSE_DEFAULT));
338 1 : COLOUT(col_debug_collection(peer, COL_TRAVERSE_DEFAULT));
339 :
340 1 : error = col_copy_collection_with_cb(&socket3, socket1, "socket3",
341 : COL_COPY_FLATDOT, copy_cb, (void *)foo);
342 1 : if (error) {
343 0 : col_destroy_collection(peer);
344 0 : col_destroy_collection(host);
345 0 : col_destroy_collection(socket1);
346 0 : col_destroy_collection(socket2);
347 0 : printf("Failed to copy collection. Error %d\n", error);
348 0 : return error;
349 : }
350 :
351 1 : COLOUT(col_debug_collection(socket3, COL_TRAVERSE_DEFAULT));
352 1 : col_destroy_collection(socket3);
353 :
354 1 : COLOUT(printf("Adding PEER collection to SOCKET2 collection as a reference named PEER2\n"));
355 :
356 : /* Embed peer host into the socket2 as reference */
357 1 : error = col_add_collection_to_collection(socket2, NULL, "peer2", peer, COL_ADD_MODE_REFERENCE);
358 1 : if (error) {
359 0 : col_destroy_collection(peer);
360 0 : col_destroy_collection(host);
361 0 : col_destroy_collection(socket1);
362 0 : col_destroy_collection(socket2);
363 0 : printf("Failed to create collection. Error %d\n", error);
364 0 : return error;
365 : }
366 :
367 1 : COLOUT(col_debug_collection(socket2, COL_TRAVERSE_DEFAULT));
368 :
369 1 : COLOUT(printf("Creating an EVENT collection.\n"));
370 :
371 : /* Construct event */
372 1 : error = col_create_collection(&event, "event", 0);
373 1 : if (error) {
374 0 : col_destroy_collection(peer);
375 0 : col_destroy_collection(host);
376 0 : col_destroy_collection(socket1);
377 0 : col_destroy_collection(socket2);
378 0 : printf("Failed to create collection. Error %d\n", error);
379 0 : return error;
380 : }
381 :
382 1 : COLOUT(col_debug_collection(event, COL_TRAVERSE_DEFAULT));
383 :
384 1 : COLOUT(printf("Adding HOST to EVENT.\n"));
385 :
386 : /* Add host to event */
387 1 : error = col_add_collection_to_collection(event, NULL, NULL, host, COL_ADD_MODE_REFERENCE);
388 1 : if (error) {
389 0 : col_destroy_collection(peer);
390 0 : col_destroy_collection(host);
391 0 : col_destroy_collection(socket1);
392 0 : col_destroy_collection(socket2);
393 0 : col_destroy_collection(event);
394 0 : printf("Failed to add collections. Error %d\n", error);
395 0 : return error;
396 : }
397 :
398 1 : COLOUT(col_debug_collection(event, COL_TRAVERSE_DEFAULT));
399 :
400 1 : COLOUT(printf("Embed SOCKET1 into EVENT.\n"));
401 : /* Donate socket1 to event */
402 : /* Socket1 should not be used after this */
403 1 : error = col_add_collection_to_collection(event, NULL, NULL, socket1, COL_ADD_MODE_EMBED);
404 1 : if (error) {
405 0 : col_destroy_collection(peer);
406 0 : col_destroy_collection(host);
407 0 : col_destroy_collection(socket1);
408 0 : col_destroy_collection(socket2);
409 0 : col_destroy_collection(event);
410 0 : printf("Failed to add collections. Error %d\n", error);
411 0 : return error;
412 : }
413 :
414 1 : COLOUT(printf("Traverse one level:\n"));
415 1 : COLOUT(col_debug_collection(event, COL_TRAVERSE_ONELEVEL));
416 1 : COLOUT(printf("Traverse ignore subcollections:\n"));
417 1 : COLOUT(col_debug_collection(event, COL_TRAVERSE_IGNORE));
418 1 : COLOUT(printf("Traverse normal:\n"));
419 1 : COLOUT(col_debug_collection(event, COL_TRAVERSE_DEFAULT));
420 1 : COLOUT(col_debug_collection(socket1, COL_TRAVERSE_DEFAULT));
421 :
422 1 : COLOUT(printf("SOCKET1 MUST NOT BE USED AFTER THIS POINT!!!\n"));
423 1 : socket1 = NULL;
424 :
425 1 : COLOUT(printf("Add collection PEER as PEER1 to subcollection SOCKET1 of the EVENT.\n"));
426 :
427 1 : COLOUT(col_debug_collection(peer, COL_TRAVERSE_DEFAULT));
428 :
429 1 : error = col_add_collection_to_collection(event, "socket1", "peer1", peer, COL_ADD_MODE_CLONE);
430 1 : if (error) {
431 0 : col_destroy_collection(peer);
432 0 : col_destroy_collection(host);
433 : /* No socket1 any more :) */
434 0 : col_destroy_collection(socket2);
435 0 : col_destroy_collection(event);
436 0 : printf("Failed to add collections. Error %d\n", error);
437 0 : return error;
438 : }
439 :
440 1 : COLOUT(col_debug_collection(event, COL_TRAVERSE_DEFAULT));
441 :
442 1 : COLOUT(printf("Add property named TIMEOUT to PEER collection.\n"));
443 :
444 : /* Add new property to the peer collection */
445 1 : error = col_add_int_property(peer, NULL, "timeout", 5);
446 1 : if (error) {
447 0 : col_destroy_collection(peer);
448 0 : col_destroy_collection(host);
449 : /* No socket1 any more :) */
450 0 : col_destroy_collection(socket2);
451 0 : col_destroy_collection(event);
452 0 : printf("Failed to add property. Error %d\n", error);
453 0 : return error;
454 : }
455 :
456 1 : COLOUT(col_debug_collection(socket2, COL_TRAVERSE_DEFAULT));
457 :
458 1 : COLOUT(printf("Add property named DELAY to PEER1 collection.\n"));
459 :
460 1 : error = col_add_int_property(event, "peer1", "delay", 10);
461 1 : if (error) {
462 0 : col_destroy_collection(peer);
463 0 : col_destroy_collection(host);
464 : /* No socket1 any more :) */
465 0 : col_destroy_collection(socket2);
466 0 : col_destroy_collection(event);
467 0 : printf("Failed to add property. Error %d\n", error);
468 0 : return error;
469 : }
470 :
471 1 : COLOUT(col_debug_collection(event, COL_TRAVERSE_DEFAULT));
472 1 : COLOUT(col_debug_collection(host, COL_TRAVERSE_DEFAULT));
473 :
474 1 : COLOUT(printf("Check if property PEER1.DELAY is in the EVENT collection.\n"));
475 :
476 : /* Check if the property in the collection */
477 1 : found = 0;
478 1 : error = col_is_item_in_collection(event, "peer1!delay", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found);
479 1 : if (error) {
480 0 : col_destroy_collection(peer);
481 0 : col_destroy_collection(host);
482 : /* No socket1 any more :) */
483 0 : col_destroy_collection(socket2);
484 0 : col_destroy_collection(event);
485 0 : printf("Failed to check property. Error %d\n", error);
486 0 : return error;
487 : }
488 :
489 1 : if (found == 1) {
490 1 : COLOUT(printf("Property is found!\n"));
491 : }
492 : else {
493 0 : COLOUT(printf("Error property is not found!\n"));
494 : }
495 :
496 :
497 1 : COLOUT(col_print_item(event, "peer1!IPv6"));
498 1 : COLOUT(col_print_item(event, "event!socket1!peer1!IPv6"));
499 1 : COLOUT(col_print_item(event, "event!peer1!IPv6"));
500 1 : COLOUT(col_print_item(event, "speer1!IPv6"));
501 1 : COLOUT(col_print_item(event, "eer1!IPv6"));
502 1 : COLOUT(col_print_item(event, "!peer1!IPv6"));
503 1 : COLOUT(col_print_item(event, "t!peer1!IPv6"));
504 :
505 : /* Traverse collection */
506 1 : if (verbose) {
507 0 : error = col_print_collection2(event);
508 0 : if (error) {
509 0 : col_destroy_collection(peer);
510 0 : col_destroy_collection(host);
511 : /* No socket1 any more :) */
512 0 : col_destroy_collection(socket2);
513 0 : col_destroy_collection(event);
514 0 : printf("Error printing collection %d\n", error);
515 0 : return error;
516 : }
517 : }
518 :
519 1 : COLOUT(printf("Delete property PEER1!DELAY from the EVENT collection.\n"));
520 :
521 1 : error = col_delete_property(event, "peer1!delay", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT);
522 1 : if (error) {
523 0 : col_destroy_collection(peer);
524 0 : col_destroy_collection(host);
525 : /* No socket1 any more :) */
526 0 : col_destroy_collection(socket2);
527 0 : col_destroy_collection(event);
528 0 : printf("Failed to delete property. Error %d\n", error);
529 0 : return error;
530 : }
531 :
532 1 : COLOUT(printf("Printing EVENT.\n"));
533 :
534 : /* Traverse collection */
535 1 : if (verbose) {
536 0 : error = col_print_collection2(event);
537 0 : if (error) {
538 0 : col_destroy_collection(peer);
539 0 : col_destroy_collection(host);
540 : /* No socket1 any more :) */
541 0 : col_destroy_collection(socket2);
542 0 : col_destroy_collection(event);
543 0 : printf("Error printing collection %d\n", error);
544 0 : return error;
545 : }
546 : }
547 :
548 1 : COLOUT(printf("Debugging EVENT.\n"));
549 1 : if (verbose) {
550 0 : error = col_debug_collection(event, COL_TRAVERSE_DEFAULT);
551 0 : if (error) {
552 0 : col_destroy_collection(peer);
553 0 : col_destroy_collection(host);
554 : /* No socket1 any more :) */
555 0 : col_destroy_collection(socket2);
556 0 : col_destroy_collection(event);
557 0 : printf("Error printing collection %d\n", error);
558 0 : return error;
559 : }
560 : }
561 1 : COLOUT(printf("Cleanup of the collections PEER, HOST and SOCKET2.\n"));
562 :
563 : /* Destroy a referenced collection */
564 1 : col_destroy_collection(peer);
565 1 : col_destroy_collection(host);
566 1 : col_destroy_collection(socket2);
567 :
568 1 : COLOUT(printf("Printing EVENT again.\n"));
569 :
570 : /* Traverse collection again - peer should still be there */
571 1 : if (verbose) {
572 0 : error = col_print_collection(event);
573 0 : if (error) {
574 0 : col_destroy_collection(event);
575 0 : printf("Error printing collection %d\n", error);
576 0 : return error;
577 : }
578 : }
579 :
580 1 : COLOUT(printf("Debugging EVENT again.\n"));
581 :
582 1 : if (verbose) {
583 0 : error = col_debug_collection(event, COL_TRAVERSE_DEFAULT);
584 0 : if (error) {
585 0 : col_destroy_collection(event);
586 0 : printf("Error printing collection %d\n", error);
587 0 : return error;
588 : }
589 : }
590 :
591 1 : COLOUT(printf("Attempt to add property to a referenced collection.\n"));
592 :
593 1 : error = col_add_int_property(event, "host", "session", 500);
594 1 : if (error) {
595 0 : col_destroy_collection(event);
596 0 : printf("Error was NOT able to add property to a referenced collection %d.\n", error);
597 0 : return error;
598 : }
599 :
600 1 : COLOUT(printf("Attempt to delete non-existent property.\n"));
601 :
602 : /* Can't delete non exitent property */
603 1 : error = col_delete_property(event, "host.host", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT);
604 1 : if (error == 0) {
605 0 : col_destroy_collection(event);
606 0 : printf("Error was able to delete property that does not exist.\n");
607 0 : return -1;
608 : }
609 1 : else COLOUT(printf("Expected error %d\n", error));
610 :
611 : /* Set collection class */
612 1 : error = col_set_collection_class(event, 2);
613 1 : if (error != 0) {
614 0 : col_destroy_collection(event);
615 0 : printf("Error was NOT able to set class.\n");
616 0 : return error;
617 : }
618 :
619 1 : error = col_get_collection_class(event, &class);
620 1 : if (error != 0) {
621 0 : col_destroy_collection(event);
622 0 : printf("Error was NOT able to get class.\n");
623 0 : return error;
624 : }
625 1 : else COLOUT(printf("Class = %d\n", class));
626 :
627 1 : if (col_is_of_class(event, 2)) {
628 1 : COLOUT(printf("Class mathced!\n"));
629 : }
630 : else {
631 0 : col_destroy_collection(event);
632 0 : printf("Error - bad class.\n");
633 0 : return -1;
634 : }
635 :
636 1 : COLOUT(printf("Done. Cleaning...\n"));
637 :
638 1 : col_destroy_collection(event);
639 :
640 1 : COLOUT(printf("Exit.\n"));
641 : TRACE_FLOW_NUMBER("add_collection_test. Returning", EOK);
642 1 : return EOK;
643 : }
644 :
645 :
646 1 : static int iterator_test(void)
647 : {
648 1 : struct collection_item *peer = NULL;
649 1 : struct collection_item *initial = NULL;
650 :
651 1 : struct collection_item *socket1 = NULL;
652 1 : struct collection_item *socket2 = NULL;
653 1 : struct collection_item *socket3 = NULL;
654 1 : struct collection_iterator *iterator = NULL;
655 1 : int error = EOK;
656 1 : struct collection_item *item = NULL;
657 1 : char binary_dump[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
658 1 : int depth = 0;
659 1 : int idepth = 0;
660 1 : int len = 0;
661 : int i;
662 : uint64_t hash1, hash2;
663 1 : int rwnd = 0;
664 :
665 1 : COLOUT(printf("\n\n==== ITERATOR TEST ====\n\n"));
666 :
667 1 : if ((error = col_create_collection(&initial, "strater", 0)) ||
668 1 : (error = col_create_collection(&peer, "peer", 0)) ||
669 2 : (error = col_add_str_property(initial, NULL, "hostname", "peerhost.mytest.com", 0)) ||
670 : /* Expect trailing zero to be truncated */
671 2 : (error = col_add_str_property(initial, NULL, "IPv4", "10.10.10.10", 12)) ||
672 2 : (error = col_add_str_property(initial, NULL, "IPv6", "bla:bla:bla:bla:bla:bla", 0)) ||
673 1 : (error = col_add_collection_to_collection(peer, NULL, NULL, initial, COL_ADD_MODE_FLAT))) {
674 0 : printf("Failed to add property. Error %d", error);
675 0 : col_destroy_collection(peer);
676 0 : col_destroy_collection(initial);
677 0 : return error;
678 : }
679 :
680 1 : col_destroy_collection(initial);
681 :
682 2 : if ((error = col_create_collection(&socket1, "socket", 0)) ||
683 2 : (error = col_add_int_property(socket1, NULL, "id", 1)) ||
684 2 : (error = col_add_long_property(socket1, NULL, "packets", 100000000L)) ||
685 1 : (error = col_add_binary_property(socket1, NULL, "stack", binary_dump, sizeof(binary_dump)))) {
686 0 : col_destroy_collection(peer);
687 0 : col_destroy_collection(socket1);
688 0 : printf("Failed to add property. Error %d\n", error);
689 0 : return error;
690 : }
691 :
692 2 : if ((error = col_create_collection(&socket2, "socket", 0)) ||
693 2 : (error = col_add_int_property(socket2, NULL, "id", 2)) ||
694 2 : (error = col_add_long_property(socket2, NULL, "packets", 200000000L)) ||
695 1 : (error = col_add_binary_property(socket2, NULL, "queue", binary_dump, sizeof(binary_dump)))) {
696 0 : col_destroy_collection(peer);
697 0 : col_destroy_collection(socket1);
698 0 : col_destroy_collection(socket2);
699 0 : printf("Failed to add property. Error %d\n", error);
700 0 : return error;
701 : }
702 :
703 1 : if ((error = col_create_collection(&socket3, "socket", 0))) {
704 0 : col_destroy_collection(peer);
705 0 : col_destroy_collection(socket1);
706 0 : col_destroy_collection(socket2);
707 0 : printf("Failed to add property. Error %d\n", error);
708 0 : return error;
709 : }
710 :
711 1 : error = col_add_collection_to_collection(peer, NULL, "first", socket1, COL_ADD_MODE_REFERENCE);
712 1 : if (error) {
713 0 : col_destroy_collection(peer);
714 0 : col_destroy_collection(socket1);
715 0 : col_destroy_collection(socket2);
716 0 : col_destroy_collection(socket3);
717 0 : printf("Failed to add collection to collection. Error %d\n", error);
718 0 : return error;
719 : }
720 :
721 1 : error = col_add_collection_to_collection(peer, NULL, "second", socket2, COL_ADD_MODE_EMBED);
722 1 : if (error) {
723 0 : col_destroy_collection(peer);
724 0 : col_destroy_collection(socket1);
725 0 : col_destroy_collection(socket2);
726 0 : col_destroy_collection(socket3);
727 0 : printf("Failed to add collection to collection. Error %d\n", error);
728 0 : return error;
729 : }
730 :
731 1 : error = col_add_collection_to_collection(peer, NULL, "third", socket3, COL_ADD_MODE_EMBED);
732 1 : if (error) {
733 0 : col_destroy_collection(peer);
734 0 : col_destroy_collection(socket1);
735 0 : col_destroy_collection(socket3);
736 0 : printf("Failed to add collection to collection. Error %d\n", error);
737 0 : return error;
738 : }
739 :
740 1 : error = col_add_collection_to_collection(peer, NULL, "forth", socket1, COL_ADD_MODE_FLATDOT);
741 1 : if (error) {
742 0 : col_destroy_collection(peer);
743 0 : col_destroy_collection(socket1);
744 0 : printf("Failed to add collection to collection. Error %d\n", error);
745 0 : return error;
746 : }
747 :
748 1 : error = col_add_collection_to_collection(peer, NULL, NULL, socket1, COL_ADD_MODE_FLATDOT);
749 1 : if (error) {
750 0 : col_destroy_collection(peer);
751 0 : col_destroy_collection(socket1);
752 0 : printf("Failed to add collection to collection. Error %d\n", error);
753 0 : return error;
754 : }
755 :
756 1 : col_destroy_collection(socket1);
757 :
758 : /* Bind iterator */
759 1 : error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_DEFAULT);
760 1 : if (error) {
761 0 : printf("Error (bind): %d\n", error);
762 0 : col_destroy_collection(peer);
763 0 : return error;
764 : }
765 :
766 1 : COLOUT(printf("\n\nCollection (traverse default):\n\n"));
767 1 : COLOUT(col_debug_collection(peer, COL_TRAVERSE_DEFAULT));
768 :
769 1 : COLOUT(printf("\n\nCollection (traverse flat):\n\n"));
770 1 : COLOUT(col_debug_collection(peer, COL_TRAVERSE_FLAT | COL_TRAVERSE_END));
771 :
772 1 : COLOUT(printf("\n\nIteration (1):\n\n"));
773 :
774 : do {
775 :
776 :
777 : /* Loop through a collection */
778 16 : error = col_iterate_collection(iterator, &item);
779 16 : if (error) {
780 0 : printf("Error (iterate): %d\n", error);
781 0 : col_unbind_iterator(iterator);
782 0 : col_destroy_collection(peer);
783 0 : return error;
784 : }
785 :
786 : /* Are we done ? */
787 16 : if (item == NULL) break;
788 :
789 15 : depth = 0;
790 15 : col_get_item_depth(iterator, &depth);
791 15 : idepth = 0;
792 15 : col_get_iterator_depth(iterator, &idepth);
793 :
794 :
795 15 : COLOUT(printf("%*sProperty (%s), type = %d, data size = %d depth = %d idepth = %d\n",
796 : depth * 4, "",
797 : col_get_item_property(item, NULL),
798 : col_get_item_type(item),
799 : col_get_item_length(item),
800 : depth,
801 : idepth));
802 :
803 18 : if ((strcmp(col_get_item_property(item, NULL), "id")==0) &&
804 3 : (*((int *)(col_get_item_data(item))) == 1)) {
805 2 : COLOUT(printf("\n\nFound property we need - go up!!!\n\n\n"));
806 :
807 : /* This should work! */
808 2 : error = col_iterate_up(iterator, 1);
809 2 : if (error) {
810 0 : printf("We expected success but got error %d\n", error);
811 0 : col_unbind_iterator(iterator);
812 0 : col_destroy_collection(peer);
813 0 : return error;
814 : }
815 :
816 4 : if ((error = col_modify_str_item(item, "id2", "test", 0)) ||
817 4 : ((verbose) && (error = col_debug_item(item))) ||
818 4 : (error = col_modify_str_item(item, NULL, "test", 2)) ||
819 4 : ((verbose) && (error = col_debug_item(item))) ||
820 4 : (error = col_modify_binary_item(item, NULL, binary_dump, sizeof(binary_dump))) ||
821 4 : ((verbose) && (error = col_debug_item(item))) ||
822 4 : (error = col_modify_bool_item(item, NULL, 1)) ||
823 4 : ((verbose) && (error = col_debug_item(item))) ||
824 4 : (error = col_modify_int_item(item, "int", 1)) ||
825 4 : ((verbose) && (error = col_debug_item(item))) ||
826 4 : (error = col_modify_long_item(item, "long", 1000000000L)) ||
827 4 : ((verbose) && (error = col_debug_item(item))) ||
828 4 : (error = col_modify_ulong_item(item, "ulong", 4000000000UL)) ||
829 4 : ((verbose) && (error = col_debug_item(item))) ||
830 4 : (error = col_modify_unsigned_item(item, "unsigned", 4000000000U)) ||
831 4 : ((verbose) && (error = col_debug_item(item))) ||
832 4 : (error = col_modify_double_item(item, "double", -1.1)) ||
833 2 : ((verbose) && (error = col_debug_item(item)))) {
834 0 : printf("Failed to change property.\n");
835 0 : col_unbind_iterator(iterator);
836 0 : col_destroy_collection(peer);
837 0 : return error;
838 : }
839 :
840 2 : COLOUT(printf("Item name: %s\n", col_get_item_property(item, NULL)));
841 2 : COLOUT(printf("Item hash: %lu\n", (unsigned long int)col_get_item_hash(item)));
842 2 : error = col_modify_item_property(item, "new_name");
843 2 : if (error) {
844 0 : printf("We expected success but got error %d\n", error);
845 0 : col_unbind_iterator(iterator);
846 0 : col_destroy_collection(peer);
847 0 : return error;
848 : }
849 2 : len = 0;
850 2 : COLOUT(printf("Item name: %s\n", col_get_item_property(item, &len)));
851 2 : COLOUT(printf("Item hash: %lu\n", (unsigned long int)col_get_item_hash(item)));
852 2 : COLOUT(printf("Item length: %d\n", len));
853 :
854 2 : len = 0;
855 2 : hash1 = col_make_hash("new_name", 0, &len);
856 2 : COLOUT(printf("String name: %s\n", "new_name"));
857 2 : COLOUT(printf("String hash: %lu\n", (unsigned long int)hash1));
858 2 : COLOUT(printf("String length: %d\n", len));
859 :
860 2 : len = 0;
861 2 : hash2 = col_make_hash("new_name_suffix", 8, &len);
862 2 : COLOUT(printf("String name: %.*s\n", len, "new_name_suffix"));
863 2 : COLOUT(printf("String hash: %lu\n", (unsigned long int)hash2));
864 2 : COLOUT(printf("String length: %d\n", len));
865 2 : if (hash1 != hash2) {
866 0 : printf("Hash calculation failed\n");
867 0 : col_unbind_iterator(iterator);
868 0 : col_destroy_collection(peer);
869 0 : return EINVAL;
870 : }
871 :
872 2 : hash2 = col_make_hash("new_name", 8, &len);
873 2 : COLOUT(printf("String name: %.*s\n", len, "new_name"));
874 2 : COLOUT(printf("String hash: %lu\n", (unsigned long int)hash2));
875 2 : COLOUT(printf("String length: %d\n", len));
876 2 : if (hash1 != hash2) {
877 0 : printf("Hash calculation failed\n");
878 0 : col_unbind_iterator(iterator);
879 0 : col_destroy_collection(peer);
880 0 : return EINVAL;
881 : }
882 :
883 : }
884 : }
885 15 : while(1);
886 :
887 1 : col_unbind_iterator(iterator);
888 :
889 : /* Bind iterator again in flat mode */
890 1 : error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_FLAT);
891 1 : if (error) {
892 0 : printf("Error (bind): %d\n", error);
893 0 : col_destroy_collection(peer);
894 0 : return error;
895 : }
896 :
897 1 : COLOUT(printf("\n\nIteration (2 - flat):\n\n"));
898 :
899 : do {
900 :
901 : /* Loop through a collection */
902 27 : error = col_iterate_collection(iterator, &item);
903 27 : if (error) {
904 0 : printf("Error (iterate): %d\n", error);
905 0 : col_destroy_collection(peer);
906 0 : col_unbind_iterator(iterator);
907 0 : return error;
908 : }
909 :
910 : /* Are we done ? */
911 27 : if (item == NULL) break;
912 :
913 26 : depth = 0;
914 26 : col_get_item_depth(iterator, &depth);
915 26 : COLOUT(printf("%*s", depth * 4, ""));
916 26 : COLOUT(col_debug_item(item));
917 :
918 26 : if ((strcmp(col_get_item_property(item, NULL), "queue") == 0) &&
919 : (rwnd == 0)) {
920 1 : COLOUT(printf("Rewinding iterator...\n"));
921 1 : col_rewind_iterator(iterator);
922 1 : rwnd++;
923 : }
924 :
925 : }
926 26 : while(1);
927 :
928 : /* Do not forget to unbind iterator - otherwise there will be a leak */
929 1 : col_unbind_iterator(iterator);
930 :
931 : /* Bind iterator again in flat mode */
932 1 : error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_FLAT | COL_TRAVERSE_END);
933 1 : if (error) {
934 0 : printf("Error (bind): %d\n", error);
935 0 : col_destroy_collection(peer);
936 0 : return error;
937 : }
938 :
939 1 : COLOUT(printf("\n\nIteration (3 flat with end):\n\n"));
940 :
941 : do {
942 :
943 : /* Loop through a collection */
944 18 : error = col_iterate_collection(iterator, &item);
945 18 : if (error) {
946 0 : printf("Error (iterate): %d\n", error);
947 0 : col_destroy_collection(peer);
948 0 : col_unbind_iterator(iterator);
949 0 : return error;
950 : }
951 :
952 : /* Are we done ? */
953 18 : if (item == NULL) break;
954 :
955 17 : depth = 0;
956 17 : col_get_item_depth(iterator, &depth);
957 17 : COLOUT(printf("%*s", depth * 4, ""));
958 17 : COLOUT(col_debug_item(item));
959 :
960 : }
961 17 : while(1);
962 :
963 : /* Do not forget to unbind iterator - otherwise there will be a leak */
964 1 : col_unbind_iterator(iterator);
965 :
966 : /* Bind iterator again in flat mode */
967 1 : error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_DEFAULT | COL_TRAVERSE_END);
968 1 : if (error) {
969 0 : printf("Error (bind): %d\n", error);
970 0 : col_destroy_collection(peer);
971 0 : return error;
972 : }
973 :
974 1 : COLOUT(printf("\n\nIteration (4 default with end):\n\n"));
975 :
976 : do {
977 :
978 : /* Loop through a collection */
979 24 : error = col_iterate_collection(iterator, &item);
980 24 : if (error) {
981 0 : printf("Error (iterate): %d\n", error);
982 0 : col_destroy_collection(peer);
983 0 : col_unbind_iterator(iterator);
984 0 : return error;
985 : }
986 :
987 : /* Are we done ? */
988 24 : if (item == NULL) break;
989 :
990 23 : depth = 0;
991 23 : col_get_item_depth(iterator, &depth);
992 23 : COLOUT(printf("%*s", depth * 4, ""));
993 23 : COLOUT(col_debug_item(item));
994 :
995 : }
996 23 : while(1);
997 :
998 : /* Do not forget to unbind iterator - otherwise there will be a leak */
999 1 : col_unbind_iterator(iterator);
1000 :
1001 : /* Bind iterator again in flat mode */
1002 1 : error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_SHOWSUB | COL_TRAVERSE_END);
1003 1 : if (error) {
1004 0 : printf("Error (bind): %d\n", error);
1005 0 : col_destroy_collection(peer);
1006 0 : return error;
1007 : }
1008 :
1009 :
1010 1 : COLOUT(printf("\n\nIteration (5 show headers and references with end):\n\n"));
1011 :
1012 : do {
1013 :
1014 : /* Loop through a collection */
1015 27 : error = col_iterate_collection(iterator, &item);
1016 27 : if (error) {
1017 0 : printf("Error (iterate): %d\n", error);
1018 0 : col_destroy_collection(peer);
1019 0 : col_unbind_iterator(iterator);
1020 0 : return error;
1021 : }
1022 :
1023 : /* Are we done ? */
1024 27 : if (item == NULL) break;
1025 :
1026 26 : depth = 0;
1027 26 : col_get_item_depth(iterator, &depth);
1028 26 : COLOUT(printf("%*s", depth * 4, ""));
1029 26 : COLOUT(col_debug_item(item));
1030 :
1031 : }
1032 26 : while(1);
1033 :
1034 : /* Do not forget to unbind iterator - otherwise there will be a leak */
1035 1 : col_unbind_iterator(iterator);
1036 :
1037 : /* Bind iterator again in flat mode */
1038 1 : error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_SHOWSUB);
1039 1 : if (error) {
1040 0 : printf("Error (bind): %d\n", error);
1041 0 : col_destroy_collection(peer);
1042 0 : return error;
1043 : }
1044 :
1045 :
1046 1 : COLOUT(printf("\n\nIteration (6 show headers and references no END):\n\n"));
1047 :
1048 : do {
1049 :
1050 : /* Loop through a collection */
1051 23 : error = col_iterate_collection(iterator, &item);
1052 23 : if (error) {
1053 0 : printf("Error (iterate): %d\n", error);
1054 0 : col_destroy_collection(peer);
1055 0 : col_unbind_iterator(iterator);
1056 0 : return error;
1057 : }
1058 :
1059 : /* Are we done ? */
1060 23 : if (item == NULL) break;
1061 :
1062 22 : depth = 0;
1063 22 : col_get_item_depth(iterator, &depth);
1064 22 : COLOUT(printf("%*s", depth * 4, ""));
1065 22 : COLOUT(col_debug_item(item));
1066 :
1067 : }
1068 22 : while(1);
1069 :
1070 : /* Do not forget to unbind iterator - otherwise there will be a leak */
1071 1 : col_unbind_iterator(iterator);
1072 :
1073 : /* Bind iterator again in flat mode */
1074 1 : error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_ONLYSUB);
1075 1 : if (error) {
1076 0 : printf("Error (bind): %d\n", error);
1077 0 : col_destroy_collection(peer);
1078 0 : return error;
1079 : }
1080 :
1081 1 : COLOUT(printf("\n\nIteration (7 show headers only no END):\n\n"));
1082 :
1083 : do {
1084 :
1085 : /* Loop through a collection */
1086 20 : error = col_iterate_collection(iterator, &item);
1087 20 : if (error) {
1088 0 : printf("Error (iterate): %d\n", error);
1089 0 : col_destroy_collection(peer);
1090 0 : col_unbind_iterator(iterator);
1091 0 : return error;
1092 : }
1093 :
1094 : /* Are we done ? */
1095 20 : if (item == NULL) break;
1096 :
1097 19 : depth = 0;
1098 19 : col_get_item_depth(iterator, &depth);
1099 19 : COLOUT(printf("%*s", depth * 4, ""));
1100 19 : COLOUT(col_debug_item(item));
1101 :
1102 : }
1103 19 : while(1);
1104 :
1105 : /* Do not forget to unbind iterator - otherwise there will be a leak */
1106 1 : col_unbind_iterator(iterator);
1107 :
1108 :
1109 : /* Bind iterator */
1110 1 : error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_DEFAULT);
1111 1 : if (error) {
1112 0 : printf("Error (bind): %d\n", error);
1113 0 : col_destroy_collection(peer);
1114 0 : return error;
1115 : }
1116 :
1117 1 : COLOUT(printf("\n\nIterate up test:\n\n"));
1118 :
1119 : do {
1120 :
1121 : /* Loop through a collection */
1122 13 : error = col_iterate_collection(iterator, &item);
1123 13 : if (error) {
1124 0 : printf("Error (iterate): %d\n", error);
1125 0 : col_unbind_iterator(iterator);
1126 0 : col_destroy_collection(peer);
1127 0 : return error;
1128 : }
1129 :
1130 : /* Are we done ? */
1131 13 : if (item == NULL) break;
1132 :
1133 12 : depth = 0;
1134 12 : col_get_item_depth(iterator, &depth);
1135 12 : idepth = 0;
1136 12 : col_get_iterator_depth(iterator, &idepth);
1137 :
1138 :
1139 12 : COLOUT(printf("%*sProperty (%s), type = %d, data size = %d depth = %d idepth = %d\n",
1140 : depth * 4, "",
1141 : col_get_item_property(item, NULL),
1142 : col_get_item_type(item),
1143 : col_get_item_length(item),
1144 : depth,
1145 : idepth));
1146 :
1147 12 : if (strcmp(col_get_item_property(item, NULL), "queue") == 0) {
1148 :
1149 1 : COLOUT(printf("\n\nFound property we need - go up!!!\n"));
1150 1 : COLOUT(printf("Expect bail out of collection processing.\n\n"));
1151 :
1152 : /* This should work! */
1153 1 : error = col_iterate_up(iterator, 10);
1154 1 : if (error) {
1155 0 : printf("We expected success but got error %d\n", error);
1156 0 : col_unbind_iterator(iterator);
1157 0 : col_destroy_collection(peer);
1158 0 : return error;
1159 : }
1160 :
1161 : }
1162 : }
1163 12 : while(1);
1164 :
1165 1 : col_unbind_iterator(iterator);
1166 :
1167 : /* Bind iterator again in flat mode */
1168 1 : error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_FLAT | COL_TRAVERSE_END);
1169 1 : if (error) {
1170 0 : printf("Error (bind): %d\n", error);
1171 0 : col_destroy_collection(peer);
1172 0 : return error;
1173 : }
1174 :
1175 1 : COLOUT(printf("\n\nCircled looping:\n\n"));
1176 :
1177 201 : for (i = 0; i < 200; i++) {
1178 : /* Loop through a collection */
1179 200 : error = col_iterate_collection(iterator, &item);
1180 200 : if (error) {
1181 0 : printf("Error (iterate): %d\n", error);
1182 0 : col_destroy_collection(peer);
1183 0 : col_unbind_iterator(iterator);
1184 0 : return error;
1185 : }
1186 :
1187 : /* Are we done ? */
1188 200 : if (item == NULL) {
1189 11 : COLOUT(printf("Reached end.\n\n"));
1190 : }
1191 : else {
1192 189 : depth = 0;
1193 189 : col_get_item_depth(iterator, &depth);
1194 189 : COLOUT(printf("%*s", depth * 4, ""));
1195 189 : COLOUT(col_debug_item(item));
1196 : }
1197 : }
1198 :
1199 : /* Do not forget to unbind iterator - otherwise there will be a leak */
1200 1 : col_unbind_iterator(iterator);
1201 :
1202 : /* Bind iterator again in flat mode */
1203 1 : error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_FLAT | COL_TRAVERSE_END);
1204 1 : if (error) {
1205 0 : printf("Error (bind): %d\n", error);
1206 0 : col_destroy_collection(peer);
1207 0 : return error;
1208 : }
1209 :
1210 1 : COLOUT(printf("\n\nCircled looping with pin:\n\n"));
1211 :
1212 : do {
1213 : /* Loop through a collection */
1214 10 : error = col_iterate_collection(iterator, &item);
1215 10 : if (error) {
1216 0 : printf("Error (iterate): %d\n", error);
1217 0 : col_destroy_collection(peer);
1218 0 : col_unbind_iterator(iterator);
1219 0 : return error;
1220 : }
1221 :
1222 10 : if (strcmp(col_get_item_property(item, NULL), "queue") == 0) {
1223 : /* Make it a new looping point */
1224 1 : col_pin_iterator(iterator);
1225 1 : COLOUT(printf("Found pin point.\n\n"));
1226 1 : break;
1227 : }
1228 : /* Are we done ? */
1229 9 : if (item == NULL) {
1230 0 : printf("Unexpected end.\n\n");
1231 0 : col_destroy_collection(peer);
1232 0 : col_unbind_iterator(iterator);
1233 0 : return EINVAL;
1234 : }
1235 : else {
1236 9 : depth = 0;
1237 9 : col_get_item_depth(iterator, &depth);
1238 9 : COLOUT(printf("%*s", depth * 4, ""));
1239 9 : COLOUT(col_debug_item(item));
1240 : }
1241 : }
1242 9 : while(1);
1243 :
1244 : /* Second loop around the pin point */
1245 201 : for (i = 0; i < 200; i++) {
1246 : /* Loop through a collection */
1247 200 : error = col_iterate_collection(iterator, &item);
1248 200 : if (error) {
1249 0 : printf("Error (iterate): %d\n", error);
1250 0 : col_destroy_collection(peer);
1251 0 : col_unbind_iterator(iterator);
1252 0 : return error;
1253 : }
1254 :
1255 : /* Are we done ? */
1256 200 : if (item == NULL) {
1257 11 : COLOUT(printf("Reached end.\n\n"));
1258 : }
1259 : else {
1260 189 : depth = 0;
1261 189 : col_get_item_depth(iterator, &depth);
1262 189 : COLOUT(printf("%*s", depth * 4, ""));
1263 189 : COLOUT(col_debug_item(item));
1264 : }
1265 : }
1266 :
1267 : /* Do not forget to unbind iterator - otherwise there will be a leak */
1268 1 : col_unbind_iterator(iterator);
1269 :
1270 :
1271 : /* Bind iterator again in flat mode */
1272 1 : error = col_bind_iterator(&iterator, peer, COL_TRAVERSE_DEFAULT | COL_TRAVERSE_END);
1273 1 : if (error) {
1274 0 : printf("Error (bind): %d\n", error);
1275 0 : col_destroy_collection(peer);
1276 0 : return error;
1277 : }
1278 :
1279 1 : COLOUT(printf("\n\nCircled looping with pin (default):\n\n"));
1280 :
1281 : do {
1282 : /* Loop through a collection */
1283 13 : error = col_iterate_collection(iterator, &item);
1284 13 : if (error) {
1285 0 : printf("Error (iterate): %d\n", error);
1286 0 : col_destroy_collection(peer);
1287 0 : col_unbind_iterator(iterator);
1288 0 : return error;
1289 : }
1290 :
1291 13 : if (strcmp(col_get_item_property(item, NULL), "queue") == 0) {
1292 : /* Make it a new looping point */
1293 1 : col_pin_iterator(iterator);
1294 1 : COLOUT(printf("Found pin point.\n\n"));
1295 1 : break;
1296 : }
1297 : /* Are we done ? */
1298 12 : if (item == NULL) {
1299 0 : printf("Unexpected end.\n\n");
1300 0 : col_destroy_collection(peer);
1301 0 : col_unbind_iterator(iterator);
1302 0 : return EINVAL;
1303 : }
1304 : else {
1305 12 : depth = 0;
1306 12 : col_get_item_depth(iterator, &depth);
1307 12 : COLOUT(printf("%*s", depth * 4, ""));
1308 12 : COLOUT(col_debug_item(item));
1309 : }
1310 : }
1311 12 : while(1);
1312 :
1313 : /* Second loop around the pin point */
1314 201 : for (i = 0; i < 200; i++) {
1315 : /* Loop through a collection */
1316 200 : error = col_iterate_collection(iterator, &item);
1317 200 : if (error) {
1318 0 : printf("Error (iterate): %d\n", error);
1319 0 : col_destroy_collection(peer);
1320 0 : col_unbind_iterator(iterator);
1321 0 : return error;
1322 : }
1323 :
1324 : /* Are we done ? */
1325 200 : if (item == NULL) {
1326 8 : COLOUT(printf("Reached end.\n\n"));
1327 : }
1328 : else {
1329 192 : depth = 0;
1330 192 : col_get_item_depth(iterator, &depth);
1331 192 : COLOUT(printf("%*s", depth * 4, ""));
1332 192 : COLOUT(col_debug_item(item));
1333 : }
1334 : }
1335 :
1336 : /* Do not forget to unbind iterator - otherwise there will be a leak */
1337 1 : col_unbind_iterator(iterator);
1338 1 : col_destroy_collection(peer);
1339 :
1340 1 : return EOK;
1341 : }
1342 :
1343 :
1344 1 : static int insert_extract_test(void)
1345 : {
1346 : struct collection_item *col;
1347 : struct collection_item *col2;
1348 1 : int error = EOK;
1349 1 : struct collection_item *item = NULL;
1350 :
1351 1 : COLOUT(printf("\n\n==== INSERTION TEST ====\n\n"));
1352 :
1353 2 : if ((error = col_create_collection(&col, "insertion", 0)) ||
1354 1 : (error = col_insert_str_property(col, NULL, COL_DSP_END,
1355 : NULL, 0, COL_INSERT_NOCHECK,
1356 1 : "property1", "value1", 0)) ||
1357 1 : (error = col_insert_str_property(col, NULL, COL_DSP_INDEX,
1358 : NULL, 1, COL_INSERT_NOCHECK,
1359 1 : "second", "second", 0)) ||
1360 1 : (error = col_insert_str_property(col, NULL, COL_DSP_END,
1361 : NULL, 0, COL_INSERT_NOCHECK,
1362 1 : "property2", "value2", 0)) ||
1363 1 : (error = col_insert_str_property(col, NULL, COL_DSP_FRONT,
1364 : NULL, 0, COL_INSERT_NOCHECK,
1365 1 : "property0", "value0", 0)) ||
1366 1 : (error = col_insert_str_property(col, NULL, COL_DSP_BEFORE,
1367 : "property0", 0, COL_INSERT_NOCHECK,
1368 1 : "property_-1", "value_-1", 0)) ||
1369 1 : (error = col_insert_str_property(col, NULL, COL_DSP_BEFORE,
1370 : "property1", 0, COL_INSERT_NOCHECK,
1371 1 : "property0_5", "value0_5", 0)) ||
1372 1 : (error = col_insert_str_property(col, NULL, COL_DSP_BEFORE,
1373 : "property2", 0, COL_INSERT_NOCHECK,
1374 1 : "property1_5", "value1_5", 0)) ||
1375 1 : (error = col_insert_str_property(col, NULL, COL_DSP_AFTER,
1376 : "property_-1", 0, COL_INSERT_NOCHECK,
1377 1 : "property_-0_5", "value_-0_5", 0)) ||
1378 1 : (error = col_insert_str_property(col, NULL, COL_DSP_AFTER,
1379 : "property1_5", 0, COL_INSERT_NOCHECK,
1380 1 : "property1_6", "value1_6", 0)) ||
1381 1 : (error = col_insert_str_property(col, NULL, COL_DSP_INDEX,
1382 : NULL, 10, COL_INSERT_NOCHECK,
1383 1 : "property10", "value10", 0)) ||
1384 1 : (error = col_insert_str_property(col, NULL, COL_DSP_INDEX,
1385 : NULL, 0, COL_INSERT_NOCHECK,
1386 1 : "property_-2", "value_-2", 0)) ||
1387 1 : (error = col_insert_str_property(col, NULL, COL_DSP_INDEX,
1388 : NULL, 1, COL_INSERT_NOCHECK,
1389 1 : "property_-1_5", "value_-1_5", 0)) ||
1390 1 : (error = col_insert_str_property(col, NULL, COL_DSP_FIRSTDUP,
1391 : NULL, 0, COL_INSERT_NOCHECK,
1392 1 : "property0", "value0firstdup", 0)) ||
1393 1 : (error = col_insert_str_property(col, NULL, COL_DSP_LASTDUP,
1394 : NULL, 0, COL_INSERT_NOCHECK,
1395 1 : "property0", "value0lastdup", 0)) ||
1396 1 : (error = col_insert_str_property(col, NULL, COL_DSP_NDUP,
1397 : NULL, 1, COL_INSERT_NOCHECK,
1398 1 : "property0", "value0middledup", 0)) ||
1399 1 : (error = col_insert_str_property(col, NULL, 0,
1400 : NULL, 0, COL_INSERT_DUPOVER ,
1401 1 : "property0", "value0firstdupupdate", 0)) ||
1402 1 : (error = col_insert_str_property(col, NULL, 0,
1403 : NULL, 0, COL_INSERT_DUPOVERT,
1404 1 : "property1", "value1update", 0)) ||
1405 1 : ((error = col_insert_str_property(col, NULL, 0,
1406 : NULL, 0, COL_INSERT_DUPERROR,
1407 1 : "property0", "does not matter", 0)) != EEXIST) ||
1408 1 : (error = col_insert_str_property(col, NULL, COL_DSP_NDUP,
1409 : NULL, 5, COL_INSERT_NOCHECK,
1410 1 : "property10", "value10dup", 0)) ||
1411 1 : (error = col_insert_str_property(col, NULL, COL_DSP_LASTDUP,
1412 : NULL, 0, COL_INSERT_NOCHECK,
1413 1 : "property10", "value10lastdup", 0)) ||
1414 1 : (error = col_insert_str_property(col, NULL, COL_DSP_END,
1415 : NULL, 0, COL_INSERT_DUPMOVET,
1416 1 : "property_-2", "value-2moved_to_bottom", 0)) ||
1417 1 : (error = col_insert_str_property(col, NULL, COL_DSP_FRONT,
1418 : NULL, 0, COL_INSERT_DUPMOVE,
1419 : "property1_6", "value_1_6_moved_moved_to_front", 0))) {
1420 :
1421 0 : printf("ERROR in the ITERATION TEST\n");
1422 0 : col_debug_collection(col, COL_TRAVERSE_DEFAULT);
1423 0 : col_destroy_collection(col);
1424 0 : return error;
1425 : }
1426 :
1427 1 : COLOUT(printf("\n\nCollection:\n\n"));
1428 1 : COLOUT(col_debug_collection(col, COL_TRAVERSE_DEFAULT));
1429 :
1430 :
1431 1 : COLOUT(printf("\n\n==== EXTRACTION TEST ====\n\n"));
1432 :
1433 2 : if ((error = col_create_collection(&col2, "extraction", 0)) ||
1434 :
1435 1 : (error = col_extract_item(col, NULL, COL_DSP_FRONT,
1436 1 : NULL, 0, 0, &item)) ||
1437 :
1438 1 : (error = col_insert_item(col2, NULL, item, COL_DSP_FRONT,
1439 1 : NULL, 0, COL_INSERT_NOCHECK)) ||
1440 :
1441 2 : ((verbose) && (error = col_debug_collection(col2, COL_TRAVERSE_DEFAULT))) ||
1442 :
1443 1 : (error = col_extract_item(col, NULL, COL_DSP_FRONT,
1444 1 : NULL, 0, 0, &item)) ||
1445 :
1446 1 : (error = col_insert_item(col2, NULL, item, COL_DSP_FRONT,
1447 1 : NULL, 0, COL_INSERT_NOCHECK)) ||
1448 :
1449 2 : ((verbose) && (error = col_debug_collection(col2, COL_TRAVERSE_DEFAULT))) ||
1450 :
1451 1 : (error = col_extract_item(col, NULL, COL_DSP_END,
1452 1 : NULL, 0, 0, &item)) ||
1453 :
1454 1 : (error = col_insert_item(col2, NULL, item, COL_DSP_END,
1455 1 : NULL, 0, COL_INSERT_NOCHECK)) ||
1456 :
1457 2 : ((verbose) && (error = col_debug_collection(col2, COL_TRAVERSE_DEFAULT))) ||
1458 :
1459 1 : (error = col_insert_str_property(col, NULL, COL_DSP_INDEX,
1460 : NULL, 100, COL_INSERT_NOCHECK,
1461 1 : "property100", "value100", 0)) ||
1462 :
1463 1 : (error = col_extract_item(col, NULL, COL_DSP_AFTER,
1464 1 : "property10", 0, COL_TYPE_STRING, &item)) ||
1465 :
1466 1 : (error = col_insert_item(col2, NULL, item, COL_DSP_END,
1467 1 : NULL, 0, COL_INSERT_NOCHECK)) ||
1468 :
1469 2 : ((verbose) && (error = col_debug_collection(col2, COL_TRAVERSE_DEFAULT))) ||
1470 :
1471 1 : (error = col_extract_item(col, NULL, COL_DSP_BEFORE,
1472 1 : "property0", 0, COL_TYPE_STRING, &item)) ||
1473 :
1474 1 : (error = col_insert_item(col2, NULL, item, COL_DSP_END,
1475 1 : NULL, 0, COL_INSERT_NOCHECK)) ||
1476 :
1477 2 : ((verbose) && (error = col_debug_collection(col2, COL_TRAVERSE_DEFAULT))) ||
1478 :
1479 1 : (error = col_extract_item(col, NULL, COL_DSP_INDEX,
1480 1 : NULL, 1, 0, &item)) ||
1481 :
1482 1 : (error = col_insert_item(col2, NULL, item, COL_DSP_END,
1483 1 : NULL, 0, COL_INSERT_NOCHECK)) ||
1484 :
1485 2 : ((verbose) && (error = col_debug_collection(col2, COL_TRAVERSE_DEFAULT))) ||
1486 :
1487 1 : (error = col_extract_item(col, NULL, COL_DSP_NDUP,
1488 1 : "property0", 1, 0, &item)) ||
1489 :
1490 1 : (error = col_insert_item(col2, NULL, item, COL_DSP_END,
1491 1 : NULL, 0, COL_INSERT_NOCHECK)) ||
1492 :
1493 2 : ((verbose) && (error = col_debug_collection(col2, COL_TRAVERSE_DEFAULT))) ||
1494 :
1495 1 : (error = col_extract_item(col, NULL, COL_DSP_LASTDUP,
1496 1 : "property0", 0, 0, &item)) ||
1497 :
1498 1 : (error = col_insert_item(col2, NULL, item, COL_DSP_END,
1499 1 : NULL, 0, COL_INSERT_NOCHECK)) ||
1500 :
1501 2 : ((verbose) && (error = col_debug_collection(col2, COL_TRAVERSE_DEFAULT))) ||
1502 :
1503 1 : (error = col_extract_item(col, NULL, COL_DSP_FIRSTDUP,
1504 1 : "property0", 0, 0, &item)) ||
1505 :
1506 1 : (error = col_insert_item(col2, NULL, item, COL_DSP_END,
1507 1 : NULL, 0, COL_INSERT_NOCHECK)) ||
1508 :
1509 1 : ((verbose) && (error = col_debug_collection(col2, COL_TRAVERSE_DEFAULT)))) {
1510 :
1511 0 : COLOUT(printf("ERROR in the EXTRACTION TEST\n"));
1512 0 : COLOUT(printf("Collection 1\n"));
1513 0 : COLOUT(col_debug_collection(col, COL_TRAVERSE_DEFAULT));
1514 0 : COLOUT(printf("Collection 2\n"));
1515 0 : COLOUT(col_debug_collection(col2, COL_TRAVERSE_DEFAULT));
1516 0 : col_destroy_collection(col);
1517 0 : col_destroy_collection(col2);
1518 0 : return error;
1519 : }
1520 :
1521 1 : COLOUT(printf("Collection 1\n"));
1522 1 : COLOUT(col_debug_collection(col, COL_TRAVERSE_DEFAULT));
1523 1 : COLOUT(printf("Collection 2\n"));
1524 1 : COLOUT(col_debug_collection(col2, COL_TRAVERSE_DEFAULT));
1525 :
1526 1 : col_destroy_collection(col2);
1527 1 : col_destroy_collection(col);
1528 :
1529 :
1530 1 : return EOK;
1531 : }
1532 :
1533 : /* Cleanup collback */
1534 3 : static void cb(const char *property,
1535 : int property_len,
1536 : int type,
1537 : void *data,
1538 : int length,
1539 : void *ext_data)
1540 : {
1541 3 : COLOUT(printf("%s\n", *((const char **)ext_data)));
1542 3 : COLOUT(printf("Property: %s\n", property));
1543 3 : COLOUT(printf("Length: %d\n", property_len));
1544 3 : COLOUT(printf("Type: %d\n", type));
1545 3 : COLOUT(printf("Data len: %d\n", length));
1546 3 : }
1547 :
1548 1 : static int delete_test(void)
1549 : {
1550 :
1551 : struct collection_item *col;
1552 1 : int error = EOK;
1553 1 : const char *str = "Cleanup Callback Test";
1554 :
1555 1 : COLOUT(printf("\n\n==== DELETION TEST 1====\n\n"));
1556 :
1557 2 : if ((error = col_create_collection(&col, "test", 0)) ||
1558 2 : (error = col_add_int_property(col, NULL, "tt", 1)) ||
1559 2 : ((verbose) && (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT))) ||
1560 2 : (error = col_add_int_property(col, NULL, "test", 1)) ||
1561 2 : ((verbose) && (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT))) ||
1562 2 : (error = col_delete_property(col, "test", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT)) ||
1563 2 : ((verbose) && (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT))) ||
1564 2 : (error = col_add_int_property(col, NULL, "test", 1)) ||
1565 2 : ((verbose) && (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT))) ||
1566 2 : (error = col_delete_property(col, "test", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT)) ||
1567 2 : ((verbose) && (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT))) ||
1568 1 : (error = col_add_int_property(col, NULL, "test", 1))) {
1569 0 : printf("Error in delete test %d\n", error);
1570 0 : col_destroy_collection(col);
1571 0 : return error;
1572 : }
1573 :
1574 1 : COLOUT(col_debug_collection(col, COL_TRAVERSE_DEFAULT));
1575 1 : col_destroy_collection(col);
1576 :
1577 1 : COLOUT(printf("\n\n==== DELETION TEST 1 END ====\n\n"));
1578 1 : COLOUT(printf("\n\n==== DELETION TEST 2====\n\n"));
1579 :
1580 2 : if ((error = col_create_collection(&col, "test2", 0)) ||
1581 2 : (error = col_add_int_property(col, NULL, "tt", 1)) ||
1582 2 : ((verbose) && (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT))) ||
1583 2 : (error = col_add_int_property(col, NULL, "test", 1)) ||
1584 2 : ((verbose) && (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT))) ||
1585 2 : (error = col_remove_item(col, NULL, COL_DSP_END, NULL, 0, COL_TYPE_ANY)) ||
1586 2 : ((verbose) && (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT))) ||
1587 2 : (error = col_add_int_property(col, NULL, "test", 1)) ||
1588 2 : ((verbose) && (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT))) ||
1589 2 : (error = col_remove_item_from_current(col, COL_DSP_AFTER, "tt", 0, COL_TYPE_ANY)) ||
1590 2 : ((verbose) && (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT))) ||
1591 1 : (error = col_add_int_property(col, NULL, "test", 1))) {
1592 0 : printf("Error in delete test %d\n", error);
1593 0 : col_destroy_collection(col);
1594 0 : return error;
1595 : }
1596 :
1597 1 : COLOUT(col_debug_collection(col, COL_TRAVERSE_DEFAULT));
1598 :
1599 1 : COLOUT(printf("\n\n==== DELETION TEST 2 END ====\n\n"));
1600 :
1601 :
1602 1 : col_destroy_collection_with_cb(col, cb, (void *)(&str));
1603 :
1604 1 : return error;
1605 : }
1606 :
1607 : /* Search test */
1608 1 : static int search_test(void)
1609 : {
1610 1 : struct collection_item *level1 = NULL;
1611 1 : struct collection_item *level2 = NULL;
1612 1 : struct collection_item *level3 = NULL;
1613 1 : struct collection_item *level4 = NULL;
1614 1 : char binary_dump[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
1615 1 : int found = 0;
1616 1 : int error = 0;
1617 :
1618 1 : COLOUT(printf("\n\n==== SEARCH TEST ====\n\n"));
1619 :
1620 1 : if ((error = col_create_collection(&level1, "level1", 0)) ||
1621 1 : (error = col_create_collection(&level2, "level2", 0)) ||
1622 2 : (error = col_add_collection_to_collection(level1, NULL, NULL, level2, COL_ADD_MODE_REFERENCE)) ||
1623 1 : (error = col_create_collection(&level3, "level3", 0)) ||
1624 2 : (error = col_add_collection_to_collection(level1, "level2", NULL, level3, COL_ADD_MODE_REFERENCE)) ||
1625 1 : (error = col_create_collection(&level4, "leveL4", 0)) ||
1626 2 : (error = col_add_collection_to_collection(level1, "level3", NULL, level4, COL_ADD_MODE_REFERENCE)) ||
1627 2 : (error = col_add_int_property(level1, "leveL4", "id", 1)) ||
1628 2 : (error = col_add_long_property(level1, "level3", "packets", 100000000L)) ||
1629 1 : (error = col_add_binary_property(level1, "level2", "stack", binary_dump, sizeof(binary_dump)))) {
1630 0 : col_destroy_collection(level1);
1631 0 : col_destroy_collection(level2);
1632 0 : col_destroy_collection(level3);
1633 0 : col_destroy_collection(level4);
1634 0 : printf("Failed to build test. Error %d\n", error);
1635 0 : return error;
1636 : }
1637 :
1638 1 : COLOUT(col_debug_collection(level1, COL_TRAVERSE_DEFAULT));
1639 :
1640 1 : error = col_is_item_in_collection(level1, "level1!level2!level3!level4!", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found);
1641 1 : if (!error) {
1642 0 : col_destroy_collection(level1);
1643 0 : col_destroy_collection(level2);
1644 0 : col_destroy_collection(level3);
1645 0 : col_destroy_collection(level4);
1646 0 : printf("Expected error here since the search data is illegal but got success\n");
1647 0 : return EINVAL;
1648 : }
1649 :
1650 1 : found = 0;
1651 1 : error = col_is_item_in_collection(level1, "level1!level2!level3!level4!id", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found);
1652 1 : if ((error) || (!found)) {
1653 0 : col_destroy_collection(level1);
1654 0 : col_destroy_collection(level2);
1655 0 : col_destroy_collection(level3);
1656 0 : col_destroy_collection(level4);
1657 0 : printf("Failed to find item [level1!level2!level3!level4!id]. Error %d\n", error);
1658 0 : return error ? error : ENOENT;
1659 : }
1660 1 : else COLOUT(printf("Expected item is found\n"));
1661 :
1662 :
1663 1 : found = 0;
1664 1 : error = col_is_item_in_collection(level1, NULL, COL_TYPE_INTEGER, COL_TRAVERSE_DEFAULT, &found);
1665 1 : if ((error) || (!found)) {
1666 0 : col_destroy_collection(level1);
1667 0 : col_destroy_collection(level2);
1668 0 : col_destroy_collection(level3);
1669 0 : col_destroy_collection(level4);
1670 0 : printf("Failed to find first int item [level1!level2!level3!level4!id]. Error %d\n", error);
1671 0 : return error ? error : ENOENT;
1672 : }
1673 1 : else COLOUT(printf("Expected item is found\n"));
1674 :
1675 :
1676 1 : found = 0;
1677 1 : error = col_is_item_in_collection(level1, "", COL_TYPE_INTEGER, COL_TRAVERSE_DEFAULT, &found);
1678 1 : if ((error) || (!found)) {
1679 0 : col_destroy_collection(level1);
1680 0 : col_destroy_collection(level2);
1681 0 : col_destroy_collection(level3);
1682 0 : col_destroy_collection(level4);
1683 0 : printf("Failed to find first int item [level1!level2!level3!level4!id]. Error %d\n", error);
1684 0 : return error ? error : ENOENT;
1685 : }
1686 1 : else COLOUT(printf("Expected item is found\n"));
1687 :
1688 :
1689 1 : found = 0;
1690 1 : error = col_is_item_in_collection(level1, "level3!level4!id", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found);
1691 1 : if ((error) || (!found)) {
1692 0 : col_destroy_collection(level1);
1693 0 : col_destroy_collection(level2);
1694 0 : col_destroy_collection(level3);
1695 0 : col_destroy_collection(level4);
1696 0 : printf("Failed to find item [level3!level4!id]. Error %d\n", error);
1697 0 : return error ? error : ENOENT;
1698 : }
1699 1 : else COLOUT(printf("Expected item is found\n"));
1700 :
1701 1 : found = 0;
1702 1 : error = col_is_item_in_collection(level1, "level3!packets", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found);
1703 1 : if ((error) || (!found)) {
1704 0 : col_destroy_collection(level1);
1705 0 : col_destroy_collection(level2);
1706 0 : col_destroy_collection(level3);
1707 0 : col_destroy_collection(level4);
1708 0 : printf("Failed to find item [level3.packets]. Error %d\n", error);
1709 0 : return error ? error : ENOENT;
1710 : }
1711 1 : else COLOUT(printf("Expected item is found\n"));
1712 :
1713 1 : found = 0;
1714 1 : error = col_is_item_in_collection(level1, "level1!level2!stack", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found);
1715 1 : if ((error) || (!found)) {
1716 0 : col_destroy_collection(level1);
1717 0 : col_destroy_collection(level2);
1718 0 : col_destroy_collection(level3);
1719 0 : col_destroy_collection(level4);
1720 0 : printf("Failed to find item [level1!level2!stack]. Error %d\n", error);
1721 0 : return error ? error : ENOENT;
1722 : }
1723 1 : else COLOUT(printf("Expected item is found\n"));
1724 :
1725 1 : found = 0;
1726 1 : error = col_is_item_in_collection(level1, "level1!level2!level3", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found);
1727 1 : if ((error) || (!found)) {
1728 0 : col_destroy_collection(level1);
1729 0 : col_destroy_collection(level2);
1730 0 : col_destroy_collection(level3);
1731 0 : col_destroy_collection(level4);
1732 0 : printf("Failed to find item [level1!level2!level3]. Error %d\n", error);
1733 0 : return error ? error : ENOENT;
1734 : }
1735 1 : else COLOUT(printf("Expected item is found\n"));
1736 :
1737 : /* Negative tests */
1738 1 : found = 0;
1739 1 : error = col_is_item_in_collection(level1, NULL, 0, COL_TRAVERSE_DEFAULT, &found);
1740 1 : if ((error != ENOENT) || (found)) {
1741 0 : col_destroy_collection(level1);
1742 0 : col_destroy_collection(level2);
1743 0 : col_destroy_collection(level3);
1744 0 : col_destroy_collection(level4);
1745 0 : if (error) {
1746 0 : printf("Unexpected error with NULL & 0 test %d\n", error);
1747 : }
1748 : else {
1749 0 : printf("Found unexpected item with NULL & 0. Error %d\n", error);
1750 0 : error = EINVAL;
1751 : }
1752 0 : return error;
1753 : }
1754 1 : else COLOUT(printf("No item is found as expected.\n"));
1755 :
1756 1 : found = 0;
1757 1 : error = col_is_item_in_collection(level1, "", 0, COL_TRAVERSE_DEFAULT, &found);
1758 1 : if ((error != ENOENT) || (found)) {
1759 0 : col_destroy_collection(level1);
1760 0 : col_destroy_collection(level2);
1761 0 : col_destroy_collection(level3);
1762 0 : col_destroy_collection(level4);
1763 0 : if (error) {
1764 0 : printf("Unexpected error with \"\" & 0 tests %d\n", error);
1765 : }
1766 : else {
1767 0 : printf("Found unexpected item with \"\" & 0. Error %d\n", error);
1768 0 : error = EINVAL;
1769 : }
1770 0 : return error;
1771 : }
1772 1 : else COLOUT(printf("No item is found as expected.\n"));
1773 :
1774 1 : col_destroy_collection(level1);
1775 1 : col_destroy_collection(level2);
1776 1 : col_destroy_collection(level3);
1777 1 : col_destroy_collection(level4);
1778 :
1779 1 : COLOUT(printf("\n\n==== SEARCH TEST END ====\n\n"));
1780 :
1781 1 : return EOK;
1782 : }
1783 :
1784 : /* Sort test */
1785 1 : static int sort_test(void)
1786 : {
1787 1 : struct collection_item *level1 = NULL;
1788 1 : struct collection_item *level2a = NULL;
1789 1 : struct collection_item *level2b = NULL;
1790 1 : struct collection_item *level3 = NULL;
1791 1 : int error = 0;
1792 :
1793 1 : COLOUT(printf("\n\n==== SORT TEST ====\n\n"));
1794 :
1795 1 : if ((error = col_create_collection(&level1, "level1", 0)) ||
1796 1 : (error = col_create_collection(&level2a, "level2a", 0)) ||
1797 2 : (error = col_add_collection_to_collection(level1, NULL, NULL, level2a, COL_ADD_MODE_REFERENCE)) ||
1798 1 : (error = col_create_collection(&level2b, "level2b", 0)) ||
1799 2 : (error = col_add_collection_to_collection(level1, NULL, NULL, level2b, COL_ADD_MODE_REFERENCE)) ||
1800 1 : (error = col_create_collection(&level3, "level3", 0)) ||
1801 2 : (error = col_add_collection_to_collection(level1, "level2a", NULL, level3, COL_ADD_MODE_REFERENCE)) ||
1802 2 : (error = col_add_collection_to_collection(level1, "level2b", NULL, level3, COL_ADD_MODE_REFERENCE)) ||
1803 2 : (error = col_add_int_property(level1, NULL, "int3", 1)) ||
1804 2 : (error = col_add_int_property(level1, NULL, "int2", 2)) ||
1805 2 : (error = col_add_int_property(level1, NULL, "int1", 3)) ||
1806 2 : (error = col_add_bool_property(level1, NULL, "bool3", 1)) ||
1807 2 : (error = col_add_bool_property(level1, NULL, "bool2", 1)) ||
1808 2 : (error = col_add_bool_property(level1, NULL, "bool1", 0)) ||
1809 2 : (error = col_add_unsigned_property(level1, NULL, "unsigned1", 2)) ||
1810 2 : (error = col_add_unsigned_property(level1, NULL, "unsigned3", 1)) ||
1811 2 : (error = col_add_unsigned_property(level1, NULL, "unsigned2", 3)) ||
1812 2 : (error = col_add_long_property(level1, NULL, "long3", 1)) ||
1813 2 : (error = col_add_long_property(level1, NULL, "long2", 2)) ||
1814 2 : (error = col_add_long_property(level1, NULL, "long1", 3)) ||
1815 2 : (error = col_add_ulong_property(level1, NULL, "ulong1", 2)) ||
1816 2 : (error = col_add_ulong_property(level1, NULL, "ulong3", 1)) ||
1817 2 : (error = col_add_ulong_property(level1, NULL, "ulong2", 3)) ||
1818 2 : (error = col_add_double_property(level1, NULL, "double1", 2.2)) ||
1819 2 : (error = col_add_double_property(level1, NULL, "double3", 1.1)) ||
1820 2 : (error = col_add_double_property(level1, NULL, "double2", 3.3)) ||
1821 2 : (error = col_add_int_property(level3, NULL, "int3L3", 1)) ||
1822 2 : (error = col_add_int_property(level3, NULL, "int2L3", 2)) ||
1823 2 : (error = col_add_int_property(level3, NULL, "int1L3", 3)) ||
1824 2 : (error = col_add_unsigned_property(level1, "level2a!level3", "unsigned1L3", 2)) ||
1825 2 : (error = col_add_unsigned_property(level1, "level2a!level3", "unsigned3L3", 1)) ||
1826 2 : (error = col_add_unsigned_property(level1, "level2a!level3", "unsigned2L3", 3)) ||
1827 2 : (error = col_add_long_property(level1, "level2b!level3", "long3L3", 1)) ||
1828 2 : (error = col_add_long_property(level1, "level2b!level3", "long2L3", 2)) ||
1829 2 : (error = col_add_long_property(level1, "level2b!level3", "long1L3", 3)) ||
1830 2 : (error = col_add_ulong_property(level1, "level3", "ulong1L3", 2)) ||
1831 2 : (error = col_add_ulong_property(level1, "level3", "ulong3L3", 1)) ||
1832 2 : (error = col_add_ulong_property(level1, "level3", "ulong2L3", 3)) ||
1833 2 : (error = col_add_bool_property(level3, NULL, "bool3", 1)) ||
1834 2 : (error = col_add_bool_property(level3, NULL, "bool2", 1)) ||
1835 2 : (error = col_add_bool_property(level3, NULL, "bool1", 0)) ||
1836 2 : (error = col_add_double_property(level3, NULL, "double1L3", 2.2)) ||
1837 2 : (error = col_add_double_property(level3, NULL, "double3L3", 1.1)) ||
1838 1 : (error = col_add_double_property(level3, NULL, "double2L3", 3.3))) {
1839 0 : col_destroy_collection(level1);
1840 0 : col_destroy_collection(level2a);
1841 0 : col_destroy_collection(level2b);
1842 0 : col_destroy_collection(level3);
1843 0 : printf("Failed to build test. Error %d\n", error);
1844 0 : return error;
1845 : }
1846 :
1847 1 : COLOUT(printf("\nUNSORTED COLLECTION\n\n"));
1848 1 : COLOUT(col_debug_collection(level1, COL_TRAVERSE_DEFAULT));
1849 :
1850 1 : error = col_sort_collection(level1, COL_CMPIN_PROP_EQU, COL_SORT_SUB | COL_SORT_MYSUB);
1851 1 : if (error) {
1852 0 : col_destroy_collection(level1);
1853 0 : col_destroy_collection(level2a);
1854 0 : col_destroy_collection(level2b);
1855 0 : col_destroy_collection(level3);
1856 0 : printf("Failed sort. Error %d\n", error);
1857 0 : return error;
1858 : }
1859 :
1860 1 : COLOUT(printf("\nSORTED BUT SKIPPING REFERENCES\n\n"));
1861 1 : COLOUT(col_debug_collection(level1, COL_TRAVERSE_DEFAULT));
1862 :
1863 1 : error = col_sort_collection(level1, COL_CMPIN_PROP_EQU, COL_SORT_SUB);
1864 1 : if (error) {
1865 0 : col_destroy_collection(level1);
1866 0 : col_destroy_collection(level2a);
1867 0 : col_destroy_collection(level2b);
1868 0 : col_destroy_collection(level3);
1869 0 : printf("Failed sort. Error %d\n", error);
1870 0 : return error;
1871 : }
1872 :
1873 1 : COLOUT(printf("\nSORTED BUT NOT SKIPPING REFERENCES\n\n"));
1874 1 : COLOUT(col_debug_collection(level1, COL_TRAVERSE_DEFAULT));
1875 :
1876 1 : error = col_sort_collection(level1, COL_CMPIN_DATA_LEN, COL_SORT_SUB | COL_SORT_DESC);
1877 1 : if (error) {
1878 0 : col_destroy_collection(level1);
1879 0 : col_destroy_collection(level2a);
1880 0 : col_destroy_collection(level2b);
1881 0 : col_destroy_collection(level3);
1882 0 : printf("Failed sort. Error %d\n", error);
1883 0 : return error;
1884 : }
1885 :
1886 1 : COLOUT(printf("\nSORTED DESC NOT SKIPPING BY LENGTH OF DATA\n\n"));
1887 1 : COLOUT(col_debug_collection(level1, COL_TRAVERSE_DEFAULT));
1888 :
1889 1 : error = col_sort_collection(level1, COL_CMPIN_PROP_LEN, COL_SORT_SUB | COL_SORT_DESC);
1890 1 : if (error) {
1891 0 : col_destroy_collection(level1);
1892 0 : col_destroy_collection(level2a);
1893 0 : col_destroy_collection(level2b);
1894 0 : col_destroy_collection(level3);
1895 0 : printf("Failed sort. Error %d\n", error);
1896 0 : return error;
1897 : }
1898 :
1899 1 : COLOUT(printf("\nSORTED DESC NOT SKIPPING BY LENGTH OF PROPERTY\n\n"));
1900 1 : COLOUT(col_debug_collection(level1, COL_TRAVERSE_DEFAULT));
1901 :
1902 1 : error = col_sort_collection(level1, COL_CMPIN_DATA, COL_SORT_SUB | COL_SORT_DESC);
1903 1 : if (error) {
1904 0 : col_destroy_collection(level1);
1905 0 : col_destroy_collection(level2a);
1906 0 : col_destroy_collection(level2b);
1907 0 : col_destroy_collection(level3);
1908 0 : printf("Failed sort. Error %d\n", error);
1909 0 : return error;
1910 : }
1911 :
1912 1 : COLOUT(printf("\nSORTED DESC NOT SKIPPING BY DATA\n\n"));
1913 1 : COLOUT(col_debug_collection(level1, COL_TRAVERSE_DEFAULT));
1914 :
1915 1 : col_destroy_collection(level1);
1916 1 : col_destroy_collection(level2a);
1917 1 : col_destroy_collection(level2b);
1918 1 : col_destroy_collection(level3);
1919 :
1920 1 : COLOUT(printf("\n\n==== SORT TEST END ====\n\n"));
1921 :
1922 1 : return EOK;
1923 : }
1924 :
1925 : /* Main function of the unit test */
1926 :
1927 1 : int main(int argc, char *argv[])
1928 : {
1929 1 : int error = 0;
1930 1 : test_fn tests[] = { ref_collection_test,
1931 : single_collection_test,
1932 : add_collection_test,
1933 : mixed_collection_test,
1934 : iterator_test,
1935 : insert_extract_test,
1936 : delete_test,
1937 : search_test,
1938 : sort_test,
1939 : NULL };
1940 : test_fn t;
1941 1 : int i = 0;
1942 :
1943 1 : if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = 1;
1944 :
1945 1 : printf("Start\n");
1946 :
1947 1 : while ((t = tests[i++])) {
1948 9 : error = t();
1949 9 : if (error) {
1950 0 : printf("Failed!\n");
1951 0 : return error;
1952 : }
1953 : }
1954 :
1955 1 : printf("Success!\n");
1956 1 : return 0;
1957 :
1958 : }
|