aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/xattr.c
blob: fe2c9ee9ad796d0a6086fe32ba836eeb46e2bd68 (plain) (blame)
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include "../config-host.h"
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/xattr.h>
#include <unistd.h>

#include "helpers.h"
#include "liburing.h"

static int no_xattr;

/* Define constants. */
#define XATTR_SIZE  255
#define QUEUE_DEPTH 32

#define FILENAME    "xattr.test"
#define KEY1        "user.val1"
#define KEY2        "user.val2"
#define VALUE1      "value1"
#define VALUE2      "value2-a-lot-longer"


/* Call fsetxattr. */
static int io_uring_fsetxattr(struct io_uring *ring, int fd, const char *name,
			      const void *value, size_t size, int flags)
{
	struct io_uring_sqe *sqe;
	struct io_uring_cqe *cqe;
	int ret;

	sqe = io_uring_get_sqe(ring);
	if (!sqe) {
		fprintf(stderr, "Error cannot get sqe\n");
		return -1;
	}

	io_uring_prep_fsetxattr(sqe, fd, name, value, flags, size);

	ret = io_uring_submit(ring);
	if (ret != 1) {
		fprintf(stderr, "Error io_uring_submit_and_wait: ret=%d\n", ret);
		return -1;
	}

	ret = io_uring_wait_cqe(ring, &cqe);
	if (ret) {
		fprintf(stderr, "Error io_uring_wait_cqe: ret=%d\n", ret);
		return -1;
	}

	ret = cqe->res;
	if (ret < 0) {
		if (cqe->res == -EINVAL || cqe->res == -EOPNOTSUPP)
			no_xattr = 1;
	}
	io_uring_cqe_seen(ring, cqe);
	return ret;
}

/* Submit fgetxattr request. */
static int io_uring_fgetxattr(struct io_uring *ring, int fd, const char *name,
			      void *value, size_t size)
{
	struct io_uring_sqe *sqe;
	struct io_uring_cqe *cqe;
	int ret;

	sqe = io_uring_get_sqe(ring);
	if (!sqe) {
		fprintf(stderr, "Error cannot get sqe\n");
		return -1;
	}

	io_uring_prep_fgetxattr(sqe, fd, name, value, size);

	ret = io_uring_submit(ring);
	if (ret != 1) {
		fprintf(stderr, "Error io_uring_submit_and_wait: ret=%d\n", ret);
		return -1;
	}

	ret = io_uring_wait_cqe(ring, &cqe);
	if (ret) {
		fprintf(stderr, "Error io_uring_wait_cqe: ret=%d\n", ret);
		return -1;
	}

	ret = cqe->res;
	if (ret == -1) {
		fprintf(stderr, "Error couldn'tget value\n");
		return -1;
	}

	io_uring_cqe_seen(ring, cqe);
	return ret;
}

/* Call setxattr. */
static int io_uring_setxattr(struct io_uring *ring, const char *path,
			     const char *name, const void *value, size_t size,
			     int flags)
{
	struct io_uring_sqe *sqe;
	struct io_uring_cqe *cqe;
	int ret;

	sqe = io_uring_get_sqe(ring);
	if (!sqe) {
		fprintf(stderr, "Error cannot get sqe\n");
		return -1;
	}

	io_uring_prep_setxattr(sqe, name, value, path, flags, size);

	ret = io_uring_submit_and_wait(ring, 1);
	if (ret != 1) {
		fprintf(stderr, "Error io_uring_submit_and_wait: ret=%d\n", ret);
		return -1;
	}

	ret = io_uring_wait_cqe(ring, &cqe);
	if (ret) {
		fprintf(stderr, "Error io_uring_wait_cqe: ret=%d\n", ret);
		return -1;
	}

	ret = cqe->res;
	if (ret < 0) {
		if (ret == -EINVAL || ret == -EOPNOTSUPP)
			no_xattr = 1;
	}
	io_uring_cqe_seen(ring, cqe);
	return ret;
}

/* Submit getxattr request. */
static int io_uring_getxattr(struct io_uring *ring, const char *path,
			     const char *name, void *value, size_t size)
{
	struct io_uring_sqe *sqe;
	struct io_uring_cqe *cqe;
	int ret;

	sqe = io_uring_get_sqe(ring);
	if (!sqe) {
		fprintf(stderr, "Error cannot get sqe\n");
		return -1;
	}

	io_uring_prep_getxattr(sqe, name, value, path, size);

	ret = io_uring_submit(ring);
	if (ret != 1) {
		fprintf(stderr, "Error io_uring_submit_and_wait: ret=%d\n", ret);
		return -1;
	}

	ret = io_uring_wait_cqe(ring, &cqe);
	if (ret) {
		fprintf(stderr, "Error io_uring_wait_cqe: ret=%d\n", ret);
		return -1;
	}

	ret = cqe->res;
	if (ret == -1) {
		fprintf(stderr, "Error couldn'tget value\n");
		return -1;
	}

	io_uring_cqe_seen(ring, cqe);
	return ret;
}

