Atrinik Server  4.0
packet.c
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program 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 General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
25 #include <global.h>
26 #include <check.h>
27 #include <checkstd.h>
28 #include <check_proto.h>
29 #include <toolkit/packet.h>
30 #include <toolkit/string.h>
31 
32 #define packet_verify_data(packet, str) \
33 { \
34  char *hex; \
35  hex = emalloc(sizeof(*hex) * ((packet)->len * 2 + 1)); \
36  string_tohex((packet)->data, (packet)->len, hex, (packet)->len * 2 + 1, \
37  false); \
38  ck_assert_str_eq(hex, (str)); \
39  efree(hex); \
40 }
41 
42 START_TEST(test_packet_new)
43 {
44  packet_struct *packet;
45 
46  packet = packet_new(0, 0, 0);
47  ck_assert_ptr_ne(packet, NULL);
48  packet_free(packet);
49 
50  packet = packet_new(0, 0, 0);
51  packet_append_string_terminated(packet, "hello world");
52  ck_assert_str_eq((const char *) packet->data, "hello world");
53  packet_free(packet);
54 
55  packet = packet_new(0, 5, 0);
56  packet_append_string_terminated(packet, "hello world");
57  ck_assert_str_eq((const char *) packet->data, "hello world");
58  packet_free(packet);
59 
60  packet = packet_new(0, 5, 5);
61  packet_append_string_terminated(packet, "hello world");
62  ck_assert_str_eq((const char *) packet->data, "hello world");
63  packet_free(packet);
64 
65  packet = packet_new(0, 5, 100);
66  packet_append_string_terminated(packet, "hello world");
67  ck_assert_str_eq((const char *) packet->data, "hello world");
68  packet_free(packet);
69 
70  packet = packet_new(0, 100, 0);
71  packet_append_string_terminated(packet, "hello world");
72  ck_assert_str_eq((const char *) packet->data, "hello world");
73  packet_free(packet);
74 
75  packet = packet_new(0, 100, 100);
76  packet_append_string_terminated(packet, "hello world");
77  ck_assert_str_eq((const char *) packet->data, "hello world");
78  packet_free(packet);
79 
80  packet = packet_new(0, 0, 100);
81  packet_append_string_terminated(packet, "hello world");
82  ck_assert_str_eq((const char *) packet->data, "hello world");
83  packet_free(packet);
84 }
85 END_TEST
86 
87 START_TEST(test_packet_dup)
88 {
89  packet_struct *packet, *packet2;
90 
91  packet = packet_new(0, 0, 0);
92  packet_append_int32(packet, 50);
93  packet_verify_data(packet, "00000032");
94  packet2 = packet_dup(packet);
95  packet_verify_data(packet2, "00000032");
96  packet_free(packet);
97  packet_free(packet2);
98 
99  packet = packet_new(0, 0, 0);
100  packet_verify_data(packet, "");
101  packet2 = packet_dup(packet);
102  packet_verify_data(packet2, "");
103  packet_free(packet);
104  packet_free(packet2);
105 }
106 END_TEST
107 
108 START_TEST(test_packet_save)
109 {
110  packet_struct *packet;
111  packet_save_t packet_save_buf;
112 
113  packet = packet_new(0, 0, 0);
114  packet_save(packet, &packet_save_buf);
115  ck_assert_uint_eq(packet_save_buf.pos, 0);
116  packet_free(packet);
117 
118  packet = packet_new(0, 0, 0);
119  packet_append_uint32(packet, 42);
120  packet_save(packet, &packet_save_buf);
121  ck_assert_uint_eq(packet_save_buf.pos, 4);
122  packet_free(packet);
123 
124  packet = packet_new(0, 0, 0);
125  packet_append_uint32(packet, 42);
126  packet_save(packet, &packet_save_buf);
127  ck_assert_uint_eq(packet_save_buf.pos, 4);
128  packet_append_uint32(packet, 42);
129  ck_assert_uint_eq(packet_save_buf.pos, 4);
130  packet_free(packet);
131 }
132 END_TEST
133 
134 START_TEST(test_packet_load)
135 {
136  packet_struct *packet;
137  packet_save_t packet_save_buf;
138 
139  packet = packet_new(0, 0, 0);
140  ck_assert_uint_eq(packet_get_pos(packet), 0);
141  packet_save(packet, &packet_save_buf);
142  packet_load(packet, &packet_save_buf);
143  ck_assert_uint_eq(packet_get_pos(packet), 0);
144  packet_free(packet);
145 
146  packet = packet_new(0, 0, 0);
147  ck_assert_uint_eq(packet_get_pos(packet), 0);
148  packet_append_uint32(packet, 42);
149  ck_assert_uint_eq(packet_get_pos(packet), 4);
150  packet_save(packet, &packet_save_buf);
151  packet_load(packet, &packet_save_buf);
152  ck_assert_uint_eq(packet_get_pos(packet), 4);
153  packet_free(packet);
154 
155  packet = packet_new(0, 0, 0);
156  ck_assert_uint_eq(packet_get_pos(packet), 0);
157  packet_append_uint32(packet, 42);
158  ck_assert_uint_eq(packet_get_pos(packet), 4);
159  packet_save(packet, &packet_save_buf);
160  packet_load(packet, &packet_save_buf);
161  ck_assert_uint_eq(packet_get_pos(packet), 4);
162  packet_append_uint32(packet, 42);
163  ck_assert_uint_eq(packet_get_pos(packet), 8);
164  packet_free(packet);
165 
166  packet = packet_new(0, 0, 0);
167  ck_assert_uint_eq(packet_get_pos(packet), 0);
168  packet_append_uint32(packet, 42);
169  ck_assert_uint_eq(packet_get_pos(packet), 4);
170  packet_save(packet, &packet_save_buf);
171  packet_append_uint32(packet, 42);
172  packet_load(packet, &packet_save_buf);
173  ck_assert_uint_eq(packet_get_pos(packet), 4);
174  packet_free(packet);
175 }
176 END_TEST
177 
178 START_TEST(test_packet_append_uint8)
179 {
180  packet_struct *packet;
181 
182  packet = packet_new(0, 0, 0);
183  packet_append_uint8(packet, 0);
184  packet_verify_data(packet, "00");
185  packet_free(packet);
186 
187  packet = packet_new(0, 0, 0);
188  packet_append_uint8(packet, 42);
189  packet_verify_data(packet, "2A");
190  packet_free(packet);
191 
192  packet = packet_new(0, 0, 0);
193  packet_append_uint8(packet, UINT8_MAX);
194  packet_verify_data(packet, "FF");
195  packet_free(packet);
196 }
197 END_TEST
198 
199 START_TEST(test_packet_append_int8)
200 {
201  packet_struct *packet;
202 
203  packet = packet_new(0, 0, 0);
204  packet_append_int8(packet, 0);
205  packet_verify_data(packet, "00");
206  packet_free(packet);
207 
208  packet = packet_new(0, 0, 0);
209  packet_append_int8(packet, 42);
210  packet_verify_data(packet, "2A");
211  packet_free(packet);
212 
213  packet = packet_new(0, 0, 0);
214  packet_append_int8(packet, -42);
215  packet_verify_data(packet, "D6");
216  packet_free(packet);
217 
218  packet = packet_new(0, 0, 0);
219  packet_append_int8(packet, INT8_MAX);
220  packet_verify_data(packet, "7F");
221  packet_free(packet);
222 
223  packet = packet_new(0, 0, 0);
224  packet_append_int8(packet, INT8_MIN);
225  packet_verify_data(packet, "80");
226  packet_free(packet);
227 }
228 END_TEST
229 
230 START_TEST(test_packet_append_uint16)
231 {
232  packet_struct *packet;
233 
234  packet = packet_new(0, 0, 0);
235  packet_append_uint16(packet, 0);
236  packet_verify_data(packet, "0000");
237  packet_free(packet);
238 
239  packet = packet_new(0, 0, 0);
240  packet_append_uint16(packet, 42);
241  packet_verify_data(packet, "002A");
242  packet_free(packet);
243 
244  packet = packet_new(0, 0, 0);
245  packet_append_uint16(packet, UINT16_MAX);
246  packet_verify_data(packet, "FFFF");
247  packet_free(packet);
248 }
249 END_TEST
250 
251 START_TEST(test_packet_append_int16)
252 {
253  packet_struct *packet;
254 
255  packet = packet_new(0, 0, 0);
256  packet_append_int16(packet, 0);
257  packet_verify_data(packet, "0000");
258  packet_free(packet);
259 
260  packet = packet_new(0, 0, 0);
261  packet_append_int16(packet, 42);
262  packet_verify_data(packet, "002A");
263  packet_free(packet);
264 
265  packet = packet_new(0, 0, 0);
266  packet_append_int16(packet, -42);
267  packet_verify_data(packet, "FFD6");
268  packet_free(packet);
269 
270  packet = packet_new(0, 0, 0);
271  packet_append_int16(packet, INT16_MAX);
272  packet_verify_data(packet, "7FFF");
273  packet_free(packet);
274 
275  packet = packet_new(0, 0, 0);
276  packet_append_int16(packet, INT16_MIN);
277  packet_verify_data(packet, "8000");
278  packet_free(packet);
279 }
280 END_TEST
281 
282 START_TEST(test_packet_append_uint32)
283 {
284  packet_struct *packet;
285 
286  packet = packet_new(0, 0, 0);
287  packet_append_uint32(packet, 0);
288  packet_verify_data(packet, "00000000");
289  packet_free(packet);
290 
291  packet = packet_new(0, 0, 0);
292  packet_append_uint32(packet, 42);
293  packet_verify_data(packet, "0000002A");
294  packet_free(packet);
295 
296  packet = packet_new(0, 0, 0);
297  packet_append_uint32(packet, UINT32_MAX);
298  packet_verify_data(packet, "FFFFFFFF");
299  packet_free(packet);
300 }
301 END_TEST
302 
303 START_TEST(test_packet_append_int32)
304 {
305  packet_struct *packet;
306 
307  packet = packet_new(0, 0, 0);
308  packet_append_int32(packet, 0);
309  packet_verify_data(packet, "00000000");
310  packet_free(packet);
311 
312  packet = packet_new(0, 0, 0);
313  packet_append_int32(packet, 42);
314  packet_verify_data(packet, "0000002A");
315  packet_free(packet);
316 
317  packet = packet_new(0, 0, 0);
318  packet_append_int32(packet, -42);
319  packet_verify_data(packet, "FFFFFFD6");
320  packet_free(packet);
321 
322  packet = packet_new(0, 0, 0);
323  packet_append_int32(packet, INT32_MAX);
324  packet_verify_data(packet, "7FFFFFFF");
325  packet_free(packet);
326 
327  packet = packet_new(0, 0, 0);
328  packet_append_int32(packet, INT32_MIN);
329  packet_verify_data(packet, "80000000");
330  packet_free(packet);
331 }
332 END_TEST
333 
334 START_TEST(test_packet_append_uint64)
335 {
336  packet_struct *packet;
337 
338  packet = packet_new(0, 0, 0);
339  packet_append_uint64(packet, 0);
340  packet_verify_data(packet, "0000000000000000");
341  packet_free(packet);
342 
343  packet = packet_new(0, 0, 0);
344  packet_append_uint64(packet, 42);
345  packet_verify_data(packet, "000000000000002A");
346  packet_free(packet);
347 
348  packet = packet_new(0, 0, 0);
349  packet_append_uint64(packet, UINT64_MAX);
350  packet_verify_data(packet, "FFFFFFFFFFFFFFFF");
351  packet_free(packet);
352 }
353 END_TEST
354 
355 START_TEST(test_packet_append_int64)
356 {
357  packet_struct *packet;
358 
359  packet = packet_new(0, 0, 0);
360  packet_append_int64(packet, 0);
361  packet_verify_data(packet, "0000000000000000");
362  packet_free(packet);
363 
364  packet = packet_new(0, 0, 0);
365  packet_append_int64(packet, 42);
366  packet_verify_data(packet, "000000000000002A");
367  packet_free(packet);
368 
369  packet = packet_new(0, 0, 0);
370  packet_append_int64(packet, -42);
371  packet_verify_data(packet, "FFFFFFFFFFFFFFD6");
372  packet_free(packet);
373 
374  packet = packet_new(0, 0, 0);
375  packet_append_int64(packet, INT64_MAX);
376  packet_verify_data(packet, "7FFFFFFFFFFFFFFF");
377  packet_free(packet);
378 
379  packet = packet_new(0, 0, 0);
380  packet_append_int64(packet, INT64_MIN);
381  packet_verify_data(packet, "8000000000000000");
382  packet_free(packet);
383 }
384 END_TEST
385 
386 START_TEST(test_packet_append_float)
387 {
388  packet_struct *packet;
389 
390  packet = packet_new(0, 0, 0);
391  packet_append_float(packet, 1.0);
392  packet_verify_data(packet, "3F800000");
393  packet_free(packet);
394 
395  packet = packet_new(0, 0, 0);
396  packet_append_float(packet, 0.0001);
397  packet_verify_data(packet, "38D1B717");
398  packet_free(packet);
399 
400  packet = packet_new(0, 0, 0);
401  packet_append_float(packet, -10.0);
402  packet_verify_data(packet, "C1200000");
403  packet_free(packet);
404 
405  packet = packet_new(0, 0, 0);
406  packet_append_float(packet, 12345678.);
407  packet_verify_data(packet, "4B3C614E");
408  packet_free(packet);
409 
410  packet = packet_new(0, 0, 0);
411  packet_append_float(packet, -109.56);
412  packet_verify_data(packet, "C2DB1EB8");
413  packet_free(packet);
414 }
415 END_TEST
416 
417 START_TEST(test_packet_append_double)
418 {
419  packet_struct *packet;
420 
421  packet = packet_new(0, 0, 0);
422  packet_append_double(packet, 1.0);
423  packet_verify_data(packet, "3FF0000000000000");
424  packet_free(packet);
425 
426  packet = packet_new(0, 0, 0);
427  packet_append_double(packet, 0.0001);
428  packet_verify_data(packet, "3F1A36E2EB1C432D");
429  packet_free(packet);
430 
431  packet = packet_new(0, 0, 0);
432  packet_append_double(packet, -10.0);
433  packet_verify_data(packet, "C024000000000000");
434  packet_free(packet);
435 
436  packet = packet_new(0, 0, 0);
437  packet_append_double(packet, 123456789);
438  packet_verify_data(packet, "419D6F3454000000");
439  packet_free(packet);
440 
441  packet = packet_new(0, 0, 0);
442  packet_append_double(packet, -109.56);
443  packet_verify_data(packet, "C05B63D70A3D70A4");
444  packet_free(packet);
445 }
446 END_TEST
447 
448 START_TEST(test_packet_append_data_len)
449 {
450  packet_struct *packet;
451  const uint8_t data[] = {0xff, 0x03, 0x00};
452 
453  packet = packet_new(0, 0, 0);
454  packet_append_data_len(packet, data, 0);
455  packet_verify_data(packet, "");
456  packet_free(packet);
457 
458  packet = packet_new(0, 0, 0);
459  packet_append_data_len(packet, data, 1);
460  packet_verify_data(packet, "FF");
461  packet_free(packet);
462 
463  packet = packet_new(0, 0, 0);
464  packet_append_data_len(packet, data, 3);
465  packet_verify_data(packet, "FF0300");
466  packet_free(packet);
467 }
468 END_TEST
469 
470 START_TEST(test_packet_append_string_len)
471 {
472  packet_struct *packet;
473 
474  packet = packet_new(0, 0, 0);
475  packet_append_string_len(packet, "", 0);
476  packet_verify_data(packet, "");
477  packet_free(packet);
478 
479  packet = packet_new(0, 0, 0);
480  packet_append_string_len(packet, "test", 4);
481  packet_verify_data(packet, "74657374");
482  packet_free(packet);
483 
484  packet = packet_new(0, 0, 0);
485  packet_append_string_len(packet, "test", 2);
486  packet_verify_data(packet, "7465");
487  packet_free(packet);
488 }
489 END_TEST
490 
491 START_TEST(test_packet_append_string)
492 {
493  packet_struct *packet;
494 
495  packet = packet_new(0, 0, 0);
496  packet_append_string(packet, "");
497  packet_verify_data(packet, "");
498  packet_free(packet);
499 
500  packet = packet_new(0, 0, 0);
501  packet_append_string(packet, "test");
502  packet_verify_data(packet, "74657374");
503  packet_free(packet);
504 }
505 END_TEST
506 
507 START_TEST(test_packet_append_string_len_terminated)
508 {
509  packet_struct *packet;
510 
511  packet = packet_new(0, 0, 0);
512  packet_append_string_len_terminated(packet, "", 0);
513  packet_verify_data(packet, "00");
514  packet_free(packet);
515 
516  packet = packet_new(0, 0, 0);
517  packet_append_string_len_terminated(packet, "test", 2);
518  packet_verify_data(packet, "746500");
519  packet_free(packet);
520 }
521 END_TEST
522 
523 START_TEST(test_packet_append_string_terminated)
524 {
525  packet_struct *packet;
526 
527  packet = packet_new(0, 0, 0);
528  packet_append_string_terminated(packet, "");
529  packet_verify_data(packet, "00");
530  packet_free(packet);
531 
532  packet = packet_new(0, 0, 0);
533  packet_append_string_terminated(packet, "test");
534  packet_verify_data(packet, "7465737400");
535  packet_free(packet);
536 }
537 END_TEST
538 
539 START_TEST(test_packet_append_packet)
540 {
541  packet_struct *packet, *packet2;
542 
543  packet = packet_new(0, 0, 0);
544  packet2 = packet_new(0, 0, 0);
545  packet_verify_data(packet, "");
546  packet_verify_data(packet2, "");
547  packet_append_packet(packet, packet2);
548  packet_verify_data(packet, "");
549  packet_verify_data(packet2, "");
550  packet_free(packet);
551  packet_free(packet2);
552 
553  packet = packet_new(0, 0, 0);
554  packet2 = packet_new(0, 0, 0);
555  packet_append_string_terminated(packet2, "test");
556  packet_verify_data(packet, "");
557  packet_verify_data(packet2, "7465737400");
558  packet_append_packet(packet, packet2);
559  packet_verify_data(packet, "7465737400");
560  packet_verify_data(packet2, "7465737400");
561  packet_free(packet);
562  packet_free(packet2);
563 }
564 END_TEST
565 
566 START_TEST(test_packet_to_uint8)
567 {
568  size_t pos;
569 
570  pos = 0;
571  ck_assert_uint_eq(packet_to_uint8((uint8_t []) {0x00}, 1, &pos), 0);
572 
573  pos = 0;
574  ck_assert_uint_eq(packet_to_uint8((uint8_t []) {0x2A}, 1, &pos), 42);
575 
576  pos = 0;
577  ck_assert_uint_eq(packet_to_uint8((uint8_t []) {0xFF}, 1, &pos), UINT8_MAX);
578 }
579 END_TEST
580 
581 START_TEST(test_packet_to_int8)
582 {
583  size_t pos;
584 
585  pos = 0;
586  ck_assert_int_eq(packet_to_int8((uint8_t []) {0x00}, 1, &pos), 0);
587 
588  pos = 0;
589  ck_assert_int_eq(packet_to_int8((uint8_t []) {0x2A}, 1, &pos), 42);
590 
591  pos = 0;
592  ck_assert_int_eq(packet_to_int8((uint8_t []) {0x7F}, 1, &pos), INT8_MAX);
593 
594  pos = 0;
595  ck_assert_int_eq(packet_to_int8((uint8_t []) {0x80}, 1, &pos), INT8_MIN);
596 }
597 END_TEST
598 
599 START_TEST(test_packet_to_uint16)
600 {
601  size_t pos;
602 
603  pos = 0;
604  ck_assert_uint_eq(packet_to_uint16((uint8_t []) {0x00, 0x00}, 2, &pos), 0);
605 
606  pos = 0;
607  ck_assert_uint_eq(packet_to_uint16((uint8_t []) {0x00, 0x2A}, 2, &pos), 42);
608 
609  pos = 0;
610  ck_assert_uint_eq(packet_to_uint16((uint8_t []) {0xFF, 0xFF}, 2, &pos),
611  UINT16_MAX);
612 }
613 END_TEST
614 
615 START_TEST(test_packet_to_int16)
616 {
617  size_t pos;
618 
619  pos = 0;
620  ck_assert_int_eq(packet_to_int16((uint8_t []) {0x00, 0x00}, 2, &pos), 0);
621 
622  pos = 0;
623  ck_assert_int_eq(packet_to_int16((uint8_t []) {0x00, 0x2A}, 2, &pos), 42);
624 
625  pos = 0;
626  ck_assert_int_eq(packet_to_int16((uint8_t []) {0xFF, 0xD6}, 2, &pos), -42);
627 
628  pos = 0;
629  ck_assert_int_eq(packet_to_int16((uint8_t []) {0x7F, 0xFF}, 2, &pos),
630  INT16_MAX);
631 
632  pos = 0;
633  ck_assert_int_eq(packet_to_int16((uint8_t []) {0x80, 0x00}, 2, &pos),
634  INT16_MIN);
635 }
636 END_TEST
637 
638 START_TEST(test_packet_to_uint32)
639 {
640  size_t pos;
641 
642  pos = 0;
643  ck_assert_uint_eq(packet_to_uint32((uint8_t []) {0x00, 0x00, 0x00, 0x00}, 4,
644  &pos), 0);
645 
646  pos = 0;
647  ck_assert_uint_eq(packet_to_uint32((uint8_t []) {0x00, 0x00, 0x00, 0x2A}, 4,
648  &pos), 42);
649 
650  pos = 0;
651  ck_assert_uint_eq(packet_to_uint32((uint8_t []) {0xFF, 0xFF, 0xFF, 0xFF}, 4,
652  &pos), UINT32_MAX);
653 }
654 END_TEST
655 
656 START_TEST(test_packet_to_int32)
657 {
658  size_t pos;
659 
660  pos = 0;
661  ck_assert_int_eq(packet_to_int32((uint8_t []) {0x00, 0x00, 0x00, 0x00}, 4,
662  &pos), 0);
663 
664  pos = 0;
665  ck_assert_int_eq(packet_to_int32((uint8_t []) {0x00, 0x00, 0x00, 0x2A}, 4,
666  &pos), 42);
667 
668  pos = 0;
669  ck_assert_int_eq(packet_to_int32((uint8_t []) {0xFF, 0xFF, 0xFF, 0xD6}, 4,
670  &pos), -42);
671 
672  pos = 0;
673  ck_assert_int_eq(packet_to_int32((uint8_t []) {0x7F, 0xFF, 0xFF, 0xFF}, 4,
674  &pos), INT32_MAX);
675 
676  pos = 0;
677  ck_assert_int_eq(packet_to_int32((uint8_t []) {0x80, 0x00, 0x00, 0x00}, 4,
678  &pos), INT32_MIN);
679 }
680 END_TEST
681 
682 START_TEST(test_packet_to_uint64)
683 {
684  size_t pos;
685 
686  pos = 0;
687  ck_assert_uint_eq(packet_to_uint64((uint8_t []) {0x00, 0x00, 0x00, 0x00,
688  0x00, 0x00, 0x00, 0x00}, 8, &pos), 0);
689 
690  pos = 0;
691  ck_assert_uint_eq(packet_to_uint64((uint8_t []) {0x00, 0x00, 0x00, 0x00,
692  0x00, 0x00, 0x00, 0x2A}, 8, &pos), 42);
693 
694  pos = 0;
695  ck_assert_uint_eq(packet_to_uint64((uint8_t []) {0xFF, 0xFF, 0xFF, 0xFF,
696  0xFF, 0xFF, 0xFF, 0xFF}, 8, &pos), UINT64_MAX);
697 }
698 END_TEST
699 
700 START_TEST(test_packet_to_int64)
701 {
702  size_t pos;
703 
704  pos = 0;
705  ck_assert_int_eq(packet_to_int64((uint8_t []) {0x00, 0x00, 0x00, 0x00,
706  0x00, 0x00, 0x00, 0x00}, 8, &pos), 0);
707 
708  pos = 0;
709  ck_assert_int_eq(packet_to_int64((uint8_t []) {0x00, 0x00, 0x00, 0x00,
710  0x00, 0x00, 0x00, 0x2A}, 8, &pos), 42);
711 
712  pos = 0;
713  ck_assert_int_eq(packet_to_int64((uint8_t []) {0xFF, 0xFF, 0xFF, 0xFF,
714  0xFF, 0xFF, 0xFF, 0xD6}, 8, &pos), -42);
715 
716  pos = 0;
717  ck_assert_int_eq(packet_to_int64((uint8_t []) {0x7F, 0xFF, 0xFF, 0xFF,
718  0xFF, 0xFF, 0xFF, 0xFF}, 8, &pos), INT64_MAX);
719 
720  pos = 0;
721  ck_assert_int_eq(packet_to_int64((uint8_t []) {0x80, 0x00, 0x00, 0x00,
722  0x00, 0x00, 0x00, 0x00}, 8, &pos), INT64_MIN);
723 }
724 END_TEST
725 
726 START_TEST(test_packet_to_float)
727 {
728  size_t pos;
729  char buf[MAX_BUF], buf2[MAX_BUF];
730 
731  pos = 0;
732  snprintf(VS(buf), "%f", 1.0);
733  snprintf(VS(buf2), "%f", packet_to_float((uint8_t []) {0x3F, 0x80,
734  0x00, 0x00}, 4, &pos));
735  ck_assert_str_eq(buf, buf2);
736 
737  pos = 0;
738  snprintf(VS(buf), "%f", 0.0001);
739  snprintf(VS(buf2), "%f", packet_to_float((uint8_t []) {0x38, 0xD1,
740  0xB7, 0x17}, 4, &pos));
741  ck_assert_str_eq(buf, buf2);
742 
743  pos = 0;
744  snprintf(VS(buf), "%f", -10.0);
745  snprintf(VS(buf2), "%f", packet_to_float((uint8_t []) {0xC1, 0x20,
746  0x00, 0x00}, 4, &pos));
747  ck_assert_str_eq(buf, buf2);
748 
749  pos = 0;
750  snprintf(VS(buf), "%f", 12345678.);
751  snprintf(VS(buf2), "%f", packet_to_float((uint8_t []) {0x4B, 0x3C,
752  0x61, 0x4E}, 4, &pos));
753  ck_assert_str_eq(buf, buf2);
754 }
755 END_TEST
756 
757 START_TEST(test_packet_to_double)
758 {
759  size_t pos;
760  char buf[MAX_BUF], buf2[MAX_BUF];
761 
762  pos = 0;
763  snprintf(VS(buf), "%f", 1.0);
764  snprintf(VS(buf2), "%f", packet_to_double((uint8_t []) {0x3F, 0xF0,
765  0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8, &pos));
766  ck_assert_str_eq(buf, buf2);
767 
768  pos = 0;
769  snprintf(VS(buf), "%f", 0.0001);
770  snprintf(VS(buf2), "%f", packet_to_double((uint8_t []) {0x3F, 0x1A,
771  0x36, 0xE2, 0xEB, 0x1C, 0x43, 0x2D}, 8, &pos));
772  ck_assert_str_eq(buf, buf2);
773 
774  pos = 0;
775  snprintf(VS(buf), "%f", -10.0);
776  snprintf(VS(buf2), "%f", packet_to_double((uint8_t []) {0xC0, 0x24,
777  0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8, &pos));
778  ck_assert_str_eq(buf, buf2);
779 
780  pos = 0;
781  snprintf(VS(buf), "%f", 123456789.0);
782  snprintf(VS(buf2), "%f", packet_to_double((uint8_t []) {0x41, 0x9D,
783  0x6F, 0x34, 0x54, 0x00, 0x00, 0x00}, 8, &pos));
784  ck_assert_str_eq(buf, buf2);
785 
786  pos = 0;
787  snprintf(VS(buf), "%f", -109.56);
788  snprintf(VS(buf2), "%f", packet_to_double((uint8_t []) {0xC0, 0x5B,
789  0x63, 0xD7, 0x0A, 0x3D, 0x70, 0xA4}, 8, &pos));
790  ck_assert_str_eq(buf, buf2);
791 }
792 END_TEST
793 
794 START_TEST(test_packet_to_string)
795 {
796  size_t pos;
797  char buf[MAX_BUF], buf2[2];
798 
799  pos = 0;
800  ck_assert_str_eq(packet_to_string((uint8_t []) {0x74, 0x65,
801  0x73, 0x74, 0x00}, 5, &pos, VS(buf)), "test");
802 
803  pos = 0;
804  ck_assert_str_eq(packet_to_string((uint8_t []) {0x74, 0x65,
805  0x73, 0x74, 0x00}, 4, &pos, VS(buf)), "test");
806 
807  pos = 0;
808  ck_assert_str_eq(packet_to_string((uint8_t []) {0x74, 0x65,
809  0x73, 0x74}, 4, &pos, VS(buf)), "test");
810 
811  pos = 0;
812  ck_assert_str_eq(packet_to_string((uint8_t []) {0x74, 0x65,
813  0x73, 0x74}, 4, &pos, VS(buf2)), "t");
814 
815  pos = 0;
816  ck_assert_str_eq(packet_to_string((uint8_t []) {0x74, 0x65,
817  0x73, 0x74, 0x00}, 5, &pos, VS(buf2)), "t");
818 }
819 END_TEST
820 
821 START_TEST(test_packet_to_stringbuffer)
822 {
823  size_t pos;
824  StringBuffer *sb;
825  char *cp;
826 
827  pos = 0;
828  sb = stringbuffer_new();
829  packet_to_stringbuffer((uint8_t []) {0x74, 0x65, 0x73, 0x74, 0x00}, 5,
830  &pos, sb);
831  cp = stringbuffer_finish(sb);
832  ck_assert_str_eq(cp, "test");
833  efree(cp);
834 
835  pos = 0;
836  sb = stringbuffer_new();
837  packet_to_stringbuffer((uint8_t []) {0x74, 0x65, 0x73, 0x74, 0x00}, 4,
838  &pos, sb);
839  cp = stringbuffer_finish(sb);
840  ck_assert_str_eq(cp, "test");
841  efree(cp);
842 
843  pos = 0;
844  sb = stringbuffer_new();
845  packet_to_stringbuffer((uint8_t []) {0x74, 0x65, 0x73, 0x74}, 4,
846  &pos, sb);
847  cp = stringbuffer_finish(sb);
848  ck_assert_str_eq(cp, "test");
849  efree(cp);
850 }
851 END_TEST
852 
853 static Suite *suite(void)
854 {
855  Suite *s = suite_create("packet");
856  TCase *tc_core = tcase_create("Core");
857 
858  tcase_add_unchecked_fixture(tc_core, check_setup, check_teardown);
859  tcase_add_checked_fixture(tc_core, check_test_setup, check_test_teardown);
860 
861  suite_add_tcase(s, tc_core);
862  tcase_add_test(tc_core, test_packet_new);
863  tcase_add_test(tc_core, test_packet_dup);
864  tcase_add_test(tc_core, test_packet_save);
865  tcase_add_test(tc_core, test_packet_load);
866  tcase_add_test(tc_core, test_packet_append_uint8);
867  tcase_add_test(tc_core, test_packet_append_int8);
868  tcase_add_test(tc_core, test_packet_append_uint16);
869  tcase_add_test(tc_core, test_packet_append_int16);
870  tcase_add_test(tc_core, test_packet_append_uint32);
871  tcase_add_test(tc_core, test_packet_append_int32);
872  tcase_add_test(tc_core, test_packet_append_uint64);
873  tcase_add_test(tc_core, test_packet_append_int64);
874  tcase_add_test(tc_core, test_packet_append_float);
875  tcase_add_test(tc_core, test_packet_append_double);
876  tcase_add_test(tc_core, test_packet_append_data_len);
877  tcase_add_test(tc_core, test_packet_append_string_len);
878  tcase_add_test(tc_core, test_packet_append_string);
879  tcase_add_test(tc_core, test_packet_append_string_len_terminated);
880  tcase_add_test(tc_core, test_packet_append_string_terminated);
881  tcase_add_test(tc_core, test_packet_append_packet);
882  tcase_add_test(tc_core, test_packet_to_uint8);
883  tcase_add_test(tc_core, test_packet_to_int8);
884  tcase_add_test(tc_core, test_packet_to_uint16);
885  tcase_add_test(tc_core, test_packet_to_int16);
886  tcase_add_test(tc_core, test_packet_to_uint32);
887  tcase_add_test(tc_core, test_packet_to_int32);
888  tcase_add_test(tc_core, test_packet_to_uint64);
889  tcase_add_test(tc_core, test_packet_to_int64);
890  tcase_add_test(tc_core, test_packet_to_float);
891  tcase_add_test(tc_core, test_packet_to_double);
892  tcase_add_test(tc_core, test_packet_to_string);
893  tcase_add_test(tc_core, test_packet_to_stringbuffer);
894 
895  return s;
896 }
897 
898 void check_server_packet(void)
899 {
900  check_run_suite(suite(), __FILE__);
901 }
union @21 data
Data about the rule.