1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
/***************************************************************************
* Copyright (c) 2024 Microsoft Corporation
* Copyright (c) 2025-present Eclipse ThreadX Contributors
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: MIT
**************************************************************************/
#include <stdio.h>
#include "nx_api.h"
#include "nx_azure_iot_hub_client.h"
#include "nx_azure_iot_cert.h"
#include "nx_azure_iot_ciphersuites.h"
#define DEMO_DHCP_DISABLE
#define DEMO_IPV4_ADDRESS IP_ADDRESS(192, 168, 100, 33)
#define DEMO_IPV4_MASK 0xFFFFFF00UL
#define DEMO_GATEWAY_ADDRESS IP_ADDRESS(192, 168, 100, 1)
#define DEMO_DNS_SERVER_ADDRESS IP_ADDRESS(192, 168, 100, 1)
#define NETWORK_DRIVER _nx_ram_network_driver
/* Include main.c in the test case since we need to disable DHCP in this test. */
#include "main.c"
#define STRING_UNSIGNED_ARGS(s) (UCHAR *)s, strlen(s)
#ifndef DEMO_CLOUD_STACK_SIZE
#define DEMO_CLOUD_STACK_SIZE 2048
#endif /* DEMO_CLOUD_STACK_SIZE */
#ifndef DEMO_CLOUD_THREAD_PRIORITY
#define DEMO_CLOUD_THREAD_PRIORITY (4)
#endif /* DEMO_CLOUD_THREAD_PRIORITY */
UINT __wrap__nxde_mqtt_client_delete(NXD_MQTT_CLIENT *client_ptr);
UINT __wrap__nxde_mqtt_client_receive_notify_set(NXD_MQTT_CLIENT *client_ptr,
VOID (*receive_notify)(NXD_MQTT_CLIENT *client_ptr,
UINT message_count));
static NX_AZURE_IOT iot;
static NX_AZURE_IOT_HUB_CLIENT iot_client;
static NX_AZURE_IOT_HUB_CLIENT iot_clients[3];
static NX_SECURE_X509_CERT root_ca_cert;
static UCHAR metadata_buffer[NX_AZURE_IOT_TLS_METADATA_BUFFER_SIZE];
static ULONG demo_cloud_thread_stack[DEMO_CLOUD_STACK_SIZE / sizeof(ULONG)];
extern int g_argc;
extern char **g_argv;
extern NX_AZURE_IOT *_nx_azure_iot_created_ptr;
VOID demo_entry(NX_IP* ip_ptr, NX_PACKET_POOL* pool_ptr, NX_DNS* dns_ptr, UINT (*unix_time_callback)(ULONG *unix_time))
{
CHAR *host_name = "host_name";
CHAR *device_id = "device_id";
CHAR *module_id = "module_id";
CHAR *symmetric_key = "symmetric_key";
UINT i;
/* Initialize root certificate. */
assert_int_equal(nx_secure_x509_certificate_initialize(&root_ca_cert, (UCHAR *)_nx_azure_iot_root_cert, (USHORT)_nx_azure_iot_root_cert_size,
NX_NULL, 0, NULL, 0, NX_SECURE_X509_KEY_TYPE_NONE),
NX_AZURE_IOT_SUCCESS);
/*********** Test nx_azure_iot_create() ***********/
/* FAIL: NX_AZURE_IOT pointer is null. */
assert_int_not_equal(nx_azure_iot_create(NX_NULL, (UCHAR *)"Azure IoT", ip_ptr, pool_ptr, dns_ptr, (UCHAR *)demo_cloud_thread_stack,
sizeof(demo_cloud_thread_stack), DEMO_CLOUD_THREAD_PRIORITY, unix_time_callback),
NX_AZURE_IOT_SUCCESS);
/* FAIL: NX_IP pointer is null. */
assert_int_not_equal(nx_azure_iot_create(&iot, (UCHAR *)"Azure IoT", NX_NULL, pool_ptr, dns_ptr, (UCHAR *)demo_cloud_thread_stack,
sizeof(demo_cloud_thread_stack), DEMO_CLOUD_THREAD_PRIORITY, unix_time_callback),
NX_AZURE_IOT_SUCCESS);
/* FAIL: NX_PACKET_POOL pointer is null. */
assert_int_not_equal(nx_azure_iot_create(&iot, (UCHAR *)"Azure IoT", ip_ptr, NX_NULL, dns_ptr, (UCHAR *)demo_cloud_thread_stack,
sizeof(demo_cloud_thread_stack), DEMO_CLOUD_THREAD_PRIORITY, unix_time_callback),
NX_AZURE_IOT_SUCCESS);
/* FAIL: NX_DNS pointer is null. */
assert_int_not_equal(nx_azure_iot_create(&iot, (UCHAR *)"Azure IoT", ip_ptr, pool_ptr, NX_NULL, (UCHAR *)demo_cloud_thread_stack,
sizeof(demo_cloud_thread_stack), DEMO_CLOUD_THREAD_PRIORITY, unix_time_callback),
NX_AZURE_IOT_SUCCESS);
/* FAIL: stack pointer is null. */
assert_int_not_equal(nx_azure_iot_create(&iot, (UCHAR *)"Azure IoT", ip_ptr, pool_ptr, dns_ptr, NX_NULL,
sizeof(demo_cloud_thread_stack), DEMO_CLOUD_THREAD_PRIORITY, unix_time_callback),
NX_AZURE_IOT_SUCCESS);
/* FAIL: stack size is 0. */
assert_int_not_equal(nx_azure_iot_create(&iot, (UCHAR *)"Azure IoT", ip_ptr, pool_ptr, dns_ptr, (UCHAR *)demo_cloud_thread_stack,
0, DEMO_CLOUD_THREAD_PRIORITY, unix_time_callback),
NX_AZURE_IOT_SUCCESS);
/* SUCCESS: all parameters are valid. */
assert_int_equal(nx_azure_iot_create(&iot, (UCHAR *)"Azure IoT", ip_ptr, pool_ptr, dns_ptr, (UCHAR *)demo_cloud_thread_stack,
sizeof(demo_cloud_thread_stack), DEMO_CLOUD_THREAD_PRIORITY, unix_time_callback),
NX_AZURE_IOT_SUCCESS);
/*********** Test nx_azure_iot_delete() ***********/
/* FAIL: NX_AZURE_IOT pointer is null. */
assert_int_not_equal(nx_azure_iot_delete(NX_NULL), NX_AZURE_IOT_SUCCESS);
/* FAIL: nx_cloud_delete fail. */
/* Manually set nx_cloud_modules_count to none zero. */
iot.nx_azure_iot_cloud.nx_cloud_modules_count++;
assert_int_not_equal(nx_azure_iot_delete(&iot), NX_AZURE_IOT_SUCCESS);
iot.nx_azure_iot_cloud.nx_cloud_modules_count--;
/* SUCCESS: iot is created. */
assert_int_equal(nx_azure_iot_delete(&iot), NX_AZURE_IOT_SUCCESS);
/*********** Test nx_azure_iot_hub_client_initialize() ***********/
assert_int_equal(nx_azure_iot_create(&iot, (UCHAR *)"Azure IoT", ip_ptr, pool_ptr, dns_ptr, (UCHAR *)demo_cloud_thread_stack,
sizeof(demo_cloud_thread_stack), DEMO_CLOUD_THREAD_PRIORITY, unix_time_callback),
NX_AZURE_IOT_SUCCESS);
/* FAIL: NX_AZURE_IOT pointer is null. */
assert_int_not_equal(nx_azure_iot_hub_client_initialize(&iot_client, NX_NULL,
STRING_UNSIGNED_ARGS(host_name),
STRING_UNSIGNED_ARGS(device_id),
STRING_UNSIGNED_ARGS(module_id),
_nx_azure_iot_tls_supported_crypto,
_nx_azure_iot_tls_supported_crypto_size,
_nx_azure_iot_tls_ciphersuite_map,
_nx_azure_iot_tls_ciphersuite_map_size,
metadata_buffer, sizeof(metadata_buffer),
&root_ca_cert),
NX_AZURE_IOT_SUCCESS);
/* FAIL: NX_AZURE_IOT_HUB_CLIENT pointer is null. */
assert_int_not_equal(nx_azure_iot_hub_client_initialize(NX_NULL, &iot,
STRING_UNSIGNED_ARGS(host_name),
STRING_UNSIGNED_ARGS(device_id),
STRING_UNSIGNED_ARGS(module_id),
_nx_azure_iot_tls_supported_crypto,
_nx_azure_iot_tls_supported_crypto_size,
_nx_azure_iot_tls_ciphersuite_map,
_nx_azure_iot_tls_ciphersuite_map_size,
metadata_buffer, sizeof(metadata_buffer),
&root_ca_cert),
NX_AZURE_IOT_SUCCESS);
/* FAIL: host_name pointer is null. */
assert_int_not_equal(nx_azure_iot_hub_client_initialize(&iot_client, &iot,
NX_NULL, 0,
STRING_UNSIGNED_ARGS(device_id),
STRING_UNSIGNED_ARGS(module_id),
_nx_azure_iot_tls_supported_crypto,
_nx_azure_iot_tls_supported_crypto_size,
_nx_azure_iot_tls_ciphersuite_map,
_nx_azure_iot_tls_ciphersuite_map_size,
metadata_buffer, sizeof(metadata_buffer),
&root_ca_cert),
NX_AZURE_IOT_SUCCESS);
/* FAIL: device_id pointer is null. */
assert_int_not_equal(nx_azure_iot_hub_client_initialize(&iot_client, &iot,
STRING_UNSIGNED_ARGS(host_name),
NX_NULL, 0,
STRING_UNSIGNED_ARGS(module_id),
_nx_azure_iot_tls_supported_crypto,
_nx_azure_iot_tls_supported_crypto_size,
_nx_azure_iot_tls_ciphersuite_map,
_nx_azure_iot_tls_ciphersuite_map_size,
metadata_buffer, sizeof(metadata_buffer),
&root_ca_cert),
NX_AZURE_IOT_SUCCESS);
/* FAIL: host_name_length is 0. */
assert_int_not_equal(nx_azure_iot_hub_client_initialize(&iot_client, &iot,
"", 0,
STRING_UNSIGNED_ARGS(device_id),
STRING_UNSIGNED_ARGS(module_id),
_nx_azure_iot_tls_supported_crypto,
_nx_azure_iot_tls_supported_crypto_size,
_nx_azure_iot_tls_ciphersuite_map,
_nx_azure_iot_tls_ciphersuite_map_size,
metadata_buffer, sizeof(metadata_buffer),
&root_ca_cert),
NX_AZURE_IOT_SUCCESS);
/* FAIL: device_id_length is 0. */
assert_int_not_equal(nx_azure_iot_hub_client_initialize(&iot_client, &iot,
STRING_UNSIGNED_ARGS(host_name),
"", 0,
STRING_UNSIGNED_ARGS(module_id),
_nx_azure_iot_tls_supported_crypto,
_nx_azure_iot_tls_supported_crypto_size,
_nx_azure_iot_tls_ciphersuite_map,
_nx_azure_iot_tls_ciphersuite_map_size,
metadata_buffer, sizeof(metadata_buffer),
&root_ca_cert),
NX_AZURE_IOT_SUCCESS);
/* FAIL: invalidate cloud_id manually. */
iot.nx_azure_iot_cloud.nx_cloud_id = 0;
assert_int_not_equal(nx_azure_iot_hub_client_initialize(&iot_client, &iot,
STRING_UNSIGNED_ARGS(host_name),
STRING_UNSIGNED_ARGS(device_id),
STRING_UNSIGNED_ARGS(module_id),
_nx_azure_iot_tls_supported_crypto,
_nx_azure_iot_tls_supported_crypto_size,
_nx_azure_iot_tls_ciphersuite_map,
_nx_azure_iot_tls_ciphersuite_map_size,
metadata_buffer, sizeof(metadata_buffer),
&root_ca_cert),
NX_AZURE_IOT_SUCCESS);
iot.nx_azure_iot_cloud.nx_cloud_id = NX_CLOUD_ID;
/* FAIL: nxd_mqtt_client_receive_notify_set will fail by hijacked function. */
will_return(__wrap__nxde_mqtt_client_receive_notify_set, NXD_MQTT_INVALID_PARAMETER);
will_return(__wrap__nxde_mqtt_client_delete, NXD_MQTT_SUCCESS);
assert_int_not_equal(nx_azure_iot_hub_client_initialize(&iot_client, &iot,
STRING_UNSIGNED_ARGS(host_name),
STRING_UNSIGNED_ARGS(device_id),
STRING_UNSIGNED_ARGS(module_id),
_nx_azure_iot_tls_supported_crypto,
_nx_azure_iot_tls_supported_crypto_size,
_nx_azure_iot_tls_ciphersuite_map,
_nx_azure_iot_tls_ciphersuite_map_size,
metadata_buffer, sizeof(metadata_buffer),
&root_ca_cert),
NX_AZURE_IOT_SUCCESS);
/* SUCCESS: all parameters are valid. */
will_return_always(__wrap__nxde_mqtt_client_receive_notify_set, NXD_MQTT_SUCCESS);
will_return(__wrap__nxde_mqtt_client_delete, NXD_MQTT_SUCCESS);
assert_int_equal(nx_azure_iot_hub_client_initialize(&iot_client, &iot,
STRING_UNSIGNED_ARGS(host_name),
STRING_UNSIGNED_ARGS(device_id),
STRING_UNSIGNED_ARGS(module_id),
_nx_azure_iot_tls_supported_crypto,
_nx_azure_iot_tls_supported_crypto_size,
_nx_azure_iot_tls_ciphersuite_map,
_nx_azure_iot_tls_ciphersuite_map_size,
metadata_buffer, sizeof(metadata_buffer),
&root_ca_cert),
NX_AZURE_IOT_SUCCESS);
assert_int_equal(nx_azure_iot_hub_client_deinitialize(&iot_client),
NX_AZURE_IOT_SUCCESS);
/* SUCCESS: all parameters are valid and module_id pointer is null. */
assert_int_equal(nx_azure_iot_hub_client_initialize(&iot_client, &iot,
STRING_UNSIGNED_ARGS(host_name),
STRING_UNSIGNED_ARGS(device_id),
NX_NULL, 0,
_nx_azure_iot_tls_supported_crypto,
_nx_azure_iot_tls_supported_crypto_size,
_nx_azure_iot_tls_ciphersuite_map,
_nx_azure_iot_tls_ciphersuite_map_size,
metadata_buffer, sizeof(metadata_buffer),
&root_ca_cert),
NX_AZURE_IOT_SUCCESS);
will_return(__wrap__nxde_mqtt_client_delete, NXD_MQTT_SUCCESS);
assert_int_equal(nx_azure_iot_hub_client_deinitialize(&iot_client),
NX_AZURE_IOT_SUCCESS);
/* SUCCESS: all parameters are valid and module_id pointer is null. */
assert_int_equal(nx_azure_iot_hub_client_initialize(&iot_client, &iot,
STRING_UNSIGNED_ARGS(host_name),
STRING_UNSIGNED_ARGS(device_id),
NX_NULL, 0,
_nx_azure_iot_tls_supported_crypto,
_nx_azure_iot_tls_supported_crypto_size,
_nx_azure_iot_tls_ciphersuite_map,
_nx_azure_iot_tls_ciphersuite_map_size,
metadata_buffer, sizeof(metadata_buffer),
&root_ca_cert),
NX_AZURE_IOT_SUCCESS);
/*********** Test nx_azure_iot_hub_client_deinitialize() ***********/
/* FAIL: NX_AZURE_IOT_HUB_CLIENT pointer is null. */
assert_int_not_equal(nx_azure_iot_hub_client_deinitialize(NX_NULL),
NX_AZURE_IOT_SUCCESS);
/* FAIL: iot_client is not deleted yet. */
assert_int_not_equal(nx_azure_iot_delete(&iot), NX_AZURE_IOT_SUCCESS);
/* FAIL: nxd_mqtt_client_delete will fail by hijacked function. */
will_return(__wrap__nxde_mqtt_client_delete, NXD_MQTT_INVALID_PARAMETER);
assert_int_not_equal(nx_azure_iot_hub_client_deinitialize(&iot_client),
NX_AZURE_IOT_SUCCESS);
/* SUCCESS: iot_client is created. */
will_return_always(__wrap__nxde_mqtt_client_delete, NXD_MQTT_SUCCESS);
assert_int_equal(nx_azure_iot_hub_client_deinitialize(&iot_client),
NX_AZURE_IOT_SUCCESS);
/* FAIL: iot_client is already deleted. */
assert_int_not_equal(nx_azure_iot_hub_client_deinitialize(&iot_client),
NX_AZURE_IOT_SUCCESS);
/*********** Test multiple nx_azure_iot_hub_client_initialize()/deinitialize() ***********/
for (i = 0; i < sizeof(iot_clients) / sizeof(NX_AZURE_IOT_HUB_CLIENT); i++)
{
assert_int_equal(nx_azure_iot_hub_client_initialize(&iot_clients[i], &iot,
STRING_UNSIGNED_ARGS(host_name),
STRING_UNSIGNED_ARGS(device_id),
NX_NULL, 0,
_nx_azure_iot_tls_supported_crypto,
_nx_azure_iot_tls_supported_crypto_size,
_nx_azure_iot_tls_ciphersuite_map,
_nx_azure_iot_tls_ciphersuite_map_size,
metadata_buffer, sizeof(metadata_buffer),
&root_ca_cert),
NX_AZURE_IOT_SUCCESS);
}
/* Manually remove the header of nx_azure_iot_hub_client_list_header. */
/* FAIL: this one removed from nx_azure_iot_hub_client_list_header. */
_nx_azure_iot_created_ptr -> nx_azure_iot_resource_list_header = _nx_azure_iot_created_ptr -> nx_azure_iot_resource_list_header -> resource_next;
assert_int_not_equal(nx_azure_iot_hub_client_deinitialize(&iot_clients[sizeof(iot_clients) / sizeof(NX_AZURE_IOT_HUB_CLIENT) - 1]),
NX_AZURE_IOT_SUCCESS);
for (i = 0; i < sizeof(iot_clients) / sizeof(NX_AZURE_IOT_HUB_CLIENT) - 1; i++)
{
assert_int_equal(nx_azure_iot_hub_client_deinitialize(&iot_clients[i]),
NX_AZURE_IOT_SUCCESS);
}
/* SUCCESS: iot is created. */
assert_int_equal(nx_azure_iot_delete(&iot), NX_AZURE_IOT_SUCCESS);
/* Check if all the packet are released */
assert_int_equal(pool_ptr -> nx_packet_pool_available, pool_ptr -> nx_packet_pool_total);
}
UINT __real__nxde_mqtt_client_receive_notify_set(NXD_MQTT_CLIENT *client_ptr,
VOID (*receive_notify)(NXD_MQTT_CLIENT *client_ptr,
UINT message_count));
UINT __wrap__nxde_mqtt_client_receive_notify_set(NXD_MQTT_CLIENT *client_ptr,
VOID (*receive_notify)(NXD_MQTT_CLIENT *client_ptr,
UINT message_count))
{
if ((UINT)mock() != NXD_MQTT_SUCCESS)
{
printf("HIJACKED: %s\n", __func__);
return(NXD_MQTT_INVALID_PARAMETER);
}
return(__real__nxde_mqtt_client_receive_notify_set(client_ptr, receive_notify));
}
UINT __real__nxde_mqtt_client_delete(NXD_MQTT_CLIENT *client_ptr);
UINT __wrap__nxde_mqtt_client_delete(NXD_MQTT_CLIENT *client_ptr)
{
if ((UINT)mock() != NXD_MQTT_SUCCESS)
{
printf("HIJACKED: %s\n", __func__);
return(NXD_MQTT_INVALID_PARAMETER);
}
return(__real__nxde_mqtt_client_delete(client_ptr));
}
|