Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/io/file.cpp

raw
1 /*
2 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2025 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 6 мар. 2019 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/test-fw/utest.h>
23 #include <lsp-plug.in/test-fw/ByteBuffer.h>
24 #include <lsp-plug.in/common/status.h>
25 #include <lsp-plug.in/stdlib/string.h>
26 #include <lsp-plug.in/io/StdioFile.h>
27 #include <lsp-plug.in/io/NativeFile.h>
28 #include <lsp-plug.in/io/InFileStream.h>
29 #include <lsp-plug.in/io/InSequence.h>
30 #include <lsp-plug.in/io/OutFileStream.h>
31
32 using namespace lsp;
33 using namespace lsp::io;
34
35 UTEST_BEGIN("runtime.io", file)
36
37 void testClosedFile(const char *label, File &fd)
38 {
39 uint8_t tmpbuf[0x100];
40
41 printf("Testing %s...\n", label);
42
43 // Test for read failues
44 UTEST_ASSERT(fd.read(tmpbuf, sizeof(tmpbuf)) < 0);
45 UTEST_ASSERT(fd.pread(0, tmpbuf, sizeof(tmpbuf)) < 0);
46
47 // Test for write failures
48 UTEST_ASSERT(fd.write(tmpbuf, sizeof(tmpbuf) < 0));
49 UTEST_ASSERT(fd.pwrite(0, tmpbuf, sizeof(tmpbuf) < 0));
50 UTEST_ASSERT(fd.position() < 0);
51
52 // Test for supplementary failures
53 UTEST_ASSERT(fd.seek(0, File::FSK_SET) != STATUS_OK);
54 UTEST_ASSERT(fd.position() < 0);
55 UTEST_ASSERT(fd.flush() != STATUS_OK);
56 UTEST_ASSERT(fd.sync() != STATUS_OK);
57
58 // Test for close success
59 UTEST_ASSERT(fd.close() == STATUS_OK);
60 }
61
62 void testWriteonlyFile(File &fd)
63 {
64 uint32_t tmpbuf[0x100];
65
66 UTEST_ASSERT(fd.seek(0, SEEK_END) == STATUS_OK);
67 UTEST_ASSERT(fd.position() == 0);
68
69 // Write data to file
70 wssize_t written = 0;
71 for (size_t i=0; i<0x100; ++i)
72 {
73 for (size_t j=0; j<0x100; ++j)
74 tmpbuf[j] = uint32_t((i << 8) | j);
75
76 UTEST_ASSERT(fd.write(tmpbuf, sizeof(tmpbuf)) == sizeof(tmpbuf));
77 written += sizeof(tmpbuf);
78 }
79 UTEST_ASSERT(fd.flush() == STATUS_OK);
80
81 // Get status
82 io::fattr_t attr;
83 UTEST_ASSERT(fd.stat(&attr) == STATUS_OK);
84 UTEST_ASSERT(attr.type == io::fattr_t::FT_REGULAR);
85 UTEST_ASSERT(attr.size == wsize_t(written));
86
87 // Obtain position and change it
88 UTEST_ASSERT(fd.position() == written);
89 wssize_t position = 0x1000;
90 UTEST_ASSERT(fd.seek(position, SEEK_SET) == STATUS_OK);
91 UTEST_ASSERT(fd.position() == position);
92
93 // Ensure that read gives failures
94 UTEST_ASSERT(fd.read(tmpbuf, sizeof(tmpbuf)) < 0);
95 UTEST_ASSERT(fd.read(tmpbuf, 0) < 0);
96 UTEST_ASSERT(fd.pread(0x10000, tmpbuf, sizeof(tmpbuf)) < 0);
97 UTEST_ASSERT(fd.pread(0x10000, tmpbuf, 0) < 0);
98 UTEST_ASSERT(fd.position() == position);
99
100 // Ensure that positional write gives no failures
101 ::memset(tmpbuf, 0x55, sizeof(tmpbuf));
102 UTEST_ASSERT(fd.write(tmpbuf, sizeof(tmpbuf)) == sizeof(tmpbuf));
103 UTEST_ASSERT(fd.write(tmpbuf, 0) == 0);
104 position += sizeof(tmpbuf);
105 UTEST_ASSERT(fd.position() == position);
106 ::memset(tmpbuf, 0xaa, sizeof(tmpbuf));
107 UTEST_ASSERT(fd.pwrite(written, tmpbuf, sizeof(tmpbuf)) == sizeof(tmpbuf));
108 UTEST_ASSERT(fd.pwrite(written, tmpbuf, 0) == 0);
109 UTEST_ASSERT(fd.position() == position);
110
111 // Ensure that sync() and flush() work properly
112 UTEST_ASSERT(fd.flush() == STATUS_OK);
113 UTEST_ASSERT(fd.sync() == STATUS_OK);
114
115 // Try to truncate file
116 UTEST_ASSERT(fd.truncate(written) == STATUS_OK);
117 UTEST_ASSERT(fd.seek(0, SEEK_END) == STATUS_OK);
118 UTEST_ASSERT(fd.position() == written);
119 UTEST_ASSERT(fd.pwrite(written, tmpbuf, sizeof(tmpbuf)) == sizeof(tmpbuf));
120
121 // Test for close success
122 UTEST_ASSERT(fd.close() == STATUS_OK);
123 }
124
125 void testReadonlyFile(File &fd)
126 {
127 uint32_t tmpbuf[0x100], ckbuf[0x100];
128 UTEST_ASSERT(fd.position() == 0);
129
130 // Read data from file
131 wssize_t read = 0;
132 for (size_t i=0; i<0x100; ++i)
133 {
134 if (read != 0x1000)
135 {
136 for (size_t j=0; j<0x100; ++j)
137 ckbuf[j] = uint32_t((i << 8) | j);
138 }
139 else
140 ::memset(ckbuf, 0x55, sizeof(ckbuf));
141
142 UTEST_ASSERT(fd.read(tmpbuf, sizeof(tmpbuf)) == sizeof(tmpbuf));
143 UTEST_ASSERT(::memcmp(tmpbuf, ckbuf, sizeof(tmpbuf)) == 0);
144 read += sizeof(tmpbuf);
145 }
146
147 // Read the last chunk and check EOF detection
148 ::memset(ckbuf, 0xaa, sizeof(ckbuf));
149 UTEST_ASSERT(fd.read(tmpbuf, sizeof(tmpbuf)) == sizeof(tmpbuf));
150 UTEST_ASSERT(::memcmp(tmpbuf, ckbuf, sizeof(tmpbuf)) == 0);
151 read += sizeof(tmpbuf);
152 UTEST_ASSERT(fd.read(tmpbuf, sizeof(tmpbuf)) == (-STATUS_EOF));
153
154 // Get status
155 io::fattr_t attr;
156 UTEST_ASSERT(fd.stat(&attr) == STATUS_OK);
157 UTEST_ASSERT(attr.type == io::fattr_t::FT_REGULAR);
158 UTEST_ASSERT(attr.size == wsize_t(read));
159
160 // Obtain position and change it
161 UTEST_ASSERT(fd.position() == read);
162 wssize_t position = 0x1000;
163 UTEST_ASSERT(fd.seek(position, SEEK_SET) == STATUS_OK);
164 UTEST_ASSERT(fd.position() == position);
165
166 // Ensure that write gives failures
167 UTEST_ASSERT(fd.write(tmpbuf, sizeof(tmpbuf)) < 0);
168 UTEST_ASSERT(fd.write(tmpbuf, 0) < 0);
169 UTEST_ASSERT(fd.pwrite(0x10000, tmpbuf, sizeof(tmpbuf)) < 0);
170 UTEST_ASSERT(fd.pwrite(0x10000, tmpbuf, 0) < 0);
171 UTEST_ASSERT(fd.position() == position);
172
173 // Ensure that positional read gives no failures
174 ::memset(ckbuf, 0x55, sizeof(ckbuf));
175 UTEST_ASSERT(fd.read(tmpbuf, sizeof(tmpbuf)) == sizeof(tmpbuf));
176 UTEST_ASSERT(fd.read(tmpbuf, 0) == 0);
177 UTEST_ASSERT(::memcmp(tmpbuf, ckbuf, sizeof(tmpbuf)) == 0);
178 position += sizeof(tmpbuf);
179 UTEST_ASSERT(fd.position() == position);
180 ::memset(ckbuf, 0xaa, sizeof(ckbuf));
181 UTEST_ASSERT(fd.pread(read - sizeof(tmpbuf), tmpbuf, sizeof(tmpbuf)) == sizeof(tmpbuf));
182 UTEST_ASSERT(fd.pread(read - sizeof(tmpbuf), tmpbuf, 0) == 0);
183 UTEST_ASSERT(fd.position() == position);
184
185 // Ensure that sync() and flush() do not work
186 UTEST_ASSERT(fd.flush() != STATUS_OK);
187 UTEST_ASSERT(fd.sync() != STATUS_OK);
188
189 // Try to truncate file
190 UTEST_ASSERT(fd.truncate(0x1000) != STATUS_OK);
191 UTEST_ASSERT(fd.seek(0, SEEK_END) == STATUS_OK);
192 UTEST_ASSERT(fd.position() == read);
193
194 // Test for close success
195 UTEST_ASSERT(fd.close() == STATUS_OK);
196 }
197
198 template <class TemplateFile>
199 void testWriteonlyFileName(const char *label, const LSPString *path, TemplateFile &fd)
200 {
201 printf("Testing %s...\n", label);
202
203 // Open file with creation and truncation
204 UTEST_ASSERT(fd.open(path, File::FM_WRITE | File::FM_CREATE | File::FM_TRUNC) == STATUS_OK);
205 testWriteonlyFile(fd);
206 }
207
208 template <class TemplateFile>
209 void testReadonlyFileName(const char *label, const LSPString *path, TemplateFile &fd)
210 {
211 printf("Testing %s...\n", label);
212
213 // Open file
214 UTEST_ASSERT(fd.open(path, File::FM_READ) == STATUS_OK);
215 testReadonlyFile(fd);
216 }
217
218 void testWriteonlyDescriptor(const char *label, FILE *f, StdioFile &fd)
219 {
220 printf("Testing %s...\n", label);
221 UTEST_ASSERT(fd.wrap(f, File::FM_WRITE, false) == STATUS_OK);
222 testWriteonlyFile(fd);
223 }
224
225 void testReadonlyDescriptor(const char *label, FILE *f, StdioFile &fd)
226 {
227 printf("Testing %s...\n", label);
228 UTEST_ASSERT(fd.wrap(f, File::FM_READ, false) == STATUS_OK);
229 testReadonlyFile(fd);
230 }
231
232 template <class TemplateFile>
233 void testUnexistingFile(const char *label, TemplateFile &fd)
234 {
235 printf("Testing %s...\n", label);
236
237 LSPString path;
238 UTEST_ASSERT(path.fmt_utf8("%s" FILE_SEPARATOR_S "utest-nonexisting-%s.tmp", tempdir(), full_name()));
239
240 UTEST_ASSERT(fd.open(&path, File::FM_WRITE) != STATUS_OK);
241 UTEST_ASSERT(fd.close() == STATUS_OK);
242 }
243
244 void createFile(const io::Path *prefix, int num)
245 {
246 io::Path path;
247 const char *buf;
248 io::OutFileStream ofs;
249 LSPString postfix;
250
251 printf(" creating file: %d\n", num);
252
253 // Form file name
254 UTEST_ASSERT(postfix.fmt_ascii("rendel-%02d.tmp", num) > 0);
255 UTEST_ASSERT(path.set(prefix) == STATUS_OK);
256 UTEST_ASSERT(path.append(&postfix) == STATUS_OK);
257
258 // Write data to file and close
259 UTEST_ASSERT(ofs.open(&path, io::File::FM_WRITE_NEW) == STATUS_OK);
260 UTEST_ASSERT((buf = path.as_utf8()) != NULL);
261 UTEST_ASSERT(ofs.write(buf, strlen(buf)) > 0);
262 UTEST_ASSERT(ofs.close() == STATUS_OK);
263 }
264
265 void renameFile(const io::Path *prefix, int from, int to, int mode)
266 {
267 io::Path f, t;
268 LSPString postfix;
269 printf(" renaming file: %d -> %d\n", from, to);
270
271 // Form source file name
272 UTEST_ASSERT(postfix.fmt_ascii("rendel-%02d.tmp", from) > 0);
273 UTEST_ASSERT(f.set(prefix) == STATUS_OK);
274 UTEST_ASSERT(f.append(&postfix) == STATUS_OK);
275
276 // Form destination file name
277 UTEST_ASSERT(postfix.fmt_ascii("rendel-%02d.tmp", to) > 0);
278 UTEST_ASSERT(t.set(prefix) == STATUS_OK);
279 UTEST_ASSERT(t.append(&postfix) == STATUS_OK);
280
281 // Rename the file
282 status_t res = STATUS_NOT_IMPLEMENTED;
283 switch (mode)
284 {
285 case 0: res = io::File::rename(f.as_utf8(), t.as_utf8()); break;
286 case 1: res = io::File::rename(f.as_utf8(), t.as_string()); break;
287 case 2: res = io::File::rename(f.as_utf8(), &t); break;
288 case 3: res = io::File::rename(f.as_string(), t.as_utf8()); break;
289 case 4: res = io::File::rename(f.as_string(), t.as_string()); break;
290 case 5: res = io::File::rename(f.as_string(), &t); break;
291 case 6: res = io::File::rename(&f, t.as_utf8()); break;
292 case 7: res = io::File::rename(&f, t.as_string()); break;
293 case 8: res = io::File::rename(&f, &t); break;
294 default: break;
295 }
296
297 UTEST_ASSERT(res == STATUS_OK);
298 }
299
300 void checkFile(const io::Path *prefix, int from, int to)
301 {
302 io::Path f, t;
303 io::InFileStream ifs;
304 io::InSequence is;
305 LSPString postfix, payload;
306 printf(" checking file: unexists %d, exists %d\n", from, to);
307
308 // Form source file name
309 UTEST_ASSERT(postfix.fmt_ascii("rendel-%02d.tmp", from) > 0);
310 UTEST_ASSERT(f.set(prefix) == STATUS_OK);
311 UTEST_ASSERT(f.append(&postfix) == STATUS_OK);
312 UTEST_ASSERT(!f.exists());
313
314 // Form destination file name
315 UTEST_ASSERT(postfix.fmt_ascii("rendel-%02d.tmp", to) > 0);
316 UTEST_ASSERT(t.set(prefix) == STATUS_OK);
317 UTEST_ASSERT(t.append(&postfix) == STATUS_OK);
318
319 // Open file
320 UTEST_ASSERT(ifs.open(&t) == STATUS_OK);
321 UTEST_ASSERT(is.wrap(&ifs, WRAP_NONE, "UTF-8") == STATUS_OK);
322 UTEST_ASSERT(is.read_line(&payload, true) == STATUS_OK);
323 UTEST_ASSERT(is.close() == STATUS_OK);
324 UTEST_ASSERT(ifs.close() == STATUS_OK);
325
326 // Test the output
327 UTEST_ASSERT_MSG(payload.equals(f.as_string()),
328 "Payload: %s, expected: %s", payload.get_native(), f.as_native()
329 );
330 }
331
332 void testRenameDelete()
333 {
334 printf("Testing rename and delete...\n");
335
336 io::Path prefix;
337 UTEST_ASSERT(prefix.fmt("%s/utest-%s-rendel", tempdir(), full_name()));
338
339 // Create files
340 for (int i=0; i<18; ++i)
341 createFile(&prefix, i);
342
343 // Rename/move files
344 for (int i=0; i<18; i += 2)
345 renameFile(&prefix, i, i+1, i >> 1);
346
347 // Check file
348 for (int i=0; i<18; i += 2)
349 checkFile(&prefix, i, i+1);
350 }
351
352 void testCopy()
353 {
354 printf("Testing file copy...\n");
355
356 ssize_t res, nread;
357 io::Path dst, src;
358 io::NativeFile in, out;
359 UTEST_ASSERT(src.fmt("%s/utest-%s-copy-src.bin", tempdir(), full_name()) > 0);
360 UTEST_ASSERT(dst.fmt("%s/utest-%s-copy-dst.bin", tempdir(), full_name()) > 0);
361
362 // Generate source file
363 printf(" generating source file...\n");
364 ByteBuffer buf1(0x1000), buf2(0x1000);
365 UTEST_ASSERT(in.open(&src, io::File::FM_READWRITE_NEW) == STATUS_OK);
366 wssize_t written = 0;
367
368 for (size_t i=0; i<0x100; ++i)
369 {
370 buf1.randomize();
371 UTEST_ASSERT(in.write(buf1.data(), buf1.size()) == ssize_t(buf1.size()));
372 written += buf1.size();
373 }
374
375 UTEST_ASSERT(in.flush() == STATUS_OK);
376
377 // Copy the file
378 printf(" copying file %s to %s...\n", src.as_native(), dst.as_native());
379 wssize_t copied = io::File::copy(&src, &dst, 0x1234);
380 UTEST_ASSERT_MSG(copied == written,
381 "Expected to be written %ld bytes but written %ld bytes",
382 long(written), long(copied));
383
384 // Validate the file
385 printf(" verifying file contents...\n");
386 UTEST_ASSERT(in.seek(0, io::File::FSK_SET) == STATUS_OK);
387 UTEST_ASSERT(out.open(&dst, io::File::FM_READ) == STATUS_OK);
388
389 copied = 0;
390 while ((res = in.read(buf1.data(), buf1.size())) >= 0)
391 {
392 nread = out.read(buf2.data(), res);
393 UTEST_ASSERT(nread == res);
394 UTEST_ASSERT(buf1.equals(buf2.data(), res));
395
396 copied += nread;
397 }
398 UTEST_ASSERT(res == -STATUS_EOF);
399 UTEST_ASSERT(in.close() == STATUS_OK);
400 UTEST_ASSERT(out.close() == STATUS_OK);
401 UTEST_ASSERT(copied == written);
402
403 printf(" all is ok, %d bytes copied\n", written);
404 }
405
406 UTEST_MAIN
407 {
408 LSPString path;
409
410 File none_fd;
411 StdioFile std_fd;
412 NativeFile native_fd;
413
414 UTEST_ASSERT(path.fmt_utf8("%s" FILE_SEPARATOR_S "utest-%s.tmp", tempdir(), full_name()));
415
416 // Test closed files, all should fail the same way
417 testClosedFile("test_closed_file (abstract)", none_fd);
418 testClosedFile("test_closed_file (stdio)", std_fd);
419 testClosedFile("test_closed_file (native)", native_fd);
420
421 // Test stdio file
422 testWriteonlyFileName("test_writeonly_filename (stdio)", &path, std_fd);
423 testReadonlyFileName("test_readonly_filename (stdio)", &path, std_fd);
424 testUnexistingFile("test_unexsiting_file (stdio)", std_fd);
425
426 // Test stdio file as a wrapper
427 FILE *fd = fopen(path.get_native(), "wb+");
428 UTEST_ASSERT(fd != NULL);
429 testWriteonlyDescriptor("test_writeonly_descriptor (stdio)", fd, std_fd);
430 UTEST_ASSERT(fseek(fd, 0, SEEK_SET) == 0);
431 testReadonlyDescriptor("test_readonly_descriptor (stdio)", fd, std_fd);
432 UTEST_ASSERT(fclose(fd) == 0);
433
434 // Test native file
435 testWriteonlyFileName("test_writeonly_filename (native)", &path, native_fd);
436 testReadonlyFileName("test_readonly_filename (native)", &path, native_fd);
437 testUnexistingFile("test_unexsiting_file (native)", native_fd);
438
439 // Test rename and delete
440 testRenameDelete();
441
442 // Test file copy
443 testCopy();
444 }
445
446 UTEST_END
447