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