1 | /* $Id: tstRTFileOpenEx-1.cpp 100678 2023-07-21 13:53:16Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - File Opening, extended API.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2019-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/file.h>
|
---|
42 |
|
---|
43 | #include <iprt/err.h>
|
---|
44 | #include <iprt/path.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 | #include <iprt/test.h>
|
---|
47 |
|
---|
48 |
|
---|
49 | /*********************************************************************************************************************************
|
---|
50 | * Global Variables *
|
---|
51 | *********************************************************************************************************************************/
|
---|
52 | static const char g_szTestFile[] = "tstFileOpenEx-1.tst";
|
---|
53 |
|
---|
54 |
|
---|
55 | /** @note FsPerf have a copy of this code. */
|
---|
56 | static void tstOpenExTest(unsigned uLine, int cbExist, int cbNext, const char *pszFilename, uint64_t fAction,
|
---|
57 | int rcExpect, RTFILEACTION enmActionExpected)
|
---|
58 | {
|
---|
59 | uint64_t const fCreateMode = (0644 << RTFILE_O_CREATE_MODE_SHIFT);
|
---|
60 | RTFILE hFile;
|
---|
61 | int rc;
|
---|
62 |
|
---|
63 | /*
|
---|
64 | * File existence and size.
|
---|
65 | */
|
---|
66 | bool fOkay = false;
|
---|
67 | RTFSOBJINFO ObjInfo;
|
---|
68 | rc = RTPathQueryInfoEx(pszFilename, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
|
---|
69 | if (RT_SUCCESS(rc))
|
---|
70 | fOkay = cbExist == (int64_t)ObjInfo.cbObject;
|
---|
71 | else
|
---|
72 | fOkay = rc == VERR_FILE_NOT_FOUND && cbExist < 0;
|
---|
73 | if (!fOkay)
|
---|
74 | {
|
---|
75 | if (cbExist >= 0)
|
---|
76 | {
|
---|
77 | rc = RTFileOpen(&hFile, pszFilename, RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | fCreateMode);
|
---|
78 | if (RT_SUCCESS(rc))
|
---|
79 | {
|
---|
80 | while (cbExist > 0)
|
---|
81 | {
|
---|
82 | int cbToWrite = (int)strlen(pszFilename);
|
---|
83 | if (cbToWrite > cbExist)
|
---|
84 | cbToWrite = cbExist;
|
---|
85 | rc = RTFileWrite(hFile, pszFilename, cbToWrite, NULL);
|
---|
86 | if (RT_FAILURE(rc))
|
---|
87 | {
|
---|
88 | RTTestIFailed("%u: RTFileWrite(%s,%#x) -> %Rrc\n", uLine, pszFilename, cbToWrite, rc);
|
---|
89 | break;
|
---|
90 | }
|
---|
91 | cbExist -= cbToWrite;
|
---|
92 | }
|
---|
93 |
|
---|
94 | RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
|
---|
95 | }
|
---|
96 | else
|
---|
97 | RTTestIFailed("%u: RTFileOpen(%s) -> %Rrc\n", uLine, pszFilename, rc);
|
---|
98 |
|
---|
99 | }
|
---|
100 | else
|
---|
101 | {
|
---|
102 | rc = RTFileDelete(pszFilename);
|
---|
103 | if (rc != VINF_SUCCESS && rc != VERR_FILE_NOT_FOUND)
|
---|
104 | RTTestIFailed("%u: RTFileDelete(%s) -> %Rrc\n", uLine, pszFilename, rc);
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | /*
|
---|
109 | * The actual test.
|
---|
110 | */
|
---|
111 | RTFILEACTION enmActuallyTaken = RTFILEACTION_END;
|
---|
112 | hFile = NIL_RTFILE;
|
---|
113 | rc = RTFileOpenEx(pszFilename, fAction | RTFILE_O_READWRITE | RTFILE_O_DENY_NONE | fCreateMode, &hFile, &enmActuallyTaken);
|
---|
114 | if ( rc != rcExpect
|
---|
115 | || enmActuallyTaken != enmActionExpected
|
---|
116 | || (RT_SUCCESS(rc) ? hFile == NIL_RTFILE : hFile != NIL_RTFILE))
|
---|
117 | RTTestIFailed("%u: RTFileOpenEx(%s, %#llx) -> %Rrc + %d (hFile=%p), expected %Rrc + %d\n",
|
---|
118 | uLine, pszFilename, fAction, rc, enmActuallyTaken, hFile, rcExpect, enmActionExpected);
|
---|
119 | if (RT_SUCCESS(rc))
|
---|
120 | {
|
---|
121 | if ( enmActionExpected == RTFILEACTION_REPLACED
|
---|
122 | || enmActionExpected == RTFILEACTION_TRUNCATED)
|
---|
123 | {
|
---|
124 | uint8_t abBuf[16];
|
---|
125 | rc = RTFileRead(hFile, abBuf, 1, NULL);
|
---|
126 | if (rc != VERR_EOF)
|
---|
127 | RTTestIFailed("%u: RTFileRead(%s,,1,) -> %Rrc, expected VERR_EOF\n", uLine, pszFilename, rc);
|
---|
128 | }
|
---|
129 |
|
---|
130 | while (cbNext > 0)
|
---|
131 | {
|
---|
132 | int cbToWrite = (int)strlen(pszFilename);
|
---|
133 | if (cbToWrite > cbNext)
|
---|
134 | cbToWrite = cbNext;
|
---|
135 | rc = RTFileWrite(hFile, pszFilename, cbToWrite, NULL);
|
---|
136 | if (RT_FAILURE(rc))
|
---|
137 | {
|
---|
138 | RTTestIFailed("%u: RTFileWrite(%s,%#x) -> %Rrc\n", uLine, pszFilename, cbToWrite, rc);
|
---|
139 | break;
|
---|
140 | }
|
---|
141 | cbNext -= cbToWrite;
|
---|
142 | }
|
---|
143 |
|
---|
144 | rc = RTFileClose(hFile);
|
---|
145 | if (RT_FAILURE(rc))
|
---|
146 | RTTestIFailed("%u: RTFileClose(%p) -> %Rrc\n", uLine, hFile, rc);
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | /** @note FsPerf have a copy of this code. */
|
---|
152 | static void tstFileActionTaken(RTTEST hTest)
|
---|
153 | {
|
---|
154 | RTTestSub(hTest, "Action taken");
|
---|
155 |
|
---|
156 | /*
|
---|
157 | * RTFILE_O_OPEN and RTFILE_O_OPEN_CREATE.
|
---|
158 | */
|
---|
159 | /* RTFILE_O_OPEN - non-existing: */
|
---|
160 | tstOpenExTest(__LINE__, -1, -1, g_szTestFile, RTFILE_O_OPEN, VERR_FILE_NOT_FOUND, RTFILEACTION_INVALID);
|
---|
161 |
|
---|
162 | /* RTFILE_O_OPEN_CREATE - non-existing: */
|
---|
163 | tstOpenExTest(__LINE__, -1, -1, g_szTestFile, RTFILE_O_OPEN_CREATE, VINF_SUCCESS, RTFILEACTION_CREATED);
|
---|
164 |
|
---|
165 | /* RTFILE_O_OPEN_CREATE - existing: */
|
---|
166 | tstOpenExTest(__LINE__, 0, 0, g_szTestFile, RTFILE_O_OPEN_CREATE, VINF_SUCCESS, RTFILEACTION_OPENED);
|
---|
167 |
|
---|
168 | /* RTFILE_O_OPEN - existing: */
|
---|
169 | tstOpenExTest(__LINE__, 0, 0, g_szTestFile, RTFILE_O_OPEN, VINF_SUCCESS, RTFILEACTION_OPENED);
|
---|
170 |
|
---|
171 | /*
|
---|
172 | * RTFILE_O_OPEN and RTFILE_O_OPEN_CREATE w/ TRUNCATE variations.
|
---|
173 | */
|
---|
174 | /* RTFILE_O_OPEN + TRUNCATE - existing zero sized file: */
|
---|
175 | tstOpenExTest(__LINE__, 0, 0, g_szTestFile, RTFILE_O_OPEN | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_TRUNCATED);
|
---|
176 |
|
---|
177 | /* RTFILE_O_OPEN_CREATE + TRUNCATE - existing zero sized file: */
|
---|
178 | tstOpenExTest(__LINE__, 0, 10, g_szTestFile, RTFILE_O_OPEN_CREATE | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_TRUNCATED);
|
---|
179 |
|
---|
180 | /* RTFILE_O_OPEN_CREATE + TRUNCATE - existing non-zero sized file: */
|
---|
181 | tstOpenExTest(__LINE__, 10, 10, g_szTestFile, RTFILE_O_OPEN_CREATE | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_TRUNCATED);
|
---|
182 |
|
---|
183 | /* RTFILE_O_OPEN + TRUNCATE - existing non-zero sized file: */
|
---|
184 | tstOpenExTest(__LINE__, 10, -1, g_szTestFile, RTFILE_O_OPEN | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_TRUNCATED);
|
---|
185 |
|
---|
186 | /* RTFILE_O_OPEN + TRUNCATE - non-existing file: */
|
---|
187 | tstOpenExTest(__LINE__, -1, -1, g_szTestFile, RTFILE_O_OPEN | RTFILE_O_TRUNCATE, VERR_FILE_NOT_FOUND, RTFILEACTION_INVALID);
|
---|
188 |
|
---|
189 | /* RTFILE_O_OPEN_CREATE + TRUNCATE - non-existing file: */
|
---|
190 | tstOpenExTest(__LINE__, -1, 0, g_szTestFile, RTFILE_O_OPEN_CREATE | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_CREATED);
|
---|
191 |
|
---|
192 | /*
|
---|
193 | * RTFILE_O_CREATE and RTFILE_O_CREATE_REPLACE.
|
---|
194 | */
|
---|
195 | /* RTFILE_O_CREATE_REPLACE - existing: */
|
---|
196 | tstOpenExTest(__LINE__, 0, -1, g_szTestFile, RTFILE_O_CREATE_REPLACE, VINF_SUCCESS, RTFILEACTION_REPLACED);
|
---|
197 |
|
---|
198 | /* RTFILE_O_CREATE_REPLACE - non-existing: */
|
---|
199 | tstOpenExTest(__LINE__, -1, 0, g_szTestFile, RTFILE_O_CREATE_REPLACE, VINF_SUCCESS, RTFILEACTION_CREATED);
|
---|
200 |
|
---|
201 | /* RTFILE_O_CREATE - existing: */
|
---|
202 | tstOpenExTest(__LINE__, 0, -1, g_szTestFile, RTFILE_O_CREATE, VERR_ALREADY_EXISTS, RTFILEACTION_ALREADY_EXISTS);
|
---|
203 |
|
---|
204 | /* RTFILE_O_CREATE - non-existing: */
|
---|
205 | tstOpenExTest(__LINE__, -1, -1, g_szTestFile, RTFILE_O_CREATE, VINF_SUCCESS, RTFILEACTION_CREATED);
|
---|
206 |
|
---|
207 | /*
|
---|
208 | * RTFILE_O_CREATE and RTFILE_O_CREATE_REPLACE w/ TRUNCATE variations.
|
---|
209 | */
|
---|
210 | /* RTFILE_O_CREATE+TRUNCATE - non-existing: */
|
---|
211 | tstOpenExTest(__LINE__, -1, 10, g_szTestFile, RTFILE_O_CREATE | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_CREATED);
|
---|
212 |
|
---|
213 | /* RTFILE_O_CREATE+TRUNCATE - existing: */
|
---|
214 | tstOpenExTest(__LINE__, 10, 10, g_szTestFile, RTFILE_O_CREATE | RTFILE_O_TRUNCATE, VERR_ALREADY_EXISTS, RTFILEACTION_ALREADY_EXISTS);
|
---|
215 |
|
---|
216 | /* RTFILE_O_CREATE_REPLACE+TRUNCATE - existing: */
|
---|
217 | tstOpenExTest(__LINE__, 10, -1, g_szTestFile, RTFILE_O_CREATE_REPLACE | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_REPLACED);
|
---|
218 |
|
---|
219 | /* RTFILE_O_CREATE_REPLACE+TRUNCATE - non-existing: */
|
---|
220 | tstOpenExTest(__LINE__, -1, -1, g_szTestFile, RTFILE_O_CREATE_REPLACE | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_CREATED);
|
---|
221 |
|
---|
222 | RTTESTI_CHECK_RC(RTFileDelete(g_szTestFile), VINF_SUCCESS);
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | int main()
|
---|
227 | {
|
---|
228 | RTTEST hTest;
|
---|
229 | int rc = RTTestInitAndCreate("tstRTFileOpenEx-1", &hTest);
|
---|
230 | if (rc)
|
---|
231 | return rc;
|
---|
232 | RTTestBanner(hTest);
|
---|
233 | tstFileActionTaken(hTest);
|
---|
234 | RTFileDelete(g_szTestFile);
|
---|
235 | return RTTestSummaryAndDestroy(hTest);
|
---|
236 | }
|
---|
237 |
|
---|