VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstFile.cpp@ 98715

Last change on this file since 98715 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 14.8 KB
Line 
1/* $Id: tstFile.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT Testcase - File I/O.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/test.h>
42#include <iprt/file.h>
43#include <iprt/errcore.h>
44#include <iprt/path.h>
45#include <iprt/rand.h>
46#include <iprt/string.h>
47#include <iprt/stream.h>
48
49
50/*********************************************************************************************************************************
51* Global Variables *
52*********************************************************************************************************************************/
53static const char g_szTestStr[] = "Sausages and bacon for breakfast again!\n";
54static char g_szTestStr2[] =
55"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut "
56"enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor "
57"in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non "
58"proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n"
59"\n"
60"Curabitur pretium tincidunt lacus. Nulla gravida orci a odio. Nullam varius, turpis et commodo pharetra, est eros bibendum "
61"elit, nec luctus magna felis sollicitudin mauris. Integer in mauris eu nibh euismod gravida. Duis ac tellus et risus "
62"vulputate vehicula. Donec lobortis risus a elit. Etiam tempor. Ut ullamcorper, ligula eu tempor congue, eros est euismod "
63"turpis, id tincidunt sapien risus a quam. Maecenas fermentum consequat mi. Donec fermentum. Pellentesque malesuada nulla a mi. "
64"Duis sapien sem, aliquet nec, commodo eget, consequat quis, neque. Aliquam faucibus, elit ut dictum aliquet, felis nisl "
65"adipiscing sapien, sed malesuada diam lacus eget erat. Cras mollis scelerisque nunc. Nullam arcu. Aliquam consequat. Curabitur "
66"augue lorem, dapibus quis, laoreet et, pretium ac, nisi. Aenean magna nisl, mollis quis, molestie eu, feugiat in, orci. In hac "
67"habitasse platea dictumst.\n";
68
69/**
70 * Structure holding queried file system properties we're performing our tests on.
71 */
72typedef struct FsProps
73{
74 RTFOFF cbTotal;
75 RTFOFF cbFree;
76 uint32_t cbBlock;
77 uint32_t cbSector;
78} FsProps;
79/** Queried file system properties we're performing our tests on. */
80static FsProps s_FsProps;
81
82
83static void tstAppend(RTFILE hFile)
84{
85 char achBuf[sizeof(g_szTestStr2) * 4];
86
87 /*
88 * Write some stuff and read it back.
89 */
90 size_t const cbWrite1 = sizeof(g_szTestStr2) / 4;
91 RTTESTI_CHECK_RC_RETV(RTFileWrite(hFile, g_szTestStr2, sizeof(g_szTestStr2) - 1, NULL), VINF_SUCCESS);
92
93 size_t const offWrite2 = cbWrite1;
94 size_t const cbWrite2 = sizeof(g_szTestStr2) / 2;
95 RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
96 RTTESTI_CHECK_RC_RETV(RTFileWrite(hFile, &g_szTestStr2[offWrite2], cbWrite2, NULL), VINF_SUCCESS);
97
98 RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
99 RTTESTI_CHECK_RC_RETV(RTFileRead(hFile, achBuf, cbWrite1 + cbWrite2, NULL), VINF_SUCCESS);
100 if (memcmp(achBuf, g_szTestStr2, cbWrite1 + cbWrite2) != 0)
101 RTTestIFailed("Read back #1 failed (%#zx + %#zx)", cbWrite1, cbWrite2);
102
103#if 1 //ndef RT_OS_WINDOWS
104 /*
105 * Truncate the file and write some more. This is problematic on windows,
106 * we currently have a questionable hack in place to make this work.
107 */
108 RTTESTI_CHECK_RC_RETV(RTFileSetSize(hFile, 0), VINF_SUCCESS);
109
110 size_t const offWrite3 = cbWrite1 + cbWrite2;
111 size_t const cbWrite3 = sizeof(g_szTestStr2) - 1 - offWrite3;
112 RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
113 RTTESTI_CHECK_RC_RETV(RTFileWrite(hFile, &g_szTestStr2[offWrite3], cbWrite3, NULL), VINF_SUCCESS);
114
115 RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
116 RTTESTI_CHECK_RC_RETV(RTFileRead(hFile, achBuf, cbWrite3, NULL), VINF_SUCCESS);
117 if (memcmp(achBuf, &g_szTestStr2[offWrite3], cbWrite3) != 0)
118 RTTestIFailed("Read back #2 failed (%#zx)", cbWrite3);
119#endif
120}
121
122
123static void tstBasics(RTFILE hFile)
124{
125 RTFOFF cbMax = -2;
126 int rc = RTFileQueryMaxSizeEx(hFile, &cbMax);
127 if (rc != VERR_NOT_IMPLEMENTED)
128 {
129 if (rc != VINF_SUCCESS)
130 RTTestIFailed("RTFileQueryMaxSizeEx failed: %Rrc", rc);
131 else
132 {
133 RTTESTI_CHECK_MSG(cbMax > 0, ("cbMax=%RTfoff", cbMax));
134 RTTESTI_CHECK_MSG(cbMax == RTFileGetMaxSize(hFile),
135 ("cbMax=%RTfoff, RTFileGetMaxSize->%RTfoff", cbMax, RTFileGetMaxSize(hFile)));
136 }
137 }
138
139 uint64_t cbFileSize = _2G + RTRandU32Ex(_1K, _1M); /* Try growing file beyond 2G by default. */
140 if ((uint64_t)s_FsProps.cbFree <= cbFileSize)
141 {
142 RTTestIPrintf(RTTESTLVL_ALWAYS, "Warning: Free disk space less than testcase file size (%RTfoff vs. %RU64), limiting\n",
143 s_FsProps.cbFree, cbFileSize);
144 cbFileSize = s_FsProps.cbFree - _1M; /* Leave a bit of space on the fs. */
145 }
146 /** @todo Also check and clamp for fs max file size limits? */
147 RTTESTI_CHECK_MSG_RETV(cbFileSize, ("No space left on file system (disk full)"));
148
149 rc = RTFileSetSize(hFile, cbFileSize);
150 if (RT_FAILURE(rc))
151 RTTestIFailed("Failed to grow file #1 to %RU64. rc=%Rrc", cbFileSize, rc);
152 else
153 {
154 uint64_t cb;
155 RTTESTI_CHECK_RC(RTFileQuerySize(hFile, &cb), VINF_SUCCESS);
156 RTTESTI_CHECK_MSG(cb == cbFileSize, ("RTFileQuerySize return %RX64 bytes, expected %RX64.", cb, cbFileSize));
157
158 /*
159 * Try some writes at the beginning of the file.
160 */
161 uint64_t offFile = RTFileTell(hFile);
162 RTTESTI_CHECK_MSG(offFile == 0, ("RTFileTell -> %#RX64, expected 0 (#1)", offFile));
163
164 size_t cbWritten = 0;
165 while (cbWritten < sizeof(g_szTestStr))
166 {
167 size_t cbWrittenPart;
168 rc = RTFileWrite(hFile, &g_szTestStr[cbWritten], sizeof(g_szTestStr) - cbWritten, &cbWrittenPart);
169 if (RT_FAILURE(rc))
170 break;
171 cbWritten += cbWrittenPart;
172 }
173 if (RT_FAILURE(rc))
174 RTTestIFailed("Failed to write to file #1 at offset 0. rc=%Rrc\n", rc);
175 else
176 {
177 /* check that it was written correctly. */
178 rc = RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL);
179 if (RT_FAILURE(rc))
180 RTTestIFailed("Failed to seek offset 0 in file #1. rc=%Rrc\n", rc);
181 else
182 {
183 char szReadBuf[sizeof(g_szTestStr)];
184 size_t cbRead = 0;
185 while (cbRead < sizeof(g_szTestStr))
186 {
187 size_t cbReadPart;
188 rc = RTFileRead(hFile, &szReadBuf[cbRead], sizeof(g_szTestStr) - cbRead, &cbReadPart);
189 if (RT_FAILURE(rc))
190 break;
191 cbRead += cbReadPart;
192 }
193 if (RT_FAILURE(rc))
194 RTTestIFailed("Failed to read from file #1 at offset 0. rc=%Rrc\n", rc);
195 else
196 {
197 if (!memcmp(szReadBuf, g_szTestStr, sizeof(g_szTestStr)))
198 RTPrintf("tstFile: head write ok\n");
199 else
200 RTTestIFailed("Data read from file #1 at offset 0 differs from what we wrote there.\n");
201 }
202 }
203 }
204
205 /*
206 * Try some writes at the end of the file.
207 */
208 rc = RTFileSeek(hFile, cbFileSize, RTFILE_SEEK_BEGIN, NULL);
209 if (RT_FAILURE(rc))
210 RTTestIFailed("Failed to seek to %RU64 in file #1. rc=%Rrc\n", cbFileSize, rc);
211 else
212 {
213 offFile = RTFileTell(hFile);
214 if (offFile != cbFileSize)
215 RTTestIFailed("RTFileTell -> %#llx, expected %#llx (#2)\n", offFile, cbFileSize);
216 else
217 {
218 cbWritten = 0;
219 while (cbWritten < sizeof(g_szTestStr))
220 {
221 size_t cbWrittenPart;
222 rc = RTFileWrite(hFile, &g_szTestStr[cbWritten], sizeof(g_szTestStr) - cbWritten, &cbWrittenPart);
223 if (RT_FAILURE(rc))
224 break;
225 cbWritten += cbWrittenPart;
226 }
227 if (RT_FAILURE(rc))
228 RTTestIFailed("Failed to write to file #1 at offset %RU64. rc=%Rrc\n", cbFileSize, rc);
229 else
230 {
231 rc = RTFileSeek(hFile, offFile, RTFILE_SEEK_BEGIN, NULL);
232 if (RT_FAILURE(rc))
233 RTTestIFailed("Failed to seek offset %RX64 in file #1. rc=%Rrc\n", offFile, rc);
234 else
235 {
236 char szReadBuf[sizeof(g_szTestStr)];
237 size_t cbRead = 0;
238 while (cbRead < sizeof(g_szTestStr))
239 {
240 size_t cbReadPart;
241 rc = RTFileRead(hFile, &szReadBuf[cbRead], sizeof(g_szTestStr) - cbRead, &cbReadPart);
242 if (RT_FAILURE(rc))
243 break;
244 cbRead += cbReadPart;
245 }
246 if (RT_FAILURE(rc))
247 RTTestIFailed("Failed to read from file #1 at offset %RU64. rc=%Rrc\n", cbFileSize, rc);
248 else
249 {
250 if (!memcmp(szReadBuf, g_szTestStr, sizeof(g_szTestStr)))
251 RTPrintf("tstFile: tail write ok\n");
252 else
253 RTTestIFailed("Data read from file #1 at offset %RU64 differs from what we wrote there.\n",
254 cbFileSize);
255 }
256 }
257 }
258 }
259 }
260
261 /*
262 * Some general seeking around.
263 */
264 RTFOFF offSeek = RTRandS64Ex(0, cbFileSize);
265 rc = RTFileSeek(hFile, offSeek, RTFILE_SEEK_BEGIN, NULL);
266 if (RT_FAILURE(rc))
267 RTTestIFailed("Failed to seek to %RTfoff in file #1. rc=%Rrc\n", offSeek, rc);
268 else
269 {
270 offFile = RTFileTell(hFile);
271 if (offFile != (uint64_t)offSeek)
272 RTTestIFailed("RTFileTell -> %#RTfoff, expected %RTfoff (#3)\n", offFile, offSeek);
273 }
274
275 /* seek end */
276 rc = RTFileSeek(hFile, 0, RTFILE_SEEK_END, NULL);
277 if (RT_FAILURE(rc))
278 RTTestIFailed("Failed to seek to end of file #1. rc=%Rrc\n", rc);
279 else
280 {
281 offFile = RTFileTell(hFile);
282 if (offFile != cbFileSize + sizeof(g_szTestStr)) /* assuming tail write was ok. */
283 RTTestIFailed("RTFileTell -> %RTfoff, expected %#RX64 (#4)\n", offFile, cbFileSize + sizeof(g_szTestStr));
284 }
285
286 /* seek start */
287 rc = RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL);
288 if (RT_FAILURE(rc))
289 RTTestIFailed("Failed to seek to end of file #1. rc=%Rrc\n", rc);
290 else
291 {
292 offFile = RTFileTell(hFile);
293 if (offFile != 0)
294 RTTestIFailed("RTFileTell -> %RTfoff, expected 0 (#5)\n", offFile);
295 }
296 }
297}
298
299int main()
300{
301 RTTEST hTest;
302 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTFile", &hTest);
303 if (rcExit != RTEXITCODE_SUCCESS)
304 return rcExit;
305 RTTestBanner(hTest);
306
307 /*
308 * Query file system sizes first.
309 * This is needed beforehand, so that we don't perform tests which cannot succeed because of known limitations
310 * (too little space, file system maximum file size restrictions, ++).
311 */
312 char szCWD[RTPATH_MAX];
313 int rc = RTPathGetCurrent(szCWD, sizeof(szCWD));
314 RTTESTI_CHECK_MSG(RT_SUCCESS(rc), ("Unable to query current directory, rc=%Rrc", rc));
315 rc = RTFsQuerySizes(szCWD, &s_FsProps.cbTotal, &s_FsProps.cbFree, &s_FsProps.cbBlock, &s_FsProps.cbSector);
316 RTTESTI_CHECK_MSG(RT_SUCCESS(rc), ("Unable to query file system sizes of '%s', rc=%Rrc", szCWD, rc));
317
318 /*
319 * Some basic tests.
320 */
321 RTTestSub(hTest, "Basics");
322 RTFILE hFile = NIL_RTFILE;
323 RTTESTI_CHECK_RC(rc = RTFileOpen(&hFile, "tstFile#1.tst", RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE),
324 VINF_SUCCESS);
325 if (RT_SUCCESS(rc))
326 {
327 tstBasics(hFile);
328 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
329 RTTESTI_CHECK_RC(RTFileDelete("tstFile#1.tst"), VINF_SUCCESS);
330 }
331
332 /*
333 * Test appending & truncation.
334 */
335 RTTestSub(hTest, "Append");
336 hFile = NIL_RTFILE;
337 RTTESTI_CHECK_RC(rc = RTFileOpen(&hFile, "tstFile#2.tst",
338 RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_APPEND),
339 VINF_SUCCESS);
340 if (RT_SUCCESS(rc))
341 {
342 tstAppend(hFile);
343 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
344 RTTESTI_CHECK_RC(RTFileDelete("tstFile#2.tst"), VINF_SUCCESS);
345 }
346
347 /*
348 * Done.
349 */
350 return RTTestSummaryAndDestroy(hTest);
351}
352
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette