summaryrefslogtreecommitdiff
path: root/test/regression/azure_iot/user_agent_string_unit_test.c
blob: 6f3cfadc251d4cacc3801862dc5ed5e426c10ce7 (plain)
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
/***************************************************************************
 * 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 <setjmp.h>
#include <cmocka.h>  /* macros: https://api.cmocka.org/group__cmocka__asserts.html */

#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 */

static UINT interface_type_check(ULONG interface_type);
static UINT deplete_packets(NX_PACKET_POOL *pool_ptr, UINT remaining_packets);
static VOID release_packets(NX_PACKET_POOL *pool_ptr, UINT count);

static NX_AZURE_IOT iot;
static NX_AZURE_IOT_HUB_CLIENT iot_client;
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)];
static NX_PACKET *allocated_packets[256];
static UCHAR large_property_name[2048];
static ULONG small_pool_stack[1024 >> 2];
static NX_PACKET_POOL small_pool;
extern int g_argc;
extern char **g_argv;

ULONG g_interface_type = 0;

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";
CHAR property_name[] = "property_name";
CHAR property_value[] = "property_value";
NX_PACKET *packet_ptr;
UINT count;
ULONG pool_ptr_available_packet;
ULONG small_pool_available_packet;

    assert_int_equal(nx_packet_pool_create(&small_pool, "Small Packet Pool", 4,
                                           (UCHAR *)small_pool_stack , sizeof(small_pool_stack)),
                     NX_AZURE_IOT_SUCCESS);

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

    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);

    /* Record number of available packet before test */
    pool_ptr_available_packet = pool_ptr -> nx_packet_pool_available;
    small_pool_available_packet = small_pool.nx_packet_pool_available;

    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(""),
                                                        _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 to set the type.  */
    g_interface_type = NX_INTERFACE_TYPE_UNKNOWN;
    will_return(__wrap__nxe_ip_driver_interface_direct_command, NX_NOT_SUCCESSFUL);

    assert_int_equal(nx_azure_iot_hub_client_connect(&iot_client, NX_TRUE, NX_IP_PERIODIC_RATE),
                     NX_AZURE_IOT_SUCCESS);
    
    /* The interface type should not be updated.  */
    assert_int_equal(interface_type_check(NX_INTERFACE_TYPE_UNKNOWN), NX_AZURE_IOT_SUCCESS);

    nx_azure_iot_hub_client_disconnect(&iot_client);

    /* Set the type as NX_INTERFACE_TYPE_OTHER.  */
    g_interface_type = NX_INTERFACE_TYPE_OTHER;
    will_return(__wrap__nxe_ip_driver_interface_direct_command, NX_SUCCESS);

    assert_int_equal(nx_azure_iot_hub_client_connect(&iot_client, NX_TRUE, NX_IP_PERIODIC_RATE),
                     NX_AZURE_IOT_SUCCESS);
    
    /* The interface type should be updated as NX_INTERFACE_TYPE_OTHER.  */
    assert_int_equal(interface_type_check(NX_INTERFACE_TYPE_OTHER), NX_AZURE_IOT_SUCCESS);

    nx_azure_iot_hub_client_disconnect(&iot_client);

    /* Set the type as NX_INTERFACE_TYPE_ETHERNET.  */
    g_interface_type = NX_INTERFACE_TYPE_ETHERNET;
    will_return(__wrap__nxe_ip_driver_interface_direct_command, NX_SUCCESS);

    assert_int_equal(nx_azure_iot_hub_client_connect(&iot_client, NX_TRUE, NX_IP_PERIODIC_RATE),
                     NX_AZURE_IOT_SUCCESS);
    
    /* The interface type should be updated as NX_INTERFACE_TYPE_ETHERNET.  */
    assert_int_equal(interface_type_check(NX_INTERFACE_TYPE_ETHERNET), NX_AZURE_IOT_SUCCESS);

    nx_azure_iot_hub_client_disconnect(&iot_client);

    /* Set the type as NX_INTERFACE_TYPE_LORAWAN.  */
    g_interface_type = NX_INTERFACE_TYPE_LORAWAN;
    will_return(__wrap__nxe_ip_driver_interface_direct_command, NX_SUCCESS);

    assert_int_equal(nx_azure_iot_hub_client_connect(&iot_client, NX_TRUE, NX_IP_PERIODIC_RATE),
                     NX_AZURE_IOT_SUCCESS);
    
    /* The interface type should be updated as NX_INTERFACE_TYPE_LORAWAN.  */
    assert_int_equal(interface_type_check(NX_INTERFACE_TYPE_LORAWAN), NX_AZURE_IOT_SUCCESS);

    nx_azure_iot_hub_client_disconnect(&iot_client);

    /* Set the type as NX_INTERFACE_TYPE_MAX - 1.  */
    g_interface_type = NX_INTERFACE_TYPE_MAX - 1;
    will_return(__wrap__nxe_ip_driver_interface_direct_command, NX_SUCCESS);

    assert_int_equal(nx_azure_iot_hub_client_connect(&iot_client, NX_TRUE, NX_IP_PERIODIC_RATE),
                     NX_AZURE_IOT_SUCCESS);
    
    /* The interface type should be updated as NX_INTERFACE_TYPE_MAX - 1.  */
    assert_int_equal(interface_type_check(NX_INTERFACE_TYPE_MAX - 1), NX_AZURE_IOT_SUCCESS);

    nx_azure_iot_hub_client_disconnect(&iot_client);

    /* Set the type as NX_INTERFACE_TYPE_MAX.  */
    g_interface_type = NX_INTERFACE_TYPE_MAX;
    will_return(__wrap__nxe_ip_driver_interface_direct_command, NX_SUCCESS);

    assert_int_equal(nx_azure_iot_hub_client_connect(&iot_client, NX_TRUE, NX_IP_PERIODIC_RATE),
                     NX_AZURE_IOT_SUCCESS);
    
    /* The interface type should be updated as NX_INTERFACE_TYPE_OTHER.  */
    assert_int_equal(interface_type_check(NX_INTERFACE_TYPE_OTHER), NX_AZURE_IOT_SUCCESS);

    nx_azure_iot_hub_client_disconnect(&iot_client);

    iot_client.nx_azure_iot_hub_client_resource.resource_mqtt.nxd_mqtt_client_socket.nx_tcp_socket_state = NX_TCP_CLOSED;
    assert_int_equal(nx_azure_iot_hub_client_deinitialize(&iot_client),
                     NX_AZURE_IOT_SUCCESS);

    /* Check if all the packet are released */
    assert_int_equal(pool_ptr -> nx_packet_pool_available, pool_ptr_available_packet);
    assert_int_equal(small_pool.nx_packet_pool_available, small_pool_available_packet);

    /* SUCCESS: iot is created. */
    assert_int_equal(nx_azure_iot_delete(&iot), NX_AZURE_IOT_SUCCESS);
}

