VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstFileModeStringToFlags.cpp@ 47762

Last change on this file since 47762 was 47762, checked in by vboxsync, 12 years ago

IPRT: Added RTFileModeToFlags* APIs + testcase.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.8 KB
Line 
1/* $Id: tstFileModeStringToFlags.cpp 47762 2013-08-15 13:04:51Z vboxsync $ */
2/** @file
3 * IPRT Testcase - File mode string to IPRT file mode flags.
4 */
5
6/*
7 * Copyright (C) 2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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* Header Files *
29*******************************************************************************/
30#include <iprt/file.h>
31#include <iprt/stream.h>
32#include <iprt/string.h>
33#include <iprt/test.h>
34
35
36int main()
37{
38 RTTEST hTest;
39 int rc = RTTestInitAndCreate("tstRTStrVersion", &hTest);
40 if (rc)
41 return rc;
42 RTTestBanner(hTest);
43
44 RTTestSub(hTest, "RTFileModeToFlags");
45 static struct
46 {
47 int iResult;
48 const char *pszMode;
49 uint64_t uMode;
50 } const aTests[] =
51 {
52 /* Invalid parameters. */
53 { VERR_INVALID_PARAMETER, "", 0 },
54 { VERR_INVALID_PARAMETER, "foo", 0 },
55 { VERR_INVALID_PARAMETER, "--", 0 },
56 { VERR_INVALID_PARAMETER, "++", 0 },
57 { VERR_INVALID_PARAMETER, "++", 0 },
58 /* Missing action. */
59 { VERR_INVALID_PARAMETER, "z", 0 },
60 /* Open for reading ("r"). */
61 { VINF_SUCCESS , "r", RTFILE_O_OPEN | RTFILE_O_READ },
62 { VINF_SUCCESS , "r+", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE },
63 { VINF_SUCCESS , "r+++", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE },
64 { VINF_SUCCESS , "+++r", RTFILE_O_OPEN | RTFILE_O_READ },
65 { VINF_SUCCESS , "r+t", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE },
66 { VINF_SUCCESS , "r+b", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE },
67 /* Open / append ("a"). */
68 { VINF_SUCCESS , "a", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_APPEND },
69 { VINF_SUCCESS , "a+", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE | RTFILE_O_APPEND },
70 { VINF_SUCCESS , "a+++", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE | RTFILE_O_APPEND },
71 { VINF_SUCCESS , "+++a", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_APPEND },
72 { VINF_SUCCESS , "a+t", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE | RTFILE_O_APPEND },
73 { VINF_SUCCESS , "a+b", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE | RTFILE_O_APPEND },
74 /* Create / open ("c"). */
75 { VINF_SUCCESS , "c", RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE },
76 { VINF_SUCCESS , "c+", RTFILE_O_OPEN_CREATE | RTFILE_O_READ | RTFILE_O_WRITE },
77 { VINF_SUCCESS , "c+++", RTFILE_O_OPEN_CREATE | RTFILE_O_READ | RTFILE_O_WRITE },
78 { VERR_INVALID_PARAMETER, "cr", 0 },
79 { VERR_INVALID_PARAMETER, "cr+", 0 },
80 /* Create / replace ("w"). */
81 { VINF_SUCCESS , "w", RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_TRUNCATE },
82 { VERR_INVALID_PARAMETER, "ww", 0 },
83 { VERR_INVALID_PARAMETER, "wc", 0 },
84 { VINF_SUCCESS , "wb", RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_TRUNCATE },
85 { VINF_SUCCESS , "wb+", RTFILE_O_CREATE_REPLACE | RTFILE_O_READ | RTFILE_O_WRITE | RTFILE_O_TRUNCATE },
86 { VINF_SUCCESS , "w+", RTFILE_O_CREATE_REPLACE | RTFILE_O_READ | RTFILE_O_WRITE | RTFILE_O_TRUNCATE },
87 { VINF_SUCCESS , "w++", RTFILE_O_CREATE_REPLACE | RTFILE_O_READ | RTFILE_O_WRITE | RTFILE_O_TRUNCATE },
88 /* Create only ("x"). */
89 { VINF_SUCCESS , "x", RTFILE_O_CREATE | RTFILE_O_WRITE },
90 { VERR_INVALID_PARAMETER, "xx", 0 },
91 { VERR_INVALID_PARAMETER, "xc", 0 },
92 { VINF_SUCCESS , "xb", RTFILE_O_CREATE | RTFILE_O_WRITE },
93 { VINF_SUCCESS , "xb+", RTFILE_O_CREATE | RTFILE_O_READ | RTFILE_O_WRITE },
94 { VINF_SUCCESS , "x+", RTFILE_O_CREATE | RTFILE_O_READ | RTFILE_O_WRITE },
95 { VINF_SUCCESS , "x++", RTFILE_O_CREATE | RTFILE_O_READ | RTFILE_O_WRITE }
96 };
97
98 for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTests); iTest++)
99 {
100 uint64_t uMode;
101 int iResult = RTFileModeToFlags(aTests[iTest].pszMode, &uMode);
102 if (iResult != aTests[iTest].iResult)
103 {
104 RTTestFailed(hTest, "#%u: mode string '%s', result is %Rrc, expected %Rrc",
105 iTest, aTests[iTest].pszMode, iResult, aTests[iTest].iResult);
106 break;
107 }
108
109 if ( RT_SUCCESS(iResult)
110 && uMode != aTests[iTest].uMode)
111 {
112 RTTestFailed(hTest, "#%u: mode string '%s', got 0x%x, expected 0x%x",
113 iTest, aTests[iTest].pszMode, uMode, aTests[iTest].uMode);
114 break;
115 }
116 }
117
118 RTTestSub(hTest, "RTFileModeToFlagsEx");
119 static struct
120 {
121 int iResult;
122 const char *pszDisposition;
123 const char *pszMode;
124 /** @todo pszSharing not used yet. */
125 uint64_t uMode;
126 } const aTestsEx[] =
127 {
128 /* Invalid parameters. */
129 { VERR_INVALID_PARAMETER, "", "", 0 },
130 { VERR_INVALID_PARAMETER, "foo", "", 0 },
131 { VERR_INVALID_PARAMETER, "--", "", 0 },
132 { VERR_INVALID_PARAMETER, "++", "", 0 },
133 { VERR_INVALID_PARAMETER, "++", "", 0 },
134 /* Missing action. */
135 { VERR_INVALID_PARAMETER, "z", "", 0 },
136 /* Open existing ("oe"). */
137 { VINF_SUCCESS , "oe", "r", RTFILE_O_OPEN | RTFILE_O_READ },
138 { VINF_SUCCESS , "oe", "w", RTFILE_O_OPEN | RTFILE_O_WRITE },
139 { VINF_SUCCESS , "oe", "rw", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE },
140 { VINF_SUCCESS , "oe", "rw+", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE },
141 { VINF_SUCCESS , "oe", "++r", RTFILE_O_OPEN | RTFILE_O_READ },
142 { VINF_SUCCESS , "oe", "r+t", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE },
143 { VINF_SUCCESS , "oe", "r+b", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE },
144 /* Open / create ("oc"). */
145 { VINF_SUCCESS , "oc", "r", RTFILE_O_OPEN_CREATE | RTFILE_O_READ },
146 { VINF_SUCCESS , "oc", "r+", RTFILE_O_OPEN_CREATE | RTFILE_O_READ | RTFILE_O_WRITE },
147 { VINF_SUCCESS , "oc", "r+++", RTFILE_O_OPEN_CREATE | RTFILE_O_READ | RTFILE_O_WRITE },
148 { VINF_SUCCESS , "oc", "+++r", RTFILE_O_OPEN_CREATE | RTFILE_O_READ },
149 { VINF_SUCCESS , "oc", "w+t", RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
150 { VINF_SUCCESS , "oc", "w+b", RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
151 { VINF_SUCCESS , "oc", "w+t", RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
152 { VINF_SUCCESS , "oc", "wr", RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
153 { VINF_SUCCESS , "oc", "rw", RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
154 /* Open and truncate ("ot"). */
155 { VINF_SUCCESS , "ot", "r", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_READ },
156 { VINF_SUCCESS , "ot", "r+", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_READ | RTFILE_O_WRITE },
157 { VINF_SUCCESS , "ot", "r+++", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_READ | RTFILE_O_WRITE },
158 { VINF_SUCCESS , "ot", "+++r", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_READ },
159 { VINF_SUCCESS , "ot", "w+t", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_WRITE | RTFILE_O_READ },
160 { VINF_SUCCESS , "ot", "w+b", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_WRITE | RTFILE_O_READ },
161 { VINF_SUCCESS , "ot", "w+t", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_WRITE | RTFILE_O_READ },
162 { VINF_SUCCESS , "ot", "wr", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_WRITE | RTFILE_O_READ },
163 { VINF_SUCCESS , "ot", "rw", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_WRITE | RTFILE_O_READ },
164 /* Create always ("ca"). */
165 { VINF_SUCCESS , "ca", "r", RTFILE_O_CREATE_REPLACE | RTFILE_O_READ },
166 { VINF_SUCCESS , "ca", "r+", RTFILE_O_CREATE_REPLACE | RTFILE_O_READ | RTFILE_O_WRITE },
167 { VINF_SUCCESS , "ca", "r+++", RTFILE_O_CREATE_REPLACE | RTFILE_O_READ | RTFILE_O_WRITE },
168 { VINF_SUCCESS , "ca", "+++r", RTFILE_O_CREATE_REPLACE | RTFILE_O_READ },
169 { VINF_SUCCESS , "ca", "w+t", RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_READ },
170 { VINF_SUCCESS , "ca", "w+b", RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_READ },
171 { VINF_SUCCESS , "ca", "w+t", RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_READ },
172 { VINF_SUCCESS , "ca", "wr", RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_READ },
173 { VINF_SUCCESS , "ca", "rw", RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_READ },
174 /* Create if not exist ("ce"). */
175 { VINF_SUCCESS , "ce", "r", RTFILE_O_CREATE | RTFILE_O_READ },
176 { VINF_SUCCESS , "ce", "r+", RTFILE_O_CREATE | RTFILE_O_READ | RTFILE_O_WRITE },
177 { VINF_SUCCESS , "ce", "r+++", RTFILE_O_CREATE | RTFILE_O_READ | RTFILE_O_WRITE },
178 { VINF_SUCCESS , "ce", "+++r", RTFILE_O_CREATE | RTFILE_O_READ },
179 { VINF_SUCCESS , "ce", "w+t", RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
180 { VINF_SUCCESS , "ce", "w+b", RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
181 { VINF_SUCCESS , "ce", "w+t", RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
182 { VINF_SUCCESS , "ce", "wr", RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
183 { VINF_SUCCESS , "ce", "rw", RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_READ }
184 };
185
186 for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTestsEx); iTest++)
187 {
188 uint64_t uMode;
189 int iResult = RTFileModeToFlagsEx(aTestsEx[iTest].pszMode, aTestsEx[iTest].pszDisposition,
190 NULL /* pszSharing */, &uMode);
191 if (iResult != aTestsEx[iTest].iResult)
192 {
193 RTTestFailed(hTest, "#%u: disp '%s', mode '%s', result is %Rrc, expected %Rrc",
194 iTest, aTestsEx[iTest].pszDisposition, aTestsEx[iTest].pszMode,
195 iResult, aTestsEx[iTest].iResult);
196 break;
197 }
198
199 if ( RT_SUCCESS(iResult)
200 && uMode != aTestsEx[iTest].uMode)
201 {
202 RTTestFailed(hTest, "#%u: disp '%s', mode '%s', got 0x%x, expected 0x%x",
203 iTest, aTestsEx[iTest].pszDisposition, aTestsEx[iTest].pszMode,
204 uMode, aTestsEx[iTest].uMode);
205 break;
206 }
207 }
208
209 /*
210 * Summary.
211 */
212 return RTTestSummaryAndDestroy(hTest);
213}
214
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette