2
0

testpipe.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdlib.h>
  17. #include "testutil.h"
  18. #include "fspr_file_io.h"
  19. #include "fspr_errno.h"
  20. #include "fspr_general.h"
  21. #include "fspr_lib.h"
  22. #include "fspr_thread_proc.h"
  23. #include "fspr_strings.h"
  24. static fspr_file_t *readp = NULL;
  25. static fspr_file_t *writep = NULL;
  26. static void create_pipe(abts_case *tc, void *data)
  27. {
  28. fspr_status_t rv;
  29. rv = fspr_file_pipe_create(&readp, &writep, p);
  30. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  31. ABTS_PTR_NOTNULL(tc, readp);
  32. ABTS_PTR_NOTNULL(tc, writep);
  33. }
  34. static void close_pipe(abts_case *tc, void *data)
  35. {
  36. fspr_status_t rv;
  37. fspr_size_t nbytes = 256;
  38. char buf[256];
  39. rv = fspr_file_close(readp);
  40. rv = fspr_file_close(writep);
  41. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  42. rv = fspr_file_read(readp, buf, &nbytes);
  43. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_EBADF(rv));
  44. }
  45. static void set_timeout(abts_case *tc, void *data)
  46. {
  47. fspr_status_t rv;
  48. fspr_interval_time_t timeout;
  49. rv = fspr_file_pipe_create(&readp, &writep, p);
  50. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  51. ABTS_PTR_NOTNULL(tc, readp);
  52. ABTS_PTR_NOTNULL(tc, writep);
  53. rv = fspr_file_pipe_timeout_get(readp, &timeout);
  54. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  55. ABTS_ASSERT(tc, "Timeout mismatch, expected -1", timeout == -1);
  56. rv = fspr_file_pipe_timeout_set(readp, fspr_time_from_sec(1));
  57. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  58. rv = fspr_file_pipe_timeout_get(readp, &timeout);
  59. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  60. ABTS_ASSERT(tc, "Timeout mismatch, expected 1 second",
  61. timeout == fspr_time_from_sec(1));
  62. }
  63. static void read_write(abts_case *tc, void *data)
  64. {
  65. fspr_status_t rv;
  66. char *buf;
  67. fspr_size_t nbytes;
  68. nbytes = strlen("this is a test");
  69. buf = (char *)fspr_palloc(p, nbytes + 1);
  70. rv = fspr_file_pipe_create(&readp, &writep, p);
  71. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  72. ABTS_PTR_NOTNULL(tc, readp);
  73. ABTS_PTR_NOTNULL(tc, writep);
  74. rv = fspr_file_pipe_timeout_set(readp, fspr_time_from_sec(1));
  75. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  76. if (!rv) {
  77. rv = fspr_file_read(readp, buf, &nbytes);
  78. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_TIMEUP(rv));
  79. ABTS_INT_EQUAL(tc, 0, nbytes);
  80. }
  81. }
  82. static void read_write_notimeout(abts_case *tc, void *data)
  83. {
  84. fspr_status_t rv;
  85. char *buf = "this is a test";
  86. char *input;
  87. fspr_size_t nbytes;
  88. nbytes = strlen("this is a test");
  89. rv = fspr_file_pipe_create(&readp, &writep, p);
  90. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  91. ABTS_PTR_NOTNULL(tc, readp);
  92. ABTS_PTR_NOTNULL(tc, writep);
  93. rv = fspr_file_write(writep, buf, &nbytes);
  94. ABTS_INT_EQUAL(tc, strlen("this is a test"), nbytes);
  95. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  96. nbytes = 256;
  97. input = fspr_pcalloc(p, nbytes + 1);
  98. rv = fspr_file_read(readp, input, &nbytes);
  99. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  100. ABTS_INT_EQUAL(tc, strlen("this is a test"), nbytes);
  101. ABTS_STR_EQUAL(tc, "this is a test", input);
  102. }
  103. static void test_pipe_writefull(abts_case *tc, void *data)
  104. {
  105. int iterations = 1000;
  106. int i;
  107. int bytes_per_iteration = 8000;
  108. char *buf = (char *)malloc(bytes_per_iteration);
  109. char responsebuf[128];
  110. fspr_size_t nbytes;
  111. int bytes_processed;
  112. fspr_proc_t proc = {0};
  113. fspr_procattr_t *procattr;
  114. const char *args[2];
  115. fspr_status_t rv;
  116. fspr_exit_why_e why;
  117. rv = fspr_procattr_create(&procattr, p);
  118. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  119. rv = fspr_procattr_io_set(procattr, APR_CHILD_BLOCK, APR_CHILD_BLOCK,
  120. APR_CHILD_BLOCK);
  121. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  122. rv = fspr_procattr_error_check_set(procattr, 1);
  123. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  124. args[0] = "readchild" EXTENSION;
  125. args[1] = NULL;
  126. rv = fspr_proc_create(&proc, "./readchild" EXTENSION, args, NULL, procattr, p);
  127. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  128. rv = fspr_file_pipe_timeout_set(proc.in, fspr_time_from_sec(10));
  129. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  130. rv = fspr_file_pipe_timeout_set(proc.out, fspr_time_from_sec(10));
  131. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  132. i = iterations;
  133. do {
  134. rv = fspr_file_write_full(proc.in, buf, bytes_per_iteration, NULL);
  135. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  136. } while (--i);
  137. free(buf);
  138. rv = fspr_file_close(proc.in);
  139. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  140. nbytes = sizeof(responsebuf);
  141. rv = fspr_file_read(proc.out, responsebuf, &nbytes);
  142. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  143. bytes_processed = (int)fspr_strtoi64(responsebuf, NULL, 10);
  144. ABTS_INT_EQUAL(tc, iterations * bytes_per_iteration, bytes_processed);
  145. ABTS_ASSERT(tc, "wait for child process",
  146. fspr_proc_wait(&proc, NULL, &why, APR_WAIT) == APR_CHILD_DONE);
  147. ABTS_ASSERT(tc, "child terminated normally", why == APR_PROC_EXIT);
  148. }
  149. abts_suite *testpipe(abts_suite *suite)
  150. {
  151. suite = ADD_SUITE(suite)
  152. abts_run_test(suite, create_pipe, NULL);
  153. abts_run_test(suite, close_pipe, NULL);
  154. abts_run_test(suite, set_timeout, NULL);
  155. abts_run_test(suite, close_pipe, NULL);
  156. abts_run_test(suite, read_write, NULL);
  157. abts_run_test(suite, close_pipe, NULL);
  158. abts_run_test(suite, read_write_notimeout, NULL);
  159. abts_run_test(suite, test_pipe_writefull, NULL);
  160. abts_run_test(suite, close_pipe, NULL);
  161. return suite;
  162. }