summaryrefslogtreecommitdiffstats
path: root/tests/unit-utils-io.c
blob: 642f77849d64c2f3e3da4f1b5237b5ff92ded7e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * simple unit test for utils_io.c (blockwise low level functions)
 *
 * Copyright (C) 2018-2024 Red Hat, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "utils_io.h"

enum fn_enum {
	READ_BUFFER = 0,
	WRITE_BUFFER,
	READ_BLOCKWISE,
	WRITE_BLOCKWISE,
	READ_LSEEK_BLOCKWISE,
	WRITE_LSEEK_BLOCKWISE
} test_fn;

char		*test_file;
size_t		test_bsize;
size_t		test_alignment;
size_t		test_length;
off_t		test_offset; //FIXME: check for proper 64bit support (and test it!)
size_t		test_mem_alignment = 4096;

static int test_read_buffer(void)
{
	void *buffer = NULL;
	int fd = -1;
	ssize_t ret = -EINVAL;

	//printf("Entering test_read_buffer\n");

	if (posix_memalign(&buffer, test_mem_alignment, test_length)) {
		fprintf(stderr, "Failed to allocate aligned buffer.\n");
		goto out;
	}

	fd = open(test_file, O_RDONLY | O_DIRECT);
	if (fd < 0) {
		fprintf(stderr, "Failed to open %s.\n", test_file);
		goto out;
	}

	ret = read_buffer(fd, buffer, test_length);
	if (ret < 0)
		goto out;

	ret = (size_t) ret == test_length ? 0 : -EIO;
out:
	if (fd >= 0)
		close(fd);
	free(buffer);
	return ret;
}

static int test_write_buffer(void)
{
	void *buffer = NULL;
	int fd = -1;
	ssize_t ret = -EINVAL;

	//printf("Entering test_write_buffer\n");

	if (posix_memalign(&buffer, test_mem_alignment, test_length)) {
		fprintf(stderr, "Failed to allocate aligned buffer.\n");
		goto out;
	}

	fd = open(test_file, O_WRONLY | O_DIRECT);
	if (fd < 0) {
		fprintf(stderr, "Failed to open %s.\n", test_file);
		goto out;
	}

	ret = write_buffer(fd, buffer, test_length);
	if (ret < 0)
		goto out;

	ret = (size_t) ret == test_length ? 0 : -EIO;
out:
	if (fd >= 0)
		close(fd);
	free(buffer);
	return ret;
}

static int test_read_blockwise(void)
{
	void *buffer = NULL;
	int fd = -1;
	ssize_t ret = -EINVAL;

	//printf("Entering test_read_blockwise ");
	//printf("test_bsize: %zu, test_length: %zu\n", test_bsize, test_length);

	if (posix_memalign(&buffer, test_mem_alignment, test_length)) {
		fprintf(stderr, "Failed to allocate aligned buffer.\n");
		goto out;
	}

	fd = open(test_file, O_RDONLY | O_DIRECT);
	if (fd < 0) {
		fprintf(stderr, "Failed to open %s.\n", test_file);
		goto out;
	}


	ret = read_blockwise(fd, test_bsize, test_mem_alignment, buffer, test_length);
	if (ret < 0)
		goto out;

	ret = (size_t) ret == test_length ? 0 : -EIO;
out:
	if (fd >= 0)
		close(fd);
	free(buffer);
	return ret;
}

static int test_write_blockwise(void)
{
	void *buffer = NULL;
	int fd = -1;
	ssize_t ret = -EINVAL;

	//printf("Entering test_write_blockwise\n");

	if (posix_memalign(&buffer, test_mem_alignment, test_length)) {
		fprintf(stderr, "Failed to allocate aligned buffer.\n");
		goto out;
	}

	fd = open(test_file, O_RDWR | O_DIRECT);
	if (fd < 0) {
		fprintf(stderr, "Failed to open %s.\n", test_file);
		goto out;
	}

	ret = write_blockwise(fd, test_bsize, test_mem_alignment, buffer, test_length);
	if (ret < 0)
		goto out;

	ret = (size_t) ret == test_length ? 0 : -EIO;
out:
	if (fd >= 0)
		close(fd);
	free(buffer);
	return ret;
}

