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