VirtualBox

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

Last change on this file since 79325 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 10.1 KB
Line 
1/* $Id: tstFile.cpp 76553 2019-01-01 01:45:53Z 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/file.h>
32#include <iprt/errcore.h>
33#include <iprt/string.h>
34#include <iprt/stream.h>
35#include <iprt/initterm.h>
36
37
38int main()
39{
40 int cErrors = 0;
41 RTPrintf("tstFile: TESTING\n");
42 RTR3InitExeNoArguments(0);
43
44 RTFILE File;
45 int rc = RTFileOpen(&File, "tstFile#1.tst", RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE);
46 if (RT_FAILURE(rc))
47 {
48 RTPrintf("tstFile: FATAL ERROR - Failed to open file #1. rc=%Rrc\n", rc);
49 return 1;
50 }
51
52 RTFOFF cbMax = -2;
53 rc = RTFileGetMaxSizeEx(File, &cbMax);
54 if (RT_FAILURE(rc))
55 {
56 RTPrintf("tstFile: RTFileGetMaxSizeEx failed: %Rrc\n", rc);
57 cErrors++;
58 }
59 else if (cbMax <= 0)
60 {
61 RTPrintf("tstFile: RTFileGetMaxSizeEx failed: cbMax=%RTfoff\n", cbMax);
62 cErrors++;
63 }
64 else if (RTFileGetMaxSize(File) != cbMax)
65 {
66 RTPrintf("tstFile: RTFileGetMaxSize failed; returns %RTfoff instead of %RTfoff\n", RTFileGetMaxSize(File), cbMax);
67 cErrors++;
68 }
69 else
70 RTPrintf("Maximum file size is %RTfoff bytes.\n", cbMax);
71
72 /* grow file beyond 2G */
73 rc = RTFileSetSize(File, _2G + _1M);
74 if (RT_FAILURE(rc))
75 {
76 RTPrintf("Failed to grow file #1 to 2.001GB. rc=%Rrc\n", rc);
77 cErrors++;
78 }
79 else
80 {
81 uint64_t cb;
82 rc = RTFileGetSize(File, &cb);
83 if (RT_FAILURE(rc))
84 {
85 RTPrintf("Failed to get file size of #1. rc=%Rrc\n", rc);
86 cErrors++;
87 }
88 else if (cb != _2G + _1M)
89 {
90 RTPrintf("RTFileGetSize return %RX64 bytes, expected %RX64.\n", cb, _2G + _1M);
91 cErrors++;
92 }
93 else
94 RTPrintf("tstFile: cb=%RX64\n", cb);
95
96 /*
97 * Try some writes at the beginning of the file.
98 */
99 uint64_t offFile = RTFileTell(File);
100 if (offFile != 0)
101 {
102 RTPrintf("RTFileTell -> %#RX64, expected 0 (#1)\n", offFile);
103 cErrors++;
104 }
105 static const char szTestBuf[] = "Sausages and bacon for breakfast again!";
106 size_t cbWritten = 0;
107 while (cbWritten < sizeof(szTestBuf))
108 {
109 size_t cbWrittenPart;
110 rc = RTFileWrite(File, &szTestBuf[cbWritten], sizeof(szTestBuf) - cbWritten, &cbWrittenPart);
111 if (RT_FAILURE(rc))
112 break;
113 cbWritten += cbWrittenPart;
114 }
115 if (RT_FAILURE(rc))
116 {
117 RTPrintf("Failed to write to file #1 at offset 0. rc=%Rrc\n", rc);
118 cErrors++;
119 }
120 else
121 {
122 /* check that it was written correctly. */
123 rc = RTFileSeek(File, 0, RTFILE_SEEK_BEGIN, NULL);
124 if (RT_FAILURE(rc))
125 {
126 RTPrintf("Failed to seek offset 0 in file #1. rc=%Rrc\n", rc);
127 cErrors++;
128 }
129 else
130 {
131 char szReadBuf[sizeof(szTestBuf)];
132 size_t cbRead = 0;
133 while (cbRead < sizeof(szTestBuf))
134 {
135 size_t cbReadPart;
136 rc = RTFileRead(File, &szReadBuf[cbRead], sizeof(szTestBuf) - cbRead, &cbReadPart);
137 if (RT_FAILURE(rc))
138 break;
139 cbRead += cbReadPart;
140 }
141 if (RT_FAILURE(rc))
142 {
143 RTPrintf("Failed to read from file #1 at offset 0. rc=%Rrc\n", rc);
144 cErrors++;
145 }
146 else
147 {
148 if (!memcmp(szReadBuf, szTestBuf, sizeof(szTestBuf)))
149 RTPrintf("tstFile: head write ok\n");
150 else
151 {
152 RTPrintf("Data read from file #1 at offset 0 differs from what we wrote there.\n");
153 cErrors++;
154 }
155 }
156 }
157 }
158
159 /*
160 * Try some writes at the end of the file.
161 */
162 rc = RTFileSeek(File, _2G + _1M, RTFILE_SEEK_BEGIN, NULL);
163 if (RT_FAILURE(rc))
164 {
165 RTPrintf("Failed to seek to _2G + _1M in file #1. rc=%Rrc\n", rc);
166 cErrors++;
167 }
168 else
169 {
170 offFile = RTFileTell(File);
171 if (offFile != _2G + _1M)
172 {
173 RTPrintf("RTFileTell -> %#llx, expected %#llx (#2)\n", offFile, _2G + _1M);
174 cErrors++;
175 }
176 else
177 {
178 cbWritten = 0;
179 while (cbWritten < sizeof(szTestBuf))
180 {
181 size_t cbWrittenPart;
182 rc = RTFileWrite(File, &szTestBuf[cbWritten], sizeof(szTestBuf) - cbWritten, &cbWrittenPart);
183 if (RT_FAILURE(rc))
184 break;
185 cbWritten += cbWrittenPart;
186 }
187 if (RT_FAILURE(rc))
188 {
189 RTPrintf("Failed to write to file #1 at offset 2G + 1M. rc=%Rrc\n", rc);
190 cErrors++;
191 }
192 else
193 {
194 rc = RTFileSeek(File, offFile, RTFILE_SEEK_BEGIN, NULL);
195 if (RT_FAILURE(rc))
196 {
197 RTPrintf("Failed to seek offset %RX64 in file #1. rc=%Rrc\n", offFile, rc);
198 cErrors++;
199 }
200 else
201 {
202 char szReadBuf[sizeof(szTestBuf)];
203 size_t cbRead = 0;
204 while (cbRead < sizeof(szTestBuf))
205 {
206 size_t cbReadPart;
207 rc = RTFileRead(File, &szReadBuf[cbRead], sizeof(szTestBuf) - cbRead, &cbReadPart);
208 if (RT_FAILURE(rc))
209 break;
210 cbRead += cbReadPart;
211 }
212 if (RT_FAILURE(rc))
213 {
214 RTPrintf("Failed to read from file #1 at offset 2G + 1M. rc=%Rrc\n", rc);
215 cErrors++;
216 }
217 else
218 {
219 if (!memcmp(szReadBuf, szTestBuf, sizeof(szTestBuf)))
220 RTPrintf("tstFile: tail write ok\n");
221 else
222 {
223 RTPrintf("Data read from file #1 at offset 2G + 1M differs from what we wrote there.\n");
224 cErrors++;
225 }
226 }
227 }
228 }
229 }
230 }
231
232 /*
233 * Some general seeking around.
234 */
235 rc = RTFileSeek(File, _2G + 1, RTFILE_SEEK_BEGIN, NULL);
236 if (RT_FAILURE(rc))
237 {
238 RTPrintf("Failed to seek to _2G + 1 in file #1. rc=%Rrc\n", rc);
239 cErrors++;
240 }
241 else
242 {
243 offFile = RTFileTell(File);
244 if (offFile != _2G + 1)
245 {
246 RTPrintf("RTFileTell -> %#llx, expected %#llx (#3)\n", offFile, _2G + 1);
247 cErrors++;
248 }
249 }
250
251 /* seek end */
252 rc = RTFileSeek(File, 0, RTFILE_SEEK_END, NULL);
253 if (RT_FAILURE(rc))
254 {
255 RTPrintf("Failed to seek to end of file #1. rc=%Rrc\n", rc);
256 cErrors++;
257 }
258 else
259 {
260 offFile = RTFileTell(File);
261 if (offFile != _2G + _1M + sizeof(szTestBuf)) /* assuming tail write was ok. */
262 {
263 RTPrintf("RTFileTell -> %#RX64, expected %#RX64 (#4)\n", offFile, _2G + _1M + sizeof(szTestBuf));
264 cErrors++;
265 }
266 }
267
268 /* seek start */
269 rc = RTFileSeek(File, 0, RTFILE_SEEK_BEGIN, NULL);
270 if (RT_FAILURE(rc))
271 {
272 RTPrintf("Failed to seek to end of file #1. rc=%Rrc\n", rc);
273 cErrors++;
274 }
275 else
276 {
277 offFile = RTFileTell(File);
278 if (offFile != 0)
279 {
280 RTPrintf("RTFileTell -> %#llx, expected 0 (#5)\n", offFile);
281 cErrors++;
282 }
283 }
284 }
285
286
287 /*
288 * Cleanup.
289 */
290 rc = RTFileClose(File);
291 if (RT_FAILURE(rc))
292 {
293 RTPrintf("Failed to close file #1. rc=%Rrc\n", rc);
294 cErrors++;
295 }
296 rc = RTFileDelete("tstFile#1.tst");
297 if (RT_FAILURE(rc))
298 {
299 RTPrintf("Failed to delete file #1. rc=%Rrc\n", rc);
300 cErrors++;
301 }
302
303 /*
304 * Summary
305 */
306 if (cErrors == 0)
307 RTPrintf("tstFile: SUCCESS\n");
308 else
309 RTPrintf("tstFile: FAILURE - %d errors\n", cErrors);
310 return !!cErrors;
311}
312
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