VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTFileModeStringToFlags.cpp@ 102335

Last change on this file since 102335 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.7 KB
Line 
1/* $Id: tstRTFileModeStringToFlags.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT Testcase - File mode string to IPRT file mode flags.
4 */
5
6/*
7 * Copyright (C) 2013-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/errcore.h>
44#include <iprt/stream.h>
45#include <iprt/string.h>
46#include <iprt/test.h>
47
48
49int main()
50{
51 RTTEST hTest;
52 int rc = RTTestInitAndCreate("tstRTStrVersion", &hTest);
53 if (rc)
54 return rc;
55 RTTestBanner(hTest);
56
57 RTTestSub(hTest, "RTFileModeToFlags");
58 static struct
59 {
60 int iResult;
61 const char *pszMode;
62 uint64_t uMode;
63 } const aTests[] =
64 {
65 /* Invalid parameters. */
66 { VERR_INVALID_PARAMETER, "", 0 },
67 { VERR_INVALID_PARAMETER, "foo", 0 },
68 { VERR_INVALID_PARAMETER, "--", 0 },
69 { VERR_INVALID_PARAMETER, "++", 0 },
70 { VERR_INVALID_PARAMETER, "++", 0 },
71 /* Missing action. */
72 { VERR_INVALID_PARAMETER, "z", 0 },
73 /* Open for reading ("r"). */
74 { VINF_SUCCESS , "r", RTFILE_O_OPEN | RTFILE_O_READ },
75 { VINF_SUCCESS , "r+", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE },
76 { VINF_SUCCESS , "r+++", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE },
77 { VINF_SUCCESS , "+++r", RTFILE_O_OPEN | RTFILE_O_READ },
78 { VINF_SUCCESS , "r+t", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE },
79 { VINF_SUCCESS , "r+b", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE },
80 /* Open / append ("a"). */
81 { VINF_SUCCESS , "a", RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_APPEND },
82 { VINF_SUCCESS , "a+", RTFILE_O_OPEN_CREATE | RTFILE_O_READ | RTFILE_O_WRITE | RTFILE_O_APPEND },
83 { VINF_SUCCESS , "a+++", RTFILE_O_OPEN_CREATE | RTFILE_O_READ | RTFILE_O_WRITE | RTFILE_O_APPEND },
84 { VINF_SUCCESS , "+++a", RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_APPEND },
85 { VINF_SUCCESS , "a+t", RTFILE_O_OPEN_CREATE | RTFILE_O_READ | RTFILE_O_WRITE | RTFILE_O_APPEND },
86 { VINF_SUCCESS , "a+b", RTFILE_O_OPEN_CREATE | RTFILE_O_READ | RTFILE_O_WRITE | RTFILE_O_APPEND },
87 /* Create / open ("c"). */
88 { VINF_SUCCESS , "c", RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE },
89 { VINF_SUCCESS , "c+", RTFILE_O_OPEN_CREATE | RTFILE_O_READ | RTFILE_O_WRITE },
90 { VINF_SUCCESS , "c+++", RTFILE_O_OPEN_CREATE | RTFILE_O_READ | RTFILE_O_WRITE },
91 { VERR_INVALID_PARAMETER, "cr", 0 },
92 { VERR_INVALID_PARAMETER, "cr+", 0 },
93 /* Create / replace ("w"). */
94 { VINF_SUCCESS , "w", RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_TRUNCATE },
95 { VERR_INVALID_PARAMETER, "ww", 0 },
96 { VERR_INVALID_PARAMETER, "wc", 0 },
97 { VINF_SUCCESS , "wb", RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_TRUNCATE },
98 { VINF_SUCCESS , "wb+", RTFILE_O_CREATE_REPLACE | RTFILE_O_READ | RTFILE_O_WRITE | RTFILE_O_TRUNCATE },
99 { VINF_SUCCESS , "w+", RTFILE_O_CREATE_REPLACE | RTFILE_O_READ | RTFILE_O_WRITE | RTFILE_O_TRUNCATE },
100 { VINF_SUCCESS , "w++", RTFILE_O_CREATE_REPLACE | RTFILE_O_READ | RTFILE_O_WRITE | RTFILE_O_TRUNCATE },
101 /* Create only ("x"). */
102 { VINF_SUCCESS , "x", RTFILE_O_CREATE | RTFILE_O_WRITE },
103 { VERR_INVALID_PARAMETER, "xx", 0 },
104 { VERR_INVALID_PARAMETER, "xc", 0 },
105 { VINF_SUCCESS , "xb", RTFILE_O_CREATE | RTFILE_O_WRITE },
106 { VINF_SUCCESS , "xb+", RTFILE_O_CREATE | RTFILE_O_READ | RTFILE_O_WRITE },
107 { VINF_SUCCESS , "x+", RTFILE_O_CREATE | RTFILE_O_READ | RTFILE_O_WRITE },
108 { VINF_SUCCESS , "x++", RTFILE_O_CREATE | RTFILE_O_READ | RTFILE_O_WRITE }
109 };
110
111 for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTests); iTest++)
112 {
113 uint64_t uMode;
114 int iResult = RTFileModeToFlags(aTests[iTest].pszMode, &uMode);
115 if (iResult != aTests[iTest].iResult)
116 {
117 RTTestFailed(hTest, "#%u: mode string '%s', result is %Rrc, expected %Rrc",
118 iTest, aTests[iTest].pszMode, iResult, aTests[iTest].iResult);
119 break;
120 }
121
122 /** @todo Testing sharing modes are not implemented yet,
123 * so just remove them from testing. */
124 uMode &= ~RTFILE_O_DENY_NONE;
125
126 if ( RT_SUCCESS(iResult)
127 && uMode != aTests[iTest].uMode)
128 {
129 RTTestFailed(hTest, "#%u: mode string '%s', got 0x%x, expected 0x%x",
130 iTest, aTests[iTest].pszMode, uMode, aTests[iTest].uMode);
131 break;
132 }
133 }
134
135 RTTestSub(hTest, "RTFileModeToFlagsEx");
136 static struct
137 {
138 int iResult;
139 const char *pszDisposition;
140 const char *pszMode;
141 /** @todo pszSharing not used yet. */
142 uint64_t uMode;
143 } const aTestsEx[] =
144 {
145 /* Invalid parameters. */
146 { VERR_INVALID_PARAMETER, "", "", 0 },
147 { VERR_INVALID_PARAMETER, "foo", "", 0 },
148 { VERR_INVALID_PARAMETER, "--", "", 0 },
149 { VERR_INVALID_PARAMETER, "++", "", 0 },
150 { VERR_INVALID_PARAMETER, "++", "", 0 },
151 /* Missing action. */
152 { VERR_INVALID_PARAMETER, "z", "", 0 },
153 /* Open existing ("oe"). */
154 { VINF_SUCCESS , "oe", "r", RTFILE_O_OPEN | RTFILE_O_READ },
155 { VINF_SUCCESS , "oe", "w", RTFILE_O_OPEN | RTFILE_O_WRITE },
156 { VINF_SUCCESS , "oe", "rw", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE },
157 { VINF_SUCCESS , "oe", "rw+", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE },
158 { VINF_SUCCESS , "oe", "++r", RTFILE_O_OPEN | RTFILE_O_READ },
159 { VINF_SUCCESS , "oe", "r+t", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE },
160 { VINF_SUCCESS , "oe", "r+b", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE },
161 /* Open / create ("oc"). */
162 { VINF_SUCCESS , "oc", "r", RTFILE_O_OPEN_CREATE | RTFILE_O_READ },
163 { VINF_SUCCESS , "oc", "r+", RTFILE_O_OPEN_CREATE | RTFILE_O_READ | RTFILE_O_WRITE },
164 { VINF_SUCCESS , "oc", "r+++", RTFILE_O_OPEN_CREATE | RTFILE_O_READ | RTFILE_O_WRITE },
165 { VINF_SUCCESS , "oc", "+++r", RTFILE_O_OPEN_CREATE | RTFILE_O_READ },
166 { VINF_SUCCESS , "oc", "w+t", RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
167 { VINF_SUCCESS , "oc", "w+b", RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
168 { VINF_SUCCESS , "oc", "w+t", RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
169 { VINF_SUCCESS , "oc", "wr", RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
170 { VINF_SUCCESS , "oc", "rw", RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
171 /* Open and truncate ("ot"). */
172 { VINF_SUCCESS , "ot", "r", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_READ },
173 { VINF_SUCCESS , "ot", "r+", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_READ | RTFILE_O_WRITE },
174 { VINF_SUCCESS , "ot", "r+++", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_READ | RTFILE_O_WRITE },
175 { VINF_SUCCESS , "ot", "+++r", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_READ },
176 { VINF_SUCCESS , "ot", "w+t", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_WRITE | RTFILE_O_READ },
177 { VINF_SUCCESS , "ot", "w+b", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_WRITE | RTFILE_O_READ },
178 { VINF_SUCCESS , "ot", "w+t", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_WRITE | RTFILE_O_READ },
179 { VINF_SUCCESS , "ot", "wr", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_WRITE | RTFILE_O_READ },
180 { VINF_SUCCESS , "ot", "rw", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_WRITE | RTFILE_O_READ },
181 /* Create always ("ca"). */
182 { VINF_SUCCESS , "ca", "r", RTFILE_O_CREATE_REPLACE | RTFILE_O_READ },
183 { VINF_SUCCESS , "ca", "r+", RTFILE_O_CREATE_REPLACE | RTFILE_O_READ | RTFILE_O_WRITE },
184 { VINF_SUCCESS , "ca", "r+++", RTFILE_O_CREATE_REPLACE | RTFILE_O_READ | RTFILE_O_WRITE },
185 { VINF_SUCCESS , "ca", "+++r", RTFILE_O_CREATE_REPLACE | RTFILE_O_READ },
186 { VINF_SUCCESS , "ca", "w+t", RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_READ },
187 { VINF_SUCCESS , "ca", "w+b", RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_READ },
188 { VINF_SUCCESS , "ca", "w+t", RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_READ },
189 { VINF_SUCCESS , "ca", "wr", RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_READ },
190 { VINF_SUCCESS , "ca", "rw", RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_READ },
191 /* Create if not exist ("ce"). */
192 { VINF_SUCCESS , "ce", "r", RTFILE_O_CREATE | RTFILE_O_READ },
193 { VINF_SUCCESS , "ce", "r+", RTFILE_O_CREATE | RTFILE_O_READ | RTFILE_O_WRITE },
194 { VINF_SUCCESS , "ce", "r+++", RTFILE_O_CREATE | RTFILE_O_READ | RTFILE_O_WRITE },
195 { VINF_SUCCESS , "ce", "+++r", RTFILE_O_CREATE | RTFILE_O_READ },
196 { VINF_SUCCESS , "ce", "w+t", RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
197 { VINF_SUCCESS , "ce", "w+b", RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
198 { VINF_SUCCESS , "ce", "w+t", RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
199 { VINF_SUCCESS , "ce", "wr", RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
200 { VINF_SUCCESS , "ce", "rw", RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_READ }
201 };
202
203 for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTestsEx); iTest++)
204 {
205 uint64_t uMode;
206 int iResult = RTFileModeToFlagsEx(aTestsEx[iTest].pszMode, aTestsEx[iTest].pszDisposition,
207 NULL /* pszSharing */, &uMode);
208 if (iResult != aTestsEx[iTest].iResult)
209 {
210 RTTestFailed(hTest, "#%u: disp '%s', mode '%s', result is %Rrc, expected %Rrc",
211 iTest, aTestsEx[iTest].pszDisposition, aTestsEx[iTest].pszMode,
212 iResult, aTestsEx[iTest].iResult);
213 break;
214 }
215
216 /** @todo Testing sharing modes are not implemented yet,
217 * so just remove them from testing. */
218 uMode &= ~RTFILE_O_DENY_NONE;
219
220 if ( RT_SUCCESS(iResult)
221 && uMode != aTestsEx[iTest].uMode)
222 {
223 RTTestFailed(hTest, "#%u: disp '%s', mode '%s', got 0x%x, expected 0x%x",
224 iTest, aTestsEx[iTest].pszDisposition, aTestsEx[iTest].pszMode,
225 uMode, aTestsEx[iTest].uMode);
226 break;
227 }
228 }
229
230 /*
231 * Summary.
232 */
233 return RTTestSummaryAndDestroy(hTest);
234}
235
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