2 * simple unit test for utils_io.c (blockwise low level functions)
4 * Copyright (C) 2018-2020 Red Hat, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/types.h>
43 size_t test_alignment;
45 off_t test_offset; //FIXME: check for proper 64bit support (and test it!)
46 size_t test_mem_alignment = 4096;
48 static int test_read_buffer(void)
52 ssize_t ret = -EINVAL;
54 //printf("Entering test_read_buffer\n");
56 if (posix_memalign(&buffer, test_mem_alignment, test_length)) {
57 fprintf(stderr, "Failed to allocate aligned buffer.\n");
61 fd = open(test_file, O_RDONLY | O_DIRECT);
63 fprintf(stderr, "Failed to open %s.\n", test_file);
67 ret = read_buffer(fd, buffer, test_length);
71 ret = (size_t) ret == test_length ? 0 : -EIO;
79 static int test_write_buffer(void)
83 ssize_t ret = -EINVAL;
85 //printf("Entering test_write_buffer\n");
87 if (posix_memalign(&buffer, test_mem_alignment, test_length)) {
88 fprintf(stderr, "Failed to allocate aligned buffer.\n");
92 fd = open(test_file, O_WRONLY | O_DIRECT);
94 fprintf(stderr, "Failed to open %s.\n", test_file);
98 ret = write_buffer(fd, buffer, test_length);
102 return (size_t) ret == test_length ? 0 : -EIO;
110 static int test_read_blockwise(void)
114 ssize_t ret = -EINVAL;
116 //printf("Entering test_read_blockwise ");
117 //printf("test_bsize: %zu, test_length: %zu\n", test_bsize, test_length);
119 if (posix_memalign(&buffer, test_mem_alignment, test_length)) {
120 fprintf(stderr, "Failed to allocate aligned buffer.\n");
124 fd = open(test_file, O_RDONLY | O_DIRECT);
126 fprintf(stderr, "Failed to open %s.\n", test_file);
131 ret = read_blockwise(fd, test_bsize, test_mem_alignment, buffer, test_length);
135 ret = (size_t) ret == test_length ? 0 : -EIO;
143 static int test_write_blockwise(void)
147 ssize_t ret = -EINVAL;
149 //printf("Entering test_write_blockwise\n");
151 if (posix_memalign(&buffer, test_mem_alignment, test_length)) {
152 fprintf(stderr, "Failed to allocate aligned buffer.\n");
156 fd = open(test_file, O_RDWR | O_DIRECT);
158 fprintf(stderr, "Failed to open %s.\n", test_file);
162 ret = write_blockwise(fd, test_bsize, test_mem_alignment, buffer, test_length);
166 ret = (size_t) ret == test_length ? 0 : -EIO;
174 static int test_read_lseek_blockwise(void)
178 ssize_t ret = -EINVAL;
180 //printf("Entering test_read_lseek_blockwise\n");
182 if (posix_memalign(&buffer, test_mem_alignment, test_length)) {
183 fprintf(stderr, "Failed to allocate aligned buffer.\n");
187 fd = open(test_file, O_RDONLY | O_DIRECT);
189 fprintf(stderr, "Failed to open %s.\n", test_file);
193 ret = read_lseek_blockwise(fd, test_bsize, test_mem_alignment, buffer, test_length, test_offset);
197 ret = (size_t) ret == test_length ? 0 : -EIO;
205 static int test_write_lseek_blockwise(void)
209 ssize_t ret = -EINVAL;
211 //printf("Entering test_write_lseek_blockwise\n");
213 if (posix_memalign(&buffer, test_mem_alignment, test_length)) {
214 fprintf(stderr, "Failed to allocate aligned buffer.\n");
218 fd = open(test_file, O_RDWR | O_DIRECT);
220 fprintf(stderr, "Failed to open %s.\n", test_file);
224 ret = write_lseek_blockwise(fd, test_bsize, test_mem_alignment, buffer, test_length, test_offset);
228 ret = (size_t) ret == test_length ? 0 : -EIO;
236 static void usage(void)
238 fprintf(stderr, "Use:\tunit-utils-io file/device blockwise_fn length [bsize] [offset].\n");
241 static int parse_input_params(int argc, char **argv)
244 unsigned long offset;
251 if (stat(argv[1], &st)) {
252 fprintf(stderr, "File/device %s is missing?\n", argv[1]);
256 if (sscanf(argv[3], "%zu", &test_length) != 1)
258 if (argc >= 5 && sscanf(argv[4], "%zu", &test_bsize) != 1)
261 if (sscanf(argv[5], "%ld", &offset) != 1)
263 test_offset = offset;
266 if (!strcmp(argv[2], "read_buffer"))
267 test_fn = READ_BUFFER;
268 else if (!strcmp(argv[2], "write_buffer"))
269 test_fn = WRITE_BUFFER;
270 else if (!strcmp(argv[2], "read_blockwise")) {
275 test_fn = READ_BLOCKWISE;
276 } else if (!strcmp(argv[2], "write_blockwise")) {
281 test_fn = WRITE_BLOCKWISE;
282 } else if (!strcmp(argv[2], "read_lseek_blockwise")) {
287 test_fn = READ_LSEEK_BLOCKWISE;
288 } else if (!strcmp(argv[2], "write_lseek_blockwise")) {
293 test_fn = WRITE_LSEEK_BLOCKWISE;
299 /* printf("function '%s': length %zu", argv[2], test_length);
301 printf(", bsize %zu", test_bsize);
303 printf(", offset %llu", test_offset);
309 int main(int argc, char **argv)
312 int r = EXIT_FAILURE;
314 if (parse_input_params(argc, argv))
317 ps = sysconf(_SC_PAGESIZE);
319 test_mem_alignment = (size_t)ps;
323 r = test_read_buffer();
326 r = test_write_buffer();
329 r = test_read_blockwise();
331 case WRITE_BLOCKWISE:
332 r = test_write_blockwise();
334 case READ_LSEEK_BLOCKWISE:
335 r = test_read_lseek_blockwise();
337 case WRITE_LSEEK_BLOCKWISE:
338 r = test_write_lseek_blockwise();
341 fprintf(stderr, "Internal test error.\n");
345 return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;