1 #include "ejdb2.h"
2 #include "jbl.h"
3 #include "jbl_internal.h"
4 #include <CUnit/Basic.h>
5
6 #define INT64_FORMAT PRId64
7 #define UINT64_FORMAT PRIu64
8 #define INT64_HEX_FORMAT PRIx64
9
10 typedef unsigned short int u16;
11 typedef unsigned int u32;
12 typedef unsigned long long int u64;
13
init_suite(void)14 int init_suite(void) {
15 int rc = ejdb_init();
16 return rc;
17 }
18
clean_suite(void)19 int clean_suite(void) {
20 return 0;
21 }
22
CalcAllocation(int needed_size,int alloc_size)23 static int CalcAllocation(int needed_size, int alloc_size) {
24 int calc_size;
25 calc_size = alloc_size;
26 while (calc_size < needed_size) {
27 calc_size <<= 1; // same as *= 2
28 //calc_size += CHUNK_SIZE; -- this is slower than the above line, because there are more reallocations
29 }
30 return calc_size;
31 }
32
CheckAllocation(binn * item,int add_size)33 static BOOL CheckAllocation(binn *item, int add_size) {
34 int alloc_size;
35 void *ptr;
36 if (item->used_size + add_size > item->alloc_size) {
37 if (item->pre_allocated) {
38 return FALSE;
39 }
40 alloc_size = CalcAllocation(item->used_size + add_size, item->alloc_size);
41 ptr = realloc(item->pbuf, alloc_size);
42 if (ptr == NULL) {
43 return FALSE;
44 }
45 item->pbuf = ptr;
46 item->alloc_size = alloc_size;
47 }
48 return TRUE;
49 }
50
51 #define BINN_MAGIC 0x1F22B11F
52
53 #define MAX_BINN_HEADER 9 // [1:type][4:size][4:count]
54 #define MIN_BINN_SIZE 3 // [1:type][1:size][1:count]
55 #define CHUNK_SIZE 256 // 1024
56
57 extern void*(*malloc_fn)(int len);
58 extern void* (*realloc_fn)(void *ptr, int len);
59 extern void (*free_fn)(void *ptr);
60
61 /*************************************************************************************/
62
63 typedef unsigned short int u16;
64 typedef unsigned int u32;
65 typedef unsigned long long int u64;
66
67 /***************************************************************************/
68
memdup(void * src,int size)69 void *memdup(void *src, int size) {
70 void *dest;
71 if ((src == NULL) || (size <= 0)) {
72 return NULL;
73 }
74 dest = malloc(size);
75 if (dest == NULL) {
76 return NULL;
77 }
78 memcpy(dest, src, size);
79 return dest;
80 }
81
82 /***************************************************************************/
83
i64toa(int64 value,char * buf,int radix)84 char *i64toa(int64 value, char *buf, int radix) {
85 #ifdef _MSC_VER
86 return _i64toa(value, buf, radix);
87 #else
88 switch (radix) {
89 case 10:
90 snprintf(buf, 64, "%" INT64_FORMAT, value);
91 break;
92 case 16:
93 snprintf(buf, 64, "%" INT64_HEX_FORMAT, value);
94 break;
95 default:
96 buf[0] = 0;
97 }
98 return buf;
99 #endif
100 }
101
102 /*************************************************************************************/
103
pass_int64(int64 a)104 void pass_int64(int64 a) {
105
106 CU_ASSERT(a == 9223372036854775807);
107 CU_ASSERT(a > 9223372036854775806);
108 }
109
return_int64()110 int64 return_int64() {
111
112 return 9223372036854775807;
113 }
114
return_passed_int64(int64 a)115 int64 return_passed_int64(int64 a) {
116
117 return a;
118 }
119
120 /*************************************************************************************/
121
test_int64()122 void test_int64() {
123 int64 i64;
124 //uint64 b;
125 //long long int b; -- did not work!
126 char buf[64];
127
128 printf("testing int64... ");
129
130 pass_int64(9223372036854775807);
131
132 i64 = return_int64();
133 CU_ASSERT(i64 == 9223372036854775807);
134
135 /* do not worked!
136 b = 9223372036854775807;
137 printf("value of b1=%" G_GINT64_FORMAT "\n", b);
138 snprintf(64, buf, "%" G_GINT64_FORMAT, b);
139 printf(" value of b2=%s\n", buf);
140
141 ltoa(i64, buf, 10);
142 printf(" value of i64=%s\n", buf);
143 */
144
145 i64toa(i64, buf, 10);
146 //printf(" value of i64=%s\n", buf);
147 CU_ASSERT(strcmp(buf, "9223372036854775807") == 0);
148
149 i64 = return_passed_int64(-987654321987654321);
150 CU_ASSERT(i64 == -987654321987654321);
151
152 //snprintf(64, buf, "%" G_GINT64_FORMAT, i64);
153 i64toa(i64, buf, 10);
154 CU_ASSERT(strcmp(buf, "-987654321987654321") == 0);
155
156 printf("OK\n");
157 }
158
159 /*************************************************************************************/
160
161 //! this code may not work on processors that does not use the default float standard
162 // original name: AlmostEqual2sComplement
AlmostEqualFloats(float A,float B,int maxUlps)163 BOOL AlmostEqualFloats(float A, float B, int maxUlps) {
164 int aInt, bInt, intDiff;
165 // Make sure maxUlps is non-negative and small enough that the
166 // default NAN won't compare as equal to anything.
167 CU_ASSERT(maxUlps > 0 && maxUlps < 4 * 1024 * 1024);
168 aInt = *(int*) &A;
169 bInt = *(int*) &B;
170 // Make aInt lexicographically ordered as a twos-complement int
171 if (aInt < 0) {
172 aInt = 0x80000000 - aInt;
173 }
174 if (bInt < 0) {
175 bInt = 0x80000000 - bInt;
176 }
177 intDiff = abs(aInt - bInt);
178 if (intDiff <= maxUlps) {
179 return TRUE;
180 }
181 return FALSE;
182 }
183
184 /*************************************************************************************/
185
186 #define VERYSMALL (1.0E-150)
187 #define EPSILON (1.0E-8)
188
189 #ifndef max
190 #define max(a, b) (((a) > (b)) ? (a) : (b))
191 #endif
192
193 #ifndef min
194 #define min(a, b) (((a) < (b)) ? (a) : (b))
195 #endif
196
AlmostEqualDoubles(double a,double b)197 BOOL AlmostEqualDoubles(double a, double b) {
198 double absDiff, maxAbs, absA, absB;
199
200 absDiff = fabs(a - b);
201 if (absDiff < VERYSMALL) {
202 return TRUE;
203 }
204
205 absA = fabs(a);
206 absB = fabs(b);
207 maxAbs = max(absA, absB);
208 if ((absDiff / maxAbs) < EPSILON) {
209 return TRUE;
210 }
211 printf("a=%g b=%g\n", a, b);
212 return FALSE;
213 }
214
215 /*************************************************************************************/
216
test_floating_point_numbers()217 void test_floating_point_numbers() {
218 char buf[256];
219 float f1;
220 double d1;
221
222 printf("testing floating point... ");
223
224 f1 = 1.25;
225 CU_ASSERT(f1 == 1.25);
226 d1 = 1.25;
227 CU_ASSERT(d1 == 1.25);
228
229 d1 = 0;
230 d1 = f1;
231 CU_ASSERT(d1 == 1.25);
232 f1 = 0;
233 f1 = d1;
234 CU_ASSERT(f1 == 1.25);
235
236 d1 = 1.234;
237 CU_ASSERT(AlmostEqualDoubles(d1, 1.234) == TRUE);
238 f1 = d1;
239 CU_ASSERT(AlmostEqualFloats(f1, 1.234, 2) == TRUE);
240
241 d1 = 1.2345;
242 CU_ASSERT(AlmostEqualDoubles(d1, 1.2345) == TRUE);
243 f1 = d1;
244 CU_ASSERT(AlmostEqualFloats(f1, 1.2345, 2) == TRUE);
245
246
247 // from string to number, and back to string
248
249 d1 = atof("1.234"); // converts from string to double
250 CU_ASSERT(AlmostEqualDoubles(d1, 1.234) == TRUE);
251 f1 = d1; // converts from double to float
252 CU_ASSERT(AlmostEqualFloats(f1, 1.234, 2) == TRUE);
253
254 /*
255 sprintf(buf, "%f", d1); // from double to string
256 CU_ASSERT(buf[0] != 0);
257 CU_ASSERT(strcmp(buf, "1.234") == 0);
258 */
259
260 sprintf(buf, "%g", d1);
261 CU_ASSERT(buf[0] != 0);
262 CU_ASSERT(strcmp(buf, "1.234") == 0);
263
264
265 d1 = atof("12.34");
266 CU_ASSERT(d1 == 12.34);
267 f1 = d1;
268 CU_ASSERT(AlmostEqualFloats(f1, 12.34, 2) == TRUE);
269
270 /*
271 sprintf(buf, "%f", d1); // from double to string
272 CU_ASSERT(buf[0] != 0);
273 CU_ASSERT(strcmp(buf, "12.34") == 0);
274 */
275
276 sprintf(buf, "%g", d1);
277 CU_ASSERT(buf[0] != 0);
278 CU_ASSERT(strcmp(buf, "12.34") == 0);
279
280
281 d1 = atof("1.234e25");
282 CU_ASSERT(AlmostEqualDoubles(d1, 1.234e25) == TRUE);
283 f1 = d1;
284 CU_ASSERT(AlmostEqualFloats(f1, 1.234e25, 2) == TRUE);
285
286 sprintf(buf, "%g", d1);
287 CU_ASSERT(buf[0] != 0);
288 //printf("\nbuf=%s\n", buf);
289 //CU_ASSERT(strcmp(buf, "1.234e+025") == 0);
290
291
292 printf("OK\n");
293 }
294
295 /*************************************************************************************/
296
test1()297 void test1() {
298 static const int fix_size = 512;
299 int i = 8768787, blobsize;
300 char *ptr, *p2, *ptr2;
301 binn *obj1, *list, *map, *obj; //, *list2=INVALID_BINN, *map2=INVALID_BINN, *obj2=INVALID_BINN;
302 binn value;
303 // test values
304 char vbyte, *pblob;
305 signed short vint16;
306 unsigned short vuint16;
307 signed int vint32;
308 unsigned int vuint32;
309 signed long long int vint64;
310 unsigned long long int vuint64;
311
312 printf("testing binn 1... ");
313
314 // CalcAllocation and CheckAllocation -------------------------------------------------
315
316 CU_ASSERT(CalcAllocation(512, 512) == 512);
317 CU_ASSERT(CalcAllocation(510, 512) == 512);
318 CU_ASSERT(CalcAllocation(1, 512) == 512);
319 CU_ASSERT(CalcAllocation(0, 512) == 512);
320
321 CU_ASSERT(CalcAllocation(513, 512) == 1024);
322 CU_ASSERT(CalcAllocation(512 + CHUNK_SIZE, 512) == 1024);
323 CU_ASSERT(CalcAllocation(1025, 512) == 2048);
324 CU_ASSERT(CalcAllocation(1025, 1024) == 2048);
325 CU_ASSERT(CalcAllocation(2100, 1024) == 4096);
326
327 //CU_ASSERT(CheckAllocation(xxx) == xxx);
328
329
330 // binn_new() ----------------------------------------------------------------------
331
332 // invalid create calls
333 CU_ASSERT(binn_new(-1, 0, NULL) == INVALID_BINN);
334 CU_ASSERT(binn_new(0, 0, NULL) == INVALID_BINN);
335 CU_ASSERT(binn_new(5, 0, NULL) == INVALID_BINN);
336 CU_ASSERT(binn_new(BINN_MAP, -1, NULL) == INVALID_BINN);
337 ptr = (char*) &obj1; // create a valid pointer
338 CU_ASSERT(binn_new(BINN_MAP, -1, ptr) == INVALID_BINN);
339 CU_ASSERT(binn_new(BINN_MAP, MIN_BINN_SIZE - 1, ptr) == INVALID_BINN);
340
341 // first valid create call
342 obj1 = binn_new(BINN_LIST, 0, NULL);
343 CU_ASSERT(obj1 != INVALID_BINN);
344
345 CU_ASSERT(obj1->header == BINN_MAGIC);
346 CU_ASSERT(obj1->type == BINN_LIST);
347 CU_ASSERT(obj1->count == 0);
348 CU_ASSERT(obj1->pbuf != NULL);
349 CU_ASSERT(obj1->alloc_size > MAX_BINN_HEADER);
350 CU_ASSERT(obj1->used_size == MAX_BINN_HEADER);
351 CU_ASSERT(obj1->pre_allocated == FALSE);
352
353 binn_free(obj1);
354
355
356 // valid create call
357 list = binn_new(BINN_LIST, 0, NULL);
358 CU_ASSERT(list != INVALID_BINN);
359
360 // valid create call
361 map = binn_new(BINN_MAP, 0, NULL);
362 CU_ASSERT(map != INVALID_BINN);
363
364 // valid create call
365 obj = binn_new(BINN_OBJECT, 0, NULL);
366 CU_ASSERT(obj != INVALID_BINN);
367
368 CU_ASSERT(list->header == BINN_MAGIC);
369 CU_ASSERT(list->type == BINN_LIST);
370 CU_ASSERT(list->count == 0);
371 CU_ASSERT(list->pbuf != NULL);
372 CU_ASSERT(list->alloc_size > MAX_BINN_HEADER);
373 CU_ASSERT(list->used_size == MAX_BINN_HEADER);
374 CU_ASSERT(list->pre_allocated == FALSE);
375
376 CU_ASSERT(map->header == BINN_MAGIC);
377 CU_ASSERT(map->type == BINN_MAP);
378 CU_ASSERT(map->count == 0);
379 CU_ASSERT(map->pbuf != NULL);
380 CU_ASSERT(map->alloc_size > MAX_BINN_HEADER);
381 CU_ASSERT(map->used_size == MAX_BINN_HEADER);
382 CU_ASSERT(map->pre_allocated == FALSE);
383
384 CU_ASSERT(obj->header == BINN_MAGIC);
385 CU_ASSERT(obj->type == BINN_OBJECT);
386 CU_ASSERT(obj->count == 0);
387 CU_ASSERT(obj->pbuf != NULL);
388 CU_ASSERT(obj->alloc_size > MAX_BINN_HEADER);
389 CU_ASSERT(obj->used_size == MAX_BINN_HEADER);
390 CU_ASSERT(obj->pre_allocated == FALSE);
391
392
393 // test create with pre-allocated buffer ----------------------------------------------
394
395 ptr = malloc(fix_size);
396 CU_ASSERT(ptr != NULL);
397
398 obj1 = binn_new(BINN_OBJECT, fix_size, ptr);
399 CU_ASSERT(obj1 != INVALID_BINN);
400
401 CU_ASSERT(obj1->header == BINN_MAGIC);
402 CU_ASSERT(obj1->type == BINN_OBJECT);
403 CU_ASSERT(obj1->count == 0);
404 CU_ASSERT(obj1->pbuf != NULL);
405 CU_ASSERT(obj1->alloc_size == fix_size);
406 CU_ASSERT(obj1->used_size == MAX_BINN_HEADER);
407 CU_ASSERT(obj1->pre_allocated == TRUE);
408
409
410 // add values - invalid ---------------------------------------------------------------
411
412 CU_ASSERT(binn_map_set(list, 55001, BINN_INT32, &i, 0) == FALSE);
413 CU_ASSERT(binn_object_set(list, "test", BINN_INT32, &i, 0) == FALSE);
414
415 CU_ASSERT(binn_list_add(map, BINN_INT32, &i, 0) == FALSE);
416 CU_ASSERT(binn_object_set(map, "test", BINN_INT32, &i, 0) == FALSE);
417
418 CU_ASSERT(binn_list_add(obj, BINN_INT32, &i, 0) == FALSE);
419 CU_ASSERT(binn_map_set(obj, 55001, BINN_INT32, &i, 0) == FALSE);
420
421 // invalid type
422 CU_ASSERT(binn_list_add(list, -1, &i, 0) == FALSE);
423 CU_ASSERT(binn_list_add(list, 0x1FFFF, &i, 0) == FALSE);
424 CU_ASSERT(binn_map_set(map, 5501, -1, &i, 0) == FALSE);
425 CU_ASSERT(binn_map_set(map, 5501, 0x1FFFF, &i, 0) == FALSE);
426 CU_ASSERT(binn_object_set(obj, "test", -1, &i, 0) == FALSE);
427 CU_ASSERT(binn_object_set(obj, "test", 0x1FFFF, &i, 0) == FALSE);
428
429 // null pointers
430 CU_ASSERT(binn_list_add(list, BINN_INT8, NULL, 0) == FALSE);
431 CU_ASSERT(binn_list_add(list, BINN_INT16, NULL, 0) == FALSE);
432 CU_ASSERT(binn_list_add(list, BINN_INT32, NULL, 0) == FALSE);
433 CU_ASSERT(binn_list_add(list, BINN_INT64, NULL, 0) == FALSE);
434 //CU_ASSERT(binn_list_add(list, BINN_STRING, NULL, 0) == TRUE); //*
435 CU_ASSERT(binn_map_set(map, 5501, BINN_INT8, NULL, 0) == FALSE);
436 CU_ASSERT(binn_map_set(map, 5501, BINN_INT16, NULL, 0) == FALSE);
437 CU_ASSERT(binn_map_set(map, 5501, BINN_INT32, NULL, 0) == FALSE);
438 CU_ASSERT(binn_map_set(map, 5501, BINN_INT64, NULL, 0) == FALSE);
439 //CU_ASSERT(binn_map_set(map, 5501, BINN_STRING, NULL, 0) == TRUE); //*
440 CU_ASSERT(binn_object_set(obj, "test", BINN_INT8, NULL, 0) == FALSE);
441 CU_ASSERT(binn_object_set(obj, "test", BINN_INT16, NULL, 0) == FALSE);
442 CU_ASSERT(binn_object_set(obj, "test", BINN_INT32, NULL, 0) == FALSE);
443 CU_ASSERT(binn_object_set(obj, "test", BINN_INT64, NULL, 0) == FALSE);
444 //CU_ASSERT(binn_object_set(obj, "test", BINN_STRING, NULL, 0) == TRUE); //*
445
446 // blobs with null pointers
447 CU_ASSERT(binn_list_add(list, BINN_BLOB, NULL, -1) == FALSE);
448 CU_ASSERT(binn_list_add(list, BINN_BLOB, NULL, 10) == FALSE);
449 CU_ASSERT(binn_map_set(map, 5501, BINN_BLOB, NULL, -1) == FALSE);
450 CU_ASSERT(binn_map_set(map, 5501, BINN_BLOB, NULL, 10) == FALSE);
451 CU_ASSERT(binn_object_set(obj, "test", BINN_BLOB, NULL, -1) == FALSE);
452 CU_ASSERT(binn_object_set(obj, "test", BINN_BLOB, NULL, 10) == FALSE);
453
454 // blobs with negative values
455 CU_ASSERT(binn_list_add(list, BINN_BLOB, &i, -1) == FALSE);
456 CU_ASSERT(binn_list_add(list, BINN_BLOB, &i, -15) == FALSE);
457 CU_ASSERT(binn_map_set(map, 5501, BINN_BLOB, &i, -1) == FALSE);
458 CU_ASSERT(binn_map_set(map, 5501, BINN_BLOB, &i, -15) == FALSE);
459 CU_ASSERT(binn_object_set(obj, "test", BINN_BLOB, &i, -1) == FALSE);
460 CU_ASSERT(binn_object_set(obj, "test", BINN_BLOB, &i, -15) == FALSE);
461
462
463 // read values - invalid 1 - empty binns -------------------------------------------
464
465 ptr2 = binn_ptr(list);
466 CU_ASSERT(ptr2 != NULL);
467 CU_ASSERT(binn_list_get_value(ptr2, 0, &value) == FALSE);
468 CU_ASSERT(binn_list_get_value(ptr2, 1, &value) == FALSE);
469 CU_ASSERT(binn_list_get_value(ptr2, 2, &value) == FALSE);
470 CU_ASSERT(binn_list_get_value(ptr2, -1, &value) == FALSE);
471
472 ptr2 = binn_ptr(map);
473 CU_ASSERT(ptr2 != NULL);
474 CU_ASSERT(binn_list_get_value(ptr2, 0, &value) == FALSE);
475 CU_ASSERT(binn_list_get_value(ptr2, 1, &value) == FALSE);
476 CU_ASSERT(binn_list_get_value(ptr2, 2, &value) == FALSE);
477 CU_ASSERT(binn_list_get_value(ptr2, -1, &value) == FALSE);
478
479
480 ptr2 = binn_ptr(obj);
481 CU_ASSERT(ptr2 != NULL);
482 CU_ASSERT(binn_list_get_value(ptr2, 0, &value) == FALSE);
483 CU_ASSERT(binn_list_get_value(ptr2, 1, &value) == FALSE);
484 CU_ASSERT(binn_list_get_value(ptr2, 2, &value) == FALSE);
485 CU_ASSERT(binn_list_get_value(ptr2, -1, &value) == FALSE);
486
487 // add values - valid -----------------------------------------------------------------
488
489 CU_ASSERT(binn_list_add(list, BINN_INT32, &i, 0) == TRUE);
490 CU_ASSERT(binn_map_set(map, 5501, BINN_INT32, &i, 0) == TRUE);
491 CU_ASSERT(binn_map_set(map, 5501, BINN_INT32, &i, 0) == FALSE); // with the same ID
492 CU_ASSERT(binn_object_set(obj, "test", BINN_INT32, &i, 0) == TRUE);
493 CU_ASSERT(binn_object_set(obj, "test", BINN_INT32, &i, 0) == FALSE); // with the same name
494
495 vbyte = (char) 255;
496 vint16 = -32000;
497 vuint16 = 65000;
498 vint32 = -65000000;
499 vuint32 = 65000000;
500 vint64 = -6500000000000000;
501 vuint64 = 6500000000000000;
502 blobsize = 150;
503 pblob = malloc(blobsize);
504 CU_ASSERT(pblob != NULL);
505 memset(pblob, 55, blobsize);
506
507 CU_ASSERT(binn_list_add(list, BINN_NULL, 0, 0) == TRUE); // second
508 CU_ASSERT(binn_list_add(list, BINN_UINT8, &vbyte, 0) == TRUE); // third
509 CU_ASSERT(binn_list_add(list, BINN_INT16, &vint16, 0) == TRUE); // fourth
510 CU_ASSERT(binn_list_add(list, BINN_UINT16, &vuint16, 0) == TRUE); // fifth
511 CU_ASSERT(binn_list_add(list, BINN_INT32, &vint32, 0) == TRUE); // 6th
512 CU_ASSERT(binn_list_add(list, BINN_UINT32, &vuint32, 0) == TRUE); // 7th
513 CU_ASSERT(binn_list_add(list, BINN_INT64, &vint64, 0) == TRUE); // 8th
514 CU_ASSERT(binn_list_add(list, BINN_UINT64, &vuint64, 0) == TRUE); // 9th
515 CU_ASSERT(binn_list_add(list, BINN_STRING, "this is the string", 0) == TRUE); // 10th
516 CU_ASSERT(binn_list_add(list, BINN_BLOB, pblob, blobsize) == TRUE); // 11th
517
518 CU_ASSERT(binn_map_set(map, 99000, BINN_NULL, 0, 0) == TRUE); // third
519 CU_ASSERT(binn_map_set(map, 99001, BINN_UINT8, &vbyte, 0) == TRUE); // fourth
520 CU_ASSERT(binn_map_set(map, 99002, BINN_INT16, &vint16, 0) == TRUE); // fifth
521 CU_ASSERT(binn_map_set(map, 99003, BINN_UINT16, &vuint16, 0) == TRUE); // 6th
522 CU_ASSERT(binn_map_set(map, 99004, BINN_INT32, &vint32, 0) == TRUE); // 7th
523 CU_ASSERT(binn_map_set(map, 99005, BINN_UINT32, &vuint32, 0) == TRUE); // 8th
524 CU_ASSERT(binn_map_set(map, 99006, BINN_INT64, &vint64, 0) == TRUE); // 9th
525 CU_ASSERT(binn_map_set(map, 99007, BINN_UINT64, &vuint64, 0) == TRUE); // 10th
526 CU_ASSERT(binn_map_set(map, 99008, BINN_STRING, "this is the string", 0) == TRUE); // 11th
527 CU_ASSERT(binn_map_set(map, 99009, BINN_BLOB, pblob, blobsize) == TRUE); // 12th
528
529 CU_ASSERT(binn_object_set(obj, "key0", BINN_NULL, 0, 0) == TRUE); // third
530 CU_ASSERT(binn_object_set(obj, "key1", BINN_UINT8, &vbyte, 0) == TRUE); // fourth
531 CU_ASSERT(binn_object_set(obj, "key2", BINN_INT16, &vint16, 0) == TRUE); // fifth
532 CU_ASSERT(binn_object_set(obj, "key3", BINN_UINT16, &vuint16, 0) == TRUE); // 6th
533 CU_ASSERT(binn_object_set(obj, "key4", BINN_INT32, &vint32, 0) == TRUE); // 7th
534 CU_ASSERT(binn_object_set(obj, "key5", BINN_UINT32, &vuint32, 0) == TRUE); // 8th
535 CU_ASSERT(binn_object_set(obj, "key6", BINN_INT64, &vint64, 0) == TRUE); // 9th
536 CU_ASSERT(binn_object_set(obj, "key7", BINN_UINT64, &vuint64, 0) == TRUE); // 10th
537 CU_ASSERT(binn_object_set(obj, "key8", BINN_STRING, "this is the string", 0) == TRUE); // 11th
538 CU_ASSERT(binn_object_set(obj, "key9", BINN_BLOB, pblob, blobsize) == TRUE); // 12th
539
540 // blobs with size = 0
541 CU_ASSERT(binn_list_add(list, BINN_BLOB, ptr, 0) == TRUE);
542 CU_ASSERT(binn_list_add(list, BINN_STRING, "", 0) == TRUE);
543 CU_ASSERT(binn_list_add(list, BINN_STRING, "after the empty items", 0) == TRUE);
544
545
546 // add values to a fixed-size binn (pre-allocated buffer) --------------------------
547
548 CU_ASSERT(binn_list_add(obj1, BINN_INT32, &i, 0) == FALSE);
549 CU_ASSERT(binn_map_set(obj1, 55001, BINN_INT32, &i, 0) == FALSE);
550
551 CU_ASSERT(binn_object_set(obj1, "test", BINN_UINT32, &vuint32, 0) == TRUE);
552 CU_ASSERT(binn_object_set(obj1, "test", BINN_UINT32, &vuint32, 0) == FALSE); // with the same name
553
554 CU_ASSERT(binn_object_set(obj1, "key1", BINN_STRING, "this is the value", 0) == TRUE);
555 CU_ASSERT(binn_object_set(obj1, "key2", BINN_STRING, "the second value", 0) == TRUE);
556
557 // create a long string buffer to make the test. the string is longer than the available space
558 // in the binn.
559 ptr2 = malloc(fix_size);
560 CU_ASSERT(ptr2 != NULL);
561 p2 = ptr2;
562 for (i = 0; i < fix_size - 1; i++) {
563 *p2 = 'A';
564 p2++;
565 }
566 *p2 = '\0';
567 CU_ASSERT(strlen(ptr2) == fix_size - 1);
568
569 CU_ASSERT(binn_object_set(obj1, "v2", BINN_STRING, ptr2,
570 0) == FALSE); // it fails because it uses a pre-allocated memory block
571
572 CU_ASSERT(binn_object_set(obj, "v2", BINN_STRING, ptr2,
573 0) == TRUE); // but this uses a dynamically allocated memory block, so it works with it
574 CU_ASSERT(binn_object_set(obj, "Key00", BINN_STRING, "after the big string",
575 0) == TRUE); // and test the 'Key00' against the 'Key0'
576
577 CU_ASSERT(binn_object_set(obj, "list", BINN_LIST, binn_ptr(list), binn_size(list)) == TRUE);
578 CU_ASSERT(binn_object_set(obj, "Key10", BINN_STRING, "after the list",
579 0) == TRUE); // and test the 'Key10' against the 'Key1'
580
581
582 // read values - invalid 2 ------------------------------------------------------------
583
584
585 // read keys --------------------------------------------------------------------------
586
587
588 // binn_size - invalid and valid args --------------------------------------------
589
590 CU_ASSERT(binn_size(NULL) == 0);
591
592 CU_ASSERT(binn_size(list) == list->size);
593 CU_ASSERT(binn_size(map) == map->size);
594 CU_ASSERT(binn_size(obj) == obj->size);
595 CU_ASSERT(binn_size(obj1) == obj1->size);
596
597
598 // destroy them all -------------------------------------------------------------------
599
600 binn_free(list);
601 binn_free(map);
602 binn_free(obj);
603 binn_free(obj1);
604 free(pblob);
605 free(ptr);
606 free(ptr2);
607
608
609 printf("OK\n");
610 }
611
612 /*************************************************************************************/
613
_test2()614 void _test2() {
615 binn *list = INVALID_BINN, *map = INVALID_BINN, *obj = INVALID_BINN;
616 binn value;
617 BOOL vbool;
618 int blobsize;
619 char *pblob, *pstr;
620 signed int vint32;
621 double vdouble;
622
623 char *str_list = "test list";
624 char *str_map = "test map";
625 char *str_obj = "test object";
626
627 printf("testing binn 2");
628
629 blobsize = 150;
630 pblob = malloc(blobsize);
631 CU_ASSERT(pblob != NULL);
632 memset(pblob, 55, blobsize);
633
634 CU_ASSERT(list == INVALID_BINN);
635 CU_ASSERT(map == INVALID_BINN);
636 CU_ASSERT(obj == INVALID_BINN);
637
638 // add values without creating before
639
640 CU_ASSERT(binn_list_add_int32(list, 123) == FALSE);
641 CU_ASSERT(binn_map_set_int32(map, 1001, 456) == FALSE);
642 CU_ASSERT(binn_object_set_int32(obj, "int", 789) == FALSE);
643
644 // create the structures
645
646 list = binn_list();
647 map = binn_map();
648 obj = binn_object();
649
650 CU_ASSERT(list != INVALID_BINN);
651 CU_ASSERT(map != INVALID_BINN);
652 CU_ASSERT(obj != INVALID_BINN);
653
654 // add values without creating before
655
656 CU_ASSERT(binn_list_add_int32(list, 123) == TRUE);
657 CU_ASSERT(binn_map_set_int32(map, 1001, 456) == TRUE);
658 CU_ASSERT(binn_object_set_int32(obj, "int", 789) == TRUE);
659
660 // check the structures
661
662 CU_ASSERT(list->header == BINN_MAGIC);
663 CU_ASSERT(list->type == BINN_LIST);
664 CU_ASSERT(list->count == 1);
665 CU_ASSERT(list->pbuf != NULL);
666 CU_ASSERT(list->alloc_size > MAX_BINN_HEADER);
667 CU_ASSERT(list->used_size > MAX_BINN_HEADER);
668 CU_ASSERT(list->pre_allocated == FALSE);
669
670 CU_ASSERT(map->header == BINN_MAGIC);
671 CU_ASSERT(map->type == BINN_MAP);
672 CU_ASSERT(map->count == 1);
673 CU_ASSERT(map->pbuf != NULL);
674 CU_ASSERT(map->alloc_size > MAX_BINN_HEADER);
675 CU_ASSERT(map->used_size > MAX_BINN_HEADER);
676 CU_ASSERT(map->pre_allocated == FALSE);
677
678 CU_ASSERT(obj->header == BINN_MAGIC);
679 CU_ASSERT(obj->type == BINN_OBJECT);
680 CU_ASSERT(obj->count == 1);
681 CU_ASSERT(obj->pbuf != NULL);
682 CU_ASSERT(obj->alloc_size > MAX_BINN_HEADER);
683 CU_ASSERT(obj->used_size > MAX_BINN_HEADER);
684 CU_ASSERT(obj->pre_allocated == FALSE);
685
686
687 // continue adding values
688
689 CU_ASSERT(binn_list_add_double(list, 1.23) == TRUE);
690 CU_ASSERT(binn_map_set_double(map, 1002, 4.56) == TRUE);
691 CU_ASSERT(binn_object_set_double(obj, "double", 7.89) == TRUE);
692
693 CU_ASSERT(list->count == 2);
694 CU_ASSERT(map->count == 2);
695 CU_ASSERT(obj->count == 2);
696
697 CU_ASSERT(binn_list_add_bool(list, TRUE) == TRUE);
698 CU_ASSERT(binn_map_set_bool(map, 1003, TRUE) == TRUE);
699 CU_ASSERT(binn_object_set_bool(obj, "bool", TRUE) == TRUE);
700
701 CU_ASSERT(list->count == 3);
702 CU_ASSERT(map->count == 3);
703 CU_ASSERT(obj->count == 3);
704
705 CU_ASSERT(binn_list_add_str(list, str_list) == TRUE);
706 CU_ASSERT(binn_map_set_str(map, 1004, str_map) == TRUE);
707 CU_ASSERT(binn_object_set_str(obj, "text", str_obj) == TRUE);
708
709 CU_ASSERT(list->count == 4);
710 CU_ASSERT(map->count == 4);
711 CU_ASSERT(obj->count == 4);
712
713 CU_ASSERT(binn_list_add_blob(list, pblob, blobsize) == TRUE);
714 CU_ASSERT(binn_map_set_blob(map, 1005, pblob, blobsize) == TRUE);
715 CU_ASSERT(binn_object_set_blob(obj, "blob", pblob, blobsize) == TRUE);
716
717 CU_ASSERT(list->count == 5);
718 CU_ASSERT(map->count == 5);
719 CU_ASSERT(obj->count == 5);
720
721 CU_ASSERT(binn_count(list) == 5);
722 CU_ASSERT(binn_count(map) == 5);
723 CU_ASSERT(binn_count(obj) == 5);
724
725 CU_ASSERT(binn_size(list) == list->size);
726 CU_ASSERT(binn_size(map) == map->size);
727 CU_ASSERT(binn_size(obj) == obj->size);
728
729 CU_ASSERT(binn_type(list) == BINN_LIST);
730 CU_ASSERT(binn_type(map) == BINN_MAP);
731 CU_ASSERT(binn_type(obj) == BINN_OBJECT);
732
733
734 // try to read them
735
736 // integer
737
738 CU_ASSERT(binn_list_get_value(list, 1, &value) == TRUE);
739
740 CU_ASSERT(value.header == BINN_MAGIC);
741 CU_ASSERT(value.writable == FALSE);
742 CU_ASSERT(value.allocated == FALSE);
743
744 CU_ASSERT(value.type == BINN_UINT8);
745 CU_ASSERT(value.ptr != &value.vuint8); // it must return a pointer to the byte in the buffer
746
747 CU_ASSERT(value.size == 0);
748 CU_ASSERT(value.count == 0);
749 CU_ASSERT(value.vint == 123);
750
751 memset(&value, 0, sizeof(binn));
752
753 CU_ASSERT(binn_map_get_value(map, 1001, &value) == TRUE);
754
755 CU_ASSERT(value.header == BINN_MAGIC);
756 CU_ASSERT(value.writable == FALSE);
757
758 CU_ASSERT(value.type == BINN_UINT16);
759 CU_ASSERT(value.ptr == &value.vuint16);
760
761 CU_ASSERT(value.size == 0);
762 CU_ASSERT(value.count == 0);
763 CU_ASSERT(value.vint == 456);
764
765 memset(&value, 0, sizeof(binn));
766
767 CU_ASSERT(binn_object_get_value(obj, "int", &value) == TRUE);
768
769 CU_ASSERT(value.header == BINN_MAGIC);
770 CU_ASSERT(value.writable == FALSE);
771
772 CU_ASSERT(value.type == BINN_UINT16);
773 CU_ASSERT(value.ptr == &value.vuint16);
774
775 CU_ASSERT(value.size == 0);
776 CU_ASSERT(value.count == 0);
777 CU_ASSERT(value.vint == 789);
778
779 memset(&value, 0, sizeof(binn));
780
781
782 // double
783
784 CU_ASSERT(binn_list_get_value(list, 2, &value) == TRUE);
785
786 CU_ASSERT(value.header == BINN_MAGIC);
787 CU_ASSERT(value.writable == FALSE);
788 CU_ASSERT(value.type == BINN_FLOAT64);
789 CU_ASSERT(value.ptr == &value.vint);
790 CU_ASSERT(value.size == 0);
791 CU_ASSERT(value.count == 0);
792 CU_ASSERT(value.vdouble == 1.23);
793
794 memset(&value, 0, sizeof(binn));
795
796 CU_ASSERT(binn_map_get_value(map, 1002, &value) == TRUE);
797
798 CU_ASSERT(value.header == BINN_MAGIC);
799 CU_ASSERT(value.writable == FALSE);
800 CU_ASSERT(value.type == BINN_FLOAT64);
801 CU_ASSERT(value.ptr == &value.vint);
802 CU_ASSERT(value.size == 0);
803 CU_ASSERT(value.count == 0);
804 CU_ASSERT(value.vdouble == 4.56);
805
806 memset(&value, 0, sizeof(binn));
807
808 CU_ASSERT(binn_object_get_value(obj, "double", &value) == TRUE);
809
810 CU_ASSERT(value.header == BINN_MAGIC);
811 CU_ASSERT(value.writable == FALSE);
812 CU_ASSERT(value.type == BINN_FLOAT64);
813 CU_ASSERT(value.ptr == &value.vint);
814 CU_ASSERT(value.size == 0);
815 CU_ASSERT(value.count == 0);
816 CU_ASSERT(value.vdouble == 7.89);
817
818 memset(&value, 0, sizeof(binn));
819
820
821 // bool
822
823 CU_ASSERT(binn_list_get_value(list, 3, &value) == TRUE);
824
825 CU_ASSERT(value.header == BINN_MAGIC);
826 CU_ASSERT(value.writable == FALSE);
827 CU_ASSERT(value.type == BINN_BOOL);
828 CU_ASSERT(value.ptr == &value.vint);
829 CU_ASSERT(value.size == 0);
830 CU_ASSERT(value.count == 0);
831 CU_ASSERT(value.vbool == TRUE);
832
833 memset(&value, 0, sizeof(binn));
834
835 CU_ASSERT(binn_map_get_value(map, 1003, &value) == TRUE);
836
837 CU_ASSERT(value.header == BINN_MAGIC);
838 CU_ASSERT(value.writable == FALSE);
839 CU_ASSERT(value.type == BINN_BOOL);
840 CU_ASSERT(value.ptr == &value.vint);
841 CU_ASSERT(value.size == 0);
842 CU_ASSERT(value.count == 0);
843 CU_ASSERT(value.vbool == TRUE);
844
845 CU_ASSERT(binn_object_get_value(obj, "bool", &value) == TRUE);
846
847 CU_ASSERT(value.header == BINN_MAGIC);
848 CU_ASSERT(value.writable == FALSE);
849 CU_ASSERT(value.type == BINN_BOOL);
850 CU_ASSERT(value.ptr == &value.vint);
851 CU_ASSERT(value.size == 0);
852 CU_ASSERT(value.count == 0);
853 CU_ASSERT(value.vbool == TRUE);
854
855 memset(&value, 0, sizeof(binn));
856
857
858 // string
859
860 CU_ASSERT(binn_list_get_value(list, 4, &value) == TRUE);
861
862 CU_ASSERT(value.header == BINN_MAGIC);
863 CU_ASSERT(value.writable == FALSE);
864 CU_ASSERT(value.type == BINN_STRING);
865 CU_ASSERT(value.ptr != 0);
866 CU_ASSERT(value.size == strlen(str_list));
867 CU_ASSERT(strcmp(value.ptr, str_list) == 0);
868 CU_ASSERT(value.count == 0);
869
870 memset(&value, 0, sizeof(binn));
871
872 CU_ASSERT(binn_map_get_value(map, 1004, &value) == TRUE);
873
874 CU_ASSERT(value.header == BINN_MAGIC);
875 CU_ASSERT(value.writable == FALSE);
876 CU_ASSERT(value.type == BINN_STRING);
877 CU_ASSERT(value.size == strlen(str_map));
878 CU_ASSERT(strcmp(value.ptr, str_map) == 0);
879 CU_ASSERT(value.count == 0);
880
881 memset(&value, 0, sizeof(binn));
882
883 CU_ASSERT(binn_object_get_value(obj, "text", &value) == TRUE);
884
885 CU_ASSERT(value.header == BINN_MAGIC);
886 CU_ASSERT(value.writable == FALSE);
887 CU_ASSERT(value.type == BINN_STRING);
888 CU_ASSERT(value.size == strlen(str_obj));
889 CU_ASSERT(strcmp(value.ptr, str_obj) == 0);
890 CU_ASSERT(value.count == 0);
891
892 memset(&value, 0, sizeof(binn));
893
894
895 // blob
896
897 CU_ASSERT(binn_list_get_value(list, 5, &value) == TRUE);
898
899 CU_ASSERT(value.header == BINN_MAGIC);
900 CU_ASSERT(value.writable == FALSE);
901 CU_ASSERT(value.type == BINN_BLOB);
902 CU_ASSERT(value.ptr != 0);
903 CU_ASSERT(value.size == blobsize);
904 CU_ASSERT(memcmp(value.ptr, pblob, blobsize) == 0);
905 CU_ASSERT(value.count == 0);
906
907 memset(&value, 0, sizeof(binn));
908
909 CU_ASSERT(binn_map_get_value(map, 1005, &value) == TRUE);
910
911 CU_ASSERT(value.header == BINN_MAGIC);
912 CU_ASSERT(value.writable == FALSE);
913 CU_ASSERT(value.type == BINN_BLOB);
914 CU_ASSERT(value.ptr != 0);
915 CU_ASSERT(value.size == blobsize);
916 CU_ASSERT(memcmp(value.ptr, pblob, blobsize) == 0);
917 CU_ASSERT(value.count == 0);
918
919 memset(&value, 0, sizeof(binn));
920
921 CU_ASSERT(binn_object_get_value(obj, "blob", &value) == TRUE);
922
923 CU_ASSERT(value.header == BINN_MAGIC);
924 CU_ASSERT(value.writable == FALSE);
925 CU_ASSERT(value.type == BINN_BLOB);
926 CU_ASSERT(value.ptr != 0);
927 CU_ASSERT(value.size == blobsize);
928 CU_ASSERT(memcmp(value.ptr, pblob, blobsize) == 0);
929 CU_ASSERT(value.count == 0);
930
931 memset(&value, 0, sizeof(binn));
932
933
934 // read with other interface
935
936 CU_ASSERT(binn_list_get_int32(list, 1, &vint32) == TRUE);
937 CU_ASSERT(vint32 == 123);
938
939 CU_ASSERT(binn_map_get_int32(map, 1001, &vint32) == TRUE);
940 CU_ASSERT(vint32 == 456);
941
942 CU_ASSERT(binn_object_get_int32(obj, "int", &vint32) == TRUE);
943 CU_ASSERT(vint32 == 789);
944
945 // double
946
947 CU_ASSERT(binn_list_get_double(list, 2, &vdouble) == TRUE);
948 CU_ASSERT(vdouble == 1.23);
949
950 CU_ASSERT(binn_map_get_double(map, 1002, &vdouble) == TRUE);
951 CU_ASSERT(vdouble == 4.56);
952
953 CU_ASSERT(binn_object_get_double(obj, "double", &vdouble) == TRUE);
954 CU_ASSERT(vdouble == 7.89);
955
956 // bool
957
958 CU_ASSERT(binn_list_get_bool(list, 3, &vbool) == TRUE);
959 CU_ASSERT(vbool == TRUE);
960
961 CU_ASSERT(binn_map_get_bool(map, 1003, &vbool) == TRUE);
962 CU_ASSERT(vbool == TRUE);
963
964 CU_ASSERT(binn_object_get_bool(obj, "bool", &vbool) == TRUE);
965 CU_ASSERT(vbool == TRUE);
966
967 // string
968
969 CU_ASSERT(binn_list_get_str(list, 4, &pstr) == TRUE);
970 CU_ASSERT(pstr != 0);
971 CU_ASSERT(strcmp(pstr, str_list) == 0);
972
973 CU_ASSERT(binn_map_get_str(map, 1004, &pstr) == TRUE);
974 CU_ASSERT(pstr != 0);
975 CU_ASSERT(strcmp(pstr, str_map) == 0);
976
977 CU_ASSERT(binn_object_get_str(obj, "text", &pstr) == TRUE);
978 CU_ASSERT(pstr != 0);
979 CU_ASSERT(strcmp(pstr, str_obj) == 0);
980
981 // blob
982
983 value.ptr = 0;
984 value.size = 0;
985 CU_ASSERT(binn_list_get_blob(list, 5, &value.ptr, &value.size) == TRUE);
986 CU_ASSERT(value.ptr != 0);
987 CU_ASSERT(value.size == blobsize);
988 CU_ASSERT(memcmp(value.ptr, pblob, blobsize) == 0);
989
990 value.ptr = 0;
991 value.size = 0;
992 CU_ASSERT(binn_map_get_blob(map, 1005, &value.ptr, &value.size) == TRUE);
993 CU_ASSERT(value.ptr != 0);
994 CU_ASSERT(value.size == blobsize);
995 CU_ASSERT(memcmp(value.ptr, pblob, blobsize) == 0);
996
997 value.ptr = 0;
998 value.size = 0;
999 CU_ASSERT(binn_object_get_blob(obj, "blob", &value.ptr, &value.size) == TRUE);
1000 CU_ASSERT(value.ptr != 0);
1001 CU_ASSERT(value.size == blobsize);
1002 CU_ASSERT(memcmp(value.ptr, pblob, blobsize) == 0);
1003
1004
1005 // read with other interface
1006
1007 CU_ASSERT(binn_list_int32(list, 1) == 123);
1008 CU_ASSERT(binn_map_int32(map, 1001) == 456);
1009 CU_ASSERT(binn_object_int32(obj, "int") == 789);
1010
1011 // double
1012
1013 CU_ASSERT(binn_list_double(list, 2) == 1.23);
1014 CU_ASSERT(binn_map_double(map, 1002) == 4.56);
1015 CU_ASSERT(binn_object_double(obj, "double") == 7.89);
1016
1017 // bool
1018
1019 CU_ASSERT(binn_list_bool(list, 3) == TRUE);
1020 CU_ASSERT(binn_map_bool(map, 1003) == TRUE);
1021 CU_ASSERT(binn_object_bool(obj, "bool") == TRUE);
1022
1023 // string
1024
1025 pstr = binn_list_str(list, 4);
1026 CU_ASSERT(pstr != 0);
1027 CU_ASSERT(strcmp(pstr, str_list) == 0);
1028
1029 pstr = binn_map_str(map, 1004);
1030 CU_ASSERT(pstr != 0);
1031 CU_ASSERT(strcmp(pstr, str_map) == 0);
1032
1033 pstr = binn_object_str(obj, "text");
1034 CU_ASSERT(pstr != 0);
1035 CU_ASSERT(strcmp(pstr, str_obj) == 0);
1036
1037 // blob
1038
1039 value.ptr = binn_list_blob(list, 5, &value.size);
1040 CU_ASSERT(value.ptr != 0);
1041 CU_ASSERT(value.size == blobsize);
1042 CU_ASSERT(memcmp(value.ptr, pblob, blobsize) == 0);
1043
1044 value.ptr = binn_map_blob(map, 1005, &value.size);
1045 CU_ASSERT(value.ptr != 0);
1046 CU_ASSERT(value.size == blobsize);
1047 CU_ASSERT(memcmp(value.ptr, pblob, blobsize) == 0);
1048
1049 value.ptr = binn_object_blob(obj, "blob", &value.size);
1050 CU_ASSERT(value.ptr != 0);
1051 CU_ASSERT(value.size == blobsize);
1052 CU_ASSERT(memcmp(value.ptr, pblob, blobsize) == 0);
1053
1054
1055 binn_free(list);
1056 binn_free(map);
1057 binn_free(obj);
1058
1059
1060 free(pblob);
1061
1062 printf("OK\n");
1063 }
1064
test2()1065 void test2() {
1066 _test2();
1067 }
1068
1069 /*************************************************************************************/
1070
test4()1071 void test4() {
1072 static const int fix_size = 512;
1073 int i, id, type, count, size, header_size, blobsize;
1074 char *ptr, *p2, *pstr, key[256];
1075 binn *list, *map, *obj, *obj1;
1076 binn value;
1077 // test values
1078 char vbyte, *pblob;
1079 signed short vint16, *pint16;
1080 unsigned short vuint16, *puint16;
1081 signed int vint32, *pint32;
1082 unsigned int vuint32, *puint32;
1083 signed long long int vint64, *pint64;
1084 unsigned long long int vuint64, *puint64;
1085
1086 printf("testing binn 3... ");
1087
1088 list = binn_list();
1089 CU_ASSERT(list != INVALID_BINN);
1090
1091 map = binn_map();
1092 CU_ASSERT(map != INVALID_BINN);
1093
1094 obj = binn_object();
1095 CU_ASSERT(obj != INVALID_BINN);
1096
1097 CU_ASSERT(list->header == BINN_MAGIC);
1098 CU_ASSERT(list->type == BINN_LIST);
1099 CU_ASSERT(list->count == 0);
1100 CU_ASSERT(list->pbuf != NULL);
1101 CU_ASSERT(list->alloc_size > MAX_BINN_HEADER);
1102 CU_ASSERT(list->used_size == MAX_BINN_HEADER);
1103 CU_ASSERT(list->pre_allocated == FALSE);
1104
1105 CU_ASSERT(map->header == BINN_MAGIC);
1106 CU_ASSERT(map->type == BINN_MAP);
1107 CU_ASSERT(map->count == 0);
1108 CU_ASSERT(map->pbuf != NULL);
1109 CU_ASSERT(map->alloc_size > MAX_BINN_HEADER);
1110 CU_ASSERT(map->used_size == MAX_BINN_HEADER);
1111 CU_ASSERT(map->pre_allocated == FALSE);
1112
1113 CU_ASSERT(obj->header == BINN_MAGIC);
1114 CU_ASSERT(obj->type == BINN_OBJECT);
1115 CU_ASSERT(obj->count == 0);
1116 CU_ASSERT(obj->pbuf != NULL);
1117 CU_ASSERT(obj->alloc_size > MAX_BINN_HEADER);
1118 CU_ASSERT(obj->used_size == MAX_BINN_HEADER);
1119 CU_ASSERT(obj->pre_allocated == FALSE);
1120
1121
1122 // test create with pre-allocated buffer ----------------------------------------------
1123
1124 ptr = malloc(fix_size);
1125 CU_ASSERT(ptr != NULL);
1126
1127 obj1 = binn_new(BINN_OBJECT, fix_size, ptr);
1128 CU_ASSERT(obj1 != INVALID_BINN);
1129
1130 CU_ASSERT(obj1->header == BINN_MAGIC);
1131 CU_ASSERT(obj1->type == BINN_OBJECT);
1132 CU_ASSERT(obj1->count == 0);
1133 CU_ASSERT(obj1->pbuf != NULL);
1134 CU_ASSERT(obj1->alloc_size == fix_size);
1135 CU_ASSERT(obj1->used_size == MAX_BINN_HEADER);
1136 CU_ASSERT(obj1->pre_allocated == TRUE);
1137
1138
1139 // add values - invalid ---------------------------------------------------------------
1140
1141
1142 // read values - invalid 1 - empty binns -------------------------------------------
1143
1144 ptr = binn_ptr(list);
1145 CU_ASSERT(ptr != NULL);
1146 CU_ASSERT(binn_list_read(ptr, 0, &type, &size) == NULL);
1147 CU_ASSERT(binn_list_read(ptr, 1, &type, &size) == NULL);
1148 CU_ASSERT(binn_list_read(ptr, 2, &type, &size) == NULL);
1149 CU_ASSERT(binn_list_read(ptr, -1, &type, &size) == NULL);
1150
1151 ptr = binn_ptr(map);
1152 CU_ASSERT(ptr != NULL);
1153 CU_ASSERT(binn_map_read(ptr, 0, &type, &size) == NULL);
1154 CU_ASSERT(binn_map_read(ptr, 55001, &type, &size) == NULL);
1155 CU_ASSERT(binn_map_read(ptr, -1, &type, &size) == NULL);
1156
1157 ptr = binn_ptr(obj);
1158 CU_ASSERT(ptr != NULL);
1159 CU_ASSERT(binn_object_read(ptr, NULL, &type, &size) == NULL);
1160 CU_ASSERT(binn_object_read(ptr, "", &type, &size) == NULL);
1161 CU_ASSERT(binn_object_read(ptr, "test", &type, &size) == NULL);
1162
1163
1164 // add values - valid -----------------------------------------------------------------
1165
1166 CU_ASSERT(binn_list_add(list, BINN_INT32, &i, 0) == TRUE);
1167 CU_ASSERT(binn_map_set(map, 5501, BINN_INT32, &i, 0) == TRUE);
1168 CU_ASSERT(binn_map_set(map, 5501, BINN_INT32, &i, 0) == FALSE); // with the same ID
1169 CU_ASSERT(binn_object_set(obj, "test", BINN_INT32, &i, 0) == TRUE);
1170 CU_ASSERT(binn_object_set(obj, "test", BINN_INT32, &i, 0) == FALSE); // with the same name
1171
1172 vbyte = (char) 255;
1173 vint16 = -32000;
1174 vuint16 = 65000;
1175 vint32 = -65000000;
1176 vuint32 = 65000000;
1177 vint64 = -6500000000000000;
1178 vuint64 = 6500000000000000;
1179 blobsize = 150;
1180 pblob = malloc(blobsize);
1181 CU_ASSERT(pblob != NULL);
1182 memset(pblob, 55, blobsize);
1183
1184 CU_ASSERT(binn_list_add(list, BINN_NULL, 0, 0) == TRUE); // second
1185 CU_ASSERT(binn_list_add(list, BINN_UINT8, &vbyte, 0) == TRUE); // third
1186 CU_ASSERT(binn_list_add(list, BINN_INT16, &vint16, 0) == TRUE); // fourth
1187 CU_ASSERT(binn_list_add(list, BINN_UINT16, &vuint16, 0) == TRUE); // fifth
1188 CU_ASSERT(binn_list_add(list, BINN_INT32, &vint32, 0) == TRUE); // 6th
1189 CU_ASSERT(binn_list_add(list, BINN_UINT32, &vuint32, 0) == TRUE); // 7th
1190 CU_ASSERT(binn_list_add(list, BINN_INT64, &vint64, 0) == TRUE); // 8th
1191 CU_ASSERT(binn_list_add(list, BINN_UINT64, &vuint64, 0) == TRUE); // 9th
1192 CU_ASSERT(binn_list_add(list, BINN_STRING, "this is the string", 0) == TRUE); // 10th
1193 CU_ASSERT(binn_list_add(list, BINN_BLOB, pblob, blobsize) == TRUE); // 11th
1194
1195 CU_ASSERT(binn_map_set(map, 99000, BINN_NULL, 0, 0) == TRUE); // third
1196 CU_ASSERT(binn_map_set(map, 99001, BINN_UINT8, &vbyte, 0) == TRUE); // fourth
1197 CU_ASSERT(binn_map_set(map, 99002, BINN_INT16, &vint16, 0) == TRUE); // fifth
1198 CU_ASSERT(binn_map_set(map, 99003, BINN_UINT16, &vuint16, 0) == TRUE); // 6th
1199 CU_ASSERT(binn_map_set(map, 99004, BINN_INT32, &vint32, 0) == TRUE); // 7th
1200 CU_ASSERT(binn_map_set(map, 99005, BINN_UINT32, &vuint32, 0) == TRUE); // 8th
1201 CU_ASSERT(binn_map_set(map, 99006, BINN_INT64, &vint64, 0) == TRUE); // 9th
1202 CU_ASSERT(binn_map_set(map, 99007, BINN_UINT64, &vuint64, 0) == TRUE); // 10th
1203 CU_ASSERT(binn_map_set(map, 99008, BINN_STRING, "this is the string", 0) == TRUE); // 11th
1204 CU_ASSERT(binn_map_set(map, 99009, BINN_BLOB, pblob, blobsize) == TRUE); // 12th
1205
1206 CU_ASSERT(binn_object_set(obj, "key0", BINN_NULL, 0, 0) == TRUE); // third
1207 CU_ASSERT(binn_object_set(obj, "key1", BINN_UINT8, &vbyte, 0) == TRUE); // fourth
1208 CU_ASSERT(binn_object_set(obj, "key2", BINN_INT16, &vint16, 0) == TRUE); // fifth
1209 CU_ASSERT(binn_object_set(obj, "key3", BINN_UINT16, &vuint16, 0) == TRUE); // 6th
1210 CU_ASSERT(binn_object_set(obj, "key4", BINN_INT32, &vint32, 0) == TRUE); // 7th
1211 CU_ASSERT(binn_object_set(obj, "key5", BINN_UINT32, &vuint32, 0) == TRUE); // 8th
1212 CU_ASSERT(binn_object_set(obj, "key6", BINN_INT64, &vint64, 0) == TRUE); // 9th
1213 CU_ASSERT(binn_object_set(obj, "key7", BINN_UINT64, &vuint64, 0) == TRUE); // 10th
1214 CU_ASSERT(binn_object_set(obj, "key8", BINN_STRING, "this is the string", 0) == TRUE); // 11th
1215 CU_ASSERT(binn_object_set(obj, "key9", BINN_BLOB, pblob, blobsize) == TRUE); // 12th
1216
1217 // blobs with size = 0
1218 CU_ASSERT(binn_list_add(list, BINN_BLOB, ptr, 0) == TRUE);
1219 CU_ASSERT(binn_list_add(list, BINN_STRING, "", 0) == TRUE);
1220 CU_ASSERT(binn_list_add(list, BINN_STRING, "after the empty items", 0) == TRUE);
1221
1222
1223 // add values to a fixed-size binn (pre-allocated buffer) --------------------------
1224
1225 CU_ASSERT(binn_list_add(obj1, BINN_INT32, &i, 0) == FALSE);
1226 CU_ASSERT(binn_map_set(obj1, 55001, BINN_INT32, &i, 0) == FALSE);
1227
1228 CU_ASSERT(binn_object_set(obj1, "test", BINN_UINT32, &vuint32, 0) == TRUE);
1229 CU_ASSERT(binn_object_set(obj1, "test", BINN_UINT32, &vuint32, 0) == FALSE); // with the same name
1230
1231 CU_ASSERT(binn_object_set(obj1, "key1", BINN_STRING, "this is the value", 0) == TRUE);
1232 CU_ASSERT(binn_object_set(obj1, "key2", BINN_STRING, "the second value", 0) == TRUE);
1233
1234 // create a long string buffer to make the test. the string is longer than the available space
1235 // in the binn.
1236 ptr = malloc(fix_size);
1237 CU_ASSERT(ptr != NULL);
1238 p2 = ptr;
1239 for (i = 0; i < fix_size - 1; i++) {
1240 *p2 = 'A';
1241 p2++;
1242 }
1243 *p2 = '\0';
1244 CU_ASSERT(strlen(ptr) == fix_size - 1);
1245
1246 CU_ASSERT(binn_object_set(obj1, "v2", BINN_STRING, ptr,
1247 0) == FALSE); // it fails because it uses a pre-allocated memory block
1248
1249 CU_ASSERT(binn_object_set(obj, "v2", BINN_STRING, ptr,
1250 0) == TRUE); // but this uses a dynamically allocated memory block, so it works with it
1251 CU_ASSERT(binn_object_set(obj, "Key00", BINN_STRING, "after the big string",
1252 0) == TRUE); // and test the 'Key00' against the 'Key0'
1253
1254 free(ptr);
1255 ptr = 0;
1256
1257 CU_ASSERT(binn_object_set(obj, "list", BINN_LIST, binn_ptr(list), binn_size(list)) == TRUE);
1258 CU_ASSERT(binn_object_set(obj, "Key10", BINN_STRING, "after the list",
1259 0) == TRUE); // and test the 'Key10' against the 'Key1'
1260
1261
1262 // read values - invalid 2 ------------------------------------------------------------
1263
1264
1265 // read keys --------------------------------------------------------------------------
1266
1267 ptr = binn_ptr(map);
1268 CU_ASSERT(ptr != NULL);
1269
1270 CU_ASSERT(binn_map_get_pair(ptr, -1, &id, &value) == FALSE);
1271 CU_ASSERT(binn_map_get_pair(ptr, 0, &id, &value) == FALSE);
1272
1273 CU_ASSERT(binn_map_get_pair(ptr, 1, &id, &value) == TRUE);
1274 CU_ASSERT(id == 5501);
1275 CU_ASSERT(binn_map_get_pair(ptr, 2, &id, &value) == TRUE);
1276 CU_ASSERT(id == 99000);
1277 CU_ASSERT(binn_map_get_pair(ptr, 3, &id, &value) == TRUE);
1278 CU_ASSERT(id == 99001);
1279 CU_ASSERT(binn_map_get_pair(ptr, 10, &id, &value) == TRUE);
1280 CU_ASSERT(id == 99008);
1281 CU_ASSERT(binn_map_get_pair(ptr, 11, &id, &value) == TRUE);
1282 CU_ASSERT(id == 99009);
1283
1284
1285 ptr = binn_ptr(obj);
1286 CU_ASSERT(ptr != NULL);
1287
1288 CU_ASSERT(binn_object_get_pair(ptr, -1, key, &value) == FALSE);
1289 CU_ASSERT(binn_object_get_pair(ptr, 0, key, &value) == FALSE);
1290
1291 CU_ASSERT(binn_object_get_pair(ptr, 1, key, &value) == TRUE);
1292 CU_ASSERT(strcmp(key, "test") == 0);
1293 CU_ASSERT(binn_object_get_pair(ptr, 2, key, &value) == TRUE);
1294 CU_ASSERT(strcmp(key, "key0") == 0);
1295 CU_ASSERT(binn_object_get_pair(ptr, 3, key, &value) == TRUE);
1296 CU_ASSERT(strcmp(key, "key1") == 0);
1297 CU_ASSERT(binn_object_get_pair(ptr, 10, key, &value) == TRUE);
1298 CU_ASSERT(strcmp(key, "key8") == 0);
1299 CU_ASSERT(binn_object_get_pair(ptr, 11, key, &value) == TRUE);
1300 CU_ASSERT(strcmp(key, "key9") == 0);
1301
1302
1303 // read values - valid ----------------------------------------------------------------
1304
1305 ptr = binn_ptr(obj1);
1306 CU_ASSERT(ptr != NULL);
1307
1308 type = 0;
1309 size = 0;
1310 pstr = binn_object_read(ptr, "key1", &type, &size);
1311 CU_ASSERT(pstr != NULL);
1312 CU_ASSERT(type == BINN_STRING);
1313 CU_ASSERT(size > 0);
1314 CU_ASSERT(strcmp(pstr, "this is the value") == 0);
1315
1316 type = 0;
1317 size = 0;
1318 pstr = binn_object_read(ptr, "key2", &type, &size);
1319 CU_ASSERT(pstr != NULL);
1320 CU_ASSERT(type == BINN_STRING);
1321 CU_ASSERT(size > 0);
1322 CU_ASSERT(strcmp(pstr, "the second value") == 0);
1323
1324 type = 0;
1325 size = 0;
1326 pint32 = binn_object_read(ptr, "test", &type, &size);
1327 CU_ASSERT(pint32 != NULL);
1328 CU_ASSERT(type == BINN_UINT32);
1329 //CU_ASSERT(size > 0);
1330 CU_ASSERT(*pint32 == vuint32);
1331
1332
1333 ptr = binn_ptr(list);
1334 CU_ASSERT(ptr != NULL);
1335
1336 type = 0;
1337 size = 0;
1338 pstr = binn_list_read(ptr, 2, &type, &size);
1339 CU_ASSERT(pstr != NULL);
1340 CU_ASSERT(type == BINN_NULL);
1341 //CU_ASSERT(size > 0);
1342 //CU_ASSERT(strcmp(pstr, "this is the value") == 0);
1343
1344 type = 0;
1345 size = 0;
1346 p2 = binn_list_read(ptr, 3, &type, &size);
1347 CU_ASSERT(p2 != NULL);
1348 CU_ASSERT(type == BINN_UINT8);
1349 CU_ASSERT(*p2 == vbyte);
1350
1351 type = 0;
1352 size = 0;
1353 pint16 = binn_list_read(ptr, 4, &type, &size);
1354 CU_ASSERT(pint16 != NULL);
1355 CU_ASSERT(type == BINN_INT16);
1356 CU_ASSERT(*pint16 == vint16);
1357
1358 type = 0;
1359 size = 0;
1360 puint16 = binn_list_read(ptr, 5, &type, &size);
1361 CU_ASSERT(puint16 != NULL);
1362 CU_ASSERT(type == BINN_UINT16);
1363 CU_ASSERT(*puint16 == vuint16);
1364
1365 type = 0;
1366 size = 0;
1367 pint32 = binn_list_read(ptr, 6, &type, &size);
1368 CU_ASSERT(pint32 != NULL);
1369 CU_ASSERT(type == BINN_INT32);
1370 CU_ASSERT(*pint32 == vint32);
1371 // in the second time the value must be the same...
1372 type = 0;
1373 size = 0;
1374 pint32 = binn_list_read(ptr, 6, &type, &size);
1375 CU_ASSERT(pint32 != NULL);
1376 CU_ASSERT(type == BINN_INT32);
1377 CU_ASSERT(*pint32 == vint32);
1378
1379 type = 0;
1380 size = 0;
1381 puint32 = binn_list_read(ptr, 7, &type, &size);
1382 CU_ASSERT(puint32 != NULL);
1383 CU_ASSERT(type == BINN_UINT32);
1384 CU_ASSERT(*puint32 == vuint32);
1385
1386 type = 0;
1387 size = 0;
1388 pint64 = binn_list_read(ptr, 8, &type, &size);
1389 CU_ASSERT(pint64 != NULL);
1390 CU_ASSERT(type == BINN_INT64);
1391 CU_ASSERT(*pint64 == vint64);
1392 // in the second time the value must be the same...
1393 type = 0;
1394 size = 0;
1395 pint64 = binn_list_read(ptr, 8, &type, &size);
1396 CU_ASSERT(pint64 != NULL);
1397 CU_ASSERT(type == BINN_INT64);
1398 CU_ASSERT(*pint64 == vint64);
1399
1400 type = 0;
1401 size = 0;
1402 puint64 = binn_list_read(ptr, 9, &type, &size);
1403 CU_ASSERT(puint64 != NULL);
1404 CU_ASSERT(type == BINN_UINT64);
1405 CU_ASSERT(*puint64 == vuint64);
1406
1407 type = 0;
1408 size = 0;
1409 pstr = binn_list_read(ptr, 10, &type, &size);
1410 CU_ASSERT(pstr != NULL);
1411 CU_ASSERT(type == BINN_STRING);
1412 CU_ASSERT(size > 0);
1413 CU_ASSERT(strcmp(pstr, "this is the string") == 0);
1414
1415 type = 0;
1416 size = 0;
1417 p2 = binn_list_read(ptr, 11, &type, &size);
1418 CU_ASSERT(p2 != NULL);
1419 CU_ASSERT(type == BINN_BLOB);
1420 CU_ASSERT(size == blobsize);
1421 CU_ASSERT(memcmp(p2, pblob, blobsize) == 0);
1422
1423
1424 ptr = binn_ptr(map);
1425 CU_ASSERT(ptr != NULL);
1426
1427 type = 0;
1428 size = 0;
1429 pstr = binn_map_read(ptr, 99000, &type, &size);
1430 CU_ASSERT(pstr != NULL);
1431 CU_ASSERT(type == BINN_NULL);
1432 //CU_ASSERT(size > 0);
1433 //CU_ASSERT(strcmp(pstr, "this is the value") == 0);
1434
1435 type = 0;
1436 size = 0;
1437 p2 = binn_map_read(ptr, 99001, &type, &size);
1438 CU_ASSERT(p2 != NULL);
1439 CU_ASSERT(type == BINN_UINT8);
1440 CU_ASSERT(*p2 == vbyte);
1441
1442 type = 0;
1443 size = 0;
1444 pint16 = binn_map_read(ptr, 99002, &type, &size);
1445 CU_ASSERT(pint16 != NULL);
1446 CU_ASSERT(type == BINN_INT16);
1447 CU_ASSERT(*pint16 == vint16);
1448
1449 type = 0;
1450 size = 0;
1451 puint16 = binn_map_read(ptr, 99003, &type, &size);
1452 CU_ASSERT(puint16 != NULL);
1453 CU_ASSERT(type == BINN_UINT16);
1454 CU_ASSERT(*puint16 == vuint16);
1455
1456 type = 0;
1457 size = 0;
1458 pint32 = binn_map_read(ptr, 99004, &type, &size);
1459 CU_ASSERT(pint32 != NULL);
1460 CU_ASSERT(type == BINN_INT32);
1461 CU_ASSERT(*pint32 == vint32);
1462 // in the second time the value must be the same...
1463 type = 0;
1464 size = 0;
1465 pint32 = binn_map_read(ptr, 99004, &type, &size);
1466 CU_ASSERT(pint32 != NULL);
1467 CU_ASSERT(type == BINN_INT32);
1468 CU_ASSERT(*pint32 == vint32);
1469
1470 type = 0;
1471 size = 0;
1472 puint32 = binn_map_read(ptr, 99005, &type, &size);
1473 CU_ASSERT(puint32 != NULL);
1474 CU_ASSERT(type == BINN_UINT32);
1475 CU_ASSERT(*puint32 == vuint32);
1476
1477 type = 0;
1478 size = 0;
1479 pint64 = binn_map_read(ptr, 99006, &type, &size);
1480 CU_ASSERT(pint64 != NULL);
1481 CU_ASSERT(type == BINN_INT64);
1482 CU_ASSERT(*pint64 == vint64);
1483 // in the second time the value must be the same...
1484 type = 0;
1485 size = 0;
1486 pint64 = binn_map_read(ptr, 99006, &type, &size);
1487 CU_ASSERT(pint64 != NULL);
1488 CU_ASSERT(type == BINN_INT64);
1489 CU_ASSERT(*pint64 == vint64);
1490
1491 type = 0;
1492 size = 0;
1493 puint64 = binn_map_read(ptr, 99007, &type, &size);
1494 CU_ASSERT(puint64 != NULL);
1495 CU_ASSERT(type == BINN_UINT64);
1496 CU_ASSERT(*puint64 == vuint64);
1497
1498 type = 0;
1499 size = 0;
1500 pstr = binn_map_read(ptr, 99008, &type, &size);
1501 CU_ASSERT(pstr != NULL);
1502 CU_ASSERT(type == BINN_STRING);
1503 CU_ASSERT(size > 0);
1504 CU_ASSERT(strcmp(pstr, "this is the string") == 0);
1505
1506 type = 0;
1507 size = 0;
1508 p2 = binn_map_read(ptr, 99009, &type, &size);
1509 CU_ASSERT(p2 != NULL);
1510 CU_ASSERT(type == BINN_BLOB);
1511 CU_ASSERT(size == blobsize);
1512 CU_ASSERT(memcmp(p2, pblob, blobsize) == 0);
1513
1514
1515 ptr = binn_ptr(obj);
1516 CU_ASSERT(ptr != NULL);
1517
1518 type = 0;
1519 size = 0;
1520 pstr = binn_object_read(ptr, "key0", &type, &size);
1521 CU_ASSERT(pstr != NULL);
1522 CU_ASSERT(type == BINN_NULL);
1523 //CU_ASSERT(size > 0);
1524 //CU_ASSERT(strcmp(pstr, "this is the value") == 0);
1525
1526 type = 0;
1527 size = 0;
1528 p2 = binn_object_read(ptr, "key1", &type, &size);
1529 CU_ASSERT(p2 != NULL);
1530 CU_ASSERT(type == BINN_UINT8);
1531 CU_ASSERT(*p2 == vbyte);
1532
1533 type = 0;
1534 size = 0;
1535 pint16 = binn_object_read(ptr, "key2", &type, &size);
1536 CU_ASSERT(pint16 != NULL);
1537 CU_ASSERT(type == BINN_INT16);
1538 CU_ASSERT(*pint16 == vint16);
1539
1540 type = 0;
1541 size = 0;
1542 puint16 = binn_object_read(ptr, "key3", &type, &size);
1543 CU_ASSERT(puint16 != NULL);
1544 CU_ASSERT(type == BINN_UINT16);
1545 CU_ASSERT(*puint16 == vuint16);
1546
1547 type = 0;
1548 size = 0;
1549 pint32 = binn_object_read(ptr, "key4", &type, &size);
1550 CU_ASSERT(pint32 != NULL);
1551 CU_ASSERT(type == BINN_INT32);
1552 CU_ASSERT(*pint32 == vint32);
1553 // in the second time the value must be the same...
1554 type = 0;
1555 size = 0;
1556 pint32 = binn_object_read(ptr, "key4", &type, &size);
1557 CU_ASSERT(pint32 != NULL);
1558 CU_ASSERT(type == BINN_INT32);
1559 CU_ASSERT(*pint32 == vint32);
1560
1561 type = 0;
1562 size = 0;
1563 puint32 = binn_object_read(ptr, "key5", &type, &size);
1564 CU_ASSERT(puint32 != NULL);
1565 CU_ASSERT(type == BINN_UINT32);
1566 CU_ASSERT(*puint32 == vuint32);
1567
1568 type = 0;
1569 size = 0;
1570 pint64 = binn_object_read(ptr, "key6", &type, &size);
1571 CU_ASSERT(pint64 != NULL);
1572 CU_ASSERT(type == BINN_INT64);
1573 CU_ASSERT(*pint64 == vint64);
1574 // in the second time the value must be the same...
1575 type = 0;
1576 size = 0;
1577 pint64 = binn_object_read(ptr, "key6", &type, &size);
1578 CU_ASSERT(pint64 != NULL);
1579 CU_ASSERT(type == BINN_INT64);
1580 CU_ASSERT(*pint64 == vint64);
1581
1582 type = 0;
1583 size = 0;
1584 puint64 = binn_object_read(ptr, "key7", &type, &size);
1585 CU_ASSERT(puint64 != NULL);
1586 CU_ASSERT(type == BINN_UINT64);
1587 CU_ASSERT(*puint64 == vuint64);
1588
1589 type = 0;
1590 size = 0;
1591 pstr = binn_object_read(ptr, "key8", &type, &size);
1592 CU_ASSERT(pstr != NULL);
1593 CU_ASSERT(type == BINN_STRING);
1594 CU_ASSERT(size > 0);
1595 CU_ASSERT(strcmp(pstr, "this is the string") == 0);
1596
1597 type = 0;
1598 size = 0;
1599 p2 = binn_object_read(ptr, "key9", &type, &size);
1600 CU_ASSERT(p2 != NULL);
1601 CU_ASSERT(type == BINN_BLOB);
1602 CU_ASSERT(size == blobsize);
1603 CU_ASSERT(memcmp(p2, pblob, blobsize) == 0);
1604
1605 type = 0;
1606 size = 0;
1607 p2 = binn_object_read(ptr, "v2", &type, &size);
1608 CU_ASSERT(p2 != NULL);
1609 CU_ASSERT(type == BINN_STRING);
1610 CU_ASSERT(size == fix_size - 1);
1611 CU_ASSERT(strlen(p2) == fix_size - 1);
1612 CU_ASSERT(p2[0] == 'A');
1613 CU_ASSERT(p2[1] == 'A');
1614 CU_ASSERT(p2[500] == 'A');
1615 CU_ASSERT(p2[fix_size - 1] == 0);
1616
1617 type = 0;
1618 size = 0;
1619 pstr = binn_object_read(ptr, "key00", &type, &size);
1620 CU_ASSERT(pstr != NULL);
1621 CU_ASSERT(type == BINN_STRING);
1622 CU_ASSERT(size > 0);
1623 CU_ASSERT(strcmp(pstr, "after the big string") == 0);
1624
1625 type = 0;
1626 size = 0;
1627 p2 = binn_object_read(ptr, "list", &type, &size);
1628 CU_ASSERT(p2 != NULL);
1629 CU_ASSERT(type == BINN_LIST);
1630 CU_ASSERT(size > 0);
1631 //
1632 type = 0;
1633 size = 0;
1634 puint64 = binn_list_read(p2, 9, &type, &size);
1635 CU_ASSERT(puint64 != NULL);
1636 CU_ASSERT(type == BINN_UINT64);
1637 CU_ASSERT(*puint64 == vuint64);
1638 //
1639 type = 0;
1640 size = 0;
1641 pstr = binn_list_read(p2, 10, &type, &size);
1642 CU_ASSERT(pstr != NULL);
1643 CU_ASSERT(type == BINN_STRING);
1644 CU_ASSERT(size > 0);
1645 CU_ASSERT(strcmp(pstr, "this is the string") == 0);
1646 //
1647 type = 0;
1648 size = 0;
1649 pstr = binn_list_read(p2, 12, &type, &size);
1650 CU_ASSERT(pstr != NULL);
1651 CU_ASSERT(type == BINN_BLOB); // empty blob
1652 CU_ASSERT(size == 0);
1653 //
1654 type = 0;
1655 size = 0;
1656 pstr = binn_list_read(p2, 13, &type, &size);
1657 CU_ASSERT(pstr != NULL);
1658 CU_ASSERT(type == BINN_STRING); // empty string
1659 CU_ASSERT(size == 0);
1660 CU_ASSERT(strcmp(pstr, "") == 0);
1661 //
1662 type = 0;
1663 size = 0;
1664 pstr = binn_list_read(p2, 14, &type, &size);
1665 CU_ASSERT(pstr != NULL);
1666 CU_ASSERT(type == BINN_STRING);
1667 CU_ASSERT(size > 0);
1668 CU_ASSERT(strcmp(pstr, "after the empty items") == 0);
1669
1670 type = 0;
1671 size = 0;
1672 pstr = binn_object_read(ptr, "key10", &type, &size);
1673 CU_ASSERT(pstr != NULL);
1674 CU_ASSERT(type == BINN_STRING);
1675 CU_ASSERT(size > 0);
1676 CU_ASSERT(strcmp(pstr, "after the list") == 0);
1677
1678
1679 // binn_ptr, IsValidBinnHeader, binn_is_valid...
1680 // also with invalid/null pointers, with pointers containing invalid data...
1681
1682 CU_ASSERT(binn_ptr(NULL) == NULL);
1683 // pointers to invalid data
1684 //CU_ASSERT(binn_ptr(&type) == NULL);
1685 //CU_ASSERT(binn_ptr(&size) == NULL);
1686 //CU_ASSERT(binn_ptr(&count) == NULL);
1687 //CU_ASSERT(binn_is_valid_header(NULL) == FALSE);
1688
1689 ptr = binn_ptr(obj);
1690 CU_ASSERT(ptr != NULL);
1691 // test the header
1692 size = 0;
1693 CU_ASSERT(binn_is_valid_header(ptr, &type, &count, &size, &header_size) == TRUE);
1694 CU_ASSERT(type == BINN_OBJECT);
1695 CU_ASSERT(count == 15);
1696 CU_ASSERT(header_size >= MIN_BINN_SIZE && header_size <= MAX_BINN_HEADER);
1697 CU_ASSERT(size > MIN_BINN_SIZE);
1698 CU_ASSERT(size == obj->size);
1699 // test all the buffer
1700 CU_ASSERT(binn_is_valid(ptr, &type, &count, &size) == TRUE);
1701 CU_ASSERT(type == BINN_OBJECT);
1702 CU_ASSERT(count == 15);
1703 CU_ASSERT(size > MIN_BINN_SIZE);
1704 CU_ASSERT(size == obj->size);
1705
1706 ptr = binn_ptr(map);
1707 CU_ASSERT(ptr != NULL);
1708 // test the header
1709 size = 0;
1710 CU_ASSERT(binn_is_valid_header(ptr, &type, &count, &size, &header_size) == TRUE);
1711 CU_ASSERT(type == BINN_MAP);
1712 CU_ASSERT(count == 11);
1713 CU_ASSERT(header_size >= MIN_BINN_SIZE && header_size <= MAX_BINN_HEADER);
1714 CU_ASSERT(size > MIN_BINN_SIZE);
1715 CU_ASSERT(size == map->size);
1716 // test all the buffer
1717 CU_ASSERT(binn_is_valid(ptr, &type, &count, &size) == TRUE);
1718 CU_ASSERT(type == BINN_MAP);
1719 CU_ASSERT(count == 11);
1720 CU_ASSERT(size > MIN_BINN_SIZE);
1721 CU_ASSERT(size == map->size);
1722
1723 ptr = binn_ptr(list);
1724 CU_ASSERT(ptr != NULL);
1725 // test the header
1726 size = 0;
1727 CU_ASSERT(binn_is_valid_header(ptr, &type, &count, &size, &header_size) == TRUE);
1728 CU_ASSERT(type == BINN_LIST);
1729 CU_ASSERT(count == 14);
1730 CU_ASSERT(header_size >= MIN_BINN_SIZE && header_size <= MAX_BINN_HEADER);
1731 CU_ASSERT(size > MIN_BINN_SIZE);
1732 CU_ASSERT(size == list->size);
1733 // test all the buffer
1734 CU_ASSERT(binn_is_valid(ptr, &type, &count, &size) == TRUE);
1735 CU_ASSERT(type == BINN_LIST);
1736 CU_ASSERT(count == 14);
1737 CU_ASSERT(header_size >= MIN_BINN_SIZE && header_size <= MAX_BINN_HEADER);
1738 CU_ASSERT(size > MIN_BINN_SIZE);
1739 CU_ASSERT(size == list->size);
1740
1741
1742 // binn_size - invalid and valid args --------------------------------------------
1743
1744 CU_ASSERT(binn_size(NULL) == 0);
1745
1746 CU_ASSERT(binn_size(list) == list->size);
1747 CU_ASSERT(binn_size(map) == map->size);
1748 CU_ASSERT(binn_size(obj) == obj->size);
1749 CU_ASSERT(binn_size(obj1) == obj1->size);
1750
1751
1752 // destroy them all -------------------------------------------------------------------
1753
1754 binn_free(list);
1755 binn_free(map);
1756 binn_free(obj);
1757
1758 printf("OK\n");
1759 }
1760
1761 /*************************************************************************************/
1762
test_invalid_binn()1763 void test_invalid_binn() {
1764
1765 unsigned char buffers[][20] = {
1766 { 0xE0 },
1767 { 0xE0, 0x7E},
1768 { 0xE0, 0x7E, 0x7F},
1769 { 0xE0, 0x7E, 0x7F, 0x12},
1770 { 0xE0, 0x7E, 0x7F, 0x12, 0x34},
1771 { 0xE0, 0x7E, 0x7F, 0x12, 0x34, 0x01},
1772 { 0xE0, 0x7E, 0x7F, 0x12, 0x34, 0x7F},
1773 { 0xE0, 0x7E, 0x7F, 0x12, 0x34, 0xFF},
1774 { 0xE0, 0x7E, 0x7F, 0x12, 0x34, 0xFF, 0xFF},
1775 { 0xE0, 0x7E, 0x7F, 0x12, 0x34, 0xFF, 0xFF, 0xFF},
1776 { 0xE0, 0x7E, 0x7F, 0x12, 0x34, 0xFF, 0xFF, 0xFF, 0xFF},
1777 { 0xE0, 0x7E, 0x7F, 0x12, 0x34, 0xFF, 0xFF, 0xFF, 0xFF, 0x01},
1778 { 0xE0, 0x7E, 0x7F, 0x12, 0x34, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
1779 { 0xE0, 0x7E, 0xFF},
1780 { 0xE0, 0x7E, 0xFF, 0x12},
1781 { 0xE0, 0x7E, 0xFF, 0x12, 0x34},
1782 { 0xE0, 0x7E, 0xFF, 0x12, 0x34, 0x01},
1783 { 0xE0, 0x7E, 0xFF, 0x12, 0x34, 0x7F},
1784 { 0xE0, 0x7E, 0xFF, 0x12, 0x34, 0xFF},
1785 { 0xE0, 0x7E, 0xFF, 0x12, 0x34, 0xFF, 0xFF},
1786 { 0xE0, 0x7E, 0xFF, 0x12, 0x34, 0xFF, 0xFF, 0xFF},
1787 { 0xE0, 0x7E, 0xFF, 0x12, 0x34, 0xFF, 0xFF, 0xFF, 0xFF},
1788 { 0xE0, 0x7E, 0xFF, 0x12, 0x34, 0xFF, 0xFF, 0xFF, 0xFF, 0x01},
1789 { 0xE0, 0x7E, 0xFF, 0x12, 0x34, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
1790 { 0xE0, 0x8E},
1791 { 0xE0, 0x8E, 0xFF},
1792 { 0xE0, 0x8E, 0xFF, 0x12},
1793 { 0xE0, 0x8E, 0xFF, 0x12, 0x34},
1794 { 0xE0, 0x8E, 0xFF, 0x12, 0x34, 0x01},
1795 { 0xE0, 0x8E, 0xFF, 0x12, 0x34, 0x7F},
1796 { 0xE0, 0x8E, 0xFF, 0x12, 0x34, 0xFF},
1797 { 0xE0, 0x8E, 0xFF, 0x12, 0x34, 0xFF, 0xFF},
1798 { 0xE0, 0x8E, 0xFF, 0x12, 0x34, 0xFF, 0xFF, 0xFF},
1799 { 0xE0, 0x8E, 0xFF, 0x12, 0x34, 0xFF, 0xFF, 0xFF, 0xFF},
1800 { 0xE0, 0x8E, 0xFF, 0x12, 0x34, 0xFF, 0xFF, 0xFF, 0xFF, 0x01},
1801 { 0xE0, 0x8E, 0xFF, 0x12, 0x34, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
1802 };
1803
1804 int count, size, i;
1805 unsigned char *ptr;
1806
1807 puts("testing invalid binn buffers...");
1808
1809 count = sizeof buffers / sizeof buffers[0];
1810
1811 for (i = 0; i < count; i++) {
1812 ptr = buffers[i];
1813 size = strlen((char*) ptr);
1814 printf("checking invalid binn #%d size: %d bytes\n", i, size);
1815 CU_ASSERT(binn_is_valid_ex(ptr, NULL, NULL, &size) == FALSE);
1816 }
1817
1818 puts("OK");
1819 }
1820
main()1821 int main() {
1822 CU_pSuite pSuite = NULL;
1823 if (CUE_SUCCESS != CU_initialize_registry()) {
1824 return CU_get_error();
1825 }
1826 pSuite = CU_add_suite("jbl_test_binn1", init_suite, clean_suite);
1827 if (NULL == pSuite) {
1828 CU_cleanup_registry();
1829 return CU_get_error();
1830 }
1831 if ( (NULL == CU_add_test(pSuite, "test_int64", test_int64))
1832 || (NULL == CU_add_test(pSuite, "test_floating_point_numbers", test_floating_point_numbers))
1833 || (NULL == CU_add_test(pSuite, "test1", test1))
1834 || (NULL == CU_add_test(pSuite, "test2", test2))
1835 || (NULL == CU_add_test(pSuite, "test_invalid_binn", test_invalid_binn))) {
1836 CU_cleanup_registry();
1837 return CU_get_error();
1838 }
1839 CU_basic_set_mode(CU_BRM_VERBOSE);
1840 CU_basic_run_tests();
1841 int ret = CU_get_error() || CU_get_number_of_failures();
1842 CU_cleanup_registry();
1843 return ret;
1844 }
1845