Skip to contentSkip to conversation

pwritev203.c: Testcase to test RWF_APPEND

PATCH New tarun 2 participants 2 messages Apr 1, 2019 – Apr 5, 2019

lore
Share & tools
lore

tarun

author
#0 lore raw

PATCH v2 1/1 pwritev203.c: Testcase to test RWF_APPEND

From: "Tarun.T.U" <[email protected]>

---
 include/lapi/pwritev2.h                       |   7 ++
 runtest/syscalls                              |   2 +
 testcases/kernel/syscalls/pwritev2/.gitignore |   2 +
 .../kernel/syscalls/pwritev2/pwritev203.c     | 112 ++++++++++++++++++
 4 files changed, 123 insertions(+)
 create mode 100644 testcases/kernel/syscalls/pwritev2/pwritev203.c
include/lapi/pwritev2.h +7 -0
index 305e48e02..214279693 100644
@@ -23,4 +23,11 @@ ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset,
 }
 #endif
 
+/*
+ * Flag for pwritev2
+ */
+#ifndef RWF_APPEND
+# define RWF_APPEND		0x00000010U
+#endif
+
 #endif /* PWRITEV2_H */
runtest/syscalls +2 -0
index cf8189ebd..1a80c53ba 100644
@@ -922,6 +922,8 @@ pwritev201 pwritev201
 pwritev201_64 pwritev201_64
 pwritev202 pwritev202
 pwritev202_64 pwritev202_64
+pwritev203 pwritev203
+pwritev203_64 pwritev203_64
 
 quotactl01 quotactl01
 quotactl02 quotactl02
testcases/kernel/syscalls/pwritev2/.gitignore +2 -0
index 40030d9e5..9b440a879 100644
@@ -2,3 +2,5 @@
 /pwritev201_64
 /pwritev202
 /pwritev202_64
+/pwritev203
+/pwritev203_64
testcases/kernel/syscalls/pwritev2/pwritev203.c new file +112 -0

112 lines not on this page · raw message

Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/1 pwritev203.c: Testcase to test RWF_APPEND

Hi!

Quoted 107 lines
---
 include/lapi/pwritev2.h                       |   7 ++
 runtest/syscalls                              |   2 +
 testcases/kernel/syscalls/pwritev2/.gitignore |   2 +
 .../kernel/syscalls/pwritev2/pwritev203.c     | 112 ++++++++++++++++++
 4 files changed, 123 insertions(+)
 create mode 100644 testcases/kernel/syscalls/pwritev2/pwritev203.c
include/lapi/pwritev2.h +7 -0
index 305e48e02..214279693 100644
@@ -23,4 +23,11 @@ ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset,
 }
 #endif
 
+/*
+ * Flag for pwritev2
+ */
+#ifndef RWF_APPEND
+# define RWF_APPEND		0x00000010U
+#endif
+
 #endif /* PWRITEV2_H */
runtest/syscalls +2 -0
index cf8189ebd..1a80c53ba 100644
@@ -922,6 +922,8 @@ pwritev201 pwritev201
 pwritev201_64 pwritev201_64
 pwritev202 pwritev202
 pwritev202_64 pwritev202_64
+pwritev203 pwritev203
+pwritev203_64 pwritev203_64
 
 quotactl01 quotactl01
 quotactl02 quotactl02
testcases/kernel/syscalls/pwritev2/.gitignore +2 -0
index 40030d9e5..9b440a879 100644
@@ -2,3 +2,5 @@
 /pwritev201_64
 /pwritev202
 /pwritev202_64
