1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2021
4f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2001
5f08c3bdfSopenharmony_ci * 07/2001 Ported by Wayne Boyer
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci/*\
9f08c3bdfSopenharmony_ci * [Description]
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * Verify the functionality of pwrite() by writing known data using pwrite()
12f08c3bdfSopenharmony_ci * to the file at various specified offsets and later read from the file from
13f08c3bdfSopenharmony_ci * various specified offsets, comparing the data written aganist the data
14f08c3bdfSopenharmony_ci * read using read().
15f08c3bdfSopenharmony_ci */
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#include <stdlib.h>
18f08c3bdfSopenharmony_ci#include <inttypes.h>
19f08c3bdfSopenharmony_ci#include "tst_test.h"
20f08c3bdfSopenharmony_ci#include "tst_safe_prw.h"
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci#define TEMPFILE	"pwrite_file"
23f08c3bdfSopenharmony_ci#define K1              1024
24f08c3bdfSopenharmony_ci#define K2              (K1 * 2)
25f08c3bdfSopenharmony_ci#define K3              (K1 * 3)
26f08c3bdfSopenharmony_ci#define K4              (K1 * 4)
27f08c3bdfSopenharmony_ci#define NBUFS           4
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_cistatic int fildes;
30f08c3bdfSopenharmony_cistatic char *write_buf[NBUFS];
31f08c3bdfSopenharmony_cistatic char *read_buf[NBUFS];
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cistatic void l_seek(int fdesc, off_t offset, int whence, off_t checkoff)
34f08c3bdfSopenharmony_ci{
35f08c3bdfSopenharmony_ci	off_t offloc;
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci	offloc = SAFE_LSEEK(fdesc, offset, whence);
38f08c3bdfSopenharmony_ci	if (offloc != checkoff) {
39f08c3bdfSopenharmony_ci		tst_res(TFAIL, "return = %" PRId64 ", expected %" PRId64,
40f08c3bdfSopenharmony_ci			(int64_t) offloc, (int64_t) checkoff);
41f08c3bdfSopenharmony_ci	}
42f08c3bdfSopenharmony_ci}
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_cistatic void check_file_contents(void)
45f08c3bdfSopenharmony_ci{
46f08c3bdfSopenharmony_ci	int count, err_flg = 0;
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	for (count = 0; count < NBUFS; count++) {
49f08c3bdfSopenharmony_ci		l_seek(fildes, count * K1, SEEK_SET, count * K1);
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci		SAFE_READ(1, fildes, read_buf[count], K1);
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci		if (memcmp(write_buf[count], read_buf[count], K1) != 0) {
54f08c3bdfSopenharmony_ci			tst_res(TFAIL, "read/write buffer[%d] data mismatch", count);
55f08c3bdfSopenharmony_ci			err_flg++;
56f08c3bdfSopenharmony_ci		}
57f08c3bdfSopenharmony_ci	}
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci	if (!err_flg)
60f08c3bdfSopenharmony_ci		tst_res(TPASS, "Functionality of pwrite() successful");
61f08c3bdfSopenharmony_ci}
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_cistatic void verify_pwrite(void)
64f08c3bdfSopenharmony_ci{
65f08c3bdfSopenharmony_ci	SAFE_PWRITE(1, fildes, write_buf[0], K1, 0);
66f08c3bdfSopenharmony_ci	l_seek(fildes, 0, SEEK_CUR, 0);
67f08c3bdfSopenharmony_ci	l_seek(fildes, K1 / 2, SEEK_SET, K1 / 2);
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ci	SAFE_PWRITE(1, fildes, write_buf[2], K1, K2);
70f08c3bdfSopenharmony_ci	l_seek(fildes, 0, SEEK_CUR, K1 / 2);
71f08c3bdfSopenharmony_ci	l_seek(fildes, K3, SEEK_SET, K3);
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci	SAFE_WRITE(SAFE_WRITE_ALL, fildes, write_buf[3], K1);
74f08c3bdfSopenharmony_ci	l_seek(fildes, 0, SEEK_CUR, K4);
75f08c3bdfSopenharmony_ci
76f08c3bdfSopenharmony_ci	SAFE_PWRITE(1, fildes, write_buf[1], K1, K1);
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci	check_file_contents();
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci	l_seek(fildes, 0, SEEK_SET, 0);
81f08c3bdfSopenharmony_ci}
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_cistatic void setup(void)
84f08c3bdfSopenharmony_ci{
85f08c3bdfSopenharmony_ci	int count;
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_ci	for (count = 0; count < NBUFS; count++) {
88f08c3bdfSopenharmony_ci		write_buf[count] = SAFE_MALLOC(K1);
89f08c3bdfSopenharmony_ci		read_buf[count] = SAFE_MALLOC(K1);
90f08c3bdfSopenharmony_ci		memset(write_buf[count], count, K1);
91f08c3bdfSopenharmony_ci	}
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ci	fildes = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
94f08c3bdfSopenharmony_ci}
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_cistatic void cleanup(void)
97f08c3bdfSopenharmony_ci{
98f08c3bdfSopenharmony_ci	int count;
99f08c3bdfSopenharmony_ci
100f08c3bdfSopenharmony_ci	for (count = 0; count < NBUFS; count++) {
101f08c3bdfSopenharmony_ci		free(write_buf[count]);
102f08c3bdfSopenharmony_ci		free(read_buf[count]);
103f08c3bdfSopenharmony_ci	}
104f08c3bdfSopenharmony_ci
105f08c3bdfSopenharmony_ci	if (fildes > 0)
106f08c3bdfSopenharmony_ci		SAFE_CLOSE(fildes);
107f08c3bdfSopenharmony_ci}
108f08c3bdfSopenharmony_ci
109f08c3bdfSopenharmony_cistatic struct tst_test test = {
110f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
111f08c3bdfSopenharmony_ci	.setup = setup,
112f08c3bdfSopenharmony_ci	.cleanup = cleanup,
113f08c3bdfSopenharmony_ci	.test_all = verify_pwrite,
114f08c3bdfSopenharmony_ci};
115