Skip to contentSkip to conversation

Port readv01 to new LTP library

PATCH New Martin Doucha 2 participants 4 messages Mar 13, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Port readv01 to new LTP library

---
 testcases/kernel/syscalls/readv/readv01.c | 110 ++++++++--------------
 1 file changed, 38 insertions(+), 72 deletions(-)
testcases/kernel/syscalls/readv/readv01.c +38 -72
index 82fec39e1..ad0ab187b 100644
@@ -1,22 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) International Business Machines  Corp., 2001
  *   07/2001 Ported by Wayne Boyer
- *
  * Copyright (c) 2013 Cyril Hrubis <[email protected]>
- *
- * This program is free software;  you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY;  without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
- * the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program;  if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /*
@@ -31,16 +17,11 @@
 #include <sys/uio.h>
 #include <fcntl.h>
 #include <memory.h>
-#include <errno.h>
 
-#include "test.h"
-#include "safe_macros.h"
+#include "tst_test.h"
 
 #define	CHUNK		64
 
-char *TCID = "readv01";
-int TST_TOTAL = 1;
-
 static char buf[CHUNK];
 
 static struct iovec rd_iovec[] = {
@@ -51,74 +32,59 @@ static struct iovec rd_iovec[] = {
 
 static int fd;
 
-static void setup(void);
-static void cleanup(void);
-
-int main(int ac, char **av)
+static void run(void)
 {
-	int lc, i, fail;
+	int i, fail;
 	char *vec;
 
-	tst_parse_opts(ac, av, NULL, NULL);
+	SAFE_LSEEK(fd, 0, SEEK_SET);
 
-	setup();
+	if (readv(fd, rd_iovec, 0) == -1)
+		tst_res(TFAIL | TERRNO, "readv failed unexpectedly");
+	else
+		tst_res(TPASS, "readv read 0 io vectors");
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
+	memset(rd_iovec[0].iov_base, 0x00, CHUNK);
 
-		SAFE_LSEEK(cleanup, fd, 0, SEEK_SET);
+	if (readv(fd, rd_iovec, 3) != CHUNK) {
+		tst_res(TFAIL, "readv failed reading %d bytes, "
+			 "followed by two NULL vectors", CHUNK);
+	} else {
+		fail = 0;
+		vec = rd_iovec[0].iov_base;
 
-		if (readv(fd, rd_iovec, 0) == -1)
-			tst_resm(TFAIL | TERRNO, "readv failed unexpectedly");
-		else
-			tst_resm(TPASS, "readv read 0 io vectors");
-
-		memset(rd_iovec[0].iov_base, 0x00, CHUNK);
-
-		if (readv(fd, rd_iovec, 3) != CHUNK) {
-			tst_resm(TFAIL, "readv failed reading %d bytes, "
-				 "followed by two NULL vectors", CHUNK);
-		} else {
-			fail = 0;
-			vec = rd_iovec[0].iov_base;
-
-			for (i = 0; i < CHUNK; i++) {
-				if (vec[i] != 0x42)
-					fail++;
-			}
-
-			if (fail)
-				tst_resm(TFAIL, "Wrong buffer content");
-			else
-				tst_resm(TPASS, "readv passed reading %d bytes "
-				         "followed by two NULL vectors", CHUNK);
+		for (i = 0; i < CHUNK; i++) {
+			if (vec[i] != 0x42)
+				fail++;
 		}
-	}
 
-	cleanup();
-	tst_exit();
+		if (fail)
+			tst_res(TFAIL, "Wrong buffer content");
+		else
+			tst_res(TPASS, "readv passed reading %d bytes "
+			         "followed by two NULL vectors", CHUNK);
+	}
 }
 
 static void setup(void)
 {
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
 	memset(buf, 0x42, sizeof(buf));
 
-	fd = SAFE_OPEN(cleanup, "data_file", O_WRONLY | O_CREAT, 0666);
-	SAFE_WRITE(cleanup, 1, fd, buf, sizeof(buf));
-	SAFE_CLOSE(cleanup, fd);
-	fd = SAFE_OPEN(cleanup, "data_file", O_RDONLY);
+	fd = SAFE_OPEN("data_file", O_WRONLY | O_CREAT, 0666);
+	SAFE_WRITE(1, fd, buf, sizeof(buf));
+	SAFE_CLOSE(fd);
+	fd = SAFE_OPEN("data_file", O_RDONLY);
 }
 
 static void cleanup(void)
 {
-	if (fd > 0)
-		close(fd);
-
-	tst_rmdir();
+	if (fd >= 0)
+		SAFE_CLOSE(fd);
 }
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run,
+	.needs_tmpdir = 1,
+};
Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/2 Add new test cases to syscalls/readv01

Split the original test scenario into two test cases and add:
- read into buffers bigger than input file
- read into multiple buffers
- read into non-NULL buffer with size = 0 (test for kernel commit 19f18459)

Also use guarded buffers in all IO vectors. Fixes #382

---
 testcases/kernel/syscalls/readv/readv01.c | 90 +++++++++++++++--------
 1 file changed, 60 insertions(+), 30 deletions(-)
testcases/kernel/syscalls/readv/readv01.c +60 -30
index ad0ab187b..fc17100eb 100644
@@ -20,57 +20,78 @@
 
 #include "tst_test.h"
 
+/* Note: multi_iovec test assumes CHUNK is divisible by 4 */
 #define	CHUNK		64
 
 static char buf[CHUNK];
