Changeset 39
- Timestamp:
- 06/04/08 16:36:44
(6 months ago)
- Author:
- jtang
- Message:
Added chunked mqueue test.
Valgrind reports no errors now.
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r37 |
r39 |
|
| 1 | 1 | /* Author: David Windsor <dwindsor@tresys.com> |
|---|
| 2 | 2 | * |
|---|
| 3 | | * Copyright (C) 2006, 2007 Tresys Technology, LLC |
|---|
| | 3 | * Copyright (C) 2006 - 2008 Tresys Technology, LLC |
|---|
| 4 | 4 | * Developed Under US JFCOM Sponsorship |
|---|
| 5 | 5 | * |
|---|
| … | … | |
| 32 | 32 | |
|---|
| 33 | 33 | #define SIPC_MQ_KEY "mq_ipc_test" |
|---|
| | 34 | #define SIPC_BIG_MQ_KEY "mq2_ipc_test" |
|---|
| 34 | 35 | |
|---|
| 35 | 36 | #define CREATOR_APP "ipc_creator" |
|---|
| … | … | |
| 37 | 38 | #define IN_FILE "data" |
|---|
| 38 | 39 | #define DATA_LEN 8192 |
|---|
| | 40 | #define BIG_DATA_LEN (1024 * 32) |
|---|
| 39 | 41 | #define XMIT_END "0xDEADBEEF" |
|---|
| 40 | 42 | |
|---|
| 41 | | static sipc_t *writer_ipc; |
|---|
| 42 | | static sipc_t *reader_ipc; |
|---|
| | 43 | static sipc_t *writer_ipc, *reader_ipc; |
|---|
| | 44 | static sipc_t *writer2_ipc, *reader2_ipc; |
|---|
| 43 | 45 | static int send_end_xmit(sipc_t *ipc); |
|---|
| 44 | 46 | static void do_parent(void); |
|---|
| 45 | 47 | static void do_child(void); |
|---|
| | 48 | static void do_parent_chunked(void); |
|---|
| | 49 | static void do_child_chunked(void); |
|---|
| 46 | 50 | static int verify_data(char *filename, char *recv_data); |
|---|
| 47 | 51 | static inline int is_end_xmit(char *data); |
|---|
| … | … | |
| 74 | 78 | } |
|---|
| 75 | 79 | wait(&status); |
|---|
| | 80 | helper_argv[1] = SIPC_BIG_MQ_KEY; |
|---|
| | 81 | switch ((pid = fork())) { |
|---|
| | 82 | case -1: |
|---|
| | 83 | fprintf(stderr, "fork(): %s\n", strerror(errno)); |
|---|
| | 84 | return -1; |
|---|
| | 85 | case 0: |
|---|
| | 86 | execve(CREATOR_APP, helper_argv, NULL); |
|---|
| | 87 | break; |
|---|
| | 88 | default: |
|---|
| | 89 | break; |
|---|
| | 90 | } |
|---|
| | 91 | wait(&status); |
|---|
| 76 | 92 | |
|---|
| 77 | 93 | /* Open IPCs */ |
|---|
| … | … | |
| 87 | 103 | } |
|---|
| 88 | 104 | |
|---|
| | 105 | writer2_ipc = sipc_open(SIPC_BIG_MQ_KEY, SIPC_SENDER, SIPC_SYSV_MQUEUES, BIG_DATA_LEN); |
|---|
| | 106 | if (writer2_ipc == NULL) { |
|---|
| | 107 | fprintf(stderr, "Unable to initialize big IPC resource\n"); |
|---|
| | 108 | return -1; |
|---|
| | 109 | } |
|---|
| | 110 | reader2_ipc = sipc_open(SIPC_BIG_MQ_KEY, SIPC_RECEIVER, SIPC_SYSV_MQUEUES, BIG_DATA_LEN); |
|---|
| | 111 | if (reader2_ipc == NULL) { |
|---|
| | 112 | fprintf(stderr, "Error initializing big IPC resource\n"); |
|---|
| | 113 | return -1; |
|---|
| | 114 | } |
|---|
| 89 | 115 | return 0; |
|---|
| 90 | 116 | } |
|---|
| … | … | |
| 96 | 122 | char ipc_type_str[128]; |
|---|
| 97 | 123 | |
|---|
| 98 | | /* Cleanup handle here */ |
|---|
| | 124 | /* Cleanup handles here */ |
|---|
| 99 | 125 | sipc_close(reader_ipc); |
|---|
| 100 | 126 | sipc_close(writer_ipc); |
|---|
| | 127 | sipc_close(reader2_ipc); |
|---|
| | 128 | sipc_close(writer2_ipc); |
|---|
| 101 | 129 | |
|---|
| 102 | 130 | /* Build the destroyer app's argv */ |
|---|
| … | … | |
| 123 | 151 | wait(&status); |
|---|
| 124 | 152 | |
|---|
| | 153 | helper_argv[1] = SIPC_BIG_MQ_KEY; |
|---|
| | 154 | switch ((pid = fork())) { |
|---|
| | 155 | case -1: |
|---|
| | 156 | fprintf(stderr, "fork(): %s\n", strerror(errno)); |
|---|
| | 157 | return -1; |
|---|
| | 158 | case 0: |
|---|
| | 159 | execve(DESTROYER_APP, helper_argv, NULL); |
|---|
| | 160 | break; |
|---|
| | 161 | default: |
|---|
| | 162 | break; |
|---|
| | 163 | } |
|---|
| | 164 | wait(&status); |
|---|
| | 165 | |
|---|
| 125 | 166 | return 0; |
|---|
| 126 | 167 | } |
|---|
| … | … | |
| 142 | 183 | CU_ASSERT(memcmp(recv_data, message, message_len) == 0); |
|---|
| 143 | 184 | free(recv_data); |
|---|
| 144 | | CU_ASSERT(sipc_shm_recv_done(reader_ipc) == 0); |
|---|
| 145 | 185 | } |
|---|
| 146 | 186 | |
|---|
| … | … | |
| 161 | 201 | default: |
|---|
| 162 | 202 | do_parent(); |
|---|
| | 203 | break; |
|---|
| | 204 | } |
|---|
| | 205 | |
|---|
| | 206 | wait(&status); |
|---|
| | 207 | } |
|---|
| | 208 | |
|---|
| | 209 | static void test_chunked_mqueue(void) |
|---|
| | 210 | { |
|---|
| | 211 | pid_t pid; |
|---|
| | 212 | int status = 0; |
|---|
| | 213 | |
|---|
| | 214 | switch ((pid = fork())) { |
|---|
| | 215 | case -1: |
|---|
| | 216 | fprintf(stderr, "fork: %s\n", strerror(errno)); |
|---|
| | 217 | return; |
|---|
| | 218 | case 0: |
|---|
| | 219 | sipc_close(reader2_ipc); |
|---|
| | 220 | do_child_chunked(); |
|---|
| | 221 | exit(0); |
|---|
| | 222 | break; |
|---|
| | 223 | default: |
|---|
| | 224 | do_parent_chunked(); |
|---|
| 163 | 225 | break; |
|---|
| 164 | 226 | } |
|---|
| … | … | |
| 172 | 234 | {"forked mqueue", test_mqueue} |
|---|
| 173 | 235 | , |
|---|
| | 236 | {"forked chunked mqueue", test_chunked_mqueue} |
|---|
| | 237 | , |
|---|
| 174 | 238 | CU_TEST_INFO_NULL |
|---|
| 175 | 239 | }; |
|---|
| 176 | 240 | |
|---|
| 177 | 241 | /* Read a message from mqueue */ |
|---|
| 178 | | static void do_parent() |
|---|
| | 242 | static void do_parent(void) |
|---|
| 179 | 243 | { |
|---|
| 180 | 244 | char *data = NULL; |
|---|
| … | … | |
| 196 | 260 | } |
|---|
| 197 | 261 | |
|---|
| 198 | | static void do_child() |
|---|
| | 262 | static void do_child(void) |
|---|
| 199 | 263 | { |
|---|
| 200 | 264 | int rbytes = 0; |
|---|
| … | … | |
| 227 | 291 | } |
|---|
| 228 | 292 | |
|---|
| | 293 | static void do_parent_chunked(void) |
|---|
| | 294 | { |
|---|
| | 295 | char *data = NULL; |
|---|
| | 296 | int len = 0; |
|---|
| | 297 | |
|---|
| | 298 | size_t total_read = 0, j = 0; |
|---|
| | 299 | while (!sipc_recv_data(reader2_ipc, &data, &len)) { |
|---|
| | 300 | CU_ASSERT_PTR_NOT_NULL_FATAL(data); |
|---|
| | 301 | CU_ASSERT_FATAL(len > 0); |
|---|
| | 302 | |
|---|
| | 303 | size_t i; |
|---|
| | 304 | for (i = 0; i < len; i++) { |
|---|
| | 305 | if (total_read % 512 == 0) { |
|---|
| | 306 | j++; |
|---|
| | 307 | } |
|---|
| | 308 | CU_ASSERT(data[i] == j); |
|---|
| | 309 | total_read++; |
|---|
| | 310 | } |
|---|
| | 311 | |
|---|
| | 312 | free(data); |
|---|
| | 313 | if (total_read >= BIG_DATA_LEN) { |
|---|
| | 314 | return; |
|---|
| | 315 | } |
|---|
| | 316 | } |
|---|
| | 317 | CU_FAIL("Did not receive as much data as expected."); |
|---|
| | 318 | } |
|---|
| | 319 | |
|---|
| | 320 | static void do_child_chunked(void) |
|---|
| | 321 | { |
|---|
| | 322 | char *data = NULL; |
|---|
| | 323 | |
|---|
| | 324 | /* Get pointer to IPC's data member */ |
|---|
| | 325 | data = sipc_get_data_ptr(writer2_ipc); |
|---|
| | 326 | CU_ASSERT_PTR_NOT_NULL(data); |
|---|
| | 327 | |
|---|
| | 328 | size_t i, j = 0; |
|---|
| | 329 | for (i = 0; i < BIG_DATA_LEN; i++) { |
|---|
| | 330 | if (i % 512 == 0) { |
|---|
| | 331 | j++; |
|---|
| | 332 | } |
|---|
| | 333 | data[i] = j; |
|---|
| | 334 | } |
|---|
| | 335 | |
|---|
| | 336 | /* Send data, then end of xmit */ |
|---|
| | 337 | CU_ASSERT(sipc_send_data(writer2_ipc, i) == 0); |
|---|
| | 338 | |
|---|
| | 339 | /* Cleanup handle here */ |
|---|
| | 340 | sipc_close(writer2_ipc); |
|---|
| | 341 | } |
|---|
| | 342 | |
|---|
| 229 | 343 | static int send_end_xmit(sipc_t *ipc) |
|---|
| 230 | 344 | { |
|---|
| r33 |
r39 |
|
| 1 | 1 | /* Author: David Windsor <dwindsor@tresys.com> |
|---|
| 2 | 2 | * |
|---|
| 3 | | * Copyright (C) 2006, 2007 Tresys Technology, LLC |
|---|
| | 3 | * Copyright (C) 2006 - 2008 Tresys Technology, LLC |
|---|
| 4 | 4 | * Developed Under US JFCOM Sponsorship |
|---|
| 5 | 5 | * |
|---|
| r1 |
r39 |
|
| 3 | 3 | touch shm_ipc_test |
|---|
| 4 | 4 | touch mq_ipc_test |
|---|
| | 5 | touch mq2_ipc_test |
|---|
| 5 | 6 | ./test_ipc |
|---|
| 6 | 7 | rm -f shm_ipc_test |
|---|
| 7 | 8 | rm -f mq_ipc_test |
|---|
| | 9 | rm -f mq2_ipc_test |
|---|
| r37 |
r39 |
|
| 1 | 1 | /* Author: David Windsor <dwindsor@tresys.com> |
|---|
| 2 | 2 | * |
|---|
| 3 | | * Copyright (C) 2006, 2007 Tresys Technology, LLC |
|---|
| | 3 | * Copyright (C) 2006 - 2008 Tresys Technology, LLC |
|---|
| 4 | 4 | * Developed Under US JFCOM Sponsorship |
|---|
| 5 | 5 | * |
|---|
| … | … | |
| 129 | 129 | } |
|---|
| 130 | 130 | |
|---|
| 131 | | static void test_unforked_shm(void) |
|---|
| 132 | | { |
|---|
| 133 | | static char message[] = "HELLO,"; |
|---|
| 134 | | size_t message_len = sizeof(message); |
|---|
| 135 | | char *shm_data = sipc_get_data_ptr(writer_ipc); |
|---|
| 136 | | int recv_len; |
|---|
| 137 | | char *recv_data; |
|---|
| 138 | | |
|---|
| 139 | | CU_ASSERT_PTR_NOT_NULL_FATAL(shm_data); |
|---|
| 140 | | strcpy(shm_data, message); |
|---|
| 141 | | CU_ASSERT(sipc_send_data(writer_ipc, message_len) == 0); |
|---|
| 142 | | CU_ASSERT(sipc_recv_data(reader_ipc, &recv_data, &recv_len) == 0); |
|---|
| 143 | | CU_ASSERT(recv_len >= message_len); |
|---|
| 144 | | CU_ASSERT(memcmp(recv_data, message, message_len) == 0); |
|---|
| 145 | | CU_ASSERT(sipc_shm_recv_done(reader_ipc) == 0); |
|---|
| 146 | | } |
|---|
| 147 | | |
|---|
| 148 | 131 | static void test_shm(void) |
|---|
| 149 | 132 | { |
|---|
| … | … | |
| 193 | 176 | |
|---|
| 194 | 177 | CU_TestInfo shm_tests[] = { |
|---|
| 195 | | {"unforked shm", test_unforked_shm} |
|---|
| 196 | | , |
|---|
| 197 | 178 | {"forked shm", test_shm} |
|---|
| 198 | 179 | , |
|---|
| … | … | |
| 305 | 286 | while (!sipc_recv_data(reader_ipc, &data, &len)) { |
|---|
| 306 | 287 | if (END_XMIT(data)) { |
|---|
| 307 | | sipc_shm_recv_done(reader_ipc); |
|---|
| 308 | 288 | break; |
|---|
| 309 | 289 | } |
|---|
| … | … | |
| 315 | 295 | CU_PASS("Successfully read a message via SYSV shared memory"); |
|---|
| 316 | 296 | |
|---|
| | 297 | /* until sipc_shm_recv_done() is called, further reading of |
|---|
| | 298 | data will cause a non-recoverable state */ |
|---|
| | 299 | /* |
|---|
| | 300 | CU_ASSERT(sipc_recv_data(reader_ipc, &data, &len) < 0); |
|---|
| | 301 | */ |
|---|
| | 302 | sipc_shm_recv_done(reader_ipc); |
|---|
| | 303 | |
|---|
| 317 | 304 | for (i = 0; i < DATA_LEN; i++) { |
|---|
| 318 | 305 | if (recv_data[i] != i % 256) { |
|---|
| r33 |
r39 |
|
| 1 | 1 | /* Author: David Windsor <dwindsor@tresys.com> |
|---|
| 2 | 2 | * |
|---|
| 3 | | * Copyright (C) 2006, 2007 Tresys Technology, LLC |
|---|
| | 3 | * Copyright (C) 2006 - 2008 Tresys Technology, LLC |
|---|
| 4 | 4 | * Developed Under US JFCOM Sponsorship |
|---|
| 5 | 5 | * |
|---|
| r35 |
r39 |
|
| 1 | 1 | /* Author: David Windsor <dwindsor@tresys.com> |
|---|
| 2 | 2 | * |
|---|
| 3 | | * Copyright (C) 2006, 2007 Tresys Technology, LLC |
|---|
| | 3 | * Copyright (C) 2006 - 2008 Tresys Technology, LLC |
|---|
| 4 | 4 | * Developed Under US JFCOM Sponsorship |
|---|
| 5 | 5 | * |
|---|
Download in other formats:
* Generating other formats may take time.