[GSOC 2014][PATCH 6/7] JSON: Add test case for syscalls in io.c

Zhu YangMin zym0017d at gmail.com
Mon Jul 21 14:35:28 UTC 2014


A special test file for syscalls in io.c, I tried to make the trace
results as stable as each time we execute the program.

* test/.gitignore: Add json_io.
* test/.Makefile: Add json_io.c to compile list.
* test/json_io.c(newfile): contain test cases for syscalls in io.c,
The only random number would be the pipe numver when calling syscalls
like spice,vmsplice.
---
 test/.gitignore |  1 +
 test/Makefile   |  2 +-
 test/json_io.c  | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 93 insertions(+), 1 deletion(-)
 create mode 100644 test/json_io.c

diff --git a/test/.gitignore b/test/.gitignore
index c73b64a..f488409 100644
--- a/test/.gitignore
+++ b/test/.gitignore
@@ -1,3 +1,4 @@
+json_io
 vfork
 fork
 sig
diff --git a/test/Makefile b/test/Makefile
index cc7d47a..d19b187 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -3,7 +3,7 @@ CFLAGS += -Wall
 PROGS = \
     vfork fork sig skodic clone leaderkill childthread \
     sigkill_rain wait_must_be_interruptible threaded_execve \
-    mtd ubi select sigreturn
+    mtd ubi select sigreturn json_io
 
 all: $(PROGS)
 
diff --git a/test/json_io.c b/test/json_io.c
new file mode 100644
index 0000000..f1e77e9
--- /dev/null
+++ b/test/json_io.c
@@ -0,0 +1,91 @@
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/uio.h>
+#include <sys/sendfile.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <limits.h>
+#include <string.h>
+#include <linux/fs.h>
+
+void test(void)
+{
+	#define BUFSIZE (1000)
+	static char buf[BUFSIZE] = "xm\216\315r\177\0\0 \304\311\315r\177\0\0\0\0\0\0\1\0\0\0\242\10\0\0\1\0\0\0";
+	static char str[] = "helloworld, this_is_a_test_string,\n\n\n. !@$%^$!@, \\{{{\"\"\"]]]}}}%%---%%";
+	int strsz = strlen(str) + 1;
+
+	int fd1 = open("/tmp/strace_GSOC_json_test.txt", O_CLOEXEC|O_CREAT|O_RDWR|O_TRUNC, S_IWUSR|S_IRUSR|S_IROTH|S_IRGRP);
+
+	write(923, buf, 25);
+	write(fd1, buf, 0);
+	write(fd1, str, strsz);
+	write(fd1, str, 10);
+
+	read(923, buf, BUFSIZE);
+	lseek(fd1, 0, SEEK_SET);
+	read(fd1, buf, 0);
+	read(fd1, buf, BUFSIZE);
+	lseek(fd1, -10, SEEK_END);
+	read(fd1, buf, 20);
+
+	static char vecstr[5][50] = {
+		"abcdefgabcdefgabcdefg",
+		"6fgjgbcduifgabcdefgbcdefg",
+		"i890abfghgabcduioefg",
+		"vgyjghuvbbcddfsucdefgdefg",
+		"sfd0-9gcdefgabcdefgbcdefg"};
+
+	struct iovec vecbuf[5];
+	int i;
+	for (i=0; i<5; i++) {
+		vecbuf[i].iov_base = vecstr[i];
+		vecbuf[i].iov_len = strlen(vecstr[i]) + 1;
+	}
+
+	pread(fd1, buf, BUFSIZE, 0);
+	pwrite(fd1, buf, BUFSIZE, 0);
+	preadv(fd1, vecbuf, 5, 0);
+	pwritev(fd1, vecbuf, 5, 0);
+
+	int fd2 = 923;
+	off_t off = 99999346451407343;
+	sendfile(fd1, fd2, NULL, BUFSIZE);
+	sendfile(fd1, -1, &off, BUFSIZE);
+	fd2 = open("/tmp/strace_tmp_file2", O_RDWR | O_CREAT | O_TRUNC, S_IWUSR|S_IRUSR|S_IROTH|S_IRGRP);
+	write(fd2, buf, BUFSIZE);
+	lseek(fd2, 0, SEEK_SET);
+	sendfile(fd1, fd2, &off, BUFSIZE);
+	off = 0;
+	sendfile(fd1, fd2, &off, 100);
+
+	tee(fd1, fd2, 2345, SPLICE_F_MORE | SPLICE_F_MOVE | SPLICE_F_GIFT);
+	tee(fd1, fd2, 2345, SPLICE_F_NONBLOCK | SPLICE_F_MOVE);
+	tee(STDIN_FILENO, STDOUT_FILENO, 99999, SPLICE_F_NONBLOCK);
+
+	off_t o1 = 12;
+	int pfd[2];
+	pipe2(pfd, O_NONBLOCK | O_CLOEXEC);
+	splice(fd1, NULL, fd2, NULL, 100, SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
+	splice(fd1, &o1, pfd[1], NULL, 100, SPLICE_F_MOVE);
+	splice(fd1, &o1, pfd[1], NULL, 100, 8349583);
+
+	vmsplice(923, vecbuf, 5, 23487);
+	vmsplice(pfd[0], vecbuf, 5, SPLICE_F_NONBLOCK|SPLICE_F_MOVE);
+	vmsplice(pfd[1], vecbuf, 5, SPLICE_F_NONBLOCK|SPLICE_F_MOVE);
+}
+
+
+int main(int argc, char *argv[])
+{
+	int cnt = 1;
+	if (argc >= 2)
+		cnt = atoi(argv[1]);
+	while (cnt-- > 0) {
+		test();
+	}
+    return 0;
+}
\ No newline at end of file
-- 
1.9.1





More information about the Strace-devel mailing list