Home | History | Annotate | Download | only in splice
      1 /*
      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 (at) 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 #include "lapi/splice.h"
     31 #include "splice.h"
     32 
     33 #define PIPE_MAX (64*1024)
     34 
     35 static char *str_len_data;
     36 static int num_len_data = PIPE_MAX;
     37 static char *arr_in, *arr_out;
     38 
     39 static struct tst_option options[] = {
     40 	{"l:", &str_len_data, "-l <num> Length of test data (in bytes)"},
     41 	{NULL, NULL, NULL},
     42 };
     43 
     44 static void setup(void)
     45 {
     46 	int i, pipe_limit;
     47 
     48 	pipe_limit = get_max_limit(num_len_data);
     49 	num_len_data = pipe_limit;
     50 
     51 	if (tst_parse_int(str_len_data, &num_len_data, 1, pipe_limit)) {
     52 		tst_brk(TBROK, "Invalid length of data: '%s', "
     53 			"valid value: [1, %d]", str_len_data, pipe_limit);
     54 	}
     55 	tst_res(TINFO, "splice size = %d", num_len_data);
     56 	arr_in = SAFE_MALLOC(num_len_data);
     57 	arr_out = SAFE_MALLOC(num_len_data);
     58 	for (i = 0; i < num_len_data; i++)
     59 		arr_in[i] = i & 0xff;
     60 }
     61 
     62 static void cleanup(void)
     63 {
     64 	free(arr_in);
     65 	free(arr_out);
     66 }
     67 
     68 static void pipe_pipe(void)
     69 {
     70 	int pp1[2], pp2[2], i, ret;
     71 
     72 	SAFE_PIPE(pp1);
     73 	SAFE_PIPE(pp2);
     74 	SAFE_WRITE(1, pp1[1], arr_in, num_len_data);
     75 	for (i = num_len_data; i > 0; i = i - ret) {
     76 		ret = splice(pp1[0], NULL, pp2[1], NULL, i, 0);
     77 		if (ret == -1) {
     78 			tst_res(TFAIL | TERRNO, "splice error");
     79 			goto exit;
     80 		}
     81 		SAFE_READ(1, pp2[0], arr_out + num_len_data - i, ret);
     82 	}
     83 
     84 	for (i = 0; i < num_len_data; i++) {
     85 		if (arr_in[i] != arr_out[i]) {
     86 			tst_res(TFAIL, "wrong data at %d: expected: %d, "
     87 				"actual: %d", i, arr_in[i], arr_out[i]);
     88 			goto exit;
     89 		}
     90 	}
     91 	tst_res(TPASS, "splice(2) from pipe to pipe run pass.");
     92 
     93 exit:
     94 	SAFE_CLOSE(pp1[1]);
     95 	SAFE_CLOSE(pp1[0]);
     96 	SAFE_CLOSE(pp2[1]);
     97 	SAFE_CLOSE(pp2[0]);
     98 }
     99 
    100 static struct tst_test test = {
    101 	.test_all = pipe_pipe,
    102 	.setup = setup,
    103 	.cleanup = cleanup,
    104 	.options = options,
    105 	.min_kver = "2.6.31"
    106 };
    107