VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTFileAppend-1.cpp@ 59675

Last change on this file since 59675 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 KB
Line 
1/* $Id: tstRTFileAppend-1.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
2/** @file
3 * IPRT Testcase - File Appending.
4 */
5
6/*
7 * Copyright (C) 2009-2015 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/string.h>
35#include <iprt/test.h>
36
37
38void tstFileAppend1(RTTEST hTest)
39{
40 /*
41 * Open it write only and do some appending.
42 * Checking that read fails and that the file position changes after the write.
43 */
44 RTTestSub(hTest, "Basic 1");
45 RTFileDelete("tstFileAppend-1.tst");
46 RTFILE hFile = NIL_RTFILE;
47 int rc = RTFileOpen(&hFile,
48 "tstFileAppend-1.tst",
49 RTFILE_O_WRITE
50 | RTFILE_O_APPEND
51 | RTFILE_O_OPEN_CREATE
52 | RTFILE_O_DENY_NONE
53 | (0644 << RTFILE_O_CREATE_MODE_SHIFT)
54 );
55 RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
56
57 uint64_t offActual = 42;
58 uint64_t off = 0;
59 RTTESTI_CHECK_RC(rc = RTFileSeek(hFile, off, RTFILE_SEEK_CURRENT, &offActual), VINF_SUCCESS);
60 RTTESTI_CHECK_MSG(offActual == 0 || RT_FAILURE(rc), ("offActual=%llu", offActual));
61
62 RTTESTI_CHECK_RC(RTFileWrite(hFile, "0123456789", 10, NULL), VINF_SUCCESS);
63
64 offActual = 99;
65 off = 0;
66 RTTESTI_CHECK_RC(rc = RTFileSeek(hFile, off, RTFILE_SEEK_CURRENT, &offActual), VINF_SUCCESS);
67 RTTESTI_CHECK_MSG(offActual == 10 || RT_FAILURE(rc), ("offActual=%llu", offActual));
68 RTTestIPrintf(RTTESTLVL_INFO, "off=%llu after first write\n", offActual);
69
70 size_t cb = 4;
71 char szBuf[256];
72 rc = RTFileRead(hFile, szBuf, 1, &cb);
73 RTTESTI_CHECK_MSG(rc == VERR_ACCESS_DENIED || rc == VERR_INVALID_HANDLE, ("rc=%Rrc\n", rc));
74
75 offActual = 999;
76 off = 5;
77 RTTESTI_CHECK_RC(rc = RTFileSeek(hFile, off, RTFILE_SEEK_BEGIN, &offActual), VINF_SUCCESS);
78 RTTESTI_CHECK_MSG(offActual == 5 || RT_FAILURE(rc), ("offActual=%llu", offActual));
79
80 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
81
82
83 /*
84 * Open it write only and do some more appending.
85 * Checking the initial position and that it changes after the write.
86 */
87 RTTestSub(hTest, "Basic 2");
88 rc = RTFileOpen(&hFile,
89 "tstFileAppend-1.tst",
90 RTFILE_O_WRITE
91 | RTFILE_O_APPEND
92 | RTFILE_O_OPEN
93 | RTFILE_O_DENY_NONE
94 );
95 RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
96
97 offActual = 99;
98 off = 0;
99 RTTESTI_CHECK_RC(rc = RTFileSeek(hFile, off, RTFILE_SEEK_CURRENT, &offActual), VINF_SUCCESS);
100 RTTESTI_CHECK_MSG(offActual == 0 || RT_FAILURE(rc), ("offActual=%llu", offActual));
101 RTTestIPrintf(RTTESTLVL_INFO, "off=%llu on 2nd open\n", offActual);
102
103 RTTESTI_CHECK_RC(rc = RTFileWrite(hFile, "abcdefghij", 10, &cb), VINF_SUCCESS);
104
105 offActual = 999;
106 off = 0;
107 RTTESTI_CHECK_RC(rc = RTFileSeek(hFile, off, RTFILE_SEEK_CURRENT, &offActual), VINF_SUCCESS);
108 RTTESTI_CHECK_MSG(offActual == 20 || RT_FAILURE(rc), ("offActual=%llu", offActual));
109 RTTestIPrintf(RTTESTLVL_INFO, "off=%llu after 2nd write\n", offActual);
110
111 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
112
113 /*
114 * Open it read/write.
115 * Check the initial position and read stuff. Then append some more and
116 * check the new position and see that read returns 0/EOF. Finally,
117 * do some seeking and read from a new position.
118 */
119 RTTestSub(hTest, "Basic 3");
120 rc = RTFileOpen(&hFile,
121 "tstFileAppend-1.tst",
122 RTFILE_O_READWRITE
123 | RTFILE_O_APPEND
124 | RTFILE_O_OPEN
125 | RTFILE_O_DENY_NONE
126 );
127 RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
128
129 offActual = 9;
130 off = 0;
131 RTTESTI_CHECK_RC(rc = RTFileSeek(hFile, off, RTFILE_SEEK_CURRENT, &offActual), VINF_SUCCESS);
132 RTTESTI_CHECK_MSG(offActual == 0 || RT_FAILURE(rc), ("offActual=%llu", offActual));
133 RTTestIPrintf(RTTESTLVL_INFO, "off=%llu on 3rd open\n", offActual);
134
135 cb = 99;
136 RTTESTI_CHECK_RC(rc = RTFileRead(hFile, szBuf, 10, &cb), VINF_SUCCESS);
137 RTTESTI_CHECK(RT_FAILURE(rc) || cb == 10);
138 RTTESTI_CHECK_MSG(RT_FAILURE(rc) || !memcmp(szBuf, "0123456789", 10), ("read the wrong stuff: %.10s - expected 0123456789\n", szBuf));
139
140 offActual = 999;
141 off = 0;
142 RTTESTI_CHECK_RC(rc = RTFileSeek(hFile, off, RTFILE_SEEK_CURRENT, &offActual), VINF_SUCCESS);
143 RTTESTI_CHECK_MSG(offActual == 10 || RT_FAILURE(rc), ("offActual=%llu", offActual));
144 RTTestIPrintf(RTTESTLVL_INFO, "off=%llu on 1st open\n", offActual);
145
146 RTTESTI_CHECK_RC(RTFileWrite(hFile, "klmnopqrst", 10, NULL), VINF_SUCCESS);
147
148 offActual = 9999;
149 off = 0;
150 RTTESTI_CHECK_RC(rc = RTFileSeek(hFile, off, RTFILE_SEEK_CURRENT, &offActual), VINF_SUCCESS);
151 RTTESTI_CHECK_MSG(offActual == 30 || RT_FAILURE(rc), ("offActual=%llu", offActual));
152 RTTestIPrintf(RTTESTLVL_INFO, "off=%llu after 3rd write\n", offActual);
153
154 RTTESTI_CHECK_RC(rc = RTFileRead(hFile, szBuf, 1, NULL), VERR_EOF);
155 cb = 99;
156 RTTESTI_CHECK_RC(rc = RTFileRead(hFile, szBuf, 1, &cb), VINF_SUCCESS);
157 RTTESTI_CHECK(cb == 0);
158
159
160 offActual = 99999;
161 off = 15;
162 RTTESTI_CHECK_RC(rc = RTFileSeek(hFile, off, RTFILE_SEEK_BEGIN, &offActual), VINF_SUCCESS);
163 RTTESTI_CHECK_MSG(offActual == 15 || RT_FAILURE(rc), ("offActual=%llu", offActual));
164 if (RT_SUCCESS(rc) && offActual == 15)
165 {
166 RTTESTI_CHECK_RC(rc = RTFileRead(hFile, szBuf, 10, NULL), VINF_SUCCESS);
167 RTTESTI_CHECK_MSG(RT_FAILURE(rc) || !memcmp(szBuf, "fghijklmno", 10), ("read the wrong stuff: %.10s - expected fghijklmno\n", szBuf));
168
169 offActual = 9999999;
170 off = 0;
171 RTTESTI_CHECK_RC(rc = RTFileSeek(hFile, off, RTFILE_SEEK_CURRENT, &offActual), VINF_SUCCESS);
172 RTTESTI_CHECK_MSG(offActual == 25 || RT_FAILURE(rc), ("offActual=%llu", offActual));
173 RTTestIPrintf(RTTESTLVL_INFO, "off=%llu after 2nd read\n", offActual);
174 }
175
176 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
177
178 /*
179 * Open it read only + append and check that we cannot write to it.
180 */
181 RTTestSub(hTest, "Basic 4");
182 rc = RTFileOpen(&hFile,
183 "tstFileAppend-1.tst",
184 RTFILE_O_READ
185 | RTFILE_O_APPEND
186 | RTFILE_O_OPEN
187 | RTFILE_O_DENY_NONE);
188 RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
189
190 rc = RTFileWrite(hFile, "pqrstuvwx", 10, &cb);
191 RTTESTI_CHECK_MSG(rc == VERR_ACCESS_DENIED || rc == VERR_INVALID_HANDLE, ("rc=%Rrc\n", rc));
192
193 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
194 RTTESTI_CHECK_RC(RTFileDelete("tstFileAppend-1.tst"), VINF_SUCCESS);
195}
196
197
198int main()
199{
200 RTTEST hTest;
201 int rc = RTTestInitAndCreate("tstRTFileAppend-1", &hTest);
202 if (rc)
203 return rc;
204 RTTestBanner(hTest);
205 tstFileAppend1(hTest);
206 RTFileDelete("tstFileAppend-1.tst");
207 return RTTestSummaryAndDestroy(hTest);
208}
209
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