static UINT interface_type_check(ULONG interface_type)
{
UINT index;
UCHAR type;

    /* Decode the interface type.  */
    for (index = 0; index < (iot_client.iot_hub_client_core._internal.options.user_agent._internal.size - 3); index++)
    {

        /* The interface type is after the first semicolon.  */
        if ((*(iot_client.iot_hub_client_core._internal.options.user_agent._internal.ptr + index) == '%') &&
            (*(iot_client.iot_hub_client_core._internal.options.user_agent._internal.ptr + index + 1) == '3') &&
            (*(iot_client.iot_hub_client_core._internal.options.user_agent._internal.ptr + index + 2) == 'B'))
        {
            type = *(iot_client.iot_hub_client_core._internal.options.user_agent._internal.ptr + index + 3) - '0';
            if (type == interface_type)
            {
                return(NX_AZURE_IOT_SUCCESS);
            }
            else
            {
                return(NX_AZURE_IOT_FAILURE); 
            }
        }
    }

    return(NX_AZURE_IOT_FAILURE); 
}

static UINT deplete_packets(NX_PACKET_POOL *pool_ptr, UINT remaining_packets)
{
UINT count = 0;

    while (pool_ptr -> nx_packet_pool_available > remaining_packets)
    {
        nx_packet_allocate(pool_ptr, &allocated_packets[count++], 0, NX_WAIT_FOREVER);
    }
    return(count);
}