+/pwritev203
+/pwritev203_64
testcases/kernel/syscalls/pwritev2/pwritev203.c new file +52 -0
new file mode 100644
index 000000000..721b9777e
@@ -0,0 +1,112 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) Zilogic Systems Pvt. Ltd., 2019
+ * Email: [email protected]
+ */
+
+/*
+ * Test pwritev2
+ *
+ * This code test the behaviour of  RWF_APPEND in pwritev2
+ *
+ * case 1:
+ * With offset of zero and RWF_APPEND in pwritev2
+ * Excepted result: Current file offset should not update.
+ *
+ * case 2:
+ * With offset of "5" and RWF_APPEND in pwritev2
+ * Excepted result: Current file offset should not update.
+ *
+ * case 3:
+ * With offset of -1 and RWF_APPEND in pwritev2
+ * Excepted result: Current file offset should get update to current value.
+ *
+ * Testcases:
+ * Test1: Getting offset value from written file.
+ * Test2: Getting written data from file to verify written data by using LSEEK.
+ *
+ * Minimum Kernel version required is 4.16.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <sys/uio.h>
+
+#include <string.h>
+#include "tst_test.h"
+#include "lapi/pwritev2.h"
+
+#define TESTFILE0 "testfile0"
+#define TESTFILE1 "testfile1"
+#define TESTFILE2 "testfile2"
+#define MODE 0644
+#define OLD_OFFSET 14
+#define UPDATED_OFFSET 25
+
+static struct test_case {
+	char *filename;
+	off_t offset;
+	off_t exp_offset;
+
+} tcases[] = {
+	{.filename = TESTFILE0, .offset = 0, .exp_offset = OLD_OFFSET},

testcases/kernel/syscalls/pwritev2/pwritev203.c line 62

Quoted 10 lines
testcases/kernel/syscalls/pwritev2/pwritev203.c new file +10 -0
+	{.filename = TESTFILE1, .offset = 5, .exp_offset = OLD_OFFSET},
+	{.filename = TESTFILE2, .offset = -1, .exp_offset = UPDATED_OFFSET},
+};
+
+static void run_test(unsigned int i)
+{
+	struct iovec vector;
+	struct test_case *tc = &tcases[i];
+	char *std_data = "Linux Torvalds";
+	char *append_data = "Hello World";

These two should rather be defined as macros, also I'm not sure if it's wise to use anybody's name in the test data, I would do something as:

#define DATA_STR "Data string"
#define APPEND_STR "Append string"

testcases/kernel/syscalls/pwritev2/pwritev203.c line 63

Quoted 1 line
testcases/kernel/syscalls/pwritev2/pwritev203.c new file +1 -0
+	char test_data[strlen(append_data) + 1];

^
This should be sizeof(APPEND_STR)

testcases/kernel/syscalls/pwritev2/pwritev203.c line 73

Quoted 10 lines
testcases/kernel/syscalls/pwritev2/pwritev203.c new file +10 -0
+	off_t cur_offset;
+	int fd;
+
+	memset(test_data, 0, sizeof(test_data));
+
+	fd = SAFE_OPEN(tc->filename, O_RDWR | O_CREAT, MODE);
+	SAFE_WRITE(1, fd, std_data, strlen(std_data));
+
+	vector.iov_base = append_data;
+	vector.iov_len = strlen(append_data);

^
sizeof(APPEND_STR) - 1

testcases/kernel/syscalls/pwritev2/pwritev203.c line 78

Quoted 5 lines
testcases/kernel/syscalls/pwritev2/pwritev203.c new file +5 -0
+
+	TEST(pwritev2(fd, &vector, 1, tc->offset, RWF_APPEND));
+
+	if (TST_ERR == EOPNOTSUPP) {
+		tst_brk(TBROK | TERRNO | TINFO,

Combination of TBROK | TINFO is not valid, moreover EOPNOTSUPP should be mapped to TCONF.

testcases/kernel/syscalls/pwritev2/pwritev203.c line 80

Quoted 2 lines
testcases/kernel/syscalls/pwritev2/pwritev203.c new file +2 -0
+			"kernel is not supported for RWF_APPEND flag");
+	} else if (TST_RET == -1) {

There is no need for else branch here, since we call tst_brk() that exits the test in the previous if.

Also we should really check that the call returned amount of data written, so this should rather be:

	if (TST_RET != vector.iov_len) {
		tst_res(TFAIL | TTERRNO, "pwritev2() returned %ld", TST_RET);
		return;
	}

testcases/kernel/syscalls/pwritev2/pwritev203.c line 90

Quoted 10 lines
testcases/kernel/syscalls/pwritev2/pwritev203.c new file +10 -0
+		tst_res(TFAIL | TTERRNO, "pwritev2() returned with %ld",
+			TST_RET);
+		return;
+	}
+
+	cur_offset = SAFE_LSEEK(fd, 0, SEEK_CUR);
+	if (cur_offset != tc->exp_offset)
+		tst_brk(TFAIL,
+			"Returned offset %ld inspite of RWF_APPEND flag",
+			tc->exp_offset);

No tst_brk(TFAIL, ...) please. This should be tst_res(TFAIL, ...) followed by return if you want to exit current test iteration.

testcases/kernel/syscalls/pwritev2/pwritev203.c line 98

Quoted 8 lines
testcases/kernel/syscalls/pwritev2/pwritev203.c new file +8 -0
+	else
+		tst_res(TPASS, "Returned offset %ld as expected",
+			tc->exp_offset);
+
+
+	SAFE_LSEEK(fd, -(strlen(append_data)), SEEK_END);
+
+	SAFE_READ(0, fd, test_data, sizeof(test_data));

We can use SAFE_PREAD() instead of the seek and read here.

testcases/kernel/syscalls/pwritev2/pwritev203.c line 105

Quoted 7 lines
testcases/kernel/syscalls/pwritev2/pwritev203.c new file +7 -0
+	if (strcmp(append_data, test_data) != 0)
+		tst_brk(TFAIL, "Data not appended as expected");
+	else
+		tst_res(TPASS, "Data appended as expected");
+
+	SAFE_CLOSE(fd);
+	SAFE_UNLINK(tc->filename);

As we create and unlink the file in each test iteration the name could be defined as a constant, there is no need to carry it in the test_case structure.

Quoted 13 lines
testcases/kernel/syscalls/pwritev2/pwritev203.c new file +7 -0
+}
+
+static struct tst_test test = {
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = run_test,
+	.needs_tmpdir = 1,
+};
Signature · 5 lines
2.17.1


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