/* Test driver for fsetxattr and fgetxattr. */
static int test_fxattr(void)
{
	int rc = 0;
	size_t value_len;
	struct io_uring ring;
	char value[XATTR_SIZE];

	/* Init io-uring queue. */
	int ret = io_uring_queue_init(QUEUE_DEPTH, &ring, 0);
	if (ret) {
		fprintf(stderr, "child: ring setup failed: %d\n", ret);
		return -1;
	}

	/* Create the test file. */
	int fd = open(FILENAME, O_CREAT | O_RDWR, 0644);
	if (fd < 0) {
		fprintf(stderr, "Error: cannot open file: ret=%d\n", fd);
		return -1;
	}

	/* Test writing attributes. */
	if (io_uring_fsetxattr(&ring, fd, KEY1, VALUE1, strlen(VALUE1), 0) < 0) {
		if (no_xattr) {
			fprintf(stdout, "No xattr support, skipping\n");
			goto Exit;
		}
		fprintf(stderr, "Error fsetxattr cannot write key1\n");
		rc = -1;
		goto Exit;
	}

	if (io_uring_fsetxattr(&ring, fd, KEY2, VALUE2, strlen(VALUE2), 0) < 0) {
		fprintf(stderr, "Error fsetxattr cannot write key1\n");
		rc = -1;
		goto Exit;
	}

	/* Test reading attributes. */
	value_len = io_uring_fgetxattr(&ring, fd, KEY1, value, XATTR_SIZE);
	if (value_len != strlen(VALUE1) || strncmp(value, VALUE1, value_len)) {
		fprintf(stderr, "Error: fgetxattr expected value: %s, returned value: %s\n", VALUE1, value);
		rc = -1;
		goto Exit;
	}

	value_len = io_uring_fgetxattr(&ring, fd, KEY2, value, XATTR_SIZE);
	if (value_len != strlen(VALUE2) || strncmp(value, VALUE2, value_len)) {
		fprintf(stderr, "Error: fgetxattr expected value: %s, returned value: %s\n", VALUE2, value);
		rc = -1;
		goto Exit;
	}

	/* Cleanup. */
Exit:
	close(fd);
	unlink(FILENAME);

	io_uring_queue_exit(&ring);

	return rc;
}

/* Test driver for setxattr and getxattr. */
static int test_xattr(void)
{
	int rc = 0;
	int value_len;
	struct io_uring ring;
	char value[XATTR_SIZE];

	/* Init io-uring queue. */
	int ret = io_uring_queue_init(QUEUE_DEPTH, &ring, 0);
	if (ret) {
		fprintf(stderr, "child: ring setup failed: %d\n", ret);
		return -1;
	}

	/* Create the test file. */
	t_create_file(FILENAME, 0);

	/* Test writing attributes. */
	if (io_uring_setxattr(&ring, FILENAME, KEY1, VALUE1, strlen(VALUE1), 0) < 0) {
		fprintf(stderr, "Error setxattr cannot write key1\n");
		rc = -1;
		goto Exit;
	}

	if (io_uring_setxattr(&ring, FILENAME, KEY2, VALUE2, strlen(VALUE2), 0) < 0) {
		fprintf(stderr, "Error setxattr cannot write key1\n");
		rc = -1;
		goto Exit;
	}

	/* Test reading attributes. */
	value_len = io_uring_getxattr(&ring, FILENAME, KEY1, value, XATTR_SIZE);
	if (value_len != strlen(VALUE1) || strncmp(value, VALUE1, value_len)) {
		fprintf(stderr, "Error: getxattr expected value: %s, returned value: %s\n", VALUE1, value);
		rc = -1;
		goto Exit;
	}

	value_len = io_uring_getxattr(&ring, FILENAME, KEY2, value, XATTR_SIZE);
	if (value_len != strlen(VALUE2) || strncmp(value, VALUE2, value_len)) {
		fprintf(stderr, "Error: getxattr expected value: %s, returned value: %s\n", VALUE2, value);
		rc = -1;
		goto Exit;
	}

	/* Cleanup. */
Exit:
	io_uring_queue_exit(&ring);
	unlink(FILENAME);

	return rc;
}

