blob: da0d3e172d41ad3156b01c9dd1e4a7393e315be6 [file] [log] [blame]
Boyang Xuecd51b342017-05-27 10:48:28 +08001/*
2 * Copyright (c) 2017 Red Hat, Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Author: Boyang Xue <bxue@redhat.com>
18 */
19
20/*
21 * Functional test for splice(2): pipe to pipe
22 *
23 * This test case tests splice(2) from a pipe to another
24 */
25
26#define _GNU_SOURCE
27#include <fcntl.h>
28#include <stdlib.h>
29#include "tst_test.h"
30
31#define PIPE_MAX (64*1024)
32
33static char *str_len_data;
34static int num_len_data = PIPE_MAX;
35static char *arr_in, *arr_out;
36
37static struct tst_option options[] = {
38 {"l:", &str_len_data, "-l <num> Length of test data (in bytes)"},
39 {NULL, NULL, NULL},
40};
41
42static void setup(void)
43{
44 int i, pipe_max_unpriv, pipe_limit;
45
46 SAFE_FILE_SCANF("/proc/sys/fs/pipe-max-size", "%d", &pipe_max_unpriv);
47 pipe_limit = MIN(pipe_max_unpriv, num_len_data);
48 num_len_data = pipe_limit;
49
50 if (tst_parse_int(str_len_data, &num_len_data, 1, pipe_limit)) {
51 tst_brk(TBROK, "Invalid length of data: '%s', "
52 "valid value: [1, %d]", str_len_data, pipe_limit);
53 }
54 tst_res(TINFO, "splice size = %d", num_len_data);
55 arr_in = SAFE_MALLOC(num_len_data);
56 arr_out = SAFE_MALLOC(num_len_data);
57 for (i = 0; i < num_len_data; i++)
58 arr_in[i] = i & 0xff;
59}
60
61static void cleanup(void)
62{
63 free(arr_in);
64 free(arr_out);
65}
66
67static void pipe_pipe(void)
68{
69 int pp1[2], pp2[2], i, ret;
70
71 SAFE_PIPE(pp1);
72 SAFE_PIPE(pp2);
73 SAFE_WRITE(1, pp1[1], arr_in, num_len_data);
74 for (i = num_len_data; i > 0; i = i - ret) {
75 ret = splice(pp1[0], NULL, pp2[1], NULL, i, 0);
76 if (ret == -1) {
77 tst_res(TFAIL | TERRNO, "splice error");
78 goto exit;
79 }
80 SAFE_READ(1, pp2[0], arr_out + num_len_data - i, ret);
81 }
82
83 for (i = 0; i < num_len_data; i++) {
84 if (arr_in[i] != arr_out[i]) {
85 tst_res(TFAIL, "wrong data at %d: expected: %d, "
86 "actual: %d", i, arr_in[i], arr_out[i]);
87 goto exit;
88 }
89 }
90 tst_res(TPASS, "splice(2) from pipe to pipe run pass.");
91
92exit:
93 SAFE_CLOSE(pp1[1]);
94 SAFE_CLOSE(pp1[0]);
95 SAFE_CLOSE(pp2[1]);
96 SAFE_CLOSE(pp2[0]);
97}
98
99static struct tst_test test = {
100 .tid = "splice04",
101 .test_all = pipe_pipe,
102 .setup = setup,
103 .cleanup = cleanup,
104 .options = options,
105 .min_kver = "2.6.31"
106};