static VOID release_packets(NX_PACKET_POOL *pool_ptr, UINT count)
{
    while (count != 0)
    {
        nx_packet_release(allocated_packets[--count]);
    }
}

UINT __wrap__nxe_ip_driver_interface_direct_command(NX_IP *ip_ptr, UINT command, UINT interface_index, ULONG *return_value_ptr)
{
    printf("HIJACKED: %s\n", __func__);
    *return_value_ptr = g_interface_type;
    return((UINT)mock());
}

UINT __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 timeout)
{
    printf("HIJACKED: %s\n", __func__);
    tx_thread_suspend(&(iot.nx_azure_iot_ip_ptr -> nx_ip_thread));
    client_ptr -> nxd_mqtt_client_state = NXD_MQTT_CLIENT_STATE_CONNECTED;
    client_ptr -> nxd_mqtt_client_packet_identifier = 1;
    client_ptr -> nxd_mqtt_tls_session.nx_secure_tls_id = NX_SECURE_TLS_ID;
    client_ptr -> nxd_mqtt_tls_session.nx_secure_tls_local_session_active = NX_FALSE;
    client_ptr -> nxd_mqtt_tls_session.nx_secure_tls_tcp_socket = &client_ptr -> nxd_mqtt_client_socket;
    client_ptr -> nxd_mqtt_client_socket.nx_tcp_socket_connect_ip.nxd_ip_version = NX_IP_VERSION_V4;
    client_ptr -> nxd_mqtt_client_socket.nx_tcp_socket_state = NX_TCP_ESTABLISHED;
    return(NXD_MQTT_SUCCESS);
}

UINT __wrap__nxde_mqtt_client_disconnect(NXD_MQTT_CLIENT *client_ptr)
{
    printf("HIJACKED: %s\n", __func__);
    client_ptr -> nxd_mqtt_client_state = NXD_MQTT_CLIENT_STATE_IDLE;
    client_ptr -> nxd_mqtt_client_socket.nx_tcp_socket_state = NX_TCP_CLOSED;
    return(NXD_MQTT_SUCCESS);
}

UINT __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)
{
    printf("HIJACKED: %s\n", __func__);
    host_address_ptr -> nxd_ip_version = NX_IP_VERSION_V4;
    host_address_ptr -> nxd_ip_address.v4 = IP_ADDRESS(127, 0, 0, 1);
    return(NX_DNS_SUCCESS);
}

UINT __wrap__nxd_mqtt_client_publish_packet_send(NXD_MQTT_CLIENT *client_ptr, NX_PACKET *packet_ptr,
                                                 USHORT packet_id, UINT QoS, ULONG wait_option)
{
UINT topic_name_length;
UINT message_length;
UCHAR *buffer_ptr;
UINT status = (UINT)mock();

    printf("HIJACKED: %s\n", __func__);
    tx_mutex_put(client_ptr -> nxd_mqtt_client_mutex_ptr);

    if (status)
    {
        return(status);
    }
    
    /* packet ownership taken and released */
    nx_packet_release(packet_ptr);
    
    return(status);
}

UINT __wrap_nx_azure_iot_security_module_enable(NX_AZURE_IOT *nx_azure_iot_ptr)
{
    printf("HIJACKED: %s\n", __func__);
    return(NX_AZURE_IOT_SUCCESS);
}

UINT __wrap_nx_azure_iot_security_module_disable(NX_AZURE_IOT *nx_azure_iot_ptr)
{
    printf("HIJACKED: %s\n", __func__);
    return(NX_AZURE_IOT_SUCCESS);
}