1 | /* $Id: tstFileAppendWin-1.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Exploration of File Appending on Windows.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-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/win/windows.h>
|
---|
42 | #include <stdio.h>
|
---|
43 | #include <string.h>
|
---|
44 | #include <stdarg.h>
|
---|
45 |
|
---|
46 |
|
---|
47 | /*********************************************************************************************************************************
|
---|
48 | * Global Variables *
|
---|
49 | *********************************************************************************************************************************/
|
---|
50 | static int g_cErrors = 0;
|
---|
51 |
|
---|
52 |
|
---|
53 | static int MyFailure(const char *pszFormat, ...)
|
---|
54 | {
|
---|
55 | va_list va;
|
---|
56 |
|
---|
57 | printf("tstFileAppendWin-1: FATAL: ");
|
---|
58 | va_start(va, pszFormat);
|
---|
59 | vprintf(pszFormat, va);
|
---|
60 | va_end(va);
|
---|
61 | g_cErrors++;
|
---|
62 | return 1;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | void MyError(const char *pszFormat, ...)
|
---|
67 | {
|
---|
68 | va_list va;
|
---|
69 |
|
---|
70 | printf("tstFileAppendWin-1: ERROR: ");
|
---|
71 | va_start(va, pszFormat);
|
---|
72 | vprintf(pszFormat, va);
|
---|
73 | va_end(va);
|
---|
74 | g_cErrors++;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | int main()
|
---|
79 | {
|
---|
80 | HANDLE hFile;
|
---|
81 | LARGE_INTEGER off;
|
---|
82 | DWORD cb;
|
---|
83 | char szBuf[256];
|
---|
84 |
|
---|
85 |
|
---|
86 | printf("tstFileAppendWin-1: TESTING...\n");
|
---|
87 |
|
---|
88 | /*
|
---|
89 | * Open it write only and do some appending.
|
---|
90 | * Checking that read fails and that the file position changes after the read.
|
---|
91 | */
|
---|
92 | DeleteFile("tstFileAppendWin-1.tst");
|
---|
93 | hFile = CreateFile("tstFileAppendWin-1.tst",
|
---|
94 | (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA),
|
---|
95 | FILE_SHARE_READ,
|
---|
96 | NULL,
|
---|
97 | CREATE_ALWAYS,
|
---|
98 | FILE_ATTRIBUTE_NORMAL,
|
---|
99 | NULL);
|
---|
100 | if (hFile == INVALID_HANDLE_VALUE)
|
---|
101 | return MyFailure("1st CreateFile: %d\n", GetLastError());
|
---|
102 |
|
---|
103 | off.QuadPart = 0;
|
---|
104 | if (!SetFilePointerEx(hFile, off, &off, FILE_CURRENT))
|
---|
105 | MyError("1st SetFilePointerEx failed: %d\n", GetLastError());
|
---|
106 | else if (off.QuadPart != 0)
|
---|
107 | MyError("unexpected position on open: %ld - expected 0\n", (long)off.QuadPart);
|
---|
108 |
|
---|
109 | if (!WriteFile(hFile, "0123456789", 10, &cb, NULL))
|
---|
110 | MyError("write fail: %d\n", GetLastError());
|
---|
111 |
|
---|
112 | off.QuadPart = 0;
|
---|
113 | if (!SetFilePointerEx(hFile, off, &off, FILE_CURRENT))
|
---|
114 | MyError("2nd SetFilePointerEx failed: %d\n", GetLastError());
|
---|
115 | else if (off.QuadPart != 10)
|
---|
116 | MyError("unexpected position on write: %ld - expected 10\n", (long)off.QuadPart);
|
---|
117 | else
|
---|
118 | printf("tstFileAppendWin-1: off=%ld after first write\n", (long)off.QuadPart);
|
---|
119 |
|
---|
120 | SetLastError(0);
|
---|
121 | if (ReadFile(hFile, szBuf, 1, &cb, NULL))
|
---|
122 | MyError("read didn't fail! cb=%#lx lasterr=%d\n", (long)cb, GetLastError());
|
---|
123 |
|
---|
124 | off.QuadPart = 5;
|
---|
125 | if (!SetFilePointerEx(hFile, off, &off, FILE_BEGIN))
|
---|
126 | MyError("3rd SetFilePointerEx failed: %d\n", GetLastError());
|
---|
127 | else if (off.QuadPart != 5)
|
---|
128 | MyError("unexpected position after set file pointer: %ld - expected 5\n", (long)off.QuadPart);
|
---|
129 |
|
---|
130 | CloseHandle(hFile);
|
---|
131 |
|
---|
132 | /*
|
---|
133 | * Open it write only and do some more appending.
|
---|
134 | * Checking the initial position and that it changes after the write.
|
---|
135 | */
|
---|
136 | hFile = CreateFile("tstFileAppendWin-1.tst",
|
---|
137 | (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA),
|
---|
138 | FILE_SHARE_READ,
|
---|
139 | NULL,
|
---|
140 | OPEN_EXISTING,
|
---|
141 | FILE_ATTRIBUTE_NORMAL,
|
---|
142 | NULL);
|
---|
143 | if (hFile == INVALID_HANDLE_VALUE)
|
---|
144 | return MyFailure("2nd CreateFile: %d\n", GetLastError());
|
---|
145 |
|
---|
146 | off.QuadPart = 0;
|
---|
147 | if (!SetFilePointerEx(hFile, off, &off, FILE_CURRENT))
|
---|
148 | MyError("4th SetFilePointerEx failed: %d\n", GetLastError());
|
---|
149 | else if (off.QuadPart != 0)
|
---|
150 | MyError("unexpected position on open: %ld - expected 0\n", (long)off.QuadPart);
|
---|
151 | else
|
---|
152 | printf("tstFileAppendWin-1: off=%ld on 2nd open\n", (long)off.QuadPart);
|
---|
153 |
|
---|
154 | if (!WriteFile(hFile, "abcdefghij", 10, &cb, NULL))
|
---|
155 | MyError("2nd write failed: %d\n", GetLastError());
|
---|
156 |
|
---|
157 | off.QuadPart = 0;
|
---|
158 | if (!SetFilePointerEx(hFile, off, &off, FILE_CURRENT))
|
---|
159 | MyError("5th SetFilePointerEx failed: %d\n", GetLastError());
|
---|
160 | else if (off.QuadPart != 20)
|
---|
161 | MyError("unexpected position on 2nd write: %ld - expected 20\n", (long)off.QuadPart);
|
---|
162 | else
|
---|
163 | printf("tstFileAppendWin-1: off=%ld after 2nd write\n", (long)off.QuadPart);
|
---|
164 |
|
---|
165 | CloseHandle(hFile);
|
---|
166 |
|
---|
167 | /*
|
---|
168 | * Open it read/write.
|
---|
169 | * Check the initial position and read stuff. Then append some more and
|
---|
170 | * check the new position and see that read returns 0/EOF. Finally,
|
---|
171 | * do some seeking and read from a new position.
|
---|
172 | */
|
---|
173 | hFile = CreateFile("tstFileAppendWin-1.tst",
|
---|
174 | (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA) | GENERIC_READ,
|
---|
175 | FILE_SHARE_READ,
|
---|
176 | NULL,
|
---|
177 | OPEN_EXISTING,
|
---|
178 | FILE_ATTRIBUTE_NORMAL,
|
---|
179 | NULL);
|
---|
180 | if (hFile == INVALID_HANDLE_VALUE)
|
---|
181 | return MyFailure("3rd CreateFile: %d\n", GetLastError());
|
---|
182 |
|
---|
183 | off.QuadPart = 0;
|
---|
184 | if (!SetFilePointerEx(hFile, off, &off, FILE_CURRENT))
|
---|
185 | MyError("6th SetFilePointerEx failed: %d\n", GetLastError());
|
---|
186 | else if (off.QuadPart != 0)
|
---|
187 | MyError("unexpected position on open: %ld - expected 0\n", (long)off.QuadPart);
|
---|
188 | else
|
---|
189 | printf("tstFileAppendWin-1: off=%ld on 3rd open\n", (long)off.QuadPart);
|
---|
190 |
|
---|
191 | if (!ReadFile(hFile, szBuf, 10, &cb, NULL) || cb != 10)
|
---|
192 | MyError("1st ReadFile failed: %d\n", GetLastError());
|
---|
193 | else if (memcmp(szBuf, "0123456789", 10))
|
---|
194 | MyError("read the wrong stuff: %.10s - expected 0123456789\n", szBuf);
|
---|
195 |
|
---|
196 | off.QuadPart = 0;
|
---|
197 | if (!SetFilePointerEx(hFile, off, &off, FILE_CURRENT))
|
---|
198 | MyError("7th SetFilePointerEx failed: %d\n", GetLastError());
|
---|
199 | else if (off.QuadPart != 10)
|
---|
200 | MyError("unexpected position on 1st read: %ld - expected 0\n", (long)off.QuadPart);
|
---|
201 | else
|
---|
202 | printf("tstFileAppendWin-1: off=%ld on 1st read\n", (long)off.QuadPart);
|
---|
203 |
|
---|
204 | if (!WriteFile(hFile, "klmnopqrst", 10, &cb, NULL))
|
---|
205 | MyError("3rd write failed: %d\n", GetLastError());
|
---|
206 |
|
---|
207 | off.QuadPart = 0;
|
---|
208 | if (!SetFilePointerEx(hFile, off, &off, FILE_CURRENT))
|
---|
209 | MyError("8th SetFilePointerEx failed: %d\n", GetLastError());
|
---|
210 | else if (off.QuadPart != 30)
|
---|
211 | MyError("unexpected position on 3rd write: %ld - expected 30\n", (long)off.QuadPart);
|
---|
212 | else
|
---|
213 | printf("tstFileAppendWin-1: off=%ld after 3rd write\n", (long)off.QuadPart);
|
---|
214 |
|
---|
215 | SetLastError(0);
|
---|
216 | if (ReadFile(hFile, szBuf, 1, &cb, NULL) && cb != 0)
|
---|
217 | MyError("read after write didn't fail! cb=%#lx lasterr=%d\n", (long)cb, GetLastError());
|
---|
218 |
|
---|
219 | off.QuadPart = 15;
|
---|
220 | if (!SetFilePointerEx(hFile, off, &off, FILE_BEGIN))
|
---|
221 | MyError("9th SetFilePointerEx failed: %d\n", GetLastError());
|
---|
222 | else if (off.QuadPart != 15)
|
---|
223 | MyError("unexpected position on 3rd write: %ld - expected 15\n", (long)off.QuadPart);
|
---|
224 | else
|
---|
225 | {
|
---|
226 | if (!ReadFile(hFile, szBuf, 10, &cb, NULL) || cb != 10)
|
---|
227 | MyError("1st ReadFile failed: %d\n", GetLastError());
|
---|
228 | else if (memcmp(szBuf, "fghijklmno", 10))
|
---|
229 | MyError("read the wrong stuff: %.10s - expected fghijklmno\n", szBuf);
|
---|
230 |
|
---|
231 | off.QuadPart = 0;
|
---|
232 | if (!SetFilePointerEx(hFile, off, &off, FILE_CURRENT))
|
---|
233 | MyError("10th SetFilePointerEx failed: %d\n", GetLastError());
|
---|
234 | else if (off.QuadPart != 25)
|
---|
235 | MyError("unexpected position on 2nd read: %ld - expected 25\n", (long)off.QuadPart);
|
---|
236 | else
|
---|
237 | printf("tstFileAppendWin-1: off=%ld after 2nd read\n", (long)off.QuadPart);
|
---|
238 | }
|
---|
239 |
|
---|
240 | CloseHandle(hFile);
|
---|
241 |
|
---|
242 | /*
|
---|
243 | * Open it read only + append and check that we cannot write to it.
|
---|
244 | */
|
---|
245 | hFile = CreateFile("tstFileAppendWin-1.tst",
|
---|
246 | FILE_APPEND_DATA | GENERIC_READ,
|
---|
247 | FILE_SHARE_READ,
|
---|
248 | NULL,
|
---|
249 | CREATE_ALWAYS,
|
---|
250 | FILE_ATTRIBUTE_NORMAL,
|
---|
251 | NULL);
|
---|
252 | if (hFile == INVALID_HANDLE_VALUE)
|
---|
253 | return MyFailure("4th CreateFile: %d\n", GetLastError());
|
---|
254 |
|
---|
255 | SetLastError(0);
|
---|
256 | if (WriteFile(hFile, "pqrstuvwx", 10, &cb, NULL))
|
---|
257 | MyError("write didn't on read-only+append open: %d cb=%#lx\n", GetLastError(), (long)cb);
|
---|
258 |
|
---|
259 | CloseHandle(hFile);
|
---|
260 | DeleteFile("tstfileAppendWin-1.tst");
|
---|
261 |
|
---|
262 | if (g_cErrors)
|
---|
263 | printf("tstFileAppendWin-1: FAILED\n");
|
---|
264 | else
|
---|
265 | printf("tstFileAppendWin-1: SUCCESS\n");
|
---|
266 | return g_cErrors ? 1 : 0;
|
---|
267 | }
|
---|
268 |
|
---|