+static struct iovec *rd_iovec, *big_iovec, *multi_iovec, *lockup_iovec;
+static int fd;
 
-static struct iovec rd_iovec[] = {
-	{buf, CHUNK},
-	{NULL, 0},
-	{NULL, 0},
+static struct testcase {
+	struct iovec **iov;
+	int iov_count, exp_ret;
+	const char *name;
+} testcase_list[] = {
+	{&rd_iovec, 0, 0, "readv() with 0 I/O vectors"},
+	{&rd_iovec, 3, CHUNK, "readv() with NULL I/O vectors"},
+	{&big_iovec, 2, CHUNK, "readv() with too big I/O vectors"},
+	{&multi_iovec, 2, 3*CHUNK/4, "readv() with multiple I/O vectors"},
+	{&lockup_iovec, 2, CHUNK, "readv() with zero-len buffer"},
 };
 
-static int fd;
-
-static void run(void)
+static void test_readv(unsigned int n)
 {
-	int i, fail;
-	char *vec;
+	int i, fpos, fail = 0;
+	size_t j;
+	char *ptr;
+	const struct testcase *tc = testcase_list + n;
+	struct iovec *vec;
 
 	SAFE_LSEEK(fd, 0, SEEK_SET);
+	vec = *tc->iov;
 
-	if (readv(fd, rd_iovec, 0) == -1)
-		tst_res(TFAIL | TERRNO, "readv failed unexpectedly");
-	else
-		tst_res(TPASS, "readv read 0 io vectors");
+	for (i = 0; i < tc->iov_count; i++)
+		if (vec[i].iov_base && vec[i].iov_len)
+			memset(vec[i].iov_base, 0, vec[i].iov_len);
+
+	TEST(readv(fd, vec, tc->iov_count));
+
+	if (TST_RET == -1)
+		tst_res(TFAIL | TTERRNO, "readv() failed unexpectedly");
+	else if (TST_RET < 0)
+		tst_res(TFAIL | TTERRNO, "readv() returned invalid value");
+	else if (TST_RET != tc->exp_ret)
+		tst_res(TFAIL, "readv() returned unexpected value %ld",
+			TST_RET);
 
-	memset(rd_iovec[0].iov_base, 0x00, CHUNK);
+	if (TST_RET != tc->exp_ret)
+		return;
 
-	if (readv(fd, rd_iovec, 3) != CHUNK) {
-		tst_res(TFAIL, "readv failed reading %d bytes, "
-			 "followed by two NULL vectors", CHUNK);
-	} else {
-		fail = 0;
-		vec = rd_iovec[0].iov_base;
+	tst_res(TPASS, "%s", tc->name);
 
-		for (i = 0; i < CHUNK; i++) {
-			if (vec[i] != 0x42)
+	for (i = 0, fpos = 0; i < tc->iov_count; i++) {
+		ptr = vec[i].iov_base;
+
+		for (j = 0; j < vec[i].iov_len; j++, fpos++) {
+			if (ptr[j] != (fpos < tc->exp_ret ? 0x42 : 0))
 				fail++;
 		}
-
-		if (fail)
-			tst_res(TFAIL, "Wrong buffer content");
-		else
-			tst_res(TPASS, "readv passed reading %d bytes "
-			         "followed by two NULL vectors", CHUNK);
 	}
+
+	if (fail)
+		tst_res(TFAIL, "Wrong buffer content");
+	else
+		tst_res(TPASS, "readv() correctly read %d bytes ", tc->exp_ret);
 }
 
 static void setup(void)
 {
+	/* replace the default NULL pointer with end of guarded buffer */
+	lockup_iovec[0].iov_base = rd_iovec[0].iov_base + rd_iovec[0].iov_len;
+
 	memset(buf, 0x42, sizeof(buf));
 
-	fd = SAFE_OPEN("data_file", O_WRONLY | O_CREAT, 0666);
+	fd = SAFE_OPEN("data_file", O_WRONLY | O_CREAT | O_TRUNC, 0666);
 	SAFE_WRITE(1, fd, buf, sizeof(buf));
 	SAFE_CLOSE(fd);
 	fd = SAFE_OPEN("data_file", O_RDONLY);
@@ -85,6 +106,15 @@ static void cleanup(void)
 static struct tst_test test = {
 	.setup = setup,
 	.cleanup = cleanup,
-	.test_all = run,
+	.test = test_readv,
+	.tcnt = ARRAY_SIZE(testcase_list),
 	.needs_tmpdir = 1,
+	.timeout = 15,
+	.bufs = (struct tst_buffers[]) {
+		{&rd_iovec, .iov_sizes = (int[]){CHUNK, 0, 0, -1}},
+		{&big_iovec, .iov_sizes = (int[]){2*CHUNK, CHUNK, -1}},
+		{&multi_iovec, .iov_sizes = (int[]){CHUNK/4, CHUNK/2, -1}},
+		{&lockup_iovec, .iov_sizes = (int[]){0, CHUNK, -1}},
+		{}
+	}
 };
Signature · 1 line
2.25.1
#2 lore raw

PATCH 2/2 Add new test cases to syscalls/readv01

Hi!

Quoted 56 lines

Split the original test scenario into two test cases and add:
- read into buffers bigger than input file
- read into multiple buffers
- read into non-NULL buffer with size = 0 (test for kernel commit 19f18459)

Also use guarded buffers in all IO vectors. Fixes #382

---
 testcases/kernel/syscalls/readv/readv01.c | 90 +++++++++++++++--------
 1 file changed, 60 insertions(+), 30 deletions(-)
testcases/kernel/syscalls/readv/readv01.c +19 -9
index ad0ab187b..fc17100eb 100644
@@ -20,57 +20,78 @@
 
 #include "tst_test.h"
 
+/* Note: multi_iovec test assumes CHUNK is divisible by 4 */
 #define	CHUNK		64
 
 static char buf[CHUNK];
+static struct iovec *rd_iovec, *big_iovec, *multi_iovec, *lockup_iovec;
+static int fd;
 
-static struct iovec rd_iovec[] = {
-	{buf, CHUNK},
-	{NULL, 0},
-	{NULL, 0},
+static struct testcase {
+	struct iovec **iov;
+	int iov_count, exp_ret;
+	const char *name;
+} testcase_list[] = {
+	{&rd_iovec, 0, 0, "readv() with 0 I/O vectors"},
+	{&rd_iovec, 3, CHUNK, "readv() with NULL I/O vectors"},
+	{&big_iovec, 2, CHUNK, "readv() with too big I/O vectors"},
+	{&multi_iovec, 2, 3*CHUNK/4, "readv() with multiple I/O vectors"},
+	{&lockup_iovec, 2, CHUNK, "readv() with zero-len buffer"},
 };
 
-static int fd;
-
-static void run(void)
+static void test_readv(unsigned int n)
 {
-	int i, fail;
-	char *vec;
+	int i, fpos, fail = 0;
+	size_t j;
+	char *ptr;
+	const struct testcase *tc = testcase_list + n;
+	struct iovec *vec;
 

testcases/kernel/syscalls/readv/readv01.c line 55

Quoted 10 lines
testcases/kernel/syscalls/readv/readv01.c +4 -4
 	SAFE_LSEEK(fd, 0, SEEK_SET);
+	vec = *tc->iov;
 
-	if (readv(fd, rd_iovec, 0) == -1)
-		tst_res(TFAIL | TERRNO, "readv failed unexpectedly");
-	else
-		tst_res(TPASS, "readv read 0 io vectors");
+	for (i = 0; i < tc->iov_count; i++)
+		if (vec[i].iov_base && vec[i].iov_len)
+			memset(vec[i].iov_base, 0, vec[i].iov_len);

This is minor however LKML coding style mandates curly braces around multiline blocks, so the for () loop should be followed by them.

Quoted 39 lines
testcases/kernel/syscalls/readv/readv01.c +18 -15
+	TEST(readv(fd, vec, tc->iov_count));
+
+	if (TST_RET == -1)
+		tst_res(TFAIL | TTERRNO, "readv() failed unexpectedly");
+	else if (TST_RET < 0)
+		tst_res(TFAIL | TTERRNO, "readv() returned invalid value");
+	else if (TST_RET != tc->exp_ret)
+		tst_res(TFAIL, "readv() returned unexpected value %ld",
+			TST_RET);
 
-	memset(rd_iovec[0].iov_base, 0x00, CHUNK);
+	if (TST_RET != tc->exp_ret)
+		return;
 
-	if (readv(fd, rd_iovec, 3) != CHUNK) {
-		tst_res(TFAIL, "readv failed reading %d bytes, "
-			 "followed by two NULL vectors", CHUNK);
-	} else {
-		fail = 0;
-		vec = rd_iovec[0].iov_base;
+	tst_res(TPASS, "%s", tc->name);
 
-		for (i = 0; i < CHUNK; i++) {
-			if (vec[i] != 0x42)
+	for (i = 0, fpos = 0; i < tc->iov_count; i++) {
+		ptr = vec[i].iov_base;
+
+		for (j = 0; j < vec[i].iov_len; j++, fpos++) {
+			if (ptr[j] != (fpos < tc->exp_ret ? 0x42 : 0))
 				fail++;
 		}
-
-		if (fail)
-			tst_res(TFAIL, "Wrong buffer content");
-		else
-			tst_res(TPASS, "readv passed reading %d bytes "
-			         "followed by two NULL vectors", CHUNK);
 	}
+

testcases/kernel/syscalls/readv/readv01.c line 90

Quoted 10 lines
testcases/kernel/syscalls/readv/readv01.c +6 -0
+	if (fail)
+		tst_res(TFAIL, "Wrong buffer content");
+	else
+		tst_res(TPASS, "readv() correctly read %d bytes ", tc->exp_ret);
 }
 
 static void setup(void)
 {
+	/* replace the default NULL pointer with end of guarded buffer */
+	lockup_iovec[0].iov_base = rd_iovec[0].iov_base + rd_iovec[0].iov_len;

We do have tst_get_bad_addr() for this.

Maybe we can even patch the buffer library to produce bad pointer when we pass negative .size.

Quoted 8 lines
testcases/kernel/syscalls/readv/readv01.c +1 -1
 	memset(buf, 0x42, sizeof(buf));
 
-	fd = SAFE_OPEN("data_file", O_WRONLY | O_CREAT, 0666);
+	fd = SAFE_OPEN("data_file", O_WRONLY | O_CREAT | O_TRUNC, 0666);
 	SAFE_WRITE(1, fd, buf, sizeof(buf));
 	SAFE_CLOSE(fd);
 	fd = SAFE_OPEN("data_file", O_RDONLY);

testcases/kernel/syscalls/readv/readv01.c static void cleanup(void) · line 112

Quoted 8 lines
testcases/kernel/syscalls/readv/readv01.c +3 -1
@@ -85,6 +106,15 @@ static void cleanup(void)
 static struct tst_test test = {
 	.setup = setup,
 	.cleanup = cleanup,
-	.test_all = run,
+	.test = test_readv,
+	.tcnt = ARRAY_SIZE(testcase_list),
 	.needs_tmpdir = 1,
+	.timeout = 15,

What's the problem with default timeout?

Quoted 14 lines
testcases/kernel/syscalls/readv/readv01.c +7 -0
+	.bufs = (struct tst_buffers[]) {
+		{&rd_iovec, .iov_sizes = (int[]){CHUNK, 0, 0, -1}},
+		{&big_iovec, .iov_sizes = (int[]){2*CHUNK, CHUNK, -1}},
+		{&multi_iovec, .iov_sizes = (int[]){CHUNK/4, CHUNK/2, -1}},
+		{&lockup_iovec, .iov_sizes = (int[]){0, CHUNK, -1}},
+		{}
+	}
 };
Signature · 5 lines
2.25.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]