1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001 4f08c3bdfSopenharmony_ci * 07/2001 Ported by Wayne Boyer 5f08c3bdfSopenharmony_ci * 08/2002 Make it use a socket so it works with 2.5 kernel 6f08c3bdfSopenharmony_ci * Copyright (c) 2021 Xie Ziyao <xieziyao@huawei.com> 7f08c3bdfSopenharmony_ci */ 8f08c3bdfSopenharmony_ci 9f08c3bdfSopenharmony_ci/*\ 10f08c3bdfSopenharmony_ci * [Description] 11f08c3bdfSopenharmony_ci * 12f08c3bdfSopenharmony_ci * Test the basic functionality of the sendfile() system call: 13f08c3bdfSopenharmony_ci * 14f08c3bdfSopenharmony_ci * 1. Call sendfile() with offset = 0. 15f08c3bdfSopenharmony_ci * 2. Call sendfile() with offset in the middle of the file. 16f08c3bdfSopenharmony_ci */ 17f08c3bdfSopenharmony_ci 18f08c3bdfSopenharmony_ci#include <stdio.h> 19f08c3bdfSopenharmony_ci#include <inttypes.h> 20f08c3bdfSopenharmony_ci#include <sys/sendfile.h> 21f08c3bdfSopenharmony_ci 22f08c3bdfSopenharmony_ci#include "tst_test.h" 23f08c3bdfSopenharmony_ci 24f08c3bdfSopenharmony_ci#define IN_FILE "in_file" 25f08c3bdfSopenharmony_ci#define OUT_FILE "out_file" 26f08c3bdfSopenharmony_ci 27f08c3bdfSopenharmony_ci#define OFFSET_DESC(x) .desc = "with offset = "#x, .offset = x 28f08c3bdfSopenharmony_ci 29f08c3bdfSopenharmony_cistruct test_case_t { 30f08c3bdfSopenharmony_ci char *desc; 31f08c3bdfSopenharmony_ci off_t offset; 32f08c3bdfSopenharmony_ci int64_t count; 33f08c3bdfSopenharmony_ci int64_t exp_retval; 34f08c3bdfSopenharmony_ci int64_t exp_updated_offset; 35f08c3bdfSopenharmony_ci} tc[] = { 36f08c3bdfSopenharmony_ci { OFFSET_DESC(0), 26, 26, 26 }, 37f08c3bdfSopenharmony_ci { OFFSET_DESC(2), 24, 24, 26 }, 38f08c3bdfSopenharmony_ci}; 39f08c3bdfSopenharmony_ci 40f08c3bdfSopenharmony_cistatic void setup(void) 41f08c3bdfSopenharmony_ci{ 42f08c3bdfSopenharmony_ci int fd; 43f08c3bdfSopenharmony_ci char buf[27]; 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_ci fd = SAFE_CREAT(IN_FILE, 00700); 46f08c3bdfSopenharmony_ci sprintf(buf, "abcdefghijklmnopqrstuvwxyz"); 47f08c3bdfSopenharmony_ci SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, strlen(buf)); 48f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 49f08c3bdfSopenharmony_ci 50f08c3bdfSopenharmony_ci fd = SAFE_CREAT(OUT_FILE, 00700); 51f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 52f08c3bdfSopenharmony_ci} 53f08c3bdfSopenharmony_ci 54f08c3bdfSopenharmony_cistatic void run(unsigned int i) 55f08c3bdfSopenharmony_ci{ 56f08c3bdfSopenharmony_ci int in_fd = SAFE_OPEN(IN_FILE, O_RDONLY); 57f08c3bdfSopenharmony_ci int out_fd = SAFE_OPEN(OUT_FILE, O_WRONLY); 58f08c3bdfSopenharmony_ci off_t offset = tc[i].offset; 59f08c3bdfSopenharmony_ci off_t before_pos, after_pos; 60f08c3bdfSopenharmony_ci 61f08c3bdfSopenharmony_ci before_pos = SAFE_LSEEK(in_fd, 0, SEEK_CUR); 62f08c3bdfSopenharmony_ci 63f08c3bdfSopenharmony_ci TEST(sendfile(out_fd, in_fd, &offset, tc[i].count)); 64f08c3bdfSopenharmony_ci after_pos = SAFE_LSEEK(in_fd, 0, SEEK_CUR); 65f08c3bdfSopenharmony_ci 66f08c3bdfSopenharmony_ci if (tc[i].exp_retval != TST_RET) 67f08c3bdfSopenharmony_ci tst_res(TFAIL, "sendfile() failed to return expected value, " 68f08c3bdfSopenharmony_ci "expected: %" PRId64 ", got: %ld", 69f08c3bdfSopenharmony_ci tc[i].exp_retval, TST_RET); 70f08c3bdfSopenharmony_ci else if (offset != tc[i].exp_updated_offset) 71f08c3bdfSopenharmony_ci tst_res(TFAIL, "sendfile() failed to update OFFSET parameter to " 72f08c3bdfSopenharmony_ci "expected value, expected: %" PRId64 ", got: %" PRId64, 73f08c3bdfSopenharmony_ci tc[i].exp_updated_offset, (int64_t)(offset)); 74f08c3bdfSopenharmony_ci else if (before_pos != after_pos) 75f08c3bdfSopenharmony_ci tst_res(TFAIL, "sendfile() updated the file position of in_fd " 76f08c3bdfSopenharmony_ci "unexpectedly, expected file position: %" PRId64 77f08c3bdfSopenharmony_ci ", actual file position %" PRId64, 78f08c3bdfSopenharmony_ci (int64_t)(before_pos), (int64_t)(after_pos)); 79f08c3bdfSopenharmony_ci else 80f08c3bdfSopenharmony_ci tst_res(TPASS, "sendfile() with %s", tc[i].desc); 81f08c3bdfSopenharmony_ci 82f08c3bdfSopenharmony_ci SAFE_CLOSE(in_fd); 83f08c3bdfSopenharmony_ci SAFE_CLOSE(out_fd); 84f08c3bdfSopenharmony_ci} 85f08c3bdfSopenharmony_ci 86f08c3bdfSopenharmony_cistatic struct tst_test test = { 87f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 88f08c3bdfSopenharmony_ci .setup = setup, 89f08c3bdfSopenharmony_ci .test = run, 90f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tc), 91f08c3bdfSopenharmony_ci}; 92