1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001 4f08c3bdfSopenharmony_ci * 06/2017 modified by Xiao Yang <yangx.jy@cn.fujitsu.com> 5f08c3bdfSopenharmony_ci */ 6f08c3bdfSopenharmony_ci 7f08c3bdfSopenharmony_ci/* 8f08c3bdfSopenharmony_ci * Description: 9f08c3bdfSopenharmony_ci * lseek() succeeds to set the specified offset according to whence 10f08c3bdfSopenharmony_ci * and read valid data from this location. 11f08c3bdfSopenharmony_ci */ 12f08c3bdfSopenharmony_ci 13f08c3bdfSopenharmony_ci#include <errno.h> 14f08c3bdfSopenharmony_ci#include <string.h> 15f08c3bdfSopenharmony_ci#include <sys/types.h> 16f08c3bdfSopenharmony_ci#include <unistd.h> 17f08c3bdfSopenharmony_ci#include "tst_test.h" 18f08c3bdfSopenharmony_ci 19f08c3bdfSopenharmony_ci#define WRITE_STR "abcdefg" 20f08c3bdfSopenharmony_ci#define TFILE "tfile" 21f08c3bdfSopenharmony_ci 22f08c3bdfSopenharmony_cistatic int fd; 23f08c3bdfSopenharmony_cistatic struct tcase { 24f08c3bdfSopenharmony_ci off_t off; 25f08c3bdfSopenharmony_ci int whence; 26f08c3bdfSopenharmony_ci char *wname; 27f08c3bdfSopenharmony_ci off_t exp_off; 28f08c3bdfSopenharmony_ci ssize_t exp_size; 29f08c3bdfSopenharmony_ci char *exp_data; 30f08c3bdfSopenharmony_ci} tcases[] = { 31f08c3bdfSopenharmony_ci {4, SEEK_SET, "SEEK_SET", 4, 3, "efg"}, 32f08c3bdfSopenharmony_ci {-2, SEEK_CUR, "SEEK_CUR", 5, 2, "fg"}, 33f08c3bdfSopenharmony_ci {-4, SEEK_END, "SEEK_END", 3, 4, "defg"}, 34f08c3bdfSopenharmony_ci {0, SEEK_END, "SEEK_END", 7, 0, NULL}, 35f08c3bdfSopenharmony_ci}; 36f08c3bdfSopenharmony_ci 37f08c3bdfSopenharmony_cistatic void verify_lseek(unsigned int n) 38f08c3bdfSopenharmony_ci{ 39f08c3bdfSopenharmony_ci char read_buf[64]; 40f08c3bdfSopenharmony_ci struct tcase *tc = &tcases[n]; 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_ci // reset the offset to end of file 43f08c3bdfSopenharmony_ci SAFE_READ(0, fd, read_buf, sizeof(read_buf)); 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_ci memset(read_buf, 0, sizeof(read_buf)); 46f08c3bdfSopenharmony_ci 47f08c3bdfSopenharmony_ci TEST(lseek(fd, tc->off, tc->whence)); 48f08c3bdfSopenharmony_ci if (TST_RET == (off_t) -1) { 49f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, "lseek(%s, %lld, %s) failed", TFILE, 50f08c3bdfSopenharmony_ci (long long int)tc->off, tc->wname); 51f08c3bdfSopenharmony_ci return; 52f08c3bdfSopenharmony_ci } 53f08c3bdfSopenharmony_ci 54f08c3bdfSopenharmony_ci if (TST_RET != tc->exp_off) { 55f08c3bdfSopenharmony_ci tst_res(TFAIL, "lseek(%s, %lld, %s) returned %ld, expected %lld", 56f08c3bdfSopenharmony_ci TFILE, (long long int)tc->off, tc->wname, TST_RET, 57f08c3bdfSopenharmony_ci (long long int)tc->exp_off); 58f08c3bdfSopenharmony_ci return; 59f08c3bdfSopenharmony_ci } 60f08c3bdfSopenharmony_ci 61f08c3bdfSopenharmony_ci SAFE_READ(1, fd, read_buf, tc->exp_size); 62f08c3bdfSopenharmony_ci 63f08c3bdfSopenharmony_ci if (tc->exp_data && strcmp(read_buf, tc->exp_data)) { 64f08c3bdfSopenharmony_ci tst_res(TFAIL, "lseek(%s, %lld, %s) read incorrect data", 65f08c3bdfSopenharmony_ci TFILE, (long long int)tc->off, tc->wname); 66f08c3bdfSopenharmony_ci } else { 67f08c3bdfSopenharmony_ci tst_res(TPASS, "lseek(%s, %lld, %s) read correct data", 68f08c3bdfSopenharmony_ci TFILE, (long long int)tc->off, tc->wname); 69f08c3bdfSopenharmony_ci } 70f08c3bdfSopenharmony_ci} 71f08c3bdfSopenharmony_ci 72f08c3bdfSopenharmony_cistatic void setup(void) 73f08c3bdfSopenharmony_ci{ 74f08c3bdfSopenharmony_ci fd = SAFE_OPEN(TFILE, O_RDWR | O_CREAT, 0644); 75f08c3bdfSopenharmony_ci 76f08c3bdfSopenharmony_ci SAFE_WRITE(SAFE_WRITE_ALL, fd, WRITE_STR, sizeof(WRITE_STR) - 1); 77f08c3bdfSopenharmony_ci} 78f08c3bdfSopenharmony_ci 79f08c3bdfSopenharmony_cistatic void cleanup(void) 80f08c3bdfSopenharmony_ci{ 81f08c3bdfSopenharmony_ci if (fd > 0) 82f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 83f08c3bdfSopenharmony_ci} 84f08c3bdfSopenharmony_ci 85f08c3bdfSopenharmony_cistatic struct tst_test test = { 86f08c3bdfSopenharmony_ci .setup = setup, 87f08c3bdfSopenharmony_ci .cleanup = cleanup, 88f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tcases), 89f08c3bdfSopenharmony_ci .test = verify_lseek, 90f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 91f08c3bdfSopenharmony_ci}; 92