1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "apr_file_io.h"
#include "apr_file_info.h"
#include "apr_strings.h"
#include "apr_errno.h"
#include "apr_general.h"
#include "apr_poll.h"
#include "apr_lib.h"
#include "test_apr.h"
#define FILENAME "data/file_datafile.txt"
#define NEWFILENAME "data/new_datafile.txt"
#define NEWFILEDATA "This is new text in a new file."
static const struct view_fileinfo
{
apr_int32_t bits;
char *description;
} vfi[] = {
{APR_FINFO_MTIME, "MTIME"},
{APR_FINFO_CTIME, "CTIME"},
{APR_FINFO_ATIME, "ATIME"},
{APR_FINFO_SIZE, "SIZE"},
{APR_FINFO_DEV, "DEV"},
{APR_FINFO_INODE, "INODE"},
{APR_FINFO_NLINK, "NLINK"},
{APR_FINFO_TYPE, "TYPE"},
{APR_FINFO_USER, "USER"},
{APR_FINFO_GROUP, "GROUP"},
{APR_FINFO_UPROT, "UPROT"},
{APR_FINFO_GPROT, "GPROT"},
{APR_FINFO_WPROT, "WPROT"},
{0, NULL}
};
static void finfo_equal(CuTest *tc, apr_finfo_t f1, apr_finfo_t f2)
{
/* Minimum supported flags across all platforms (APR_FINFO_MIN) */
CuAssert(tc, "apr_stat and apr_getfileinfo must return APR_FINFO_TYPE",
(f1.valid & f2.valid & APR_FINFO_TYPE));
CuAssert(tc, "apr_stat and apr_getfileinfo differ in filetype",
f1.filetype == f2.filetype);
CuAssert(tc, "apr_stat and apr_getfileinfo must return APR_FINFO_SIZE",
(f1.valid & f2.valid & APR_FINFO_SIZE));
CuAssert(tc, "apr_stat and apr_getfileinfo differ in size",
f1.size == f2.size);
CuAssert(tc, "apr_stat and apr_getfileinfo must return APR_FINFO_ATIME",
(f1.valid & f2.valid & APR_FINFO_ATIME));
CuAssert(tc, "apr_stat and apr_getfileinfo differ in atime",
f1.atime == f2.atime);
CuAssert(tc, "apr_stat and apr_getfileinfo must return APR_FINFO_MTIME",
(f1.valid & f2.valid & APR_FINFO_MTIME));
CuAssert(tc, "apr_stat and apr_getfileinfo differ in mtime",
f1.mtime == f2.mtime);
CuAssert(tc, "apr_stat and apr_getfileinfo must return APR_FINFO_CTIME",
(f1.valid & f2.valid & APR_FINFO_CTIME));
CuAssert(tc, "apr_stat and apr_getfileinfo differ in ctime",
f1.ctime == f2.ctime);
if (f1.valid & f2.valid & APR_FINFO_NAME)
CuAssert(tc, "apr_stat and apr_getfileinfo differ in name",
!strcmp(f1.name, f2.name));
if (f1.fname && f2.fname)
CuAssert(tc, "apr_stat and apr_getfileinfo differ in fname",
!strcmp(f1.fname, f2.fname));
/* Additional supported flags not supported on all platforms */
if (f1.valid & f2.valid & APR_FINFO_USER)
CuAssert(tc, "apr_stat and apr_getfileinfo differ in user",
!apr_uid_compare(f1.user, f2.user));
if (f1.valid & f2.valid & APR_FINFO_GROUP)
CuAssert(tc, "apr_stat and apr_getfileinfo differ in group",
!apr_gid_compare(f1.group, f2.group));
if (f1.valid & f2.valid & APR_FINFO_INODE)
CuAssert(tc, "apr_stat and apr_getfileinfo differ in inode",
f1.inode == f2.inode);
if (f1.valid & f2.valid & APR_FINFO_DEV)
CuAssert(tc, "apr_stat and apr_getfileinfo differ in device",
f1.device == f2.device);
if (f1.valid & f2.valid & APR_FINFO_NLINK)
CuAssert(tc, "apr_stat and apr_getfileinfo differ in nlink",
f1.nlink == f2.nlink);
if (f1.valid & f2.valid & APR_FINFO_CSIZE)
CuAssert(tc, "apr_stat and apr_getfileinfo differ in csize",
f1.csize == f2.csize);
if (f1.valid & f2.valid & APR_FINFO_PROT)
CuAssert(tc, "apr_stat and apr_getfileinfo differ in protection",
f1.protection == f2.protection);
}
static void test_info_get(CuTest *tc)
{
apr_file_t *thefile;
apr_finfo_t finfo;
apr_status_t rv;
rv = apr_file_open(&thefile, FILENAME, APR_READ, APR_OS_DEFAULT, p);
CuAssertIntEquals(tc, APR_SUCCESS, rv);
rv = apr_file_info_get(&finfo, APR_FINFO_NORM, thefile);
if (rv == APR_INCOMPLETE) {
char *str;
int i;
str = apr_pstrdup(p, "APR_INCOMPLETE: Missing ");
for (i = 0; vfi[i].bits; ++i) {
if (vfi[i].bits & ~finfo.valid) {
str = apr_pstrcat(p, str, vfi[i].description, " ", NULL);
}
}
CuFail(tc, str);
}
CuAssertIntEquals(tc, APR_SUCCESS, rv);
apr_file_close(thefile);
}
static void test_stat(CuTest *tc)
{
apr_finfo_t finfo;
apr_status_t rv;
rv = apr_stat(&finfo, FILENAME, APR_FINFO_NORM, p);
if (rv == APR_INCOMPLETE) {
char *str;
int i;
str = apr_pstrdup(p, "APR_INCOMPLETE: Missing ");
for (i = 0; vfi[i].bits; ++i) {
if (vfi[i].bits & ~finfo.valid) {
str = apr_pstrcat(p, str, vfi[i].description, " ", NULL);
}
}
CuFail(tc, str);
}
CuAssertIntEquals(tc, APR_SUCCESS, rv);
}
static void test_stat_eq_finfo(CuTest *tc)
{
apr_file_t *thefile;
apr_finfo_t finfo;
apr_finfo_t stat_finfo;
apr_status_t rv;
rv = apr_file_open(&thefile, FILENAME, APR_READ, APR_OS_DEFAULT, p);
CuAssertIntEquals(tc, APR_SUCCESS, rv);
rv = apr_file_info_get(&finfo, APR_FINFO_NORM, thefile);
/* Opening the file may have toggled the atime member (time last
* accessed), so fetch our apr_stat() after getting the fileinfo
* of the open file...
*/
rv = apr_stat(&stat_finfo, FILENAME, APR_FINFO_NORM, p);
CuAssertIntEquals(tc, APR_SUCCESS, rv);
apr_file_close(thefile);
finfo_equal(tc, stat_finfo, finfo);
}
static void test_buffered_write_size(CuTest *tc)
{
const apr_size_t data_len = strlen(NEWFILEDATA);
apr_file_t *thefile;
apr_finfo_t finfo;
apr_status_t rv;
apr_size_t bytes;
rv = apr_file_open(&thefile, NEWFILENAME,
APR_READ | APR_WRITE | APR_CREATE | APR_TRUNCATE
| APR_BUFFERED | APR_DELONCLOSE,
APR_OS_DEFAULT, p);
apr_assert_success(tc, "open file", rv);
/* A funny thing happened to me the other day: I wrote something
* into a buffered file, then asked for its size using
* apr_file_info_get; and guess what? The size was 0! That's not a
* nice way to behave.
*/
bytes = data_len;
rv = apr_file_write(thefile, NEWFILEDATA, &bytes);
apr_assert_success(tc, "write file contents", rv);
CuAssertTrue(tc, data_len == bytes);
rv = apr_file_info_get(&finfo, APR_FINFO_SIZE, thefile);
apr_assert_success(tc, "get file size", rv);
CuAssertTrue(tc, bytes == (apr_size_t) finfo.size);
apr_file_close(thefile);
}
static void test_mtime_set(CuTest *tc)
{
apr_file_t *thefile;
apr_finfo_t finfo;
apr_time_t epoch = 0;
apr_status_t rv;
/* This test sort of depends on the system clock being at least
* marginally ccorrect; We'll be setting the modification time to
* the epoch.
*/
rv = apr_file_open(&thefile, NEWFILENAME,
APR_READ | APR_WRITE | APR_CREATE | APR_TRUNCATE
| APR_BUFFERED | APR_DELONCLOSE,
APR_OS_DEFAULT, p);
apr_assert_success(tc, "open file", rv);
/* Check that the current mtime is not the epoch */
rv = apr_stat(&finfo, NEWFILENAME, APR_FINFO_MTIME, p);
if (rv == APR_INCOMPLETE) {
char *str;
int i;
str = apr_pstrdup(p, "APR_INCOMPLETE: Missing ");
for (i = 0; vfi[i].bits; ++i) {
if (vfi[i].bits & ~finfo.valid) {
str = apr_pstrcat(p, str, vfi[i].description, " ", NULL);
}
}
CuFail(tc, str);
}
apr_assert_success(tc, "get initial mtime", rv);
CuAssertTrue(tc, finfo.mtime != epoch);
/* Reset the mtime to the epoch and verify the result.
* Note: we blindly assume that if the first apr_stat succeeded,
* the second one will, too.
*/
rv = apr_file_mtime_set(NEWFILENAME, epoch, p);
apr_assert_success(tc, "set mtime", rv);
rv = apr_stat(&finfo, NEWFILENAME, APR_FINFO_MTIME, p);
apr_assert_success(tc, "get modified mtime", rv);
CuAssertTrue(tc, finfo.mtime == epoch);
apr_file_close(thefile);
}
CuSuite *testfileinfo(void)
{
CuSuite *suite = CuSuiteNew("File Info");
SUITE_ADD_TEST(suite, test_info_get);
SUITE_ADD_TEST(suite, test_stat);
SUITE_ADD_TEST(suite, test_stat_eq_finfo);
SUITE_ADD_TEST(suite, test_buffered_write_size);
SUITE_ADD_TEST(suite, test_mtime_set);
return suite;
}
|