VirtualBox

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

Last change on this file since 82836 was 82836, checked in by vboxsync, 5 years ago

IPRT/win: Enabled new RTFileSetSize implementation that works like more like posix wrt RTFILE_O_APPEND (a bit risky). ticketref:19003

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 12.7 KB
Line 
1/* $Id: tstFile.cpp 82836 2020-01-22 21:55:17Z vboxsync $ */
2/** @file
3 * IPRT Testcase - File I/O.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/test.h>
32#include <iprt/file.h>
33#include <iprt/errcore.h>
34#include <iprt/string.h>
35#include <iprt/stream.h>
36
37
38/*********************************************************************************************************************************
39* Global Variables *
40*********************************************************************************************************************************/
41static const char g_szTestStr[] = "Sausages and bacon for breakfast again!\n";
42static char g_szTestStr2[] =
43"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut "
44"enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor "
45"in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non "
46"proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n"
47"\n"
48"Curabitur pretium tincidunt lacus. Nulla gravida orci a odio. Nullam varius, turpis et commodo pharetra, est eros bibendum "
49"elit, nec luctus magna felis sollicitudin mauris. Integer in mauris eu nibh euismod gravida. Duis ac tellus et risus "
50"vulputate vehicula. Donec lobortis risus a elit. Etiam tempor. Ut ullamcorper, ligula eu tempor congue, eros est euismod "
51"turpis, id tincidunt sapien risus a quam. Maecenas fermentum consequat mi. Donec fermentum. Pellentesque malesuada nulla a mi. "
52"Duis sapien sem, aliquet nec, commodo eget, consequat quis, neque. Aliquam faucibus, elit ut dictum aliquet, felis nisl "
53"adipiscing sapien, sed malesuada diam lacus eget erat. Cras mollis scelerisque nunc. Nullam arcu. Aliquam consequat. Curabitur "
54"augue lorem, dapibus quis, laoreet et, pretium ac, nisi. Aenean magna nisl, mollis quis, molestie eu, feugiat in, orci. In hac "
55"habitasse platea dictumst.\n";
56
57
58static void tstAppend(RTFILE hFile)
59{
60 char achBuf[sizeof(g_szTestStr2) * 4];
61
62 /*
63 * Write some stuff and read it back.
64 */
65 size_t const cbWrite1 = sizeof(g_szTestStr2) / 4;
66 RTTESTI_CHECK_RC_RETV(RTFileWrite(hFile, g_szTestStr2, sizeof(g_szTestStr2) - 1, NULL), VINF_SUCCESS);
67
68 size_t const offWrite2 = cbWrite1;
69 size_t const cbWrite2 = sizeof(g_szTestStr2) / 2;
70 RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
71 RTTESTI_CHECK_RC_RETV(RTFileWrite(hFile, &g_szTestStr2[offWrite2], cbWrite2, NULL), VINF_SUCCESS);
72
73 RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
74 RTTESTI_CHECK_RC_RETV(RTFileRead(hFile, achBuf, cbWrite1 + cbWrite2, NULL), VINF_SUCCESS);
75 if (memcmp(achBuf, g_szTestStr2, cbWrite1 + cbWrite2) != 0)
76 RTTestIFailed("Read back #1 failed (%#zx + %#zx)", cbWrite1, cbWrite2);
77
78#if 1 //ndef RT_OS_WINDOWS
79 /*
80 * Truncate the file and write some more. This is problematic on windows.
81 */
82 RTTESTI_CHECK_RC_RETV(RTFileSetSize(hFile, 0), VINF_SUCCESS);
83
84 size_t const offWrite3 = cbWrite1 + cbWrite2;
85 size_t const cbWrite3 = sizeof(g_szTestStr2) - 1 - offWrite3;
86 RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
87 RTTESTI_CHECK_RC_RETV(RTFileWrite(hFile, &g_szTestStr2[offWrite3], cbWrite3, NULL), VINF_SUCCESS);
88
89 RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
90 RTTESTI_CHECK_RC_RETV(RTFileRead(hFile, achBuf, cbWrite3, NULL), VINF_SUCCESS);
91 if (memcmp(achBuf, &g_szTestStr2[offWrite3], cbWrite3) != 0)
92 RTTestIFailed("Read back #2 failed (%#zx)", cbWrite3);
93#endif
94}
95
96
97static void tstBasics(RTFILE hFile)
98{
99 RTFOFF cbMax = -2;
100 int rc = RTFileQueryMaxSizeEx(hFile, &cbMax);
101 if (rc != VERR_NOT_IMPLEMENTED)
102 {
103 if (rc != VINF_SUCCESS)
104 RTTestIFailed("RTFileQueryMaxSizeEx failed: %Rrc", rc);
105 else
106 {
107 RTTESTI_CHECK_MSG(cbMax > 0, ("cbMax=%RTfoff", cbMax));
108 RTTESTI_CHECK_MSG(cbMax == RTFileGetMaxSize(hFile),
109 ("cbMax=%RTfoff, RTFileGetMaxSize->%RTfoff", cbMax, RTFileGetMaxSize(hFile)));
110 }
111 }
112
113 /* grow file beyond 2G */
114 rc = RTFileSetSize(hFile, _2G + _1M);
115 if (RT_FAILURE(rc))
116 RTTestIFailed("Failed to grow file #1 to 2.001GB. rc=%Rrc", rc);
117 else
118 {
119 uint64_t cb;
120 RTTESTI_CHECK_RC(RTFileQuerySize(hFile, &cb), VINF_SUCCESS);
121 RTTESTI_CHECK_MSG(cb == _2G + _1M, ("RTFileQuerySize return %RX64 bytes, expected %RX64.", cb, _2G + _1M));
122
123 /*
124 * Try some writes at the beginning of the file.
125 */
126 uint64_t offFile = RTFileTell(hFile);
127 RTTESTI_CHECK_MSG(offFile == 0, ("RTFileTell -> %#RX64, expected 0 (#1)", offFile));
128
129 size_t cbWritten = 0;
130 while (cbWritten < sizeof(g_szTestStr))
131 {
132 size_t cbWrittenPart;
133 rc = RTFileWrite(hFile, &g_szTestStr[cbWritten], sizeof(g_szTestStr) - cbWritten, &cbWrittenPart);
134 if (RT_FAILURE(rc))
135 break;
136 cbWritten += cbWrittenPart;
137 }
138 if (RT_FAILURE(rc))
139 RTTestIFailed("Failed to write to file #1 at offset 0. rc=%Rrc\n", rc);
140 else
141 {
142 /* check that it was written correctly. */
143 rc = RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL);
144 if (RT_FAILURE(rc))
145 RTTestIFailed("Failed to seek offset 0 in file #1. rc=%Rrc\n", rc);
146 else
147 {
148 char szReadBuf[sizeof(g_szTestStr)];
149 size_t cbRead = 0;
150 while (cbRead < sizeof(g_szTestStr))
151 {
152 size_t cbReadPart;
153 rc = RTFileRead(hFile, &szReadBuf[cbRead], sizeof(g_szTestStr) - cbRead, &cbReadPart);
154 if (RT_FAILURE(rc))
155 break;
156 cbRead += cbReadPart;
157 }
158 if (RT_FAILURE(rc))
159 RTTestIFailed("Failed to read from file #1 at offset 0. rc=%Rrc\n", rc);
160 else
161 {
162 if (!memcmp(szReadBuf, g_szTestStr, sizeof(g_szTestStr)))
163 RTPrintf("tstFile: head write ok\n");
164 else
165 RTTestIFailed("Data read from file #1 at offset 0 differs from what we wrote there.\n");
166 }
167 }
168 }
169
170 /*
171 * Try some writes at the end of the file.
172 */
173 rc = RTFileSeek(hFile, _2G + _1M, RTFILE_SEEK_BEGIN, NULL);
174 if (RT_FAILURE(rc))
175 RTTestIFailed("Failed to seek to _2G + _1M in file #1. rc=%Rrc\n", rc);
176 else
177 {
178 offFile = RTFileTell(hFile);
179 if (offFile != _2G + _1M)
180 RTTestIFailed("RTFileTell -> %#llx, expected %#llx (#2)\n", offFile, _2G + _1M);
181 else
182 {
183 cbWritten = 0;
184 while (cbWritten < sizeof(g_szTestStr))
185 {
186 size_t cbWrittenPart;
187 rc = RTFileWrite(hFile, &g_szTestStr[cbWritten], sizeof(g_szTestStr) - cbWritten, &cbWrittenPart);
188 if (RT_FAILURE(rc))
189 break;
190 cbWritten += cbWrittenPart;
191 }
192 if (RT_FAILURE(rc))
193 RTTestIFailed("Failed to write to file #1 at offset 2G + 1M. rc=%Rrc\n", rc);
194 else
195 {
196 rc = RTFileSeek(hFile, offFile, RTFILE_SEEK_BEGIN, NULL);
197 if (RT_FAILURE(rc))
198 RTTestIFailed("Failed to seek offset %RX64 in file #1. rc=%Rrc\n", offFile, rc);
199 else
200 {
201 char szReadBuf[sizeof(g_szTestStr)];
202 size_t cbRead = 0;
203 while (cbRead < sizeof(g_szTestStr))
204 {
205 size_t cbReadPart;
206 rc = RTFileRead(hFile, &szReadBuf[cbRead], sizeof(g_szTestStr) - cbRead, &cbReadPart);
207 if (RT_FAILURE(rc))
208 break;
209 cbRead += cbReadPart;
210 }
211 if (RT_FAILURE(rc))
212 RTTestIFailed("Failed to read from file #1 at offset 2G + 1M. rc=%Rrc\n", rc);
213 else
214 {
215 if (!memcmp(szReadBuf, g_szTestStr, sizeof(g_szTestStr)))
216 RTPrintf("tstFile: tail write ok\n");
217 else
218 RTTestIFailed("Data read from file #1 at offset 2G + 1M differs from what we wrote there.\n");
219 }
220 }
221 }
222 }
223 }
224
225 /*
226 * Some general seeking around.
227 */
228 rc = RTFileSeek(hFile, _2G + 1, RTFILE_SEEK_BEGIN, NULL);
229 if (RT_FAILURE(rc))
230 RTTestIFailed("Failed to seek to _2G + 1 in file #1. rc=%Rrc\n", rc);
231 else
232 {
233 offFile = RTFileTell(hFile);
234 if (offFile != _2G + 1)
235 RTTestIFailed("RTFileTell -> %#llx, expected %#llx (#3)\n", offFile, _2G + 1);
236 }
237
238 /* seek end */
239 rc = RTFileSeek(hFile, 0, RTFILE_SEEK_END, NULL);
240 if (RT_FAILURE(rc))
241 RTTestIFailed("Failed to seek to end of file #1. rc=%Rrc\n", rc);
242 else
243 {
244 offFile = RTFileTell(hFile);
245 if (offFile != _2G + _1M + sizeof(g_szTestStr)) /* assuming tail write was ok. */
246 RTTestIFailed("RTFileTell -> %#RX64, expected %#RX64 (#4)\n", offFile, _2G + _1M + sizeof(g_szTestStr));
247 }
248
249 /* seek start */
250 rc = RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL);
251 if (RT_FAILURE(rc))
252 RTTestIFailed("Failed to seek to end of file #1. rc=%Rrc\n", rc);
253 else
254 {
255 offFile = RTFileTell(hFile);
256 if (offFile != 0)
257 RTTestIFailed("RTFileTell -> %#llx, expected 0 (#5)\n", offFile);
258 }
259 }
260}
261
262int main()
263{
264 RTTEST hTest;
265 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTFile", &hTest);
266 if (rcExit != RTEXITCODE_SUCCESS)
267 return rcExit;
268 RTTestBanner(hTest);
269
270 /*
271 * Some basic tests.
272 */
273 RTTestSub(hTest, "Basics");
274 int rc = -1;
275 RTFILE hFile = NIL_RTFILE;
276 RTTESTI_CHECK_RC(rc = RTFileOpen(&hFile, "tstFile#1.tst", RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE),
277 VINF_SUCCESS);
278 if (RT_SUCCESS(rc))
279 {
280 tstBasics(hFile);
281 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
282 RTTESTI_CHECK_RC(RTFileDelete("tstFile#1.tst"), VINF_SUCCESS);
283 }
284
285 /*
286 * Test appending & truncation.
287 */
288 RTTestSub(hTest, "Append");
289 hFile = NIL_RTFILE;
290 RTTESTI_CHECK_RC(rc = RTFileOpen(&hFile, "tstFile#2.tst",
291 RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_APPEND),
292 VINF_SUCCESS);
293 if (RT_SUCCESS(rc))
294 {
295 tstAppend(hFile);
296 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
297 RTTESTI_CHECK_RC(RTFileDelete("tstFile#2.tst"), VINF_SUCCESS);
298 }
299
300 /*
301 * Done.
302 */
303 return RTTestSummaryAndDestroy(hTest);
304}
305
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