1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 #include <stdio.h>
12 #include <stdarg.h>
13 #include <stddef.h>
14 #include <setjmp.h>
15 #include <cmocka.h>  /* macros: https://api.cmocka.org/group__cmocka__asserts.html */
16 
17 #include "nx_api.h"
18 #include "nx_azure_iot_provisioning_client.h"
19 #include "nx_azure_iot_cert.h"
20 #include "nx_azure_iot_ciphersuites.h"
21 
22 
23 #define DEMO_DHCP_DISABLE
24 #define DEMO_IPV4_ADDRESS                           IP_ADDRESS(192, 168, 100, 33)
25 #define DEMO_IPV4_MASK                              0xFFFFFF00UL
26 #define DEMO_GATEWAY_ADDRESS                        IP_ADDRESS(192, 168, 100, 1)
27 #define DEMO_DNS_SERVER_ADDRESS                     IP_ADDRESS(192, 168, 100, 1)
28 #define NETWORK_DRIVER                              _nx_ram_network_driver
29 
30 /* Include main.c in the test case since we need to disable DHCP in this test. */
31 #include "main.c"
32 
33 #ifndef DEMO_CLOUD_STACK_SIZE
34 #define DEMO_CLOUD_STACK_SIZE                       2048
35 #endif /* DEMO_CLOUD_STACK_SIZE */
36 
37 #ifndef DEMO_CLOUD_THREAD_PRIORITY
38 #define DEMO_CLOUD_THREAD_PRIORITY                  (4)
39 #endif /* DEMO_CLOUD_THREAD_PRIORITY */
40 
41 #ifndef MAXIMUM_PROPERTY_BUFFER
42 #define MAXIMUM_PROPERTY_BUFFER                     256
43 #endif /* MAXIMUM_PROPERTY_BUFFER */
44 
45 #ifndef TEST_DEFAULT_WAIT_TIME
46 #define TEST_DEFAULT_WAIT_TIME                      500
47 #endif /* TEST_DEFAULT_WAIT_TIME */
48 
49 #define PROVISIONING_SERVICE_REPONSE_TOPIC          "$dps/registrations/res/202/?"
50 #define PROVISIONING_SERVICE_REPONSE_200_TOPIC      "$dps/registrations/res/200/?"
51 #define PROVISIONING_SERVICE_REPONSE_BAD_TOPIC      "$dps/registrations/res/401/?"
52 #define PROVISIONING_SERVICE_REPONSE_THROTTLE_TOPIC "$dps/registrations/res/429/?"
53 
54 #define CUSTOM_PAYLOAD                              "{\"modelId\":\"pnp_model\"}"
55 
56 typedef VOID (*NX_AZURE_TEST_FN)();
57 
58 typedef struct
59 {
60     UCHAR *name;
61     ULONG name_length;
62     UCHAR *value;
63     ULONG value_length;
64 } PROPERTY;
65 
66 static NX_AZURE_IOT iot;
67 static NX_AZURE_IOT_PROVISIONING_CLIENT iot_prov_client;
68 static NX_SECURE_X509_CERT root_ca_cert;
69 static UCHAR metadata_buffer[NX_AZURE_IOT_TLS_METADATA_BUFFER_SIZE];
70 static ULONG demo_cloud_thread_stack[DEMO_CLOUD_STACK_SIZE / sizeof(ULONG)];
71 
72 static const CHAR *expected_register_topic_start = "$dps/registrations/PUT/iotdps-register/?$rid=";
73 static const CHAR *expected_query_topic_start = "$dps/registrations/GET/iotdps-get-operationstatus/?$rid=";
74 static const CHAR *expected_message = "{\"registrationId\" : \"reg_id\"}";
75 static const CHAR *expected_message_with_custom_payload = "{\"registrationId\" : \"reg_id\", \"payload\" : "CUSTOM_PAYLOAD"}";
76 static const CHAR assigned_hub_response[] = "{ \
77     \"operationId\":\"4.002305f54fc89692.b1f11200-8776-4b5d-867b-dc21c4b59c12\",\"status\":\"assigned\",\"registrationState\": \
78          {\"registrationId\":\"reg_id\",\"createdDateTimeUtc\":\"2019-12-27T19:51:41.6630592Z\",\"assignedHub\":\"test.azure-iothub.com\", \
79           \"deviceId\":\"testId\",\"status\":\"assigned\",\"substatus\":\"initialAssignment\",\"lastUpdatedDateTimeUtc\":\"2019-12-27T19:51:41.8579703Z\", \
80           \"etag\":\"XXXXXXXXXXX=\"\
81          }\
82 }";
83 static const CHAR failure_hub_response[] = "{ \
84     \"operationId\":\"4.002305f54fc89692.b1f11200-8776-4b5d-867b-dc21c4b59c12\",\"status\":\"failed\",\"registrationState\": \
85          {\"registrationId\":\"reg_id\",\"createdDateTimeUtc\":\"2019-12-27T19:51:41.6630592Z\",\
86           \"status\":\"failed\",\"errorCode\":400207,\"errorMessage\":\"Custom allocation failed with status code: 400\",\
87           \"lastUpdatedDateTimeUtc\":\"2019-12-27T19:51:41.8579703Z\", \
88           \"etag\":\"XXXXXXXXXXX=\"\
89          }\
90 }";
91 static const CHAR *assigned_hub_name = "test.azure-iothub.com";
92 static const CHAR *assigned_device_id = "testId";
93 static const CHAR assigning_hub_response[] = "{ \
94     \"operationId\":\"4.002305f54fc89692.b1f11200-8776-4b5d-867b-dc21c4b59c12\",\"status\":\"assigning\" \
95 }";
96 static const CHAR *invalid_response_data_set[] = {
97 
98     /* operationId in the response is invalid */
99     "{ \
100     \"operationId\": [],\"status\":\"assigned\",\"registrationState\": \
101          {\"registrationId\":\"reg_id\",\"createdDateTimeUtc\":\"2019-12-27T19:51:41.6630592Z\",\"assignedHub\":\"test.azure-iothub.com\", \
102           \"deviceId\":\"testId\",\"status\":\"assigned\",\"substatus\":\"initialAssignment\",\"lastUpdatedDateTimeUtc\":\"2019-12-27T19:51:41.8579703Z\", \
103           \"etag\":\"XXXXXXXXXXX=\"\
104          }\
105     }",
106 
107     /* status in the response is invalid */
108     "{ \
109     \"operationId\":\"4.002305f54fc89692.b1f11200-8776-4b5d-867b-dc21c4b59c12\",\"status\":[],\"registrationState\": \
110          {\"registrationId\":\"reg_id\",\"createdDateTimeUtc\":\"2019-12-27T19:51:41.6630592Z\",\"assignedHub\":\"test.azure-iothub.com\", \
111           \"deviceId\":\"testId\",\"status\":[],\"substatus\":\"initialAssignment\",\"lastUpdatedDateTimeUtc\":\"2019-12-27T19:51:41.8579703Z\", \
112           \"etag\":\"XXXXXXXXXXX=\"\
113          }\
114     }",
115 
116     /* invalid deviceId in the response */
117     "{ \
118     \"operationId\":\"4.002305f54fc89692.b1f11200-8776-4b5d-867b-dc21c4b59c12\",\"status\":\"assigned\",\"registrationState\": \
119          {\"registrationId\":\"reg_id\",\"createdDateTimeUtc\":\"2019-12-27T19:51:41.6630592Z\",\"assignedHub\":\"test.azure-iothub.com\", \
120           \"deviceId\":[],\"status\":\"assigned\",\"substatus\":\"initialAssignment\",\"lastUpdatedDateTimeUtc\":\"2019-12-27T19:51:41.8579703Z\", \
121           \"etag\":\"XXXXXXXXXXX=\"\
122          }\
123     }",
124 
125     /* invalid assignedHub in the response */
126     "{ \
127     \"operationId\":\"4.002305f54fc89692.b1f11200-8776-4b5d-867b-dc21c4b59c12\",\"status\":\"assigned\",\"registrationState\": \
128          {\"registrationId\":\"reg_id\",\"createdDateTimeUtc\":\"2019-12-27T19:51:41.6630592Z\",\"assignedHub\":[], \
129           \"deviceId\":\"test1\",\"status\":\"assigned\",\"substatus\":\"initialAssignment\",\"lastUpdatedDateTimeUtc\":\"2019-12-27T19:51:41.8579703Z\", \
130           \"etag\":\"XXXXXXXXXXX=\"\
131          }\
132     }",
133 
134     /* no JSON object return via dps */
135     "[\"invalid dps response json\"]",
136 
137     /* Invalid JSON */
138     "[\"invalid json\"}"
139 
140     /* Invalid JSON */
141     "\n"
142 };
143 static const CHAR throttle_hub_response[] = "{ \
144     \"errorCode\":429001, \
145     \"trackingId\":\"\",\"message\":\"Operations are being throttled for this tenant.\", \
146     \"timestampUtc\":\"2021-10-26T00:02:24.263671Z\" \
147 }";
148 static CHAR *symmetric_key = "MTIzNDU2Nzg5MGFiY2RlZmdoaWprbG1ub3BxcnN0dXY=="; /* 1234567890abcdefghijklmnopqrstuv */
149 
150 static NX_IP* g_ip_ptr;
151 static NX_PACKET_POOL* g_pool_ptr;
152 static NX_DNS* g_dns_ptr;
153 static ULONG g_available_packet;
154 
155 static CHAR g_endpoint[] = "host_name";
156 static CHAR g_id_scope[] = "id_scope";
157 static CHAR g_reg_id[] = "reg_id";
158 static CHAR g_SAS_token[] = "SAS_token";
159 static UINT g_done = 0;
160 static UINT g__wrap__nxde_mqtt_client_secure_connect_status = 0;
161 static UINT g__wrap__nxde_mqtt_client_subscribe_status = 0;
162 static UINT g__wrap__nxde_mqtt_client_disconnect_status = 0;
163 static UINT g__wrap__nxd_mqtt_client_publish_packet_send_status = 0;
164 static UINT g__wrap__nxde_dns_host_by_name_get_status = 0;
165 static UINT g_req_id = 0;
166 static UINT g_assigned_response_after = 0;
167 static UINT g_total_append = 0;
168 static UINT g_failed_append_index = 0;
169 static UINT g_total_allocation = 0;
170 static UINT g_failed_allocation_index = 0;
171 static UCHAR property_value[MAXIMUM_PROPERTY_BUFFER];
172 static UCHAR property_value2[MAXIMUM_PROPERTY_BUFFER];
173 static PROPERTY req_id_property = { "$rid", sizeof("$rid") - 1, property_value, sizeof(property_value) };
174 static PROPERTY retry_after_property = { "retry-after", sizeof("retry-after") - 1 , property_value2, sizeof(property_value2) };
175 static VOID (*test_receive_notify)(NXD_MQTT_CLIENT *client_ptr, UINT message_count) = NX_NULL;
176 static VOID (*test_connect_notify)(struct NXD_MQTT_CLIENT_STRUCT *client_ptr, UINT status, VOID *context) = NX_NULL;
177 static VOID *test_connect_notify_context = NX_NULL;
178 static VOID (*test_disconnect_notify)(struct NXD_MQTT_CLIENT_STRUCT *client_ptr) = NX_NULL;
179 static UCHAR iothub_hostname[32];
180 static UCHAR iothub_device_id[32];
181 
__wrap_nx_azure_iot_security_module_enable(NX_AZURE_IOT * nx_azure_iot_ptr)182 UINT __wrap_nx_azure_iot_security_module_enable(NX_AZURE_IOT *nx_azure_iot_ptr)
183 {
184     printf("HIJACKED: %s\n", __func__);
185     return(NX_AZURE_IOT_SUCCESS);
186 }
187 
__wrap_nx_azure_iot_security_module_disable(NX_AZURE_IOT * nx_azure_iot_ptr)188 UINT __wrap_nx_azure_iot_security_module_disable(NX_AZURE_IOT *nx_azure_iot_ptr)
189 {
190     printf("HIJACKED: %s\n", __func__);
191     return(NX_AZURE_IOT_SUCCESS);
192 }
193 
__wrap__nxde_mqtt_client_secure_connect(NXD_MQTT_CLIENT * client_ptr,NXD_ADDRESS * server_ip,UINT server_port,UINT (* tls_setup)(NXD_MQTT_CLIENT * client_ptr,NX_SECURE_TLS_SESSION *,NX_SECURE_X509_CERT *,NX_SECURE_X509_CERT *),UINT keepalive,UINT clean_session,ULONG wait_option)194 UINT __wrap__nxde_mqtt_client_secure_connect(NXD_MQTT_CLIENT *client_ptr, NXD_ADDRESS *server_ip, UINT server_port,
195                                              UINT (*tls_setup)(NXD_MQTT_CLIENT *client_ptr, NX_SECURE_TLS_SESSION *,
196                                                                NX_SECURE_X509_CERT *, NX_SECURE_X509_CERT *),
197                                              UINT keepalive, UINT clean_session, ULONG wait_option)
198 {
199 UINT status = g__wrap__nxde_mqtt_client_secure_connect_status;
200 
201     printf("HIJACKED: %s\n", __func__);
202     if (status)
203     {
204         return(status);
205     }
206 
207     tx_thread_suspend(&(iot.nx_azure_iot_ip_ptr -> nx_ip_thread));
208     client_ptr -> nxd_mqtt_client_state = NXD_MQTT_CLIENT_STATE_CONNECTED;
209     client_ptr -> nxd_mqtt_client_packet_identifier = 1;
210     client_ptr -> nxd_mqtt_tls_session.nx_secure_tls_id = NX_SECURE_TLS_ID;
211     client_ptr -> nxd_mqtt_tls_session.nx_secure_tls_local_session_active = NX_FALSE;
212     client_ptr -> nxd_mqtt_tls_session.nx_secure_tls_tcp_socket = &client_ptr -> nxd_mqtt_client_socket;
213     client_ptr -> nxd_mqtt_client_socket.nx_tcp_socket_connect_ip.nxd_ip_version = NX_IP_VERSION_V4;
214     client_ptr -> nxd_mqtt_client_socket.nx_tcp_socket_state = NX_TCP_ESTABLISHED;
215     client_ptr -> nxd_mqtt_connect_notify(client_ptr, NXD_MQTT_SUCCESS, client_ptr -> nxd_mqtt_connect_context);
216     test_connect_notify = client_ptr -> nxd_mqtt_connect_notify;
217     test_connect_notify_context = client_ptr -> nxd_mqtt_connect_context;
218     test_disconnect_notify = client_ptr -> nxd_mqtt_disconnect_notify;
219 
220     return(status);
221 }
222 
__wrap__nxde_dns_host_by_name_get(NX_DNS * dns_ptr,UCHAR * host_name,NXD_ADDRESS * host_address_ptr,ULONG wait_option,UINT lookup_type)223 UINT __wrap__nxde_dns_host_by_name_get(NX_DNS *dns_ptr, UCHAR *host_name, NXD_ADDRESS *host_address_ptr,
224                                        ULONG wait_option, UINT lookup_type)
225 {
226 UINT status = g__wrap__nxde_dns_host_by_name_get_status;
227 
228     printf("HIJACKED: %s\n", __func__);
229     if (status)
230     {
231         return(status);
232     }
233 
234     host_address_ptr -> nxd_ip_address.v4 = IP_ADDRESS(127, 0, 0, 1);
235     return(NX_DNS_SUCCESS);
236 }
237 
__wrap__nxde_mqtt_client_subscribe(NXD_MQTT_CLIENT * client_ptr,CHAR * topic_name,UINT topic_name_length,UINT QoS)238 UINT __wrap__nxde_mqtt_client_subscribe(NXD_MQTT_CLIENT *client_ptr, CHAR *topic_name,
239                                         UINT topic_name_length, UINT QoS)
240 {
241     printf("HIJACKED: %s\n", __func__);
242     return(g__wrap__nxde_mqtt_client_subscribe_status);
243 }
244 
__wrap__nxde_mqtt_client_disconnect(NXD_MQTT_CLIENT * client_ptr)245 UINT __wrap__nxde_mqtt_client_disconnect(NXD_MQTT_CLIENT *client_ptr)
246 {
247 UINT status = g__wrap__nxde_mqtt_client_disconnect_status;
248 
249     printf("HIJACKED: %s\n", __func__);
250     if (status)
251     {
252         return(status);
253     }
254 
255     client_ptr -> nxd_mqtt_client_state = NXD_MQTT_CLIENT_STATE_IDLE;
256     client_ptr -> nxd_mqtt_client_socket.nx_tcp_socket_state = NX_TCP_CLOSED;
257     return(status);
258 }
259 
__wrap__nxd_mqtt_client_publish_packet_send(NXD_MQTT_CLIENT * client_ptr,NX_PACKET * packet_ptr,USHORT packet_id,UINT QoS,ULONG wait_option)260 UINT __wrap__nxd_mqtt_client_publish_packet_send(NXD_MQTT_CLIENT *client_ptr, NX_PACKET *packet_ptr,
261                                                  USHORT packet_id, UINT QoS, ULONG wait_option)
262 {
263 INT topic_name_length;
264 UINT message_length;
265 UCHAR *buffer_ptr;
266 UCHAR *topic_ptr;
267 UINT status = g__wrap__nxd_mqtt_client_publish_packet_send_status;
268 
269     printf("HIJACKED: %s\n", __func__);
270 
271     if (status)
272     {
273         return(status);
274     }
275 
276     buffer_ptr = packet_ptr -> nx_packet_prepend_ptr;
277     topic_name_length = (buffer_ptr[5] << 8) | (buffer_ptr[6]);
278     message_length = packet_ptr -> nx_packet_length - (9 + topic_name_length);
279     topic_ptr = &buffer_ptr[7];
280 
281     /* Check if it is query or register request */
282     if (strstr((CHAR *)topic_ptr, "operationId=") != NULL)
283     {
284         assert_memory_equal(topic_ptr, expected_query_topic_start, strlen(expected_query_topic_start));
285         assert_int_equal(sscanf((CHAR *)&topic_ptr[strlen(expected_query_topic_start)], "%u", &g_req_id), 1);
286     }
287     else
288     {
289         assert_memory_equal(topic_ptr, expected_register_topic_start, strlen(expected_register_topic_start));
290         assert_int_equal(sscanf((CHAR *)&topic_ptr[strlen(expected_register_topic_start)], "%u", &g_req_id), 1);
291     }
292 
293     if (iot_prov_client.nx_azure_iot_provisioning_client_registration_payload)
294     {
295         assert_int_equal(message_length, strlen(expected_message_with_custom_payload));
296         assert_memory_equal(&buffer_ptr[9 + topic_name_length],
297                             expected_message_with_custom_payload, message_length);
298     }
299     else
300     {
301         assert_int_equal(message_length, strlen(expected_message));
302         assert_memory_equal(&buffer_ptr[9 + topic_name_length], expected_message, message_length);
303     }
304 
305     assert_int_equal(QoS, 1);
306 
307     /* packet ownership taken and released */
308     nx_packet_release(packet_ptr);
309 
310     return(status);
311 }
312 
__wrap__nxde_mqtt_client_receive_notify_set(NXD_MQTT_CLIENT * client_ptr,VOID (* receive_notify)(NXD_MQTT_CLIENT * client_ptr,UINT message_count))313 UINT __wrap__nxde_mqtt_client_receive_notify_set(NXD_MQTT_CLIENT *client_ptr,
314                                                  VOID (*receive_notify)(NXD_MQTT_CLIENT *client_ptr, UINT message_count))
315 {
316     printf("HIJACKED: %s\n", __func__);
317     test_receive_notify = receive_notify;
318     return(NX_AZURE_IOT_SUCCESS);
319 }
320 
321 UINT __real__nx_packet_data_append(NX_PACKET *packet_ptr, VOID *data_start, ULONG data_size,
322                                      NX_PACKET_POOL *pool_ptr, ULONG wait_option);
__wrap__nx_packet_data_append(NX_PACKET * packet_ptr,VOID * data_start,ULONG data_size,NX_PACKET_POOL * pool_ptr,ULONG wait_option)323 UINT __wrap__nx_packet_data_append(NX_PACKET *packet_ptr, VOID *data_start, ULONG data_size,
324                                      NX_PACKET_POOL *pool_ptr, ULONG wait_option)
325 {
326     printf("HIJACKED: %s\n", __func__);
327 
328     if (g_failed_append_index == g_total_append)
329     {
330         return(NX_NOT_SUCCESSFUL);
331     }
332 
333     g_total_append++;
334     return __real__nx_packet_data_append(packet_ptr, data_start, data_size, pool_ptr, wait_option);
335 }
336 
337 UINT __real_nx_azure_iot_buffer_allocate(NX_AZURE_IOT *nx_azure_iot_ptr, UCHAR **buffer_pptr,
338                                          UINT *buffer_size, VOID **buffer_context);
__wrap_nx_azure_iot_buffer_allocate(NX_AZURE_IOT * nx_azure_iot_ptr,UCHAR ** buffer_pptr,UINT * buffer_size,VOID ** buffer_context)339 UINT __wrap_nx_azure_iot_buffer_allocate(NX_AZURE_IOT *nx_azure_iot_ptr, UCHAR **buffer_pptr,
340                                          UINT *buffer_size, VOID **buffer_context)
341 {
342     printf("HIJACKED: %s\n", __func__);
343 
344     if (g_failed_allocation_index == g_total_allocation)
345     {
346         return(NX_NOT_SUCCESSFUL);
347     }
348 
349     g_total_allocation++;
350     return __real_nx_azure_iot_buffer_allocate(nx_azure_iot_ptr, buffer_pptr, buffer_size, buffer_context);
351 }
352 
mqtt_client_set_fixed_header(NXD_MQTT_CLIENT * client_ptr,NX_PACKET * packet_ptr,UCHAR control_header,UINT length,UINT wait_option)353 static UINT mqtt_client_set_fixed_header(NXD_MQTT_CLIENT *client_ptr, NX_PACKET *packet_ptr, UCHAR control_header, UINT length, UINT wait_option)
354 {
355 UCHAR  fixed_header[5];
356 UCHAR *byte = fixed_header;
357 UINT   count = 0;
358 UINT   ret;
359 
360     *byte = control_header;
361     byte++;
362 
363     do
364     {
365         if (length & 0xFFFFFF80)
366         {
367             *(byte + count) = (UCHAR)((length & 0x7F) | 0x80);
368         }
369         else
370         {
371             *(byte + count) = length & 0x7F;
372         }
373         length = length >> 7;
374 
375         count++;
376     } while (length != 0);
377 
378     ret = __real__nx_packet_data_append(packet_ptr, fixed_header, count + 1,
379                                         client_ptr -> nxd_mqtt_client_packet_pool_ptr, wait_option);
380 
381     return(ret);
382 }
383 
on_complete_callback(NX_AZURE_IOT_PROVISIONING_CLIENT * prov_client_ptr,UINT status)384 static VOID on_complete_callback(NX_AZURE_IOT_PROVISIONING_CLIENT *prov_client_ptr, UINT status)
385 {
386     g_done = 1;
387 }
388 
construct_provisioning_service_response(NX_AZURE_IOT_PROVISIONING_CLIENT * prov_client_ptr,NX_PACKET ** packet_pptr,const UCHAR * topic,ULONG topic_len,PROPERTY * properties,UINT property_count,const UCHAR * message_payload_ptr,ULONG message_payload_length)389 static VOID construct_provisioning_service_response(NX_AZURE_IOT_PROVISIONING_CLIENT *prov_client_ptr,
390                                                     NX_PACKET **packet_pptr,
391                                                     const UCHAR *topic, ULONG topic_len,
392                                                     PROPERTY *properties, UINT property_count,
393                                                     const UCHAR *message_payload_ptr, ULONG message_payload_length)
394 {
395 NX_PACKET *packet_ptr;
396 ULONG total_length;
397 ULONG topic_length = topic_len;
398 UCHAR bytes[2];
399 UINT i;
400 
401     assert_int_equal(nx_packet_allocate(iot.nx_azure_iot_pool_ptr, &packet_ptr, 0, NX_NO_WAIT),
402                      NX_AZURE_IOT_SUCCESS);
403 
404     for (i = 0; i < property_count; i++)
405     {
406         topic_length += properties[i].name_length + properties[i].value_length + 2; /* '=' and '&' */
407     }
408 
409     if (property_count)
410     {
411         topic_length--; /* Reduce by one since last '&' is not needed. */
412     }
413     total_length = topic_length + 2 + 2 + message_payload_length; /* Two bytes for fixed topic_length field
414                                                                      and two bytes for packet id. */
415 
416     /* Set fixed header. */
417     assert_int_equal(mqtt_client_set_fixed_header(&(prov_client_ptr -> nx_azure_iot_provisioning_client_resource.resource_mqtt), packet_ptr,
418                                                   (UCHAR)((MQTT_CONTROL_PACKET_TYPE_PUBLISH << 4) | MQTT_PUBLISH_QOS_LEVEL_1),
419                                                   total_length, NX_NO_WAIT),
420                      NX_AZURE_IOT_SUCCESS);
421 
422     /* Set topic length. */
423     bytes[0] = (topic_length >> 8) & 0xFF;
424     bytes[1] = topic_length & 0xFF;
425     assert_int_equal(__real__nx_packet_data_append(packet_ptr, bytes, sizeof(bytes),
426                                                    iot.nx_azure_iot_pool_ptr, NX_NO_WAIT),
427                      NX_AZURE_IOT_SUCCESS);
428 
429     /* Set topic. */
430     assert_int_equal(__real__nx_packet_data_append(packet_ptr, (VOID *)topic, topic_len,
431                                                    iot.nx_azure_iot_pool_ptr, NX_NO_WAIT),
432                      NX_AZURE_IOT_SUCCESS);
433     for (i = 0; i < property_count; i++)
434     {
435         if (i != 0)
436         {
437             assert_int_equal(__real__nx_packet_data_append(packet_ptr, "&", 1,
438                                                            iot.nx_azure_iot_pool_ptr, NX_NO_WAIT),
439                              NX_AZURE_IOT_SUCCESS);
440         }
441         assert_int_equal(__real__nx_packet_data_append(packet_ptr, properties[i].name, properties[i].name_length,
442                                                        iot.nx_azure_iot_pool_ptr, NX_NO_WAIT),
443                          NX_AZURE_IOT_SUCCESS);
444         assert_int_equal(__real__nx_packet_data_append(packet_ptr, "=", 1,
445                                                        iot.nx_azure_iot_pool_ptr, NX_NO_WAIT),
446                          NX_AZURE_IOT_SUCCESS);
447         assert_int_equal(__real__nx_packet_data_append(packet_ptr, properties[i].value, properties[i].value_length,
448                                                        iot.nx_azure_iot_pool_ptr, NX_NO_WAIT),
449                          NX_AZURE_IOT_SUCCESS);
450     }
451 
452     /* Set packet ID. The value does not matter. */
453     assert_int_equal(__real__nx_packet_data_append(packet_ptr, bytes, sizeof(bytes),
454                                                    iot.nx_azure_iot_pool_ptr, NX_NO_WAIT),
455                      NX_AZURE_IOT_SUCCESS);
456 
457     /* Set message payload. */
458     if (message_payload_length > 0)
459     {
460         assert_int_equal(__real__nx_packet_data_append(packet_ptr, (VOID *)message_payload_ptr, message_payload_length,
461                                                        iot.nx_azure_iot_pool_ptr, NX_NO_WAIT),
462                          NX_AZURE_IOT_SUCCESS);
463     }
464 
465     *packet_pptr = packet_ptr;
466 }
467 
468 
generate_response(NX_AZURE_IOT_PROVISIONING_CLIENT * prov_client_ptr,const UCHAR * topic,ULONG topic_length,const UCHAR * message_payload_ptr,ULONG message_payload_length,UINT retry_after)469 static VOID generate_response(NX_AZURE_IOT_PROVISIONING_CLIENT *prov_client_ptr,
470                               const UCHAR *topic, ULONG topic_length,
471                               const UCHAR *message_payload_ptr, ULONG message_payload_length,
472                               UINT retry_after)
473 {
474 PROPERTY response_properties[2] = { retry_after_property, req_id_property };
475 NX_PACKET *packet_ptr;
476 
477     /* Check if client expecting response */
478     if (g_req_id == 0)
479     {
480         return;
481     }
482 
483     response_properties[1].value_length = snprintf(response_properties[1].value,
484                                                    response_properties[1].value_length, "%u", g_req_id);
485     response_properties[0].value_length = snprintf(response_properties[0].value,
486                                                    response_properties[0].value_length, "%u", retry_after);
487     /* Put Provisioning response packet to MQTT receive queue.  */
488     construct_provisioning_service_response(prov_client_ptr,
489                                             &packet_ptr, topic, topic_length,
490                                             response_properties,
491                                             sizeof(response_properties)/sizeof(response_properties[0]),
492                                             message_payload_ptr, message_payload_length);
493 
494     prov_client_ptr -> nx_azure_iot_provisioning_client_resource.resource_mqtt.message_receive_queue_head = packet_ptr;
495     prov_client_ptr -> nx_azure_iot_provisioning_client_resource.resource_mqtt.message_receive_queue_depth = 1;
496 
497     /* Expected callback function to be set. */
498     assert_ptr_not_equal(test_receive_notify, NX_NULL);
499 
500     /* Simulate callback from MQTT layer.  */
501     tx_mutex_get(prov_client_ptr -> nx_azure_iot_ptr -> nx_azure_iot_mutex_ptr, NX_WAIT_FOREVER);
502     test_receive_notify(&prov_client_ptr -> nx_azure_iot_provisioning_client_resource.resource_mqtt, 1);
503     tx_mutex_put(prov_client_ptr -> nx_azure_iot_ptr -> nx_azure_iot_mutex_ptr);
504 
505     /* response generated for this req_id */
506     g_req_id = 0;
507 }
508 
generate_good_response(NX_AZURE_IOT_PROVISIONING_CLIENT * prov_client_ptr)509 static VOID generate_good_response(NX_AZURE_IOT_PROVISIONING_CLIENT *prov_client_ptr)
510 {
511     if (g_req_id == 0)
512     {
513         return;
514     }
515 
516     if (g_assigned_response_after == 0)
517     {
518         generate_response(prov_client_ptr,
519                           PROVISIONING_SERVICE_REPONSE_TOPIC,
520                           sizeof(PROVISIONING_SERVICE_REPONSE_TOPIC) - 1,
521                           assigned_hub_response, sizeof(assigned_hub_response) - 1, 0);
522     }
523     else
524     {
525         generate_response(prov_client_ptr,
526                           PROVISIONING_SERVICE_REPONSE_TOPIC,
527                           sizeof(PROVISIONING_SERVICE_REPONSE_TOPIC) - 1,
528                           assigning_hub_response, sizeof(assigning_hub_response) - 1, 3);
529         g_assigned_response_after--;
530     }
531 }
532 
generate_good_response_with_throttle(NX_AZURE_IOT_PROVISIONING_CLIENT * prov_client_ptr)533 static VOID generate_good_response_with_throttle(NX_AZURE_IOT_PROVISIONING_CLIENT *prov_client_ptr)
534 {
535     if (g_req_id == 0)
536     {
537         return;
538     }
539 
540     if (g_assigned_response_after == 0)
541     {
542         generate_response(prov_client_ptr,
543                           PROVISIONING_SERVICE_REPONSE_TOPIC,
544                           sizeof(PROVISIONING_SERVICE_REPONSE_TOPIC) - 1,
545                           assigned_hub_response, sizeof(assigned_hub_response) - 1, 0);
546     }
547     else
548     {
549         generate_response(prov_client_ptr,
550                           PROVISIONING_SERVICE_REPONSE_THROTTLE_TOPIC,
551                           sizeof(PROVISIONING_SERVICE_REPONSE_THROTTLE_TOPIC) - 1,
552                           throttle_hub_response, sizeof(throttle_hub_response) - 1, 31);
553         g_assigned_response_after--;
554     }
555 }
556 
generate_bad_response(NX_AZURE_IOT_PROVISIONING_CLIENT * prov_client_ptr)557 static VOID generate_bad_response(NX_AZURE_IOT_PROVISIONING_CLIENT *prov_client_ptr)
558 {
559     generate_response(prov_client_ptr,
560                       PROVISIONING_SERVICE_REPONSE_BAD_TOPIC,
561                       sizeof(PROVISIONING_SERVICE_REPONSE_BAD_TOPIC) - 1, " ", 1, 0);
562 }
563 
generate_failure_response(NX_AZURE_IOT_PROVISIONING_CLIENT * prov_client_ptr)564 static VOID generate_failure_response(NX_AZURE_IOT_PROVISIONING_CLIENT *prov_client_ptr)
565 {
566     generate_response(prov_client_ptr,
567                       PROVISIONING_SERVICE_REPONSE_200_TOPIC,
568                       sizeof(PROVISIONING_SERVICE_REPONSE_200_TOPIC) - 1,
569                       failure_hub_response, sizeof(failure_hub_response) - 1, 0);
570 }
571 
reset_global_state()572 static VOID reset_global_state()
573 {
574    /* reset global state */
575     g__wrap__nxde_mqtt_client_secure_connect_status = 0;
576     g__wrap__nxde_mqtt_client_subscribe_status = 0;
577     g__wrap__nxde_mqtt_client_disconnect_status = 0;
578     g__wrap__nxd_mqtt_client_publish_packet_send_status = 0;
579     g__wrap__nxde_dns_host_by_name_get_status = 0;
580     g_done = 0;
581     g_req_id = 0;
582     g_assigned_response_after = 0;
583     g_failed_append_index = (UINT)-1;
584     g_total_append = 0;
585     g_failed_allocation_index = (UINT)-1;
586     g_total_allocation = 0;
587 }
588 
589 /* Hook execute before all tests. */
test_suit_begin()590 static VOID test_suit_begin()
591 {
592 
593     /* Initialize root certificate.  */
594     assert_int_equal(nx_secure_x509_certificate_initialize(&root_ca_cert,
595                                                            (UCHAR *)_nx_azure_iot_root_cert,
596                                                            (USHORT)_nx_azure_iot_root_cert_size,
597                                                            NX_NULL, 0, NULL, 0, NX_SECURE_X509_KEY_TYPE_NONE),
598                      NX_AZURE_IOT_SUCCESS);
599 
600     assert_int_equal(nx_azure_iot_create(&iot, (UCHAR *)"Azure IoT",
601                                          g_ip_ptr, g_pool_ptr, g_dns_ptr,
602                                          (UCHAR *)demo_cloud_thread_stack,
603                                          sizeof(demo_cloud_thread_stack),
604                                          DEMO_CLOUD_THREAD_PRIORITY, unix_time_get),
605                      NX_AZURE_IOT_SUCCESS);
606 }
607 
608 /* Hook execute after all tests are executed successfully */
test_suit_end()609 static VOID test_suit_end()
610 {
611 
612     /* SUCCESS: iot is deleted. */
613     assert_int_equal(nx_azure_iot_delete(&iot), NX_AZURE_IOT_SUCCESS);
614 }
615 
616 /* Hook executed before every test */
test_begin()617 static VOID test_begin()
618 {
619     reset_global_state();
620 
621     /* Record number of available packet before test */
622     g_available_packet = g_pool_ptr -> nx_packet_pool_available;
623 }
624 
625 /* Hook execute after all tests are executed successfully */
test_end()626 static VOID test_end()
627 {
628 
629     /* Check if all the packet are released */
630     assert_int_equal(g_pool_ptr -> nx_packet_pool_available, g_available_packet);
631 }
632 
633 /**
634  * Test provisioning client initialization with invalid parameter
635  *
636  **/
test_nx_azure_iot_provisioning_client_initialize_invalid_arguments()637 static VOID test_nx_azure_iot_provisioning_client_initialize_invalid_arguments()
638 {
639     assert_int_not_equal(nx_azure_iot_provisioning_client_initialize(NX_NULL, &iot,
640                                                                      g_endpoint, sizeof(g_endpoint) - 1,
641                                                                      g_id_scope, sizeof(g_id_scope) - 1,
642                                                                      g_reg_id, sizeof(g_reg_id) - 1,
643                                                                      _nx_azure_iot_tls_supported_crypto,
644                                                                      _nx_azure_iot_tls_supported_crypto_size,
645                                                                      _nx_azure_iot_tls_ciphersuite_map,
646                                                                      _nx_azure_iot_tls_ciphersuite_map_size,
647                                                                      metadata_buffer, sizeof(metadata_buffer),
648                                                                      &root_ca_cert),
649                          NX_AZURE_IOT_SUCCESS);
650 
651     assert_int_not_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, NX_NULL,
652                                                                      g_endpoint, sizeof(g_endpoint) - 1,
653                                                                      g_id_scope, sizeof(g_id_scope) - 1,
654                                                                      g_reg_id, sizeof(g_reg_id) - 1,
655                                                                      _nx_azure_iot_tls_supported_crypto,
656                                                                      _nx_azure_iot_tls_supported_crypto_size,
657                                                                      _nx_azure_iot_tls_ciphersuite_map,
658                                                                      _nx_azure_iot_tls_ciphersuite_map_size,
659                                                                      metadata_buffer, sizeof(metadata_buffer),
660                                                                      &root_ca_cert),
661                          NX_AZURE_IOT_SUCCESS);
662 
663 
664     assert_int_not_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
665                                                                      g_endpoint, sizeof(g_endpoint) - 1,
666                                                                      NX_NULL, 0,
667                                                                      g_reg_id, sizeof(g_reg_id) - 1,
668                                                                      _nx_azure_iot_tls_supported_crypto,
669                                                                      _nx_azure_iot_tls_supported_crypto_size,
670                                                                      _nx_azure_iot_tls_ciphersuite_map,
671                                                                      _nx_azure_iot_tls_ciphersuite_map_size,
672                                                                      metadata_buffer, sizeof(metadata_buffer),
673                                                                      &root_ca_cert),
674                          NX_AZURE_IOT_SUCCESS);
675 
676     assert_int_not_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
677                                                                      g_endpoint, sizeof(g_endpoint) - 1,
678                                                                      NX_NULL, 0,
679                                                                      g_reg_id, sizeof(g_reg_id) - 1,
680                                                                      _nx_azure_iot_tls_supported_crypto,
681                                                                      _nx_azure_iot_tls_supported_crypto_size,
682                                                                      _nx_azure_iot_tls_ciphersuite_map,
683                                                                      _nx_azure_iot_tls_ciphersuite_map_size,
684                                                                      metadata_buffer, sizeof(metadata_buffer),
685                                                                      &root_ca_cert),
686                          NX_AZURE_IOT_SUCCESS);
687 
688     assert_int_not_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
689                                                                      g_endpoint, sizeof(g_endpoint) - 1,
690                                                                      g_id_scope, sizeof(g_id_scope) - 1,
691                                                                      NX_NULL, 0,
692                                                                      _nx_azure_iot_tls_supported_crypto,
693                                                                      _nx_azure_iot_tls_supported_crypto_size,
694                                                                      _nx_azure_iot_tls_ciphersuite_map,
695                                                                      _nx_azure_iot_tls_ciphersuite_map_size,
696                                                                      metadata_buffer, sizeof(metadata_buffer),
697                                                                      &root_ca_cert),
698                          NX_AZURE_IOT_SUCCESS);
699 
700 
701     assert_int_not_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
702                                                                      g_endpoint, sizeof(g_endpoint) - 1,
703                                                                      "", 0,
704                                                                      g_reg_id, sizeof(g_reg_id) - 1,
705                                                                      _nx_azure_iot_tls_supported_crypto,
706                                                                      _nx_azure_iot_tls_supported_crypto_size,
707                                                                      _nx_azure_iot_tls_ciphersuite_map,
708                                                                      _nx_azure_iot_tls_ciphersuite_map_size,
709                                                                      metadata_buffer, sizeof(metadata_buffer),
710                                                                      &root_ca_cert),
711                          NX_AZURE_IOT_SUCCESS);
712 
713     assert_int_not_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
714                                                                      g_endpoint, sizeof(g_endpoint) - 1,
715                                                                      "", 0,
716                                                                      g_reg_id, sizeof(g_reg_id) - 1,
717                                                                      _nx_azure_iot_tls_supported_crypto,
718                                                                      _nx_azure_iot_tls_supported_crypto_size,
719                                                                      _nx_azure_iot_tls_ciphersuite_map,
720                                                                      _nx_azure_iot_tls_ciphersuite_map_size,
721                                                                      metadata_buffer, sizeof(metadata_buffer),
722                                                                      &root_ca_cert),
723                          NX_AZURE_IOT_SUCCESS);
724 
725     assert_int_not_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
726                                                                      g_endpoint, sizeof(g_endpoint) - 1,
727                                                                      g_id_scope, sizeof(g_id_scope) - 1,
728                                                                      "", 0,
729                                                                      _nx_azure_iot_tls_supported_crypto,
730                                                                      _nx_azure_iot_tls_supported_crypto_size,
731                                                                      _nx_azure_iot_tls_ciphersuite_map,
732                                                                      _nx_azure_iot_tls_ciphersuite_map_size,
733                                                                      metadata_buffer, sizeof(metadata_buffer),
734                                                                      &root_ca_cert),
735                          NX_AZURE_IOT_SUCCESS);
736 }
737 
738 /**
739  * Test provisioning client set device cert with invalid parameter
740  *
741  **/
test_nx_azure_iot_provisioning_client_device_cert_set_invalid_arguments()742 static VOID test_nx_azure_iot_provisioning_client_device_cert_set_invalid_arguments()
743 {
744     assert_int_not_equal(nx_azure_iot_provisioning_client_device_cert_set(NX_NULL,
745                                                                           &root_ca_cert),
746                          NX_AZURE_IOT_SUCCESS);
747 }
748 
749 /**
750  * Test provisioning client set symmetric key with invalid parameter
751  *
752  **/
test_nx_azure_iot_provisioning_client_symmetric_key_set_invalid_arguments()753 static VOID test_nx_azure_iot_provisioning_client_symmetric_key_set_invalid_arguments()
754 {
755     assert_int_not_equal(nx_azure_iot_provisioning_client_symmetric_key_set(NX_NULL,
756                                                                             symmetric_key, strlen(symmetric_key)),
757                          NX_AZURE_IOT_SUCCESS);
758 
759     assert_int_not_equal(nx_azure_iot_provisioning_client_symmetric_key_set(&iot_prov_client,
760                                                                             NX_NULL, 0),
761                          NX_AZURE_IOT_SUCCESS);
762 }
763 
764 /**
765  * Test provisioning client register with invalid parameter
766  *
767  **/
test_nx_azure_iot_provisioning_client_register_invalid_arguments()768 static VOID test_nx_azure_iot_provisioning_client_register_invalid_arguments()
769 {
770     assert_int_not_equal(nx_azure_iot_provisioning_client_register(NX_NULL, 0),
771                          NX_AZURE_IOT_SUCCESS);
772 }
773 
774 /**
775  * Test provisioning client callback set with invalid parameter
776  *
777  **/
test_nx_azure_iot_provisioning_client_completion_callback_set_invalid_arguments()778 static VOID test_nx_azure_iot_provisioning_client_completion_callback_set_invalid_arguments()
779 {
780     assert_int_not_equal(nx_azure_iot_provisioning_client_completion_callback_set(NX_NULL, on_complete_callback),
781                          NX_AZURE_IOT_SUCCESS);
782 }
783 
784 /**
785  * Test provisioning client deinitialization with invalid parameter
786  *
787  **/
test_nx_azure_iot_provisioning_client_deinitialize_invalid_arguments()788 static VOID test_nx_azure_iot_provisioning_client_deinitialize_invalid_arguments()
789 {
790     assert_int_not_equal(nx_azure_iot_provisioning_client_deinitialize(NX_NULL),
791                          NX_AZURE_IOT_SUCCESS);
792 }
793 
794 /**
795  * Test provisioning client registration payload set with invalid parameter
796  *
797  **/
test_nx_azure_iot_provisioning_client_registration_payload_set_invalid_arguments()798 static VOID test_nx_azure_iot_provisioning_client_registration_payload_set_invalid_arguments()
799 {
800     assert_int_not_equal(nx_azure_iot_provisioning_client_registration_payload_set(NX_NULL, NX_NULL, 0),
801                          NX_AZURE_IOT_SUCCESS);
802 }
803 
804 /**
805  * Test provisioning client initialization
806  *
807  **/
test_nx_azure_iot_provisioning_client_init_success()808 static VOID test_nx_azure_iot_provisioning_client_init_success()
809 {
810     printf("test starts =>: %s\n", __func__);
811     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
812                                                                  g_endpoint, sizeof(g_endpoint) - 1,
813                                                                  g_id_scope, sizeof(g_id_scope) - 1,
814                                                                  g_reg_id, sizeof(g_reg_id) - 1,
815                                                                  _nx_azure_iot_tls_supported_crypto,
816                                                                  _nx_azure_iot_tls_supported_crypto_size,
817                                                                  _nx_azure_iot_tls_ciphersuite_map,
818                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
819                                                                  metadata_buffer, sizeof(metadata_buffer),
820                                                                  &root_ca_cert),
821                      NX_AZURE_IOT_SUCCESS);
822 
823     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
824                      NX_AZURE_IOT_SUCCESS);
825 }
826 
827 /**
828  * Test provisioning client registration without initialization
829  *
830  **/
test_nx_azure_iot_provisioning_client_register_without_initialization_fail()831 static VOID test_nx_azure_iot_provisioning_client_register_without_initialization_fail()
832 {
833     printf("test starts =>: %s\n", __func__);
834     assert_int_not_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, 0),
835                          NX_AZURE_IOT_SUCCESS);
836 }
837 
838 /**
839  * Test provisioning client registration fails, if dns failed
840  *
841  **/
test_nx_azure_iot_provisioning_client_register_dns_failed()842 static VOID test_nx_azure_iot_provisioning_client_register_dns_failed()
843 {
844     printf("test starts =>: %s\n", __func__);
845     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
846                                                                  g_endpoint, sizeof(g_endpoint) - 1,
847                                                                  g_id_scope, sizeof(g_id_scope) - 1,
848                                                                  g_reg_id, sizeof(g_reg_id) - 1,
849                                                                  _nx_azure_iot_tls_supported_crypto,
850                                                                  _nx_azure_iot_tls_supported_crypto_size,
851                                                                  _nx_azure_iot_tls_ciphersuite_map,
852                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
853                                                                  metadata_buffer, sizeof(metadata_buffer),
854                                                                  &root_ca_cert),
855                      NX_AZURE_IOT_SUCCESS);
856     assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
857                                                                               on_complete_callback),
858                      NX_AZURE_IOT_SUCCESS);
859     g__wrap__nxde_dns_host_by_name_get_status = NX_NOT_SUCCESSFUL;
860 
861     assert_int_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, 0),
862                      NX_AZURE_IOT_PENDING);
863     while (!g_done)
864     {
865         tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
866     }
867 
868     assert_int_not_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
869     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
870                      NX_AZURE_IOT_SUCCESS);
871 }
872 
873 /**
874  * Test provisioning client registration fails, if subscribe fails
875  *
876  **/
test_nx_azure_iot_provisioning_client_register_subscribe_failed()877 static VOID test_nx_azure_iot_provisioning_client_register_subscribe_failed()
878 {
879     printf("test starts =>: %s\n", __func__);
880     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
881                                                                  g_endpoint, sizeof(g_endpoint) - 1,
882                                                                  g_id_scope, sizeof(g_id_scope) - 1,
883                                                                  g_reg_id, sizeof(g_reg_id) - 1,
884                                                                  _nx_azure_iot_tls_supported_crypto,
885                                                                  _nx_azure_iot_tls_supported_crypto_size,
886                                                                  _nx_azure_iot_tls_ciphersuite_map,
887                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
888                                                                  metadata_buffer, sizeof(metadata_buffer),
889                                                                  &root_ca_cert),
890                      NX_AZURE_IOT_SUCCESS);
891     assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
892                                                                               on_complete_callback),
893                      NX_AZURE_IOT_SUCCESS);
894     g__wrap__nxde_mqtt_client_subscribe_status = NX_NOT_SUCCESSFUL;
895 
896     assert_int_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, 0),
897                      NX_AZURE_IOT_PENDING);
898     while (!g_done)
899     {
900         tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
901     }
902 
903     assert_int_not_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
904     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
905                      NX_AZURE_IOT_SUCCESS);
906 }
907 
908 /**
909  * Test provisioning client registration fails, if publish to service fails
910  *
911  **/
test_nx_azure_iot_provisioning_client_register_send_failed()912 static VOID test_nx_azure_iot_provisioning_client_register_send_failed()
913 {
914     printf("test starts =>: %s\n", __func__);
915     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
916                                                                  g_endpoint, sizeof(g_endpoint) - 1,
917                                                                  g_id_scope, sizeof(g_id_scope) - 1,
918                                                                  g_reg_id, sizeof(g_reg_id) - 1,
919                                                                  _nx_azure_iot_tls_supported_crypto,
920                                                                  _nx_azure_iot_tls_supported_crypto_size,
921                                                                  _nx_azure_iot_tls_ciphersuite_map,
922                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
923                                                                  metadata_buffer, sizeof(metadata_buffer),
924                                                                  &root_ca_cert),
925                      NX_AZURE_IOT_SUCCESS);
926     assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
927                                                                               on_complete_callback),
928                      NX_AZURE_IOT_SUCCESS);
929     g__wrap__nxd_mqtt_client_publish_packet_send_status = NX_NOT_SUCCESSFUL;
930 
931     assert_int_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, 0),
932                      NX_AZURE_IOT_PENDING);
933     while (!g_done)
934     {
935         tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
936     }
937 
938     assert_int_not_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
939     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
940                      NX_AZURE_IOT_SUCCESS);
941 }
942 
943 /**
944  * Test provisioning client registration fails, if mqtt client disconnect while waiting for response
945  *
946  **/
test_nx_azure_iot_provisioning_client_register_receive_failed()947 static VOID  test_nx_azure_iot_provisioning_client_register_receive_failed()
948 {
949     printf("test starts =>: %s\n", __func__);
950     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
951                                                                  g_endpoint, sizeof(g_endpoint) - 1,
952                                                                  g_id_scope, sizeof(g_id_scope) - 1,
953                                                                  g_reg_id, sizeof(g_reg_id) - 1,
954                                                                  _nx_azure_iot_tls_supported_crypto,
955                                                                  _nx_azure_iot_tls_supported_crypto_size,
956                                                                  _nx_azure_iot_tls_ciphersuite_map,
957                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
958                                                                  metadata_buffer, sizeof(metadata_buffer),
959                                                                  &root_ca_cert),
960                      NX_AZURE_IOT_SUCCESS);
961     assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
962                                                                               on_complete_callback),
963                      NX_AZURE_IOT_SUCCESS);
964 
965     assert_int_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, 0),
966                      NX_AZURE_IOT_PENDING);
967 
968     /* Expected callback function to be set. */
969     assert_ptr_not_equal(test_connect_notify, NX_NULL);
970     while (!g_done)
971     {
972         tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
973 
974         if (g_req_id != 0)
975         {
976             /* Generate disconnect */
977             test_connect_notify(&iot_prov_client.nx_azure_iot_provisioning_client_resource.resource_mqtt,
978                                 NXD_MQTT_CONNECT_FAILURE, test_connect_notify_context);
979         }
980     }
981 
982     assert_int_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_DISCONNECTED);
983     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
984                      NX_AZURE_IOT_SUCCESS);
985 
986 }
987 
988 /**
989  * Test provisioning client registration fails, if bad response received by client
990  *
991  **/
test_nx_azure_iot_provisioning_client_register_receive_bad_response_failed()992 static VOID  test_nx_azure_iot_provisioning_client_register_receive_bad_response_failed()
993 {
994     printf("test starts =>: %s\n", __func__);
995     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
996                                                                  g_endpoint, sizeof(g_endpoint) - 1,
997                                                                  g_id_scope, sizeof(g_id_scope) - 1,
998                                                                  g_reg_id, sizeof(g_reg_id) - 1,
999                                                                  _nx_azure_iot_tls_supported_crypto,
1000                                                                  _nx_azure_iot_tls_supported_crypto_size,
1001                                                                  _nx_azure_iot_tls_ciphersuite_map,
1002                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
1003                                                                  metadata_buffer, sizeof(metadata_buffer),
1004                                                                  &root_ca_cert),
1005                      NX_AZURE_IOT_SUCCESS);
1006     assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
1007                                                                               on_complete_callback),
1008                      NX_AZURE_IOT_SUCCESS);
1009 
1010     assert_int_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, 0),
1011                      NX_AZURE_IOT_PENDING);
1012     while (!g_done)
1013     {
1014         generate_bad_response(&iot_prov_client);
1015         tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
1016     }
1017 
1018     assert_int_not_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
1019     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1020                      NX_AZURE_IOT_SUCCESS);
1021 }
1022 
1023 /**
1024  * Test provisioning client registration fails, if bad JSON response received by client
1025  *
1026  **/
test_nx_azure_iot_provisioning_client_register_receive_invalid_json_failed()1027 static VOID  test_nx_azure_iot_provisioning_client_register_receive_invalid_json_failed()
1028 {
1029     printf("test starts =>: %s\n", __func__);
1030 
1031     for (INT index = 0; index < sizeof(invalid_response_data_set)/sizeof(invalid_response_data_set[0]); index++)
1032     {
1033         reset_global_state();
1034         assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
1035                                                                      g_endpoint, sizeof(g_endpoint) - 1,
1036                                                                      g_id_scope, sizeof(g_id_scope) - 1,
1037                                                                      g_reg_id, sizeof(g_reg_id) - 1,
1038                                                                      _nx_azure_iot_tls_supported_crypto,
1039                                                                      _nx_azure_iot_tls_supported_crypto_size,
1040                                                                      _nx_azure_iot_tls_ciphersuite_map,
1041                                                                      _nx_azure_iot_tls_ciphersuite_map_size,
1042                                                                      metadata_buffer, sizeof(metadata_buffer),
1043                                                                      &root_ca_cert),
1044                          NX_AZURE_IOT_SUCCESS);
1045         assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
1046                                                                                   on_complete_callback),
1047                          NX_AZURE_IOT_SUCCESS);
1048 
1049         assert_int_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, 0),
1050                          NX_AZURE_IOT_PENDING);
1051 
1052         while (!g_done)
1053         {
1054             generate_response(&iot_prov_client,
1055                               PROVISIONING_SERVICE_REPONSE_TOPIC,
1056                               sizeof(PROVISIONING_SERVICE_REPONSE_TOPIC) - 1,
1057                               invalid_response_data_set[index], strlen(invalid_response_data_set[index]), 3);
1058             tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
1059         }
1060 
1061         assert_int_not_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
1062         assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1063                          NX_AZURE_IOT_SUCCESS);
1064     }
1065 }
1066 
1067 /**
1068  * Test provisioning client registration succeeds
1069  *
1070  **/
test_nx_azure_iot_provisioning_client_register_success()1071 static VOID test_nx_azure_iot_provisioning_client_register_success()
1072 {
1073     printf("test starts =>: %s\n", __func__);
1074     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
1075                                                                  g_endpoint, sizeof(g_endpoint) - 1,
1076                                                                  g_id_scope, sizeof(g_id_scope) - 1,
1077                                                                  g_reg_id, sizeof(g_reg_id) - 1,
1078                                                                  _nx_azure_iot_tls_supported_crypto,
1079                                                                  _nx_azure_iot_tls_supported_crypto_size,
1080                                                                  _nx_azure_iot_tls_ciphersuite_map,
1081                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
1082                                                                  metadata_buffer, sizeof(metadata_buffer),
1083                                                                  &root_ca_cert),
1084                      NX_AZURE_IOT_SUCCESS);
1085     assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
1086                                                                               on_complete_callback),
1087                      NX_AZURE_IOT_SUCCESS);
1088 
1089     assert_int_equal(nx_azure_iot_provisioning_client_symmetric_key_set(&iot_prov_client,
1090                                                                         symmetric_key, strlen(symmetric_key)),
1091                      NX_AZURE_IOT_SUCCESS);
1092 
1093     assert_int_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, 0),
1094                      NX_AZURE_IOT_PENDING);
1095     while (!g_done)
1096     {
1097         generate_good_response(&iot_prov_client);
1098         tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
1099     }
1100 
1101     assert_int_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
1102     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1103                      NX_AZURE_IOT_SUCCESS);
1104 }
1105 
1106 /**
1107  * Test provisioning client registration failed with failure response
1108  *
1109  **/
test_nx_azure_iot_provisioning_client_register_failure_failed()1110 VOID test_nx_azure_iot_provisioning_client_register_failure_failed()
1111 {
1112     printf("test starts =>: %s\n", __func__);
1113     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
1114                                                                  g_endpoint, sizeof(g_endpoint) - 1,
1115                                                                  g_id_scope, sizeof(g_id_scope) - 1,
1116                                                                  g_reg_id, sizeof(g_reg_id) - 1,
1117                                                                  _nx_azure_iot_tls_supported_crypto,
1118                                                                  _nx_azure_iot_tls_supported_crypto_size,
1119                                                                  _nx_azure_iot_tls_ciphersuite_map,
1120                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
1121                                                                  metadata_buffer, sizeof(metadata_buffer),
1122                                                                  &root_ca_cert),
1123                      NX_AZURE_IOT_SUCCESS);
1124     assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
1125                                                                               on_complete_callback),
1126                      NX_AZURE_IOT_SUCCESS);
1127 
1128     assert_int_equal(nx_azure_iot_provisioning_client_symmetric_key_set(&iot_prov_client,
1129                                                                         symmetric_key, strlen(symmetric_key)),
1130                      NX_AZURE_IOT_SUCCESS);
1131 
1132     assert_int_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, 0),
1133                      NX_AZURE_IOT_PENDING);
1134     while (!g_done)
1135     {
1136         generate_failure_response(&iot_prov_client);
1137         tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
1138     }
1139 
1140     assert_int_not_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
1141     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1142                      NX_AZURE_IOT_SUCCESS);
1143 }
1144 
1145 /**
1146  * Test provisioning client registration with payload succeeds
1147  *
1148  **/
test_nx_azure_iot_provisioning_client_register_with_payload_success()1149 VOID test_nx_azure_iot_provisioning_client_register_with_payload_success()
1150 {
1151     printf("test starts =>: %s\n", __func__);
1152     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
1153                                                                  g_endpoint, sizeof(g_endpoint) - 1,
1154                                                                  g_id_scope, sizeof(g_id_scope) - 1,
1155                                                                  g_reg_id, sizeof(g_reg_id) - 1,
1156                                                                  _nx_azure_iot_tls_supported_crypto,
1157                                                                  _nx_azure_iot_tls_supported_crypto_size,
1158                                                                  _nx_azure_iot_tls_ciphersuite_map,
1159                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
1160                                                                  metadata_buffer, sizeof(metadata_buffer),
1161                                                                  &root_ca_cert),
1162                      NX_AZURE_IOT_SUCCESS);
1163     assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
1164                                                                               on_complete_callback),
1165                      NX_AZURE_IOT_SUCCESS);
1166 
1167     assert_int_equal(nx_azure_iot_provisioning_client_symmetric_key_set(&iot_prov_client,
1168                                                                         symmetric_key, strlen(symmetric_key)),
1169                      NX_AZURE_IOT_SUCCESS);
1170 
1171     assert_int_equal(nx_azure_iot_provisioning_client_registration_payload_set(&iot_prov_client,
1172                                                                                CUSTOM_PAYLOAD, sizeof(CUSTOM_PAYLOAD) - 1),
1173                      NX_AZURE_IOT_SUCCESS);
1174 
1175     assert_int_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, 0),
1176                      NX_AZURE_IOT_PENDING);
1177     while (!g_done)
1178     {
1179         generate_good_response(&iot_prov_client);
1180         tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
1181     }
1182 
1183     assert_int_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
1184     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1185                      NX_AZURE_IOT_SUCCESS);
1186 }
1187 
1188 /**
1189  * Test provisioning client registration fails with allocation fail
1190  *
1191  **/
test_nx_azure_iot_provisioning_client_register_allocation_fail_failure()1192 static VOID test_nx_azure_iot_provisioning_client_register_allocation_fail_failure()
1193 {
1194 UINT total_allocation_in_success_case;
1195 UINT status;
1196 
1197     printf("test starts =>: %s\n", __func__);
1198 
1199     test_nx_azure_iot_provisioning_client_register_success();
1200     total_allocation_in_success_case = g_total_allocation;
1201 
1202     for (INT index = 0; index < total_allocation_in_success_case; index++)
1203     {
1204         reset_global_state();
1205         g_failed_allocation_index = index;
1206 
1207         status = nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
1208                                                              g_endpoint, sizeof(g_endpoint) - 1,
1209                                                              g_id_scope, sizeof(g_id_scope) - 1,
1210                                                              g_reg_id, sizeof(g_reg_id) - 1,
1211                                                              _nx_azure_iot_tls_supported_crypto,
1212                                                              _nx_azure_iot_tls_supported_crypto_size,
1213                                                              _nx_azure_iot_tls_ciphersuite_map,
1214                                                              _nx_azure_iot_tls_ciphersuite_map_size,
1215                                                              metadata_buffer, sizeof(metadata_buffer),
1216                                                              &root_ca_cert);
1217 
1218         if (status)
1219         {
1220             continue;
1221         }
1222 
1223         status = nx_azure_iot_provisioning_client_symmetric_key_set(&iot_prov_client,
1224                                                                     symmetric_key, strlen(symmetric_key));
1225         if (status)
1226         {
1227             assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1228                              NX_AZURE_IOT_SUCCESS);
1229             continue;
1230         }
1231 
1232         assert_int_equal(nx_azure_iot_provisioning_client_registration_payload_set(&iot_prov_client,
1233                                                                                    CUSTOM_PAYLOAD,
1234                                                                                    sizeof(CUSTOM_PAYLOAD) - 1),
1235                          NX_AZURE_IOT_SUCCESS);
1236 
1237         assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
1238                                                                                   on_complete_callback),
1239                          NX_AZURE_IOT_SUCCESS);
1240 
1241         status = nx_azure_iot_provisioning_client_register(&iot_prov_client, 0);
1242         if (status != NX_AZURE_IOT_PENDING)
1243         {
1244             assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1245                              NX_AZURE_IOT_SUCCESS);
1246             continue;
1247         }
1248 
1249         while (!g_done)
1250         {
1251             generate_good_response(&iot_prov_client);
1252             tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
1253         }
1254 
1255         assert_int_not_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
1256         assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1257                          NX_AZURE_IOT_SUCCESS);
1258     }
1259 }
1260 
1261 /**
1262  * Test provisioning client registration fails with append data
1263  *
1264  **/
test_nx_azure_iot_provisioning_client_register_append_data_fail_failure()1265 static VOID test_nx_azure_iot_provisioning_client_register_append_data_fail_failure()
1266 {
1267 UINT total_append_in_success_case;
1268 UINT status;
1269 
1270     printf("test starts =>: %s\n", __func__);
1271 
1272     test_nx_azure_iot_provisioning_client_register_success();
1273     total_append_in_success_case = g_total_append;
1274 
1275     for (INT index = 0; index < total_append_in_success_case; index++)
1276     {
1277         reset_global_state();
1278         g_failed_append_index = index;
1279 
1280         status = nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
1281                                                              g_endpoint, sizeof(g_endpoint) - 1,
1282                                                              g_id_scope, sizeof(g_id_scope) - 1,
1283                                                              g_reg_id, sizeof(g_reg_id) - 1,
1284                                                              _nx_azure_iot_tls_supported_crypto,
1285                                                              _nx_azure_iot_tls_supported_crypto_size,
1286                                                              _nx_azure_iot_tls_ciphersuite_map,
1287                                                              _nx_azure_iot_tls_ciphersuite_map_size,
1288                                                              metadata_buffer, sizeof(metadata_buffer),
1289                                                              &root_ca_cert);
1290 
1291         if (status)
1292         {
1293             continue;
1294         }
1295 
1296         assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
1297                                                                                   on_complete_callback),
1298                          NX_AZURE_IOT_SUCCESS);
1299 
1300         status = nx_azure_iot_provisioning_client_register(&iot_prov_client, 0);
1301         if (status != NX_AZURE_IOT_PENDING)
1302         {
1303             assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1304                              NX_AZURE_IOT_SUCCESS);
1305             continue;
1306         }
1307 
1308         while (!g_done)
1309         {
1310             generate_good_response(&iot_prov_client);
1311             tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
1312         }
1313 
1314         assert_int_not_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
1315         assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1316                          NX_AZURE_IOT_SUCCESS);
1317     }
1318 }
1319 
1320 /**
1321  * Test provisioning client registration succeeds, when throttled by service.
1322  *
1323  **/
test_nx_azure_iot_provisioning_client_register_with_throttle_response_success()1324 static VOID test_nx_azure_iot_provisioning_client_register_with_throttle_response_success()
1325 {
1326     printf("test starts =>: %s\n", __func__);
1327     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
1328                                                                  g_endpoint, sizeof(g_endpoint) - 1,
1329                                                                  g_id_scope, sizeof(g_id_scope) - 1,
1330                                                                  g_reg_id, sizeof(g_reg_id) - 1,
1331                                                                  _nx_azure_iot_tls_supported_crypto,
1332                                                                  _nx_azure_iot_tls_supported_crypto_size,
1333                                                                  _nx_azure_iot_tls_ciphersuite_map,
1334                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
1335                                                                  metadata_buffer, sizeof(metadata_buffer),
1336                                                                  &root_ca_cert),
1337                      NX_AZURE_IOT_SUCCESS);
1338     assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
1339                                                                               on_complete_callback),
1340                      NX_AZURE_IOT_SUCCESS);
1341 
1342     g_assigned_response_after = 3;
1343     assert_int_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, 0),
1344                      NX_AZURE_IOT_PENDING);
1345     while (!g_done)
1346     {
1347         generate_good_response_with_throttle(&iot_prov_client);
1348         tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
1349     }
1350 
1351     assert_int_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
1352     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1353                      NX_AZURE_IOT_SUCCESS);
1354 }
1355 
1356 /**
1357  * Test provisioning client registration succeeds, when throttled by service after assigning .
1358  *
1359  **/
test_nx_azure_iot_provisioning_client_register_with_throttle_response_2_success()1360 static VOID test_nx_azure_iot_provisioning_client_register_with_throttle_response_2_success()
1361 {
1362     printf("test starts =>: %s\n", __func__);
1363     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
1364                                                                  g_endpoint, sizeof(g_endpoint) - 1,
1365                                                                  g_id_scope, sizeof(g_id_scope) - 1,
1366                                                                  g_reg_id, sizeof(g_reg_id) - 1,
1367                                                                  _nx_azure_iot_tls_supported_crypto,
1368                                                                  _nx_azure_iot_tls_supported_crypto_size,
1369                                                                  _nx_azure_iot_tls_ciphersuite_map,
1370                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
1371                                                                  metadata_buffer, sizeof(metadata_buffer),
1372                                                                  &root_ca_cert),
1373                      NX_AZURE_IOT_SUCCESS);
1374     assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
1375                                                                               on_complete_callback),
1376                      NX_AZURE_IOT_SUCCESS);
1377 
1378     g_assigned_response_after = 3;
1379     assert_int_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, 0),
1380                      NX_AZURE_IOT_PENDING);
1381     while (!g_done)
1382     {
1383         if (g_assigned_response_after == 3)
1384         {
1385             generate_good_response(&iot_prov_client);
1386         }
1387         else
1388         {
1389             generate_good_response_with_throttle(&iot_prov_client);
1390         }
1391 
1392         tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
1393     }
1394 
1395     assert_int_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
1396     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1397                      NX_AZURE_IOT_SUCCESS);
1398 }
1399 
1400 /**
1401  * Test provisioning client registration succeeds, when multiple request required.
1402  *
1403  **/
test_nx_azure_iot_provisioning_client_register_with_multiple_response_success()1404 static VOID test_nx_azure_iot_provisioning_client_register_with_multiple_response_success()
1405 {
1406     printf("test starts =>: %s\n", __func__);
1407     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
1408                                                                  g_endpoint, sizeof(g_endpoint) - 1,
1409                                                                  g_id_scope, sizeof(g_id_scope) - 1,
1410                                                                  g_reg_id, sizeof(g_reg_id) - 1,
1411                                                                  _nx_azure_iot_tls_supported_crypto,
1412                                                                  _nx_azure_iot_tls_supported_crypto_size,
1413                                                                  _nx_azure_iot_tls_ciphersuite_map,
1414                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
1415                                                                  metadata_buffer, sizeof(metadata_buffer),
1416                                                                  &root_ca_cert),
1417                      NX_AZURE_IOT_SUCCESS);
1418     assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
1419                                                                               on_complete_callback),
1420                      NX_AZURE_IOT_SUCCESS);
1421 
1422     g_assigned_response_after = 3;
1423     assert_int_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, 0),
1424                      NX_AZURE_IOT_PENDING);
1425     while (!g_done)
1426     {
1427         generate_good_response(&iot_prov_client);
1428         tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
1429     }
1430 
1431     assert_int_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
1432     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1433                      NX_AZURE_IOT_SUCCESS);
1434 }
1435 
1436 /**
1437  * Test provisioning client registration blocking fails, if dns failed
1438  *
1439  **/
test_nx_azure_iot_provisioning_client_register_dns_blocking_failed()1440 static VOID test_nx_azure_iot_provisioning_client_register_dns_blocking_failed()
1441 {
1442     printf("test starts =>: %s\n", __func__);
1443     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
1444                                                                  g_endpoint, sizeof(g_endpoint) - 1,
1445                                                                  g_id_scope, sizeof(g_id_scope) - 1,
1446                                                                  g_reg_id, sizeof(g_reg_id) - 1,
1447                                                                  _nx_azure_iot_tls_supported_crypto,
1448                                                                  _nx_azure_iot_tls_supported_crypto_size,
1449                                                                  _nx_azure_iot_tls_ciphersuite_map,
1450                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
1451                                                                  metadata_buffer, sizeof(metadata_buffer),
1452                                                                  &root_ca_cert),
1453                      NX_AZURE_IOT_SUCCESS);
1454     assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
1455                                                                               on_complete_callback),
1456                      NX_AZURE_IOT_SUCCESS);
1457     g__wrap__nxde_dns_host_by_name_get_status = NX_NOT_SUCCESSFUL;
1458 
1459     assert_int_not_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, NX_WAIT_FOREVER),
1460                          NX_AZURE_IOT_SUCCESS);
1461 
1462     assert_int_not_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
1463     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1464                      NX_AZURE_IOT_SUCCESS);
1465 }
1466 
1467 /**
1468  * Test provisioning client registration blocking fails, if subscribe fails
1469  *
1470  **/
test_nx_azure_iot_provisioning_client_register_subscribe_blocking_failed()1471 static VOID test_nx_azure_iot_provisioning_client_register_subscribe_blocking_failed()
1472 {
1473     printf("test starts =>: %s\n", __func__);
1474     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
1475                                                                  g_endpoint, sizeof(g_endpoint) - 1,
1476                                                                  g_id_scope, sizeof(g_id_scope) - 1,
1477                                                                  g_reg_id, sizeof(g_reg_id) - 1,
1478                                                                  _nx_azure_iot_tls_supported_crypto,
1479                                                                  _nx_azure_iot_tls_supported_crypto_size,
1480                                                                  _nx_azure_iot_tls_ciphersuite_map,
1481                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
1482                                                                  metadata_buffer, sizeof(metadata_buffer),
1483                                                                  &root_ca_cert),
1484                      NX_AZURE_IOT_SUCCESS);
1485     g__wrap__nxde_mqtt_client_subscribe_status = NX_NOT_SUCCESSFUL;
1486 
1487     assert_int_not_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, NX_WAIT_FOREVER),
1488                          NX_AZURE_IOT_SUCCESS);
1489 
1490     assert_int_not_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
1491     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1492                      NX_AZURE_IOT_SUCCESS);
1493 }
1494 
1495 /**
1496  * Test provisioning client registration blocking fails, if publish to service fails
1497  *
1498  **/
test_nx_azure_iot_provisioning_client_register_send_blocking_failed()1499 static VOID test_nx_azure_iot_provisioning_client_register_send_blocking_failed()
1500 {
1501     printf("test starts =>: %s\n", __func__);
1502     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
1503                                                                  g_endpoint, sizeof(g_endpoint) - 1,
1504                                                                  g_id_scope, sizeof(g_id_scope) - 1,
1505                                                                  g_reg_id, sizeof(g_reg_id) - 1,
1506                                                                  _nx_azure_iot_tls_supported_crypto,
1507                                                                  _nx_azure_iot_tls_supported_crypto_size,
1508                                                                  _nx_azure_iot_tls_ciphersuite_map,
1509                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
1510                                                                  metadata_buffer, sizeof(metadata_buffer),
1511                                                                  &root_ca_cert),
1512                      NX_AZURE_IOT_SUCCESS);
1513     g__wrap__nxd_mqtt_client_publish_packet_send_status = NX_NOT_SUCCESSFUL;
1514 
1515     assert_int_not_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, NX_WAIT_FOREVER),
1516                          NX_AZURE_IOT_SUCCESS);
1517 
1518     assert_int_not_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
1519     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1520                      NX_AZURE_IOT_SUCCESS);
1521 }
1522 
1523 /**
1524  * Test provisioning client registration blocking fails, if mqtt client disconnect while waiting for response
1525  *
1526  **/
test_nx_azure_iot_provisioning_client_register_receive_blocking_failed()1527 static VOID  test_nx_azure_iot_provisioning_client_register_receive_blocking_failed()
1528 {
1529     printf("test starts =>: %s\n", __func__);
1530     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
1531                                                                  g_endpoint, sizeof(g_endpoint) - 1,
1532                                                                  g_id_scope, sizeof(g_id_scope) - 1,
1533                                                                  g_reg_id, sizeof(g_reg_id) - 1,
1534                                                                  _nx_azure_iot_tls_supported_crypto,
1535                                                                  _nx_azure_iot_tls_supported_crypto_size,
1536                                                                  _nx_azure_iot_tls_ciphersuite_map,
1537                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
1538                                                                  metadata_buffer, sizeof(metadata_buffer),
1539                                                                  &root_ca_cert),
1540                      NX_AZURE_IOT_SUCCESS);
1541     assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
1542                                                                               on_complete_callback),
1543                      NX_AZURE_IOT_SUCCESS);
1544 
1545     assert_int_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, TEST_DEFAULT_WAIT_TIME),
1546                      NX_AZURE_IOT_PENDING);
1547 
1548     /* Expected callback function to be set. */
1549     assert_ptr_not_equal(test_connect_notify, NX_NULL);
1550     while (!g_done)
1551     {
1552         tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
1553 
1554         if (g_reg_id != 0)
1555         {
1556             /* Generate disconnect */
1557             test_connect_notify(&iot_prov_client.nx_azure_iot_provisioning_client_resource.resource_mqtt,
1558                                 NXD_MQTT_CONNECT_FAILURE, test_connect_notify_context);
1559         }
1560     }
1561 
1562     assert_int_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_DISCONNECTED);
1563     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1564                      NX_AZURE_IOT_SUCCESS);
1565 
1566 }
1567 
1568 /**
1569  * Test provisioning client registration blocking fails, if bad response received by client
1570  *
1571  **/
test_nx_azure_iot_provisioning_client_register_receive_bad_response_blocking_failed()1572 static VOID  test_nx_azure_iot_provisioning_client_register_receive_bad_response_blocking_failed()
1573 {
1574     printf("test starts =>: %s\n", __func__);
1575     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
1576                                                                  g_endpoint, sizeof(g_endpoint) - 1,
1577                                                                  g_id_scope, sizeof(g_id_scope) - 1,
1578                                                                  g_reg_id, sizeof(g_reg_id) - 1,
1579                                                                  _nx_azure_iot_tls_supported_crypto,
1580                                                                  _nx_azure_iot_tls_supported_crypto_size,
1581                                                                  _nx_azure_iot_tls_ciphersuite_map,
1582                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
1583                                                                  metadata_buffer, sizeof(metadata_buffer),
1584                                                                  &root_ca_cert),
1585                      NX_AZURE_IOT_SUCCESS);
1586     assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
1587                                                                               on_complete_callback),
1588                      NX_AZURE_IOT_SUCCESS);
1589 
1590     assert_int_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, TEST_DEFAULT_WAIT_TIME),
1591                      NX_AZURE_IOT_PENDING);
1592     while (!g_done)
1593     {
1594         generate_bad_response(&iot_prov_client);
1595         tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
1596     }
1597 
1598     assert_int_not_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
1599     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1600                      NX_AZURE_IOT_SUCCESS);
1601 }
1602 
1603 /**
1604  * Test provisioning client registration blocking fails, if bad JSON response received by client
1605  *
1606  **/
test_nx_azure_iot_provisioning_client_register_receive_invalid_json_blocking_failed()1607 static VOID  test_nx_azure_iot_provisioning_client_register_receive_invalid_json_blocking_failed()
1608 {
1609     printf("test starts =>: %s\n", __func__);
1610 
1611     for (INT index = 0; index < sizeof(invalid_response_data_set)/sizeof(invalid_response_data_set[0]); index++)
1612     {
1613         reset_global_state();
1614         assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
1615                                                                      g_endpoint, sizeof(g_endpoint) - 1,
1616                                                                      g_id_scope, sizeof(g_id_scope) - 1,
1617                                                                      g_reg_id, sizeof(g_reg_id) - 1,
1618                                                                      _nx_azure_iot_tls_supported_crypto,
1619                                                                      _nx_azure_iot_tls_supported_crypto_size,
1620                                                                      _nx_azure_iot_tls_ciphersuite_map,
1621                                                                      _nx_azure_iot_tls_ciphersuite_map_size,
1622                                                                      metadata_buffer, sizeof(metadata_buffer),
1623                                                                      &root_ca_cert),
1624                          NX_AZURE_IOT_SUCCESS);
1625         assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
1626                                                                                   on_complete_callback),
1627                          NX_AZURE_IOT_SUCCESS);
1628 
1629         assert_int_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, TEST_DEFAULT_WAIT_TIME),
1630                          NX_AZURE_IOT_PENDING);
1631 
1632         while (!g_done)
1633         {
1634             generate_response(&iot_prov_client,
1635                               PROVISIONING_SERVICE_REPONSE_TOPIC,
1636                               sizeof(PROVISIONING_SERVICE_REPONSE_TOPIC) - 1,
1637                               invalid_response_data_set[index], strlen(invalid_response_data_set[index]), 3);
1638             tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
1639         }
1640 
1641         assert_int_not_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
1642         assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1643                          NX_AZURE_IOT_SUCCESS);
1644     }
1645 }
1646 
1647 /**
1648  * Test provisioning client registration blocking succeeds
1649  *
1650  **/
test_nx_azure_iot_provisioning_client_register_blocking_success()1651 static VOID test_nx_azure_iot_provisioning_client_register_blocking_success()
1652 {
1653     printf("test starts =>: %s\n", __func__);
1654     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
1655                                                                  g_endpoint, sizeof(g_endpoint) - 1,
1656                                                                  g_id_scope, sizeof(g_id_scope) - 1,
1657                                                                  g_reg_id, sizeof(g_reg_id) - 1,
1658                                                                  _nx_azure_iot_tls_supported_crypto,
1659                                                                  _nx_azure_iot_tls_supported_crypto_size,
1660                                                                  _nx_azure_iot_tls_ciphersuite_map,
1661                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
1662                                                                  metadata_buffer, sizeof(metadata_buffer),
1663                                                                  &root_ca_cert),
1664                      NX_AZURE_IOT_SUCCESS);
1665     assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
1666                                                                               on_complete_callback),
1667                      NX_AZURE_IOT_SUCCESS);
1668 
1669     assert_int_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, TEST_DEFAULT_WAIT_TIME),
1670                      NX_AZURE_IOT_PENDING);
1671     while (!g_done)
1672     {
1673         generate_good_response(&iot_prov_client);
1674         tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
1675     }
1676 
1677     assert_int_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
1678     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1679                      NX_AZURE_IOT_SUCCESS);
1680 }
1681 
1682 /**
1683  * Test provisioning client registration blocking succeeds, when multiple request required.
1684  *
1685  **/
test_nx_azure_iot_provisioning_client_register_with_multiple_response_blocking_success()1686 static VOID test_nx_azure_iot_provisioning_client_register_with_multiple_response_blocking_success()
1687 {
1688     printf("test starts =>: %s\n", __func__);
1689     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
1690                                                                  g_endpoint, sizeof(g_endpoint) - 1,
1691                                                                  g_id_scope, sizeof(g_id_scope) - 1,
1692                                                                  g_reg_id, sizeof(g_reg_id) - 1,
1693                                                                  _nx_azure_iot_tls_supported_crypto,
1694                                                                  _nx_azure_iot_tls_supported_crypto_size,
1695                                                                  _nx_azure_iot_tls_ciphersuite_map,
1696                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
1697                                                                  metadata_buffer, sizeof(metadata_buffer),
1698                                                                  &root_ca_cert),
1699                      NX_AZURE_IOT_SUCCESS);
1700     assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
1701                                                                               on_complete_callback),
1702                      NX_AZURE_IOT_SUCCESS);
1703 
1704     g_assigned_response_after = 3;
1705     assert_int_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, TEST_DEFAULT_WAIT_TIME),
1706                      NX_AZURE_IOT_PENDING);
1707     while (!g_done)
1708     {
1709         generate_good_response(&iot_prov_client);
1710         tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
1711     }
1712 
1713     assert_int_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
1714     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1715                      NX_AZURE_IOT_SUCCESS);
1716 }
1717 
1718 /**
1719  * Test provisioning client registration fails, if mqtt client disconnect while waiting for response
1720  *
1721  **/
test_nx_azure_iot_provisioning_client_register_disconnect_failed()1722 static VOID  test_nx_azure_iot_provisioning_client_register_disconnect_failed()
1723 {
1724     printf("test starts =>: %s\n", __func__);
1725     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
1726                                                                  g_endpoint, sizeof(g_endpoint) - 1,
1727                                                                  g_id_scope, sizeof(g_id_scope) - 1,
1728                                                                  g_reg_id, sizeof(g_reg_id) - 1,
1729                                                                  _nx_azure_iot_tls_supported_crypto,
1730                                                                  _nx_azure_iot_tls_supported_crypto_size,
1731                                                                  _nx_azure_iot_tls_ciphersuite_map,
1732                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
1733                                                                  metadata_buffer, sizeof(metadata_buffer),
1734                                                                  &root_ca_cert),
1735                      NX_AZURE_IOT_SUCCESS);
1736     assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
1737                                                                               on_complete_callback),
1738                      NX_AZURE_IOT_SUCCESS);
1739 
1740     assert_int_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, 0),
1741                      NX_AZURE_IOT_PENDING);
1742 
1743     /* Expected callback function to be set. */
1744     assert_ptr_not_equal(test_connect_notify, NX_NULL);
1745     while (!g_done)
1746     {
1747         tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
1748 
1749         if (g_reg_id != 0)
1750         {
1751             /* Generate disconnect */
1752             test_disconnect_notify(&iot_prov_client.nx_azure_iot_provisioning_client_resource.resource_mqtt);
1753         }
1754     }
1755 
1756     assert_int_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_DISCONNECTED);
1757     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1758                      NX_AZURE_IOT_SUCCESS);
1759 
1760 }
1761 
1762 /**
1763  * Test provisioning client iothub device get fails if invalid arguments passed.
1764  *
1765  **/
test_nx_azure_iot_provisioning_client_iothub_device_info_invalid_arguments_failed()1766 static VOID test_nx_azure_iot_provisioning_client_iothub_device_info_invalid_arguments_failed()
1767 {
1768 UINT iothub_hostname_len = 0;
1769 UINT device_id_len =  0;
1770 
1771     printf("test starts =>: %s\n", __func__);
1772     assert_int_not_equal(nx_azure_iot_provisioning_client_iothub_device_info_get(NX_NULL,
1773                                                                                  iothub_hostname, &iothub_hostname_len,
1774                                                                                  iothub_device_id, &device_id_len),
1775                          NX_AZURE_IOT_SUCCESS);
1776 
1777     assert_int_not_equal(nx_azure_iot_provisioning_client_iothub_device_info_get(&iot_prov_client,
1778                                                                                  NX_NULL, &iothub_hostname_len,
1779                                                                                  iothub_device_id, &device_id_len),
1780                          NX_AZURE_IOT_SUCCESS);
1781 
1782 
1783     assert_int_not_equal(nx_azure_iot_provisioning_client_iothub_device_info_get(&iot_prov_client,
1784                                                                                  iothub_hostname, &iothub_hostname_len,
1785                                                                                  NX_NULL, &device_id_len),
1786                          NX_AZURE_IOT_SUCCESS);
1787 }
1788 
1789 /**
1790  * Test provisioning client iothub device get fails if buffers are too small.
1791  *
1792  **/
test_nx_azure_iot_provisioning_client_iothub_device_info_invalid_state_failed()1793 static VOID test_nx_azure_iot_provisioning_client_iothub_device_info_invalid_state_failed()
1794 {
1795 UINT iothub_hostname_len = sizeof(iothub_hostname);
1796 UINT device_id_len =  sizeof(iothub_device_id);
1797 
1798     printf("test starts =>: %s\n", __func__);
1799     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
1800                                                                  g_endpoint, sizeof(g_endpoint) - 1,
1801                                                                  g_id_scope, sizeof(g_id_scope) - 1,
1802                                                                  g_reg_id, sizeof(g_reg_id) - 1,
1803                                                                  _nx_azure_iot_tls_supported_crypto,
1804                                                                  _nx_azure_iot_tls_supported_crypto_size,
1805                                                                  _nx_azure_iot_tls_ciphersuite_map,
1806                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
1807                                                                  metadata_buffer, sizeof(metadata_buffer),
1808                                                                  &root_ca_cert),
1809                      NX_AZURE_IOT_SUCCESS);
1810     assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
1811                                                                               on_complete_callback),
1812                      NX_AZURE_IOT_SUCCESS);
1813 
1814     assert_int_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, TEST_DEFAULT_WAIT_TIME),
1815                      NX_AZURE_IOT_PENDING);
1816     assert_int_not_equal(nx_azure_iot_provisioning_client_iothub_device_info_get(&iot_prov_client,
1817                                                                                  iothub_hostname, &iothub_hostname_len,
1818                                                                                  iothub_device_id, &device_id_len),
1819                          NX_AZURE_IOT_SUCCESS);
1820     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1821                      NX_AZURE_IOT_SUCCESS);
1822 }
1823 
1824 /**
1825  * Test provisioning client iothub device get fails if buffers are too small.
1826  *
1827  **/
test_nx_azure_iot_provisioning_client_iothub_device_info_oom_failed()1828 static VOID test_nx_azure_iot_provisioning_client_iothub_device_info_oom_failed()
1829 {
1830 UINT iothub_hostname_len = 0;
1831 UINT device_id_len =  0;
1832 
1833     printf("test starts =>: %s\n", __func__);
1834     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
1835                                                                  g_endpoint, sizeof(g_endpoint) - 1,
1836                                                                  g_id_scope, sizeof(g_id_scope) - 1,
1837                                                                  g_reg_id, sizeof(g_reg_id) - 1,
1838                                                                  _nx_azure_iot_tls_supported_crypto,
1839                                                                  _nx_azure_iot_tls_supported_crypto_size,
1840                                                                  _nx_azure_iot_tls_ciphersuite_map,
1841                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
1842                                                                  metadata_buffer, sizeof(metadata_buffer),
1843                                                                  &root_ca_cert),
1844                      NX_AZURE_IOT_SUCCESS);
1845     assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
1846                                                                               on_complete_callback),
1847                      NX_AZURE_IOT_SUCCESS);
1848 
1849     assert_int_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, TEST_DEFAULT_WAIT_TIME),
1850                      NX_AZURE_IOT_PENDING);
1851     while (!g_done)
1852     {
1853         generate_good_response(&iot_prov_client);
1854         tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
1855     }
1856 
1857     assert_int_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
1858     assert_int_not_equal(nx_azure_iot_provisioning_client_iothub_device_info_get(&iot_prov_client,
1859                                                                                  iothub_hostname, &iothub_hostname_len,
1860                                                                                  iothub_device_id, &device_id_len),
1861                          NX_AZURE_IOT_SUCCESS);
1862     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1863                      NX_AZURE_IOT_SUCCESS);
1864 }
1865 
1866 /**
1867  * Test provisioning client iothub device get succeeds.
1868  *
1869  **/
test_nx_azure_iot_provisioning_client_iothub_device_info_success()1870 static VOID test_nx_azure_iot_provisioning_client_iothub_device_info_success()
1871 {
1872 UINT iothub_hostname_len = sizeof(iothub_hostname);
1873 UINT device_id_len =  sizeof(iothub_device_id);
1874 
1875     printf("test starts =>: %s\n", __func__);
1876     assert_int_equal(nx_azure_iot_provisioning_client_initialize(&iot_prov_client, &iot,
1877                                                                  g_endpoint, sizeof(g_endpoint) - 1,
1878                                                                  g_id_scope, sizeof(g_id_scope) - 1,
1879                                                                  g_reg_id, sizeof(g_reg_id) - 1,
1880                                                                  _nx_azure_iot_tls_supported_crypto,
1881                                                                  _nx_azure_iot_tls_supported_crypto_size,
1882                                                                  _nx_azure_iot_tls_ciphersuite_map,
1883                                                                  _nx_azure_iot_tls_ciphersuite_map_size,
1884                                                                  metadata_buffer, sizeof(metadata_buffer),
1885                                                                  &root_ca_cert),
1886                      NX_AZURE_IOT_SUCCESS);
1887     assert_int_equal(nx_azure_iot_provisioning_client_completion_callback_set(&iot_prov_client,
1888                                                                               on_complete_callback),
1889                      NX_AZURE_IOT_SUCCESS);
1890 
1891     assert_int_equal(nx_azure_iot_provisioning_client_register(&iot_prov_client, TEST_DEFAULT_WAIT_TIME),
1892                      NX_AZURE_IOT_PENDING);
1893     while (!g_done)
1894     {
1895         generate_good_response(&iot_prov_client);
1896         tx_thread_sleep(NX_IP_PERIODIC_RATE / 10);
1897     }
1898 
1899     assert_int_equal(iot_prov_client.nx_azure_iot_provisioning_client_result, NX_AZURE_IOT_SUCCESS);
1900     assert_int_equal(nx_azure_iot_provisioning_client_iothub_device_info_get(&iot_prov_client,
1901                                                                              iothub_hostname, &iothub_hostname_len,
1902                                                                              iothub_device_id, &device_id_len),
1903                      NX_AZURE_IOT_SUCCESS);
1904     assert_memory_equal(iothub_hostname, assigned_hub_name, strlen(assigned_hub_name));
1905     assert_memory_equal(iothub_device_id, assigned_device_id, strlen(assigned_device_id));
1906     assert_int_equal(nx_azure_iot_provisioning_client_deinitialize(&iot_prov_client),
1907                      NX_AZURE_IOT_SUCCESS);
1908 }
1909 
demo_entry(NX_IP * ip_ptr,NX_PACKET_POOL * pool_ptr,NX_DNS * dns_ptr,UINT (* unix_time_callback)(ULONG * unix_time))1910 VOID demo_entry(NX_IP* ip_ptr, NX_PACKET_POOL* pool_ptr, NX_DNS* dns_ptr, UINT (*unix_time_callback)(ULONG *unix_time))
1911 {
1912     NX_AZURE_TEST_FN tests[] = { test_nx_azure_iot_provisioning_client_initialize_invalid_arguments,
1913                                test_nx_azure_iot_provisioning_client_device_cert_set_invalid_arguments,
1914                                test_nx_azure_iot_provisioning_client_symmetric_key_set_invalid_arguments,
1915                                test_nx_azure_iot_provisioning_client_register_invalid_arguments,
1916                                test_nx_azure_iot_provisioning_client_completion_callback_set_invalid_arguments,
1917                                test_nx_azure_iot_provisioning_client_deinitialize_invalid_arguments,
1918                                test_nx_azure_iot_provisioning_client_registration_payload_set_invalid_arguments,
1919                                test_nx_azure_iot_provisioning_client_register_without_initialization_fail,
1920                                test_nx_azure_iot_provisioning_client_init_success,
1921                                test_nx_azure_iot_provisioning_client_register_dns_failed,
1922                                test_nx_azure_iot_provisioning_client_register_subscribe_failed,
1923                                test_nx_azure_iot_provisioning_client_register_send_failed,
1924                                test_nx_azure_iot_provisioning_client_register_receive_failed,
1925                                test_nx_azure_iot_provisioning_client_register_receive_bad_response_failed,
1926                                test_nx_azure_iot_provisioning_client_register_receive_invalid_json_failed,
1927                                test_nx_azure_iot_provisioning_client_register_allocation_fail_failure,
1928                                test_nx_azure_iot_provisioning_client_register_append_data_fail_failure,
1929                                test_nx_azure_iot_provisioning_client_register_success,
1930                                test_nx_azure_iot_provisioning_client_register_failure_failed,
1931                                test_nx_azure_iot_provisioning_client_register_with_payload_success,
1932                                test_nx_azure_iot_provisioning_client_register_with_throttle_response_success,
1933                                test_nx_azure_iot_provisioning_client_register_with_throttle_response_2_success,
1934                                test_nx_azure_iot_provisioning_client_register_with_multiple_response_success,
1935                                test_nx_azure_iot_provisioning_client_register_dns_blocking_failed,
1936                                test_nx_azure_iot_provisioning_client_register_subscribe_blocking_failed,
1937                                test_nx_azure_iot_provisioning_client_register_send_blocking_failed,
1938                                test_nx_azure_iot_provisioning_client_register_receive_blocking_failed,
1939                                test_nx_azure_iot_provisioning_client_register_receive_bad_response_blocking_failed,
1940                                test_nx_azure_iot_provisioning_client_register_receive_invalid_json_blocking_failed,
1941                                test_nx_azure_iot_provisioning_client_register_blocking_success,
1942                                test_nx_azure_iot_provisioning_client_register_with_multiple_response_blocking_success,
1943                                test_nx_azure_iot_provisioning_client_register_disconnect_failed,
1944                                test_nx_azure_iot_provisioning_client_iothub_device_info_invalid_arguments_failed,
1945                                test_nx_azure_iot_provisioning_client_iothub_device_info_invalid_state_failed,
1946                                test_nx_azure_iot_provisioning_client_iothub_device_info_oom_failed,
1947                                test_nx_azure_iot_provisioning_client_iothub_device_info_success };
1948     INT number_of_tests =  sizeof(tests)/sizeof(tests[0]);
1949     g_ip_ptr = ip_ptr;
1950     g_pool_ptr = pool_ptr;
1951     g_dns_ptr = dns_ptr;
1952 
1953     test_suit_begin();
1954 
1955     printf("Number of tests %d\r\n", number_of_tests);
1956     for (INT index = 0; index < number_of_tests; index++)
1957     {
1958         test_begin();
1959         tests[index]();
1960         test_end();
1961     }
1962 
1963     test_suit_end();
1964 }
1965