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