Debian Codegraph @

src/vendor/azure-uamqp-c/tests/iothub_e2e/iothub_e2e.c

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 #ifdef __cplusplus
5 #include <cstdlib>
6 #include <cstddef>
7 #else
8 #include <stdlib.h>
9 #include <stddef.h>
10 #include <stdbool.h>
11 #endif
12 #include "testrunnerswitcher.h"
13 #include "azure_c_shared_utility/platform.h"
14 #include "azure_c_shared_utility/crt_abstractions.h"
15 #include "azure_c_shared_utility/threadapi.h"
16 #include "azure_c_shared_utility/tlsio.h"
17 #include "azure_c_shared_utility/strings.h"
18 #include "azure_c_shared_utility/sastoken.h"
19 #include "azure_uamqp_c/uamqp.h"
20
21 static TEST_MUTEX_HANDLE g_testByTest;
22 static char* iothub_name;
23 static char* hostname;
24 static char* key_name;
25 static char* shared_access_key;
26 static char* iothub_suffix;
27
28 #define TEST_TIMEOUT 30 // seconds
29
30 typedef struct SERVER_INSTANCE_TAG
31 {
32 CONNECTION_HANDLE connection;
33 SESSION_HANDLE session;
34 LINK_HANDLE link;
35 MESSAGE_RECEIVER_HANDLE message_receiver;
36 size_t received_messages;
37 XIO_HANDLE header_detect_io;
38 } SERVER_INSTANCE;
39
40 static bool auth;
41
42 static void on_cbs_open_complete(void* context, CBS_OPEN_COMPLETE_RESULT open_complete_result)
43 {
44 (void)context;
45 switch (open_complete_result)
46 {
47 default:
48 ASSERT_FAIL("CBS instance open failed.");
49 break;
50
51 case CBS_OPEN_OK:
52 (void)printf("CBS instance open.\r\n");
53 break;
54 }
55 }
56
57 static void on_cbs_error(void* context)
58 {
59 (void)context;
60 }
61
62 void on_cbs_put_token_complete(void* context, CBS_OPERATION_RESULT cbs_operation_result, unsigned int status_code, const char* status_description)
63 {
64 (void)context;
65 (void)status_code;
66 (void)status_description;
67
68 if (cbs_operation_result == CBS_OPERATION_RESULT_OK)
69 {
70 auth = true;
71 }
72 }
73
74 static void on_message_send_complete(void* context, MESSAGE_SEND_RESULT send_result, AMQP_VALUE delivery_state)
75 {
76 size_t* sent_messages = (size_t*)context;
77
78 (void)delivery_state;
79
80 if (send_result == MESSAGE_SEND_OK)
81 {
82 (*sent_messages)++;
83 }
84 else
85 {
86 ASSERT_FAIL("Message send failed");
87 }
88 }
89
90 BEGIN_TEST_SUITE(iothub_e2e)
91
92 TEST_SUITE_INITIALIZE(suite_init)
93 {
94 int result;
95 int beginName, endName, beginIothub, endIothub, beginHost, endHost, beginKey;
96 size_t totalLen;
97 const char* iothub_connection_string;
98
99 g_testByTest = TEST_MUTEX_CREATE();
100 ASSERT_IS_NOT_NULL(g_testByTest);
101
102 result = platform_init();
103 ASSERT_ARE_EQUAL(int, 0, result, "platform_init failed");
104
105 iothub_connection_string = getenv("IOTHUB_CONNECTION_STRING");
106 ASSERT_IS_NOT_NULL(iothub_connection_string, "Could not get IoTHub connection string");
107
108 totalLen = strlen(iothub_connection_string);
109 if (sscanf(iothub_connection_string, "HostName=%n%*[^.]%n.%n%*[^;];%nSharedAccessKeyName=%n%*[^;];%nSharedAccessKey=%n", &beginHost, &endHost, &beginIothub, &endIothub, &beginName, &endName, &beginKey) != 0)
110 {
111 ASSERT_FAIL("Failure determining the string length parameters.\r\n");
112 }
113 else
114 {
115 if ((iothub_name = (char*)malloc(endHost - beginHost + 1)) == NULL)
116 {
117 ASSERT_FAIL("Failure allocating iothubName.\r\n");
118 }
119 else if ((hostname = (char*)malloc(endIothub - beginHost + 1)) == NULL)
120 {
121 ASSERT_FAIL("Failure allocating hostname.\r\n");
122 }
123 else if ((key_name = (char*)malloc(endName - beginName + 1)) == NULL)
124 {
125 ASSERT_FAIL("Failure allocating hostName.\r\n");
126 }
127 else if ((shared_access_key = (char*)malloc(totalLen + 1 - beginKey + 1)) == NULL)
128 {
129 ASSERT_FAIL("Failure allocating hostName.\r\n");
130 }
131 else if (sscanf(iothub_connection_string, "HostName=%[^.].%[^;];SharedAccessKeyName=%[^;];SharedAccessKey=%s", iothub_name,
132 hostname + endHost - beginHost + 1,
133 key_name,
134 shared_access_key) != 4)
135 {
136 ASSERT_FAIL("Failure determining the string values.\r\n");
137 }
138 else
139 {
140 (void)strcpy(hostname, iothub_name);
141 hostname[endHost - beginHost] = '.';
142 if (mallocAndStrcpy_s(&iothub_suffix, hostname + endHost - beginHost + 1) != 0)
143 {
144 ASSERT_FAIL("[IoTHubAccount] Failure constructing the iothubSuffix.");
145 }
146 else
147 {
148 result = 0;
149 }
150 }
151 }
152 }
153
154 TEST_SUITE_CLEANUP(suite_cleanup)
155 {
156 platform_deinit();
157
158 free(iothub_suffix);
159 free(iothub_name);
160 free(hostname);
161 free(key_name);
162 free(shared_access_key);
163
164 TEST_MUTEX_DESTROY(g_testByTest);
165 }
166
167 TEST_FUNCTION(send_1_message_to_iothub_unsettled_auth_with_cbs)
168 {
169 // arrange
170 int result;
171 ASYNC_OPERATION_HANDLE async_operation;
172 CONNECTION_HANDLE client_connection;
173 SESSION_HANDLE client_session;
174 LINK_HANDLE client_link;
175 MESSAGE_HANDLE client_send_message;
176 MESSAGE_SENDER_HANDLE client_message_sender;
177 size_t sent_messages = 0;
178 AMQP_VALUE source;
179 AMQP_VALUE target;
180 time_t now_time;
181 time_t start_time;
182 SASL_MECHANISM_HANDLE sasl_mechanism;
183 XIO_HANDLE tls_io;
184 XIO_HANDLE sasl_client_io;
185 SASLCLIENTIO_CONFIG sasl_io_config;
186 size_t target_length;
187 char* target_string;
188 STRING_HANDLE key_string;
189 STRING_HANDLE scope_string;
190 STRING_HANDLE keyname_string;
191 STRING_HANDLE sas_token;
192 CBS_HANDLE cbs;
193 unsigned char hello[] = { 'H', 'e', 'l', 'l', 'o' };
194 BINARY_DATA binary_data;
195 ASYNC_OPERATION_HANDLE send_operation;
196
197 // start the client
198 TLSIO_CONFIG tlsio_config;
199 tlsio_config.hostname = hostname;
200 tlsio_config.port = 5671;
201 tlsio_config.underlying_io_interface = NULL;
202 tlsio_config.underlying_io_parameters = NULL;
203
204 tls_io = xio_create(platform_get_default_tlsio(), &tlsio_config);
205 ASSERT_IS_NOT_NULL(tls_io, "Could not create TLS IO");
206
207 /* create SASL MSSBCBS handler */
208 sasl_mechanism = saslmechanism_create(saslmssbcbs_get_interface(), NULL);
209 ASSERT_IS_NOT_NULL(sasl_mechanism, "Could not create SASL mechanism handle");
210
211 sasl_io_config.underlying_io = tls_io;
212 sasl_io_config.sasl_mechanism = sasl_mechanism;
213 sasl_client_io = xio_create(saslclientio_get_interface_description(), &sasl_io_config);
214 ASSERT_IS_NOT_NULL(sasl_client_io, "Could not create SASL client IO");
215
216 /* create the connection, session and link */
217 client_connection = connection_create(sasl_client_io, hostname, "some", NULL, NULL);
218 ASSERT_IS_NOT_NULL(client_connection, "Could not create client connection");
219
220 (void)connection_set_trace(client_connection, true);
221 client_session = session_create(client_connection, NULL, NULL);
222 ASSERT_IS_NOT_NULL(client_session, "Could not create client session");
223
224 const char* eh_testdevice_device_key;
225 eh_testdevice_device_key = getenv("UAMQP_E2E_DEVICE_KEY");
226 ASSERT_IS_NOT_NULL(eh_testdevice_device_key, "Could not get eh_testdevice device key");
227
228 key_string = STRING_new();
229 ASSERT_IS_NOT_NULL(key_string, "Could not create key_string");
230 result = STRING_concat(key_string, eh_testdevice_device_key);
231 ASSERT_ARE_EQUAL(int, 0, result, "Could not create key_string");
232 scope_string = STRING_new();
233 ASSERT_IS_NOT_NULL(scope_string, "Could not create scope string");
234 result = STRING_concat(scope_string, hostname);
235 ASSERT_ARE_EQUAL(int, 0, result, "cannot create scope for SAS token");
236 result = STRING_concat(scope_string, "/devices/eh_testdevice");
237 ASSERT_ARE_EQUAL(int, 0, result, "cannot create scope for SAS token");
238 keyname_string = STRING_new();
239 ASSERT_IS_NOT_NULL(keyname_string, "Could not create keyname_string");
240
241 sas_token = SASToken_Create(key_string, scope_string, keyname_string, (size_t)time(NULL) + 3600);
242 ASSERT_IS_NOT_NULL(sas_token, "Could not create sas_token");
243
244 cbs = cbs_create(client_session);
245 result = cbs_open_async(cbs, on_cbs_open_complete, cbs, on_cbs_error, cbs);
246 ASSERT_ARE_EQUAL(int, 0, result, "cannot open cbs client");
247
248 auth = false;
249
250 async_operation = cbs_put_token_async(cbs, "servicebus.windows.net:sastoken", STRING_c_str(scope_string), STRING_c_str(sas_token), on_cbs_put_token_complete, cbs);
251 ASSERT_ARE_NOT_EQUAL(void_ptr, NULL, async_operation, "cannot put cbs token");
252
253 start_time = time(NULL);
254 while ((now_time = time(NULL)),
255 (difftime(now_time, start_time) < TEST_TIMEOUT))
256 {
257 // schedule work for all components
258 connection_dowork(client_connection);
259
260 ThreadAPI_Sleep(1);
261
262 if (auth)
263 {
264 break;
265 }
266 }
267
268 source = messaging_create_source("ingress");
269 ASSERT_IS_NOT_NULL(source, "Could not create source");
270 target_length = snprintf(NULL, 0, "amqps://%s/devices/%s/messages/events", hostname, "eh_testdevice");
271 target_string = (char*)malloc(target_length + 1);
272 ASSERT_IS_NOT_NULL(target_string, "Could not allocate memory for target string");
273 target_length = snprintf(target_string, target_length + 1, "amqps://%s/devices/%s/messages/events", hostname, "eh_testdevice");
274 target = messaging_create_target(target_string);
275 ASSERT_IS_NOT_NULL(target, "Could not create target");
276 client_link = link_create(client_session, "sender-link", role_sender, source, target);
277 ASSERT_IS_NOT_NULL(client_link, "Could not create client link");
278 result = link_set_snd_settle_mode(client_link, sender_settle_mode_settled);
279 ASSERT_ARE_EQUAL(int, 0, result, "cannot set sender settle mode");
280
281 amqpvalue_destroy(source);
282 amqpvalue_destroy(target);
283
284 client_send_message = message_create();
285 ASSERT_IS_NOT_NULL(client_send_message, "Could not create message");
286 binary_data.bytes = hello;
287 binary_data.length = sizeof(hello);
288 result = message_add_body_amqp_data(client_send_message, binary_data);
289 ASSERT_ARE_EQUAL(int, 0, result, "cannot set message body");
290
291 /* create a message sender */
292 client_message_sender = messagesender_create(client_link, NULL, NULL);
293 ASSERT_IS_NOT_NULL(client_message_sender, "Could not create message sender");
294 result = messagesender_open(client_message_sender);
295 ASSERT_ARE_EQUAL(int, 0, result, "cannot open message sender");
296 send_operation = messagesender_send_async(client_message_sender, client_send_message, on_message_send_complete, &sent_messages, 0);
297 ASSERT_IS_NOT_NULL(send_operation, "cannot send message");
298 message_destroy(client_send_message);
299
300 while ((now_time = time(NULL)),
301 (difftime(now_time, start_time) < TEST_TIMEOUT))
302 {
303 // schedule work for all components
304 connection_dowork(client_connection);
305
306 // if we sent the message, break
307 if (sent_messages >= 1)
308 {
309 break;
310 }
311
312 ThreadAPI_Sleep(1);
313 }
314
315 // assert
316 ASSERT_ARE_EQUAL(size_t, 1, sent_messages, "Bad sent messages count");
317
318 // cleanup
319 STRING_delete(key_string);
320 STRING_delete(keyname_string);
321 STRING_delete(scope_string);
322 STRING_delete(sas_token);
323
324 cbs_destroy(cbs);
325 messagesender_destroy(client_message_sender);
326 link_destroy(client_link);
327 session_destroy(client_session);
328 connection_destroy(client_connection);
329 xio_destroy(sasl_client_io);
330 xio_destroy(tls_io);
331 saslmechanism_destroy(sasl_mechanism);
332 free(target_string);
333 }
334
335 END_TEST_SUITE(iothub_e2e)