1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2017 Red Hat, Inc. 4f08c3bdfSopenharmony_ci * Author: Boyang Xue <bxue@redhat.com> 5f08c3bdfSopenharmony_ci */ 6f08c3bdfSopenharmony_ci 7f08c3bdfSopenharmony_ci/* 8f08c3bdfSopenharmony_ci * Functional test for splice(2): pipe <-> socket 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * This test case tests splice(2) from a pipe to a socket and vice versa 11f08c3bdfSopenharmony_ci */ 12f08c3bdfSopenharmony_ci 13f08c3bdfSopenharmony_ci#define _GNU_SOURCE 14f08c3bdfSopenharmony_ci#include <errno.h> 15f08c3bdfSopenharmony_ci#include <sys/types.h> 16f08c3bdfSopenharmony_ci#include <sys/socket.h> 17f08c3bdfSopenharmony_ci#include <sys/stat.h> 18f08c3bdfSopenharmony_ci#include <sys/wait.h> 19f08c3bdfSopenharmony_ci#include <fcntl.h> 20f08c3bdfSopenharmony_ci#include <stdlib.h> 21f08c3bdfSopenharmony_ci#include <unistd.h> 22f08c3bdfSopenharmony_ci#include "tst_test.h" 23f08c3bdfSopenharmony_ci#include "lapi/splice.h" 24f08c3bdfSopenharmony_ci#include "splice.h" 25f08c3bdfSopenharmony_ci 26f08c3bdfSopenharmony_ci#define PIPE_MAX (64*1024) 27f08c3bdfSopenharmony_ci 28f08c3bdfSopenharmony_cistatic char *str_len_data; 29f08c3bdfSopenharmony_cistatic int num_len_data = PIPE_MAX; 30f08c3bdfSopenharmony_cistatic char *arr_in, *arr_out; 31f08c3bdfSopenharmony_ci 32f08c3bdfSopenharmony_cistatic void setup(void) 33f08c3bdfSopenharmony_ci{ 34f08c3bdfSopenharmony_ci int i, pipe_limit; 35f08c3bdfSopenharmony_ci 36f08c3bdfSopenharmony_ci pipe_limit = get_max_limit(num_len_data); 37f08c3bdfSopenharmony_ci num_len_data = pipe_limit; 38f08c3bdfSopenharmony_ci 39f08c3bdfSopenharmony_ci if (tst_parse_int(str_len_data, &num_len_data, 1, pipe_limit)) { 40f08c3bdfSopenharmony_ci tst_brk(TBROK, "Invalid length of data: '%s', " 41f08c3bdfSopenharmony_ci "valid value: [1, %d]", str_len_data, pipe_limit); 42f08c3bdfSopenharmony_ci } 43f08c3bdfSopenharmony_ci tst_res(TINFO, "splice size = %d", num_len_data); 44f08c3bdfSopenharmony_ci arr_in = SAFE_MALLOC(num_len_data); 45f08c3bdfSopenharmony_ci arr_out = SAFE_MALLOC(num_len_data); 46f08c3bdfSopenharmony_ci for (i = 0; i < num_len_data; i++) 47f08c3bdfSopenharmony_ci arr_in[i] = i & 0xff; 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_ci} 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_cistatic void cleanup(void) 52f08c3bdfSopenharmony_ci{ 53f08c3bdfSopenharmony_ci free(arr_in); 54f08c3bdfSopenharmony_ci free(arr_out); 55f08c3bdfSopenharmony_ci} 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_cistatic void pipe_socket(void) 58f08c3bdfSopenharmony_ci{ 59f08c3bdfSopenharmony_ci int pp1[2], pp2[2], sv[2], i, ret; 60f08c3bdfSopenharmony_ci 61f08c3bdfSopenharmony_ci SAFE_PIPE(pp1); 62f08c3bdfSopenharmony_ci SAFE_PIPE(pp2); 63f08c3bdfSopenharmony_ci SAFE_SOCKETPAIR(AF_UNIX, SOCK_STREAM, 0, sv); 64f08c3bdfSopenharmony_ci 65f08c3bdfSopenharmony_ci SAFE_WRITE(SAFE_WRITE_ALL, pp1[1], arr_in, num_len_data); 66f08c3bdfSopenharmony_ci for (i = num_len_data; i > 0; i = i - ret) { 67f08c3bdfSopenharmony_ci ret = splice(pp1[0], NULL, sv[0], 0, i, 0); 68f08c3bdfSopenharmony_ci if (ret == -1) { 69f08c3bdfSopenharmony_ci tst_res(TFAIL | TERRNO, "splice error"); 70f08c3bdfSopenharmony_ci goto exit; 71f08c3bdfSopenharmony_ci } 72f08c3bdfSopenharmony_ci } 73f08c3bdfSopenharmony_ci for (i = num_len_data; i > 0; i = i - ret) { 74f08c3bdfSopenharmony_ci ret = splice(sv[1], 0, pp2[1], NULL, i, 0); 75f08c3bdfSopenharmony_ci if (ret == -1) { 76f08c3bdfSopenharmony_ci if (errno == EINVAL) { 77f08c3bdfSopenharmony_ci tst_res(TCONF, "splice does not support " 78f08c3bdfSopenharmony_ci "af_unix sockets"); 79f08c3bdfSopenharmony_ci } else { 80f08c3bdfSopenharmony_ci tst_res(TFAIL | TERRNO, "splice error"); 81f08c3bdfSopenharmony_ci } 82f08c3bdfSopenharmony_ci goto exit; 83f08c3bdfSopenharmony_ci } 84f08c3bdfSopenharmony_ci SAFE_READ(1, pp2[0], arr_out + num_len_data - i, ret); 85f08c3bdfSopenharmony_ci } 86f08c3bdfSopenharmony_ci 87f08c3bdfSopenharmony_ci for (i = 0; i < num_len_data; i++) { 88f08c3bdfSopenharmony_ci if (arr_in[i] != arr_out[i]) { 89f08c3bdfSopenharmony_ci tst_res(TFAIL, "wrong data at %d: expected: %d, " 90f08c3bdfSopenharmony_ci "actual: %d", i, arr_in[i], arr_out[i]); 91f08c3bdfSopenharmony_ci goto exit; 92f08c3bdfSopenharmony_ci } 93f08c3bdfSopenharmony_ci } 94f08c3bdfSopenharmony_ci tst_res(TPASS, "splice(2): pipe <-> socket run pass."); 95f08c3bdfSopenharmony_ciexit: 96f08c3bdfSopenharmony_ci for (i = 0; i < 2; i++) { 97f08c3bdfSopenharmony_ci SAFE_CLOSE(pp1[i]); 98f08c3bdfSopenharmony_ci SAFE_CLOSE(pp2[i]); 99f08c3bdfSopenharmony_ci SAFE_CLOSE(sv[i]); 100f08c3bdfSopenharmony_ci } 101f08c3bdfSopenharmony_ci} 102f08c3bdfSopenharmony_ci 103f08c3bdfSopenharmony_cistatic struct tst_test test = { 104f08c3bdfSopenharmony_ci .test_all = pipe_socket, 105f08c3bdfSopenharmony_ci .setup = setup, 106f08c3bdfSopenharmony_ci .cleanup = cleanup, 107f08c3bdfSopenharmony_ci .options = (struct tst_option[]) { 108f08c3bdfSopenharmony_ci {"l:", &str_len_data, "Length of test data (in bytes)"}, 109f08c3bdfSopenharmony_ci {} 110f08c3bdfSopenharmony_ci }, 111f08c3bdfSopenharmony_ci}; 112