testproc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "fspr_thread_proc.h"
  17. #include "fspr_errno.h"
  18. #include "fspr_general.h"
  19. #include "fspr_lib.h"
  20. #include "fspr_strings.h"
  21. #include "testutil.h"
  22. #define TESTSTR "This is a test"
  23. static fspr_proc_t newproc;
  24. static void test_create_proc(abts_case *tc, void *data)
  25. {
  26. const char *args[2];
  27. fspr_procattr_t *attr;
  28. fspr_file_t *testfile = NULL;
  29. fspr_status_t rv;
  30. fspr_size_t length;
  31. char *buf;
  32. rv = fspr_procattr_create(&attr, p);
  33. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  34. rv = fspr_procattr_io_set(attr, APR_FULL_BLOCK, APR_FULL_BLOCK,
  35. APR_NO_PIPE);
  36. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  37. rv = fspr_procattr_dir_set(attr, "data");
  38. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  39. rv = fspr_procattr_cmdtype_set(attr, APR_PROGRAM);
  40. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  41. args[0] = "proc_child" EXTENSION;
  42. args[1] = NULL;
  43. rv = fspr_proc_create(&newproc, "../proc_child" EXTENSION, args, NULL,
  44. attr, p);
  45. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  46. testfile = newproc.in;
  47. length = strlen(TESTSTR);
  48. rv = fspr_file_write(testfile, TESTSTR, &length);
  49. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  50. ABTS_INT_EQUAL(tc, strlen(TESTSTR), length);
  51. testfile = newproc.out;
  52. length = 256;
  53. buf = fspr_pcalloc(p, length);
  54. rv = fspr_file_read(testfile, buf, &length);
  55. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  56. ABTS_STR_EQUAL(tc, TESTSTR, buf);
  57. }
  58. static void test_proc_wait(abts_case *tc, void *data)
  59. {
  60. fspr_status_t rv;
  61. rv = fspr_proc_wait(&newproc, NULL, NULL, APR_WAIT);
  62. ABTS_INT_EQUAL(tc, APR_CHILD_DONE, rv);
  63. }
  64. static void test_file_redir(abts_case *tc, void *data)
  65. {
  66. fspr_file_t *testout = NULL;
  67. fspr_file_t *testerr = NULL;
  68. fspr_off_t offset;
  69. fspr_status_t rv;
  70. const char *args[2];
  71. fspr_procattr_t *attr;
  72. fspr_file_t *testfile = NULL;
  73. fspr_size_t length;
  74. char *buf;
  75. testfile = NULL;
  76. rv = fspr_file_open(&testfile, "data/stdin",
  77. APR_READ | APR_WRITE | APR_CREATE | APR_EXCL,
  78. APR_OS_DEFAULT, p);
  79. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  80. rv = fspr_file_open(&testout, "data/stdout",
  81. APR_READ | APR_WRITE | APR_CREATE | APR_EXCL,
  82. APR_OS_DEFAULT, p);
  83. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  84. rv = fspr_file_open(&testerr, "data/stderr",
  85. APR_READ | APR_WRITE | APR_CREATE | APR_EXCL,
  86. APR_OS_DEFAULT, p);
  87. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  88. length = strlen(TESTSTR);
  89. fspr_file_write(testfile, TESTSTR, &length);
  90. offset = 0;
  91. rv = fspr_file_seek(testfile, APR_SET, &offset);
  92. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  93. ABTS_ASSERT(tc, "File position mismatch, expected 0", offset == 0);
  94. rv = fspr_procattr_create(&attr, p);
  95. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  96. rv = fspr_procattr_child_in_set(attr, testfile, NULL);
  97. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  98. rv = fspr_procattr_child_out_set(attr, testout, NULL);
  99. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  100. rv = fspr_procattr_child_err_set(attr, testerr, NULL);
  101. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  102. rv = fspr_procattr_dir_set(attr, "data");
  103. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  104. rv = fspr_procattr_cmdtype_set(attr, APR_PROGRAM);
  105. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  106. args[0] = "proc_child";
  107. args[1] = NULL;
  108. rv = fspr_proc_create(&newproc, "../proc_child" EXTENSION, args, NULL,
  109. attr, p);
  110. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  111. rv = fspr_proc_wait(&newproc, NULL, NULL, APR_WAIT);
  112. ABTS_INT_EQUAL(tc, APR_CHILD_DONE, rv);
  113. offset = 0;
  114. rv = fspr_file_seek(testout, APR_SET, &offset);
  115. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  116. length = 256;
  117. buf = fspr_pcalloc(p, length);
  118. rv = fspr_file_read(testout, buf, &length);
  119. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  120. ABTS_STR_EQUAL(tc, TESTSTR, buf);
  121. fspr_file_close(testfile);
  122. fspr_file_close(testout);
  123. fspr_file_close(testerr);
  124. rv = fspr_file_remove("data/stdin", p);;
  125. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  126. rv = fspr_file_remove("data/stdout", p);;
  127. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  128. rv = fspr_file_remove("data/stderr", p);;
  129. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  130. }
  131. abts_suite *testproc(abts_suite *suite)
  132. {
  133. suite = ADD_SUITE(suite)
  134. abts_run_test(suite, test_create_proc, NULL);
  135. abts_run_test(suite, test_proc_wait, NULL);
  136. abts_run_test(suite, test_file_redir, NULL);
  137. return suite;
  138. }