1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci* Copyright (c) 2015 Fujitsu Ltd. 4f08c3bdfSopenharmony_ci* Author: Xiao Yang <yangx.jy@cn.fujitsu.com> 5f08c3bdfSopenharmony_ci*/ 6f08c3bdfSopenharmony_ci 7f08c3bdfSopenharmony_ci/* 8f08c3bdfSopenharmony_ci* Test Name: pwritev01 9f08c3bdfSopenharmony_ci* 10f08c3bdfSopenharmony_ci* Test Description: 11f08c3bdfSopenharmony_ci* Testcase to check the basic functionality of the pwritev(2). 12f08c3bdfSopenharmony_ci* pwritev(2) should succeed to write the expected content of data 13f08c3bdfSopenharmony_ci* and after writing the file, the file offset is not changed. 14f08c3bdfSopenharmony_ci*/ 15f08c3bdfSopenharmony_ci 16f08c3bdfSopenharmony_ci#define _GNU_SOURCE 17f08c3bdfSopenharmony_ci#include <string.h> 18f08c3bdfSopenharmony_ci#include <sys/uio.h> 19f08c3bdfSopenharmony_ci#include "tst_test.h" 20f08c3bdfSopenharmony_ci#include "pwritev.h" 21f08c3bdfSopenharmony_ci#include "tst_safe_prw.h" 22f08c3bdfSopenharmony_ci 23f08c3bdfSopenharmony_ci#define CHUNK 64 24f08c3bdfSopenharmony_ci 25f08c3bdfSopenharmony_cistatic char buf[CHUNK]; 26f08c3bdfSopenharmony_cistatic char initbuf[CHUNK * 2]; 27f08c3bdfSopenharmony_cistatic char preadbuf[CHUNK]; 28f08c3bdfSopenharmony_cistatic int fd; 29f08c3bdfSopenharmony_ci 30f08c3bdfSopenharmony_cistatic struct iovec wr_iovec[] = { 31f08c3bdfSopenharmony_ci {buf, CHUNK}, 32f08c3bdfSopenharmony_ci {NULL, 0}, 33f08c3bdfSopenharmony_ci}; 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_cistatic struct tcase { 36f08c3bdfSopenharmony_ci int count; 37f08c3bdfSopenharmony_ci off_t offset; 38f08c3bdfSopenharmony_ci ssize_t size; 39f08c3bdfSopenharmony_ci} tcases[] = { 40f08c3bdfSopenharmony_ci {1, 0, CHUNK}, 41f08c3bdfSopenharmony_ci {2, 0, CHUNK}, 42f08c3bdfSopenharmony_ci {1, CHUNK/2, CHUNK}, 43f08c3bdfSopenharmony_ci}; 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_cistatic void verify_pwritev(unsigned int n) 46f08c3bdfSopenharmony_ci{ 47f08c3bdfSopenharmony_ci int i; 48f08c3bdfSopenharmony_ci struct tcase *tc = &tcases[n]; 49f08c3bdfSopenharmony_ci 50f08c3bdfSopenharmony_ci SAFE_PWRITE(1, fd, initbuf, sizeof(initbuf), 0); 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_ci SAFE_LSEEK(fd, 0, SEEK_SET); 53f08c3bdfSopenharmony_ci 54f08c3bdfSopenharmony_ci TEST(pwritev(fd, wr_iovec, tc->count, tc->offset)); 55f08c3bdfSopenharmony_ci if (TST_RET < 0) { 56f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, "pwritev() failed"); 57f08c3bdfSopenharmony_ci return; 58f08c3bdfSopenharmony_ci } 59f08c3bdfSopenharmony_ci 60f08c3bdfSopenharmony_ci if (TST_RET != tc->size) { 61f08c3bdfSopenharmony_ci tst_res(TFAIL, "pwritev() wrote %li bytes, expected %zi", 62f08c3bdfSopenharmony_ci TST_RET, tc->size); 63f08c3bdfSopenharmony_ci return; 64f08c3bdfSopenharmony_ci } 65f08c3bdfSopenharmony_ci 66f08c3bdfSopenharmony_ci if (SAFE_LSEEK(fd, 0, SEEK_CUR) != 0) { 67f08c3bdfSopenharmony_ci tst_res(TFAIL, "pwritev() had changed file offset"); 68f08c3bdfSopenharmony_ci return; 69f08c3bdfSopenharmony_ci } 70f08c3bdfSopenharmony_ci 71f08c3bdfSopenharmony_ci SAFE_PREAD(1, fd, preadbuf, tc->size, tc->offset); 72f08c3bdfSopenharmony_ci 73f08c3bdfSopenharmony_ci for (i = 0; i < tc->size; i++) { 74f08c3bdfSopenharmony_ci if (preadbuf[i] != 0x61) 75f08c3bdfSopenharmony_ci break; 76f08c3bdfSopenharmony_ci } 77f08c3bdfSopenharmony_ci 78f08c3bdfSopenharmony_ci if (i != tc->size) { 79f08c3bdfSopenharmony_ci tst_res(TFAIL, "buffer wrong at %i have %02x expected 61", 80f08c3bdfSopenharmony_ci i, preadbuf[i]); 81f08c3bdfSopenharmony_ci return; 82f08c3bdfSopenharmony_ci } 83f08c3bdfSopenharmony_ci 84f08c3bdfSopenharmony_ci tst_res(TPASS, "writev() wrote %zi bytes successfully " 85f08c3bdfSopenharmony_ci "with content 'a' expectedly ", tc->size); 86f08c3bdfSopenharmony_ci} 87f08c3bdfSopenharmony_ci 88f08c3bdfSopenharmony_cistatic void setup(void) 89f08c3bdfSopenharmony_ci{ 90f08c3bdfSopenharmony_ci memset(&buf, 0x61, CHUNK); 91f08c3bdfSopenharmony_ci 92f08c3bdfSopenharmony_ci fd = SAFE_OPEN("file", O_RDWR | O_CREAT, 0644); 93f08c3bdfSopenharmony_ci} 94f08c3bdfSopenharmony_ci 95f08c3bdfSopenharmony_cistatic void cleanup(void) 96f08c3bdfSopenharmony_ci{ 97f08c3bdfSopenharmony_ci if (fd > 0) 98f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 99f08c3bdfSopenharmony_ci} 100f08c3bdfSopenharmony_ci 101f08c3bdfSopenharmony_cistatic struct tst_test test = { 102f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tcases), 103f08c3bdfSopenharmony_ci .setup = setup, 104f08c3bdfSopenharmony_ci .cleanup = cleanup, 105f08c3bdfSopenharmony_ci .test = verify_pwritev, 106f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 107f08c3bdfSopenharmony_ci}; 108