Debian Codegraph @

src/security/core/tests/common/test_utils.c

raw
1 /*
2 * Copyright(c) 2006 to 2021 ZettaScale Technology and others
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0 which is available at
6 * http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
7 * v. 1.0 which is available at
8 * http://www.eclipse.org/org/documents/edl-v10.php.
9 *
10 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11 */
12 #include <stdlib.h>
13 #include <assert.h>
14
15 #include "CUnit/Test.h"
16 #include "dds/dds.h"
17 #include "dds/ddsrt/process.h"
18 #include "dds/ddsrt/string.h"
19 #include "dds/ddsrt/threads.h"
20 #include "dds/ddsrt/heap.h"
21 #include "dds/ddsi/ddsi_entity.h"
22 #include "dds/ddsi/ddsi_entity_index.h"
23 #include "dds/ddsi/ddsi_security_omg.h"
24 #include "dds/ddsi/ddsi_participant.h"
25 #include "dds__entity.h"
26 #include "dds/security/dds_security_api.h"
27 #include "authentication_wrapper.h"
28 #include "test_utils.h"
29 #include "SecurityCoreTests.h"
30
31 struct Identity localIdentityList[MAX_LOCAL_IDENTITIES];
32 int numLocal = 0;
33
34 struct Identity remoteIdentityList[MAX_REMOTE_IDENTITIES];
35 int numRemote = 0;
36
37 struct Handshake handshakeList[MAX_HANDSHAKES];
38 int numHandshake = 0;
39
40 const char * g_pk_none = "NONE";
41 const char * g_pk_sign = "SIGN";
42 const char * g_pk_encrypt = "ENCRYPT";
43 const char * g_pk_sign_oa = "SIGN_WITH_ORIGIN_AUTHENTICATION";
44 const char * g_pk_encrypt_oa = "ENCRYPT_WITH_ORIGIN_AUTHENTICATION";
45
46 static char * get_validation_result_str (DDS_Security_ValidationResult_t result)
47 {
48 switch (result)
49 {
50 case DDS_SECURITY_VALIDATION_OK: return "OK";
51 case DDS_SECURITY_VALIDATION_PENDING_RETRY: return "PENDING_RETRY";
52 case DDS_SECURITY_VALIDATION_PENDING_HANDSHAKE_REQUEST: return "PENDING_HANDSHAKE_REQUEST";
53 case DDS_SECURITY_VALIDATION_PENDING_HANDSHAKE_MESSAGE: return "PENDING_HANDSHAKE_MESSAGE";
54 case DDS_SECURITY_VALIDATION_OK_FINAL_MESSAGE: return "OK_FINAL_MESSAGE";
55 case DDS_SECURITY_VALIDATION_FAILED: return "FAILED";
56 }
57 abort ();
58 return "";
59 }
60
61 static char * get_node_type_str (enum hs_node_type node_type)
62 {
63 switch (node_type)
64 {
65 case HSN_UNDEFINED: return "UNDEFINED";
66 case HSN_REQUESTER: return "REQUESTER";
67 case HSN_REPLIER: return "REPLIER";
68 }
69 abort ();
70 return "";
71 }
72
73 void print_test_msg (const char *msg, ...)
74 {
75 va_list args;
76 dds_time_t t = dds_time ();
77 printf ("%d.%06d ", (int32_t) (t / DDS_NSECS_IN_SEC), (int32_t) (t % DDS_NSECS_IN_SEC) / 1000);
78 va_start (args, msg);
79 vprintf (msg, args);
80 va_end (args);
81 }
82
83 static void add_local_identity (DDS_Security_IdentityHandle handle, DDS_Security_GUID_t *guid)
84 {
85 print_test_msg ("add local identity %"PRId64"\n", handle);
86 localIdentityList[numLocal].handle = handle;
87 memcpy (&localIdentityList[numLocal].guid, guid, sizeof(DDS_Security_GUID_t));
88 numLocal++;
89 }
90
91 static int find_local_identity (DDS_Security_IdentityHandle handle)
92 {
93 for (int i = 0; i < (int) numLocal; i++)
94 {
95 if (localIdentityList[i].handle == handle)
96 return i;
97 }
98 return -1;
99 }
100
101 static int find_remote_identity (DDS_Security_IdentityHandle handle)
102 {
103 for (int i = 0; i < numRemote; i++)
104 {
105 if (remoteIdentityList[i].handle == handle)
106 return i;
107 }
108 return -1;
109 }
110
111 static void add_remote_identity (DDS_Security_IdentityHandle handle, DDS_Security_GUID_t *guid)
112 {
113 if (find_remote_identity (handle) < 0)
114 {
115 print_test_msg ("add remote identity %"PRId64"\n", handle);
116 remoteIdentityList[numRemote].handle = handle;
117 memcpy (&remoteIdentityList[numRemote].guid, guid, sizeof(DDS_Security_GUID_t));
118 numRemote++;
119 }
120 }
121
122 static void clear_stores(void)
123 {
124 numLocal = 0;
125 numRemote = 0;
126 numHandshake = 0;
127 }
128
129 static struct Handshake *add_handshake (enum hs_node_type node_type, DDS_Security_IdentityHandle lHandle, DDS_Security_IdentityHandle rHandle)
130 {
131 print_test_msg ("add handshake %"PRId64"-%"PRId64"\n", lHandle, rHandle);
132 handshakeList[numHandshake].handle = -1;
133 handshakeList[numHandshake].node_type = node_type;
134 handshakeList[numHandshake].handshakeResult = DDS_SECURITY_VALIDATION_FAILED;
135 handshakeList[numHandshake].lidx = find_local_identity (lHandle);
136 handshakeList[numHandshake].ridx = find_remote_identity (rHandle);
137 handshakeList[numHandshake].finalResult = DDS_SECURITY_VALIDATION_FAILED;
138 handshakeList[numHandshake].err_msg = NULL;
139 numHandshake++;
140 return &handshakeList[numHandshake - 1];
141 }
142
143 static int find_handshake (DDS_Security_HandshakeHandle handle)
144 {
145 for (int i = 0; i < numHandshake; i++)
146 {
147 if (handshakeList[i].handle == handle)
148 return i;
149 }
150 return -1;
151 }
152
153 static void handle_process_message (dds_domainid_t domain_id, DDS_Security_IdentityHandle handshake, dds_time_t abstimeout)
154 {
155 struct message *msg;
156 switch (test_authentication_plugin_take_msg (domain_id, MESSAGE_KIND_PROCESS_HANDSHAKE, 0, 0, handshake, abstimeout, &msg))
157 {
158 case TAKE_MESSAGE_OK: {
159 int idx;
160 if ((idx = find_handshake (msg->hsHandle)) >= 0)
161 {
162 print_test_msg ("set handshake %"PRId64" final result to '%s' (errmsg: %s)\n", msg->hsHandle, get_validation_result_str (msg->result), msg->err_msg);
163 handshakeList[idx].finalResult = msg->result;
164 handshakeList[idx].err_msg = ddsrt_strdup (msg->err_msg);
165 }
166 test_authentication_plugin_release_msg (msg);
167 break;
168 }
169 case TAKE_MESSAGE_TIMEOUT_EMPTY: {
170 print_test_msg ("handle_process_message: timed out on empty queue\n");
171 break;
172 }
173 case TAKE_MESSAGE_TIMEOUT_NONEMPTY: {
174 print_test_msg ("handle_process_message: timed out on non-empty queue\n");
175 break;
176 }
177 }
178 }
179
180 static void handle_begin_handshake_request (dds_domainid_t domain_id, struct Handshake *hs, DDS_Security_IdentityHandle lid, DDS_Security_IdentityHandle rid, dds_time_t abstimeout)
181 {
182 struct message *msg;
183 print_test_msg ("handle begin handshake request %"PRId64"<->%"PRId64"\n", lid, rid);
184 switch (test_authentication_plugin_take_msg (domain_id, MESSAGE_KIND_BEGIN_HANDSHAKE_REQUEST, lid, rid, 0, abstimeout, &msg))
185 {
186 case TAKE_MESSAGE_OK: {
187 hs->handle = msg->hsHandle;
188 hs->handshakeResult = msg->result;
189 if (msg->result != DDS_SECURITY_VALIDATION_FAILED)
190 handle_process_message (domain_id, msg->hsHandle, abstimeout);
191 else
192 hs->err_msg = ddsrt_strdup (msg->err_msg);
193 test_authentication_plugin_release_msg (msg);
194 break;
195 }
196 case TAKE_MESSAGE_TIMEOUT_EMPTY: {
197 print_test_msg ("handle_begin_handshake_request: timed out on empty queue\n");
198 break;
199 }
200 case TAKE_MESSAGE_TIMEOUT_NONEMPTY: {
201 print_test_msg ("handle_begin_handshake_request: timed out on non-empty queue\n");
202 break;
203 }
204 }
205 }
206
207 static void handle_begin_handshake_reply (dds_domainid_t domain_id, struct Handshake *hs, DDS_Security_IdentityHandle lid, DDS_Security_IdentityHandle rid, dds_time_t abstimeout)
208 {
209 struct message *msg;
210 print_test_msg ("handle begin handshake reply %"PRId64"<->%"PRId64"\n", lid, rid);
211 switch (test_authentication_plugin_take_msg (domain_id, MESSAGE_KIND_BEGIN_HANDSHAKE_REPLY, lid, rid, 0, abstimeout, &msg))
212 {
213 case TAKE_MESSAGE_OK: {
214 hs->handle = msg->hsHandle;
215 hs->handshakeResult = msg->result;
216 if (msg->result != DDS_SECURITY_VALIDATION_FAILED)
217 handle_process_message (domain_id, msg->hsHandle, abstimeout);
218 else
219 hs->err_msg = ddsrt_strdup (msg->err_msg);
220 test_authentication_plugin_release_msg (msg);
221 break;
222 }
223 case TAKE_MESSAGE_TIMEOUT_EMPTY: {
224 print_test_msg ("handle_begin_handshake_reply: timed out on empty queue\n");
225 break;
226 }
227 case TAKE_MESSAGE_TIMEOUT_NONEMPTY: {
228 print_test_msg ("handle_begin_handshake_reply: timed out on non-empty queue\n");
229 break;
230 }
231 }
232 }
233
234 static void handle_validate_remote_identity (dds_domainid_t domain_id, DDS_Security_IdentityHandle lid, int count, dds_time_t abstimeout)
235 {
236 enum take_message_result res = TAKE_MESSAGE_OK;
237 struct message *msg;
238 while (count-- > 0 && (res = test_authentication_plugin_take_msg (domain_id, MESSAGE_KIND_VALIDATE_REMOTE_IDENTITY, lid, 0, 0, abstimeout, &msg)) == TAKE_MESSAGE_OK)
239 {
240 struct Handshake *hs;
241 add_remote_identity (msg->ridHandle, &msg->rguid);
242 hs = add_handshake (HSN_UNDEFINED, lid, msg->ridHandle);
243 if (msg->result == DDS_SECURITY_VALIDATION_PENDING_HANDSHAKE_REQUEST)
244 {
245 hs->node_type = HSN_REQUESTER;
246 handle_begin_handshake_request (domain_id, hs, lid, msg->ridHandle, abstimeout);
247 }
248 else if (msg->result == DDS_SECURITY_VALIDATION_PENDING_HANDSHAKE_MESSAGE)
249 {
250 hs->node_type = HSN_REPLIER;
251 handle_begin_handshake_reply (domain_id, hs, lid, msg->ridHandle, abstimeout);
252 }
253 else
254 {
255 print_test_msg ("validate remote failed\n");
256 }
257 test_authentication_plugin_release_msg (msg);
258 }
259
260 switch (res)
261 {
262 case TAKE_MESSAGE_OK:
263 break;
264 case TAKE_MESSAGE_TIMEOUT_EMPTY:
265 print_test_msg ("handle_validate_remote_identity: timed out on empty queue\n");
266 break;
267 case TAKE_MESSAGE_TIMEOUT_NONEMPTY:
268 print_test_msg ("handle_validate_remote_identity: timed out on non-empty queue\n");
269 break;
270 }
271 }
272
273 static void handle_validate_local_identity (dds_domainid_t domain_id, bool exp_localid_fail, const char * exp_localid_msg, dds_time_t abstimeout)
274 {
275 struct message *msg;
276 switch (test_authentication_plugin_take_msg (domain_id, MESSAGE_KIND_VALIDATE_LOCAL_IDENTITY, 0, 0, 0, abstimeout, &msg))
277 {
278 case TAKE_MESSAGE_OK:
279 break;
280 case TAKE_MESSAGE_TIMEOUT_EMPTY:
281 print_test_msg ("handle_validate_local_identity: timed out on empty queue\n");
282 break;
283 case TAKE_MESSAGE_TIMEOUT_NONEMPTY:
284 print_test_msg ("handle_validate_local_identity: timed out on non-empty queue\n");
285 break;
286 }
287 CU_ASSERT_FATAL (msg != NULL);
288 assert (msg != NULL);
289 CU_ASSERT_FATAL ((msg->result == DDS_SECURITY_VALIDATION_OK) != exp_localid_fail);
290 if (exp_localid_fail && exp_localid_msg)
291 {
292 print_test_msg ("validate_local_identity failed as expected (msg: %s)\n", msg->err_msg);
293 CU_ASSERT_FATAL (msg->err_msg && strstr (msg->err_msg, exp_localid_msg) != NULL);
294 }
295 else
296 {
297 add_local_identity (msg->lidHandle, &msg->lguid);
298 }
299 test_authentication_plugin_release_msg (msg);
300 }
301
302 void validate_handshake (dds_domainid_t domain_id, bool exp_localid_fail, const char * exp_localid_msg, struct Handshake *hs_list[], int *nhs, dds_duration_t timeout)
303 {
304 dds_time_t abstimeout = dds_time() + timeout;
305 clear_stores ();
306
307 if (nhs)
308 *nhs = 0;
309 if (hs_list)
310 *hs_list = NULL;
311
312 handle_validate_local_identity (domain_id, exp_localid_fail, exp_localid_msg, abstimeout);
313 if (!exp_localid_fail)
314 {
315 handle_validate_remote_identity (domain_id, localIdentityList[0].handle, 1, abstimeout);
316 for (int n = 0; n < numHandshake; n++)
317 {
318 struct Handshake *hs = &handshakeList[n];
319 print_test_msg ("Result: hs %"PRId64", node type %s, final result %s\n", hs->handle, get_node_type_str (hs->node_type), get_validation_result_str (hs->finalResult));
320 if (hs->err_msg && strlen (hs->err_msg))
321 print_test_msg ("- err_msg: %s\n", hs->err_msg);
322 }
323 if (nhs)
324 *nhs = numHandshake;
325 if (hs_list)
326 *hs_list = handshakeList;
327 else
328 handshake_list_fini (handshakeList, numHandshake);
329 }
330 print_test_msg ("finished validate handshake for domain %d\n\n", domain_id);
331 }
332
333 void validate_handshake_nofail (dds_domainid_t domain_id, dds_duration_t timeout)
334 {
335 struct Handshake *hs_list;
336 int nhs;
337 validate_handshake (domain_id, false, NULL, &hs_list, &nhs, timeout);
338 for (int n = 0; n < nhs; n++)
339 {
340 struct Handshake hs = hs_list[n];
341 DDS_Security_ValidationResult_t exp_result = hs.node_type == HSN_REQUESTER ? DDS_SECURITY_VALIDATION_OK_FINAL_MESSAGE : DDS_SECURITY_VALIDATION_OK;
342 CU_ASSERT_EQUAL_FATAL (hs.finalResult, exp_result);
343 }
344 handshake_list_fini (hs_list, nhs);
345 }
346
347 void validate_handshake_result(struct Handshake *hs, bool exp_fail_hs_req, const char * fail_hs_req_msg, bool exp_fail_hs_reply, const char * fail_hs_reply_msg)
348 {
349 DDS_Security_ValidationResult_t exp_result = hs->node_type == HSN_REQUESTER ? DDS_SECURITY_VALIDATION_OK_FINAL_MESSAGE : DDS_SECURITY_VALIDATION_OK;
350 if (hs->node_type == HSN_REQUESTER)
351 {
352 CU_ASSERT_EQUAL_FATAL (hs->finalResult, exp_fail_hs_req ? DDS_SECURITY_VALIDATION_FAILED : exp_result);
353 if (exp_fail_hs_req)
354 {
355 if (fail_hs_req_msg == NULL)
356 {
357 CU_ASSERT_EQUAL_FATAL (hs->err_msg, NULL);
358 }
359 else
360 {
361 CU_ASSERT_FATAL (hs->err_msg && strstr(hs->err_msg, fail_hs_req_msg) != NULL);
362 }
363 }
364 }
365 else if (hs->node_type == HSN_REPLIER)
366 {
367 CU_ASSERT_EQUAL_FATAL (hs->finalResult, exp_fail_hs_reply ? DDS_SECURITY_VALIDATION_FAILED : exp_result);
368 if (exp_fail_hs_reply)
369 {
370 if (fail_hs_reply_msg == NULL)
371 {
372 CU_ASSERT_EQUAL_FATAL (hs->err_msg, NULL);
373 }
374 else
375 {
376 CU_ASSERT_FATAL (hs->err_msg && strstr(hs->err_msg, fail_hs_reply_msg) != NULL);
377 }
378 }
379 }
380 }
381
382 void handshake_list_fini (struct Handshake *hs_list, int nhs)
383 {
384 for (int n = 0; n < nhs; n++)
385 {
386 struct Handshake hs = hs_list[n];
387 ddsrt_free (hs.err_msg);
388 }
389 }
390
391 void sync_writer_to_readers (dds_entity_t pp_wr, dds_entity_t wr, uint32_t exp_count, dds_time_t abstimeout)
392 {
393 dds_attach_t triggered;
394 dds_entity_t ws = dds_create_waitset (pp_wr);
395 CU_ASSERT_FATAL (ws > 0);
396 dds_publication_matched_status_t pub_matched;
397
398 dds_return_t ret = dds_waitset_attach (ws, wr, wr);
399 CU_ASSERT_EQUAL_FATAL (ret, DDS_RETCODE_OK);
400 while (true)
401 {
402 ret = dds_waitset_wait_until (ws, &triggered, 1, abstimeout);
403 CU_ASSERT_EQUAL_FATAL (exp_count > 0, ret >= 1);
404 if (exp_count > 0)
405 CU_ASSERT_EQUAL_FATAL (wr, (dds_entity_t)(intptr_t) triggered);
406 ret = dds_get_publication_matched_status (wr, &pub_matched);
407 CU_ASSERT_EQUAL_FATAL (ret, DDS_RETCODE_OK);
408 if (pub_matched.total_count >= exp_count)
409 break;
410 };
411 dds_delete (ws);
412 CU_ASSERT_EQUAL_FATAL (pub_matched.total_count, exp_count);
413 }
414
415 void sync_reader_to_writers (dds_entity_t pp_rd, dds_entity_t rd, uint32_t exp_count, dds_time_t abstimeout)
416 {
417 dds_attach_t triggered;
418 dds_entity_t ws = dds_create_waitset (pp_rd);
419 CU_ASSERT_FATAL (ws > 0);
420 dds_subscription_matched_status_t sub_matched;
421
422 dds_return_t ret = dds_waitset_attach (ws, rd, rd);
423 CU_ASSERT_EQUAL_FATAL (ret, DDS_RETCODE_OK);
424 while (true)
425 {
426 ret = dds_waitset_wait_until (ws, &triggered, 1, abstimeout);
427 CU_ASSERT_EQUAL_FATAL (exp_count > 0, ret >= 1);
428 if (exp_count > 0)
429 CU_ASSERT_EQUAL_FATAL (rd, (dds_entity_t)(intptr_t) triggered);
430 ret = dds_get_subscription_matched_status (rd, &sub_matched);
431 CU_ASSERT_EQUAL_FATAL (ret, DDS_RETCODE_OK);
432 if (sub_matched.total_count >= exp_count)
433 break;
434 };
435 dds_delete (ws);
436 CU_ASSERT_EQUAL_FATAL (sub_matched.total_count, exp_count);
437 }
438
439 char *create_topic_name (const char *prefix, uint32_t nr, char *name, size_t size)
440 {
441 ddsrt_pid_t pid = ddsrt_getpid ();
442 ddsrt_tid_t tid = ddsrt_gettid ();
443 (void)snprintf(name, size, "%s%"PRIu32"_pid%" PRIdPID "_tid%" PRIdTID "", prefix, nr, pid, tid);
444 return name;
445 }
446
447 bool reader_wait_for_data (dds_entity_t pp, dds_entity_t rd, dds_duration_t dur)
448 {
449 dds_attach_t triggered;
450 dds_entity_t ws = dds_create_waitset (pp);
451 CU_ASSERT_FATAL (ws > 0);
452 dds_return_t ret = dds_waitset_attach (ws, rd, rd);
453 CU_ASSERT_EQUAL_FATAL (ret, DDS_RETCODE_OK);
454 ret = dds_waitset_wait (ws, &triggered, 1, dur);
455 if (ret > 0)
456 CU_ASSERT_EQUAL_FATAL (rd, (dds_entity_t)(intptr_t)triggered);
457 dds_delete (ws);
458 return ret > 0;
459 }
460
461 dds_qos_t * get_default_test_qos (void)
462 {
463 dds_qos_t * qos = dds_create_qos ();
464 CU_ASSERT_FATAL (qos != NULL);
465 dds_qset_history (qos, DDS_HISTORY_KEEP_ALL, -1);
466 dds_qset_durability (qos, DDS_DURABILITY_TRANSIENT_LOCAL);
467 dds_qset_reliability (qos, DDS_RELIABILITY_RELIABLE, DDS_INFINITY);
468 return qos;
469 }
470
471 void rd_wr_init_w_partitions_fail(
472 dds_entity_t pp_wr, dds_entity_t *pub, dds_entity_t *pub_tp, dds_entity_t *wr,
473 dds_entity_t pp_rd, dds_entity_t *sub, dds_entity_t *sub_tp, dds_entity_t *rd,
474 const char * topic_name,
475 const char ** partition_names,
476 bool exp_pubtp_fail, bool exp_wr_fail,
477 bool exp_subtp_fail, bool exp_rd_fail)
478 {
479 dds_qos_t * qos = get_default_test_qos ();
480 if (partition_names)
481 {
482 uint32_t npart = 0;
483 while (partition_names[npart] != NULL)
484 npart++;
485 dds_qset_partition (qos, npart, partition_names);
486 }
487 *pub = dds_create_publisher (pp_wr, qos, NULL);
488 CU_ASSERT_FATAL (*pub > 0);
489 *sub = dds_create_subscriber (pp_rd, qos, NULL);
490 CU_ASSERT_FATAL (*sub > 0);
491 *pub_tp = dds_create_topic (pp_wr, &SecurityCoreTests_Type1_desc, topic_name, NULL, NULL);
492 CU_ASSERT_EQUAL_FATAL (exp_pubtp_fail, *pub_tp <= 0);
493 *sub_tp = dds_create_topic (pp_rd, &SecurityCoreTests_Type1_desc, topic_name, NULL, NULL);
494 CU_ASSERT_EQUAL_FATAL (exp_subtp_fail, *sub_tp <= 0);
495 if (!exp_pubtp_fail)
496 {
497 *wr = dds_create_writer (*pub, *pub_tp, qos, NULL);
498 CU_ASSERT_EQUAL_FATAL (exp_wr_fail, *wr <= 0);
499 if (exp_wr_fail)
500 goto fail;
501 dds_set_status_mask (*wr, DDS_PUBLICATION_MATCHED_STATUS);
502 }
503 if (!exp_subtp_fail)
504 {
505 *rd = dds_create_reader (*sub, *sub_tp, qos, NULL);
506 CU_ASSERT_EQUAL_FATAL (exp_rd_fail, *rd <= 0);
507 if (exp_rd_fail)
508 goto fail;
509 dds_set_status_mask (*rd, DDS_SUBSCRIPTION_MATCHED_STATUS);
510 }
511 fail:
512 dds_delete_qos (qos);
513 }
514
515 void rd_wr_init_fail(
516 dds_entity_t pp_wr, dds_entity_t *pub, dds_entity_t *pub_tp, dds_entity_t *wr,
517 dds_entity_t pp_rd, dds_entity_t *sub, dds_entity_t *sub_tp, dds_entity_t *rd,
518 const char * topic_name,
519 bool exp_pubtp_fail, bool exp_wr_fail,
520 bool exp_subtp_fail, bool exp_rd_fail)
521 {
522 rd_wr_init_w_partitions_fail (pp_wr, pub, pub_tp, wr, pp_rd, sub, sub_tp, rd, topic_name, NULL, exp_pubtp_fail, exp_wr_fail, exp_subtp_fail, exp_rd_fail);
523 }
524
525 void rd_wr_init(
526 dds_entity_t pp_wr, dds_entity_t *pub, dds_entity_t *pub_tp, dds_entity_t *wr,
527 dds_entity_t pp_rd, dds_entity_t *sub, dds_entity_t *sub_tp, dds_entity_t *rd,
528 const char * topic_name)
529 {
530 rd_wr_init_w_partitions_fail (pp_wr, pub, pub_tp, wr, pp_rd, sub, sub_tp, rd, topic_name, NULL, false, false, false, false);
531 }
532
533 void write_read_for(dds_entity_t wr, dds_entity_t pp_rd, dds_entity_t rd, dds_duration_t dur, bool exp_write_fail, bool exp_read_fail)
534 {
535 SecurityCoreTests_Type1 sample = { 1, 1 };
536 SecurityCoreTests_Type1 rd_sample;
537 void * samples[] = { &rd_sample };
538 dds_sample_info_t info[1];
539 dds_return_t ret;
540 dds_time_t tend = dds_time () + dur;
541 bool write_fail = false, read_fail = false;
542
543 dds_set_status_mask (rd, DDS_DATA_AVAILABLE_STATUS);
544 do
545 {
546 print_test_msg ("write\n");
547 if (dds_write (wr, &sample) != DDS_RETCODE_OK)
548 write_fail = true;
549
550 while (!write_fail)
551 {
552 if ((ret = dds_take (rd, samples, info, 1, 1)) > 0)
553 {
554 print_test_msg ("take sample\n");
555 if (info[0].instance_state != DDS_IST_ALIVE || !info[0].valid_data)
556 {
557 print_test_msg ("invalid sample instance_state=%d valid_data=%d\n", info[0].instance_state, info[0].valid_data);
558 read_fail = true;
559 }
560 else
561 CU_ASSERT_EQUAL_FATAL (rd_sample.value, 1);
562 CU_ASSERT_EQUAL_FATAL (ret, 1);
563 break;
564 }
565 if (ret < 0 || !reader_wait_for_data (pp_rd, rd, DDS_MSECS (1000)))
566 {
567 print_test_msg ("take no sample\n");
568 read_fail = true;
569 break;
570 }
571 }
572 if (write_fail || read_fail)
573 break;
574 dds_sleepfor (DDS_MSECS (100));
575 }
576 while (dds_time () < tend);
577 CU_ASSERT_EQUAL_FATAL (write_fail, exp_write_fail);
578 CU_ASSERT_EQUAL_FATAL (read_fail, exp_read_fail);
579 }
580
581 #define GET_SECURITY_PLUGIN_CONTEXT(name_) \
582 struct dds_security_##name_##_impl * get_##name_##_context(dds_entity_t participant) \
583 { \
584 struct dds_entity *pp_entity = NULL; \
585 dds_return_t ret = dds_entity_lock (participant, DDS_KIND_PARTICIPANT, &pp_entity); \
586 CU_ASSERT_EQUAL_FATAL (ret, 0); \
587 thread_state_awake (lookup_thread_state(), &pp_entity->m_domain->gv); \
588 struct ddsi_participant *pp = entidx_lookup_participant_guid (pp_entity->m_domain->gv.entity_index, &pp_entity->m_guid); \
589 CU_ASSERT_FATAL (pp != NULL); \
590 struct dds_security_##name_##_impl *context = (struct dds_security_##name_##_impl *) q_omg_participant_get_##name_ (pp); \
591 thread_state_asleep (lookup_thread_state ()); \
592 dds_entity_unlock (pp_entity); \
593 return context; \
594 }
595
596 GET_SECURITY_PLUGIN_CONTEXT(access_control)
597 GET_SECURITY_PLUGIN_CONTEXT(authentication)
598 GET_SECURITY_PLUGIN_CONTEXT(cryptography)
599
600 const char * pk_to_str(DDS_Security_ProtectionKind pk)
601 {
602 switch (pk)
603 {
604 case DDS_SECURITY_PROTECTION_KIND_NONE: return g_pk_none;
605 case DDS_SECURITY_PROTECTION_KIND_SIGN: return g_pk_sign;
606 case DDS_SECURITY_PROTECTION_KIND_ENCRYPT: return g_pk_encrypt;
607 case DDS_SECURITY_PROTECTION_KIND_SIGN_WITH_ORIGIN_AUTHENTICATION: return g_pk_sign_oa;
608 case DDS_SECURITY_PROTECTION_KIND_ENCRYPT_WITH_ORIGIN_AUTHENTICATION: return g_pk_encrypt_oa;
609 }
610 assert (false);
611 return NULL;
612 }
613
614 const char * bpk_to_str(DDS_Security_BasicProtectionKind bpk)
615 {
616 switch (bpk)
617 {
618 case DDS_SECURITY_BASICPROTECTION_KIND_NONE: return g_pk_none;
619 case DDS_SECURITY_BASICPROTECTION_KIND_SIGN: return g_pk_sign;
620 case DDS_SECURITY_BASICPROTECTION_KIND_ENCRYPT: return g_pk_encrypt;
621 }
622 assert (false);
623 return NULL;
624 }
625
626 DDS_Security_DatawriterCryptoHandle get_builtin_writer_crypto_handle(dds_entity_t participant, unsigned entityid)
627 {
628 DDS_Security_DatawriterCryptoHandle crypto_handle;
629 struct dds_entity *pp_entity;
630 struct ddsi_participant *pp;
631 struct ddsi_writer *wr;
632 CU_ASSERT_EQUAL_FATAL(dds_entity_pin(participant, &pp_entity), 0);
633 thread_state_awake(lookup_thread_state(), &pp_entity->m_domain->gv);
634 pp = entidx_lookup_participant_guid(pp_entity->m_domain->gv.entity_index, &pp_entity->m_guid);
635 wr = ddsi_get_builtin_writer (pp, entityid);
636 CU_ASSERT_FATAL(wr != NULL);
637 crypto_handle = wr->sec_attr->crypto_handle;
638 thread_state_asleep(lookup_thread_state());
639 dds_entity_unpin(pp_entity);
640 return crypto_handle;
641 }
642
643 DDS_Security_DatawriterCryptoHandle get_writer_crypto_handle(dds_entity_t writer)
644 {
645 DDS_Security_DatawriterCryptoHandle crypto_handle;
646 struct dds_entity *wr_entity;
647 struct ddsi_writer *wr;
648 CU_ASSERT_EQUAL_FATAL(dds_entity_pin(writer, &wr_entity), 0);
649 thread_state_awake(lookup_thread_state(), &wr_entity->m_domain->gv);
650 wr = entidx_lookup_writer_guid(wr_entity->m_domain->gv.entity_index, &wr_entity->m_guid);
651 CU_ASSERT_FATAL(wr != NULL);
652 crypto_handle = wr->sec_attr->crypto_handle;
653 thread_state_asleep(lookup_thread_state());
654 dds_entity_unpin(wr_entity);
655 return crypto_handle;
656 }