1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2014 Fujitsu Ltd. 4f08c3bdfSopenharmony_ci * Author: Xing Gu <gux.fnst@cn.fujitsu.com> 5f08c3bdfSopenharmony_ci */ 6f08c3bdfSopenharmony_ci/* 7f08c3bdfSopenharmony_ci * Description: 8f08c3bdfSopenharmony_ci * Verify that, 9f08c3bdfSopenharmony_ci * 1) splice() returns -1 and sets errno to EBADF if the file 10f08c3bdfSopenharmony_ci * descriptor fd_in is not valid. 11f08c3bdfSopenharmony_ci * 2) splice() returns -1 and sets errno to EBADF if the file 12f08c3bdfSopenharmony_ci * descriptor fd_out is not valid. 13f08c3bdfSopenharmony_ci * 3) splice() returns -1 and sets errno to EBADF if the file 14f08c3bdfSopenharmony_ci * descriptor fd_in does not have proper read-write mode. 15f08c3bdfSopenharmony_ci * 4) splice() returns -1 and sets errno to EINVAL if target 16f08c3bdfSopenharmony_ci * file is opened in append mode. 17f08c3bdfSopenharmony_ci * 5) splice() returns -1 and sets errno to EINVAL if neither 18f08c3bdfSopenharmony_ci * of the descriptors refer to a pipe. 19f08c3bdfSopenharmony_ci * 6) splice() returns -1 and sets errno to ESPIPE if off_in is 20f08c3bdfSopenharmony_ci * not NULL when the file descriptor fd_in refers to a pipe. 21f08c3bdfSopenharmony_ci * 7) splice() returns -1 and sets errno to ESPIPE if off_out is 22f08c3bdfSopenharmony_ci * not NULL when the file descriptor fd_out refers to a pipe. 23f08c3bdfSopenharmony_ci */ 24f08c3bdfSopenharmony_ci 25f08c3bdfSopenharmony_ci#define _GNU_SOURCE 26f08c3bdfSopenharmony_ci 27f08c3bdfSopenharmony_ci#include <sys/types.h> 28f08c3bdfSopenharmony_ci#include <sys/stat.h> 29f08c3bdfSopenharmony_ci#include <fcntl.h> 30f08c3bdfSopenharmony_ci#include <unistd.h> 31f08c3bdfSopenharmony_ci 32f08c3bdfSopenharmony_ci#include "tst_test.h" 33f08c3bdfSopenharmony_ci#include "lapi/splice.h" 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_ci#define TEST_FILE "testfile" 36f08c3bdfSopenharmony_ci#define TEST_FILE2 "testfile2" 37f08c3bdfSopenharmony_ci#define TEST_FILE3 "testfile3" 38f08c3bdfSopenharmony_ci 39f08c3bdfSopenharmony_ci#define STR "abcdefghigklmnopqrstuvwxyz" 40f08c3bdfSopenharmony_ci#define SPLICE_TEST_LEN 10 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_cistatic int badfd = -1; 43f08c3bdfSopenharmony_cistatic int rdfd; 44f08c3bdfSopenharmony_cistatic int wrfd; 45f08c3bdfSopenharmony_cistatic int appendfd; 46f08c3bdfSopenharmony_cistatic int pipes[2]; 47f08c3bdfSopenharmony_cistatic loff_t offset; 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_cistatic struct tcase { 50f08c3bdfSopenharmony_ci int *fdin; 51f08c3bdfSopenharmony_ci loff_t *offin; 52f08c3bdfSopenharmony_ci int *fdout; 53f08c3bdfSopenharmony_ci loff_t *offout; 54f08c3bdfSopenharmony_ci int exp_errno; 55f08c3bdfSopenharmony_ci} tcases[] = { 56f08c3bdfSopenharmony_ci { &badfd, NULL, &pipes[1], NULL, EBADF }, 57f08c3bdfSopenharmony_ci { &pipes[0], NULL, &badfd, NULL, EBADF }, 58f08c3bdfSopenharmony_ci { &wrfd, NULL, &pipes[1], NULL, EBADF }, 59f08c3bdfSopenharmony_ci { &pipes[0], NULL, &appendfd, NULL, EINVAL }, 60f08c3bdfSopenharmony_ci { &rdfd, NULL, &wrfd, NULL, EINVAL }, 61f08c3bdfSopenharmony_ci { &pipes[0], &offset, &wrfd, NULL, ESPIPE }, 62f08c3bdfSopenharmony_ci { &rdfd, NULL, &pipes[1], &offset, ESPIPE }, 63f08c3bdfSopenharmony_ci}; 64f08c3bdfSopenharmony_ci 65f08c3bdfSopenharmony_cistatic void setup(void) 66f08c3bdfSopenharmony_ci{ 67f08c3bdfSopenharmony_ci SAFE_FILE_PRINTF(TEST_FILE, STR); 68f08c3bdfSopenharmony_ci rdfd = SAFE_OPEN(TEST_FILE, O_RDONLY); 69f08c3bdfSopenharmony_ci 70f08c3bdfSopenharmony_ci wrfd = SAFE_OPEN(TEST_FILE2, O_WRONLY | O_CREAT, 0644); 71f08c3bdfSopenharmony_ci 72f08c3bdfSopenharmony_ci appendfd = SAFE_OPEN(TEST_FILE3, O_RDWR | O_CREAT | O_APPEND, 0644); 73f08c3bdfSopenharmony_ci 74f08c3bdfSopenharmony_ci SAFE_PIPE(pipes); 75f08c3bdfSopenharmony_ci 76f08c3bdfSopenharmony_ci SAFE_WRITE(SAFE_WRITE_ALL, pipes[1], STR, sizeof(STR) - 1); 77f08c3bdfSopenharmony_ci} 78f08c3bdfSopenharmony_ci 79f08c3bdfSopenharmony_cistatic void splice_verify(unsigned int n) 80f08c3bdfSopenharmony_ci{ 81f08c3bdfSopenharmony_ci struct tcase *tc = &tcases[n]; 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_ci TEST(splice(*(tc->fdin), tc->offin, *(tc->fdout), 84f08c3bdfSopenharmony_ci tc->offout, SPLICE_TEST_LEN, 0)); 85f08c3bdfSopenharmony_ci 86f08c3bdfSopenharmony_ci if (TST_RET != -1) { 87f08c3bdfSopenharmony_ci tst_res(TFAIL, "splice() returned %ld expected %s", 88f08c3bdfSopenharmony_ci TST_RET, tst_strerrno(tc->exp_errno)); 89f08c3bdfSopenharmony_ci return; 90f08c3bdfSopenharmony_ci } 91f08c3bdfSopenharmony_ci 92f08c3bdfSopenharmony_ci if (TST_ERR != tc->exp_errno) { 93f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, 94f08c3bdfSopenharmony_ci "splice() failed unexpectedly; expected: %d - %s", 95f08c3bdfSopenharmony_ci tc->exp_errno, tst_strerrno(tc->exp_errno)); 96f08c3bdfSopenharmony_ci return; 97f08c3bdfSopenharmony_ci } 98f08c3bdfSopenharmony_ci 99f08c3bdfSopenharmony_ci tst_res(TPASS | TTERRNO, "splice() failed as expected"); 100f08c3bdfSopenharmony_ci} 101f08c3bdfSopenharmony_ci 102f08c3bdfSopenharmony_cistatic void cleanup(void) 103f08c3bdfSopenharmony_ci{ 104f08c3bdfSopenharmony_ci if (rdfd > 0) 105f08c3bdfSopenharmony_ci SAFE_CLOSE(rdfd); 106f08c3bdfSopenharmony_ci 107f08c3bdfSopenharmony_ci if (wrfd > 0) 108f08c3bdfSopenharmony_ci SAFE_CLOSE(wrfd); 109f08c3bdfSopenharmony_ci 110f08c3bdfSopenharmony_ci if (appendfd > 0) 111f08c3bdfSopenharmony_ci SAFE_CLOSE(appendfd); 112f08c3bdfSopenharmony_ci 113f08c3bdfSopenharmony_ci if (pipes[0] > 0) 114f08c3bdfSopenharmony_ci SAFE_CLOSE(pipes[0]); 115f08c3bdfSopenharmony_ci 116f08c3bdfSopenharmony_ci if (pipes[1] > 0) 117f08c3bdfSopenharmony_ci SAFE_CLOSE(pipes[1]); 118f08c3bdfSopenharmony_ci} 119f08c3bdfSopenharmony_ci 120f08c3bdfSopenharmony_cistatic struct tst_test test = { 121f08c3bdfSopenharmony_ci .setup = setup, 122f08c3bdfSopenharmony_ci .cleanup = cleanup, 123f08c3bdfSopenharmony_ci .test = splice_verify, 124f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tcases), 125f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 126f08c3bdfSopenharmony_ci}; 127