Loading...
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 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 | /* * Copyright (c) 2016 Intel Corporation. * * SPDX-License-Identifier: Apache-2.0 */ #include <tc_util.h> #include <mqtt_pkt.h> #include <misc/util.h> /* for ARRAY_SIZE */ #include <ztest.h> #define RC_STR(rc) (rc == TC_PASS ? PASS : FAIL) #define CLIENTID "zephyr" #define CLIENTID_LEN 6 #define TOPIC "sensors" #define TOPIC_LEN 7 #define WILL_TOPIC "quitting" #define WILL_TOPIC_LEN 8 #define USERNAME "zephyr1" #define USERNAME_LEN 7 /* MQTT messages in this test are under 256 bytes */ #define BUF_SIZE 256 static u8_t buf[BUF_SIZE]; static u16_t buf_len; /** * @brief MQTT test structure */ struct mqtt_test { /* test name, for example: "test connect 1" */ const char *test_name; /* cast to something like: * struct mqtt_connect_msg *msg_connect = (struct mqtt_connect_msg *)msg */ void *msg; /* pointer to the eval routine, for example: * eval_fcn = eval_msg_connect */ int (*eval_fcn)(struct mqtt_test *); /* expected result */ u8_t *expected; /* length of 'expected' */ u16_t expected_len; }; #define MAX_TOPICS 4 /** * @brief MQTT SUBSCRIBE msg */ struct msg_subscribe { u16_t pkt_id; int items; const char *topics[MAX_TOPICS]; enum mqtt_qos qos[MAX_TOPICS]; }; /** * @brief MQTT SUBACK msg */ struct msg_suback { u16_t pkt_id; int elements; enum mqtt_qos qos[MAX_TOPICS]; }; /** * @brief MQTT msg with Packet Identifier as payload: * PUBACK, PUBREC, PUBREL, PUBCOMP, UNSUBACK */ struct msg_pkt_id { u16_t pkt_id; }; /** * @brief eval_msg_connect Evaluate the given mqtt_test against the * connect packing/unpacking routines. * @param [in] mqtt_test MQTT test structure * @return TC_PASS on success * @return TC_FAIL on error */ static int eval_msg_connect(struct mqtt_test *mqtt_test); /** * @brief eval_msg_publish Evaluate the given mqtt_test against the * publish packing/unpacking routines. * @param [in] mqtt_test MQTT test structure * @return TC_PASS on success * @return TC_FAIL on error */ static int eval_msg_publish(struct mqtt_test *mqtt_test); /** * @brief eval_msg_subscribe Evaluate the given mqtt_test against the * subscribe packing/unpacking routines. * @param [in] mqtt_test MQTT test structure * @return TC_PASS on success * @return TC_FAIL on error */ static int eval_msg_subscribe(struct mqtt_test *mqtt_test); /** * @brief eval_msg_suback Evaluate the given mqtt_test against the * suback packing/unpacking routines. * @param [in] mqtt_test MQTT test structure * @return TC_PASS on success * @return TC_FAIL on error */ static int eval_msg_suback(struct mqtt_test *mqtt_test); /** * @brief eval_msg_pingreq Evaluate the given mqtt_test against the * pingreq packing/unpacking routines. * @param [in] mqtt_test MQTT test structure * @return TC_PASS on success * @return TC_FAIL on error */ static int eval_msg_pingreq(struct mqtt_test *mqtt_test); /** * @brief eval_msg_pingresp Evaluate the given mqtt_test against the * pingresp packing/unpacking routines. * @param [in] mqtt_test MQTT test structure * @return TC_PASS on success * @return TC_FAIL on error */ static int eval_msg_pingresp(struct mqtt_test *mqtt_test); /** * @brief eval_msg_puback Evaluate the given mqtt_test against the * puback routines. * @param [in] mqtt_test MQTT test structure * @return TC_PASS on success * @return TC_FAIL on error */ static int eval_msg_puback(struct mqtt_test *mqtt_test); /** * @brief eval_msg_puback Evaluate the given mqtt_test against the * pubcomp routines. * @param [in] mqtt_test MQTT test structure * @return TC_PASS on success * @return TC_FAIL on error */ static int eval_msg_pubcomp(struct mqtt_test *mqtt_test); /** * @brief eval_msg_pubrec Evaluate the given mqtt_test against the * pubrec routines. * @param [in] mqtt_test MQTT test structure * @return TC_PASS on success * @return TC_FAIL on error */ static int eval_msg_pubrec(struct mqtt_test *mqtt_test); /** * @brief eval_msg_pubrel Evaluate the given mqtt_test against the * pubrel routines. * @param [in] mqtt_test MQTT test structure * @return TC_PASS on success * @return TC_FAIL on error */ static int eval_msg_pubrel(struct mqtt_test *mqtt_test); /** * @brief eval_msg_unsuback Evaluate the given mqtt_test against the * unsuback routines. * @param [in] mqtt_test MQTT test structure * @return TC_PASS on success * @return TC_FAIL on error */ static int eval_msg_unsuback(struct mqtt_test *mqtt_test); /** * @brief eval_msg_disconnect Evaluate the given mqtt_test against the * disconnect routines. * @param [in] mqtt_test MQTT test structure * @return TC_PASS on success * @return TC_FAIL on error */ static int eval_msg_disconnect(struct mqtt_test *mqtt_test); /** * @brief eval_buffers Evaluate if two given buffers are equal * @param [in] buf Input buffer 1, mostly used as the 'computed' * buffer * @param [in] buf_len 'buf' length * @param [in] expected Expected buffer * @param [in] len 'expected' len * @return TC_PASS on success * @return TC_FAIL on error and prints both buffers */ static int eval_buffers(u8_t *buf, u16_t buf_len, u8_t *expected, u16_t len); /** * @brief print_array Prints the array 'a' of 'size' elements * @param a The array * @param size Array's size */ static void print_array(u8_t *a, u16_t size); /** * @brief test_strlen Computes the C string length allowing NULL as * input parameter * @param [in] str Input string * @return String length, assuming that strlen(NULL) is 0 */ static size_t test_strlen(const char *str); /* * MQTT CONNECT msg: * Clean session: 1 Client id: [6] 'zephyr' Will flag: 0 * Will QoS: 0 Will retain: 0 Will topic: [0] * Will msg: [0] Keep alive: 0 User name: [0] * Password: [0] * * Message can be generated by the following command: * mosquitto_sub -V mqttv311 -i zephyr -k 0 -t sensors */ static u8_t connect1[] = {0x10, 0x12, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54, 0x04, 0x02, 0x00, 0x00, 0x00, 0x06, 0x7a, 0x65, 0x70, 0x68, 0x79, 0x72}; static struct mqtt_connect_msg msg_connect1 = { .clean_session = 1, .client_id = CLIENTID, .client_id_len = CLIENTID_LEN, .will_flag = 0, .will_qos = 0, .will_retain = 0, .will_topic = NULL, .will_msg = NULL, .will_msg_len = 0, .keep_alive = 0, .user_name = NULL, .password = NULL, .password_len = 0 }; /* * MQTT CONNECT msg: * Same message as connect1, but change the keep alive parameter * * Message can be generated by the following command: * mosquitto_sub -V mqttv311 -i zephyr -k 365 -t sensors */ static u8_t connect2[] = {0x10, 0x12, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54, 0x04, 0x02, 0x01, 0x6d, 0x00, 0x06, 0x7a, 0x65, 0x70, 0x68, 0x79, 0x72}; static struct mqtt_connect_msg msg_connect2 = { .clean_session = 1, .client_id = CLIENTID, .client_id_len = CLIENTID_LEN, .will_flag = 0, .will_qos = 0, .will_retain = 0, .will_topic = NULL, .will_msg = NULL, .will_msg_len = 0, .keep_alive = 365, .user_name = NULL, .password = NULL, .password_len = 0 }; /* * MQTT CONNECT msg: * Clean session: 1 Client id: [6] 'zephyr' Will flag: 1 * Will QoS: 0 Will retain: 0 Will topic: [8] quitting * Will msg: [3] bye Keep alive: 0 * * Message can be generated by the following command: * mosquitto_sub -V mqttv311 -i zephyr -k 0 -t sensors --will-topic quitting \ * --will-qos 0 --will-payload bye */ static u8_t connect3[] = {0x10, 0x21, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54, 0x04, 0x06, 0x00, 0x00, 0x00, 0x06, 0x7a, 0x65, 0x70, 0x68, 0x79, 0x72, 0x00, 0x08, 0x71, 0x75, 0x69, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x00, 0x03, 0x62, 0x79, 0x65}; static struct mqtt_connect_msg msg_connect3 = { .clean_session = 1, .client_id = CLIENTID, .client_id_len = CLIENTID_LEN, .will_flag = 1, .will_qos = 0, .will_retain = 0, .will_topic = WILL_TOPIC, .will_topic_len = WILL_TOPIC_LEN, .will_msg = (u8_t *)"bye", .will_msg_len = 3, .keep_alive = 0, .user_name = NULL, .password = NULL, .password_len = 0 }; /* * MQTT CONNECT msg: * Same message as connect3, but set Will retain: 1 * * Message can be generated by the following command: * mosquitto_sub -V mqttv311 -i zephyr -k 0 -t sensors --will-topic quitting \ * --will-qos 0 --will-payload bye --will-retain */ static u8_t connect4[] = {0x10, 0x21, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54, 0x04, 0x26, 0x00, 0x00, 0x00, 0x06, 0x7a, 0x65, 0x70, 0x68, 0x79, 0x72, 0x00, 0x08, 0x71, 0x75, 0x69, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x00, 0x03, 0x62, 0x79, 0x65}; static struct mqtt_connect_msg msg_connect4 = { .clean_session = 1, .client_id = CLIENTID, .client_id_len = CLIENTID_LEN, .will_flag = 1, .will_qos = 0, .will_retain = 1, .will_topic = WILL_TOPIC, .will_topic_len = WILL_TOPIC_LEN, .will_msg = (u8_t *)"bye", .will_msg_len = 3, .keep_alive = 0, .user_name = NULL, .password = NULL, .password_len = 0 }; /* * MQTT CONNECT msg: * Same message as connect3, but set Will QoS: 1 * * Message can be generated by the following command: * mosquitto_sub -V mqttv311 -i zephyr -k 0 -t sensors --will-topic quitting \ * --will-qos 1 --will-payload bye */ static u8_t connect5[] = {0x10, 0x21, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54, 0x04, 0x0e, 0x00, 0x00, 0x00, 0x06, 0x7a, 0x65, 0x70, 0x68, 0x79, 0x72, 0x00, 0x08, 0x71, 0x75, 0x69, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x00, 0x03, 0x62, 0x79, 0x65}; static struct mqtt_connect_msg msg_connect5 = { .clean_session = 1, .client_id = CLIENTID, .client_id_len = CLIENTID_LEN, .will_flag = 1, .will_qos = 1, .will_retain = 0, .will_topic = WILL_TOPIC, .will_topic_len = WILL_TOPIC_LEN, .will_msg = (u8_t *)"bye", .will_msg_len = 3, .keep_alive = 0, .user_name = NULL, .password = NULL, .password_len = 0 }; /* * MQTT CONNECT msg: * Same message as connect5, but set Will retain: 1 * * Message can be generated by the following command: * mosquitto_sub -V mqttv311 -i zephyr -k 0 -t sensors --will-topic quitting \ * --will-qos 1 --will-payload bye --will-retain */ static u8_t connect6[] = {0x10, 0x21, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54, 0x04, 0x2e, 0x00, 0x00, 0x00, 0x06, 0x7a, 0x65, 0x70, 0x68, 0x79, 0x72, 0x00, 0x08, 0x71, 0x75, 0x69, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x00, 0x03, 0x62, 0x79, 0x65}; static struct mqtt_connect_msg msg_connect6 = { .clean_session = 1, .client_id = CLIENTID, .client_id_len = CLIENTID_LEN, .will_flag = 1, .will_qos = 1, .will_retain = 1, .will_topic = WILL_TOPIC, .will_topic_len = WILL_TOPIC_LEN, .will_msg = (u8_t *)"bye", .will_msg_len = 3, .keep_alive = 0, .user_name = NULL, .password = NULL, .password_len = 0 }; /* * MQTT CONNECT msg: * Same message as connect6, but set username: zephyr1 and password: password * * Message can be generated by the following command: * mosquitto_sub -V mqttv311 -i zephyr -k 0 -t sensors --will-topic quitting \ * --will-qos 1 --will-payload bye --will-retain -u zephyr1 -P password */ static u8_t connect7[] = {0x10, 0x34, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54, 0x04, 0xee, 0x00, 0x00, 0x00, 0x06, 0x7a, 0x65, 0x70, 0x68, 0x79, 0x72, 0x00, 0x08, 0x71, 0x75, 0x69, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x00, 0x03, 0x62, 0x79, 0x65, 0x00, 0x07, 0x7a, 0x65, 0x70, 0x68, 0x79, 0x72, 0x31, 0x00, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64}; static struct mqtt_connect_msg msg_connect7 = { .clean_session = 1, .client_id = CLIENTID, .client_id_len = CLIENTID_LEN, .will_flag = 1, .will_qos = 1, .will_retain = 1, .will_topic = WILL_TOPIC, .will_topic_len = WILL_TOPIC_LEN, .will_msg = (u8_t *)"bye", .will_msg_len = 3, .keep_alive = 0, .user_name = USERNAME, .user_name_len = USERNAME_LEN, .password = (u8_t *)"password", .password_len = 8 }; static u8_t disconnect1[] = {0xe0, 0x00}; /* * MQTT PUBLISH msg: * DUP: 0, QoS: 0, Retain: 0, topic: sensors, message: OK * * Message can be generated by the following command: * mosquitto_pub -V mqttv311 -i zephyr -t sensors -q 0 -m "OK" */ static u8_t publish1[] = {0x30, 0x0b, 0x00, 0x07, 0x73, 0x65, 0x6e, 0x73, 0x6f, 0x72, 0x73, 0x4f, 0x4b}; static struct mqtt_publish_msg msg_publish1 = { .dup = 0, .qos = 0, .retain = 0, .topic = TOPIC, .topic_len = TOPIC_LEN, .pkt_id = 0, .msg = (u8_t *)"OK", .msg_len = 2, }; /* * MQTT PUBLISH msg: * DUP: 0, QoS: 0, Retain: 1, topic: sensors, message: OK * * Message can be generated by the following command: * mosquitto_pub -V mqttv311 -i zephyr -t sensors -q 0 -m "OK" -r */ static u8_t publish2[] = {0x31, 0x0b, 0x00, 0x07, 0x73, 0x65, 0x6e, 0x73, 0x6f, 0x72, 0x73, 0x4f, 0x4b}; static struct mqtt_publish_msg msg_publish2 = { .dup = 0, .qos = 0, .retain = 1, .topic = TOPIC, .topic_len = TOPIC_LEN, .pkt_id = 0, .msg = (u8_t *)"OK", .msg_len = 2 }; /* * MQTT PUBLISH msg: * DUP: 0, QoS: 1, Retain: 1, topic: sensors, message: OK, pkt_id: 1 * * Message can be generated by the following command: * mosquitto_pub -V mqttv311 -i zephyr -t sensors -q 1 -m "OK" -r */ static u8_t publish3[] = {0x33, 0x0d, 0x00, 0x07, 0x73, 0x65, 0x6e, 0x73, 0x6f, 0x72, 0x73, 0x00, 0x01, 0x4f, 0x4b}; static struct mqtt_publish_msg msg_publish3 = { .dup = 0, .qos = 1, .retain = 1, .topic = TOPIC, .topic_len = TOPIC_LEN, .pkt_id = 1, .msg = (u8_t *)"OK", .msg_len = 2 }; /* * MQTT PUBLISH msg: * DUP: 0, QoS: 2, Retain: 0, topic: sensors, message: OK, pkt_id: 1 * * Message can be generated by the following command: * mosquitto_pub -V mqttv311 -i zephyr -t sensors -q 2 -m "OK" */ static u8_t publish4[] = {0x34, 0x0d, 0x00, 0x07, 0x73, 0x65, 0x6e, 0x73, 0x6f, 0x72, 0x73, 0x00, 0x01, 0x4f, 0x4b}; static struct mqtt_publish_msg msg_publish4 = { .dup = 0, .qos = 2, .retain = 0, .topic = TOPIC, .topic_len = TOPIC_LEN, .pkt_id = 1, .msg = (u8_t *)"OK", .msg_len = 2 }; /* * MQTT SUBSCRIBE msg: * pkt_id: 1, topic: sensors, qos: 0 * * Message can be generated by the following command: * mosquitto_sub -V mqttv311 -i zephyr -t sensors -q 0 */ static u8_t subscribe1[] = {0x82, 0x0c, 0x00, 0x01, 0x00, 0x07, 0x73, 0x65, 0x6e, 0x73, 0x6f, 0x72, 0x73, 0x00}; static struct msg_subscribe msg_subscribe1 = { .pkt_id = 1, .items = 1, .topics = {"sensors"}, .qos = {MQTT_QoS0} }; /* * MQTT SUBSCRIBE msg: * pkt_id: 1, topic: sensors, qos: 1 * * Message can be generated by the following command: * mosquitto_sub -V mqttv311 -i zephyr -t sensors -q 1 */ static u8_t subscribe2[] = {0x82, 0x0c, 0x00, 0x01, 0x00, 0x07, 0x73, 0x65, 0x6e, 0x73, 0x6f, 0x72, 0x73, 0x01}; static struct msg_subscribe msg_subscribe2 = { .pkt_id = 1, .items = 1, .topics = {"sensors"}, .qos = {MQTT_QoS1} }; /* * MQTT SUBSCRIBE msg: * pkt_id: 1, topic: sensors, qos: 2 * * Message can be generated by the following command: * mosquitto_sub -V mqttv311 -i zephyr -t sensors -q 2 */ static u8_t subscribe3[] = {0x82, 0x0c, 0x00, 0x01, 0x00, 0x07, 0x73, 0x65, 0x6e, 0x73, 0x6f, 0x72, 0x73, 0x02}; static struct msg_subscribe msg_subscribe3 = { .pkt_id = 1, .items = 1, .topics = {"sensors"}, .qos = {MQTT_QoS2} }; /* * MQTT SUBACK msg * pkt_id: 1, qos: 0 * * Message can be generated by the following command: * mosquitto_sub -V mqttv311 -i zephyr -t sensors -q 0 */ static u8_t suback1[] = {0x90, 0x03, 0x00, 0x01, 0x00}; static struct msg_suback msg_suback1 = { .pkt_id = 1, .elements = 1, .qos = {MQTT_QoS0} }; /* * MQTT SUBACK message * pkt_id: 1, qos: 1 * * Message can be generated by the following command: * mosquitto_sub -V mqttv311 -i zephyr -t sensors -q 1 */ static u8_t suback2[] = {0x90, 0x03, 0x00, 0x01, 0x01}; static struct msg_suback msg_suback2 = { .pkt_id = 1, .elements = 1, .qos = {MQTT_QoS1} }; /* * MQTT SUBACK message * pkt_id: 1, qos: 2 * * Message can be generated by the following command: * mosquitto_sub -V mqttv311 -i zephyr -t sensors -q 2 */ static u8_t suback3[] = {0x90, 0x03, 0x00, 0x01, 0x02}; static struct msg_suback msg_suback3 = { .pkt_id = 1, .elements = 1, .qos = {MQTT_QoS2} }; static u8_t pingreq1[] = {0xc0, 0x00}; static u8_t pingresp1[] = {0xd0, 0x00}; static u8_t puback1[] = {0x40, 0x02, 0x00, 0x01}; static struct msg_pkt_id msg_puback1 = {.pkt_id = 1}; static u8_t pubrec1[] = {0x50, 0x02, 0x00, 0x01}; static struct msg_pkt_id msg_pubrec1 = {.pkt_id = 1}; static u8_t pubrel1[] = {0x62, 0x02, 0x00, 0x01}; static struct msg_pkt_id msg_pubrel1 = {.pkt_id = 1}; static u8_t pubcomp1[] = {0x70, 0x02, 0x00, 0x01}; static struct msg_pkt_id msg_pubcomp1 = {.pkt_id = 1}; static u8_t unsuback1[] = {0xb0, 0x02, 0x00, 0x01}; static struct msg_pkt_id msg_unsuback1 = {.pkt_id = 1}; static struct mqtt_test mqtt_tests[] = { {.test_name = "CONNECT, new session, zeros", .msg = &msg_connect1, .eval_fcn = eval_msg_connect, .expected = connect1, .expected_len = sizeof(connect1)}, {.test_name = "CONNECT, new session, zeros and keep alive = 365", .msg = &msg_connect2, .eval_fcn = eval_msg_connect, .expected = connect2, .expected_len = sizeof(connect2)}, {.test_name = "CONNECT, new session, will", .msg = &msg_connect3, .eval_fcn = eval_msg_connect, .expected = connect3, .expected_len = sizeof(connect3)}, {.test_name = "CONNECT, new session, will retain", .msg = &msg_connect4, .eval_fcn = eval_msg_connect, .expected = connect4, .expected_len = sizeof(connect4)}, {.test_name = "CONNECT, new session, will qos = 1", .msg = &msg_connect5, .eval_fcn = eval_msg_connect, .expected = connect5, .expected_len = sizeof(connect5)}, {.test_name = "CONNECT, new session, will qos = 1, will retain", .msg = &msg_connect6, .eval_fcn = eval_msg_connect, .expected = connect6, .expected_len = sizeof(connect6)}, {.test_name = "CONNECT, new session, username and password", .msg = &msg_connect7, .eval_fcn = eval_msg_connect, .expected = connect7, .expected_len = sizeof(connect7)}, {.test_name = "DISCONNECT", .msg = NULL, .eval_fcn = eval_msg_disconnect, .expected = disconnect1, .expected_len = sizeof(disconnect1)}, {.test_name = "PUBLISH, qos = 0", .msg = &msg_publish1, .eval_fcn = eval_msg_publish, .expected = publish1, .expected_len = sizeof(publish1)}, {.test_name = "PUBLISH, retain = 1", .msg = &msg_publish2, .eval_fcn = eval_msg_publish, .expected = publish2, .expected_len = sizeof(publish2)}, {.test_name = "PUBLISH, retain = 1, qos = 1", .msg = &msg_publish3, .eval_fcn = eval_msg_publish, .expected = publish3, .expected_len = sizeof(publish3)}, {.test_name = "PUBLISH, qos = 2", .msg = &msg_publish4, .eval_fcn = eval_msg_publish, .expected = publish4, .expected_len = sizeof(publish4)}, {.test_name = "SUBSCRIBE, one topic, qos = 0", .msg = &msg_subscribe1, .eval_fcn = eval_msg_subscribe, .expected = subscribe1, .expected_len = sizeof(subscribe1)}, {.test_name = "SUBSCRIBE, one topic, qos = 1", .msg = &msg_subscribe2, .eval_fcn = eval_msg_subscribe, .expected = subscribe2, .expected_len = sizeof(subscribe2)}, {.test_name = "SUBSCRIBE, one topic, qos = 2", .msg = &msg_subscribe3, .eval_fcn = eval_msg_subscribe, .expected = subscribe3, .expected_len = sizeof(subscribe3)}, {.test_name = "SUBACK, one topic, qos = 0", .msg = &msg_suback1, .eval_fcn = eval_msg_suback, .expected = suback1, .expected_len = sizeof(suback1)}, {.test_name = "SUBACK, one topic, qos = 1", .msg = &msg_suback2, .eval_fcn = eval_msg_suback, .expected = suback2, .expected_len = sizeof(suback2)}, {.test_name = "SUBACK, one topic, qos = 2", .msg = &msg_suback3, .eval_fcn = eval_msg_suback, .expected = suback3, .expected_len = sizeof(suback3)}, {.test_name = "PINGREQ", .msg = NULL, .eval_fcn = eval_msg_pingreq, .expected = pingreq1, .expected_len = sizeof(pingreq1)}, {.test_name = "PINGRESP", .msg = NULL, .eval_fcn = eval_msg_pingresp, .expected = pingresp1, .expected_len = sizeof(pingresp1)}, {.test_name = "PUBACK", .msg = &msg_puback1, .eval_fcn = eval_msg_puback, .expected = puback1, .expected_len = sizeof(puback1)}, {.test_name = "PUBREC", .msg = &msg_pubrec1, .eval_fcn = eval_msg_pubrec, .expected = pubrec1, .expected_len = sizeof(pubrec1)}, {.test_name = "PUBREL", .msg = &msg_pubrel1, .eval_fcn = eval_msg_pubrel, .expected = pubrel1, .expected_len = sizeof(pubrel1)}, {.test_name = "PUBCOMP", .msg = &msg_pubcomp1, .eval_fcn = eval_msg_pubcomp, .expected = pubcomp1, .expected_len = sizeof(pubcomp1)}, {.test_name = "UNSUBACK", .msg = &msg_unsuback1, .eval_fcn = eval_msg_unsuback, .expected = unsuback1, .expected_len = sizeof(unsuback1)}, /* last test case, do not remove it */ {.test_name = NULL} }; static void print_array(u8_t *a, u16_t size) { u16_t i; TC_PRINT("\n"); for (i = 0; i < size; i++) { TC_PRINT("%x ", a[i]); if ((i+1) % 8 == 0) { TC_PRINT("\n"); } } TC_PRINT("\n"); } static size_t test_strlen(const char *str) { if (str == NULL) { return 0; } return strlen(str); } static int eval_buffers(u8_t *buf, u16_t buf_len, u8_t *expected, u16_t len) { if (buf_len != len) { goto exit_eval; } if (memcmp(expected, buf, buf_len) != 0) { goto exit_eval; } return TC_PASS; exit_eval: TC_PRINT("%s\n", FAIL); TC_PRINT("Computed:"); print_array(buf, buf_len); TC_PRINT("Expected:"); print_array(expected, len); return TC_FAIL; } static int eval_msg_connect(struct mqtt_test *mqtt_test) { struct mqtt_connect_msg *msg; struct mqtt_connect_msg msg2; int rc; msg = (struct mqtt_connect_msg *)mqtt_test->msg; rc = mqtt_pack_connect(buf, &buf_len, sizeof(buf), msg); /**TESTPOINTS: Check eval_msg_connect functions*/ zassert_false(rc, "mqtt_pack_connect failed"); rc = eval_buffers(buf, buf_len, mqtt_test->expected, mqtt_test->expected_len); zassert_false(rc, "eval_buffers failed"); rc = mqtt_unpack_connect(buf, buf_len, &msg2); zassert_false(rc, "mqtt_unpack_connect failed"); zassert_equal(msg->clean_session, msg2.clean_session, "clean session check failed"); zassert_equal(test_strlen(msg->client_id), msg2.client_id_len, "client ID length check failed"); zassert_false(memcmp(msg->client_id, msg2.client_id, msg2.client_id_len), "client ID check failed"); zassert_equal(msg->will_flag, msg2.will_flag, "will flag error"); zassert_equal(msg->will_qos, msg2.will_qos, "will QoS error"); zassert_equal(msg->will_retain, msg2.will_retain, "will retain error"); zassert_equal(test_strlen(msg->will_topic), msg2.will_topic_len, "will topic length error"); zassert_false(memcmp(msg->will_topic, msg2.will_topic, msg2.will_topic_len), "will topic error"); zassert_equal(msg->will_msg_len, msg2.will_msg_len, "will msg len error"); zassert_false(memcmp(msg->will_msg, msg2.will_msg, msg2.will_msg_len), "will msg error"); zassert_equal(msg->keep_alive, msg2.keep_alive, "keep alive error"); zassert_equal(test_strlen(msg->user_name), msg2.user_name_len, "username length error"); zassert_false(memcmp(msg->user_name, msg2.user_name, msg2.user_name_len), "username error"); zassert_equal(msg->password_len, msg2.password_len, "password length error"); zassert_false(memcmp(msg->password, msg2.password, msg2.password_len), "password error"); return TC_PASS; } static int eval_msg_disconnect(struct mqtt_test *mqtt_test) { int rc; rc = mqtt_pack_disconnect(buf, &buf_len, sizeof(buf)); /**TESTPOINTS: Check eval_msg_disconnect functions*/ zassert_false(rc, "mqtt_pack_disconnect failed"); rc = eval_buffers(buf, buf_len, mqtt_test->expected, mqtt_test->expected_len); zassert_false(rc, "eval_buffers failed"); rc = mqtt_unpack_disconnect(buf, buf_len); zassert_false(rc, "mqtt_unpack_disconnect failed"); return TC_PASS; } static int eval_msg_publish(struct mqtt_test *mqtt_test) { struct mqtt_publish_msg *msg; int rc; msg = (struct mqtt_publish_msg *)mqtt_test->msg; rc = mqtt_pack_publish(buf, &buf_len, sizeof(buf), msg); /**TESTPOINT: Check eval_msg_publish function*/ zassert_false(rc, "mqtt_pack_publish failed"); return eval_buffers(buf, buf_len, mqtt_test->expected, mqtt_test->expected_len); } static int eval_msg_subscribe(struct mqtt_test *mqtt_test) { struct msg_subscribe *msg = (struct msg_subscribe *)mqtt_test->msg; int rc; rc = mqtt_pack_subscribe(buf, &buf_len, sizeof(buf), msg->pkt_id, msg->items, msg->topics, msg->qos); /**TESTPOINT: Check eval_msg_subscribe function*/ zassert_false(rc, "mqtt_pack_subscribe failed"); return eval_buffers(buf, buf_len, mqtt_test->expected, mqtt_test->expected_len); } static int eval_msg_suback(struct mqtt_test *mqtt_test) { struct msg_suback *msg = (struct msg_suback *)mqtt_test->msg; int rc; rc = mqtt_pack_suback(buf, &buf_len, sizeof(buf), msg->pkt_id, msg->elements, msg->qos); /**TESTPOINT: Check eval_msg_suback function*/ zassert_false(rc, "mqtt_pack_suback failed"); return eval_buffers(buf, buf_len, mqtt_test->expected, mqtt_test->expected_len); } static int eval_msg_pingreq(struct mqtt_test *mqtt_test) { int rc; rc = mqtt_pack_pingreq(buf, &buf_len, sizeof(buf)); /**TESTPOINTS: Check eval_msg_pingreq functions*/ zassert_false(rc, "mqtt_pack_pingreq failed"); rc = eval_buffers(buf, buf_len, mqtt_test->expected, mqtt_test->expected_len); zassert_false(rc, "eval_buffers failed"); rc = mqtt_unpack_pingreq(buf, buf_len); zassert_false(rc, "mqtt_unpack_pingreq failed"); return TC_PASS; } static int eval_msg_pingresp(struct mqtt_test *mqtt_test) { int rc; rc = mqtt_pack_pingresp(buf, &buf_len, sizeof(buf)); /**TESTPOINTS: Check evam_msg_pingresp functions*/ zassert_false(rc, "mqtt_pack_pingresp failed"); rc = eval_buffers(buf, buf_len, mqtt_test->expected, mqtt_test->expected_len); zassert_false(rc, "eval_buffers failed"); rc = mqtt_unpack_pingresp(buf, buf_len); zassert_false(rc, "mqtt_unpack_pingresp failed"); return TC_PASS; } static int eval_msg_packet_id(struct mqtt_test *mqtt_test, enum mqtt_packet type) { int (*pack)(u8_t *, u16_t *, u16_t, u16_t) = NULL; int (*unpack)(u8_t *, u16_t, u16_t *) = NULL; struct msg_pkt_id *msg = (struct msg_pkt_id *)mqtt_test->msg; u16_t pkt_id; int rc; switch (type) { case MQTT_PUBACK: pack = mqtt_pack_puback; unpack = mqtt_unpack_puback; break; case MQTT_PUBCOMP: pack = mqtt_pack_pubcomp; unpack = mqtt_unpack_pubcomp; break; case MQTT_PUBREC: pack = mqtt_pack_pubrec; unpack = mqtt_unpack_pubrec; break; case MQTT_PUBREL: pack = mqtt_pack_pubrel; unpack = mqtt_unpack_pubrel; break; case MQTT_UNSUBACK: pack = mqtt_pack_unsuback; unpack = mqtt_unpack_unsuback; break; default: return TC_FAIL; } rc = pack(buf, &buf_len, sizeof(buf), msg->pkt_id); /**TESTPOINTS: Check eval_msg_packet_id functions*/ zassert_false(rc, "pack failed"); rc = eval_buffers(buf, buf_len, mqtt_test->expected, mqtt_test->expected_len); zassert_false(rc, "eval_buffers failed"); rc = unpack(buf, buf_len, &pkt_id); zassert_false(rc, "unpack failed"); zassert_equal(pkt_id, msg->pkt_id, "packet identifier error"); return TC_PASS; } static int eval_msg_puback(struct mqtt_test *mqtt_test) { return eval_msg_packet_id(mqtt_test, MQTT_PUBACK); } static int eval_msg_pubcomp(struct mqtt_test *mqtt_test) { return eval_msg_packet_id(mqtt_test, MQTT_PUBCOMP); } static int eval_msg_pubrec(struct mqtt_test *mqtt_test) { return eval_msg_packet_id(mqtt_test, MQTT_PUBREC); } static int eval_msg_pubrel(struct mqtt_test *mqtt_test) { return eval_msg_packet_id(mqtt_test, MQTT_PUBREL); } static int eval_msg_unsuback(struct mqtt_test *mqtt_test) { return eval_msg_packet_id(mqtt_test, MQTT_UNSUBACK); } void test_mqtt_packet(void) { TC_START("MQTT Library test"); int rc; int i; i = 0; do { struct mqtt_test *test = &mqtt_tests[i]; if (test->test_name == NULL) { break; } rc = test->eval_fcn(test); TC_PRINT("[%s] %d - %s\n", RC_STR(rc), i + 1, test->test_name); /**TESTPOINT: Check eval_fcn*/ zassert_false(rc, "mqtt_packet test error"); i++; } while (1); } void test_main(void) { ztest_test_suite(test_mqtt_packet_fn, ztest_unit_test(test_mqtt_packet)); ztest_run_test_suite(test_mqtt_packet_fn); } |