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