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
17
18 #include "apr_general.h"
19 #include "apr_pools.h"
20 #include "apr_errno.h"
21 #include "apr_file_io.h"
22 #include "testutil.h"
23
24 #define TEST "Testing\n"
25 #define TEST2 "Testing again\n"
26 #define FILEPATH "data/"
27
test_file_dup(abts_case * tc,void * data)28 static void test_file_dup(abts_case *tc, void *data)
29 {
30 apr_file_t *file1 = NULL;
31 apr_file_t *file3 = NULL;
32 apr_status_t rv;
33 apr_finfo_t finfo;
34
35 /* First, create a new file, empty... */
36 rv = apr_file_open(&file1, FILEPATH "testdup.file",
37 APR_FOPEN_READ | APR_FOPEN_WRITE | APR_FOPEN_CREATE |
38 APR_FOPEN_DELONCLOSE, APR_OS_DEFAULT, p);
39 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
40 ABTS_PTR_NOTNULL(tc, file1);
41
42 rv = apr_file_dup(&file3, file1, p);
43 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
44 ABTS_PTR_NOTNULL(tc, file3);
45
46 rv = apr_file_close(file1);
47 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
48
49 /* cleanup after ourselves */
50 rv = apr_file_close(file3);
51 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
52 rv = apr_stat(&finfo, FILEPATH "testdup.file", APR_FINFO_NORM, p);
53 ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOENT(rv));
54 }
55
test_file_readwrite(abts_case * tc,void * data)56 static void test_file_readwrite(abts_case *tc, void *data)
57 {
58 apr_file_t *file1 = NULL;
59 apr_file_t *file3 = NULL;
60 apr_status_t rv;
61 apr_finfo_t finfo;
62 apr_size_t txtlen = sizeof(TEST);
63 char buff[50];
64 apr_off_t fpos;
65
66 /* First, create a new file, empty... */
67 rv = apr_file_open(&file1, FILEPATH "testdup.readwrite.file",
68 APR_FOPEN_READ | APR_FOPEN_WRITE | APR_FOPEN_CREATE |
69 APR_FOPEN_DELONCLOSE, APR_OS_DEFAULT, p);
70 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
71 ABTS_PTR_NOTNULL(tc, file1);
72
73 rv = apr_file_dup(&file3, file1, p);
74 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
75 ABTS_PTR_NOTNULL(tc, file3);
76
77 rv = apr_file_write(file3, TEST, &txtlen);
78 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
79 ABTS_SIZE_EQUAL(tc, sizeof(TEST), txtlen);
80
81 fpos = 0;
82 rv = apr_file_seek(file1, APR_SET, &fpos);
83 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
84 ABTS_ASSERT(tc, "File position mismatch, expected 0", fpos == 0);
85
86 txtlen = 50;
87 rv = apr_file_read(file1, buff, &txtlen);
88 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
89 ABTS_STR_EQUAL(tc, TEST, buff);
90
91 /* cleanup after ourselves */
92 rv = apr_file_close(file1);
93 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
94
95 rv = apr_file_close(file3);
96 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
97 rv = apr_stat(&finfo, FILEPATH "testdup.readwrite.file", APR_FINFO_NORM, p);
98 ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOENT(rv));
99 }
100
test_dup2(abts_case * tc,void * data)101 static void test_dup2(abts_case *tc, void *data)
102 {
103 apr_file_t *testfile = NULL;
104 apr_file_t *errfile = NULL;
105 apr_file_t *saveerr = NULL;
106 apr_status_t rv;
107
108 rv = apr_file_open(&testfile, FILEPATH "testdup2.file",
109 APR_FOPEN_READ | APR_FOPEN_WRITE | APR_FOPEN_CREATE |
110 APR_FOPEN_DELONCLOSE, APR_OS_DEFAULT, p);
111 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
112 ABTS_PTR_NOTNULL(tc, testfile);
113
114 rv = apr_file_open_stderr(&errfile, p);
115 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
116
117 /* Set aside the real errfile */
118 rv = apr_file_dup(&saveerr, errfile, p);
119 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
120 ABTS_PTR_NOTNULL(tc, saveerr);
121
122 rv = apr_file_dup2(errfile, testfile, p);
123 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
124 ABTS_PTR_NOTNULL(tc, errfile);
125
126 apr_file_close(testfile);
127
128 rv = apr_file_dup2(errfile, saveerr, p);
129 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
130 ABTS_PTR_NOTNULL(tc, errfile);
131
132 apr_file_close(saveerr);
133 }
134
test_dup2_readwrite(abts_case * tc,void * data)135 static void test_dup2_readwrite(abts_case *tc, void *data)
136 {
137 apr_file_t *errfile = NULL;
138 apr_file_t *testfile = NULL;
139 apr_file_t *saveerr = NULL;
140 apr_status_t rv;
141 apr_size_t txtlen = sizeof(TEST);
142 char buff[50];
143 apr_off_t fpos;
144
145 rv = apr_file_open(&testfile, FILEPATH "testdup2.readwrite.file",
146 APR_FOPEN_READ | APR_FOPEN_WRITE | APR_FOPEN_CREATE |
147 APR_FOPEN_DELONCLOSE, APR_OS_DEFAULT, p);
148 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
149 ABTS_PTR_NOTNULL(tc, testfile);
150
151 rv = apr_file_open_stderr(&errfile, p);
152 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
153
154 /* Set aside the real errfile */
155 rv = apr_file_dup(&saveerr, errfile, p);
156 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
157 ABTS_PTR_NOTNULL(tc, saveerr);
158
159 rv = apr_file_dup2(errfile, testfile, p);
160 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
161 ABTS_PTR_NOTNULL(tc, errfile);
162
163 txtlen = sizeof(TEST2);
164 rv = apr_file_write(errfile, TEST2, &txtlen);
165 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
166 ABTS_SIZE_EQUAL(tc, sizeof(TEST2), txtlen);
167
168 fpos = 0;
169 rv = apr_file_seek(testfile, APR_SET, &fpos);
170 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
171 ABTS_ASSERT(tc, "File position mismatch, expected 0", fpos == 0);
172
173 txtlen = 50;
174 rv = apr_file_read(testfile, buff, &txtlen);
175 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
176 ABTS_STR_EQUAL(tc, TEST2, buff);
177
178 apr_file_close(testfile);
179
180 rv = apr_file_dup2(errfile, saveerr, p);
181 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
182 ABTS_PTR_NOTNULL(tc, errfile);
183
184 apr_file_close(saveerr);
185 }
186
testdup(abts_suite * suite)187 abts_suite *testdup(abts_suite *suite)
188 {
189 suite = ADD_SUITE(suite)
190
191 abts_run_test(suite, test_file_dup, NULL);
192 abts_run_test(suite, test_file_readwrite, NULL);
193 abts_run_test(suite, test_dup2, NULL);
194 abts_run_test(suite, test_dup2_readwrite, NULL);
195
196 return suite;
197 }
198
199