Skip to contentSkip to conversation

read: verify read size and data correctness

PATCH In review Jinseok Kim 2 participants 4 messages Mar 21 – Apr 16

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 read: verify read size and data correctness

The test only checked that read(2) did not fail, which allows partial reads or unexpected data to pass unnoticed.

Verify that read(2) returns the expected number of bytes and that the read data matches the written buffer.

---
 testcases/kernel/syscalls/read/read01.c | 27 ++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)
testcases/kernel/syscalls/read/read01.c +20 -7
index 68d6346c5..f205979b9 100644
@@ -10,25 +10,38 @@
 #define SIZE 512
 
 static int fd;
-static char buf[SIZE];
+static char write_buf[SIZE];
+static char read_buf[SIZE];
 
 static void verify_read(void)
 {
 	SAFE_LSEEK(fd, 0, SEEK_SET);
 
-	TEST(read(fd, buf, SIZE));
+	TEST(read(fd, read_buf, SIZE));
 
-	if (TST_RET == -1)
+	if (TST_RET == -1) {
 		tst_res(TFAIL | TTERRNO, "read(2) failed");
-	else
-		tst_res(TPASS, "read(2) returned %ld", TST_RET);
+		return;
+	}
+
+	if (TST_RET != SIZE) {
+		tst_res(TFAIL, "read(2) returned %ld, expected %d", TST_RET, SIZE);
+		return;
+	}
+
+	if (memcmp(write_buf, read_buf, SIZE)) {
+		tst_res(TFAIL, "read(2) returned unexpected data");
+		return;
+	}
+
+	tst_res(TPASS, "read(2) returned expected data %ld", TST_RET);
 }
 
 static void setup(void)
 {
-	memset(buf, '*', SIZE);
+	memset(write_buf, '*', SIZE);
 	fd = SAFE_OPEN("testfile", O_RDWR | O_CREAT, 0700);
-	SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, SIZE);
+	SAFE_WRITE(SAFE_WRITE_ALL, fd, write_buf, SIZE);
 }
 
 static void cleanup(void)

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi Jinseok,

the name of the commit is related to read01, so it should be:

read01: verify read size and data correctness

Quoted 19 lines

The test only checked that read(2) did not fail, which allows partial reads or unexpected data to pass unnoticed.

Verify that read(2) returns the expected number of bytes and that the read data matches the written buffer.

---
 testcases/kernel/syscalls/read/read01.c | 27 ++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)
testcases/kernel/syscalls/read/read01.c +0 -0
index 68d6346c5..f205979b9 100644
@@ -10,25 +10,38 @@
 #define SIZE 512
 
 static int fd;

testcases/kernel/syscalls/read/read01.c line 20

Quoted 10 lines
testcases/kernel/syscalls/read/read01.c +3 -2
-static char buf[SIZE];
+static char write_buf[SIZE];
+static char read_buf[SIZE];
 
 static void verify_read(void)
 {
 	SAFE_LSEEK(fd, 0, SEEK_SET);
 
-	TEST(read(fd, buf, SIZE));
+	TEST(read(fd, read_buf, SIZE));

We have TST_EXP_PASS().

testcases/kernel/syscalls/read/read01.c line 25

Quoted 8 lines
testcases/kernel/syscalls/read/read01.c +3 -3
-	if (TST_RET == -1)
+	if (TST_RET == -1) {
 		tst_res(TFAIL | TTERRNO, "read(2) failed");
-	else
-		tst_res(TPASS, "read(2) returned %ld", TST_RET);
+		return;
+	}

So this won't be needed.

testcases/kernel/syscalls/read/read01.c line 30

Quoted 5 lines
testcases/kernel/syscalls/read/read01.c +5 -0
+
+	if (TST_RET != SIZE) {
+		tst_res(TFAIL, "read(2) returned %ld, expected %d", TST_RET, SIZE);
+		return;
+	}

TST_EXP_EQ_LI(TST_RET, SIZE);

testcases/kernel/syscalls/read/read01.c line 35

Quoted 5 lines
testcases/kernel/syscalls/read/read01.c +5 -0
+
+	if (memcmp(write_buf, read_buf, SIZE)) {
+		tst_res(TFAIL, "read(2) returned unexpected data");
+		return;
+	}

TST_EXP_EQ_STRN(write_buf, read_buf, SIZE);

testcases/kernel/syscalls/read/read01.c line 36

Quoted 2 lines
testcases/kernel/syscalls/read/read01.c +2 -0
+
+	tst_res(TPASS, "read(2) returned expected data %ld", TST_RET);

So this is not needed anymore.

Quoted 13 lines
testcases/kernel/syscalls/read/read01.c +2 -3
 }
 
 static void setup(void)
 {
-	memset(buf, '*', SIZE);
+	memset(write_buf, '*', SIZE);
 	fd = SAFE_OPEN("testfile", O_RDWR | O_CREAT, 0700);
-	SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, SIZE);
+	SAFE_WRITE(SAFE_WRITE_ALL, fd, write_buf, SIZE);
 }
 
 static void cleanup(void)
--
Quoted 4 lines

2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

PATCH 1/1 read: verify read size and data correctness

Hi,

Thanks for the review.

While looking into this further, I realized that strengthening read01.c in this way would make it overlap with read04.c.
Would you prefer me to:

- keep read01.c as a minimal success test, or
- update read01.c and drop read04.c as redundant?

The main distinction seems to be full read vs short read at EOF, so I wanted to confirm your preference before sending the next revision.

Thanks,
Jinseok.

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Hi Jinseok,

Quoted 11 lines

Hi,

Thanks for the review.

While looking into this further, I realized that strengthening read01.c in this way would make it overlap with read04.c.
Would you prefer me to:

- keep read01.c as a minimal success test, or
- update read01.c and drop read04.c as redundant?

Quoted 6 lines

The main distinction seems to be full read vs short read at EOF, so I wanted to confirm your preference before sending the next revision.

Thanks,
Jinseok.

I'm sorry, indeed I didn't notice that this test was overlapping I would keep the test minimal then, because we don't want to have two tests haveing the same purpose.

Regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp