1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2017 Red Hat, Inc.
4f08c3bdfSopenharmony_ci * Author: Boyang Xue <bxue@redhat.com>
5f08c3bdfSopenharmony_ci *
6f08c3bdfSopenharmony_ci * Functional test for splice(2): pipe to pipe
7f08c3bdfSopenharmony_ci */
8f08c3bdfSopenharmony_ci
9f08c3bdfSopenharmony_ci#define _GNU_SOURCE
10f08c3bdfSopenharmony_ci#include <fcntl.h>
11f08c3bdfSopenharmony_ci#include <stdlib.h>
12f08c3bdfSopenharmony_ci#include "tst_test.h"
13f08c3bdfSopenharmony_ci#include "lapi/splice.h"
14f08c3bdfSopenharmony_ci#include "splice.h"
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_ci#define PIPE_MAX (64*1024)
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_cistatic char *str_len_data;
19f08c3bdfSopenharmony_cistatic int num_len_data = PIPE_MAX;
20f08c3bdfSopenharmony_cistatic char *arr_in, *arr_out;
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_cistatic void setup(void)
23f08c3bdfSopenharmony_ci{
24f08c3bdfSopenharmony_ci	int i, pipe_limit;
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci	pipe_limit = get_max_limit(num_len_data);
27f08c3bdfSopenharmony_ci	num_len_data = pipe_limit;
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_ci	if (tst_parse_int(str_len_data, &num_len_data, 1, pipe_limit)) {
30f08c3bdfSopenharmony_ci		tst_brk(TBROK, "Invalid length of data: '%s', "
31f08c3bdfSopenharmony_ci			"valid value: [1, %d]", str_len_data, pipe_limit);
32f08c3bdfSopenharmony_ci	}
33f08c3bdfSopenharmony_ci	tst_res(TINFO, "splice size = %d", num_len_data);
34f08c3bdfSopenharmony_ci	arr_in = SAFE_MALLOC(num_len_data);
35f08c3bdfSopenharmony_ci	arr_out = SAFE_MALLOC(num_len_data);
36f08c3bdfSopenharmony_ci	for (i = 0; i < num_len_data; i++)
37f08c3bdfSopenharmony_ci		arr_in[i] = i & 0xff;
38f08c3bdfSopenharmony_ci}
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_cistatic void cleanup(void)
41f08c3bdfSopenharmony_ci{
42f08c3bdfSopenharmony_ci	free(arr_in);
43f08c3bdfSopenharmony_ci	free(arr_out);
44f08c3bdfSopenharmony_ci}
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_cistatic void pipe_pipe(void)
47f08c3bdfSopenharmony_ci{
48f08c3bdfSopenharmony_ci	int pp1[2], pp2[2], i, ret;
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	SAFE_PIPE(pp1);
51f08c3bdfSopenharmony_ci	SAFE_PIPE(pp2);
52f08c3bdfSopenharmony_ci	SAFE_WRITE(SAFE_WRITE_ALL, pp1[1], arr_in, num_len_data);
53f08c3bdfSopenharmony_ci	for (i = num_len_data; i > 0; i = i - ret) {
54f08c3bdfSopenharmony_ci		ret = splice(pp1[0], NULL, pp2[1], NULL, i, 0);
55f08c3bdfSopenharmony_ci		if (ret == -1) {
56f08c3bdfSopenharmony_ci			tst_res(TFAIL | TERRNO, "splice error");
57f08c3bdfSopenharmony_ci			goto exit;
58f08c3bdfSopenharmony_ci		}
59f08c3bdfSopenharmony_ci		SAFE_READ(1, pp2[0], arr_out + num_len_data - i, ret);
60f08c3bdfSopenharmony_ci	}
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci	for (i = 0; i < num_len_data; i++) {
63f08c3bdfSopenharmony_ci		if (arr_in[i] != arr_out[i]) {
64f08c3bdfSopenharmony_ci			tst_res(TFAIL, "wrong data at %d: expected: %d, "
65f08c3bdfSopenharmony_ci				"actual: %d", i, arr_in[i], arr_out[i]);
66f08c3bdfSopenharmony_ci			goto exit;
67f08c3bdfSopenharmony_ci		}
68f08c3bdfSopenharmony_ci	}
69f08c3bdfSopenharmony_ci	tst_res(TPASS, "splice(2) from pipe to pipe run pass.");
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_ciexit:
72f08c3bdfSopenharmony_ci	SAFE_CLOSE(pp1[1]);
73f08c3bdfSopenharmony_ci	SAFE_CLOSE(pp1[0]);
74f08c3bdfSopenharmony_ci	SAFE_CLOSE(pp2[1]);
75f08c3bdfSopenharmony_ci	SAFE_CLOSE(pp2[0]);
76f08c3bdfSopenharmony_ci}
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_cistatic struct tst_test test = {
79f08c3bdfSopenharmony_ci	.test_all = pipe_pipe,
80f08c3bdfSopenharmony_ci	.setup = setup,
81f08c3bdfSopenharmony_ci	.cleanup = cleanup,
82f08c3bdfSopenharmony_ci	.options = (struct tst_option[]) {
83f08c3bdfSopenharmony_ci		{"l:", &str_len_data, "Length of test data (in bytes)"},
84f08c3bdfSopenharmony_ci		{}
85f08c3bdfSopenharmony_ci	},
86f08c3bdfSopenharmony_ci};
87