1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
#include "apr_general.h"
19
#include "apr_pools.h"
20
#include "apr_errno.h"
21
#include "apr_file_io.h"
24
#define TEST "Testing\n"
25
#define TEST2 "Testing again\n"
26
#define FILEPATH "data/"
28
static void test_file_dup(abts_case *tc, void *data)
30
apr_file_t *file1 = NULL;
31
apr_file_t *file3 = NULL;
35
/* First, create a new file, empty... */
36
rv = apr_file_open(&file1, FILEPATH "testdup.file",
37
APR_READ | APR_WRITE | APR_CREATE |
38
APR_DELONCLOSE, APR_OS_DEFAULT, p);
39
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
40
ABTS_PTR_NOTNULL(tc, file1);
42
rv = apr_file_dup(&file3, file1, p);
43
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
44
ABTS_PTR_NOTNULL(tc, file3);
46
rv = apr_file_close(file1);
47
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
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));
56
static void test_file_readwrite(abts_case *tc, void *data)
58
apr_file_t *file1 = NULL;
59
apr_file_t *file3 = NULL;
62
apr_size_t txtlen = sizeof(TEST);
66
/* First, create a new file, empty... */
67
rv = apr_file_open(&file1, FILEPATH "testdup.readwrite.file",
68
APR_READ | APR_WRITE | APR_CREATE |
69
APR_DELONCLOSE, APR_OS_DEFAULT, p);
70
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
71
ABTS_PTR_NOTNULL(tc, file1);
73
rv = apr_file_dup(&file3, file1, p);
74
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
75
ABTS_PTR_NOTNULL(tc, file3);
77
rv = apr_file_write(file3, TEST, &txtlen);
78
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
79
ABTS_INT_EQUAL(tc, sizeof(TEST), txtlen);
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);
87
rv = apr_file_read(file1, buff, &txtlen);
88
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
89
ABTS_STR_EQUAL(tc, TEST, buff);
91
/* cleanup after ourselves */
92
rv = apr_file_close(file1);
93
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
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));
101
static void test_dup2(abts_case *tc, void *data)
103
apr_file_t *testfile = NULL;
104
apr_file_t *errfile = NULL;
105
apr_file_t *saveerr = NULL;
108
rv = apr_file_open(&testfile, FILEPATH "testdup2.file",
109
APR_READ | APR_WRITE | APR_CREATE |
110
APR_DELONCLOSE, APR_OS_DEFAULT, p);
111
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
112
ABTS_PTR_NOTNULL(tc, testfile);
114
rv = apr_file_open_stderr(&errfile, p);
115
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
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);
122
rv = apr_file_dup2(errfile, testfile, p);
123
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
124
ABTS_PTR_NOTNULL(tc, errfile);
126
apr_file_close(testfile);
128
rv = apr_file_dup2(errfile, saveerr, p);
129
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
130
ABTS_PTR_NOTNULL(tc, errfile);
133
static void test_dup2_readwrite(abts_case *tc, void *data)
135
apr_file_t *errfile = NULL;
136
apr_file_t *testfile = NULL;
137
apr_file_t *saveerr = NULL;
139
apr_size_t txtlen = sizeof(TEST);
143
rv = apr_file_open(&testfile, FILEPATH "testdup2.readwrite.file",
144
APR_READ | APR_WRITE | APR_CREATE |
145
APR_DELONCLOSE, APR_OS_DEFAULT, p);
146
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
147
ABTS_PTR_NOTNULL(tc, testfile);
149
rv = apr_file_open_stderr(&errfile, p);
150
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
152
/* Set aside the real errfile */
153
rv = apr_file_dup(&saveerr, errfile, p);
154
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
155
ABTS_PTR_NOTNULL(tc, saveerr);
157
rv = apr_file_dup2(errfile, testfile, p);
158
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
159
ABTS_PTR_NOTNULL(tc, errfile);
161
txtlen = sizeof(TEST2);
162
rv = apr_file_write(errfile, TEST2, &txtlen);
163
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
164
ABTS_INT_EQUAL(tc, sizeof(TEST2), txtlen);
167
rv = apr_file_seek(testfile, APR_SET, &fpos);
168
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
169
ABTS_ASSERT(tc, "File position mismatch, expected 0", fpos == 0);
172
rv = apr_file_read(testfile, buff, &txtlen);
173
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
174
ABTS_STR_EQUAL(tc, TEST2, buff);
176
apr_file_close(testfile);
178
rv = apr_file_dup2(errfile, saveerr, p);
179
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
180
ABTS_PTR_NOTNULL(tc, errfile);
183
abts_suite *testdup(abts_suite *suite)
185
suite = ADD_SUITE(suite)
187
abts_run_test(suite, test_file_dup, NULL);
188
abts_run_test(suite, test_file_readwrite, NULL);
189
abts_run_test(suite, test_dup2, NULL);
190
abts_run_test(suite, test_dup2_readwrite, NULL);