Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

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
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
/*
 * Copyright (c) 2016 Intel Corporation.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <tc_util.h>
#include <mqtt_internal.h>
#include <misc/util.h>	/* for ARRAY_SIZE */
#include <ztest.h>

#define CLIENTID	"zephyr"
#define CLIENTID_LEN	6
#define TOPIC		"sensors"
#define TOPIC_LEN	7
#define WILL_TOPIC	"quitting"
#define WILL_TOPIC_LEN	8
#define WILL_MSG	"bye"
#define WILL_MSG_LEN	3
#define USERNAME	"zephyr1"
#define USERNAME_LEN	7
#define PASSWORD	"password"
#define PASSWORD_LEN	8

#define BUFFER_SIZE 128

static ZTEST_DMEM u8_t rx_buffer[BUFFER_SIZE];
static ZTEST_DMEM u8_t tx_buffer[BUFFER_SIZE];
static ZTEST_DMEM struct mqtt_client client;

static ZTEST_DMEM struct mqtt_topic topic_qos_0 = {
	.qos = 0,
	.topic.utf8 = TOPIC,
	.topic.size = TOPIC_LEN
};
static ZTEST_DMEM struct mqtt_topic topic_qos_1 = {
	.qos = 1,
	.topic.utf8 = TOPIC,
	.topic.size = TOPIC_LEN
};
static ZTEST_DMEM struct mqtt_topic topic_qos_2 = {
	.qos = 2,
	.topic.utf8 = TOPIC,
	.topic.size = TOPIC_LEN
};
static ZTEST_DMEM struct mqtt_topic will_topic_qos_0 = {
	.qos = 0,
	.topic.utf8 = WILL_TOPIC,
	.topic.size = WILL_TOPIC_LEN
};
static ZTEST_DMEM struct mqtt_topic will_topic_qos_1 = {
	.qos = 1,
	.topic.utf8 = WILL_TOPIC,
	.topic.size = WILL_TOPIC_LEN
};
static ZTEST_DMEM struct mqtt_utf8 will_msg = {
	.utf8 = WILL_MSG,
	.size = WILL_MSG_LEN
};
static ZTEST_DMEM struct mqtt_utf8 username = {
	.utf8 = USERNAME,
	.size = USERNAME_LEN
};
static ZTEST_DMEM struct mqtt_utf8 password = {
	.utf8 = PASSWORD,
	.size = PASSWORD_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_publish_param *msg_publish =
	 *                                  (struct mqtt_publish_param *)ctx
	 */
	void *ctx;

	/* 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;
};

/**
 * @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_corrupted_publish Evaluate the given mqtt_test against the
 *				     corrupted publish message.
 * @param [in] mqtt_test	     MQTT test structure
 * @return			     TC_PASS on success
 * @return			     TC_FAIL on error
 */
static int eval_msg_corrupted_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_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_max_pkt_len	Evaluate header with maximum allowed packet
 *				length.
 * @param [in] mqtt_test	MQTT test structure
 * @return			TC_PASS on success
 * @return			TC_FAIL on error
 */
static int eval_max_pkt_len(struct mqtt_test *mqtt_test);

/**
 * @brief eval_corrupted_pkt_len Evaluate header exceeding maximum
 *				 allowed packet length.
 * @param [in] mqtt_test	 MQTT test structure
 * @return			 TC_PASS on success
 * @return			 TC_FAIL on error
 */
static int eval_corrupted_pkt_len(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] 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(const struct buf_ctx *buf,
			const 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(const u8_t *a, u16_t size);

/*
 * 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: 60		User name: [0]
 * Password: [0]
 *
 * Message can be generated by the following command:
 * mosquitto_sub -V mqttv311 -i zephyr -k 60 -t sensors
 */
static ZTEST_DMEM
u8_t connect1[] = {0x10, 0x12, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54,
		   0x04, 0x02, 0x00, 0x3c, 0x00, 0x06, 0x7a, 0x65,
		   0x70, 0x68, 0x79, 0x72};

static ZTEST_DMEM struct mqtt_client client_connect1 = {
	.clean_session = 1, .client_id.utf8 = CLIENTID,
	.client_id.size = CLIENTID_LEN,
	.will_retain = 0, .will_topic = NULL,
	.will_message = NULL, .user_name = NULL,
	.password = NULL
};

/*
 * 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 60 -t sensors --will-topic quitting \
 * --will-qos 0 --will-payload bye
 */
static ZTEST_DMEM
u8_t connect2[] = {0x10, 0x21, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54,
		   0x04, 0x06, 0x00, 0x3c, 0x00, 0x06, 0x7a, 0x65,
		   0x70, 0x68, 0x79, 0x72, 0x00, 0x08, 0x71, 0x75,
		   0x69, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x00, 0x03,
		   0x62, 0x79, 0x65};

static ZTEST_DMEM struct mqtt_client client_connect2 = {
	.clean_session = 1, .client_id.utf8 = CLIENTID,
	.client_id.size = CLIENTID_LEN,
	.will_retain = 0, .will_topic = &will_topic_qos_0,
	.will_message = &will_msg, .user_name = NULL,
	.password = NULL
};

/*
 * 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 60 -t sensors --will-topic quitting \
 * --will-qos 0 --will-payload bye  --will-retain
 */
static ZTEST_DMEM
u8_t connect3[] = {0x10, 0x21, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54,
		   0x04, 0x26, 0x00, 0x3c, 0x00, 0x06, 0x7a, 0x65,
		   0x70, 0x68, 0x79, 0x72, 0x00, 0x08, 0x71, 0x75,
		   0x69, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x00, 0x03,
		   0x62, 0x79, 0x65};

static ZTEST_DMEM struct mqtt_client client_connect3 = {
	.clean_session = 1, .client_id.utf8 = CLIENTID,
	.client_id.size = CLIENTID_LEN,
	.will_retain = 1, .will_topic = &will_topic_qos_0,
	.will_message = &will_msg, .user_name = NULL,
	.password = NULL
};

/*
 * 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 60 -t sensors --will-topic quitting \
 * --will-qos 1 --will-payload bye
 */
static ZTEST_DMEM
u8_t connect4[] = {0x10, 0x21, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54,
		   0x04, 0x0e, 0x00, 0x3c, 0x00, 0x06, 0x7a, 0x65,
		   0x70, 0x68, 0x79, 0x72, 0x00, 0x08, 0x71, 0x75,
		   0x69, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x00, 0x03,
		   0x62, 0x79, 0x65};

static ZTEST_DMEM struct mqtt_client client_connect4 = {
	.clean_session = 1, .client_id.utf8 = CLIENTID,
	.client_id.size = CLIENTID_LEN,
	.will_retain = 0, .will_topic = &will_topic_qos_1,
	.will_message = &will_msg, .user_name = NULL,
	.password = NULL
};

/*
 * 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 60 -t sensors --will-topic quitting \
 * --will-qos 1 --will-payload bye --will-retain
 */
static ZTEST_DMEM
u8_t connect5[] = {0x10, 0x21, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54,
		   0x04, 0x2e, 0x00, 0x3c, 0x00, 0x06, 0x7a, 0x65,
		   0x70, 0x68, 0x79, 0x72, 0x00, 0x08, 0x71, 0x75,
		   0x69, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x00, 0x03,
		   0x62, 0x79, 0x65};

static ZTEST_DMEM struct mqtt_client client_connect5 = {
	.clean_session = 1, .client_id.utf8 = CLIENTID,
	.client_id.size = CLIENTID_LEN,
	.will_retain = 1, .will_topic = &will_topic_qos_1,
	.will_message = &will_msg, .user_name = NULL,
	.password = NULL
};

/*
 * 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 60 -t sensors --will-topic quitting \
 * --will-qos 1 --will-payload bye --will-retain -u zephyr1 -P password
 */
static ZTEST_DMEM
u8_t connect6[] = {0x10, 0x34, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54,
		  0x04, 0xee, 0x00, 0x3c, 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 ZTEST_DMEM struct mqtt_client client_connect6 = {
	.clean_session = 1, .client_id.utf8 = CLIENTID,
	.client_id.size = CLIENTID_LEN,
	.will_retain = 1, .will_topic = &will_topic_qos_1,
	.will_message = &will_msg, .user_name = &username,
	.password = &password
};

static ZTEST_DMEM
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 ZTEST_DMEM
u8_t publish1[] = {0x30, 0x0b, 0x00, 0x07, 0x73, 0x65, 0x6e, 0x73,
		   0x6f, 0x72, 0x73, 0x4f, 0x4b};

static ZTEST_DMEM struct mqtt_publish_param msg_publish1 = {
	.dup_flag = 0, .retain_flag = 0, .message_id = 0,
	.message.topic.qos = 0,
	.message.topic.topic.utf8 = TOPIC,
	.message.topic.topic.size = TOPIC_LEN,
	.message.payload.data = (u8_t *)"OK",
	.message.payload.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 ZTEST_DMEM
u8_t publish2[] = {0x31, 0x0b, 0x00, 0x07, 0x73, 0x65, 0x6e, 0x73,
		   0x6f, 0x72, 0x73, 0x4f, 0x4b};

static ZTEST_DMEM struct mqtt_publish_param msg_publish2 = {
	.dup_flag = 0, .retain_flag = 1, .message_id = 0,
	.message.topic.qos = 0,
	.message.topic.topic.utf8 = TOPIC,
	.message.topic.topic.size = TOPIC_LEN,
	.message.payload.data = (u8_t *)"OK",
	.message.payload.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 ZTEST_DMEM
u8_t publish3[] = {0x33, 0x0d, 0x00, 0x07, 0x73, 0x65, 0x6e, 0x73,
		   0x6f, 0x72, 0x73, 0x00, 0x01, 0x4f, 0x4b};

static ZTEST_DMEM struct mqtt_publish_param msg_publish3 = {
	.dup_flag = 0, .retain_flag = 1, .message_id = 1,
	.message.topic.qos = 1,
	.message.topic.topic.utf8 = TOPIC,
	.message.topic.topic.size = TOPIC_LEN,
	.message.payload.data = (u8_t *)"OK",
	.message.payload.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 ZTEST_DMEM
u8_t publish4[] = {0x34, 0x0d, 0x00, 0x07, 0x73, 0x65, 0x6e, 0x73,
		   0x6f, 0x72, 0x73, 0x00, 0x01, 0x4f, 0x4b};
static ZTEST_DMEM struct mqtt_publish_param msg_publish4 = {
	.dup_flag = 0, .retain_flag = 0, .message_id = 1,
	.message.topic.qos = 2,
	.message.topic.topic.utf8 = TOPIC,
	.message.topic.topic.size = TOPIC_LEN,
	.message.payload.data = (u8_t *)"OK",
	.message.payload.len = 2,
};

static ZTEST_DMEM
u8_t publish_corrupted[] = {0x30, 0x07, 0x00, 0x07, 0x73, 0x65, 0x6e, 0x73,
			    0x6f, 0x72, 0x73, 0x00, 0x01, 0x4f, 0x4b};
static ZTEST_DMEM struct buf_ctx publish_corrupted_buf = {
	.cur = publish_corrupted,
	.end = publish_corrupted + sizeof(publish_corrupted)
};

/*
 * 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 ZTEST_DMEM
u8_t subscribe1[] = {0x82, 0x0c, 0x00, 0x01, 0x00, 0x07, 0x73, 0x65,
		     0x6e, 0x73, 0x6f, 0x72, 0x73, 0x00};
static ZTEST_DMEM struct mqtt_subscription_list msg_subscribe1 = {
	.message_id = 1, .list_count = 1, .list = &topic_qos_0
};

/*
 * 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 ZTEST_DMEM
u8_t subscribe2[] = {0x82, 0x0c, 0x00, 0x01, 0x00, 0x07, 0x73, 0x65,
		     0x6e, 0x73, 0x6f, 0x72, 0x73, 0x01};
static ZTEST_DMEM struct mqtt_subscription_list msg_subscribe2 = {
	.message_id = 1, .list_count = 1, .list = &topic_qos_1
};

/*
 * 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 ZTEST_DMEM
u8_t subscribe3[] = {0x82, 0x0c, 0x00, 0x01, 0x00, 0x07, 0x73, 0x65,
		     0x6e, 0x73, 0x6f, 0x72, 0x73, 0x02};
static ZTEST_DMEM struct mqtt_subscription_list msg_subscribe3 = {
	.message_id = 1, .list_count = 1, .list = &topic_qos_2
};

/*
 * 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 ZTEST_DMEM
u8_t suback1[] = {0x90, 0x03, 0x00, 0x01, 0x00};
static ZTEST_DMEM u8_t data_suback1[] = { MQTT_SUBACK_SUCCESS_QoS_0 };
static ZTEST_DMEM struct mqtt_suback_param msg_suback1 = {
	.message_id = 1, .return_codes.len = 1,
	.return_codes.data = data_suback1
};

/*
 * 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 ZTEST_DMEM
u8_t suback2[] = {0x90, 0x03, 0x00, 0x01, 0x01};
static ZTEST_DMEM u8_t data_suback2[] = { MQTT_SUBACK_SUCCESS_QoS_1 };
static ZTEST_DMEM struct mqtt_suback_param msg_suback2 = {
	.message_id = 1, .return_codes.len = 1,
	.return_codes.data = data_suback2
};

/*
 * 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 ZTEST_DMEM
u8_t suback3[] = {0x90, 0x03, 0x00, 0x01, 0x02};
static ZTEST_DMEM u8_t data_suback3[] = { MQTT_SUBACK_SUCCESS_QoS_2 };
static ZTEST_DMEM struct mqtt_suback_param msg_suback3 = {
	.message_id = 1, .return_codes.len = 1,
	.return_codes.data = data_suback3
};

static ZTEST_DMEM
u8_t pingreq1[] = {0xc0, 0x00};

static ZTEST_DMEM
u8_t puback1[] = {0x40, 0x02, 0x00, 0x01};
static ZTEST_DMEM struct mqtt_puback_param msg_puback1 = {.message_id = 1};

static ZTEST_DMEM
u8_t pubrec1[] = {0x50, 0x02, 0x00, 0x01};
static ZTEST_DMEM struct mqtt_pubrec_param msg_pubrec1 = {.message_id = 1};

static ZTEST_DMEM
u8_t pubrel1[] = {0x62, 0x02, 0x00, 0x01};
static ZTEST_DMEM struct mqtt_pubrel_param msg_pubrel1 = {.message_id = 1};

static ZTEST_DMEM
u8_t pubcomp1[] = {0x70, 0x02, 0x00, 0x01};
static ZTEST_DMEM struct mqtt_pubcomp_param msg_pubcomp1 = {.message_id = 1};

static ZTEST_DMEM
u8_t unsuback1[] = {0xb0, 0x02, 0x00, 0x01};
static ZTEST_DMEM struct mqtt_unsuback_param msg_unsuback1 = {.message_id = 1};

static ZTEST_DMEM
u8_t max_pkt_len[] = {0x30, 0xff, 0xff, 0xff, 0x7f};
static ZTEST_DMEM struct buf_ctx max_pkt_len_buf = {
	.cur = max_pkt_len, .end = max_pkt_len + sizeof(max_pkt_len)
};

static ZTEST_DMEM
u8_t corrupted_pkt_len[] = {0x30, 0xff, 0xff, 0xff, 0xff, 0x01};
static ZTEST_DMEM struct buf_ctx corrupted_pkt_len_buf = {
	.cur = corrupted_pkt_len,
	.end = corrupted_pkt_len + sizeof(corrupted_pkt_len)
};

static ZTEST_DMEM
struct mqtt_test mqtt_tests[] = {

	{.test_name = "CONNECT, new session, zeros",
	 .ctx = &client_connect1, .eval_fcn = eval_msg_connect,
	 .expected = connect1, .expected_len = sizeof(connect1)},

	{.test_name = "CONNECT, new session, will",
	 .ctx = &client_connect2, .eval_fcn = eval_msg_connect,
	 .expected = connect2, .expected_len = sizeof(connect2)},

	{.test_name = "CONNECT, new session, will retain",
	 .ctx = &client_connect3, .eval_fcn = eval_msg_connect,
	 .expected = connect3, .expected_len = sizeof(connect3)},

	{.test_name = "CONNECT, new session, will qos = 1",
	 .ctx = &client_connect4, .eval_fcn = eval_msg_connect,
	 .expected = connect4, .expected_len = sizeof(connect4)},

	{.test_name = "CONNECT, new session, will qos = 1, will retain",
	 .ctx = &client_connect5, .eval_fcn = eval_msg_connect,
	 .expected = connect5, .expected_len = sizeof(connect5)},

	{.test_name = "CONNECT, new session, username and password",
	 .ctx = &client_connect6, .eval_fcn = eval_msg_connect,
	 .expected = connect6, .expected_len = sizeof(connect6)},

	{.test_name = "DISCONNECT",
	 .ctx = NULL, .eval_fcn = eval_msg_disconnect,
	 .expected = disconnect1, .expected_len = sizeof(disconnect1)},

	{.test_name = "PUBLISH, qos = 0",
	 .ctx = &msg_publish1, .eval_fcn = eval_msg_publish,
	 .expected = publish1, .expected_len = sizeof(publish1)},

	{.test_name = "PUBLISH, retain = 1",
	 .ctx = &msg_publish2, .eval_fcn = eval_msg_publish,
	 .expected = publish2, .expected_len = sizeof(publish2)},

	{.test_name = "PUBLISH, retain = 1, qos = 1",
	 .ctx = &msg_publish3, .eval_fcn = eval_msg_publish,
	 .expected = publish3, .expected_len = sizeof(publish3)},

	{.test_name = "PUBLISH, qos = 2",
	 .ctx = &msg_publish4, .eval_fcn = eval_msg_publish,
	 .expected = publish4, .expected_len = sizeof(publish4)},

	{.test_name = "PUBLISH, corrupted message length (smaller than topic)",
	 .ctx = &publish_corrupted_buf, .eval_fcn = eval_msg_corrupted_publish},

	{.test_name = "SUBSCRIBE, one topic, qos = 0",
	 .ctx = &msg_subscribe1, .eval_fcn = eval_msg_subscribe,
	 .expected = subscribe1, .expected_len = sizeof(subscribe1)},

	{.test_name = "SUBSCRIBE, one topic, qos = 1",
	 .ctx = &msg_subscribe2, .eval_fcn = eval_msg_subscribe,
	 .expected = subscribe2, .expected_len = sizeof(subscribe2)},

	{.test_name = "SUBSCRIBE, one topic, qos = 2",
	 .ctx = &msg_subscribe3, .eval_fcn = eval_msg_subscribe,
	 .expected = subscribe3, .expected_len = sizeof(subscribe3)},

	{.test_name = "SUBACK, one topic, qos = 0",
	 .ctx = &msg_suback1, .eval_fcn = eval_msg_suback,
	 .expected = suback1, .expected_len = sizeof(suback1)},

	{.test_name = "SUBACK, one topic, qos = 1",
	 .ctx = &msg_suback2, .eval_fcn = eval_msg_suback,
	 .expected = suback2, .expected_len = sizeof(suback2)},

	{.test_name = "SUBACK, one topic, qos = 2",
	 .ctx = &msg_suback3, .eval_fcn = eval_msg_suback,
	 .expected = suback3, .expected_len = sizeof(suback3)},

	{.test_name = "PINGREQ",
	 .ctx = NULL, .eval_fcn = eval_msg_pingreq,
	 .expected = pingreq1, .expected_len = sizeof(pingreq1)},

	{.test_name = "PUBACK",
	 .ctx = &msg_puback1, .eval_fcn = eval_msg_puback,
	 .expected = puback1, .expected_len = sizeof(puback1)},

	{.test_name = "PUBREC",
	 .ctx = &msg_pubrec1, .eval_fcn = eval_msg_pubrec,
	 .expected = pubrec1, .expected_len = sizeof(pubrec1)},

	{.test_name = "PUBREL",
	 .ctx = &msg_pubrel1, .eval_fcn = eval_msg_pubrel,
	 .expected = pubrel1, .expected_len = sizeof(pubrel1)},

	{.test_name = "PUBCOMP",
	 .ctx = &msg_pubcomp1, .eval_fcn = eval_msg_pubcomp,
	 .expected = pubcomp1, .expected_len = sizeof(pubcomp1)},

	{.test_name = "UNSUBACK",
	 .ctx = &msg_unsuback1, .eval_fcn = eval_msg_unsuback,
	 .expected = unsuback1, .expected_len = sizeof(unsuback1)},

	{.test_name = "Maximum packet length",
	 .ctx = &max_pkt_len_buf, .eval_fcn = eval_max_pkt_len},

	{.test_name = "Corrupted packet length",
	 .ctx = &corrupted_pkt_len_buf, .eval_fcn = eval_corrupted_pkt_len},

	/* last test case, do not remove it */
	{.test_name = NULL}
};

static void print_array(const u8_t *a, u16_t size)
{
	u16_t i;

	TC_PRINT("\n");
	for (i = 0U; i < size; i++) {
		TC_PRINT("%x ", a[i]);
		if ((i+1) % 8 == 0U) {
			TC_PRINT("\n");
		}
	}
	TC_PRINT("\n");
}

static
int eval_buffers(const struct buf_ctx *buf, const u8_t *expected, u16_t len)
{
	if (buf->end - buf->cur != len) {
		goto exit_eval;
	}

	if (memcmp(expected, buf->cur, buf->end - buf->cur) != 0) {
		goto exit_eval;
	}

	return TC_PASS;

exit_eval:
	TC_PRINT("FAIL\n");
	TC_PRINT("Computed:");
	print_array(buf->cur, buf->end - buf->cur);
	TC_PRINT("Expected:");
	print_array(expected, len);

	return TC_FAIL;
}

static int eval_msg_connect(struct mqtt_test *mqtt_test)
{
	struct mqtt_client *test_client;
	int rc;
	struct buf_ctx buf;

	test_client = (struct mqtt_client *)mqtt_test->ctx;

	client.clean_session = test_client->clean_session;
	client.client_id = test_client->client_id;
	client.will_topic = test_client->will_topic;
	client.will_retain = test_client->will_retain;
	client.will_message = test_client->will_message;
	client.user_name = test_client->user_name;
	client.password = test_client->password;

	buf.cur = client.tx_buf;
	buf.end = client.tx_buf + client.tx_buf_size;

	rc = connect_request_encode(&client, &buf);

	/**TESTPOINTS: Check connect_request_encode functions*/
	zassert_false(rc, "connect_request_encode failed");

	rc = eval_buffers(&buf, mqtt_test->expected, mqtt_test->expected_len);

	zassert_false(rc, "eval_buffers failed");

	return TC_PASS;
}

static int eval_msg_disconnect(struct mqtt_test *mqtt_test)
{
	int rc;
	struct buf_ctx buf;

	buf.cur = client.tx_buf;
	buf.end = client.tx_buf + client.tx_buf_size;

	rc = disconnect_encode(&buf);

	/**TESTPOINTS: Check disconnect_encode functions*/
	zassert_false(rc, "disconnect_encode failed");

	rc = eval_buffers(&buf, mqtt_test->expected, mqtt_test->expected_len);

	zassert_false(rc, "eval_buffers failed");

	return TC_PASS;
}

static int eval_msg_publish(struct mqtt_test *mqtt_test)
{
	struct mqtt_publish_param *param =
			(struct mqtt_publish_param *)mqtt_test->ctx;
	struct mqtt_publish_param dec_param;
	int rc;
	u8_t type_and_flags;
	u32_t length;
	struct buf_ctx buf;

	memset(&dec_param, 0, sizeof(dec_param));

	buf.cur = client.tx_buf;
	buf.end = client.tx_buf + client.tx_buf_size;

	rc = publish_encode(param, &buf);

	/* Payload is not copied, copy it manually just after the header.*/
	memcpy(buf.end, param->message.payload.data,
	       param->message.payload.len);
	buf.end += param->message.payload.len;

	/**TESTPOINT: Check publish_encode function*/
	zassert_false(rc, "publish_encode failed");

	rc = eval_buffers(&buf, mqtt_test->expected, mqtt_test->expected_len);

	zassert_false(rc, "eval_buffers failed");

	rc = fixed_header_decode(&buf, &type_and_flags, &length);

	zassert_false(rc, "fixed_header_decode failed");

	rc = publish_decode(type_and_flags, length, &buf, &dec_param);

	/**TESTPOINT: Check publish_decode function*/
	zassert_false(rc, "publish_decode failed");

	zassert_equal(dec_param.message_id, param->message_id,
		      "message_id error");
	zassert_equal(dec_param.dup_flag, param->dup_flag,
		      "dup flag error");
	zassert_equal(dec_param.retain_flag, param->retain_flag,
		      "retain flag error");
	zassert_equal(dec_param.message.topic.qos, param->message.topic.qos,
		      "topic qos error");
	zassert_equal(dec_param.message.topic.topic.size,
		      param->message.topic.topic.size,
		      "topic len error");
	if (memcmp(dec_param.message.topic.topic.utf8,
		   param->message.topic.topic.utf8,
		   dec_param.message.topic.topic.size) != 0) {
		zassert_unreachable("topic content error");
	}
	zassert_equal(dec_param.message.payload.len,
		      param->message.payload.len,
		      "payload len error");

	return TC_PASS;
}

static int eval_msg_corrupted_publish(struct mqtt_test *mqtt_test)
{
	struct buf_ctx *buf = (struct buf_ctx *)mqtt_test->ctx;
	int rc;
	u8_t type_and_flags;
	u32_t length;
	struct mqtt_publish_param dec_param;

	rc = fixed_header_decode(buf, &type_and_flags, &length);
	zassert_equal(rc, 0, "fixed_header_decode failed");

	rc = publish_decode(type_and_flags, length, buf, &dec_param);
	zassert_equal(rc, -EINVAL, "publish_decode should fail");

	return TC_PASS;
}

static int eval_msg_subscribe(struct mqtt_test *mqtt_test)
{
	struct mqtt_subscription_list *param =
				(struct mqtt_subscription_list *)mqtt_test->ctx;
	int rc;
	struct buf_ctx buf;

	buf.cur = client.tx_buf;
	buf.end = client.tx_buf + client.tx_buf_size;

	rc = subscribe_encode(param, &buf);

	/**TESTPOINT: Check subscribe_encode function*/
	zassert_false(rc, "subscribe_encode failed");

	return eval_buffers(&buf, mqtt_test->expected, mqtt_test->expected_len);
}

static int eval_msg_suback(struct mqtt_test *mqtt_test)
{
	struct mqtt_suback_param *param =
			(struct mqtt_suback_param *)mqtt_test->ctx;
	struct mqtt_suback_param dec_param;

	int rc;
	u8_t type_and_flags;
	u32_t length;
	struct buf_ctx buf;

	buf.cur = mqtt_test->expected;
	buf.end = mqtt_test->expected + mqtt_test->expected_len;

	memset(&dec_param, 0, sizeof(dec_param));

	rc = fixed_header_decode(&buf, &type_and_flags, &length);

	zassert_false(rc, "fixed_header_decode failed");

	rc = subscribe_ack_decode(&buf, &dec_param);

	/**TESTPOINT: Check subscribe_ack_decode function*/
	zassert_false(rc, "subscribe_ack_decode failed");

	zassert_equal(dec_param.message_id, param->message_id,
		      "packet identifier error");
	zassert_equal(dec_param.return_codes.len,
		      param->return_codes.len,
		      "topic count error");
	if (memcmp(dec_param.return_codes.data, param->return_codes.data,
		   dec_param.return_codes.len) != 0) {
		zassert_unreachable("subscribe result error");
	}

	return TC_PASS;
}

static int eval_msg_pingreq(struct mqtt_test *mqtt_test)
{
	int rc;
	struct buf_ctx buf;

	buf.cur = client.tx_buf;
	buf.end = client.tx_buf + client.tx_buf_size;

	rc = ping_request_encode(&buf);

	/**TESTPOINTS: Check eval_msg_pingreq functions*/
	zassert_false(rc, "ping_request_encode failed");

	rc = eval_buffers(&buf, mqtt_test->expected, mqtt_test->expected_len);

	zassert_false(rc, "eval_buffers failed");

	return TC_PASS;
}

static int eval_msg_puback(struct mqtt_test *mqtt_test)
{
	struct mqtt_puback_param *param =
			(struct mqtt_puback_param *)mqtt_test->ctx;
	struct mqtt_puback_param dec_param;
	int rc;
	u8_t type_and_flags;
	u32_t length;
	struct buf_ctx buf;

	memset(&dec_param, 0, sizeof(dec_param));

	buf.cur = client.tx_buf;
	buf.end = client.tx_buf + client.tx_buf_size;

	rc = publish_ack_encode(param, &buf);

	/**TESTPOINTS: Check publish_ack_encode functions*/
	zassert_false(rc, "publish_ack_encode failed");

	rc = eval_buffers(&buf, mqtt_test->expected, mqtt_test->expected_len);

	zassert_false(rc, "eval_buffers failed");

	rc = fixed_header_decode(&buf, &type_and_flags, &length);

	zassert_false(rc, "fixed_header_decode failed");

	rc = publish_ack_decode(&buf, &dec_param);

	zassert_false(rc, "publish_ack_decode failed");

	zassert_equal(dec_param.message_id, param->message_id,
		      "packet identifier error");

	return TC_PASS;
}

static int eval_msg_pubcomp(struct mqtt_test *mqtt_test)
{
	struct mqtt_pubcomp_param *param =
			(struct mqtt_pubcomp_param *)mqtt_test->ctx;
	struct mqtt_pubcomp_param dec_param;
	int rc;
	u32_t length;
	u8_t type_and_flags;
	struct buf_ctx buf;

	memset(&dec_param, 0, sizeof(dec_param));

	buf.cur = client.tx_buf;
	buf.end = client.tx_buf + client.tx_buf_size;

	rc = publish_complete_encode(param, &buf);

	/**TESTPOINTS: Check publish_complete_encode functions*/
	zassert_false(rc, "publish_complete_encode failed");

	rc = eval_buffers(&buf, mqtt_test->expected, mqtt_test->expected_len);

	zassert_false(rc, "eval_buffers failed");

	rc = fixed_header_decode(&buf, &type_and_flags, &length);

	zassert_false(rc, "fixed_header_decode failed");

	rc = publish_complete_decode(&buf, &dec_param);

	zassert_false(rc, "publish_complete_decode failed");

	zassert_equal(dec_param.message_id, param->message_id,
		      "packet identifier error");

	return TC_PASS;
}

static int eval_msg_pubrec(struct mqtt_test *mqtt_test)
{
	struct mqtt_pubrec_param *param =
			(struct mqtt_pubrec_param *)mqtt_test->ctx;
	struct mqtt_pubrec_param dec_param;
	int rc;
	u32_t length;
	u8_t type_and_flags;
	struct buf_ctx buf;

	memset(&dec_param, 0, sizeof(dec_param));

	buf.cur = client.tx_buf;
	buf.end = client.tx_buf + client.tx_buf_size;

	rc = publish_receive_encode(param, &buf);

	/**TESTPOINTS: Check publish_receive_encode functions*/
	zassert_false(rc, "publish_receive_encode failed");

	rc = eval_buffers(&buf, mqtt_test->expected, mqtt_test->expected_len);

	zassert_false(rc, "eval_buffers failed");

	rc = fixed_header_decode(&buf, &type_and_flags, &length);

	zassert_false(rc, "fixed_header_decode failed");

	rc = publish_receive_decode(&buf, &dec_param);

	zassert_false(rc, "publish_receive_decode failed");

	zassert_equal(dec_param.message_id, param->message_id,
		      "packet identifier error");

	return TC_PASS;
}

static int eval_msg_pubrel(struct mqtt_test *mqtt_test)
{
	struct mqtt_pubrel_param *param =
			(struct mqtt_pubrel_param *)mqtt_test->ctx;
	struct mqtt_pubrel_param dec_param;
	int rc;
	u32_t length;
	u8_t type_and_flags;
	struct buf_ctx buf;

	memset(&dec_param, 0, sizeof(dec_param));

	buf.cur = client.tx_buf;
	buf.end = client.tx_buf + client.tx_buf_size;

	rc = publish_release_encode(param, &buf);

	/**TESTPOINTS: Check publish_release_encode functions*/
	zassert_false(rc, "publish_release_encode failed");

	rc = eval_buffers(&buf, mqtt_test->expected, mqtt_test->expected_len);

	zassert_false(rc, "eval_buffers failed");

	rc = fixed_header_decode(&buf, &type_and_flags, &length);

	zassert_false(rc, "fixed_header_decode failed");

	rc = publish_release_decode(&buf, &dec_param);

	zassert_false(rc, "publish_release_decode failed");

	zassert_equal(dec_param.message_id, param->message_id,
		      "packet identifier error");

	return TC_PASS;
}

static int eval_msg_unsuback(struct mqtt_test *mqtt_test)
{
	struct mqtt_unsuback_param *param =
			(struct mqtt_unsuback_param *)mqtt_test->ctx;
	struct mqtt_unsuback_param dec_param;
	int rc;
	u32_t length;
	u8_t type_and_flags;
	struct buf_ctx buf;

	memset(&dec_param, 0, sizeof(dec_param));

	buf.cur = mqtt_test->expected;
	buf.end = mqtt_test->expected + mqtt_test->expected_len;

	rc = fixed_header_decode(&buf, &type_and_flags, &length);

	zassert_false(rc, "fixed_header_decode failed");

	rc = unsubscribe_ack_decode(&buf, &dec_param);

	zassert_false(rc, "unsubscribe_ack_decode failed");

	zassert_equal(dec_param.message_id, param->message_id,
		      "packet identifier error");

	return TC_PASS;
}

static int eval_max_pkt_len(struct mqtt_test *mqtt_test)
{
	struct buf_ctx *buf = (struct buf_ctx *)mqtt_test->ctx;
	int rc;
	u8_t flags;
	u32_t length;

	rc = fixed_header_decode(buf, &flags, &length);

	zassert_equal(rc, 0, "fixed_header_decode failed");
	zassert_equal(length, MQTT_MAX_PAYLOAD_SIZE,
		      "Invalid packet length decoded");

	return TC_PASS;
}

static int eval_corrupted_pkt_len(struct mqtt_test *mqtt_test)
{
	struct buf_ctx *buf = (struct buf_ctx *)mqtt_test->ctx;
	int rc;
	u8_t flags;
	u32_t length;

	rc = fixed_header_decode(buf, &flags, &length);

	zassert_equal(rc, -EINVAL, "fixed_header_decode should fail");

	return TC_PASS;
}

void test_mqtt_packet(void)
{
	TC_START("MQTT Library test");

	int rc;
	int i;

	mqtt_client_init(&client);
	client.protocol_version = MQTT_VERSION_3_1_1;
	client.rx_buf = rx_buffer;
	client.rx_buf_size = sizeof(rx_buffer);
	client.tx_buf = tx_buffer;
	client.tx_buf_size = sizeof(tx_buffer);

	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", TC_RESULT_TO_STR(rc), i + 1,
			test->test_name);

		/**TESTPOINT: Check eval_fcn*/
		zassert_false(rc, "mqtt_packet test error");

		i++;
	} while (1);

	mqtt_abort(&client);
}

void test_main(void)
{
	ztest_test_suite(test_mqtt_packet_fn,
		ztest_user_unit_test(test_mqtt_packet));
	ztest_run_test_suite(test_mqtt_packet_fn);
}