1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) 2019 SUSE LLC
4 * Author: Jorik Cronenberg <jcronenberg@suse.de>
5 *
6 * Test vmsplice() from a pipe into user memory
7 */
8
9#define _GNU_SOURCE
10
11#include "tst_test.h"
12#include "lapi/fcntl.h"
13#include "lapi/vmsplice.h"
14
15
16#define TEST_BLOCK_SIZE (64*1024)	/* 64K */
17
18static char buffer[TEST_BLOCK_SIZE];
19static struct iovec *iov;
20
21static void vmsplice_test(void)
22{
23	int written, i;
24	int pipes[2];
25	char *arr_write = iov->iov_base;
26
27	memset(iov->iov_base, 0, iov->iov_len);
28
29	SAFE_PIPE(pipes);
30	SAFE_WRITE(SAFE_WRITE_ALL, pipes[1], buffer, TEST_BLOCK_SIZE);
31	written = vmsplice(pipes[0], iov, 1, 0);
32
33	if (written < 0)
34		tst_brk(TBROK | TERRNO, "vmsplice() failed");
35
36	if (written == 0) {
37		tst_res(TFAIL, "vmsplice() didn't write anything");
38	} else {
39		for (i = 0; i < TEST_BLOCK_SIZE; i++) {
40			if (arr_write[i] != buffer[i]) {
41				tst_res(TFAIL,
42					"Wrong data in user memory at %i", i);
43				break;
44			}
45		}
46		if (i == written)
47			tst_res(TPASS, "Spliced correctly into user memory");
48	}
49
50	SAFE_CLOSE(pipes[1]);
51	SAFE_CLOSE(pipes[0]);
52}
53
54static void setup(void)
55{
56	int i;
57
58	for (i = 0; i < TEST_BLOCK_SIZE; i++)
59		buffer[i] = i & 0xff;
60}
61
62static struct tst_test test = {
63	.setup = setup,
64	.test_all = vmsplice_test,
65	.bufs = (struct tst_buffers []) {
66		{&iov, .iov_sizes = (int[]){TEST_BLOCK_SIZE, -1}},
67		{}
68	}
69};
70