/* Test driver for failure cases of fsetxattr and fgetxattr. */
static int test_failure_fxattr(void)
{
	struct io_uring ring;
	char value[XATTR_SIZE];

	/* Init io-uring queue. */
	int ret = io_uring_queue_init(QUEUE_DEPTH, &ring, 0);
	if (ret) {
		fprintf(stderr, "child: ring setup failed: %d\n", ret);
		return -1;
	}

	/* Create the test file. */
	int fd = open(FILENAME, O_CREAT | O_RDWR, 0644);
	if (fd < 0) {
		fprintf(stderr, "Error: cannot open file: ret=%d\n", fd);
		return -1;
	}

	/* Test writing attributes. */
	if (io_uring_fsetxattr(&ring, -1, KEY1, VALUE1, strlen(VALUE1), 0) >= 0)
		return 1;
	if (io_uring_fsetxattr(&ring, fd, NULL, VALUE1, strlen(VALUE1), 0) >= 0)
		return 1;
	if (io_uring_fsetxattr(&ring, fd, KEY1, NULL,   strlen(VALUE1), 0) >= 0)
		return 1;
	if (io_uring_fsetxattr(&ring, fd, KEY1, VALUE1, 0, 0) != 0)
		return 1;
	if (io_uring_fsetxattr(&ring, fd, KEY1, VALUE1, -1, 0) >= 0)
		return 1;

	/* Test reading attributes. */
	if (io_uring_fgetxattr(&ring, -1, KEY1, value, XATTR_SIZE) >= 0)
		return 1;
	if (io_uring_fgetxattr(&ring, fd, NULL, value, XATTR_SIZE) >= 0)
		return 1;
	if (io_uring_fgetxattr(&ring, fd, KEY1, value, 0) != 0)
		return 1;

	/* Cleanup. */
	close(fd);
	unlink(FILENAME);
	io_uring_queue_exit(&ring);
	return 0;
}


/* Test driver for failure cases for setxattr and getxattr. */
static int test_failure_xattr(void)
{
	struct io_uring ring;
	char value[XATTR_SIZE];

	/* Init io-uring queue. */
	int ret = io_uring_queue_init(QUEUE_DEPTH, &ring, 0);
	if (ret) {
		fprintf(stderr, "child: ring setup failed: %d\n", ret);
		return -1;
	}

	/* Create the test file. */
	t_create_file(FILENAME, 0);

	/* Test writing attributes. */
	if (io_uring_setxattr(&ring, "complete garbage", KEY1, VALUE1, strlen(VALUE1), 0) >= 0)
		return 1;
	if (io_uring_setxattr(&ring, NULL,     KEY1, VALUE1, strlen(VALUE1), 0) >= 0)
		return 1;
	if (io_uring_setxattr(&ring, FILENAME, NULL, VALUE1, strlen(VALUE1), 0) >= 0)
		return 1;
	if (io_uring_setxattr(&ring, FILENAME, KEY1, NULL,   strlen(VALUE1), 0) >= 0)
		return 1;
	if (io_uring_setxattr(&ring, FILENAME, KEY1, VALUE1, 0, 0) != 0)
		return 1;

	/* Test reading attributes. */
	if (io_uring_getxattr(&ring, "complete garbage", KEY1, value, XATTR_SIZE) >= 0)
		return 1;
	if (io_uring_getxattr(&ring, NULL,     KEY1, value, XATTR_SIZE) >= 0)
		return 1;
	if (io_uring_getxattr(&ring, FILENAME, NULL, value, XATTR_SIZE) >= 0)
		return 1;
	if (io_uring_getxattr(&ring, FILENAME, KEY1, NULL,  XATTR_SIZE) != 0)
		return 1;
	if (io_uring_getxattr(&ring, FILENAME, KEY1, value, 0) != 0)
		return 1;

	/* Cleanup. */
	io_uring_queue_exit(&ring);
	unlink(FILENAME);
	return 0;
}

/* Test for invalid SQE, this will cause a segmentation fault if enabled. */
static int test_invalid_sqe(void)
{
#ifdef DESTRUCTIVE_TEST
	struct io_uring_sqe *sqe = NULL;
	struct io_uring_cqe *cqe = NULL;
	struct io_uring ring;

	/* Init io-uring queue. */
	int ret = io_uring_queue_init(QUEUE_DEPTH, &ring, 0);
	if (ret) {
		fprintf(stderr, "child: ring setup failed: %d\n", ret);
		return -1;
	}

	/* Pass invalid SQE. */
	io_uring_prep_setxattr(sqe, FILENAME, KEY1, VALUE1, strlen(VALUE1), 0);

	ret = io_uring_submit(&ring);
	if (ret != 1) {
		fprintf(stderr, "Error io_uring_submit_and_wait: ret=%d\n", ret);
		return -1;
	}

	ret = io_uring_wait_cqe(&ring, &cqe);
	if (ret) {
		fprintf(stderr, "Error io_uring_wait_cqe: ret=%d\n", ret);
		return -1;
	}

	ret = cqe->res;
	io_uring_cqe_seen(&ring, cqe);

	return ret;
#else
	return 0;
#endif
}

/* Test driver. */
int main(int argc, char *argv[])
{
	if (argc > 1)
		return 0;

	if (test_fxattr())
		return EXIT_FAILURE;
	if (no_xattr)
		return EXIT_SUCCESS;
	if (test_xattr() || test_failure_fxattr() || test_failure_xattr() ||
	    test_invalid_sqe())
		return EXIT_FAILURE;

	return EXIT_SUCCESS;
}