VirtualBox

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

Last change on this file since 53402 was 48828, checked in by vboxsync, 11 years ago

fixed tstRTFileModeStringToFlags

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.2 KB
Line 
1/* $Id: tstRTFileModeStringToFlags.cpp 48828 2013-10-02 19:19:48Z 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.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* 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_CREATE | RTFILE_O_WRITE | RTFILE_O_APPEND },
69 { VINF_SUCCESS , "a+", RTFILE_O_OPEN_CREATE | RTFILE_O_READ | RTFILE_O_WRITE | RTFILE_O_APPEND },
70 { VINF_SUCCESS , "a+++", RTFILE_O_OPEN_CREATE | RTFILE_O_READ | RTFILE_O_WRITE | RTFILE_O_APPEND },
71 { VINF_SUCCESS , "+++a", RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_APPEND },
72 { VINF_SUCCESS , "a+t", RTFILE_O_OPEN_CREATE | RTFILE_O_READ | RTFILE_O_WRITE | RTFILE_O_APPEND },
73 { VINF_SUCCESS , "a+b", RTFILE_O_OPEN_CREATE | 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 /** @todo Testing sharing modes are not implemented yet,
110 * so just remove them from testing. */
111 uMode &= ~RTFILE_O_DENY_NONE;
112
113 if ( RT_SUCCESS(iResult)
114 && uMode != aTests[iTest].uMode)
115 {
116 RTTestFailed(hTest, "#%u: mode string '%s', got 0x%x, expected 0x%x",
117 iTest, aTests[iTest].pszMode, uMode, aTests[iTest].uMode);
118 break;
119 }
120 }
121
122 RTTestSub(hTest, "RTFileModeToFlagsEx");
123 static struct
124 {
125 int iResult;
126 const char *pszDisposition;
127 const char *pszMode;
128 /** @todo pszSharing not used yet. */
129 uint64_t uMode;
130 } const aTestsEx[] =
131 {
132 /* Invalid parameters. */
133 { VERR_INVALID_PARAMETER, "", "", 0 },
134 { VERR_INVALID_PARAMETER, "foo", "", 0 },
135 { VERR_INVALID_PARAMETER, "--", "", 0 },
136 { VERR_INVALID_PARAMETER, "++", "", 0 },
137 { VERR_INVALID_PARAMETER, "++", "", 0 },
138 /* Missing action. */
139 { VERR_INVALID_PARAMETER, "z", "", 0 },
140 /* Open existing ("oe"). */
141 { VINF_SUCCESS , "oe", "r", RTFILE_O_OPEN | RTFILE_O_READ },
142 { VINF_SUCCESS , "oe", "w", RTFILE_O_OPEN | RTFILE_O_WRITE },
143 { VINF_SUCCESS , "oe", "rw", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE },
144 { VINF_SUCCESS , "oe", "rw+", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE },
145 { VINF_SUCCESS , "oe", "++r", RTFILE_O_OPEN | RTFILE_O_READ },
146 { VINF_SUCCESS , "oe", "r+t", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE },
147 { VINF_SUCCESS , "oe", "r+b", RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_WRITE },
148 /* Open / create ("oc"). */
149 { VINF_SUCCESS , "oc", "r", RTFILE_O_OPEN_CREATE | RTFILE_O_READ },
150 { VINF_SUCCESS , "oc", "r+", RTFILE_O_OPEN_CREATE | RTFILE_O_READ | RTFILE_O_WRITE },
151 { VINF_SUCCESS , "oc", "r+++", RTFILE_O_OPEN_CREATE | RTFILE_O_READ | RTFILE_O_WRITE },
152 { VINF_SUCCESS , "oc", "+++r", RTFILE_O_OPEN_CREATE | RTFILE_O_READ },
153 { VINF_SUCCESS , "oc", "w+t", RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
154 { VINF_SUCCESS , "oc", "w+b", RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
155 { VINF_SUCCESS , "oc", "w+t", RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
156 { VINF_SUCCESS , "oc", "wr", RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
157 { VINF_SUCCESS , "oc", "rw", RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
158 /* Open and truncate ("ot"). */
159 { VINF_SUCCESS , "ot", "r", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_READ },
160 { VINF_SUCCESS , "ot", "r+", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_READ | RTFILE_O_WRITE },
161 { VINF_SUCCESS , "ot", "r+++", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_READ | RTFILE_O_WRITE },
162 { VINF_SUCCESS , "ot", "+++r", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_READ },
163 { VINF_SUCCESS , "ot", "w+t", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_WRITE | RTFILE_O_READ },
164 { VINF_SUCCESS , "ot", "w+b", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_WRITE | RTFILE_O_READ },
165 { VINF_SUCCESS , "ot", "w+t", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_WRITE | RTFILE_O_READ },
166 { VINF_SUCCESS , "ot", "wr", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_WRITE | RTFILE_O_READ },
167 { VINF_SUCCESS , "ot", "rw", RTFILE_O_OPEN | RTFILE_O_TRUNCATE | RTFILE_O_WRITE | RTFILE_O_READ },
168 /* Create always ("ca"). */
169 { VINF_SUCCESS , "ca", "r", RTFILE_O_CREATE_REPLACE | RTFILE_O_READ },
170 { VINF_SUCCESS , "ca", "r+", RTFILE_O_CREATE_REPLACE | RTFILE_O_READ | RTFILE_O_WRITE },
171 { VINF_SUCCESS , "ca", "r+++", RTFILE_O_CREATE_REPLACE | RTFILE_O_READ | RTFILE_O_WRITE },
172 { VINF_SUCCESS , "ca", "+++r", RTFILE_O_CREATE_REPLACE | RTFILE_O_READ },
173 { VINF_SUCCESS , "ca", "w+t", RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_READ },
174 { VINF_SUCCESS , "ca", "w+b", RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_READ },
175 { VINF_SUCCESS , "ca", "w+t", RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_READ },
176 { VINF_SUCCESS , "ca", "wr", RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_READ },
177 { VINF_SUCCESS , "ca", "rw", RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_READ },
178 /* Create if not exist ("ce"). */
179 { VINF_SUCCESS , "ce", "r", RTFILE_O_CREATE | RTFILE_O_READ },
180 { VINF_SUCCESS , "ce", "r+", RTFILE_O_CREATE | RTFILE_O_READ | RTFILE_O_WRITE },
181 { VINF_SUCCESS , "ce", "r+++", RTFILE_O_CREATE | RTFILE_O_READ | RTFILE_O_WRITE },
182 { VINF_SUCCESS , "ce", "+++r", RTFILE_O_CREATE | RTFILE_O_READ },
183 { VINF_SUCCESS , "ce", "w+t", RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
184 { VINF_SUCCESS , "ce", "w+b", RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
185 { VINF_SUCCESS , "ce", "w+t", RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
186 { VINF_SUCCESS , "ce", "wr", RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_READ },
187 { VINF_SUCCESS , "ce", "rw", RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_READ }
188 };
189
190 for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTestsEx); iTest++)
191 {
192 uint64_t uMode;
193 int iResult = RTFileModeToFlagsEx(aTestsEx[iTest].pszMode, aTestsEx[iTest].pszDisposition,
194 NULL /* pszSharing */, &uMode);
195 if (iResult != aTestsEx[iTest].iResult)
196 {
197 RTTestFailed(hTest, "#%u: disp '%s', mode '%s', result is %Rrc, expected %Rrc",
198 iTest, aTestsEx[iTest].pszDisposition, aTestsEx[iTest].pszMode,
199 iResult, aTestsEx[iTest].iResult);
200 break;
201 }
202
203 /** @todo Testing sharing modes are not implemented yet,
204 * so just remove them from testing. */
205 uMode &= ~RTFILE_O_DENY_NONE;
206
207 if ( RT_SUCCESS(iResult)
208 && uMode != aTestsEx[iTest].uMode)
209 {
210 RTTestFailed(hTest, "#%u: disp '%s', mode '%s', got 0x%x, expected 0x%x",
211 iTest, aTestsEx[iTest].pszDisposition, aTestsEx[iTest].pszMode,
212 uMode, aTestsEx[iTest].uMode);
213 break;
214 }
215 }
216
217 /*
218 * Summary.
219 */
220 return RTTestSummaryAndDestroy(hTest);
221}
222
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