1 | /* $Id: tstFile.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - File I/O.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2020 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 | *********************************************************************************************************************************/
|
---|
41 | static const char g_szTestStr[] = "Sausages and bacon for breakfast again!\n";
|
---|
42 | static 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 |
|
---|
58 | static 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 | * we currently have a questionable hack in place to make this work.
|
---|
82 | */
|
---|
83 | RTTESTI_CHECK_RC_RETV(RTFileSetSize(hFile, 0), VINF_SUCCESS);
|
---|
84 |
|
---|
85 | size_t const offWrite3 = cbWrite1 + cbWrite2;
|
---|
86 | size_t const cbWrite3 = sizeof(g_szTestStr2) - 1 - offWrite3;
|
---|
87 | RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
|
---|
88 | RTTESTI_CHECK_RC_RETV(RTFileWrite(hFile, &g_szTestStr2[offWrite3], cbWrite3, NULL), VINF_SUCCESS);
|
---|
89 |
|
---|
90 | RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
|
---|
91 | RTTESTI_CHECK_RC_RETV(RTFileRead(hFile, achBuf, cbWrite3, NULL), VINF_SUCCESS);
|
---|
92 | if (memcmp(achBuf, &g_szTestStr2[offWrite3], cbWrite3) != 0)
|
---|
93 | RTTestIFailed("Read back #2 failed (%#zx)", cbWrite3);
|
---|
94 | #endif
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | static void tstBasics(RTFILE hFile)
|
---|
99 | {
|
---|
100 | RTFOFF cbMax = -2;
|
---|
101 | int rc = RTFileQueryMaxSizeEx(hFile, &cbMax);
|
---|
102 | if (rc != VERR_NOT_IMPLEMENTED)
|
---|
103 | {
|
---|
104 | if (rc != VINF_SUCCESS)
|
---|
105 | RTTestIFailed("RTFileQueryMaxSizeEx failed: %Rrc", rc);
|
---|
106 | else
|
---|
107 | {
|
---|
108 | RTTESTI_CHECK_MSG(cbMax > 0, ("cbMax=%RTfoff", cbMax));
|
---|
109 | RTTESTI_CHECK_MSG(cbMax == RTFileGetMaxSize(hFile),
|
---|
110 | ("cbMax=%RTfoff, RTFileGetMaxSize->%RTfoff", cbMax, RTFileGetMaxSize(hFile)));
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | /* grow file beyond 2G */
|
---|
115 | rc = RTFileSetSize(hFile, _2G + _1M);
|
---|
116 | if (RT_FAILURE(rc))
|
---|
117 | RTTestIFailed("Failed to grow file #1 to 2.001GB. rc=%Rrc", rc);
|
---|
118 | else
|
---|
119 | {
|
---|
120 | uint64_t cb;
|
---|
121 | RTTESTI_CHECK_RC(RTFileQuerySize(hFile, &cb), VINF_SUCCESS);
|
---|
122 | RTTESTI_CHECK_MSG(cb == _2G + _1M, ("RTFileQuerySize return %RX64 bytes, expected %RX64.", cb, _2G + _1M));
|
---|
123 |
|
---|
124 | /*
|
---|
125 | * Try some writes at the beginning of the file.
|
---|
126 | */
|
---|
127 | uint64_t offFile = RTFileTell(hFile);
|
---|
128 | RTTESTI_CHECK_MSG(offFile == 0, ("RTFileTell -> %#RX64, expected 0 (#1)", offFile));
|
---|
129 |
|
---|
130 | size_t cbWritten = 0;
|
---|
131 | while (cbWritten < sizeof(g_szTestStr))
|
---|
132 | {
|
---|
133 | size_t cbWrittenPart;
|
---|
134 | rc = RTFileWrite(hFile, &g_szTestStr[cbWritten], sizeof(g_szTestStr) - cbWritten, &cbWrittenPart);
|
---|
135 | if (RT_FAILURE(rc))
|
---|
136 | break;
|
---|
137 | cbWritten += cbWrittenPart;
|
---|
138 | }
|
---|
139 | if (RT_FAILURE(rc))
|
---|
140 | RTTestIFailed("Failed to write to file #1 at offset 0. rc=%Rrc\n", rc);
|
---|
141 | else
|
---|
142 | {
|
---|
143 | /* check that it was written correctly. */
|
---|
144 | rc = RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL);
|
---|
145 | if (RT_FAILURE(rc))
|
---|
146 | RTTestIFailed("Failed to seek offset 0 in file #1. rc=%Rrc\n", rc);
|
---|
147 | else
|
---|
148 | {
|
---|
149 | char szReadBuf[sizeof(g_szTestStr)];
|
---|
150 | size_t cbRead = 0;
|
---|
151 | while (cbRead < sizeof(g_szTestStr))
|
---|
152 | {
|
---|
153 | size_t cbReadPart;
|
---|
154 | rc = RTFileRead(hFile, &szReadBuf[cbRead], sizeof(g_szTestStr) - cbRead, &cbReadPart);
|
---|
155 | if (RT_FAILURE(rc))
|
---|
156 | break;
|
---|
157 | cbRead += cbReadPart;
|
---|
158 | }
|
---|
159 | if (RT_FAILURE(rc))
|
---|
160 | RTTestIFailed("Failed to read from file #1 at offset 0. rc=%Rrc\n", rc);
|
---|
161 | else
|
---|
162 | {
|
---|
163 | if (!memcmp(szReadBuf, g_szTestStr, sizeof(g_szTestStr)))
|
---|
164 | RTPrintf("tstFile: head write ok\n");
|
---|
165 | else
|
---|
166 | RTTestIFailed("Data read from file #1 at offset 0 differs from what we wrote there.\n");
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | /*
|
---|
172 | * Try some writes at the end of the file.
|
---|
173 | */
|
---|
174 | rc = RTFileSeek(hFile, _2G + _1M, RTFILE_SEEK_BEGIN, NULL);
|
---|
175 | if (RT_FAILURE(rc))
|
---|
176 | RTTestIFailed("Failed to seek to _2G + _1M in file #1. rc=%Rrc\n", rc);
|
---|
177 | else
|
---|
178 | {
|
---|
179 | offFile = RTFileTell(hFile);
|
---|
180 | if (offFile != _2G + _1M)
|
---|
181 | RTTestIFailed("RTFileTell -> %#llx, expected %#llx (#2)\n", offFile, _2G + _1M);
|
---|
182 | else
|
---|
183 | {
|
---|
184 | cbWritten = 0;
|
---|
185 | while (cbWritten < sizeof(g_szTestStr))
|
---|
186 | {
|
---|
187 | size_t cbWrittenPart;
|
---|
188 | rc = RTFileWrite(hFile, &g_szTestStr[cbWritten], sizeof(g_szTestStr) - cbWritten, &cbWrittenPart);
|
---|
189 | if (RT_FAILURE(rc))
|
---|
190 | break;
|
---|
191 | cbWritten += cbWrittenPart;
|
---|
192 | }
|
---|
193 | if (RT_FAILURE(rc))
|
---|
194 | RTTestIFailed("Failed to write to file #1 at offset 2G + 1M. rc=%Rrc\n", rc);
|
---|
195 | else
|
---|
196 | {
|
---|
197 | rc = RTFileSeek(hFile, offFile, RTFILE_SEEK_BEGIN, NULL);
|
---|
198 | if (RT_FAILURE(rc))
|
---|
199 | RTTestIFailed("Failed to seek offset %RX64 in file #1. rc=%Rrc\n", offFile, rc);
|
---|
200 | else
|
---|
201 | {
|
---|
202 | char szReadBuf[sizeof(g_szTestStr)];
|
---|
203 | size_t cbRead = 0;
|
---|
204 | while (cbRead < sizeof(g_szTestStr))
|
---|
205 | {
|
---|
206 | size_t cbReadPart;
|
---|
207 | rc = RTFileRead(hFile, &szReadBuf[cbRead], sizeof(g_szTestStr) - cbRead, &cbReadPart);
|
---|
208 | if (RT_FAILURE(rc))
|
---|
209 | break;
|
---|
210 | cbRead += cbReadPart;
|
---|
211 | }
|
---|
212 | if (RT_FAILURE(rc))
|
---|
213 | RTTestIFailed("Failed to read from file #1 at offset 2G + 1M. rc=%Rrc\n", rc);
|
---|
214 | else
|
---|
215 | {
|
---|
216 | if (!memcmp(szReadBuf, g_szTestStr, sizeof(g_szTestStr)))
|
---|
217 | RTPrintf("tstFile: tail write ok\n");
|
---|
218 | else
|
---|
219 | RTTestIFailed("Data read from file #1 at offset 2G + 1M differs from what we wrote there.\n");
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | /*
|
---|
227 | * Some general seeking around.
|
---|
228 | */
|
---|
229 | rc = RTFileSeek(hFile, _2G + 1, RTFILE_SEEK_BEGIN, NULL);
|
---|
230 | if (RT_FAILURE(rc))
|
---|
231 | RTTestIFailed("Failed to seek to _2G + 1 in file #1. rc=%Rrc\n", rc);
|
---|
232 | else
|
---|
233 | {
|
---|
234 | offFile = RTFileTell(hFile);
|
---|
235 | if (offFile != _2G + 1)
|
---|
236 | RTTestIFailed("RTFileTell -> %#llx, expected %#llx (#3)\n", offFile, _2G + 1);
|
---|
237 | }
|
---|
238 |
|
---|
239 | /* seek end */
|
---|
240 | rc = RTFileSeek(hFile, 0, RTFILE_SEEK_END, NULL);
|
---|
241 | if (RT_FAILURE(rc))
|
---|
242 | RTTestIFailed("Failed to seek to end of file #1. rc=%Rrc\n", rc);
|
---|
243 | else
|
---|
244 | {
|
---|
245 | offFile = RTFileTell(hFile);
|
---|
246 | if (offFile != _2G + _1M + sizeof(g_szTestStr)) /* assuming tail write was ok. */
|
---|
247 | RTTestIFailed("RTFileTell -> %#RX64, expected %#RX64 (#4)\n", offFile, _2G + _1M + sizeof(g_szTestStr));
|
---|
248 | }
|
---|
249 |
|
---|
250 | /* seek start */
|
---|
251 | rc = RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL);
|
---|
252 | if (RT_FAILURE(rc))
|
---|
253 | RTTestIFailed("Failed to seek to end of file #1. rc=%Rrc\n", rc);
|
---|
254 | else
|
---|
255 | {
|
---|
256 | offFile = RTFileTell(hFile);
|
---|
257 | if (offFile != 0)
|
---|
258 | RTTestIFailed("RTFileTell -> %#llx, expected 0 (#5)\n", offFile);
|
---|
259 | }
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 | int main()
|
---|
264 | {
|
---|
265 | RTTEST hTest;
|
---|
266 | RTEXITCODE rcExit = RTTestInitAndCreate("tstRTFile", &hTest);
|
---|
267 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
268 | return rcExit;
|
---|
269 | RTTestBanner(hTest);
|
---|
270 |
|
---|
271 | /*
|
---|
272 | * Some basic tests.
|
---|
273 | */
|
---|
274 | RTTestSub(hTest, "Basics");
|
---|
275 | int rc = -1;
|
---|
276 | RTFILE hFile = NIL_RTFILE;
|
---|
277 | RTTESTI_CHECK_RC(rc = RTFileOpen(&hFile, "tstFile#1.tst", RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE),
|
---|
278 | VINF_SUCCESS);
|
---|
279 | if (RT_SUCCESS(rc))
|
---|
280 | {
|
---|
281 | tstBasics(hFile);
|
---|
282 | RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
|
---|
283 | RTTESTI_CHECK_RC(RTFileDelete("tstFile#1.tst"), VINF_SUCCESS);
|
---|
284 | }
|
---|
285 |
|
---|
286 | /*
|
---|
287 | * Test appending & truncation.
|
---|
288 | */
|
---|
289 | RTTestSub(hTest, "Append");
|
---|
290 | hFile = NIL_RTFILE;
|
---|
291 | RTTESTI_CHECK_RC(rc = RTFileOpen(&hFile, "tstFile#2.tst",
|
---|
292 | RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_APPEND),
|
---|
293 | VINF_SUCCESS);
|
---|
294 | if (RT_SUCCESS(rc))
|
---|
295 | {
|
---|
296 | tstAppend(hFile);
|
---|
297 | RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
|
---|
298 | RTTESTI_CHECK_RC(RTFileDelete("tstFile#2.tst"), VINF_SUCCESS);
|
---|
299 | }
|
---|
300 |
|
---|
301 | /*
|
---|
302 | * Done.
|
---|
303 | */
|
---|
304 | return RTTestSummaryAndDestroy(hTest);
|
---|
305 | }
|
---|
306 |
|
---|