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 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761
|
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include "mqtt_mock_server_handler.h"
#include <aws/mqtt/private/client_impl.h>
#include <aws/io/channel_bootstrap.h>
#include <aws/io/event_loop.h>
#include <aws/io/host_resolver.h>
#include <aws/common/clock.h>
#include <aws/common/condition_variable.h>
#include <aws/testing/aws_test_harness.h>
#ifdef _WIN32
# define LOCAL_SOCK_TEST_PATTERN "\\\\.\\pipe\\testsock%llu"
#else
# define LOCAL_SOCK_TEST_PATTERN "testsock%llu.sock"
#endif
static const int TEST_LOG_SUBJECT = 60000;
static const int ONE_SEC = 1000000000;
struct received_publish_packet {
struct aws_byte_buf topic;
struct aws_byte_buf payload;
bool dup;
enum aws_mqtt_qos qos;
bool retain;
};
struct mqtt_connection_state_test {
struct aws_allocator *allocator;
struct aws_channel *server_channel;
struct aws_channel_handler *mock_server;
struct aws_client_bootstrap *client_bootstrap;
struct aws_server_bootstrap *server_bootstrap;
struct aws_event_loop_group *el_group;
struct aws_host_resolver *host_resolver;
struct aws_socket_endpoint endpoint;
struct aws_socket *listener;
struct aws_mqtt_client *mqtt_client;
struct aws_mqtt_client_connection *mqtt_connection;
struct aws_socket_options socket_options;
bool session_present;
bool connection_completed;
bool client_disconnect_completed;
bool server_disconnect_completed;
bool connection_interrupted;
bool connection_resumed;
bool subscribe_completed;
bool listener_destroyed;
int interruption_error;
int subscribe_complete_error;
int op_complete_error;
enum aws_mqtt_connect_return_code mqtt_return_code;
int error;
struct aws_condition_variable cvar;
struct aws_mutex lock;
/* any published messages from mock server, that you may not subscribe to. (Which should not happen in real life) */
struct aws_array_list any_published_messages; /* list of struct received_publish_packet */
size_t any_publishes_received;
size_t expected_any_publishes;
/* the published messages from mock server, that you did subscribe to. */
struct aws_array_list published_messages; /* list of struct received_publish_packet */
size_t publishes_received;
size_t expected_publishes;
/* The returned QoS from mock server */
struct aws_array_list qos_returned; /* list of uint_8 */
size_t ops_completed;
size_t expected_ops_completed;
};
static struct mqtt_connection_state_test test_data = {0};
static void s_on_any_publish_received(
struct aws_mqtt_client_connection *connection,
const struct aws_byte_cursor *topic,
const struct aws_byte_cursor *payload,
bool dup,
enum aws_mqtt_qos qos,
bool retain,
void *userdata);
static void s_on_incoming_channel_setup_fn(
struct aws_server_bootstrap *bootstrap,
int error_code,
struct aws_channel *channel,
void *user_data) {
(void)bootstrap;
struct mqtt_connection_state_test *state_test_data = user_data;
state_test_data->error = error_code;
if (!error_code) {
aws_mutex_lock(&state_test_data->lock);
state_test_data->server_disconnect_completed = false;
aws_mutex_unlock(&state_test_data->lock);
AWS_LOGF_DEBUG(TEST_LOG_SUBJECT, "server channel setup completed");
state_test_data->server_channel = channel;
struct aws_channel_slot *test_handler_slot = aws_channel_slot_new(channel);
aws_channel_slot_insert_end(channel, test_handler_slot);
mqtt_mock_server_handler_update_slot(state_test_data->mock_server, test_handler_slot);
aws_channel_slot_set_handler(test_handler_slot, state_test_data->mock_server);
}
}
static void s_on_incoming_channel_shutdown_fn(
struct aws_server_bootstrap *bootstrap,
int error_code,
struct aws_channel *channel,
void *user_data) {
(void)bootstrap;
(void)error_code;
(void)channel;
struct mqtt_connection_state_test *state_test_data = user_data;
aws_mutex_lock(&state_test_data->lock);
state_test_data->server_disconnect_completed = true;
AWS_LOGF_DEBUG(TEST_LOG_SUBJECT, "server channel shutdown completed");
aws_mutex_unlock(&state_test_data->lock);
aws_condition_variable_notify_one(&state_test_data->cvar);
}
static void s_on_listener_destroy(struct aws_server_bootstrap *bootstrap, void *user_data) {
(void)bootstrap;
struct mqtt_connection_state_test *state_test_data = user_data;
aws_mutex_lock(&state_test_data->lock);
state_test_data->listener_destroyed = true;
aws_mutex_unlock(&state_test_data->lock);
aws_condition_variable_notify_one(&state_test_data->cvar);
}
static bool s_is_listener_destroyed(void *arg) {
struct mqtt_connection_state_test *state_test_data = arg;
return state_test_data->listener_destroyed;
}
static void s_wait_on_listener_cleanup(struct mqtt_connection_state_test *state_test_data) {
aws_mutex_lock(&state_test_data->lock);
aws_condition_variable_wait_pred(
&state_test_data->cvar, &state_test_data->lock, s_is_listener_destroyed, state_test_data);
aws_mutex_unlock(&state_test_data->lock);
}
static void s_on_connection_interrupted(struct aws_mqtt_client_connection *connection, int error_code, void *userdata) {
(void)connection;
(void)error_code;
struct mqtt_connection_state_test *state_test_data = userdata;
aws_mutex_lock(&state_test_data->lock);
state_test_data->connection_interrupted = true;
state_test_data->interruption_error = error_code;
aws_mutex_unlock(&state_test_data->lock);
AWS_LOGF_DEBUG(TEST_LOG_SUBJECT, "connection interrupted");
aws_condition_variable_notify_one(&state_test_data->cvar);
}
static bool s_is_connection_interrupted(void *arg) {
struct mqtt_connection_state_test *state_test_data = arg;
return state_test_data->connection_interrupted;
}
static void s_wait_for_interrupt_to_complete(struct mqtt_connection_state_test *state_test_data) {
aws_mutex_lock(&state_test_data->lock);
aws_condition_variable_wait_pred(
&state_test_data->cvar, &state_test_data->lock, s_is_connection_interrupted, state_test_data);
state_test_data->connection_interrupted = false;
aws_mutex_unlock(&state_test_data->lock);
}
static void s_on_connection_resumed(
struct aws_mqtt_client_connection *connection,
enum aws_mqtt_connect_return_code return_code,
bool session_present,
void *userdata) {
(void)connection;
(void)return_code;
(void)session_present;
AWS_LOGF_DEBUG(TEST_LOG_SUBJECT, "reconnect completed");
struct mqtt_connection_state_test *state_test_data = userdata;
aws_mutex_lock(&state_test_data->lock);
state_test_data->connection_resumed = true;
aws_mutex_unlock(&state_test_data->lock);
aws_condition_variable_notify_one(&state_test_data->cvar);
}
static bool s_is_connection_resumed(void *arg) {
struct mqtt_connection_state_test *state_test_data = arg;
return state_test_data->connection_resumed;
}
static void s_wait_for_reconnect_to_complete(struct mqtt_connection_state_test *state_test_data) {
aws_mutex_lock(&state_test_data->lock);
aws_condition_variable_wait_pred(
&state_test_data->cvar, &state_test_data->lock, s_is_connection_resumed, state_test_data);
aws_mutex_unlock(&state_test_data->lock);
}
/** sets up a unix domain socket server and socket options. Creates an mqtt connection configured to use
* the domain socket.
*/
static int s_setup_mqtt_server_fn(struct aws_allocator *allocator, void *ctx) {
aws_mqtt_library_init(allocator);
struct mqtt_connection_state_test *state_test_data = ctx;
AWS_ZERO_STRUCT(*state_test_data);
state_test_data->allocator = allocator;
state_test_data->el_group = aws_event_loop_group_new_default(allocator, 1, NULL);
state_test_data->mock_server = new_mqtt_mock_server(allocator);
ASSERT_NOT_NULL(state_test_data->mock_server);
state_test_data->server_bootstrap = aws_server_bootstrap_new(allocator, state_test_data->el_group);
ASSERT_NOT_NULL(state_test_data->server_bootstrap);
struct aws_socket_options socket_options = {
.connect_timeout_ms = 100,
.domain = AWS_SOCKET_LOCAL,
};
state_test_data->socket_options = socket_options;
ASSERT_SUCCESS(aws_condition_variable_init(&state_test_data->cvar));
ASSERT_SUCCESS(aws_mutex_init(&state_test_data->lock));
uint64_t timestamp = 0;
ASSERT_SUCCESS(aws_sys_clock_get_ticks(×tamp));
snprintf(
state_test_data->endpoint.address,
sizeof(state_test_data->endpoint.address),
LOCAL_SOCK_TEST_PATTERN,
(long long unsigned)timestamp);
struct aws_server_socket_channel_bootstrap_options server_bootstrap_options = {
.bootstrap = state_test_data->server_bootstrap,
.host_name = state_test_data->endpoint.address,
.port = state_test_data->endpoint.port,
.socket_options = &state_test_data->socket_options,
.incoming_callback = s_on_incoming_channel_setup_fn,
.shutdown_callback = s_on_incoming_channel_shutdown_fn,
.destroy_callback = s_on_listener_destroy,
.user_data = state_test_data,
};
state_test_data->listener = aws_server_bootstrap_new_socket_listener(&server_bootstrap_options);
ASSERT_NOT_NULL(state_test_data->listener);
struct aws_host_resolver_default_options resolver_options = {
.el_group = state_test_data->el_group,
.max_entries = 1,
};
state_test_data->host_resolver = aws_host_resolver_new_default(allocator, &resolver_options);
struct aws_client_bootstrap_options bootstrap_options = {
.event_loop_group = state_test_data->el_group,
.user_data = state_test_data,
.host_resolver = state_test_data->host_resolver,
};
state_test_data->client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
state_test_data->mqtt_client = aws_mqtt_client_new(allocator, state_test_data->client_bootstrap);
state_test_data->mqtt_connection = aws_mqtt_client_connection_new(state_test_data->mqtt_client);
ASSERT_NOT_NULL(state_test_data->mqtt_connection);
ASSERT_SUCCESS(aws_mqtt_client_connection_set_connection_interruption_handlers(
state_test_data->mqtt_connection,
s_on_connection_interrupted,
state_test_data,
s_on_connection_resumed,
state_test_data));
ASSERT_SUCCESS(aws_mqtt_client_connection_set_on_any_publish_handler(
state_test_data->mqtt_connection, s_on_any_publish_received, state_test_data));
ASSERT_SUCCESS(aws_array_list_init_dynamic(
&state_test_data->published_messages, allocator, 4, sizeof(struct received_publish_packet)));
ASSERT_SUCCESS(aws_array_list_init_dynamic(
&state_test_data->any_published_messages, allocator, 4, sizeof(struct received_publish_packet)));
ASSERT_SUCCESS(aws_array_list_init_dynamic(&state_test_data->qos_returned, allocator, 2, sizeof(uint8_t)));
return AWS_OP_SUCCESS;
}
static void s_received_publish_packet_list_clean_up(struct aws_array_list *list) {
for (size_t i = 0; i < aws_array_list_length(list); ++i) {
struct received_publish_packet *val_ptr = NULL;
aws_array_list_get_at_ptr(list, (void **)&val_ptr, i);
aws_byte_buf_clean_up(&val_ptr->payload);
aws_byte_buf_clean_up(&val_ptr->topic);
}
aws_array_list_clean_up(list);
}
static int s_clean_up_mqtt_server_fn(struct aws_allocator *allocator, int setup_result, void *ctx) {
(void)allocator;
if (!setup_result) {
struct mqtt_connection_state_test *state_test_data = ctx;
s_received_publish_packet_list_clean_up(&state_test_data->published_messages);
s_received_publish_packet_list_clean_up(&state_test_data->any_published_messages);
aws_array_list_clean_up(&state_test_data->qos_returned);
aws_mqtt_client_connection_release(state_test_data->mqtt_connection);
aws_mqtt_client_release(state_test_data->mqtt_client);
aws_client_bootstrap_release(state_test_data->client_bootstrap);
aws_host_resolver_release(state_test_data->host_resolver);
aws_server_bootstrap_destroy_socket_listener(state_test_data->server_bootstrap, state_test_data->listener);
s_wait_on_listener_cleanup(state_test_data);
aws_server_bootstrap_release(state_test_data->server_bootstrap);
aws_event_loop_group_release(state_test_data->el_group);
destroy_mqtt_mock_server(state_test_data->mock_server);
}
aws_mqtt_library_clean_up();
return AWS_OP_SUCCESS;
}
static void s_on_connection_complete_fn(
struct aws_mqtt_client_connection *connection,
int error_code,
enum aws_mqtt_connect_return_code return_code,
bool session_present,
void *userdata) {
(void)connection;
struct mqtt_connection_state_test *state_test_data = userdata;
aws_mutex_lock(&state_test_data->lock);
state_test_data->session_present = session_present;
state_test_data->mqtt_return_code = return_code;
state_test_data->error = error_code;
state_test_data->connection_completed = true;
aws_mutex_unlock(&state_test_data->lock);
aws_condition_variable_notify_one(&state_test_data->cvar);
}
static bool s_is_connection_completed(void *arg) {
struct mqtt_connection_state_test *state_test_data = arg;
return state_test_data->connection_completed;
}
static void s_wait_for_connection_to_complete(struct mqtt_connection_state_test *state_test_data) {
aws_mutex_lock(&state_test_data->lock);
aws_condition_variable_wait_pred(
&state_test_data->cvar, &state_test_data->lock, s_is_connection_completed, state_test_data);
state_test_data->connection_completed = false;
aws_mutex_unlock(&state_test_data->lock);
}
void s_on_disconnect_fn(struct aws_mqtt_client_connection *connection, void *userdata) {
(void)connection;
struct mqtt_connection_state_test *state_test_data = userdata;
AWS_LOGF_DEBUG(TEST_LOG_SUBJECT, "disconnect completed");
aws_mutex_lock(&state_test_data->lock);
state_test_data->client_disconnect_completed = true;
aws_mutex_unlock(&state_test_data->lock);
aws_condition_variable_notify_one(&state_test_data->cvar);
}
static bool s_is_disconnect_completed(void *arg) {
struct mqtt_connection_state_test *state_test_data = arg;
return state_test_data->client_disconnect_completed && state_test_data->server_disconnect_completed;
}
static void s_wait_for_disconnect_to_complete(struct mqtt_connection_state_test *state_test_data) {
aws_mutex_lock(&state_test_data->lock);
aws_condition_variable_wait_pred(
&state_test_data->cvar, &state_test_data->lock, s_is_disconnect_completed, state_test_data);
state_test_data->client_disconnect_completed = false;
state_test_data->server_disconnect_completed = false;
aws_mutex_unlock(&state_test_data->lock);
}
static void s_on_any_publish_received(
struct aws_mqtt_client_connection *connection,
const struct aws_byte_cursor *topic,
const struct aws_byte_cursor *payload,
bool dup,
enum aws_mqtt_qos qos,
bool retain,
void *userdata) {
(void)connection;
struct mqtt_connection_state_test *state_test_data = userdata;
struct aws_byte_buf payload_cp;
aws_byte_buf_init_copy_from_cursor(&payload_cp, state_test_data->allocator, *payload);
struct aws_byte_buf topic_cp;
aws_byte_buf_init_copy_from_cursor(&topic_cp, state_test_data->allocator, *topic);
struct received_publish_packet received_packet = {
.payload = payload_cp,
.topic = topic_cp,
.dup = dup,
.qos = qos,
.retain = retain,
};
aws_mutex_lock(&state_test_data->lock);
aws_array_list_push_back(&state_test_data->any_published_messages, &received_packet);
state_test_data->any_publishes_received++;
aws_mutex_unlock(&state_test_data->lock);
aws_condition_variable_notify_one(&state_test_data->cvar);
}
static bool s_is_any_publish_received(void *arg) {
struct mqtt_connection_state_test *state_test_data = arg;
return state_test_data->any_publishes_received == state_test_data->expected_any_publishes;
}
static void s_wait_for_any_publish(struct mqtt_connection_state_test *state_test_data) {
aws_mutex_lock(&state_test_data->lock);
aws_condition_variable_wait_pred(
&state_test_data->cvar, &state_test_data->lock, s_is_any_publish_received, state_test_data);
state_test_data->any_publishes_received = 0;
state_test_data->expected_any_publishes = 0;
aws_mutex_unlock(&state_test_data->lock);
}
static void s_on_publish_received(
struct aws_mqtt_client_connection *connection,
const struct aws_byte_cursor *topic,
const struct aws_byte_cursor *payload,
bool dup,
enum aws_mqtt_qos qos,
bool retain,
void *userdata) {
(void)connection;
(void)topic;
struct mqtt_connection_state_test *state_test_data = userdata;
struct aws_byte_buf payload_cp;
aws_byte_buf_init_copy_from_cursor(&payload_cp, state_test_data->allocator, *payload);
struct aws_byte_buf topic_cp;
aws_byte_buf_init_copy_from_cursor(&topic_cp, state_test_data->allocator, *topic);
struct received_publish_packet received_packet = {
.payload = payload_cp,
.topic = topic_cp,
.dup = dup,
.qos = qos,
.retain = retain,
};
aws_mutex_lock(&state_test_data->lock);
aws_array_list_push_back(&state_test_data->published_messages, &received_packet);
state_test_data->publishes_received++;
aws_mutex_unlock(&state_test_data->lock);
aws_condition_variable_notify_one(&state_test_data->cvar);
}
static bool s_is_publish_received(void *arg) {
struct mqtt_connection_state_test *state_test_data = arg;
return state_test_data->publishes_received == state_test_data->expected_publishes;
}
static void s_wait_for_publish(struct mqtt_connection_state_test *state_test_data) {
aws_mutex_lock(&state_test_data->lock);
aws_condition_variable_wait_pred(
&state_test_data->cvar, &state_test_data->lock, s_is_publish_received, state_test_data);
state_test_data->publishes_received = 0;
state_test_data->expected_publishes = 0;
aws_mutex_unlock(&state_test_data->lock);
}
static void s_on_suback(
struct aws_mqtt_client_connection *connection,
uint16_t packet_id,
const struct aws_byte_cursor *topic,
enum aws_mqtt_qos qos,
int error_code,
void *userdata) {
(void)connection;
(void)packet_id;
(void)topic;
struct mqtt_connection_state_test *state_test_data = userdata;
aws_mutex_lock(&state_test_data->lock);
if (!error_code) {
aws_array_list_push_back(&state_test_data->qos_returned, &qos);
}
state_test_data->subscribe_completed = true;
state_test_data->subscribe_complete_error = error_code;
aws_mutex_unlock(&state_test_data->lock);
aws_condition_variable_notify_one(&state_test_data->cvar);
}
static bool s_is_subscribe_completed(void *arg) {
struct mqtt_connection_state_test *state_test_data = arg;
return state_test_data->subscribe_completed;
}
static void s_wait_for_subscribe_to_complete(struct mqtt_connection_state_test *state_test_data) {
aws_mutex_lock(&state_test_data->lock);
aws_condition_variable_wait_pred(
&state_test_data->cvar, &state_test_data->lock, s_is_subscribe_completed, state_test_data);
state_test_data->subscribe_completed = false;
aws_mutex_unlock(&state_test_data->lock);
}
static void s_on_multi_suback(
struct aws_mqtt_client_connection *connection,
uint16_t packet_id,
const struct aws_array_list *topic_subacks, /* contains aws_mqtt_topic_subscription pointers */
int error_code,
void *userdata) {
(void)connection;
(void)packet_id;
(void)topic_subacks;
(void)error_code;
struct mqtt_connection_state_test *state_test_data = userdata;
aws_mutex_lock(&state_test_data->lock);
state_test_data->subscribe_completed = true;
if (!error_code) {
size_t length = aws_array_list_length(topic_subacks);
for (size_t i = 0; i < length; ++i) {
struct aws_mqtt_topic_subscription *subscription = NULL;
aws_array_list_get_at(topic_subacks, &subscription, i);
aws_array_list_push_back(&state_test_data->qos_returned, &subscription->qos);
}
}
aws_mutex_unlock(&state_test_data->lock);
aws_condition_variable_notify_one(&state_test_data->cvar);
}
static void s_on_op_complete(
struct aws_mqtt_client_connection *connection,
uint16_t packet_id,
int error_code,
void *userdata) {
(void)connection;
(void)packet_id;
struct mqtt_connection_state_test *state_test_data = userdata;
AWS_LOGF_DEBUG(TEST_LOG_SUBJECT, "pub op completed");
aws_mutex_lock(&state_test_data->lock);
state_test_data->ops_completed++;
state_test_data->op_complete_error = error_code;
aws_mutex_unlock(&state_test_data->lock);
aws_condition_variable_notify_one(&state_test_data->cvar);
}
static bool s_is_ops_completed(void *arg) {
struct mqtt_connection_state_test *state_test_data = arg;
return state_test_data->ops_completed == state_test_data->expected_ops_completed;
}
static void s_wait_for_ops_completed(struct mqtt_connection_state_test *state_test_data) {
aws_mutex_lock(&state_test_data->lock);
aws_condition_variable_wait_for_pred(
&state_test_data->cvar, &state_test_data->lock, 10000000000, s_is_ops_completed, state_test_data);
aws_mutex_unlock(&state_test_data->lock);
}
/*
* Makes an Mqtt connect call, then a disconnect. Then verifies a CONNECT and DISCONNECT were sent.
*/
static int s_test_mqtt_connect_disconnect_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = false,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
};
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
/* Decode all received packets by mock server */
ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
ASSERT_UINT_EQUALS(2, mqtt_mock_server_decoded_packets_count(state_test_data->mock_server));
struct mqtt_decoded_packet *received_packet =
mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_DISCONNECT, received_packet->type);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connect_disconnect,
s_setup_mqtt_server_fn,
s_test_mqtt_connect_disconnect_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/*
* Makes an Mqtt connect call, and set will and login information for the connection. Validate the those information are
* correctly included in the CONNECT package.
*/
static int s_test_mqtt_connect_set_will_login_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_byte_cursor will_payload = aws_byte_cursor_from_c_str("this is a will.");
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("test_topic");
struct aws_byte_cursor username = aws_byte_cursor_from_c_str("user name");
struct aws_byte_cursor password = aws_byte_cursor_from_c_str("password");
enum aws_mqtt_qos will_qos = AWS_MQTT_QOS_AT_LEAST_ONCE;
ASSERT_SUCCESS(aws_mqtt_client_connection_set_will(
state_test_data->mqtt_connection, &topic, will_qos, true /*retain*/, &will_payload));
ASSERT_SUCCESS(aws_mqtt_client_connection_set_login(state_test_data->mqtt_connection, &username, &password));
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = false,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
};
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
/* Decode all received packets by mock server */
ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
ASSERT_UINT_EQUALS(2, mqtt_mock_server_decoded_packets_count(state_test_data->mock_server));
/* CONNECT packet */
struct mqtt_decoded_packet *received_packet =
mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
/* validate the received will */
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->will_message, &will_payload));
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->will_topic, &topic));
ASSERT_UINT_EQUALS(will_qos, received_packet->will_qos);
ASSERT_TRUE(true == received_packet->will_retain);
/* validate the received login information */
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->username, &username));
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->password, &password));
/* DISCONNECT packet */
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_DISCONNECT, received_packet->type);
/* Connect to the mock server again. If set will&loggin message is not called before the next connect, the
* will&loggin message will still be there and be sent to the server again */
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
/* The second CONNECT packet */
received_packet = mqtt_mock_server_get_latest_decoded_packet(state_test_data->mock_server);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
/* validate the received will */
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->will_message, &will_payload));
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->will_topic, &topic));
ASSERT_UINT_EQUALS(will_qos, received_packet->will_qos);
ASSERT_TRUE(true == received_packet->will_retain);
/* validate the received login information */
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->username, &username));
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->password, &password));
/* disconnect */
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
/* set new will & loggin message, before next connect, the next CONNECT packet will contain the new information */
struct aws_byte_cursor new_will_payload = aws_byte_cursor_from_c_str("this is a new will.");
struct aws_byte_cursor new_topic = aws_byte_cursor_from_c_str("test_topic_New");
struct aws_byte_cursor new_username = aws_byte_cursor_from_c_str("new user name");
struct aws_byte_cursor new_password = aws_byte_cursor_from_c_str("new password");
enum aws_mqtt_qos new_will_qos = AWS_MQTT_QOS_AT_MOST_ONCE;
ASSERT_SUCCESS(aws_mqtt_client_connection_set_will(
state_test_data->mqtt_connection, &new_topic, new_will_qos, true /*retain*/, &new_will_payload));
ASSERT_SUCCESS(
aws_mqtt_client_connection_set_login(state_test_data->mqtt_connection, &new_username, &new_password));
/* connect again */
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
/* The third CONNECT packet */
received_packet = mqtt_mock_server_get_latest_decoded_packet(state_test_data->mock_server);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
/* validate the received will */
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->will_message, &new_will_payload));
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->will_topic, &new_topic));
ASSERT_UINT_EQUALS(new_will_qos, received_packet->will_qos);
ASSERT_TRUE(true == received_packet->will_retain);
/* validate the received login information */
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->username, &new_username));
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->password, &new_password));
/* disconnect. FINISHED */
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connect_set_will_login,
s_setup_mqtt_server_fn,
s_test_mqtt_connect_set_will_login_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/*
* Makes a CONNECT, then the server hangs up, tests that the client reconnects on its own, then sends a DISCONNECT.
*/
static int s_test_mqtt_connection_interrupted_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = true,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
};
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
/* shut it down and make sure the client automatically reconnects.*/
aws_channel_shutdown(state_test_data->server_channel, AWS_OP_SUCCESS);
s_wait_for_reconnect_to_complete(state_test_data);
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
/* Decode all received packets by mock server */
ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
ASSERT_UINT_EQUALS(3, mqtt_mock_server_decoded_packets_count(state_test_data->mock_server));
struct mqtt_decoded_packet *received_packet =
mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 2);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_DISCONNECT, received_packet->type);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connection_interrupted,
s_setup_mqtt_server_fn,
s_test_mqtt_connection_interrupted_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/* Makes a CONNECT, with a 1 second keep alive ping interval, the mock is configured to not reply to the PING,
* this should cause a timeout, then the PING responses are turned back on, and the client should automatically
* reconnect. Then send a DISCONNECT. */
static int s_test_mqtt_connection_timeout_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = true,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
.keep_alive_time_secs = 1,
.ping_timeout_ms = 100,
};
mqtt_mock_server_set_max_ping_resp(state_test_data->mock_server, 0);
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
/* this should take about 1.1 seconds for the timeout and reconnect.*/
s_wait_for_reconnect_to_complete(state_test_data);
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
ASSERT_INT_EQUALS(AWS_ERROR_MQTT_TIMEOUT, state_test_data->interruption_error);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connection_timeout,
s_setup_mqtt_server_fn,
s_test_mqtt_connection_timeout_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/* Test set on_any_publish handler. User can set on_any_publish handler to be called whenever any publish packet is
* received */
static int s_test_mqtt_connection_any_publish_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = false,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
};
struct aws_byte_cursor topic_1 = aws_byte_cursor_from_c_str("/test/topic1");
struct aws_byte_cursor topic_2 = aws_byte_cursor_from_c_str("/test/topic2");
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
/* NOTE: mock server sends to client with no subscription at all, which should not happen in the real world! */
state_test_data->expected_any_publishes = 2;
struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
ASSERT_SUCCESS(mqtt_mock_server_send_publish(
state_test_data->mock_server,
&topic_1,
&payload_1,
false /*dup*/,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false /*retain*/));
struct aws_byte_cursor payload_2 = aws_byte_cursor_from_c_str("Test Message 2");
ASSERT_SUCCESS(mqtt_mock_server_send_publish(
state_test_data->mock_server,
&topic_2,
&payload_2,
false /*dup*/,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false /*retain*/));
s_wait_for_any_publish(state_test_data);
mqtt_mock_server_wait_for_pubacks(state_test_data->mock_server, 2);
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
/* Decode all received packets by mock server */
ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
/* CONNECT two PUBACK DISCONNECT */
ASSERT_UINT_EQUALS(4, mqtt_mock_server_decoded_packets_count(state_test_data->mock_server));
struct mqtt_decoded_packet *received_packet =
mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 2);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 3);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_DISCONNECT, received_packet->type);
/* Check the received publish packet from the client side */
ASSERT_UINT_EQUALS(2, aws_array_list_length(&state_test_data->any_published_messages));
struct received_publish_packet *publish_msg = NULL;
ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->any_published_messages, (void **)&publish_msg, 0));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&topic_1, &publish_msg->topic));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_1, &publish_msg->payload));
ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->any_published_messages, (void **)&publish_msg, 1));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&topic_2, &publish_msg->topic));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_2, &publish_msg->payload));
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connection_any_publish,
s_setup_mqtt_server_fn,
s_test_mqtt_connection_any_publish_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/* Makes a CONNECT, channel is successfully setup, but the server never sends a connack, make sure we timeout. */
static int s_test_mqtt_connection_connack_timeout_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = true,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
.keep_alive_time_secs = 1,
.ping_timeout_ms = 100,
};
mqtt_mock_server_set_max_connack(state_test_data->mock_server, 0);
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
ASSERT_INT_EQUALS(AWS_ERROR_MQTT_TIMEOUT, state_test_data->error);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connection_connack_timeout,
s_setup_mqtt_server_fn,
s_test_mqtt_connection_connack_timeout_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/* Subscribe to a topic prior to connection, make a CONNECT, have the server send PUBLISH messages,
* make sure they're received, then send a DISCONNECT. */
static int s_test_mqtt_subscribe_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = false,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
};
struct aws_byte_cursor sub_topic = aws_byte_cursor_from_c_str("/test/topic");
uint16_t packet_id = aws_mqtt_client_connection_subscribe(
state_test_data->mqtt_connection,
&sub_topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
s_on_publish_received,
state_test_data,
NULL,
s_on_suback,
state_test_data);
ASSERT_TRUE(packet_id > 0);
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
s_wait_for_subscribe_to_complete(state_test_data);
state_test_data->expected_publishes = 2;
state_test_data->expected_any_publishes = 2;
struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
ASSERT_SUCCESS(mqtt_mock_server_send_publish(
state_test_data->mock_server,
&sub_topic,
&payload_1,
false /*dup*/,
AWS_MQTT_QOS_AT_LEAST_ONCE,
true /*retain*/));
struct aws_byte_cursor payload_2 = aws_byte_cursor_from_c_str("Test Message 2");
ASSERT_SUCCESS(mqtt_mock_server_send_publish(
state_test_data->mock_server,
&sub_topic,
&payload_2,
true /*dup*/,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false /*retain*/));
s_wait_for_publish(state_test_data);
s_wait_for_any_publish(state_test_data);
mqtt_mock_server_wait_for_pubacks(state_test_data->mock_server, 2);
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
/* Decode all received packets by mock server */
ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
ASSERT_UINT_EQUALS(5, mqtt_mock_server_decoded_packets_count(state_test_data->mock_server));
struct mqtt_decoded_packet *received_packet =
mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_SUBSCRIBE, received_packet->type);
ASSERT_UINT_EQUALS(1, aws_array_list_length(&received_packet->sub_topic_filters));
struct aws_mqtt_subscription val;
ASSERT_SUCCESS(aws_array_list_front(&received_packet->sub_topic_filters, &val));
ASSERT_TRUE(aws_byte_cursor_eq(&val.topic_filter, &sub_topic));
ASSERT_UINT_EQUALS(AWS_MQTT_QOS_AT_LEAST_ONCE, val.qos);
ASSERT_UINT_EQUALS(packet_id, received_packet->packet_identifier);
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 2);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 3);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 4);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_DISCONNECT, received_packet->type);
/* Check PUBLISH packets received via subscription callback */
ASSERT_UINT_EQUALS(2, aws_array_list_length(&state_test_data->published_messages));
struct received_publish_packet *publish_msg = NULL;
ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->published_messages, (void **)&publish_msg, 0));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&sub_topic, &publish_msg->topic));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_1, &publish_msg->payload));
ASSERT_FALSE(publish_msg->dup);
ASSERT_TRUE(publish_msg->retain);
ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->published_messages, (void **)&publish_msg, 1));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&sub_topic, &publish_msg->topic));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_2, &publish_msg->payload));
ASSERT_TRUE(publish_msg->dup);
ASSERT_FALSE(publish_msg->retain);
/* Check PUBLISH packets received via on_any_publish callback */
ASSERT_UINT_EQUALS(2, aws_array_list_length(&state_test_data->any_published_messages));
ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->any_published_messages, (void **)&publish_msg, 0));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&sub_topic, &publish_msg->topic));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_1, &publish_msg->payload));
ASSERT_FALSE(publish_msg->dup);
ASSERT_TRUE(publish_msg->retain);
ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->any_published_messages, (void **)&publish_msg, 1));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&sub_topic, &publish_msg->topic));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_2, &publish_msg->payload));
ASSERT_TRUE(publish_msg->dup);
ASSERT_FALSE(publish_msg->retain);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connect_subscribe,
s_setup_mqtt_server_fn,
s_test_mqtt_subscribe_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/* Subscribe to a topic and broker returns a SUBACK with failure return code, the subscribe should fail */
static int s_test_mqtt_connect_subscribe_fail_from_broker_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = false,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
};
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
/* Disable the auto ACK packets sent by the server, we will send failure SUBACK */
mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
struct aws_byte_cursor sub_topic = aws_byte_cursor_from_c_str("/test/topic");
uint16_t packet_id = aws_mqtt_client_connection_subscribe(
state_test_data->mqtt_connection,
&sub_topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
s_on_publish_received,
state_test_data,
NULL,
s_on_suback,
state_test_data);
ASSERT_TRUE(packet_id > 0);
ASSERT_SUCCESS(mqtt_mock_server_send_single_suback(state_test_data->mock_server, packet_id, AWS_MQTT_QOS_FAILURE));
s_wait_for_subscribe_to_complete(state_test_data);
/* Check the subscribe returned QoS is failure */
size_t length = aws_array_list_length(&state_test_data->qos_returned);
ASSERT_UINT_EQUALS(1, length);
uint8_t qos = 0;
ASSERT_SUCCESS(aws_array_list_get_at(&state_test_data->qos_returned, &qos, 0));
ASSERT_UINT_EQUALS(AWS_MQTT_QOS_FAILURE, qos);
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connect_subscribe_fail_from_broker,
s_setup_mqtt_server_fn,
s_test_mqtt_connect_subscribe_fail_from_broker_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/* Subscribe to multiple topics prior to connection, make a CONNECT, have the server send PUBLISH messages,
* make sure they're received, then send a DISCONNECT. */
static int s_test_mqtt_subscribe_multi_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = false,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
};
struct aws_byte_cursor sub_topic_1 = aws_byte_cursor_from_c_str("/test/topic1");
struct aws_byte_cursor sub_topic_2 = aws_byte_cursor_from_c_str("/test/topic2");
struct aws_mqtt_topic_subscription sub1 = {
.topic = sub_topic_1,
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE,
.on_publish = s_on_publish_received,
.on_cleanup = NULL,
.on_publish_ud = state_test_data,
};
struct aws_mqtt_topic_subscription sub2 = {
.topic = sub_topic_2,
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE,
.on_publish = s_on_publish_received,
.on_cleanup = NULL,
.on_publish_ud = state_test_data,
};
struct aws_array_list topic_filters;
size_t list_len = 2;
AWS_VARIABLE_LENGTH_ARRAY(uint8_t, static_buf, list_len * sizeof(struct aws_mqtt_topic_subscription));
aws_array_list_init_static(&topic_filters, static_buf, list_len, sizeof(struct aws_mqtt_topic_subscription));
aws_array_list_push_back(&topic_filters, &sub1);
aws_array_list_push_back(&topic_filters, &sub2);
uint16_t packet_id = aws_mqtt_client_connection_subscribe_multiple(
state_test_data->mqtt_connection, &topic_filters, s_on_multi_suback, state_test_data);
ASSERT_TRUE(packet_id > 0);
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
s_wait_for_subscribe_to_complete(state_test_data);
/* Check the subscribe returned QoS is expected */
size_t length = aws_array_list_length(&state_test_data->qos_returned);
ASSERT_UINT_EQUALS(2, length);
uint8_t qos = 0;
ASSERT_SUCCESS(aws_array_list_get_at(&state_test_data->qos_returned, &qos, 0));
ASSERT_UINT_EQUALS(AWS_MQTT_QOS_EXACTLY_ONCE, qos);
ASSERT_SUCCESS(aws_array_list_get_at(&state_test_data->qos_returned, &qos, 1));
ASSERT_UINT_EQUALS(AWS_MQTT_QOS_EXACTLY_ONCE, qos);
state_test_data->expected_publishes = 2;
struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
ASSERT_SUCCESS(mqtt_mock_server_send_publish(
state_test_data->mock_server,
&sub_topic_1,
&payload_1,
false /*dup*/,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false /*retain*/));
struct aws_byte_cursor payload_2 = aws_byte_cursor_from_c_str("Test Message 2");
ASSERT_SUCCESS(mqtt_mock_server_send_publish(
state_test_data->mock_server,
&sub_topic_2,
&payload_2,
false /*dup*/,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false /*retain*/));
s_wait_for_publish(state_test_data);
/* Let's do another publish on a topic that is not subscribed by client.
* This can happen if the Server automatically assigned a subscription to the Client */
state_test_data->expected_any_publishes = 3;
struct aws_byte_cursor payload_3 = aws_byte_cursor_from_c_str("Test Message 3");
struct aws_byte_cursor topic_3 = aws_byte_cursor_from_c_str("/test/topic3");
ASSERT_SUCCESS(mqtt_mock_server_send_publish(
state_test_data->mock_server,
&topic_3,
&payload_3,
false /*dup*/,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false /*retain*/));
s_wait_for_any_publish(state_test_data);
mqtt_mock_server_wait_for_pubacks(state_test_data->mock_server, 3);
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
/* Decode all received packets by mock server */
ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
ASSERT_UINT_EQUALS(6, mqtt_mock_server_decoded_packets_count(state_test_data->mock_server));
struct mqtt_decoded_packet *received_packet =
mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_SUBSCRIBE, received_packet->type);
ASSERT_UINT_EQUALS(2, aws_array_list_length(&received_packet->sub_topic_filters));
struct aws_mqtt_subscription val;
ASSERT_SUCCESS(aws_array_list_front(&received_packet->sub_topic_filters, &val));
ASSERT_TRUE(aws_byte_cursor_eq(&val.topic_filter, &sub_topic_1));
ASSERT_UINT_EQUALS(AWS_MQTT_QOS_AT_LEAST_ONCE, val.qos);
ASSERT_SUCCESS(aws_array_list_back(&received_packet->sub_topic_filters, &val));
ASSERT_TRUE(aws_byte_cursor_eq(&val.topic_filter, &sub_topic_2));
ASSERT_UINT_EQUALS(AWS_MQTT_QOS_AT_LEAST_ONCE, val.qos);
ASSERT_UINT_EQUALS(packet_id, received_packet->packet_identifier);
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 2);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 3);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 4);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 5);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_DISCONNECT, received_packet->type);
/* Only two packets should be recorded by the published_messages, but all the three packets will be recorded by
* any_published_messages */
ASSERT_UINT_EQUALS(2, aws_array_list_length(&state_test_data->published_messages));
ASSERT_UINT_EQUALS(3, aws_array_list_length(&state_test_data->any_published_messages));
struct received_publish_packet *publish_msg = NULL;
ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->published_messages, (void **)&publish_msg, 0));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&sub_topic_1, &publish_msg->topic));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_1, &publish_msg->payload));
ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->published_messages, (void **)&publish_msg, 1));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&sub_topic_2, &publish_msg->topic));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_2, &publish_msg->payload));
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connect_subscribe_multi,
s_setup_mqtt_server_fn,
s_test_mqtt_subscribe_multi_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/* Subscribe to multiple topics prior to connection, make a CONNECT, have the server send PUBLISH messages, unsubscribe
* to a topic, have the server send PUBLISH messages again, make sure the unsubscribed topic callback will not be fired
*/
static int s_test_mqtt_unsubscribe_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = false,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
};
struct aws_byte_cursor sub_topic_1 = aws_byte_cursor_from_c_str("/test/topic1");
struct aws_byte_cursor sub_topic_2 = aws_byte_cursor_from_c_str("/test/topic2");
struct aws_mqtt_topic_subscription sub1 = {
.topic = sub_topic_1,
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE,
.on_publish = s_on_publish_received,
.on_cleanup = NULL,
.on_publish_ud = state_test_data,
};
struct aws_mqtt_topic_subscription sub2 = {
.topic = sub_topic_2,
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE,
.on_publish = s_on_publish_received,
.on_cleanup = NULL,
.on_publish_ud = state_test_data,
};
struct aws_array_list topic_filters;
size_t list_len = 2;
AWS_VARIABLE_LENGTH_ARRAY(uint8_t, static_buf, list_len * sizeof(struct aws_mqtt_topic_subscription));
aws_array_list_init_static(&topic_filters, static_buf, list_len, sizeof(struct aws_mqtt_topic_subscription));
aws_array_list_push_back(&topic_filters, &sub1);
aws_array_list_push_back(&topic_filters, &sub2);
uint16_t sub_packet_id = aws_mqtt_client_connection_subscribe_multiple(
state_test_data->mqtt_connection, &topic_filters, s_on_multi_suback, state_test_data);
ASSERT_TRUE(sub_packet_id > 0);
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
s_wait_for_subscribe_to_complete(state_test_data);
state_test_data->expected_any_publishes = 2;
struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
ASSERT_SUCCESS(mqtt_mock_server_send_publish(
state_test_data->mock_server,
&sub_topic_1,
&payload_1,
false /*dup*/,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false /*retain*/));
struct aws_byte_cursor payload_2 = aws_byte_cursor_from_c_str("Test Message 2");
ASSERT_SUCCESS(mqtt_mock_server_send_publish(
state_test_data->mock_server,
&sub_topic_2,
&payload_2,
false /*dup*/,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false /*retain*/));
s_wait_for_any_publish(state_test_data);
mqtt_mock_server_wait_for_pubacks(state_test_data->mock_server, 2);
aws_mutex_lock(&state_test_data->lock);
state_test_data->expected_ops_completed = 1;
aws_mutex_unlock(&state_test_data->lock);
/* unsubscribe to the first topic */
uint16_t unsub_packet_id = aws_mqtt_client_connection_unsubscribe(
state_test_data->mqtt_connection, &sub_topic_1, s_on_op_complete, state_test_data);
ASSERT_TRUE(unsub_packet_id > 0);
/* Even when the UNSUBACK has not received, the client will not invoke the on_pub callback for that topic */
ASSERT_SUCCESS(mqtt_mock_server_send_publish(
state_test_data->mock_server,
&sub_topic_1,
&payload_1,
false /*dup*/,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false /*retain*/));
ASSERT_SUCCESS(mqtt_mock_server_send_publish(
state_test_data->mock_server,
&sub_topic_2,
&payload_2,
false /*dup*/,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false /*retain*/));
state_test_data->expected_any_publishes = 2;
s_wait_for_any_publish(state_test_data);
mqtt_mock_server_wait_for_pubacks(state_test_data->mock_server, 2);
s_wait_for_ops_completed(state_test_data);
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
/* Decode all received packets by mock server */
ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
struct mqtt_decoded_packet *received_packet =
mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_SUBSCRIBE, received_packet->type);
ASSERT_UINT_EQUALS(2, aws_array_list_length(&received_packet->sub_topic_filters));
struct aws_mqtt_subscription val;
ASSERT_SUCCESS(aws_array_list_front(&received_packet->sub_topic_filters, &val));
ASSERT_TRUE(aws_byte_cursor_eq(&val.topic_filter, &sub_topic_1));
ASSERT_UINT_EQUALS(AWS_MQTT_QOS_AT_LEAST_ONCE, val.qos);
ASSERT_SUCCESS(aws_array_list_back(&received_packet->sub_topic_filters, &val));
ASSERT_TRUE(aws_byte_cursor_eq(&val.topic_filter, &sub_topic_2));
ASSERT_UINT_EQUALS(AWS_MQTT_QOS_AT_LEAST_ONCE, val.qos);
ASSERT_UINT_EQUALS(sub_packet_id, received_packet->packet_identifier);
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 2);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 3);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 4);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_UNSUBSCRIBE, received_packet->type);
ASSERT_UINT_EQUALS(1, aws_array_list_length(&received_packet->unsub_topic_filters));
struct aws_byte_cursor val_cur;
ASSERT_SUCCESS(aws_array_list_front(&received_packet->unsub_topic_filters, &val_cur));
ASSERT_TRUE(aws_byte_cursor_eq(&val_cur, &sub_topic_1));
ASSERT_UINT_EQUALS(unsub_packet_id, received_packet->packet_identifier);
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 5);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 6);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBACK, received_packet->type);
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 7);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_DISCONNECT, received_packet->type);
/* Only three packets should be recorded by the published_messages, but all the four packets will be recorded by
* any_published_messages */
ASSERT_UINT_EQUALS(3, aws_array_list_length(&state_test_data->published_messages));
ASSERT_UINT_EQUALS(4, aws_array_list_length(&state_test_data->any_published_messages));
struct received_publish_packet *publish_msg = NULL;
ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->published_messages, (void **)&publish_msg, 0));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&sub_topic_1, &publish_msg->topic));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_1, &publish_msg->payload));
ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->published_messages, (void **)&publish_msg, 1));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&sub_topic_2, &publish_msg->topic));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_2, &publish_msg->payload));
ASSERT_SUCCESS(aws_array_list_get_at_ptr(&state_test_data->published_messages, (void **)&publish_msg, 2));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&sub_topic_2, &publish_msg->topic));
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&payload_2, &publish_msg->payload));
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connect_unsubscribe,
s_setup_mqtt_server_fn,
s_test_mqtt_unsubscribe_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/**
* Subscribe to multiple topics prior to connection, make a CONNECT, have the server send PUBLISH messages, unsubscribe
* to a topic, disconnect with the broker, make a connection with clean_session true, then call resubscribe, client will
* successfully resubscribe to the old topics.
*/
static int s_test_mqtt_resubscribe_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = false,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
};
struct aws_byte_cursor sub_topic_1 = aws_byte_cursor_from_c_str("/test/topic1");
struct aws_byte_cursor sub_topic_2 = aws_byte_cursor_from_c_str("/test/topic2");
struct aws_byte_cursor sub_topic_3 = aws_byte_cursor_from_c_str("/test/topic3");
struct aws_mqtt_topic_subscription sub1 = {
.topic = sub_topic_1,
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE,
.on_publish = s_on_publish_received,
.on_cleanup = NULL,
.on_publish_ud = state_test_data,
};
struct aws_mqtt_topic_subscription sub2 = {
.topic = sub_topic_2,
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE,
.on_publish = s_on_publish_received,
.on_cleanup = NULL,
.on_publish_ud = state_test_data,
};
struct aws_mqtt_topic_subscription sub3 = {
.topic = sub_topic_3,
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE,
.on_publish = s_on_publish_received,
.on_cleanup = NULL,
.on_publish_ud = state_test_data,
};
struct aws_array_list topic_filters;
size_t list_len = 3;
AWS_VARIABLE_LENGTH_ARRAY(uint8_t, static_buf, list_len * sizeof(struct aws_mqtt_topic_subscription));
aws_array_list_init_static(&topic_filters, static_buf, list_len, sizeof(struct aws_mqtt_topic_subscription));
aws_array_list_push_back(&topic_filters, &sub1);
aws_array_list_push_back(&topic_filters, &sub2);
aws_array_list_push_back(&topic_filters, &sub3);
uint16_t sub_packet_id = aws_mqtt_client_connection_subscribe_multiple(
state_test_data->mqtt_connection, &topic_filters, s_on_multi_suback, state_test_data);
ASSERT_TRUE(sub_packet_id > 0);
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
s_wait_for_subscribe_to_complete(state_test_data);
aws_mutex_lock(&state_test_data->lock);
state_test_data->expected_ops_completed = 1;
aws_mutex_unlock(&state_test_data->lock);
/* unsubscribe to the first topic */
uint16_t unsub_packet_id = aws_mqtt_client_connection_unsubscribe(
state_test_data->mqtt_connection, &sub_topic_1, s_on_op_complete, state_test_data);
ASSERT_TRUE(unsub_packet_id > 0);
s_wait_for_ops_completed(state_test_data);
/* client still subscribes to topic_2 & topic_3 */
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
/* reconnection to the same server */
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
/* Get all the packets out of the way */
ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
size_t packets_count = mqtt_mock_server_decoded_packets_count(state_test_data->mock_server);
struct mqtt_decoded_packet *t_received_packet =
mqtt_mock_server_get_latest_decoded_packet(state_test_data->mock_server);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, t_received_packet->type);
/* resubscribes to topic_2 & topic_3 */
uint16_t resub_packet_id =
aws_mqtt_resubscribe_existing_topics(state_test_data->mqtt_connection, s_on_multi_suback, state_test_data);
ASSERT_TRUE(resub_packet_id > 0);
s_wait_for_subscribe_to_complete(state_test_data);
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
struct mqtt_decoded_packet *received_packet =
mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, packets_count);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_SUBSCRIBE, received_packet->type);
ASSERT_UINT_EQUALS(2, aws_array_list_length(&received_packet->sub_topic_filters));
struct aws_mqtt_subscription val;
ASSERT_SUCCESS(aws_array_list_front(&received_packet->sub_topic_filters, &val));
ASSERT_TRUE(aws_byte_cursor_eq(&val.topic_filter, &sub_topic_3));
ASSERT_UINT_EQUALS(AWS_MQTT_QOS_AT_LEAST_ONCE, val.qos);
ASSERT_SUCCESS(aws_array_list_back(&received_packet->sub_topic_filters, &val));
ASSERT_TRUE(aws_byte_cursor_eq(&val.topic_filter, &sub_topic_2));
ASSERT_UINT_EQUALS(AWS_MQTT_QOS_AT_LEAST_ONCE, val.qos);
ASSERT_UINT_EQUALS(resub_packet_id, received_packet->packet_identifier);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connect_resubscribe,
s_setup_mqtt_server_fn,
s_test_mqtt_resubscribe_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/* Make a CONNECT, PUBLISH to a topic, make sure server received, then send a DISCONNECT. */
static int s_test_mqtt_publish_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = false,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
};
struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
struct aws_byte_cursor payload_2 = aws_byte_cursor_from_c_str("Test Message 2");
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
aws_mutex_lock(&state_test_data->lock);
state_test_data->expected_ops_completed = 2;
aws_mutex_unlock(&state_test_data->lock);
uint16_t packet_id_1 = aws_mqtt_client_connection_publish(
state_test_data->mqtt_connection,
&pub_topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false,
&payload_1,
s_on_op_complete,
state_test_data);
ASSERT_TRUE(packet_id_1 > 0);
uint16_t packet_id_2 = aws_mqtt_client_connection_publish(
state_test_data->mqtt_connection,
&pub_topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false,
&payload_2,
s_on_op_complete,
state_test_data);
ASSERT_TRUE(packet_id_2 > 0);
s_wait_for_ops_completed(state_test_data);
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
/* Decode all received packets by mock server */
ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
ASSERT_UINT_EQUALS(4, mqtt_mock_server_decoded_packets_count(state_test_data->mock_server));
struct mqtt_decoded_packet *received_packet =
mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBLISH, received_packet->type);
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->topic_name, &pub_topic));
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->publish_payload, &payload_1));
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 2);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBLISH, received_packet->type);
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->topic_name, &pub_topic));
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->publish_payload, &payload_2));
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 3);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_DISCONNECT, received_packet->type);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connect_publish,
s_setup_mqtt_server_fn,
s_test_mqtt_publish_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/* Make a CONNECT, PUBLISH to a topic, free the payload before the publish completes to make sure it's safe */
static int s_test_mqtt_publish_payload_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = false,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
};
struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
struct aws_byte_buf buf_payload;
struct aws_byte_cursor ori_payload = aws_byte_cursor_from_c_str("Test Message 1");
ASSERT_SUCCESS(aws_byte_buf_init_copy_from_cursor(&buf_payload, allocator, ori_payload));
struct aws_byte_cursor payload_curser = aws_byte_cursor_from_buf(&buf_payload);
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
aws_mutex_lock(&state_test_data->lock);
state_test_data->expected_ops_completed = 1;
aws_mutex_unlock(&state_test_data->lock);
uint16_t packet_id = aws_mqtt_client_connection_publish(
state_test_data->mqtt_connection,
&pub_topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false,
&payload_curser,
s_on_op_complete,
state_test_data);
ASSERT_TRUE(packet_id > 0);
/* clean up the payload buf, as user don't need to manage the buf and keep it valid until publish completes */
aws_byte_buf_clean_up(&buf_payload);
s_wait_for_ops_completed(state_test_data);
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
/* Decode all received packets by mock server */
ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
ASSERT_UINT_EQUALS(3, mqtt_mock_server_decoded_packets_count(state_test_data->mock_server));
struct mqtt_decoded_packet *received_packet =
mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBLISH, received_packet->type);
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->topic_name, &pub_topic));
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->publish_payload, &ori_payload));
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 2);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_DISCONNECT, received_packet->type);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connect_publish_payload,
s_setup_mqtt_server_fn,
s_test_mqtt_publish_payload_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/**
* CONNECT, force the server to hang up after a successful connection and block all CONNACKS, send PUBLISH messages
* let the server send CONNACKS, make sure when the client reconnects automatically, it sends the PUBLISH messages
* that were sent during offline mode. Then send a DISCONNECT.
*/
static int s_test_mqtt_connection_offline_publish_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = false,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
.ping_timeout_ms = 10,
.keep_alive_time_secs = 1,
};
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
mqtt_mock_server_set_max_connack(state_test_data->mock_server, 0);
/* shut it down and make sure the client automatically reconnects.*/
aws_channel_shutdown(state_test_data->server_channel, AWS_OP_SUCCESS);
s_wait_for_interrupt_to_complete(state_test_data);
state_test_data->server_disconnect_completed = false;
struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
struct aws_byte_cursor payload_2 = aws_byte_cursor_from_c_str("Test Message 2");
aws_mutex_lock(&state_test_data->lock);
state_test_data->expected_ops_completed = 2;
aws_mutex_unlock(&state_test_data->lock);
ASSERT_TRUE(
aws_mqtt_client_connection_publish(
state_test_data->mqtt_connection,
&pub_topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false,
&payload_1,
s_on_op_complete,
state_test_data) > 0);
ASSERT_TRUE(
aws_mqtt_client_connection_publish(
state_test_data->mqtt_connection,
&pub_topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false,
&payload_2,
s_on_op_complete,
state_test_data) > 0);
aws_mutex_lock(&state_test_data->lock);
ASSERT_FALSE(state_test_data->connection_resumed);
aws_mutex_unlock(&state_test_data->lock);
mqtt_mock_server_set_max_connack(state_test_data->mock_server, SIZE_MAX);
s_wait_for_ops_completed(state_test_data);
aws_mutex_lock(&state_test_data->lock);
ASSERT_TRUE(state_test_data->connection_resumed);
aws_mutex_unlock(&state_test_data->lock);
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
/* Decode all received packets by mock server */
ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
size_t packets_count = mqtt_mock_server_decoded_packets_count(state_test_data->mock_server);
ASSERT_TRUE(packets_count >= 5 && packets_count <= 6);
struct mqtt_decoded_packet *received_packet =
mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 0);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, 1);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
/* if message count is 6 there was an extra connect message due to the automatic reconnect behavior and timing. */
size_t index = 2;
if (packets_count == 6) {
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, index++);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_CONNECT, received_packet->type);
ASSERT_UINT_EQUALS(connection_options.clean_session, received_packet->clean_session);
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->client_identifier, &connection_options.client_id));
}
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, index++);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBLISH, received_packet->type);
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->topic_name, &pub_topic));
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->publish_payload, &payload_1));
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, index++);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_PUBLISH, received_packet->type);
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->topic_name, &pub_topic));
ASSERT_TRUE(aws_byte_cursor_eq(&received_packet->publish_payload, &payload_2));
received_packet = mqtt_mock_server_get_decoded_packet_by_index(state_test_data->mock_server, index++);
ASSERT_UINT_EQUALS(AWS_MQTT_PACKET_DISCONNECT, received_packet->type);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connection_offline_publish,
s_setup_mqtt_server_fn,
s_test_mqtt_connection_offline_publish_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/**
* CONNECT, force the server to hang up after a successful connection and block all CONNACKS, DISCONNECT while client is
* reconnecting. Resource and pending requests are cleaned up correctly
*/
static int s_test_mqtt_connection_disconnect_while_reconnecting(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = false,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
.ping_timeout_ms = 10,
.keep_alive_time_secs = 1,
};
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
mqtt_mock_server_set_max_connack(state_test_data->mock_server, 0);
/* shut it down and the client automatically reconnects.*/
aws_channel_shutdown(state_test_data->server_channel, AWS_OP_SUCCESS);
s_wait_for_interrupt_to_complete(state_test_data);
struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
struct aws_byte_cursor payload_2 = aws_byte_cursor_from_c_str("Test Message 2");
aws_mutex_lock(&state_test_data->lock);
state_test_data->expected_ops_completed = 2;
aws_mutex_unlock(&state_test_data->lock);
ASSERT_TRUE(
aws_mqtt_client_connection_publish(
state_test_data->mqtt_connection,
&pub_topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false,
&payload_1,
s_on_op_complete,
state_test_data) > 0);
ASSERT_TRUE(
aws_mqtt_client_connection_publish(
state_test_data->mqtt_connection,
&pub_topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false,
&payload_2,
s_on_op_complete,
state_test_data) > 0);
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
aws_mqtt_client_connection_release(state_test_data->mqtt_connection);
state_test_data->mqtt_connection = NULL;
s_wait_for_ops_completed(state_test_data);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connection_disconnect_while_reconnecting,
s_setup_mqtt_server_fn,
s_test_mqtt_connection_disconnect_while_reconnecting,
s_clean_up_mqtt_server_fn,
&test_data)
/**
* Regression test: Once upon a time there was a bug caused by race condition on the state of connection.
* The scenario is the server/broker closes the connection, while the client is making requests.
* The race condition between the eventloop thread closes the connection and the main thread makes request could cause a
* bug.
* Solution: put a lock for the state of connection, protect it from accessing by multiple threads at the same time.
*/
static int s_test_mqtt_connection_closes_while_making_requests_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = false,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
.ping_timeout_ms = 10,
.keep_alive_time_secs = 1,
};
struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
aws_mutex_lock(&state_test_data->lock);
state_test_data->expected_ops_completed = 1;
aws_mutex_unlock(&state_test_data->lock);
/* shutdown the channel for some error */
aws_channel_shutdown(state_test_data->server_channel, AWS_ERROR_INVALID_STATE);
/* While the shutdown is still in process, making a publish request */
/* It may not 100% trigger the crash, the crash only happens when the s_mqtt_client_shutdown and mqtt_create_request
* happen at the same time. Like the slot is removed by shutdown, and the create request still think the slot is
* there and try to access it. Crash happens. It's not possible to trigger the crash 100% without changing the
* implementation. */
uint16_t packet_id_1 = aws_mqtt_client_connection_publish(
state_test_data->mqtt_connection,
&pub_topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false,
&payload_1,
s_on_op_complete,
state_test_data);
ASSERT_TRUE(packet_id_1 > 0);
s_wait_for_reconnect_to_complete(state_test_data);
s_wait_for_ops_completed(state_test_data);
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connection_closes_while_making_requests,
s_setup_mqtt_server_fn,
s_test_mqtt_connection_closes_while_making_requests_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/* helper to make sure ID 1 is received earlier than ID 2 and ID 2 is earlier than ID 3 */
static int s_check_packets_received_order(
struct aws_channel_handler *handler,
size_t search_start_idx,
uint16_t packet_id_1,
uint16_t packet_id_2,
uint16_t packet_id_3) {
ASSERT_SUCCESS(mqtt_mock_server_decode_packets(handler));
size_t packet_idx_1 = 0;
size_t packet_idx_2 = 0;
size_t packet_idx_3 = 0;
ASSERT_NOT_NULL(mqtt_mock_server_find_decoded_packet_by_id(handler, search_start_idx, packet_id_1, &packet_idx_1));
ASSERT_NOT_NULL(mqtt_mock_server_find_decoded_packet_by_id(handler, search_start_idx, packet_id_2, &packet_idx_2));
ASSERT_NOT_NULL(mqtt_mock_server_find_decoded_packet_by_id(handler, search_start_idx, packet_id_3, &packet_idx_3));
ASSERT_TRUE(packet_idx_3 > packet_idx_2 && packet_idx_2 > packet_idx_1);
return AWS_OP_SUCCESS;
}
/**
* Test that when the response is not back from the server, and the connection lost, the sent packets will be retried
* in the same order as they are sent before.
*/
static int s_test_mqtt_connection_resend_packets_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = false,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
.ping_timeout_ms = 10,
.keep_alive_time_secs = 1,
};
struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
struct aws_byte_cursor payload_2 = aws_byte_cursor_from_c_str("Test Message 2");
struct aws_byte_cursor payload_3 = aws_byte_cursor_from_c_str("Test Message 3");
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
/* Disable the auto ACK packets sent by the server, which blocks the requests to complete */
mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
uint16_t packet_id_1 = aws_mqtt_client_connection_publish(
state_test_data->mqtt_connection, &pub_topic, AWS_MQTT_QOS_AT_LEAST_ONCE, false, &payload_1, NULL, NULL);
ASSERT_TRUE(packet_id_1 > 0);
uint16_t packet_id_2 = aws_mqtt_client_connection_publish(
state_test_data->mqtt_connection, &pub_topic, AWS_MQTT_QOS_AT_LEAST_ONCE, false, &payload_2, NULL, NULL);
ASSERT_TRUE(packet_id_2 > 0);
uint16_t packet_id_3 = aws_mqtt_client_connection_publish(
state_test_data->mqtt_connection, &pub_topic, AWS_MQTT_QOS_AT_LEAST_ONCE, false, &payload_3, NULL, NULL);
ASSERT_TRUE(packet_id_3 > 0);
/* Wait for 1 sec. ensure all the publishes have been received by the server */
aws_thread_current_sleep(ONE_SEC);
ASSERT_SUCCESS(
s_check_packets_received_order(state_test_data->mock_server, 0, packet_id_1, packet_id_2, packet_id_3));
size_t packet_count = mqtt_mock_server_decoded_packets_count(state_test_data->mock_server);
/* shutdown the channel for some error */
aws_channel_shutdown(state_test_data->server_channel, AWS_ERROR_INVALID_STATE);
s_wait_for_reconnect_to_complete(state_test_data);
/* Wait again, and ensure the publishes have been resent */
aws_thread_current_sleep(ONE_SEC);
ASSERT_SUCCESS(s_check_packets_received_order(
state_test_data->mock_server, packet_count, packet_id_1, packet_id_2, packet_id_3));
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connection_resend_packets,
s_setup_mqtt_server_fn,
s_test_mqtt_connection_resend_packets_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/**
* Test that connection lost before the publish QoS 0 ever sent, publish QoS 0 will not be retried.
*/
static int s_test_mqtt_connection_not_retry_publish_QoS_0_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = true,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
.ping_timeout_ms = 10,
.keep_alive_time_secs = 16960, /* basically stop automatically sending PINGREQ */
};
struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
/* kill the connection */
aws_channel_shutdown(state_test_data->server_channel, AWS_ERROR_INVALID_STATE);
/* TODO: only one eventloop thread in the el group. Most likely the test will fail, the outgoing task will not be
* cancelled because of connection lost */
/* make a publish with QoS 0 immediate. */
aws_mutex_lock(&state_test_data->lock);
state_test_data->expected_ops_completed = 1;
aws_mutex_unlock(&state_test_data->lock);
uint16_t packet_id_1 = aws_mqtt_client_connection_publish(
state_test_data->mqtt_connection,
&pub_topic,
AWS_MQTT_QOS_AT_MOST_ONCE,
false,
&payload_1,
s_on_op_complete,
state_test_data);
ASSERT_TRUE(packet_id_1 > 0);
/* publish should complete after the shutdown */
s_wait_for_ops_completed(state_test_data);
/* wait for reconnect */
s_wait_for_reconnect_to_complete(state_test_data);
/* Check all received packets, no publish packets ever received by the server. Because the connection lost before it
* ever get sent. */
ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
ASSERT_NULL(
mqtt_mock_server_find_decoded_packet_by_type(state_test_data->mock_server, 0, AWS_MQTT_PACKET_PUBLISH, NULL));
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connection_not_retry_publish_QoS_0,
s_setup_mqtt_server_fn,
s_test_mqtt_connection_not_retry_publish_QoS_0_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/**
* Test that the retry policy is consistent, which means either the request has been sent or not, when the connection
* lost/resume, the request will be retried.
*/
static int s_test_mqtt_connection_consistent_retry_policy_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = false,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
.ping_timeout_ms = 10,
.protocol_operation_timeout_ms = 3000,
.keep_alive_time_secs = 16960, /* basically stop automatically sending PINGREQ */
};
struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
struct aws_byte_cursor sub_topic = aws_byte_cursor_from_c_str("/test/topic");
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
struct aws_channel_handler *handler = state_test_data->mock_server;
/* Disable the auto ACK packets sent by the server, which blocks the requests to complete */
mqtt_mock_server_disable_auto_ack(handler);
/* kill the connection */
aws_channel_shutdown(state_test_data->server_channel, AWS_ERROR_INVALID_STATE);
/* There is a hidden race condition between channel shutdown from eventloop and publish/subscribe from main thread,
* but either way, it should work as they are retried in the end. */
/* make a publish with QoS 1 immediate. */
uint16_t packet_id_1 = aws_mqtt_client_connection_publish(
state_test_data->mqtt_connection,
&pub_topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false,
&payload_1,
s_on_op_complete,
state_test_data);
ASSERT_TRUE(packet_id_1 > 0);
/* make another subscribe */
uint16_t packet_id_2 = aws_mqtt_client_connection_subscribe(
state_test_data->mqtt_connection,
&sub_topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
NULL,
NULL,
NULL,
s_on_suback,
state_test_data);
ASSERT_TRUE(packet_id_2 > 0);
/* wait for reconnect */
s_wait_for_reconnect_to_complete(state_test_data);
/* Wait for 1 sec. ensure all the requests have been received by the server */
aws_thread_current_sleep(ONE_SEC);
/* Check all received packets, subscribe and publish has been received */
ASSERT_SUCCESS(mqtt_mock_server_decode_packets(handler));
size_t packet_count = mqtt_mock_server_decoded_packets_count(handler);
/* the latest packet should be subscribe */
ASSERT_NOT_NULL(mqtt_mock_server_find_decoded_packet_by_type(handler, 0, AWS_MQTT_PACKET_SUBSCRIBE, NULL));
ASSERT_NOT_NULL(mqtt_mock_server_find_decoded_packet_by_type(handler, 0, AWS_MQTT_PACKET_PUBLISH, NULL));
/* Re-enable the auto ack to finish the requests */
mqtt_mock_server_enable_auto_ack(handler);
/* Kill the connection again, the requests will be retried since the response has not been received yet. */
aws_channel_shutdown(state_test_data->server_channel, AWS_ERROR_INVALID_STATE);
s_wait_for_reconnect_to_complete(state_test_data);
/* subscribe should be able to completed now */
s_wait_for_subscribe_to_complete(state_test_data);
/* Check all received packets, subscribe and publish has been resent */
ASSERT_SUCCESS(mqtt_mock_server_decode_packets(handler));
ASSERT_TRUE(mqtt_mock_server_decoded_packets_count(handler) > packet_count);
/* the latest packet should be subscribe */
ASSERT_NOT_NULL(
mqtt_mock_server_find_decoded_packet_by_type(handler, packet_count, AWS_MQTT_PACKET_SUBSCRIBE, NULL));
ASSERT_NOT_NULL(mqtt_mock_server_find_decoded_packet_by_type(handler, packet_count, AWS_MQTT_PACKET_PUBLISH, NULL));
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connection_consistent_retry_policy,
s_setup_mqtt_server_fn,
s_test_mqtt_connection_consistent_retry_policy_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/**
* Test that the request will not be retried even the response has not received for a while.
*/
static int s_test_mqtt_connection_not_resend_packets_on_healthy_connection_fn(
struct aws_allocator *allocator,
void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = true,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
.ping_timeout_ms = 10,
.keep_alive_time_secs = 1,
};
struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
struct aws_byte_cursor sub_topic = aws_byte_cursor_from_c_str("/test/topic");
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
struct aws_channel_handler *handler = state_test_data->mock_server;
/* Disable the auto ACK packets sent by the server, which blocks the requests to complete */
mqtt_mock_server_disable_auto_ack(handler);
/* make a publish with QoS 1 */
aws_mutex_lock(&state_test_data->lock);
state_test_data->expected_ops_completed = 1;
aws_mutex_unlock(&state_test_data->lock);
uint16_t packet_id_1 = aws_mqtt_client_connection_publish(
state_test_data->mqtt_connection,
&pub_topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false,
&payload_1,
s_on_op_complete,
state_test_data);
ASSERT_TRUE(packet_id_1 > 0);
/* make another subscribe */
uint16_t packet_id_2 = aws_mqtt_client_connection_subscribe(
state_test_data->mqtt_connection,
&sub_topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
NULL,
NULL,
NULL,
s_on_suback,
state_test_data);
ASSERT_TRUE(packet_id_2 > 0);
/* Wait for 3 sec. ensure no duplicate requests will be sent */
aws_thread_current_sleep((uint64_t)ONE_SEC * 3);
/* Check all received packets, only one publish and subscribe received */
ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
size_t pre_index = SIZE_MAX;
ASSERT_NOT_NULL(mqtt_mock_server_find_decoded_packet_by_type(handler, 0, AWS_MQTT_PACKET_PUBLISH, &pre_index));
if (pre_index + 1 < mqtt_mock_server_decoded_packets_count(handler)) {
/* If it's not the last packet, search again, and result should be NULL. */
ASSERT_NULL(
mqtt_mock_server_find_decoded_packet_by_type(handler, pre_index + 1, AWS_MQTT_PACKET_PUBLISH, NULL));
}
ASSERT_NOT_NULL(mqtt_mock_server_find_decoded_packet_by_type(handler, 0, AWS_MQTT_PACKET_SUBSCRIBE, &pre_index));
if (pre_index + 1 < mqtt_mock_server_decoded_packets_count(handler)) {
ASSERT_NULL(
mqtt_mock_server_find_decoded_packet_by_type(handler, pre_index + 1, AWS_MQTT_PACKET_SUBSCRIBE, NULL));
}
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connection_not_resend_packets_on_healthy_connection,
s_setup_mqtt_server_fn,
s_test_mqtt_connection_not_resend_packets_on_healthy_connection_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/* Make requests during offline, and destory the connection before ever online, the resource should be cleaned up
* properly */
static int s_test_mqtt_connection_destory_pending_requests_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("/test/topic");
struct aws_byte_cursor payload = aws_byte_cursor_from_c_str("Test Message 1");
ASSERT_TRUE(
aws_mqtt_client_connection_publish(
state_test_data->mqtt_connection,
&topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false,
&payload,
s_on_op_complete,
state_test_data) > 0);
ASSERT_TRUE(
aws_mqtt_client_connection_subscribe(
state_test_data->mqtt_connection,
&topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
s_on_publish_received,
state_test_data,
NULL,
s_on_suback,
state_test_data) > 0);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connection_destory_pending_requests,
s_setup_mqtt_server_fn,
s_test_mqtt_connection_destory_pending_requests_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/* Make a clean session connection, all the request will not be retried when the connection lost */
static int s_test_mqtt_clean_session_not_retry_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = true, /* make a clean_session connection */
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
.ping_timeout_ms = 10,
.keep_alive_time_secs = 1,
};
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
struct aws_channel_handler *handler = state_test_data->mock_server;
mqtt_mock_server_disable_auto_ack(handler);
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("/test/topic");
struct aws_byte_cursor payload = aws_byte_cursor_from_c_str("Test Message 1");
ASSERT_TRUE(
aws_mqtt_client_connection_publish(
state_test_data->mqtt_connection,
&topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false,
&payload,
s_on_op_complete,
state_test_data) > 0);
ASSERT_TRUE(
aws_mqtt_client_connection_subscribe(
state_test_data->mqtt_connection,
&topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
s_on_publish_received,
state_test_data,
NULL,
s_on_suback,
state_test_data) > 0);
aws_mutex_lock(&state_test_data->lock);
state_test_data->expected_ops_completed = 1;
aws_mutex_unlock(&state_test_data->lock);
/* Shutdown the connection */
aws_channel_shutdown(state_test_data->server_channel, AWS_OP_SUCCESS);
s_wait_for_interrupt_to_complete(state_test_data);
/* Once the connection lost, the requests will fail */
ASSERT_UINT_EQUALS(state_test_data->op_complete_error, AWS_ERROR_MQTT_CANCELLED_FOR_CLEAN_SESSION);
ASSERT_UINT_EQUALS(state_test_data->subscribe_complete_error, AWS_ERROR_MQTT_CANCELLED_FOR_CLEAN_SESSION);
/* Disconnect */
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_clean_session_not_retry,
s_setup_mqtt_server_fn,
s_test_mqtt_clean_session_not_retry_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/* Make a clean session connection, the previous session will be discard when a new connection with clean session true
* is created */
static int s_test_mqtt_clean_session_discard_previous_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = true, /* make a clean_session connection */
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
.ping_timeout_ms = 10,
.keep_alive_time_secs = 16960, /* basically stop automatically sending PINGREQ */
};
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("/test/topic");
struct aws_byte_cursor payload = aws_byte_cursor_from_c_str("Test Message 1");
aws_mutex_lock(&state_test_data->lock);
state_test_data->expected_ops_completed = 1;
aws_mutex_unlock(&state_test_data->lock);
/* Requests made now will be considered as the previous session. */
ASSERT_TRUE(
aws_mqtt_client_connection_publish(
state_test_data->mqtt_connection,
&topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false,
&payload,
s_on_op_complete,
state_test_data) > 0);
ASSERT_TRUE(
aws_mqtt_client_connection_subscribe(
state_test_data->mqtt_connection,
&topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
s_on_publish_received,
state_test_data,
NULL,
s_on_suback,
state_test_data) > 0);
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
struct aws_channel_handler *handler = state_test_data->mock_server;
s_wait_for_ops_completed(state_test_data);
s_wait_for_subscribe_to_complete(state_test_data);
ASSERT_UINT_EQUALS(state_test_data->op_complete_error, AWS_ERROR_MQTT_CANCELLED_FOR_CLEAN_SESSION);
ASSERT_UINT_EQUALS(state_test_data->subscribe_complete_error, AWS_ERROR_MQTT_CANCELLED_FOR_CLEAN_SESSION);
/* Check no request is received by the server */
ASSERT_SUCCESS(mqtt_mock_server_decode_packets(handler));
ASSERT_NULL(mqtt_mock_server_find_decoded_packet_by_type(handler, 0, AWS_MQTT_PACKET_PUBLISH, NULL));
ASSERT_NULL(mqtt_mock_server_find_decoded_packet_by_type(handler, 0, AWS_MQTT_PACKET_SUBSCRIBE, NULL));
/* Disconnect */
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_clean_session_discard_previous,
s_setup_mqtt_server_fn,
s_test_mqtt_clean_session_discard_previous_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/* Make a clean session connection, the requests after the connect function will be considered as the next session */
static int s_test_mqtt_clean_session_keep_next_session_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = true, /* make a clean_session connection */
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
.ping_timeout_ms = 10,
.keep_alive_time_secs = 16960, /* basically stop automatically sending PINGREQ */
};
struct aws_byte_cursor topic = aws_byte_cursor_from_c_str("/test/topic");
struct aws_byte_cursor payload = aws_byte_cursor_from_c_str("Test Message 1");
aws_mutex_lock(&state_test_data->lock);
state_test_data->expected_ops_completed = 1;
aws_mutex_unlock(&state_test_data->lock);
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
/* Requests made after the connect function will be considered as the next session, and will be sent eventually */
ASSERT_TRUE(
aws_mqtt_client_connection_publish(
state_test_data->mqtt_connection,
&topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false,
&payload,
s_on_op_complete,
state_test_data) > 0);
ASSERT_TRUE(
aws_mqtt_client_connection_subscribe(
state_test_data->mqtt_connection,
&topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
s_on_publish_received,
state_test_data,
NULL,
s_on_suback,
state_test_data) > 0);
s_wait_for_connection_to_complete(state_test_data);
struct aws_channel_handler *handler = state_test_data->mock_server;
s_wait_for_ops_completed(state_test_data);
s_wait_for_subscribe_to_complete(state_test_data);
ASSERT_UINT_EQUALS(state_test_data->op_complete_error, AWS_ERROR_SUCCESS);
ASSERT_UINT_EQUALS(state_test_data->subscribe_complete_error, AWS_ERROR_SUCCESS);
/* Check no request is received by the server */
ASSERT_SUCCESS(mqtt_mock_server_decode_packets(handler));
ASSERT_NOT_NULL(mqtt_mock_server_find_decoded_packet_by_type(handler, 0, AWS_MQTT_PACKET_PUBLISH, NULL));
ASSERT_NOT_NULL(mqtt_mock_server_find_decoded_packet_by_type(handler, 0, AWS_MQTT_PACKET_SUBSCRIBE, NULL));
/* Disconnect */
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_clean_session_keep_next_session,
s_setup_mqtt_server_fn,
s_test_mqtt_clean_session_keep_next_session_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/**
* Test that connection is healthy, user set the timeout for request, and timeout happens and the publish failed.
*/
static int s_test_mqtt_connection_publish_QoS1_timeout_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = false,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
.ping_timeout_ms = 10,
.protocol_operation_timeout_ms = 3000,
.keep_alive_time_secs = 16960, /* basically stop automatically sending PINGREQ */
};
struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
/* Disable the auto ACK packets sent by the server, which blocks the requests to complete */
mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
/* make a publish with QoS 1 immediate. */
aws_mutex_lock(&state_test_data->lock);
state_test_data->expected_ops_completed = 1;
aws_mutex_unlock(&state_test_data->lock);
uint16_t packet_id_1 = aws_mqtt_client_connection_publish(
state_test_data->mqtt_connection,
&pub_topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false,
&payload_1,
s_on_op_complete,
state_test_data);
ASSERT_TRUE(packet_id_1 > 0);
/* publish should complete after the shutdown */
s_wait_for_ops_completed(state_test_data);
/* Check the publish has been completed with timeout error */
ASSERT_UINT_EQUALS(state_test_data->op_complete_error, AWS_ERROR_MQTT_TIMEOUT);
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connection_publish_QoS1_timeout,
s_setup_mqtt_server_fn,
s_test_mqtt_connection_publish_QoS1_timeout_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/**
* Test that connection is healthy, user set the timeout for request, and timeout happens and the unsubscribe failed.
*/
static int s_test_mqtt_connection_unsub_timeout_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = false,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
.ping_timeout_ms = 10,
.protocol_operation_timeout_ms = 3000,
.keep_alive_time_secs = 16960, /* basically stop automatically sending PINGREQ */
};
struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
/* Disable the auto ACK packets sent by the server, which blocks the requests to complete */
mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
aws_mutex_lock(&state_test_data->lock);
state_test_data->expected_ops_completed = 1;
aws_mutex_unlock(&state_test_data->lock);
/* unsubscribe to the first topic */
uint16_t unsub_packet_id = aws_mqtt_client_connection_unsubscribe(
state_test_data->mqtt_connection, &pub_topic, s_on_op_complete, state_test_data);
ASSERT_TRUE(unsub_packet_id > 0);
/* publish should complete after the shutdown */
s_wait_for_ops_completed(state_test_data);
/* Check the publish has been completed with timeout error */
ASSERT_UINT_EQUALS(state_test_data->op_complete_error, AWS_ERROR_MQTT_TIMEOUT);
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connection_unsub_timeout,
s_setup_mqtt_server_fn,
s_test_mqtt_connection_unsub_timeout_fn,
s_clean_up_mqtt_server_fn,
&test_data)
/**
* Test that connection is healthy, user set the timeout for request, and connection lost will reset timeout.
*/
static int s_test_mqtt_connection_publish_QoS1_timeout_connection_lost_reset_time_fn(
struct aws_allocator *allocator,
void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;
struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = false,
.client_id = aws_byte_cursor_from_c_str("client1234"),
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
.socket_options = &state_test_data->socket_options,
.on_connection_complete = s_on_connection_complete_fn,
.ping_timeout_ms = 10,
.protocol_operation_timeout_ms = 3000,
.keep_alive_time_secs = 16960, /* basically stop automatically sending PINGREQ */
};
struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message 1");
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
/* Disable the auto ACK packets sent by the server, which blocks the requests to complete */
mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
/* make a publish with QoS 1 immediate. */
aws_mutex_lock(&state_test_data->lock);
state_test_data->expected_ops_completed = 1;
aws_mutex_unlock(&state_test_data->lock);
uint16_t packet_id_1 = aws_mqtt_client_connection_publish(
state_test_data->mqtt_connection,
&pub_topic,
AWS_MQTT_QOS_AT_LEAST_ONCE,
false,
&payload_1,
s_on_op_complete,
state_test_data);
ASSERT_TRUE(packet_id_1 > 0);
/* sleep for 2 sec, close the connection */
aws_thread_current_sleep((uint64_t)ONE_SEC * 2);
/* Kill the connection, the requests will be retried and the timeout will be reset. */
aws_channel_shutdown(state_test_data->server_channel, AWS_ERROR_INVALID_STATE);
s_wait_for_reconnect_to_complete(state_test_data);
/* sleep for 2 sec again, in total the response has not received for more than 4 sec, timeout should happen if the
* lost of connection not reset the timeout */
aws_thread_current_sleep((uint64_t)ONE_SEC * 2);
/* send a puback */
ASSERT_SUCCESS(mqtt_mock_server_send_puback(state_test_data->mock_server, packet_id_1));
/* publish should complete after the shutdown */
s_wait_for_ops_completed(state_test_data);
/* Check the publish has been completed successfully since the lost of the connection reset the timeout */
ASSERT_UINT_EQUALS(state_test_data->op_complete_error, AWS_ERROR_SUCCESS);
ASSERT_SUCCESS(
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data));
s_wait_for_disconnect_to_complete(state_test_data);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
mqtt_connection_publish_QoS1_timeout_connection_lost_reset_time,
s_setup_mqtt_server_fn,
s_test_mqtt_connection_publish_QoS1_timeout_connection_lost_reset_time_fn,
s_clean_up_mqtt_server_fn,
&test_data)
|