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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
/* $Id: tstFile.cpp $ */
/** @file
* IPRT Testcase - File I/O.
*/
/*
* Copyright (C) 2006-2020 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <iprt/test.h>
#include <iprt/file.h>
#include <iprt/errcore.h>
#include <iprt/string.h>
#include <iprt/stream.h>
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
static const char g_szTestStr[] = "Sausages and bacon for breakfast again!\n";
static char g_szTestStr2[] =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut "
"enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor "
"in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non "
"proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n"
"\n"
"Curabitur pretium tincidunt lacus. Nulla gravida orci a odio. Nullam varius, turpis et commodo pharetra, est eros bibendum "
"elit, nec luctus magna felis sollicitudin mauris. Integer in mauris eu nibh euismod gravida. Duis ac tellus et risus "
"vulputate vehicula. Donec lobortis risus a elit. Etiam tempor. Ut ullamcorper, ligula eu tempor congue, eros est euismod "
"turpis, id tincidunt sapien risus a quam. Maecenas fermentum consequat mi. Donec fermentum. Pellentesque malesuada nulla a mi. "
"Duis sapien sem, aliquet nec, commodo eget, consequat quis, neque. Aliquam faucibus, elit ut dictum aliquet, felis nisl "
"adipiscing sapien, sed malesuada diam lacus eget erat. Cras mollis scelerisque nunc. Nullam arcu. Aliquam consequat. Curabitur "
"augue lorem, dapibus quis, laoreet et, pretium ac, nisi. Aenean magna nisl, mollis quis, molestie eu, feugiat in, orci. In hac "
"habitasse platea dictumst.\n";
static void tstAppend(RTFILE hFile)
{
char achBuf[sizeof(g_szTestStr2) * 4];
/*
* Write some stuff and read it back.
*/
size_t const cbWrite1 = sizeof(g_szTestStr2) / 4;
RTTESTI_CHECK_RC_RETV(RTFileWrite(hFile, g_szTestStr2, sizeof(g_szTestStr2) - 1, NULL), VINF_SUCCESS);
size_t const offWrite2 = cbWrite1;
size_t const cbWrite2 = sizeof(g_szTestStr2) / 2;
RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
RTTESTI_CHECK_RC_RETV(RTFileWrite(hFile, &g_szTestStr2[offWrite2], cbWrite2, NULL), VINF_SUCCESS);
RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
RTTESTI_CHECK_RC_RETV(RTFileRead(hFile, achBuf, cbWrite1 + cbWrite2, NULL), VINF_SUCCESS);
if (memcmp(achBuf, g_szTestStr2, cbWrite1 + cbWrite2) != 0)
RTTestIFailed("Read back #1 failed (%#zx + %#zx)", cbWrite1, cbWrite2);
#if 1 //ndef RT_OS_WINDOWS
/*
* Truncate the file and write some more. This is problematic on windows,
* we currently have a questionable hack in place to make this work.
*/
RTTESTI_CHECK_RC_RETV(RTFileSetSize(hFile, 0), VINF_SUCCESS);
size_t const offWrite3 = cbWrite1 + cbWrite2;
size_t const cbWrite3 = sizeof(g_szTestStr2) - 1 - offWrite3;
RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
RTTESTI_CHECK_RC_RETV(RTFileWrite(hFile, &g_szTestStr2[offWrite3], cbWrite3, NULL), VINF_SUCCESS);
RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
RTTESTI_CHECK_RC_RETV(RTFileRead(hFile, achBuf, cbWrite3, NULL), VINF_SUCCESS);
if (memcmp(achBuf, &g_szTestStr2[offWrite3], cbWrite3) != 0)
RTTestIFailed("Read back #2 failed (%#zx)", cbWrite3);
#endif
}
static void tstBasics(RTFILE hFile)
{
RTFOFF cbMax = -2;
int rc = RTFileQueryMaxSizeEx(hFile, &cbMax);
if (rc != VERR_NOT_IMPLEMENTED)
{
if (rc != VINF_SUCCESS)
RTTestIFailed("RTFileQueryMaxSizeEx failed: %Rrc", rc);
else
{
RTTESTI_CHECK_MSG(cbMax > 0, ("cbMax=%RTfoff", cbMax));
RTTESTI_CHECK_MSG(cbMax == RTFileGetMaxSize(hFile),
("cbMax=%RTfoff, RTFileGetMaxSize->%RTfoff", cbMax, RTFileGetMaxSize(hFile)));
}
}
/* grow file beyond 2G */
rc = RTFileSetSize(hFile, _2G + _1M);
if (RT_FAILURE(rc))
RTTestIFailed("Failed to grow file #1 to 2.001GB. rc=%Rrc", rc);
else
{
uint64_t cb;
RTTESTI_CHECK_RC(RTFileQuerySize(hFile, &cb), VINF_SUCCESS);
RTTESTI_CHECK_MSG(cb == _2G + _1M, ("RTFileQuerySize return %RX64 bytes, expected %RX64.", cb, _2G + _1M));
/*
* Try some writes at the beginning of the file.
*/
uint64_t offFile = RTFileTell(hFile);
RTTESTI_CHECK_MSG(offFile == 0, ("RTFileTell -> %#RX64, expected 0 (#1)", offFile));
size_t cbWritten = 0;
while (cbWritten < sizeof(g_szTestStr))
{
size_t cbWrittenPart;
rc = RTFileWrite(hFile, &g_szTestStr[cbWritten], sizeof(g_szTestStr) - cbWritten, &cbWrittenPart);
if (RT_FAILURE(rc))
break;
cbWritten += cbWrittenPart;
}
if (RT_FAILURE(rc))
RTTestIFailed("Failed to write to file #1 at offset 0. rc=%Rrc\n", rc);
else
{
/* check that it was written correctly. */
rc = RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL);
if (RT_FAILURE(rc))
RTTestIFailed("Failed to seek offset 0 in file #1. rc=%Rrc\n", rc);
else
{
char szReadBuf[sizeof(g_szTestStr)];
size_t cbRead = 0;
while (cbRead < sizeof(g_szTestStr))
{
size_t cbReadPart;
rc = RTFileRead(hFile, &szReadBuf[cbRead], sizeof(g_szTestStr) - cbRead, &cbReadPart);
if (RT_FAILURE(rc))
break;
cbRead += cbReadPart;
}
if (RT_FAILURE(rc))
RTTestIFailed("Failed to read from file #1 at offset 0. rc=%Rrc\n", rc);
else
{
if (!memcmp(szReadBuf, g_szTestStr, sizeof(g_szTestStr)))
RTPrintf("tstFile: head write ok\n");
else
RTTestIFailed("Data read from file #1 at offset 0 differs from what we wrote there.\n");
}
}
}
/*
* Try some writes at the end of the file.
*/
rc = RTFileSeek(hFile, _2G + _1M, RTFILE_SEEK_BEGIN, NULL);
if (RT_FAILURE(rc))
RTTestIFailed("Failed to seek to _2G + _1M in file #1. rc=%Rrc\n", rc);
else
{
offFile = RTFileTell(hFile);
if (offFile != _2G + _1M)
RTTestIFailed("RTFileTell -> %#llx, expected %#llx (#2)\n", offFile, _2G + _1M);
else
{
cbWritten = 0;
while (cbWritten < sizeof(g_szTestStr))
{
size_t cbWrittenPart;
rc = RTFileWrite(hFile, &g_szTestStr[cbWritten], sizeof(g_szTestStr) - cbWritten, &cbWrittenPart);
if (RT_FAILURE(rc))
break;
cbWritten += cbWrittenPart;
}
if (RT_FAILURE(rc))
RTTestIFailed("Failed to write to file #1 at offset 2G + 1M. rc=%Rrc\n", rc);
else
{
rc = RTFileSeek(hFile, offFile, RTFILE_SEEK_BEGIN, NULL);
if (RT_FAILURE(rc))
RTTestIFailed("Failed to seek offset %RX64 in file #1. rc=%Rrc\n", offFile, rc);
else
{
char szReadBuf[sizeof(g_szTestStr)];
size_t cbRead = 0;
while (cbRead < sizeof(g_szTestStr))
{
size_t cbReadPart;
rc = RTFileRead(hFile, &szReadBuf[cbRead], sizeof(g_szTestStr) - cbRead, &cbReadPart);
if (RT_FAILURE(rc))
break;
cbRead += cbReadPart;
}
if (RT_FAILURE(rc))
RTTestIFailed("Failed to read from file #1 at offset 2G + 1M. rc=%Rrc\n", rc);
else
{
if (!memcmp(szReadBuf, g_szTestStr, sizeof(g_szTestStr)))
RTPrintf("tstFile: tail write ok\n");
else
RTTestIFailed("Data read from file #1 at offset 2G + 1M differs from what we wrote there.\n");
}
}
}
}
}
/*
* Some general seeking around.
*/
rc = RTFileSeek(hFile, _2G + 1, RTFILE_SEEK_BEGIN, NULL);
if (RT_FAILURE(rc))
RTTestIFailed("Failed to seek to _2G + 1 in file #1. rc=%Rrc\n", rc);
else
{
offFile = RTFileTell(hFile);
if (offFile != _2G + 1)
RTTestIFailed("RTFileTell -> %#llx, expected %#llx (#3)\n", offFile, _2G + 1);
}
/* seek end */
rc = RTFileSeek(hFile, 0, RTFILE_SEEK_END, NULL);
if (RT_FAILURE(rc))
RTTestIFailed("Failed to seek to end of file #1. rc=%Rrc\n", rc);
else
{
offFile = RTFileTell(hFile);
if (offFile != _2G + _1M + sizeof(g_szTestStr)) /* assuming tail write was ok. */
RTTestIFailed("RTFileTell -> %#RX64, expected %#RX64 (#4)\n", offFile, _2G + _1M + sizeof(g_szTestStr));
}
/* seek start */
rc = RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL);
if (RT_FAILURE(rc))
RTTestIFailed("Failed to seek to end of file #1. rc=%Rrc\n", rc);
else
{
offFile = RTFileTell(hFile);
if (offFile != 0)
RTTestIFailed("RTFileTell -> %#llx, expected 0 (#5)\n", offFile);
}
}
}
int main()
{
RTTEST hTest;
RTEXITCODE rcExit = RTTestInitAndCreate("tstRTFile", &hTest);
if (rcExit != RTEXITCODE_SUCCESS)
return rcExit;
RTTestBanner(hTest);
/*
* Some basic tests.
*/
RTTestSub(hTest, "Basics");
int rc = -1;
RTFILE hFile = NIL_RTFILE;
RTTESTI_CHECK_RC(rc = RTFileOpen(&hFile, "tstFile#1.tst", RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE),
VINF_SUCCESS);
if (RT_SUCCESS(rc))
{
tstBasics(hFile);
RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
RTTESTI_CHECK_RC(RTFileDelete("tstFile#1.tst"), VINF_SUCCESS);
}
/*
* Test appending & truncation.
*/
RTTestSub(hTest, "Append");
hFile = NIL_RTFILE;
RTTESTI_CHECK_RC(rc = RTFileOpen(&hFile, "tstFile#2.tst",
RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_APPEND),
VINF_SUCCESS);
if (RT_SUCCESS(rc))
{
tstAppend(hFile);
RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
RTTESTI_CHECK_RC(RTFileDelete("tstFile#2.tst"), VINF_SUCCESS);
}
/*
* Done.
*/
return RTTestSummaryAndDestroy(hTest);
}
|