• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
setup(void)22 static void setup(void)
23 {
24 	int i, pipe_limit;
25 
26 	pipe_limit = get_max_limit(num_len_data);
27 	num_len_data = pipe_limit;
28 
29 	if (tst_parse_int(str_len_data, &num_len_data, 1, pipe_limit)) {
30 		tst_brk(TBROK, "Invalid length of data: '%s', "
31 			"valid value: [1, %d]", str_len_data, pipe_limit);
32 	}
33 	tst_res(TINFO, "splice size = %d", num_len_data);
34 	arr_in = SAFE_MALLOC(num_len_data);
35 	arr_out = SAFE_MALLOC(num_len_data);
36 	for (i = 0; i < num_len_data; i++)
37 		arr_in[i] = i & 0xff;
38 }
39 
cleanup(void)40 static void cleanup(void)
41 {
42 	free(arr_in);
43 	free(arr_out);
44 }
45 
pipe_pipe(void)46 static void pipe_pipe(void)
47 {
48 	int pp1[2], pp2[2], i, ret;
49 
50 	SAFE_PIPE(pp1);
51 	SAFE_PIPE(pp2);
52 	SAFE_WRITE(SAFE_WRITE_ALL, pp1[1], arr_in, num_len_data);
53 	for (i = num_len_data; i > 0; i = i - ret) {
54 		ret = splice(pp1[0], NULL, pp2[1], NULL, i, 0);
55 		if (ret == -1) {
56 			tst_res(TFAIL | TERRNO, "splice error");
57 			goto exit;
58 		}
59 		SAFE_READ(1, pp2[0], arr_out + num_len_data - i, ret);
60 	}
61 
62 	for (i = 0; i < num_len_data; i++) {
63 		if (arr_in[i] != arr_out[i]) {
64 			tst_res(TFAIL, "wrong data at %d: expected: %d, "
65 				"actual: %d", i, arr_in[i], arr_out[i]);
66 			goto exit;
67 		}
68 	}
69 	tst_res(TPASS, "splice(2) from pipe to pipe run pass.");
70 
71 exit:
72 	SAFE_CLOSE(pp1[1]);
73 	SAFE_CLOSE(pp1[0]);
74 	SAFE_CLOSE(pp2[1]);
75 	SAFE_CLOSE(pp2[0]);
76 }
77 
78 static struct tst_test test = {
79 	.test_all = pipe_pipe,
80 	.setup = setup,
81 	.cleanup = cleanup,
82 	.options = (struct tst_option[]) {
83 		{"l:", &str_len_data, "Length of test data (in bytes)"},
84 		{}
85 	},
86 };
87