testdup.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_general.h"
  17. #include "fspr_pools.h"
  18. #include "fspr_errno.h"
  19. #include "fspr_file_io.h"
  20. #include "testutil.h"
  21. #define TEST "Testing\n"
  22. #define TEST2 "Testing again\n"
  23. #define FILEPATH "data/"
  24. static void test_file_dup(abts_case *tc, void *data)
  25. {
  26. fspr_file_t *file1 = NULL;
  27. fspr_file_t *file3 = NULL;
  28. fspr_status_t rv;
  29. fspr_finfo_t finfo;
  30. /* First, create a new file, empty... */
  31. rv = fspr_file_open(&file1, FILEPATH "testdup.file",
  32. APR_READ | APR_WRITE | APR_CREATE |
  33. APR_DELONCLOSE, APR_OS_DEFAULT, p);
  34. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  35. ABTS_PTR_NOTNULL(tc, file1);
  36. rv = fspr_file_dup(&file3, file1, p);
  37. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  38. ABTS_PTR_NOTNULL(tc, file3);
  39. rv = fspr_file_close(file1);
  40. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  41. /* cleanup after ourselves */
  42. rv = fspr_file_close(file3);
  43. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  44. rv = fspr_stat(&finfo, FILEPATH "testdup.file", APR_FINFO_NORM, p);
  45. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOENT(rv));
  46. }
  47. static void test_file_readwrite(abts_case *tc, void *data)
  48. {
  49. fspr_file_t *file1 = NULL;
  50. fspr_file_t *file3 = NULL;
  51. fspr_status_t rv;
  52. fspr_finfo_t finfo;
  53. fspr_size_t txtlen = sizeof(TEST);
  54. char buff[50];
  55. fspr_off_t fpos;
  56. /* First, create a new file, empty... */
  57. rv = fspr_file_open(&file1, FILEPATH "testdup.readwrite.file",
  58. APR_READ | APR_WRITE | APR_CREATE |
  59. APR_DELONCLOSE, APR_OS_DEFAULT, p);
  60. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  61. ABTS_PTR_NOTNULL(tc, file1);
  62. rv = fspr_file_dup(&file3, file1, p);
  63. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  64. ABTS_PTR_NOTNULL(tc, file3);
  65. rv = fspr_file_write(file3, TEST, &txtlen);
  66. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  67. ABTS_INT_EQUAL(tc, sizeof(TEST), txtlen);
  68. fpos = 0;
  69. rv = fspr_file_seek(file1, APR_SET, &fpos);
  70. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  71. ABTS_ASSERT(tc, "File position mismatch, expected 0", fpos == 0);
  72. txtlen = 50;
  73. rv = fspr_file_read(file1, buff, &txtlen);
  74. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  75. ABTS_STR_EQUAL(tc, TEST, buff);
  76. /* cleanup after ourselves */
  77. rv = fspr_file_close(file1);
  78. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  79. rv = fspr_file_close(file3);
  80. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  81. rv = fspr_stat(&finfo, FILEPATH "testdup.readwrite.file", APR_FINFO_NORM, p);
  82. ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOENT(rv));
  83. }
  84. static void test_dup2(abts_case *tc, void *data)
  85. {
  86. fspr_file_t *testfile = NULL;
  87. fspr_file_t *errfile = NULL;
  88. fspr_file_t *saveerr = NULL;
  89. fspr_status_t rv;
  90. rv = fspr_file_open(&testfile, FILEPATH "testdup2.file",
  91. APR_READ | APR_WRITE | APR_CREATE |
  92. APR_DELONCLOSE, APR_OS_DEFAULT, p);
  93. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  94. ABTS_PTR_NOTNULL(tc, testfile);
  95. rv = fspr_file_open_stderr(&errfile, p);
  96. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  97. /* Set aside the real errfile */
  98. rv = fspr_file_dup(&saveerr, errfile, p);
  99. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  100. ABTS_PTR_NOTNULL(tc, saveerr);
  101. rv = fspr_file_dup2(errfile, testfile, p);
  102. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  103. ABTS_PTR_NOTNULL(tc, errfile);
  104. fspr_file_close(testfile);
  105. rv = fspr_file_dup2(errfile, saveerr, p);
  106. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  107. ABTS_PTR_NOTNULL(tc, errfile);
  108. }
  109. static void test_dup2_readwrite(abts_case *tc, void *data)
  110. {
  111. fspr_file_t *errfile = NULL;
  112. fspr_file_t *testfile = NULL;
  113. fspr_file_t *saveerr = NULL;
  114. fspr_status_t rv;
  115. fspr_size_t txtlen = sizeof(TEST);
  116. char buff[50];
  117. fspr_off_t fpos;
  118. rv = fspr_file_open(&testfile, FILEPATH "testdup2.readwrite.file",
  119. APR_READ | APR_WRITE | APR_CREATE |
  120. APR_DELONCLOSE, APR_OS_DEFAULT, p);
  121. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  122. ABTS_PTR_NOTNULL(tc, testfile);
  123. rv = fspr_file_open_stderr(&errfile, p);
  124. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  125. /* Set aside the real errfile */
  126. rv = fspr_file_dup(&saveerr, errfile, p);
  127. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  128. ABTS_PTR_NOTNULL(tc, saveerr);
  129. rv = fspr_file_dup2(errfile, testfile, p);
  130. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  131. ABTS_PTR_NOTNULL(tc, errfile);
  132. txtlen = sizeof(TEST2);
  133. rv = fspr_file_write(errfile, TEST2, &txtlen);
  134. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  135. ABTS_INT_EQUAL(tc, sizeof(TEST2), txtlen);
  136. fpos = 0;
  137. rv = fspr_file_seek(testfile, APR_SET, &fpos);
  138. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  139. ABTS_ASSERT(tc, "File position mismatch, expected 0", fpos == 0);
  140. txtlen = 50;
  141. rv = fspr_file_read(testfile, buff, &txtlen);
  142. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  143. ABTS_STR_EQUAL(tc, TEST2, buff);
  144. fspr_file_close(testfile);
  145. rv = fspr_file_dup2(errfile, saveerr, p);
  146. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  147. ABTS_PTR_NOTNULL(tc, errfile);
  148. }
  149. abts_suite *testdup(abts_suite *suite)
  150. {
  151. suite = ADD_SUITE(suite)
  152. abts_run_test(suite, test_file_dup, NULL);
  153. abts_run_test(suite, test_file_readwrite, NULL);
  154. abts_run_test(suite, test_dup2, NULL);
  155. abts_run_test(suite, test_dup2_readwrite, NULL);
  156. return suite;
  157. }