1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
|
/* libcoap unit tests
*
* Copyright (C) 2012,2015 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#include "coap_config.h"
#include "test_options.h"
#include <coap.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/************************************************************************
** decoder tests
************************************************************************/
static void
t_parse_option1(void) {
/* delta == 0, length == 0, value == 0 */
coap_str_const_t teststr = { 1, (const uint8_t *)"" };
size_t result;
coap_option_t option;
/* result = coap_opt_parse(teststr.s, teststr.s + teststr.length, &option); */
result = coap_opt_parse(teststr.s, teststr.length, &option);
CU_ASSERT(result == 1);
CU_ASSERT(option.delta == 0);
CU_ASSERT(option.length == 0);
/* FIXME: value? */
}
static void
t_parse_option2(void) {
/* delta == 12, length == 1, value == 0 */
coap_str_const_t teststr = { 2, (const uint8_t *)"\xc1" };
size_t result;
coap_option_t option;
result = coap_opt_parse(teststr.s, teststr.length, &option);
CU_ASSERT(result == 2);
CU_ASSERT(option.delta == 12);
CU_ASSERT(option.length == 1);
CU_ASSERT(option.value == teststr.s + 1);
}
static void
t_parse_option3(void) {
/* delta == 3, length == 12, value == 0 */
coap_str_const_t teststr = { 13, (const uint8_t *)"\x3c\x00\x01\x02\x03\x04"
"\x05\x06\x07\x08\x09\x0a\x0b" };
size_t result;
coap_option_t option;
result = coap_opt_parse(teststr.s, teststr.length, &option);
CU_ASSERT(result == 13);
CU_ASSERT(option.delta == 3);
CU_ASSERT(option.length == 12);
CU_ASSERT(option.value == teststr.s + 1);
/* CU_ASSERT(memcmp(option.value, teststr.s + 1, 12) == 0); */
}
static void
t_parse_option4(void) {
/* delta == 15, length == 3, value == 0 */
coap_str_const_t teststr = { 2, (const uint8_t *)"\xf3" };
size_t result;
coap_option_t option;
result = coap_opt_parse(teststr.s, teststr.length, &option);
CU_ASSERT(result == 0);
}
static void
t_parse_option5(void) {
/* delta == 3, length == 15, value == 0 */
coap_str_const_t teststr = { 2, (const uint8_t *)"\x3f" };
size_t result;
coap_option_t option;
result = coap_opt_parse(teststr.s, teststr.length, &option);
CU_ASSERT(result == 0);
}
static void
t_parse_option6(void) {
/* delta == 15, length == 15 */
coap_str_const_t teststr = { 1, (const uint8_t *)"\xff" };
size_t result;
coap_option_t option;
result = coap_opt_parse(teststr.s, teststr.length, &option);
CU_ASSERT(result == 0);
}
static void
t_parse_option7(void) {
/* delta == 20, length == 0 */
coap_str_const_t teststr = { 2, (const uint8_t *)"\xd0\x07" };
size_t result;
coap_option_t option;
result = coap_opt_parse(teststr.s, teststr.length, &option);
CU_ASSERT(result == 2);
CU_ASSERT(option.delta == 20);
CU_ASSERT(option.length == 0);
}
static void
t_parse_option8(void) {
/* delta == 780, length == 0 */
coap_str_const_t teststr = { 3, (const uint8_t *)"\xe0\x01\xff" };
size_t result;
coap_option_t option;
result = coap_opt_parse(teststr.s, teststr.length, &option);
CU_ASSERT(result == 3);
CU_ASSERT(option.delta == 780);
CU_ASSERT(option.length == 0);
}
static void
t_parse_option9(void) {
/* delta == 65535, length == 0 */
coap_str_const_t teststr = { 3, (const uint8_t *)"\xe0\xfe\xf2" };
size_t result;
coap_option_t option;
result = coap_opt_parse(teststr.s, teststr.length, &option);
CU_ASSERT(result == 3);
CU_ASSERT(option.delta == 65535);
}
static void
t_parse_option10(void) {
/* delta > 65535 (illegal), length == 0 */
coap_str_const_t teststr = { 3, (const uint8_t *)"\xe0\xff\xff" };
size_t result;
coap_option_t option;
result = coap_opt_parse(teststr.s, teststr.length, &option);
CU_ASSERT(result == 0);
}
static void
t_parse_option11(void) {
/* illegal delta value (option too short) */
coap_str_const_t teststr = { 1, (const uint8_t *)"\xd0" };
size_t result;
coap_option_t option;
result = coap_opt_parse(teststr.s, teststr.length, &option);
CU_ASSERT(result == 0);
}
static void
t_parse_option12(void) {
/* delta == 280, length == 500 */
coap_str_const_t teststr = { 3, (const uint8_t *)"\xee\xff\x0b" };
size_t result;
coap_option_t option;
result = coap_opt_parse(teststr.s, teststr.length, &option);
CU_ASSERT(result == 0);
}
static void
t_parse_option13(void) {
/* delta == 280, length == 500 */
unsigned char _data[505];
coap_string_t teststr = { sizeof(_data), _data };
teststr.s[0] = 0xee;
teststr.s[1] = 0x00;
teststr.s[2] = 0x0b;
teststr.s[3] = 0x00;
teststr.s[4] = 0xe7;
size_t result;
coap_option_t option;
result = coap_opt_parse(teststr.s, teststr.length, &option);
CU_ASSERT(result == sizeof(_data));
CU_ASSERT(option.delta == 280);
CU_ASSERT(option.length == 500);
CU_ASSERT(option.value == &_data[5]);
}
static void
t_parse_option14(void) {
/* delta == 268, length == 65535 */
unsigned char *data;
unsigned int length = 4 + 65535;
data = (unsigned char *)malloc(length);
if (!data) {
CU_FAIL("internal error in test framework -- insufficient memory\n");
return;
}
data[0] = 0xde;
data[1] = 0xff;
data[2] = 0xfe;
data[3] = 0xf2;
size_t result;
coap_option_t option;
result = coap_opt_parse(data, length, &option);
CU_ASSERT(result == length);
CU_ASSERT(option.delta == 268);
CU_ASSERT(option.length == 65535);
CU_ASSERT(option.value == &data[4]);
free(data);
}
/************************************************************************
** encoder tests
************************************************************************/
static void
t_encode_option1(void) {
char teststr[] = { 0x00 };
unsigned char buf[40];
size_t result;
result = coap_opt_setheader((coap_opt_t *)buf, sizeof(buf), 0, 0);
CU_ASSERT(result == sizeof(teststr));
CU_ASSERT(memcmp(buf, teststr, result) == 0);
}
static void
t_encode_option2(void) {
uint8_t teststr[] = { 0x5d, 0xff };
unsigned char buf[40];
size_t result;
result = coap_opt_setheader((coap_opt_t *)buf, sizeof(buf), 5, 268);
CU_ASSERT(result == sizeof(teststr));
CU_ASSERT(memcmp(buf, teststr, result) == 0);
}
static void
t_encode_option3(void) {
uint8_t teststr[] = { 0xd1, 0x01 };
unsigned char buf[40];
size_t result;
result = coap_opt_setheader((coap_opt_t *)buf, sizeof(buf), 14, 1);
CU_ASSERT(result == sizeof(teststr));
CU_ASSERT(memcmp(buf, teststr, result) == 0);
}
static void
t_encode_option4(void) {
uint8_t teststr[] = { 0xdd, 0xff, 0xab };
unsigned char buf[40];
size_t result;
result = coap_opt_setheader((coap_opt_t *)buf, sizeof(buf), 268, 184);
CU_ASSERT(result == sizeof(teststr));
CU_ASSERT(memcmp(buf, teststr, result) == 0);
}
static void
t_encode_option5(void) {
uint8_t teststr[] = { 0xed, 0x13, 0x00, 0xff };
unsigned char buf[40];
size_t result;
result = coap_opt_setheader((coap_opt_t *)buf, sizeof(buf), 5133, 268);
CU_ASSERT(result == sizeof(teststr));
CU_ASSERT(memcmp(buf, teststr, result) == 0);
}
static void
t_encode_option6(void) {
uint8_t teststr[] = { 0xee, 0xfe, 0xf2, 0xfe, 0xf2 };
unsigned char buf[40];
size_t result;
result = coap_opt_setheader((coap_opt_t *)buf, sizeof(buf), 65535, 65535);
CU_ASSERT(result == sizeof(teststr));
CU_ASSERT(memcmp(buf, teststr, result) == 0);
}
static void
t_encode_option7(void) {
uint8_t teststr[] = { 0x35, 'v', 'a', 'l', 'u', 'e' };
const size_t valoff = 1;
unsigned char buf[40];
size_t result;
result = coap_opt_encode((coap_opt_t *)buf, sizeof(buf), 3,
(unsigned char *)teststr + valoff,
sizeof(teststr) - valoff);
CU_ASSERT(result == sizeof(teststr));
CU_ASSERT(memcmp(buf, teststr, result) == 0);
}
static void
t_encode_option8(void) {
/* value does not fit in message buffer */
unsigned char buf[40];
size_t result;
result = coap_opt_encode((coap_opt_t *)buf, 8, 15,
(const uint8_t *)"something", 9);
CU_ASSERT(result == 0);
result = coap_opt_encode((coap_opt_t *)buf, 1, 15,
(const uint8_t *)"something", 9);
CU_ASSERT(result == 0);
}
static void
t_encode_option9(void) {
uint8_t teststr[] = { 0xe1, 0x00, 0x00 };
unsigned char buf[40] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
size_t result;
result = coap_opt_setheader((coap_opt_t *)buf, sizeof(buf), 269, 1);
CU_ASSERT(result == sizeof(teststr));
CU_ASSERT(memcmp(buf, teststr, result) == 0);
}
/************************************************************************
** accessor tests
************************************************************************/
static void
t_access_option1(void) {
const uint8_t teststr[] = { 0x12, 'a', 'b' };
CU_ASSERT(coap_opt_delta((const coap_opt_t *)teststr) == 1);
CU_ASSERT(coap_opt_length((const coap_opt_t *)teststr) == 2);
CU_ASSERT_PTR_EQUAL_C(coap_opt_value((const coap_opt_t *)teststr), teststr + 1);
CU_ASSERT(coap_opt_size((const coap_opt_t *)teststr) == sizeof(teststr));
}
static void
t_access_option2(void) {
const uint8_t teststr[] = { 0xe2, 0x18, 0xfd, 'a', 'b' };
CU_ASSERT(coap_opt_delta((const coap_opt_t *)teststr) == 6666);
CU_ASSERT(coap_opt_length((const coap_opt_t *)teststr) == 2);
CU_ASSERT_PTR_EQUAL_C(coap_opt_value((const coap_opt_t *)teststr), teststr + 3);
CU_ASSERT(coap_opt_size((const coap_opt_t *)teststr) == sizeof(teststr));
}
static void
t_access_option3(void) {
const uint8_t teststr[] = { 0xed, 0x18, 0x0a, 0x00, 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm'
};
CU_ASSERT(coap_opt_delta((const coap_opt_t *)teststr) == 6423);
CU_ASSERT(coap_opt_length((const coap_opt_t *)teststr) == 13);
CU_ASSERT_PTR_EQUAL_C(coap_opt_value((const coap_opt_t *)teststr), teststr + 4);
CU_ASSERT(coap_opt_size((const coap_opt_t *)teststr) == sizeof(teststr));
}
static void
t_access_option4(void) {
const uint8_t teststr[] = { 0xde, 0xff, 0xfe, 0xf2, 'a', 'b', 'c' };
CU_ASSERT(coap_opt_delta((const coap_opt_t *)teststr) == 268);
CU_ASSERT(coap_opt_length((const coap_opt_t *)teststr) == 65535);
CU_ASSERT_PTR_EQUAL_C(coap_opt_value((const coap_opt_t *)teststr), teststr + 4);
CU_ASSERT(coap_opt_size((const coap_opt_t *)teststr) == 65535 + 4);
}
static void
t_access_option5(void) {
const uint8_t teststr[] = { 0xee, 0xfe, 0xf2, 0x00, 0xdd, 'a', 'b', 'c' };
CU_ASSERT(coap_opt_delta((const coap_opt_t *)teststr) == 65535);
CU_ASSERT(coap_opt_length((const coap_opt_t *)teststr) == 490);
CU_ASSERT_PTR_EQUAL_C(coap_opt_value((const coap_opt_t *)teststr), teststr + 5);
CU_ASSERT(coap_opt_size((const coap_opt_t *)teststr) == 495);
}
static void
t_access_option6(void) {
coap_log_t level = coap_get_log_level();
const uint8_t teststr[] = { 0xf2, 'a', 'b' };
coap_set_log_level(LOG_CRIT);
CU_ASSERT(coap_opt_delta((const coap_opt_t *)teststr) == 0);
coap_set_log_level(level);
CU_ASSERT(coap_opt_length((const coap_opt_t *)teststr) == 0);
CU_ASSERT_PTR_EQUAL_C(coap_opt_value((const coap_opt_t *)teststr), NULL);
CU_ASSERT(coap_opt_size((const coap_opt_t *)teststr) == 0);
}
static void
t_access_option7(void) {
const uint8_t teststr[] = { 0x2f, 'a', 'b' };
CU_ASSERT(coap_opt_delta((const coap_opt_t *)teststr) == 2);
CU_ASSERT(coap_opt_length((const coap_opt_t *)teststr) == 0);
CU_ASSERT_PTR_EQUAL_C(coap_opt_value((const coap_opt_t *)teststr), NULL);
CU_ASSERT(coap_opt_size((const coap_opt_t *)teststr) == 0);
}
/************************************************************************
** accessor tests
************************************************************************/
#define TEST_MAX_SIZE 1000
#ifdef _MSC_VER
# define ALIGNED(x)
#else
# define ALIGNED(x) __attribute__ ((aligned (x)))
#endif
static void
t_iterate_option1(void) {
/* CoAP PDU without token, options, or data */
uint8_t teststr[] ALIGNED(8) = {
0x00, 0x00, 0x00, 0x00
};
coap_pdu_t pdu = {
.max_size = TEST_MAX_SIZE,
.token = teststr,
.used_size = 0
};
coap_opt_iterator_t oi, *result;
coap_opt_t *option;
result = coap_option_iterator_init(&pdu, &oi, COAP_OPT_ALL);
CU_ASSERT(result == NULL);
CU_ASSERT(oi.bad == 1);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 1);
CU_ASSERT(option == NULL);
}
static void
t_iterate_option2(void) {
/* CoAP PDU with token but without options and data */
uint8_t teststr[3] ALIGNED(8) = {
't', 'o', 'k'
};
coap_pdu_t pdu = {
.max_size = TEST_MAX_SIZE,
.token_length = 3,
.token = teststr,
.used_size = sizeof(teststr)
};
coap_opt_iterator_t oi, *result;
coap_opt_t *option;
result = coap_option_iterator_init(&pdu, &oi, COAP_OPT_ALL);
CU_ASSERT(result == NULL);
CU_ASSERT(oi.bad == 1);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 1);
CU_ASSERT(option == NULL);
}
static void
t_iterate_option3(void) {
/* CoAP PDU with token and options */
uint8_t teststr[] ALIGNED(8) = {
't', 'o', 'k', 0x13,
'o', 'p', 't', 0x00, 0xd1, 0x10, 'x'
};
coap_pdu_t pdu = {
.max_size = TEST_MAX_SIZE,
.token_length = 3,
.token = teststr,
.used_size = sizeof(teststr)
};
coap_opt_iterator_t oi, *result;
coap_opt_t *option;
result = coap_option_iterator_init(&pdu, &oi, COAP_OPT_ALL);
CU_ASSERT_PTR_EQUAL(result, &oi);
CU_ASSERT(oi.bad == 0);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 1);
CU_ASSERT_PTR_EQUAL(option, teststr + 3);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 1);
CU_ASSERT_PTR_EQUAL(option, teststr + 7);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 30);
CU_ASSERT_PTR_EQUAL(option, teststr + 8);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 1);
CU_ASSERT_PTR_EQUAL(option, NULL);
}
static void
t_iterate_option4(void) {
/* CoAP PDU with token, options, and data */
uint8_t teststr[] ALIGNED(8) = {
't', 'o', 'k', 0x13,
'o', 'p', 't', 0x00, 0xd1, 0x10, 'x', 0xff,
'd', 'a', 't', 'a'
};
coap_pdu_t pdu = {
.max_size = TEST_MAX_SIZE,
.token_length = 3,
.token = teststr,
.used_size = sizeof(teststr)
};
coap_opt_iterator_t oi, *result;
coap_opt_t *option;
result = coap_option_iterator_init(&pdu, &oi, COAP_OPT_ALL);
CU_ASSERT_PTR_EQUAL(result, &oi);
CU_ASSERT(oi.bad == 0);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 1);
CU_ASSERT_PTR_EQUAL(option, teststr + 3);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 1);
CU_ASSERT_PTR_EQUAL(option, teststr + 7);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 30);
CU_ASSERT_PTR_EQUAL(option, teststr + 8);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 1);
CU_ASSERT_PTR_EQUAL(option, NULL);
}
static void
t_iterate_option5(void) {
/* CoAP PDU with malformed option */
uint8_t teststr[] ALIGNED(8) = {
0x52, 'o', 'p', 0xee,
0x12, 0x03, 0x00
};
coap_pdu_t pdu = {
.max_size = TEST_MAX_SIZE,
.token_length = 0,
.token = teststr,
.used_size = sizeof(teststr)
};
coap_opt_iterator_t oi, *result;
coap_opt_t *option;
result = coap_option_iterator_init(&pdu, &oi, COAP_OPT_ALL);
CU_ASSERT_PTR_EQUAL(result, &oi);
CU_ASSERT(oi.bad == 0);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 5);
CU_ASSERT_PTR_EQUAL(option, teststr);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 1);
CU_ASSERT_PTR_EQUAL(option, NULL);
}
static void
t_iterate_option6(void) {
/* option filter */
/* CoAP PDU with token, options, and data */
uint8_t teststr[] ALIGNED(8) = {
0x80, 0x20, 0x00, 0x00,
0xc0, 0x00
};
coap_pdu_t pdu = {
.max_size = TEST_MAX_SIZE,
.token_length = 0,
.token = teststr,
.used_size = sizeof(teststr)
};
coap_opt_iterator_t oi, *result;
coap_opt_t *option;
coap_opt_filter_t filter;
coap_option_filter_clear(filter);
coap_option_setb(filter, 10); /* option nr 10 only */
result = coap_option_iterator_init(&pdu, &oi, filter);
CU_ASSERT_PTR_EQUAL(result, &oi);
CU_ASSERT(oi.bad == 0);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 10);
CU_ASSERT_PTR_EQUAL(option, teststr + 1);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 10);
CU_ASSERT_PTR_EQUAL(option, teststr + 2);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 10);
CU_ASSERT_PTR_EQUAL(option, teststr + 3);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 1);
CU_ASSERT_PTR_EQUAL(option, NULL);
}
static void
t_iterate_option7(void) {
/* option filter */
uint8_t teststr[] ALIGNED(8) = {
0x80, 0x20, 0x00, 0x00,
0xc0, 0x00, 0x10, 0x10, 0x00
};
coap_pdu_t pdu = {
.max_size = TEST_MAX_SIZE,
.token_length = 0,
.token = teststr,
.used_size = sizeof(teststr)
};
coap_opt_iterator_t oi, *result;
coap_opt_t *option;
coap_opt_filter_t filter;
/* search options nr 8 and 22 */
coap_option_filter_clear(filter);
coap_option_setb(filter, 8);
coap_option_setb(filter, 22);
result = coap_option_iterator_init(&pdu, &oi, filter);
CU_ASSERT_PTR_EQUAL(result, &oi);
CU_ASSERT(oi.bad == 0);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 8);
CU_ASSERT_PTR_EQUAL(option, teststr);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 22);
CU_ASSERT_PTR_EQUAL(option, teststr + 4);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 22);
CU_ASSERT_PTR_EQUAL(option, teststr + 5);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 1);
CU_ASSERT_PTR_EQUAL(option, NULL);
}
static void
t_iterate_option8(void) {
/* option filter */
uint8_t teststr[] ALIGNED(8) = {
0x80, 0x20, 0x00, 0x00,
0xc0, 0x00, 0x10, 0x10, 0x00
};
coap_pdu_t pdu = {
.max_size = TEST_MAX_SIZE,
.token_length = 0,
.token = teststr,
.used_size = sizeof(teststr)
};
coap_opt_iterator_t oi, *result;
coap_opt_t *option;
coap_opt_filter_t filter;
/* search option nr 36 */
coap_option_filter_clear(filter);
coap_option_setb(filter, 36);
result = coap_option_iterator_init(&pdu, &oi, filter);
CU_ASSERT_PTR_EQUAL(result, &oi);
CU_ASSERT(oi.bad == 0);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 1);
CU_ASSERT_PTR_EQUAL(option, NULL);
}
static void
t_iterate_option9(void) {
/* options filter: option number too large for filter */
uint8_t teststr[] ALIGNED(8) = {
0x80, 0x20, 0x00, 0x00,
0xc0, 0x00, 0x10, 0x10, 0x00
};
coap_pdu_t pdu = {
.max_size = TEST_MAX_SIZE,
.token_length = 0,
.token = teststr,
.used_size = sizeof(teststr)
};
coap_opt_iterator_t oi, *result;
coap_opt_t *option;
coap_opt_filter_t filter;
/* search option nr 100 */
coap_option_filter_clear(filter);
coap_option_setb(filter, 100);
result = coap_option_iterator_init(&pdu, &oi, filter);
CU_ASSERT_PTR_EQUAL(result, &oi);
CU_ASSERT(oi.bad == 0);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 1);
CU_ASSERT_PTR_EQUAL(option, NULL);
}
static void
t_iterate_option10(void) {
/* options filter: option numbers in PDU exceed filter size */
uint8_t teststr[] ALIGNED(8) = {
0x80, 0x20, 0x00, 0x00,
0xd0, 0x26, 0xe0, 0x10, 0x00
};
coap_pdu_t pdu = {
.max_size = TEST_MAX_SIZE,
.token_length = 0,
.token = teststr,
.used_size = sizeof(teststr)
};
coap_opt_iterator_t oi, *result;
coap_opt_t *option;
coap_opt_filter_t filter;
/* search option nr 61 */
coap_option_filter_clear(filter);
coap_option_setb(filter, 61);
result = coap_option_iterator_init(&pdu, &oi, filter);
CU_ASSERT_PTR_EQUAL(result, &oi);
CU_ASSERT(oi.bad == 0);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT_PTR_EQUAL(option, teststr + 4);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 1);
CU_ASSERT_PTR_EQUAL(option, NULL);
}
/************************************************************************
** filter tests
************************************************************************/
static void
t_filter_option1(void) {
coap_opt_filter_t filter;
coap_option_filter_clear(filter);
CU_ASSERT(coap_option_filter_set(filter, 0) == 1);
CU_ASSERT(coap_option_filter_set(filter, 37) == 1);
CU_ASSERT(coap_option_filter_set(filter, 37) == 1);
CU_ASSERT(coap_option_filter_set(filter, 43) == 1);
CU_ASSERT(coap_option_filter_set(filter, 290) == 1);
CU_ASSERT(coap_option_filter_set(filter, 65535) == 1);
CU_ASSERT(coap_option_filter_get(filter, 0) == 1);
CU_ASSERT(coap_option_filter_get(filter, 37) == 1);
CU_ASSERT(coap_option_filter_get(filter, 43) == 1);
CU_ASSERT(coap_option_filter_get(filter, 290) == 1);
CU_ASSERT(coap_option_filter_get(filter, 65535) == 1);
CU_ASSERT(coap_option_filter_unset(filter, 37) == 1);
CU_ASSERT(coap_option_filter_get(filter, 0) == 1);
CU_ASSERT(coap_option_filter_get(filter, 43) == 1);
CU_ASSERT(coap_option_filter_get(filter, 290) == 1);
CU_ASSERT(coap_option_filter_get(filter, 65535) == 1);
CU_ASSERT(coap_option_filter_get(filter, 37) == 0);
CU_ASSERT(coap_option_filter_get(filter, 89) == 0);
}
static void
t_filter_option2(void) {
coap_opt_filter_t filter;
int s;
coap_option_filter_clear(filter);
/* fill all COAP_OPT_FILTER_SHORT slots */
for (s = 0; s < COAP_OPT_FILTER_SHORT; s++) {
CU_ASSERT(coap_option_filter_set(filter, s));
}
/* adding a short option type must fail */
CU_ASSERT(coap_option_filter_set(filter, COAP_OPT_FILTER_SHORT) == 0);
/* adding a long option type must succeed */
CU_ASSERT(coap_option_filter_set(filter, 256) == 1);
}
static void
t_filter_option3(void) {
coap_opt_filter_t filter;
int l;
coap_option_filter_clear(filter);
/* set COAP_OPT_FILTER_LONG long filters */
for (l = 0; l < COAP_OPT_FILTER_LONG; l++) {
CU_ASSERT(coap_option_filter_set(filter, 256 + l) == 1);
}
/* the next must fail and must not be found */
CU_ASSERT(coap_option_filter_set(filter, 256 + COAP_OPT_FILTER_LONG) == 0);
CU_ASSERT(coap_option_filter_get(filter, 256 + COAP_OPT_FILTER_LONG) == 0);
/* remove one item */
CU_ASSERT(coap_option_filter_unset(filter, 256) == 1);
CU_ASSERT(coap_option_filter_get(filter, 256) == 0);
/* now, storing a new filter must succeed */
CU_ASSERT(coap_option_filter_set(filter, 256 + COAP_OPT_FILTER_LONG) == 1);
CU_ASSERT(coap_option_filter_get(filter, 256 + COAP_OPT_FILTER_LONG) == 1);
/* and all other items must be available as well */
for (l = 0; l < COAP_OPT_FILTER_LONG; l++) {
CU_ASSERT(coap_option_filter_get(filter, 256 + l + 1) == 1);
}
/* set COAP_OPT_FILTER_SHORT short filters */
for (l = 0; l < COAP_OPT_FILTER_SHORT; l++) {
CU_ASSERT(coap_option_filter_set(filter, l) == 1);
}
/* the next must fail and must not be found */
CU_ASSERT(coap_option_filter_set(filter, COAP_OPT_FILTER_SHORT) == 0);
CU_ASSERT(coap_option_filter_get(filter, COAP_OPT_FILTER_SHORT) == 0);
/* remove one item */
CU_ASSERT(coap_option_filter_unset(filter, 0) == 1);
CU_ASSERT(coap_option_filter_get(filter, 0) == 0);
/* now, storing a new filter must succeed */
CU_ASSERT(coap_option_filter_set(filter, COAP_OPT_FILTER_SHORT) == 1);
CU_ASSERT(coap_option_filter_get(filter, COAP_OPT_FILTER_SHORT) == 1);
/* and all other items must be available as well */
for (l = 0; l < COAP_OPT_FILTER_SHORT; l++) {
CU_ASSERT(coap_option_filter_get(filter, l + 1) == 1);
}
}
/************************************************************************
** initialization
************************************************************************/
CU_pSuite
t_init_option_tests(void) {
CU_pSuite suite[5];
suite[0] = CU_add_suite("option parser", NULL, NULL);
if (!suite[0]) { /* signal error */
fprintf(stderr, "W: cannot add option parser test suite (%s)\n",
CU_get_error_msg());
return NULL;
}
#define OPTION_TEST(n,s) \
if (!CU_add_test(suite[0], s, t_parse_option##n)) { \
fprintf(stderr, "W: cannot add option parser test (%s)\n", \
CU_get_error_msg()); \
}
OPTION_TEST(1, "parse option #1");
OPTION_TEST(2, "parse option #2");
OPTION_TEST(3, "parse option #3");
OPTION_TEST(4, "parse option #4");
OPTION_TEST(5, "parse option #5");
OPTION_TEST(6, "parse option #6");
OPTION_TEST(7, "parse option #7");
OPTION_TEST(8, "parse option #8");
OPTION_TEST(9, "parse option #9");
OPTION_TEST(10, "parse option #10");
OPTION_TEST(11, "parse option #11");
OPTION_TEST(12, "parse option #12");
OPTION_TEST(13, "parse option #13");
OPTION_TEST(14, "parse option #14");
if ((suite[1] = CU_add_suite("option encoder", NULL, NULL))) {
#define OPTION_ENCODER_TEST(n,s) \
if (!CU_add_test(suite[1], s, t_encode_option##n)) { \
fprintf(stderr, "W: cannot add option encoder test (%s)\n", \
CU_get_error_msg()); \
}
OPTION_ENCODER_TEST(1, "encode option #1");
OPTION_ENCODER_TEST(2, "encode option #2");
OPTION_ENCODER_TEST(3, "encode option #3");
OPTION_ENCODER_TEST(4, "encode option #4");
OPTION_ENCODER_TEST(5, "encode option #5");
OPTION_ENCODER_TEST(6, "encode option #6");
OPTION_ENCODER_TEST(7, "encode option #7");
OPTION_ENCODER_TEST(8, "encode option #8");
OPTION_ENCODER_TEST(9, "encode option #9");
} else {
fprintf(stderr, "W: cannot add option encoder test suite (%s)\n",
CU_get_error_msg());
}
if ((suite[2] = CU_add_suite("option accessors", NULL, NULL))) {
#define OPTION_ACCESSOR_TEST(n,s) \
if (!CU_add_test(suite[2], s, t_access_option##n)) { \
fprintf(stderr, "W: cannot add option accessor function test (%s)\n", \
CU_get_error_msg()); \
}
OPTION_ACCESSOR_TEST(1, "access option #1");
OPTION_ACCESSOR_TEST(2, "access option #2");
OPTION_ACCESSOR_TEST(3, "access option #3");
OPTION_ACCESSOR_TEST(4, "access option #4");
OPTION_ACCESSOR_TEST(5, "access option #5");
OPTION_ACCESSOR_TEST(6, "access option #6");
OPTION_ACCESSOR_TEST(7, "access option #7");
} else {
fprintf(stderr, "W: cannot add option acessor function test suite (%s)\n",
CU_get_error_msg());
}
if ((suite[3] = CU_add_suite("option iterator", NULL, NULL))) {
#define OPTION_ITERATOR_TEST(n,s) \
if (!CU_add_test(suite[3], s, t_iterate_option##n)) { \
fprintf(stderr, "W: cannot add option iterator test (%s)\n", \
CU_get_error_msg()); \
}
OPTION_ITERATOR_TEST(1, "option iterator #1");
OPTION_ITERATOR_TEST(2, "option iterator #2");
OPTION_ITERATOR_TEST(3, "option iterator #3");
OPTION_ITERATOR_TEST(4, "option iterator #4");
OPTION_ITERATOR_TEST(5, "option iterator #5");
OPTION_ITERATOR_TEST(6, "option iterator #6");
OPTION_ITERATOR_TEST(7, "option iterator #7");
OPTION_ITERATOR_TEST(8, "option iterator #8");
OPTION_ITERATOR_TEST(9, "option iterator #9");
OPTION_ITERATOR_TEST(10, "option iterator #10");
} else {
fprintf(stderr, "W: cannot add option iterator test suite (%s)\n",
CU_get_error_msg());
}
if ((suite[4] = CU_add_suite("option filter", NULL, NULL))) {
#define OPTION_FILTER_TEST(n,s) \
if (!CU_add_test(suite[4], s, t_filter_option##n)) { \
fprintf(stderr, "W: cannot add option filter test (%s)\n", \
CU_get_error_msg()); \
}
OPTION_FILTER_TEST(1, "option filter #1");
OPTION_FILTER_TEST(2, "option filter #2");
OPTION_FILTER_TEST(3, "option filter #3");
} else {
fprintf(stderr, "W: cannot add option filter test suite (%s)\n",
CU_get_error_msg());
}
return suite[0];
}
|