1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*\
7f08c3bdfSopenharmony_ci * [Description]
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * Testcase to check if read() returns the number of bytes read correctly.
10f08c3bdfSopenharmony_ci */
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ci#include <sys/types.h>
13f08c3bdfSopenharmony_ci#include <sys/stat.h>
14f08c3bdfSopenharmony_ci#include <stdio.h>
15f08c3bdfSopenharmony_ci#include <fcntl.h>
16f08c3bdfSopenharmony_ci#include "tst_test.h"
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_cistatic const char *fname = "test_file";
19f08c3bdfSopenharmony_cistatic const char palfa[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
20f08c3bdfSopenharmony_ci#define PALFA_LEN (sizeof(palfa)-1)
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_cistatic void verify_read(void)
23f08c3bdfSopenharmony_ci{
24f08c3bdfSopenharmony_ci	int fd;
25f08c3bdfSopenharmony_ci	char prbuf[BUFSIZ];
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(fname, O_RDONLY);
28f08c3bdfSopenharmony_ci	TEST(read(fd, prbuf, BUFSIZ));
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	if (TST_RET != PALFA_LEN) {
31f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Bad read count - got %ld - expected %zu",
32f08c3bdfSopenharmony_ci				TST_RET, PALFA_LEN);
33f08c3bdfSopenharmony_ci		goto out;
34f08c3bdfSopenharmony_ci	}
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	if (memcmp(palfa, prbuf, PALFA_LEN)) {
37f08c3bdfSopenharmony_ci		tst_res(TFAIL, "read buffer not equal to write buffer");
38f08c3bdfSopenharmony_ci		goto out;
39f08c3bdfSopenharmony_ci	}
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	tst_res(TPASS, "read() data correctly");
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ciout:
44f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
45f08c3bdfSopenharmony_ci}
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_cistatic void setup(void)
48f08c3bdfSopenharmony_ci{
49f08c3bdfSopenharmony_ci	int fd;
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci	fd = SAFE_CREAT(fname, 0777);
52f08c3bdfSopenharmony_ci	SAFE_WRITE(SAFE_WRITE_ALL, fd, palfa, PALFA_LEN);
53f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
54f08c3bdfSopenharmony_ci}
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_cistatic struct tst_test test = {
57f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
58f08c3bdfSopenharmony_ci	.setup = setup,
59f08c3bdfSopenharmony_ci	.test_all = verify_read,
60f08c3bdfSopenharmony_ci};
61