static int test_read_lseek_blockwise(void)
{
	void *buffer = NULL;
	int fd = -1;
	ssize_t ret = -EINVAL;

	//printf("Entering test_read_lseek_blockwise\n");

	if (posix_memalign(&buffer, test_mem_alignment, test_length)) {
		fprintf(stderr, "Failed to allocate aligned buffer.\n");
		goto out;
	}

	fd = open(test_file, O_RDONLY | O_DIRECT);
	if (fd < 0) {
		fprintf(stderr, "Failed to open %s.\n", test_file);
		goto out;
	}

	ret = read_lseek_blockwise(fd, test_bsize, test_mem_alignment, buffer, test_length, test_offset);
	if (ret < 0)
		goto out;

	ret = (size_t) ret == test_length ? 0 : -EIO;
out:
	if (fd >= 0)
		close(fd);
	free(buffer);
	return ret;
}

static int test_write_lseek_blockwise(void)
{
	void *buffer = NULL;
	int fd = -1;
	ssize_t ret = -EINVAL;

	//printf("Entering test_write_lseek_blockwise\n");

	if (posix_memalign(&buffer, test_mem_alignment, test_length)) {
		fprintf(stderr, "Failed to allocate aligned buffer.\n");
		goto out;
	}

	fd = open(test_file, O_RDWR | O_DIRECT);
	if (fd < 0) {
		fprintf(stderr, "Failed to open %s.\n", test_file);
		goto out;
	}

	ret = write_lseek_blockwise(fd, test_bsize, test_mem_alignment, buffer, test_length, test_offset);
	if (ret < 0)
		goto out;

	ret = (size_t) ret == test_length ? 0 : -EIO;
out:
	if (fd >= 0)
		close(fd);
	free(buffer);
	return ret;
}

static void usage(void)
{
	fprintf(stderr, "Use:\tunit-utils-io file/device blockwise_fn length  [bsize] [offset].\n");
}

static int parse_input_params(int argc, char **argv)
{
	struct stat st;
	unsigned long offset;

	if (argc < 4) {
		usage();
		return 1;
	}

	if (stat(argv[1], &st)) {
		fprintf(stderr, "File/device %s is missing?\n", argv[1]);
		return 1;
	}
	test_file = argv[1];
	if (sscanf(argv[3], "%zu", &test_length) != 1)
		return 1;
	if (argc >= 5 && sscanf(argv[4], "%zu", &test_bsize) != 1)
		return 1;
	if (argc >= 6) {
		if (sscanf(argv[5], "%ld", &offset) != 1)
			return 1;
		test_offset = offset;
	}

	if (!strcmp(argv[2], "read_buffer"))
		test_fn = READ_BUFFER;
	else if (!strcmp(argv[2], "write_buffer"))
		test_fn = WRITE_BUFFER;
	else if (!strcmp(argv[2], "read_blockwise")) {
		if (argc < 5) {
			usage();
			return 1;
		}
		test_fn = READ_BLOCKWISE;
	} else if (!strcmp(argv[2], "write_blockwise")) {
		if (argc < 5) {
			usage();
			return 1;
		}
		test_fn = WRITE_BLOCKWISE;
	} else if (!strcmp(argv[2], "read_lseek_blockwise")) {
		if (argc < 6) {
			usage();
			return 1;
		}
		test_fn = READ_LSEEK_BLOCKWISE;
	} else if (!strcmp(argv[2], "write_lseek_blockwise")) {
		if (argc < 6) {
			usage();
			return 1;
		}
		test_fn = WRITE_LSEEK_BLOCKWISE;
	} else {
		usage();
		return 1;
	}

	/* printf("function '%s': length %zu", argv[2], test_length);
	if (argc >= 5)
		printf(", bsize %zu", test_bsize);
	if (argc >= 6)
		printf(", offset %llu", test_offset);
	printf("\n"); */

	return 0;
}

int main(int argc, char **argv)
{
	long ps;
	int r = EXIT_FAILURE;

#ifndef NO_CRYPTSETUP_PATH
	if (getenv("CRYPTSETUP_PATH")) {
		printf("Cannot run this test with CRYPTSETUP_PATH set.\n");
		exit(77);
	}
#endif
	if (parse_input_params(argc, argv))
		return r;

	ps = sysconf(_SC_PAGESIZE);
	if (ps > 0)
		test_mem_alignment = (size_t)ps;

	switch (test_fn) {
	case READ_BUFFER:
		r = test_read_buffer();
		break;
	case WRITE_BUFFER:
		r = test_write_buffer();
		break;
	case READ_BLOCKWISE:
		r = test_read_blockwise();
		break;
	case WRITE_BLOCKWISE:
		r = test_write_blockwise();
		break;
	case READ_LSEEK_BLOCKWISE:
		r = test_read_lseek_blockwise();
		break;
	case WRITE_LSEEK_BLOCKWISE:
		r = test_write_lseek_blockwise();
		break;
	default :
		fprintf(stderr, "Internal test error.\n");
		return r;
	}

	return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}