VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstFileAppend-1.cpp@ 23359

Last change on this file since 23359 was 21737, checked in by vboxsync, 15 years ago

export to OSE

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.3 KB
Line 
1/* $Id: tstFileAppend-1.cpp 21737 2009-07-21 10:59:51Z vboxsync $ */
2/** @file
3 * IPRT Testcase - File Appending.
4 */
5
6/*
7 * Copyright (C) 2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <iprt/file.h>
36#include <iprt/err.h>
37#include <iprt/initterm.h>
38#include <iprt/stream.h>
39#include <iprt/string.h>
40
41
42/*******************************************************************************
43* Global Variables *
44*******************************************************************************/
45static int g_cErrors = 0;
46
47
48static int MyFailure(const char *pszFormat, ...)
49{
50 va_list va;
51
52 RTPrintf("tstFileAppend-1: FATAL: ");
53 va_start(va, pszFormat);
54 RTPrintfV(pszFormat, va);
55 va_end(va);
56 g_cErrors++;
57 return 1;
58}
59
60
61void MyError(const char *pszFormat, ...)
62{
63 va_list va;
64
65 RTPrintf("tstFileAppend-1: ERROR: ");
66 va_start(va, pszFormat);
67 RTPrintfV(pszFormat, va);
68 va_end(va);
69 g_cErrors++;
70}
71
72int main()
73{
74 int rc;
75 RTFILE File;
76 int64_t off;
77 uint64_t offActual;
78 size_t cb;
79 char szBuf[256];
80
81
82 RTPrintf("tstFileAppend-1: TESTING...\n");
83
84 RTR3Init();
85
86 /*
87 * Open it write only and do some appending.
88 * Checking that read fails and that the file position changes after the write.
89 */
90 RTFileDelete("tstFileAppend-1.tst");
91 rc = RTFileOpen(&File,
92 "tstFileAppend-1.tst",
93 RTFILE_O_OPEN_CREATE |
94 RTFILE_O_WRITE |
95 RTFILE_O_APPEND |
96 RTFILE_O_DENY_NONE |
97 (0644 << RTFILE_O_CREATE_MODE_SHIFT)
98 );
99 if (RT_FAILURE(rc))
100 return MyFailure("1st RTFileOpen: %Rrc\n", rc);
101
102 off = 0;
103 rc = RTFileSeek(File, off, RTFILE_SEEK_CURRENT, &offActual);
104 if (RT_FAILURE(rc))
105 MyError("1st RTFileSeek failed: %Rrc\n", rc);
106 else if (offActual != 0)
107 MyError("unexpected position on 1st open: %llu - expected 0\n", offActual);
108
109 rc = RTFileWrite(File, "0123456789", 10, &cb);
110 if (RT_FAILURE(rc))
111 MyError("1st write fail: %Rrc\n", rc);
112
113 off = 0;
114 rc = RTFileSeek(File, off, RTFILE_SEEK_CURRENT, &offActual);
115 if (RT_FAILURE(rc))
116 MyError("2nd RTFileSeek failed: %Rrc\n", rc);
117 else if (offActual != 10)
118 MyError("unexpected position after 1st write: %llu - expected 10\n", offActual);
119 else
120 RTPrintf("tstFileAppend-1: off=%llu after first write\n", offActual);
121
122 rc = RTFileRead(File, szBuf, 1, &cb);
123 if (RT_SUCCESS(rc))
124 MyError("read didn't fail! cb=%#lx\n", (long)cb);
125
126 off = 5;
127 rc = RTFileSeek(File, off, RTFILE_SEEK_BEGIN, &offActual);
128 if (RT_FAILURE(rc))
129 MyError("3rd RTFileSeek failed: %Rrc\n", rc);
130 else if (offActual != 5)
131 MyError("unexpected position after 3rd set file pointer: %llu - expected 5\n", offActual);
132
133 RTFileClose(File);
134
135
136 /*
137 * Open it write only and do some more appending.
138 * Checking the initial position and that it changes after the write.
139 */
140 rc = RTFileOpen(&File,
141 "tstFileAppend-1.tst",
142 RTFILE_O_OPEN |
143 RTFILE_O_WRITE |
144 RTFILE_O_APPEND |
145 RTFILE_O_DENY_NONE
146 );
147 if (RT_FAILURE(rc))
148 return MyFailure("2nd RTFileOpen: %Rrc\n", rc);
149
150 off = 0;
151 rc = RTFileSeek(File, off, RTFILE_SEEK_CURRENT, &offActual);
152 if (RT_FAILURE(rc))
153 MyError("4th RTFileSeek failed: %Rrc\n", rc);
154 else if (offActual != 0)
155 MyError("unexpected position on 2nd open: %llu - expected 0\n", offActual);
156 else
157 RTPrintf("tstFileAppend-1: off=%llu on 2nd open\n", offActual);
158
159 rc = RTFileWrite(File, "abcdefghij", 10, &cb);
160 if (RT_FAILURE(rc))
161 MyError("2nd write fail: %Rrc\n", rc);
162
163 off = 0;
164 rc = RTFileSeek(File, off, RTFILE_SEEK_CURRENT, &offActual);
165 if (RT_FAILURE(rc))
166 MyError("5th RTFileSeek failed: %Rrc\n", rc);
167 else if (offActual != 20)
168 MyError("unexpected position after 2nd write: %llu - expected 20\n", offActual);
169 else
170 RTPrintf("tstFileAppend-1: off=%llu after 2nd write\n", offActual);
171
172 RTFileClose(File);
173
174 /*
175 * Open it read/write.
176 * Check the initial position and read stuff. Then append some more and
177 * check the new position and see that read returns 0/EOF. Finally,
178 * do some seeking and read from a new position.
179 */
180 rc = RTFileOpen(&File,
181 "tstFileAppend-1.tst",
182 RTFILE_O_OPEN |
183 RTFILE_O_READWRITE |
184 RTFILE_O_APPEND |
185 RTFILE_O_DENY_NONE
186 );
187 if (RT_FAILURE(rc))
188 return MyFailure("3rd RTFileOpen: %Rrc\n", rc);
189
190 off = 0;
191 rc = RTFileSeek(File, off, RTFILE_SEEK_CURRENT, &offActual);
192 if (RT_FAILURE(rc))
193 MyError("6th RTFileSeek failed: %Rrc\n", rc);
194 else if (offActual != 0)
195 MyError("unexpected position on 3rd open: %llu - expected 0\n", offActual);
196 else
197 RTPrintf("tstFileAppend-1: off=%llu on 3rd open\n", offActual);
198
199 rc = RTFileRead(File, szBuf, 10, &cb);
200 if (RT_FAILURE(rc) || cb != 10)
201 MyError("1st RTFileRead failed: %Rrc\n", rc);
202 else if (memcmp(szBuf, "0123456789", 10))
203 MyError("read the wrong stuff: %.10s - expected 0123456789\n", szBuf);
204
205 off = 0;
206 rc = RTFileSeek(File, off, RTFILE_SEEK_CURRENT, &offActual);
207 if (RT_FAILURE(rc))
208 MyError("7th RTFileSeek failed: %Rrc\n", rc);
209 else if (offActual != 10)
210 MyError("unexpected position after 1st read: %llu - expected 10\n", offActual);
211 else
212 RTPrintf("tstFileAppend-1: off=%llu on 1st open\n", offActual);
213
214 rc = RTFileWrite(File, "klmnopqrst", 10, &cb);
215 if (RT_FAILURE(rc))
216 MyError("3rd write fail: %Rrc\n", rc);
217
218 off = 0;
219 rc = RTFileSeek(File, off, RTFILE_SEEK_CURRENT, &offActual);
220 if (RT_FAILURE(rc))
221 MyError("8th RTFileSeek failed: %Rrc\n", rc);
222 else if (offActual != 30)
223 MyError("unexpected position after 3rd write: %llu - expected 30\n", offActual);
224 else
225 RTPrintf("tstFileAppend-1: off=%llu after 3rd write\n", offActual);
226
227 rc = RTFileRead(File, szBuf, 1, &cb);
228 if (RT_SUCCESS(rc) && cb != 0)
229 MyError("read after write didn't fail! cb=%#lx\n", (long)cb);
230
231 off = 15;
232 rc = RTFileSeek(File, off, RTFILE_SEEK_BEGIN, &offActual);
233 if (RT_FAILURE(rc))
234 MyError("9th RTFileSeek failed: %Rrc\n", rc);
235 else if (offActual != 15)
236 MyError("unexpected position after 9th seek: %llu - expected 15\n", offActual);
237 else
238 {
239 rc = RTFileRead(File, szBuf, 10, &cb);
240 if (RT_FAILURE(rc) || cb != 10)
241 MyError("2nd RTFileRead failed: %Rrc\n", rc);
242 else if (memcmp(szBuf, "fghijklmno", 10))
243 MyError("read the wrong stuff: %.10s - expected fghijklmno\n", szBuf);
244
245 off = 0;
246 rc = RTFileSeek(File, off, RTFILE_SEEK_CURRENT, &offActual);
247 if (RT_FAILURE(rc))
248 MyError("10th RTFileSeek failed: %Rrc\n", rc);
249 else if (offActual != 25)
250 MyError("unexpected position after 2nd read: %llu - expected 25\n", offActual);
251 else
252 RTPrintf("tstFileAppend-1: off=%llu after 2nd read\n", offActual);
253 }
254
255 RTFileClose(File);
256
257 /*
258 * Open it read only + append and check that we cannot write to it.
259 */
260 rc = RTFileOpen(&File,
261 "tstFileAppend-1.tst",
262 RTFILE_O_OPEN |
263 RTFILE_O_READ |
264 RTFILE_O_APPEND |
265 RTFILE_O_DENY_NONE
266 );
267 if (RT_FAILURE(rc))
268 return MyFailure("4th RTFileOpen: %Rrc\n", rc);
269
270 rc = RTFileWrite(File, "pqrstuvwx", 10, &cb);
271 if (RT_SUCCESS(rc))
272 MyError("write didn't fail on read-only+append open: cb=%#lx\n", (long)cb);
273
274 RTFileClose(File);
275 RTFileDelete("tstFileAppend-1.tst");
276
277 if (g_cErrors)
278 RTPrintf("tstFileAppend-1: FAILED\n");
279 else
280 RTPrintf("tstFileAppend-1: SUCCESS\n");
281 return g_cErrors ? 1 : 0;
282}
283
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