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