VirtualBox

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

Last change on this file since 1532 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.2 KB
Line 
1/* $Id: tstFile.cpp 1 1970-01-01 00:00:00Z vboxsync $ */
2/** @file
3 * InnoTek Portable Runtime Testcase - File I/O.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#include <iprt/file.h>
27#include <iprt/err.h>
28#include <iprt/string.h>
29#include <iprt/stream.h>
30#include <iprt/runtime.h>
31
32
33int main()
34{
35 int cErrors = 0;
36 RTPrintf("tstFile: TESTING\n");
37 RTR3Init();
38
39 RTFILE File;
40 int rc = RTFileOpen(&File, "tstFile#1.tst", RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE);
41 if (RT_FAILURE(rc))
42 {
43 RTPrintf("tstFile: FATAL ERROR - Failed to open file #1. rc=%Rrc\n", rc);
44 return 1;
45 }
46
47 /* grow file beyond 2G */
48 rc = RTFileSetSize(File, _2G + _1M);
49 if (RT_FAILURE(rc))
50 {
51 RTPrintf("Failed to grow file #1 to 2.001GB. rc=%Rrc\n", rc);
52 cErrors++;
53 }
54 else
55 {
56 uint64_t cb;
57 rc = RTFileGetSize(File, &cb);
58 if (RT_FAILURE(rc))
59 {
60 RTPrintf("Failed to get file size of #1. rc=%Rrc\n", rc);
61 cErrors++;
62 }
63 else if (cb != _2G + _1M)
64 {
65 RTPrintf("RTFileGetSize return %RX64 bytes, expected %RX64.\n", cb, _2G + _1M);
66 cErrors++;
67 }
68 else
69 RTPrintf("tstFile: cb=%RX64\n", cb);
70
71 /*
72 * Try some writes at the beginning of the file.
73 */
74 uint64_t offFile = RTFileTell(File);
75 if (offFile != 0)
76 {
77 RTPrintf("RTFileTell -> %#RX64, expected 0 (#1)\n", offFile);
78 cErrors++;
79 }
80 static const char szTestBuf[] = "Sausages and bacon for breakfast again!";
81 unsigned cbWritten = 0;
82 while (cbWritten < sizeof(szTestBuf))
83 {
84 unsigned cbWrittenPart;
85 rc = RTFileWrite(File, &szTestBuf[cbWritten], sizeof(szTestBuf) - cbWritten, &cbWrittenPart);
86 if (RT_FAILURE(rc))
87 break;
88 cbWritten += cbWrittenPart;
89 }
90 if (RT_FAILURE(rc))
91 {
92 RTPrintf("Failed to write to file #1 at offset 0. rc=%Rrc\n", rc);
93 cErrors++;
94 }
95 else
96 {
97 /* check that it was written correctly. */
98 rc = RTFileSeek(File, 0, RTFILE_SEEK_BEGIN, NULL);
99 if (RT_FAILURE(rc))
100 {
101 RTPrintf("Failed to seek offset 0 in file #1. rc=%Rrc\n", rc);
102 cErrors++;
103 }
104 else
105 {
106 char szReadBuf[sizeof(szTestBuf)];
107 unsigned cbRead = 0;
108 while (cbRead < sizeof(szTestBuf))
109 {
110 unsigned cbReadPart;
111 rc = RTFileRead(File, &szReadBuf[cbRead], sizeof(szTestBuf) - cbRead, &cbReadPart);
112 if (RT_FAILURE(rc))
113 break;
114 cbRead += cbReadPart;
115 }
116 if (RT_FAILURE(rc))
117 {
118 RTPrintf("Failed to read from file #1 at offset 0. rc=%Rrc\n", rc);
119 cErrors++;
120 }
121 else
122 {
123 if (!memcmp(szReadBuf, szTestBuf, sizeof(szTestBuf)))
124 RTPrintf("tstFile: head write ok\n");
125 else
126 {
127 RTPrintf("Data read from file #1 at offset 0 differs from what we wrote there.\n");
128 cErrors++;
129 }
130 }
131 }
132 }
133
134 /*
135 * Try some writes at the end of the file.
136 */
137 rc = RTFileSeek(File, _2G + _1M, RTFILE_SEEK_BEGIN, NULL);
138 if (RT_FAILURE(rc))
139 {
140 RTPrintf("Failed to seek to _2G + _1M in file #1. rc=%Rrc\n", rc);
141 cErrors++;
142 }
143 else
144 {
145 offFile = RTFileTell(File);
146 if (offFile != _2G + _1M)
147 {
148 RTPrintf("RTFileTell -> %#llx, expected %#llx (#2)\n", offFile, _2G + _1M);
149 cErrors++;
150 }
151 else
152 {
153 unsigned cbWritten = 0;
154 while (cbWritten < sizeof(szTestBuf))
155 {
156 unsigned cbWrittenPart;
157 rc = RTFileWrite(File, &szTestBuf[cbWritten], sizeof(szTestBuf) - cbWritten, &cbWrittenPart);
158 if (RT_FAILURE(rc))
159 break;
160 cbWritten += cbWrittenPart;
161 }
162 if (RT_FAILURE(rc))
163 {
164 RTPrintf("Failed to write to file #1 at offset 2G + 1M. rc=%Rrc\n", rc);
165 cErrors++;
166 }
167 else
168 {
169 rc = RTFileSeek(File, offFile, RTFILE_SEEK_BEGIN, NULL);
170 if (RT_FAILURE(rc))
171 {
172 RTPrintf("Failed to seek offset %RX64 in file #1. rc=%Rrc\n", offFile, rc);
173 cErrors++;
174 }
175 else
176 {
177 char szReadBuf[sizeof(szTestBuf)];
178 unsigned cbRead = 0;
179 while (cbRead < sizeof(szTestBuf))
180 {
181 unsigned cbReadPart;
182 rc = RTFileRead(File, &szReadBuf[cbRead], sizeof(szTestBuf) - cbRead, &cbReadPart);
183 if (RT_FAILURE(rc))
184 break;
185 cbRead += cbReadPart;
186 }
187 if (RT_FAILURE(rc))
188 {
189 RTPrintf("Failed to read from file #1 at offset 2G + 1M. rc=%Rrc\n", rc);
190 cErrors++;
191 }
192 else
193 {
194 if (!memcmp(szReadBuf, szTestBuf, sizeof(szTestBuf)))
195 RTPrintf("tstFile: tail write ok\n");
196 else
197 {
198 RTPrintf("Data read from file #1 at offset 2G + 1M differs from what we wrote there.\n");
199 cErrors++;
200 }
201 }
202 }
203 }
204 }
205 }
206
207 /*
208 * Some general seeking around.
209 */
210 rc = RTFileSeek(File, _2G + 1, RTFILE_SEEK_BEGIN, NULL);
211 if (RT_FAILURE(rc))
212 {
213 RTPrintf("Failed to seek to _2G + 1 in file #1. rc=%Rrc\n", rc);
214 cErrors++;
215 }
216 else
217 {
218 offFile = RTFileTell(File);
219 if (offFile != _2G + 1)
220 {
221 RTPrintf("RTFileTell -> %#llx, expected %#llx (#3)\n", offFile, _2G + 1);
222 cErrors++;
223 }
224 }
225
226 /* seek end */
227 rc = RTFileSeek(File, 0, RTFILE_SEEK_END, NULL);
228 if (RT_FAILURE(rc))
229 {
230 RTPrintf("Failed to seek to end of file #1. rc=%Rrc\n", rc);
231 cErrors++;
232 }
233 else
234 {
235 offFile = RTFileTell(File);
236 if (offFile != _2G + _1M + sizeof(szTestBuf)) /* assuming tail write was ok. */
237 {
238 RTPrintf("RTFileTell -> %#RX64, expected %#RX64 (#4)\n", offFile, _2G + _1M + sizeof(szTestBuf));
239 cErrors++;
240 }
241 }
242
243 /* seek start */
244 rc = RTFileSeek(File, 0, RTFILE_SEEK_BEGIN, NULL);
245 if (RT_FAILURE(rc))
246 {
247 RTPrintf("Failed to seek to end of file #1. rc=%Rrc\n", rc);
248 cErrors++;
249 }
250 else
251 {
252 offFile = RTFileTell(File);
253 if (offFile != 0)
254 {
255 RTPrintf("RTFileTell -> %#llx, expected 0 (#5)\n", offFile);
256 cErrors++;
257 }
258 }
259 }
260
261
262 /*
263 * Cleanup.
264 */
265 rc = RTFileClose(File);
266 if (RT_FAILURE(rc))
267 {
268 RTPrintf("Failed to close file #1. rc=%Rrc\n", rc);
269 cErrors++;
270 }
271 rc = RTFileDelete("tstFile#1.tst");
272 if (RT_FAILURE(rc))
273 {
274 RTPrintf("Failed to delete file #1. rc=%Rrc\n", rc);
275 cErrors++;
276 }
277
278 /*
279 * Summary
280 */
281 if (cErrors == 0)
282 RTPrintf("tstFile: SUCCESS\n");
283 else
284 RTPrintf("tstFile: FAILURE - %d errors\n", cErrors);
285 return !!cErrors;
286}
287
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