upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / test / testdup.c
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 "test_apr.h"
23
24 #define TEST "Testing\n"
25 #define TEST2 "Testing again\n"
26 #define FILEPATH "data/"
27
28 static void test_file_dup(CuTest *tc)
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_READ | APR_WRITE | APR_CREATE |
38                        APR_DELONCLOSE, APR_OS_DEFAULT, p);
39     CuAssertIntEquals(tc, APR_SUCCESS, rv);
40     CuAssertPtrNotNull(tc, file1);
41
42     rv = apr_file_dup(&file3, file1, p);
43     CuAssertIntEquals(tc, APR_SUCCESS, rv);
44     CuAssertPtrNotNull(tc, file3);
45
46     rv = apr_file_close(file1);
47     CuAssertIntEquals(tc, APR_SUCCESS, rv);
48
49     /* cleanup after ourselves */
50     rv = apr_file_close(file3);
51     CuAssertIntEquals(tc, APR_SUCCESS, rv);
52     rv = apr_stat(&finfo, FILEPATH "testdup.file", APR_FINFO_NORM, p);
53     CuAssertIntEquals(tc, 1, APR_STATUS_IS_ENOENT(rv));
54 }  
55
56 static void test_file_readwrite(CuTest *tc)
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_READ | APR_WRITE | APR_CREATE |
69                        APR_DELONCLOSE, APR_OS_DEFAULT, p);
70     CuAssertIntEquals(tc, APR_SUCCESS, rv);
71     CuAssertPtrNotNull(tc, file1);
72
73     rv = apr_file_dup(&file3, file1, p);
74     CuAssertIntEquals(tc, APR_SUCCESS, rv);
75     CuAssertPtrNotNull(tc, file3);
76
77     rv = apr_file_write(file3, TEST, &txtlen);
78     CuAssertIntEquals(tc, APR_SUCCESS, rv);
79     CuAssertIntEquals(tc, sizeof(TEST), txtlen);
80
81     fpos = 0;
82     rv = apr_file_seek(file1, APR_SET, &fpos);
83     CuAssertIntEquals(tc, APR_SUCCESS, rv);
84     CuAssert(tc, "File position mismatch, expected 0", fpos == 0);
85
86     txtlen = 50;
87     rv = apr_file_read(file1, buff, &txtlen);
88     CuAssertIntEquals(tc, APR_SUCCESS, rv);
89     CuAssertStrEquals(tc, TEST, buff);
90
91     /* cleanup after ourselves */
92     rv = apr_file_close(file1);
93     CuAssertIntEquals(tc, APR_SUCCESS, rv);
94
95     rv = apr_file_close(file3);
96     CuAssertIntEquals(tc, APR_SUCCESS, rv);
97     rv = apr_stat(&finfo, FILEPATH "testdup.readwrite.file", APR_FINFO_NORM, p);
98     CuAssertIntEquals(tc, 1, APR_STATUS_IS_ENOENT(rv));
99 }  
100
101 static void test_dup2(CuTest *tc)
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_READ | APR_WRITE | APR_CREATE |
110                        APR_DELONCLOSE, APR_OS_DEFAULT, p);
111     CuAssertIntEquals(tc, APR_SUCCESS, rv);
112     CuAssertPtrNotNull(tc, testfile);
113
114     rv = apr_file_open_stderr(&errfile, p);
115     CuAssertIntEquals(tc, APR_SUCCESS, rv);
116
117     /* Set aside the real errfile */
118     rv = apr_file_dup(&saveerr, errfile, p);
119     CuAssertIntEquals(tc, APR_SUCCESS, rv);
120     CuAssertPtrNotNull(tc, saveerr);
121
122     rv = apr_file_dup2(errfile, testfile, p);
123     CuAssertIntEquals(tc, APR_SUCCESS, rv);
124     CuAssertPtrNotNull(tc, errfile);
125
126     apr_file_close(testfile);
127
128     rv = apr_file_dup2(errfile, saveerr, p);
129     CuAssertIntEquals(tc, APR_SUCCESS, rv);
130     CuAssertPtrNotNull(tc, errfile);
131 }
132
133 static void test_dup2_readwrite(CuTest *tc)
134 {
135     apr_file_t *errfile = NULL;
136     apr_file_t *testfile = NULL;
137     apr_file_t *saveerr = NULL;
138     apr_status_t rv;
139     apr_size_t txtlen = sizeof(TEST);
140     char buff[50];
141     apr_off_t fpos;
142
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     CuAssertIntEquals(tc, APR_SUCCESS, rv);
147     CuAssertPtrNotNull(tc, testfile);
148
149     rv = apr_file_open_stderr(&errfile, p);
150     CuAssertIntEquals(tc, APR_SUCCESS, rv);
151
152     /* Set aside the real errfile */
153     rv = apr_file_dup(&saveerr, errfile, p);
154     CuAssertIntEquals(tc, APR_SUCCESS, rv);
155     CuAssertPtrNotNull(tc, saveerr);
156
157     rv = apr_file_dup2(errfile, testfile, p);
158     CuAssertIntEquals(tc, APR_SUCCESS, rv);
159     CuAssertPtrNotNull(tc, errfile);
160
161     txtlen = sizeof(TEST2);
162     rv = apr_file_write(errfile, TEST2, &txtlen);
163     CuAssertIntEquals(tc, APR_SUCCESS, rv);
164     CuAssertIntEquals(tc, sizeof(TEST2), txtlen);
165
166     fpos = 0;
167     rv = apr_file_seek(testfile, APR_SET, &fpos);
168     CuAssertIntEquals(tc, APR_SUCCESS, rv);
169     CuAssert(tc, "File position mismatch, expected 0", fpos == 0);
170
171     txtlen = 50;
172     rv = apr_file_read(testfile, buff, &txtlen);
173     CuAssertIntEquals(tc, APR_SUCCESS, rv);
174     CuAssertStrEquals(tc, TEST2, buff);
175       
176     apr_file_close(testfile);
177
178     rv = apr_file_dup2(errfile, saveerr, p);
179     CuAssertIntEquals(tc, APR_SUCCESS, rv);
180     CuAssertPtrNotNull(tc, errfile);
181 }
182
183 CuSuite *testdup(void)
184 {
185     CuSuite *suite = CuSuiteNew("File duplication");
186
187     SUITE_ADD_TEST(suite, test_file_dup);
188     SUITE_ADD_TEST(suite, test_file_readwrite);
189     SUITE_ADD_TEST(suite, test_dup2);
190     SUITE_ADD_TEST(suite, test_dup2_readwrite);
191
192     return suite;
193 }
194