Debian Codegraph @

crt/aws-c-mqtt/tests/v3/packet_encoding_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/testing/aws_test_harness.h>
7
8 #include <aws/mqtt/private/packets.h>
9
10 #ifdef _MSC_VER
11 # pragma warning(push)
12 # pragma warning(disable : 4232) /* function pointer to dll symbol */
13 #endif
14
15 enum { S_BUFFER_SIZE = 128 };
16
17 struct packet_test_fixture;
18
19 /* Function type used to init and cleanup a fixture */
20 typedef int(packet_init_fn)(struct packet_test_fixture *);
21 /* Function used to encode a packet (this should be set to a function from packets.h) */
22 typedef int(packet_encode_fn)(struct aws_byte_buf *, void *);
23 /* Function used to decode a packet (this should be set to a function from packets.h) */
24 typedef int(packet_decode_fn)(struct aws_byte_cursor *, void *);
25 /* Function used to check if two packets are equal */
26 typedef bool(packet_eq_fn)(void *, void *, size_t);
27
28 /* Helper for comparing the fixed headers of packets */
29 static bool s_fixed_header_eq(struct aws_mqtt_fixed_header *l, struct aws_mqtt_fixed_header *r) {
30
31 return l->packet_type == r->packet_type && l->flags == r->flags && l->remaining_length == r->remaining_length;
32 }
33
34 /* Default packet compare function, checks headers then memcmps the rest */
35 static bool s_packet_eq_default(void *a, void *b, size_t size) {
36
37 static const size_t HEADER_SIZE = sizeof(struct aws_mqtt_fixed_header);
38
39 return s_fixed_header_eq(a, b) &&
40 memcmp((uint8_t *)a + HEADER_SIZE, (uint8_t *)b + HEADER_SIZE, size - HEADER_SIZE) == 0;
41 }
42
43 /* Contains all of the information required to run a packet's test case */
44 struct packet_test_fixture {
45 enum aws_mqtt_packet_type type;
46 size_t size;
47 packet_init_fn *init;
48 packet_encode_fn *encode;
49 packet_decode_fn *decode;
50 packet_init_fn *teardown;
51 packet_eq_fn *equal;
52 struct aws_allocator *allocator;
53
54 void *in_packet;
55 void *out_packet;
56 struct aws_byte_buf buffer;
57 };
58
59 static int s_packet_test_before(struct aws_allocator *allocator, void *ctx) {
60
61 struct packet_test_fixture *fixture = ctx;
62 fixture->allocator = allocator;
63
64 /* Setup the fixture */
65 fixture->in_packet = aws_mem_acquire(allocator, fixture->size);
66 ASSERT_NOT_NULL(fixture->in_packet);
67 memset(fixture->in_packet, 0, fixture->size);
68
69 fixture->out_packet = aws_mem_acquire(allocator, fixture->size);
70 ASSERT_NOT_NULL(fixture->out_packet);
71 memset(fixture->out_packet, 0, fixture->size);
72
73 return AWS_OP_SUCCESS;
74 }
75
76 static int s_packet_test_run(struct aws_allocator *allocator, void *ctx) {
77
78 struct packet_test_fixture *fixture = ctx;
79
80 aws_byte_buf_init(&fixture->buffer, allocator, S_BUFFER_SIZE);
81
82 /* Init the in_packet & buffer */
83 ASSERT_SUCCESS(fixture->init(fixture));
84
85 /* Encode */
86
87 /* Create the output buffer */
88 struct aws_byte_buf output_buffer;
89 ASSERT_SUCCESS(aws_byte_buf_init(&output_buffer, allocator, S_BUFFER_SIZE));
90
91 /* Encode the packet */
92 ASSERT_SUCCESS(fixture->encode(&output_buffer, fixture->in_packet));
93
94 /* Compare the buffers */
95 ASSERT_BIN_ARRAYS_EQUALS(fixture->buffer.buffer, fixture->buffer.len, output_buffer.buffer, output_buffer.len);
96
97 aws_byte_buf_clean_up(&output_buffer);
98
99 /* Decode */
100
101 /* Decode the buffer */
102 struct aws_byte_cursor cursor = aws_byte_cursor_from_buf(&fixture->buffer);
103 ASSERT_SUCCESS(fixture->decode(&cursor, fixture->out_packet));
104
105 /* Compare the packets */
106 if (fixture->equal) {
107 ASSERT_TRUE(fixture->equal(fixture->out_packet, fixture->in_packet, fixture->size));
108 } else {
109 ASSERT_TRUE(s_packet_eq_default(fixture->out_packet, fixture->in_packet, fixture->size));
110 }
111
112 return AWS_OP_SUCCESS;
113 }
114
115 static int s_packet_test_after(struct aws_allocator *allocator, int setup_result, void *ctx) {
116 (void)setup_result;
117
118 struct packet_test_fixture *fixture = ctx;
119
120 /* Tear down the packet & buffer */
121 if (fixture->teardown) {
122 fixture->teardown(fixture);
123 }
124
125 /* Tear down the fixture */
126 aws_mem_release(allocator, fixture->in_packet);
127 aws_mem_release(allocator, fixture->out_packet);
128 aws_byte_buf_clean_up(&fixture->buffer);
129
130 return AWS_OP_SUCCESS;
131 }
132
133 #define PACKET_TEST_NAME(e_type, t_name, s_name, i, t, e) \
134 static struct packet_test_fixture mqtt_packet_##t_name##_fixture = { \
135 .type = AWS_MQTT_PACKET_##e_type, \
136 .size = sizeof(struct aws_mqtt_packet_##s_name), \
137 .init = (i), \
138 .encode = (packet_encode_fn *)&aws_mqtt_packet_##s_name##_encode, \
139 .decode = (packet_decode_fn *)&aws_mqtt_packet_##s_name##_decode, \
140 .teardown = (t), \
141 .equal = (e), \
142 }; \
143 AWS_TEST_CASE_FIXTURE( \
144 mqtt_packet_##t_name, \
145 s_packet_test_before, \
146 s_packet_test_run, \
147 s_packet_test_after, \
148 &mqtt_packet_##t_name##_fixture)
149
150 #define PACKET_TEST(e_type, s_name, i, t, e) PACKET_TEST_NAME(e_type, s_name, s_name, i, t, e)
151
152 static uint8_t s_client_id[] = "Test Client ID";
153 enum { CLIENT_ID_LEN = sizeof(s_client_id) };
154 static uint8_t s_topic_name[] = "test/topic";
155 enum { TOPIC_NAME_LEN = sizeof(s_topic_name) };
156 static uint8_t s_payload[] = "This s_payload contains data. It is some good ol' fashioned data.";
157 enum { PAYLOAD_LEN = sizeof(s_payload) };
158 static uint8_t s_username[] = "admin";
159 enum { USERNAME_LEN = sizeof(s_username) };
160 static uint8_t s_password[] = "12345";
161 enum { PASSWORD_LEN = sizeof(s_password) };
162
163 /*****************************************************************************/
164 /* Ack */
165
166 static int s_test_ack_init(struct packet_test_fixture *fixture) {
167
168 /* Init buffer */
169 uint8_t packet_id = (uint8_t)(fixture->type + 7);
170
171 /* clang-format off */
172 uint8_t header[] = {
173 (uint8_t)(fixture->type << 4), /* Packet type */
174 2, /* Remaining length */
175 0, packet_id, /* Packet identifier */
176 };
177
178 /* Init packet */
179 switch (fixture->type) {
180 case AWS_MQTT_PACKET_PUBACK:
181 ASSERT_SUCCESS(aws_mqtt_packet_puback_init(fixture->in_packet, packet_id));
182 break;
183 case AWS_MQTT_PACKET_PUBREC:
184 ASSERT_SUCCESS(aws_mqtt_packet_pubrec_init(fixture->in_packet, packet_id));
185 break;
186 case AWS_MQTT_PACKET_PUBREL:
187 ASSERT_SUCCESS(aws_mqtt_packet_pubrel_init(fixture->in_packet, packet_id));
188 /* if pubrel, bit 1 in flags must be set */
189 header[0] |= 0x2;
190 break;
191 case AWS_MQTT_PACKET_PUBCOMP:
192 ASSERT_SUCCESS(aws_mqtt_packet_pubcomp_init(fixture->in_packet, packet_id));
193 break;
194 case AWS_MQTT_PACKET_UNSUBACK:
195 ASSERT_SUCCESS(aws_mqtt_packet_unsuback_init(fixture->in_packet, packet_id));
196 break;
197 default:
198 AWS_ASSUME(false);
199 break;
200 }
201
202 aws_byte_buf_write(&fixture->buffer, header, sizeof(header));
203
204 return AWS_OP_SUCCESS;
205 }
206 #define PACKET_TEST_ACK(e_type, name) \
207 PACKET_TEST_NAME(e_type, name, ack, &s_test_ack_init, NULL, NULL)
208 PACKET_TEST_ACK(PUBACK, puback)
209 PACKET_TEST_ACK(PUBREC, pubrec)
210 PACKET_TEST_ACK(PUBREL, pubrel)
211 PACKET_TEST_ACK(PUBCOMP, pubcomp)
212 PACKET_TEST_ACK(UNSUBACK, unsuback)
213 #undef PACKET_TEST_ACK
214
215 /*****************************************************************************/
216 /* Connect */
217
218 static int s_test_connect_init(struct packet_test_fixture *fixture) {
219
220 /* Init packet */
221 ASSERT_SUCCESS(aws_mqtt_packet_connect_init(
222 fixture->in_packet,
223 aws_byte_cursor_from_array(s_client_id, CLIENT_ID_LEN),
224 false,
225 0));
226
227 /* Init buffer */
228 /* clang-format off */
229 uint8_t header[] = {
230 AWS_MQTT_PACKET_CONNECT << 4, /* Packet type */
231 10 + 2 + CLIENT_ID_LEN, /* Remaining length */
232 0, 4, 'M', 'Q', 'T', 'T', /* Protocol name */
233 4, /* Protocol level */
234 0, /* Connect Flags */
235 0, 0, /* Keep alive */
236 };
237 /* clang-format on */
238
239 aws_byte_buf_write(&fixture->buffer, header, sizeof(header));
240 aws_byte_buf_write_u8(&fixture->buffer, 0);
241 aws_byte_buf_write_u8(&fixture->buffer, CLIENT_ID_LEN);
242 aws_byte_buf_write(&fixture->buffer, s_client_id, CLIENT_ID_LEN);
243
244 return AWS_OP_SUCCESS;
245 }
246 static bool s_test_connect_eq(void *a, void *b, size_t size) {
247
248 (void)size;
249
250 struct aws_mqtt_packet_connect *l = a;
251 struct aws_mqtt_packet_connect *r = b;
252
253 return s_fixed_header_eq(&l->fixed_header, &r->fixed_header) && l->clean_session == r->clean_session &&
254 l->has_will == r->has_will && l->will_qos == r->will_qos && l->will_retain == r->will_retain &&
255 l->has_password == r->has_password && l->has_username == r->has_username &&
256 l->keep_alive_timeout == r->keep_alive_timeout &&
257 aws_byte_cursor_eq(&l->client_identifier, &r->client_identifier) &&
258 aws_byte_cursor_eq(&l->will_topic, &r->will_topic) && aws_byte_cursor_eq(&l->username, &r->username) &&
259 aws_byte_cursor_eq(&l->password, &r->password);
260 }
261 PACKET_TEST(CONNECT, connect, &s_test_connect_init, NULL, &s_test_connect_eq)
262
263 static int s_test_connect_will_init(struct packet_test_fixture *fixture) {
264 /* Init packet */
265 ASSERT_SUCCESS(aws_mqtt_packet_connect_init(
266 fixture->in_packet, aws_byte_cursor_from_array(s_client_id, CLIENT_ID_LEN), false, 0));
267 ASSERT_SUCCESS(aws_mqtt_packet_connect_add_will(
268 fixture->in_packet,
269 aws_byte_cursor_from_array(s_topic_name, TOPIC_NAME_LEN),
270 AWS_MQTT_QOS_EXACTLY_ONCE,
271 true /*retain*/,
272 aws_byte_cursor_from_array(s_payload, PAYLOAD_LEN)));
273
274 /* Init buffer */
275 /* clang-format off */
276 uint8_t header[] = {
277 AWS_MQTT_PACKET_CONNECT << 4, /* Packet type */
278 10 + (2 + CLIENT_ID_LEN) + (2 + TOPIC_NAME_LEN) + (2 + PAYLOAD_LEN), /* Remaining length */
279 0, 4, 'M', 'Q', 'T', 'T', /* Protocol name */
280 4, /* Protocol level */
281 /* Connect Flags: */
282 (1 << 2) /* Will flag, bit 2 */
283 | (AWS_MQTT_QOS_EXACTLY_ONCE << 3)/* Will QoS, bits 4-3 */
284 | (1 << 5), /* Will Retain, bit 5 */
285
286 0, 0, /* Keep alive */
287 };
288 /* clang-format on */
289
290 aws_byte_buf_write(&fixture->buffer, header, sizeof(header));
291 /* client identifier */
292 aws_byte_buf_write_be16(&fixture->buffer, CLIENT_ID_LEN);
293 aws_byte_buf_write(&fixture->buffer, s_client_id, CLIENT_ID_LEN);
294 /* will topic */
295 aws_byte_buf_write_be16(&fixture->buffer, TOPIC_NAME_LEN);
296 aws_byte_buf_write(&fixture->buffer, s_topic_name, TOPIC_NAME_LEN);
297 /* will payload */
298 aws_byte_buf_write_be16(&fixture->buffer, PAYLOAD_LEN);
299 aws_byte_buf_write(&fixture->buffer, s_payload, PAYLOAD_LEN);
300
301 return AWS_OP_SUCCESS;
302 }
303 PACKET_TEST_NAME(CONNECT, connect_will, connect, &s_test_connect_will_init, NULL, &s_test_connect_eq)
304
305 static uint8_t s_empty_payload[] = "";
306 enum { EMPTY_PAYLOAD_LEN = 0 };
307
308 static int s_test_connect_empty_payload_will_init(struct packet_test_fixture *fixture) {
309 /* Init packet */
310 ASSERT_SUCCESS(aws_mqtt_packet_connect_init(
311 fixture->in_packet, aws_byte_cursor_from_array(s_client_id, CLIENT_ID_LEN), false, 0));
312 ASSERT_SUCCESS(aws_mqtt_packet_connect_add_will(
313 fixture->in_packet,
314 aws_byte_cursor_from_array(s_topic_name, TOPIC_NAME_LEN),
315 AWS_MQTT_QOS_EXACTLY_ONCE,
316 true /*retain*/,
317 aws_byte_cursor_from_array(s_empty_payload, EMPTY_PAYLOAD_LEN)));
318
319 /* Init buffer */
320 /* clang-format off */
321 uint8_t header[] = {
322 AWS_MQTT_PACKET_CONNECT << 4, /* Packet type */
323 10 + (2 + CLIENT_ID_LEN) + (2 + TOPIC_NAME_LEN) + (2 + EMPTY_PAYLOAD_LEN), /* Remaining length */
324 0, 4, 'M', 'Q', 'T', 'T', /* Protocol name */
325 4, /* Protocol level */
326 /* Connect Flags: */
327 (1 << 2) /* Will flag, bit 2 */
328 | (AWS_MQTT_QOS_EXACTLY_ONCE << 3)/* Will QoS, bits 4-3 */
329 | (1 << 5), /* Will Retain, bit 5 */
330
331 0, 0, /* Keep alive */
332 };
333 /* clang-format on */
334
335 aws_byte_buf_write(&fixture->buffer, header, sizeof(header));
336 /* client identifier */
337 aws_byte_buf_write_be16(&fixture->buffer, CLIENT_ID_LEN);
338 aws_byte_buf_write(&fixture->buffer, s_client_id, CLIENT_ID_LEN);
339 /* will topic */
340 aws_byte_buf_write_be16(&fixture->buffer, TOPIC_NAME_LEN);
341 aws_byte_buf_write(&fixture->buffer, s_topic_name, TOPIC_NAME_LEN);
342 /* will payload */
343 aws_byte_buf_write_be16(&fixture->buffer, EMPTY_PAYLOAD_LEN);
344 aws_byte_buf_write(&fixture->buffer, s_empty_payload, EMPTY_PAYLOAD_LEN);
345
346 return AWS_OP_SUCCESS;
347 }
348 PACKET_TEST_NAME(
349 CONNECT,
350 connect_empty_payload_will,
351 connect,
352 &s_test_connect_empty_payload_will_init,
353 NULL,
354 &s_test_connect_eq)
355
356 static int s_test_connect_password_init(struct packet_test_fixture *fixture) {
357 /* Init packet */
358 ASSERT_SUCCESS(aws_mqtt_packet_connect_init(
359 fixture->in_packet, aws_byte_cursor_from_array(s_client_id, CLIENT_ID_LEN), false, 0xBEEF));
360 ASSERT_SUCCESS(aws_mqtt_packet_connect_add_credentials(
361 fixture->in_packet,
362 aws_byte_cursor_from_array(s_username, USERNAME_LEN),
363 aws_byte_cursor_from_array(s_password, PASSWORD_LEN)));
364
365 /* Init buffer */
366 /* clang-format off */
367 uint8_t header[] = {
368 AWS_MQTT_PACKET_CONNECT << 4, /* Packet type */
369 10 + (2 + CLIENT_ID_LEN) + (2 + USERNAME_LEN) + (2 + PASSWORD_LEN), /* Remaining length */
370 0, 4, 'M', 'Q', 'T', 'T', /* Protocol name */
371 4, /* Protocol level */
372 (1 << 7) | (1 << 6), /* Connect Flags: username bit 7, password bit 6 */
373 0xBE, 0xEF, /* Keep alive */
374 };
375 /* clang-format on */
376
377 aws_byte_buf_write(&fixture->buffer, header, sizeof(header));
378
379 /* client identifier */
380 aws_byte_buf_write_be16(&fixture->buffer, CLIENT_ID_LEN);
381 aws_byte_buf_write(&fixture->buffer, s_client_id, CLIENT_ID_LEN);
382 /* username */
383 aws_byte_buf_write_be16(&fixture->buffer, USERNAME_LEN);
384 aws_byte_buf_write(&fixture->buffer, s_username, USERNAME_LEN);
385 /* password */
386 aws_byte_buf_write_be16(&fixture->buffer, PASSWORD_LEN);
387 aws_byte_buf_write(&fixture->buffer, s_password, PASSWORD_LEN);
388
389 return AWS_OP_SUCCESS;
390 }
391 PACKET_TEST_NAME(CONNECT, connect_password, connect, &s_test_connect_password_init, NULL, &s_test_connect_eq)
392
393 static int s_test_connect_all_init(struct packet_test_fixture *fixture) {
394 /* Init packet */
395 ASSERT_SUCCESS(aws_mqtt_packet_connect_init(
396 fixture->in_packet, aws_byte_cursor_from_array(s_client_id, CLIENT_ID_LEN), false, 0));
397 ASSERT_SUCCESS(aws_mqtt_packet_connect_add_will(
398 fixture->in_packet,
399 aws_byte_cursor_from_array(s_topic_name, TOPIC_NAME_LEN),
400 AWS_MQTT_QOS_EXACTLY_ONCE,
401 true /*retain*/,
402 aws_byte_cursor_from_array(s_payload, PAYLOAD_LEN)));
403 ASSERT_SUCCESS(aws_mqtt_packet_connect_add_credentials(
404 fixture->in_packet,
405 aws_byte_cursor_from_array(s_username, USERNAME_LEN),
406 aws_byte_cursor_from_array(s_password, PASSWORD_LEN)));
407
408 /* Init buffer */
409 /* clang-format off */
410 uint8_t header[] = {
411 AWS_MQTT_PACKET_CONNECT << 4, /* Packet type */
412 10 + (2 + CLIENT_ID_LEN) + (2 + TOPIC_NAME_LEN) + (2 + PAYLOAD_LEN) + (2 + USERNAME_LEN) + (2 + PASSWORD_LEN), /* Remaining length */
413 0, 4, 'M', 'Q', 'T', 'T', /* Protocol name */
414 4, /* Protocol level */
415 /* Connect Flags: */
416 (1 << 2) /* Will flag, bit 2 */
417 | (AWS_MQTT_QOS_EXACTLY_ONCE << 3)/* Will QoS, bits 4-3 */
418 | (1 << 5) /* Will Retain, bit 5 */
419 | (1 << 7) | (1 << 6), /* username bit 7, password bit 6 */
420 0, 0, /* Keep alive */
421 };
422 /* clang-format on */
423
424 aws_byte_buf_write(&fixture->buffer, header, sizeof(header));
425 /* client identifier */
426 aws_byte_buf_write_be16(&fixture->buffer, CLIENT_ID_LEN);
427 aws_byte_buf_write(&fixture->buffer, s_client_id, CLIENT_ID_LEN);
428 /* will topic */
429 aws_byte_buf_write_be16(&fixture->buffer, TOPIC_NAME_LEN);
430 aws_byte_buf_write(&fixture->buffer, s_topic_name, TOPIC_NAME_LEN);
431 /* will payload */
432 aws_byte_buf_write_be16(&fixture->buffer, PAYLOAD_LEN);
433 aws_byte_buf_write(&fixture->buffer, s_payload, PAYLOAD_LEN);
434 /* username */
435 aws_byte_buf_write_be16(&fixture->buffer, USERNAME_LEN);
436 aws_byte_buf_write(&fixture->buffer, s_username, USERNAME_LEN);
437 /* password */
438 aws_byte_buf_write_be16(&fixture->buffer, PASSWORD_LEN);
439 aws_byte_buf_write(&fixture->buffer, s_password, PASSWORD_LEN);
440
441 return AWS_OP_SUCCESS;
442 }
443 PACKET_TEST_NAME(CONNECT, connect_all, connect, &s_test_connect_all_init, NULL, &s_test_connect_eq)
444
445 /*****************************************************************************/
446 /* Connack */
447
448 static int s_test_connack_init(struct packet_test_fixture *fixture) {
449
450 /* Init packet */
451 ASSERT_SUCCESS(aws_mqtt_packet_connack_init(fixture->in_packet, true, AWS_MQTT_CONNECT_ACCEPTED));
452
453 /* Init buffer */
454 /* clang-format off */
455 uint8_t header[] = {
456 AWS_MQTT_PACKET_CONNACK << 4, /* Packet type */
457 2, /* Remaining length */
458 1, /* Acknowledge flags */
459 AWS_MQTT_CONNECT_ACCEPTED, /* Return code */
460 };
461 /* clang-format on */
462
463 aws_byte_buf_write(&fixture->buffer, header, sizeof(header));
464
465 return AWS_OP_SUCCESS;
466 }
467 PACKET_TEST(CONNACK, connack, &s_test_connack_init, NULL, NULL)
468
469 /*****************************************************************************/
470 /* Publish */
471
472 static int s_test_publish_qos0_dup_init(struct packet_test_fixture *fixture) {
473
474 /* Init packet */
475 ASSERT_SUCCESS(aws_mqtt_packet_publish_init(
476 fixture->in_packet,
477 false /* retain */,
478 AWS_MQTT_QOS_AT_MOST_ONCE,
479 true /* dup */,
480 aws_byte_cursor_from_array(s_topic_name, TOPIC_NAME_LEN),
481 0,
482 aws_byte_cursor_from_array(s_payload, PAYLOAD_LEN)));
483
484 /* Init buffer */
485 /* clang-format off */
486 aws_byte_buf_write_u8(
487 &fixture->buffer,
488 (AWS_MQTT_PACKET_PUBLISH << 4) /* Packet type bits 7-4 */
489 | (1 << 3) /* DUP bit 3 */
490 | (AWS_MQTT_QOS_AT_MOST_ONCE << 1) /* QoS bits 2-1 */
491 | 0 /* RETAIN bit 0 */);
492 aws_byte_buf_write_u8(
493 &fixture->buffer, 2 + TOPIC_NAME_LEN + PAYLOAD_LEN); /* Remaining length */
494 aws_byte_buf_write_u8(
495 &fixture->buffer, 0); /* Topic name len byte 1 */
496 aws_byte_buf_write_u8(
497 &fixture->buffer, TOPIC_NAME_LEN); /* Topic name len byte 2 */
498 aws_byte_buf_write(
499 &fixture->buffer, s_topic_name, TOPIC_NAME_LEN); /* Topic name */
500 aws_byte_buf_write(
501 &fixture->buffer, s_payload, PAYLOAD_LEN); /* payload */
502 /* clang-format on */
503
504 return AWS_OP_SUCCESS;
505 }
506
507 static int s_test_publish_qos2_retain_init(struct packet_test_fixture *fixture) {
508
509 /* Init packet */
510 ASSERT_SUCCESS(aws_mqtt_packet_publish_init(
511 fixture->in_packet,
512 true /* retain */,
513 AWS_MQTT_QOS_EXACTLY_ONCE,
514 false /* dup */,
515 aws_byte_cursor_from_array(s_topic_name, TOPIC_NAME_LEN),
516 7,
517 aws_byte_cursor_from_array(s_payload, PAYLOAD_LEN)));
518
519 /* Init buffer */
520 /* clang-format off */
521 aws_byte_buf_write_u8(
522 &fixture->buffer,
523 (AWS_MQTT_PACKET_PUBLISH << 4) /* Packet type bits 7-4 */
524 | (0 << 3) /* DUP bit 3 */
525 | (AWS_MQTT_QOS_EXACTLY_ONCE << 1) /* QoS bits 2-1 */
526 | 1 /* RETAIN bit 0 */);
527 aws_byte_buf_write_u8(
528 &fixture->buffer, 4 + TOPIC_NAME_LEN + PAYLOAD_LEN); /* Remaining length */
529 aws_byte_buf_write_u8(
530 &fixture->buffer, 0); /* Topic name len byte 1 */
531 aws_byte_buf_write_u8(
532 &fixture->buffer, TOPIC_NAME_LEN); /* Topic name len byte 2 */
533 aws_byte_buf_write(
534 &fixture->buffer, s_topic_name, TOPIC_NAME_LEN); /* Topic name */
535 aws_byte_buf_write_u8(
536 &fixture->buffer, 0); /* Packet id byte 1 */
537 aws_byte_buf_write_u8(
538 &fixture->buffer, 7); /* Packet id byte 2 */
539 aws_byte_buf_write(
540 &fixture->buffer, s_payload, PAYLOAD_LEN); /* payload */
541 /* clang-format on */
542
543 return AWS_OP_SUCCESS;
544 }
545
546 static int s_test_publish_empty_payload_init(struct packet_test_fixture *fixture) {
547
548 /* Init packet */
549 ASSERT_SUCCESS(aws_mqtt_packet_publish_init(
550 fixture->in_packet,
551 false /* retain */,
552 AWS_MQTT_QOS_AT_MOST_ONCE,
553 true /* dup */,
554 aws_byte_cursor_from_array(s_topic_name, TOPIC_NAME_LEN),
555 0,
556 aws_byte_cursor_from_array(s_empty_payload, EMPTY_PAYLOAD_LEN)));
557
558 /* Init buffer */
559 /* clang-format off */
560 aws_byte_buf_write_u8(
561 &fixture->buffer,
562 (AWS_MQTT_PACKET_PUBLISH << 4) /* Packet type bits 7-4 */
563 | (1 << 3) /* DUP bit 3 */
564 | (AWS_MQTT_QOS_AT_MOST_ONCE << 1) /* QoS bits 2-1 */
565 | 0 /* RETAIN bit 0 */);
566 aws_byte_buf_write_u8(
567 &fixture->buffer, 2 + TOPIC_NAME_LEN + EMPTY_PAYLOAD_LEN); /* Remaining length */
568 aws_byte_buf_write_u8(
569 &fixture->buffer, 0); /* Topic name len byte 1 */
570 aws_byte_buf_write_u8(
571 &fixture->buffer, TOPIC_NAME_LEN); /* Topic name len byte 2 */
572 aws_byte_buf_write(
573 &fixture->buffer, s_topic_name, TOPIC_NAME_LEN); /* Topic name */
574 aws_byte_buf_write(
575 &fixture->buffer, s_empty_payload, EMPTY_PAYLOAD_LEN); /* payload */
576 /* clang-format on */
577
578 return AWS_OP_SUCCESS;
579 }
580
581 static bool s_test_publish_eq(void *a, void *b, size_t size) {
582
583 (void)size;
584
585 struct aws_mqtt_packet_publish *l = a;
586 struct aws_mqtt_packet_publish *r = b;
587
588 return s_fixed_header_eq(&l->fixed_header, &r->fixed_header) && l->packet_identifier == r->packet_identifier &&
589 aws_byte_cursor_eq(&l->topic_name, &r->topic_name) && aws_byte_cursor_eq(&l->payload, &r->payload);
590 }
591 PACKET_TEST_NAME(PUBLISH, publish_qos0_dup, publish, &s_test_publish_qos0_dup_init, NULL, &s_test_publish_eq)
592 PACKET_TEST_NAME(PUBLISH, publish_qos2_retain, publish, &s_test_publish_qos2_retain_init, NULL, &s_test_publish_eq)
593 PACKET_TEST_NAME(PUBLISH, publish_empty_payload, publish, &s_test_publish_empty_payload_init, NULL, &s_test_publish_eq)
594
595 /*****************************************************************************/
596 /* Subscribe */
597
598 static int s_test_subscribe_init(struct packet_test_fixture *fixture) {
599
600 /* Init packets */
601 ASSERT_SUCCESS(aws_mqtt_packet_subscribe_init(fixture->in_packet, fixture->allocator, 7));
602 ASSERT_SUCCESS(aws_mqtt_packet_subscribe_init(fixture->out_packet, fixture->allocator, 0));
603
604 ASSERT_SUCCESS(aws_mqtt_packet_subscribe_add_topic(
605 fixture->in_packet, aws_byte_cursor_from_array(s_topic_name, TOPIC_NAME_LEN), AWS_MQTT_QOS_EXACTLY_ONCE));
606
607 /* Init buffer */ /* clang-format off */
608 aws_byte_buf_write_u8(
609 &fixture->buffer, (AWS_MQTT_PACKET_SUBSCRIBE << 4) | 0x2); /* Packet type & flags */
610 aws_byte_buf_write_u8(
611 &fixture->buffer, 4 + TOPIC_NAME_LEN + 1); /* Remaining length */
612 aws_byte_buf_write_u8(
613 &fixture->buffer, 0);
614 aws_byte_buf_write_u8(
615 &fixture->buffer, 7);
616 aws_byte_buf_write_u8(
617 &fixture->buffer, 0);
618 aws_byte_buf_write_u8(
619 &fixture->buffer, TOPIC_NAME_LEN);
620 aws_byte_buf_write(
621 &fixture->buffer, s_topic_name, TOPIC_NAME_LEN);
622 aws_byte_buf_write_u8(
623 &fixture->buffer, AWS_MQTT_QOS_EXACTLY_ONCE);
624 /* clang-format on */
625
626 return AWS_OP_SUCCESS;
627 }
628 static int s_test_subscribe_clean_up(struct packet_test_fixture *fixture) {
629
630 aws_mqtt_packet_subscribe_clean_up(fixture->in_packet);
631 aws_mqtt_packet_subscribe_clean_up(fixture->out_packet);
632
633 return AWS_OP_SUCCESS;
634 }
635 static bool s_test_subscribe_eq(void *a, void *b, size_t size) {
636
637 (void)size;
638
639 struct aws_mqtt_packet_subscribe *l = a;
640 struct aws_mqtt_packet_subscribe *r = b;
641
642 if (!s_fixed_header_eq(&l->fixed_header, &r->fixed_header) || l->packet_identifier != r->packet_identifier) {
643 return false;
644 }
645
646 const size_t length = aws_array_list_length(&l->topic_filters);
647 if (length != aws_array_list_length(&r->topic_filters)) {
648 return false;
649 }
650
651 for (size_t i = 0; i < length; ++i) {
652 struct aws_mqtt_subscription *lt = NULL;
653 aws_array_list_get_at_ptr(&l->topic_filters, (void **)&lt, i);
654 struct aws_mqtt_subscription *rt = NULL;
655 aws_array_list_get_at_ptr(&r->topic_filters, (void **)&rt, i);
656 AWS_ASSUME(lt && rt);
657
658 if (lt->qos != rt->qos) {
659 return false;
660 }
661 if (!aws_byte_cursor_eq(&lt->topic_filter, &rt->topic_filter)) {
662 return false;
663 }
664 }
665
666 return true;
667 }
668 PACKET_TEST(SUBSCRIBE, subscribe, &s_test_subscribe_init, &s_test_subscribe_clean_up, &s_test_subscribe_eq)
669
670 /*****************************************************************************/
671 /* Suback */
672
673 static int s_test_suback_init(struct packet_test_fixture *fixture) {
674
675 /* Init packets */
676 ASSERT_SUCCESS(aws_mqtt_packet_suback_init(fixture->in_packet, fixture->allocator, 7));
677 ASSERT_SUCCESS(aws_mqtt_packet_suback_init(fixture->out_packet, fixture->allocator, 0));
678
679 ASSERT_SUCCESS(aws_mqtt_packet_suback_add_return_code(fixture->in_packet, AWS_MQTT_QOS_EXACTLY_ONCE));
680 ASSERT_SUCCESS(aws_mqtt_packet_suback_add_return_code(fixture->in_packet, AWS_MQTT_QOS_FAILURE));
681
682 /* Init buffer */ /* clang-format off */
683 aws_byte_buf_write_u8(
684 &fixture->buffer, (AWS_MQTT_PACKET_SUBACK << 4) | 0x0); /* Packet type & flags */
685 aws_byte_buf_write_u8(
686 &fixture->buffer, 2/* variable header */ + 2/* payload */); /* Remaining length */
687 aws_byte_buf_write_u8(
688 &fixture->buffer, 0);
689 aws_byte_buf_write_u8(
690 &fixture->buffer, 7);
691 aws_byte_buf_write_u8(
692 &fixture->buffer, AWS_MQTT_QOS_EXACTLY_ONCE); /* Payload */
693 aws_byte_buf_write_u8(
694 &fixture->buffer, AWS_MQTT_QOS_FAILURE); /* Payload */
695 /* clang-format on */
696
697 return AWS_OP_SUCCESS;
698 }
699 static int s_test_suback_clean_up(struct packet_test_fixture *fixture) {
700
701 aws_mqtt_packet_suback_clean_up(fixture->in_packet);
702 aws_mqtt_packet_suback_clean_up(fixture->out_packet);
703
704 return AWS_OP_SUCCESS;
705 }
706 static bool s_test_suback_eq(void *a, void *b, size_t size) {
707
708 (void)size;
709
710 struct aws_mqtt_packet_suback *l = a;
711 struct aws_mqtt_packet_suback *r = b;
712
713 if (!s_fixed_header_eq(&l->fixed_header, &r->fixed_header) || l->packet_identifier != r->packet_identifier) {
714 return false;
715 }
716
717 const size_t length = aws_array_list_length(&l->return_codes);
718 if (length != aws_array_list_length(&r->return_codes)) {
719 return false;
720 }
721
722 for (size_t i = 0; i < length; ++i) {
723 uint8_t lt = 0;
724 aws_array_list_get_at(&l->return_codes, (void *)&lt, i);
725 uint8_t rt = 0;
726 aws_array_list_get_at(&r->return_codes, (void *)&rt, i);
727 AWS_ASSUME(lt && rt);
728 }
729
730 return true;
731 }
732 PACKET_TEST(SUBACK, suback, &s_test_suback_init, &s_test_suback_clean_up, &s_test_suback_eq)
733
734 /*****************************************************************************/
735 /* Unsubscribe */
736
737 static int s_test_unsubscribe_init(struct packet_test_fixture *fixture) {
738
739 /* Init packet */
740 ASSERT_SUCCESS(aws_mqtt_packet_unsubscribe_init(fixture->in_packet, fixture->allocator, 7));
741 ASSERT_SUCCESS(aws_mqtt_packet_unsubscribe_init(fixture->out_packet, fixture->allocator, 0));
742
743 ASSERT_SUCCESS(aws_mqtt_packet_unsubscribe_add_topic(
744 fixture->in_packet, aws_byte_cursor_from_array(s_topic_name, TOPIC_NAME_LEN)));
745
746 /* Init buffer */
747 /* clang-format off */
748 aws_byte_buf_write_u8(
749 &fixture->buffer, (AWS_MQTT_PACKET_UNSUBSCRIBE << 4) | 0x2); /* Packet type & flags */
750 aws_byte_buf_write_u8(
751 &fixture->buffer, 4 + TOPIC_NAME_LEN); /* Remaining length */
752 aws_byte_buf_write_u8(
753 &fixture->buffer, 0);
754 aws_byte_buf_write_u8(
755 &fixture->buffer, 7);
756 aws_byte_buf_write_u8(
757 &fixture->buffer, 0);
758 aws_byte_buf_write_u8(
759 &fixture->buffer, TOPIC_NAME_LEN);
760 aws_byte_buf_write(
761 &fixture->buffer, s_topic_name, TOPIC_NAME_LEN);
762 /* clang-format on */
763
764 return AWS_OP_SUCCESS;
765 }
766 static int s_test_unsubscribe_clean_up(struct packet_test_fixture *fixture) {
767
768 aws_mqtt_packet_unsubscribe_clean_up(fixture->in_packet);
769 aws_mqtt_packet_unsubscribe_clean_up(fixture->out_packet);
770
771 return AWS_OP_SUCCESS;
772 }
773 static bool s_test_unsubscribe_eq(void *a, void *b, size_t size) {
774
775 (void)size;
776
777 struct aws_mqtt_packet_unsubscribe *l = a;
778 struct aws_mqtt_packet_unsubscribe *r = b;
779
780 if (!s_fixed_header_eq(&l->fixed_header, &r->fixed_header) || l->packet_identifier != r->packet_identifier) {
781 return false;
782 }
783
784 const size_t length = aws_array_list_length(&l->topic_filters);
785 if (length != aws_array_list_length(&r->topic_filters)) {
786 return false;
787 }
788
789 for (size_t i = 0; i < length; ++i) {
790 struct aws_byte_cursor *lt = NULL;
791 aws_array_list_get_at_ptr(&l->topic_filters, (void **)&lt, i);
792 struct aws_byte_cursor *rt = NULL;
793 aws_array_list_get_at_ptr(&r->topic_filters, (void **)&rt, i);
794 AWS_ASSUME(lt && rt);
795
796 if (!aws_byte_cursor_eq(lt, rt)) {
797 return false;
798 }
799 }
800
801 return true;
802 }
803 PACKET_TEST(UNSUBSCRIBE, unsubscribe, &s_test_unsubscribe_init, &s_test_unsubscribe_clean_up, &s_test_unsubscribe_eq)
804
805 /*****************************************************************************/
806 /* Connection */
807
808 static int s_test_connection_init(struct packet_test_fixture *fixture) {
809
810 /* Init packet */
811 switch (fixture->type) {
812 case AWS_MQTT_PACKET_PINGREQ:
813 ASSERT_SUCCESS(aws_mqtt_packet_pingreq_init(fixture->in_packet));
814 break;
815 case AWS_MQTT_PACKET_PINGRESP:
816 ASSERT_SUCCESS(aws_mqtt_packet_pingresp_init(fixture->in_packet));
817 break;
818 case AWS_MQTT_PACKET_DISCONNECT:
819 ASSERT_SUCCESS(aws_mqtt_packet_disconnect_init(fixture->in_packet));
820 break;
821 default:
822 AWS_FATAL_ASSERT(false);
823 break;
824 }
825
826 /* Init buffer */
827 /* clang-format off */
828 uint8_t header[] = {
829 (uint8_t)(fixture->type << 4), /* Packet type */
830 0, /* Remaining length */
831 };
832 /* clang-format on */
833
834 aws_byte_buf_write(&fixture->buffer, header, sizeof(header));
835
836 return AWS_OP_SUCCESS;
837 }
838 #define PACKET_TEST_CONNETION(e_type, name) \
839 PACKET_TEST_NAME(e_type, name, connection, &s_test_connection_init, NULL, NULL)
840 PACKET_TEST_CONNETION(PINGREQ, pingreq)
841 PACKET_TEST_CONNETION(PINGRESP, pingresp)
842 PACKET_TEST_CONNETION(DISCONNECT, disconnect)
843 #undef PACKET_TEST_CONNETION
844
845 static int s_mqtt_packet_connack_decode_failure_reserved_fn(struct aws_allocator *allocator, void *ctx) {
846 (void)ctx;
847
848 struct aws_byte_buf encoded_packet;
849 aws_byte_buf_init(&encoded_packet, allocator, 1024);
850
851 struct aws_mqtt_packet_connack connack;
852 ASSERT_SUCCESS(aws_mqtt_packet_connack_init(&connack, true, AWS_MQTT_CONNECT_SERVER_UNAVAILABLE));
853
854 ASSERT_SUCCESS(aws_mqtt_packet_connack_encode(&encoded_packet, &connack));
855
856 struct aws_byte_cursor decode_cursor = aws_byte_cursor_from_buf(&encoded_packet);
857 struct aws_mqtt_packet_connack decoded_connack;
858 ASSERT_SUCCESS(aws_mqtt_packet_connack_decode(&decode_cursor, &decoded_connack));
859
860 /* mess up the fixed header reserved bits */
861 encoded_packet.buffer[0] |= 0x01;
862
863 decode_cursor = aws_byte_cursor_from_buf(&encoded_packet);
864 ASSERT_FAILS(aws_mqtt_packet_connack_decode(&decode_cursor, &decoded_connack));
865
866 aws_byte_buf_clean_up(&encoded_packet);
867
868 return AWS_OP_SUCCESS;
869 }
870
871 AWS_TEST_CASE(mqtt_packet_connack_decode_failure_reserved, s_mqtt_packet_connack_decode_failure_reserved_fn)
872
873 static int s_mqtt_packet_ack_decode_failure_reserved_fn(struct aws_allocator *allocator, void *ctx) {
874 (void)ctx;
875
876 struct aws_byte_buf encoded_packet;
877 aws_byte_buf_init(&encoded_packet, allocator, 1024);
878
879 struct aws_mqtt_packet_ack puback;
880 ASSERT_SUCCESS(aws_mqtt_packet_puback_init(&puback, 5));
881
882 ASSERT_SUCCESS(aws_mqtt_packet_ack_encode(&encoded_packet, &puback));
883
884 struct aws_byte_cursor decode_cursor = aws_byte_cursor_from_buf(&encoded_packet);
885 struct aws_mqtt_packet_ack decoded_ack;
886 ASSERT_SUCCESS(aws_mqtt_packet_ack_decode(&decode_cursor, &decoded_ack));
887
888 /* mess up the fixed header reserved bits */
889 encoded_packet.buffer[0] |= 0x0F;
890
891 decode_cursor = aws_byte_cursor_from_buf(&encoded_packet);
892 ASSERT_FAILS(aws_mqtt_packet_ack_decode(&decode_cursor, &decoded_ack));
893
894 aws_byte_buf_clean_up(&encoded_packet);
895
896 return AWS_OP_SUCCESS;
897 }
898
899 AWS_TEST_CASE(mqtt_packet_ack_decode_failure_reserved, s_mqtt_packet_ack_decode_failure_reserved_fn)
900
901 static int s_mqtt_packet_pingresp_decode_failure_reserved_fn(struct aws_allocator *allocator, void *ctx) {
902 (void)ctx;
903
904 struct aws_byte_buf encoded_packet;
905 aws_byte_buf_init(&encoded_packet, allocator, 1024);
906
907 struct aws_mqtt_packet_connection pingresp;
908 ASSERT_SUCCESS(aws_mqtt_packet_pingresp_init(&pingresp));
909
910 ASSERT_SUCCESS(aws_mqtt_packet_connection_encode(&encoded_packet, &pingresp));
911
912 struct aws_byte_cursor decode_cursor = aws_byte_cursor_from_buf(&encoded_packet);
913 struct aws_mqtt_packet_connection decoded_pingresp;
914 ASSERT_SUCCESS(aws_mqtt_packet_connection_decode(&decode_cursor, &decoded_pingresp));
915
916 /* mess up the fixed header reserved bits */
917 encoded_packet.buffer[0] |= 0x08;
918
919 decode_cursor = aws_byte_cursor_from_buf(&encoded_packet);
920 ASSERT_FAILS(aws_mqtt_packet_connection_decode(&decode_cursor, &decoded_pingresp));
921
922 aws_byte_buf_clean_up(&encoded_packet);
923
924 return AWS_OP_SUCCESS;
925 }
926
927 AWS_TEST_CASE(mqtt_packet_pingresp_decode_failure_reserved, s_mqtt_packet_pingresp_decode_failure_reserved_fn)
928
929 #ifdef _MSC_VER
930 # pragma warning(pop)
931 #endif