On Sat, Mar 19, 2016 at 10:06 PM, Dmitry V. Levin <l...@altlinux.org> wrote:
> On Sat, Mar 19, 2016 at 06:34:34PM +0530, Jay Joshi wrote:
>> On Sat, Mar 19, 2016 at 5:07 AM, Dmitry V. Levin <l...@altlinux.org> wrote:
>> > On Fri, Mar 18, 2016 at 10:14:26AM +0530, Jay Joshi wrote:
>> >> >From 6785e2dc6f15104537d2e27387279a04c9bcfb25 Mon Sep 17 00:00:00 2001
>> >> From: JayRJoshi <jay.r.joshi...@gmail.com>
>> >> Date: Tue, 15 Mar 2016 16:41:39 +0530
>> >> Subject: [PATCH] tests: add getcwd.test
>> +int
>> +main(void)
>> +{
>> +  long res;
>> +  char cur_dir[PATH_MAX + 1];
>> +
>> +  res = syscall(__NR_getcwd, cur_dir, sizeof(cur_dir));
>> +
>> +  if (res != 0) {
>
> Why do you check for != 0?

It should be res<=0. Just a safety check against bugs, like system
might allow dir path bigger than PATH_MAX.

>> diff --git a/tests/getcwd.test b/tests/getcwd.test
>> new file mode 100755
>> index 0000000..c744c0e
>> --- /dev/null
>> +++ b/tests/getcwd.test
>> @@ -0,0 +1,11 @@
>> +#!/bin/sh
>
> Please add a short comment what's being tested here.
>
>> --- /dev/null
>> +++ b/tests/print_escaped_string.c
>
> Please rename print_escaped_string -> print_quoted_string
>
>> @@ -0,0 +1,80 @@
>> +#include "tests.h"
>> +
>> +#include <stdio.h>
>> +#include <stdlib.h>
>> +
>> +/* Modified from string_quote() from util.c
>> + * Assumes str is NUL-terminated.
>> + */
>> +
>> +void
>> +print_escaped_string(const char *str)
>> +{
>> + const unsigned char *ustr = (const unsigned char *) str;
>> + size_t size = sizeof(str);
>> +  size_t esc_size = 4 * size;
>> +  char *esc_str, *s;
>> + s = esc_str = (char *) malloc((char) esc_size);
>> +
>> +  if(s == NULL)
>> +    perror_msg_and_fail("malloc(%zu)", esc_size);
>> +
>> + unsigned int i=0;
>> + int c;
>> +
>> + while ((c = ustr[i++]) != '\0') {
>> + switch (c) {
>> + case '\"': case '\\':
>> + *s++ = '\\';
>> + *s++ = c;
>> + break;
>> + case '\f':
>> + *s++ = '\\';
>> + *s++ = 'f';
>> + break;
>> + case '\n':
>> + *s++ = '\\';
>> + *s++ = 'n';
>> + break;
>> + case '\r':
>> + *s++ = '\\';
>> + *s++ = 'r';
>> + break;
>> + case '\t':
>> + *s++ = '\\';
>> + *s++ = 't';
>> + break;
>> + case '\v':
>> + *s++ = '\\';
>> + *s++ = 'v';
>> + break;
>> + default:
>> + if (c >= ' ' && c <= 0x7e)
>> + *s++ = c;
>> + else {
>> + /* if str contains characters from ' ' to '~', than
>> +           * else won't be required,
>> +           * also esc_size would be 2 * size - 1.
>> +           */
>> + *s++ = '\\';
>> + if (i + 1 < size
>> +    && ustr[i + 1] >= '0'
>> +    && ustr[i + 1] <= '9'
>> + ) {
>> + *s++ = '0' + (c >> 6);
>> + *s++ = '0' + ((c >> 3) & 0x7);
>> + } else {
>> + if ((c >> 3) != 0) {
>> + if ((c >> 6) != 0)
>> + *s++ = '0' + (c >> 6);
>> + *s++ = '0' + ((c >> 3) & 0x7);
>> + }
>> + }
>> + *s++ = '0' + (c & 0x7);
>> + }
>> + break;
>> + }
>> +  }
>> +  *s = '\0';
>> +  printf("%s",esc_str);
>> +}
>
> Unlike string_quote(), this is not indented properly and therefore
> hard to read.

I'm using TAB for indentation. Pasting it here is creating formatting
problem. That's why I'm attaching the patch.
From 41e694ab0d2b72ba84dffa55f318ce6921129305 Mon Sep 17 00:00:00 2001
From: JayRJoshi <jay.r.joshi...@gmail.com>
Date: Sun, 20 Mar 2016 00:19:02 +0530
Subject: [PATCH] tests: add getcwd.test, add print_quoted_string function to
 libtests

* tests/tests.h (print_quoted_string): New prototype.
* tests/print_quoted_string.c: New file.
* tests/Makefile.am (libtests_a_SOURCES): Add it.
* tests/getcwd.c: New file.
* tests/getcwd.test: New test.
* tests/.gitignore: Add getcwd.
* tests/Makefile.am (check_PROGRAMS): Likewise.
(TESTS): Add getcwd.test.
---
 tests/.gitignore            |  1 +
 tests/Makefile.am           |  3 +++
 tests/getcwd.c              | 40 +++++++++++++++++++++++++++++
 tests/getcwd.test           | 13 ++++++++++
 tests/print_quoted_string.c | 62 +++++++++++++++++++++++++++++++++++++++++++++
 tests/tests.h               |  3 +++
 6 files changed, 122 insertions(+)
 create mode 100644 tests/getcwd.c
 create mode 100755 tests/getcwd.test
 create mode 100644 tests/print_quoted_string.c

diff --git a/tests/.gitignore b/tests/.gitignore
index 449af18..d1fb6d4 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -42,6 +42,7 @@ fstat64
 fstatat64
 ftruncate
 ftruncate64
+getcwd
 getdents
 getdents64
 getrandom
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3005382..9debcdd 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -49,6 +49,7 @@ libtests_a_SOURCES = \
 	tail_alloc.c \
 	tests.h \
 	tprintf.c \
+	print_quoted_string.c \
 	# end of libtests_a_SOURCES
 libtests_a_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
 check_LIBRARIES = libtests.a
@@ -92,6 +93,7 @@ check_PROGRAMS = \
 	fstatat64 \
 	ftruncate \
 	ftruncate64 \
+	getcwd \
 	getdents \
 	getdents64 \
 	getrandom \
@@ -268,6 +270,7 @@ TESTS = \
 	fstatat64.test \
 	ftruncate.test \
 	ftruncate64.test \
+	getcwd.test \
 	getdents.test \
 	getdents64.test \
 	getrandom.test \
diff --git a/tests/getcwd.c b/tests/getcwd.c
new file mode 100644
index 0000000..3d30559
--- /dev/null
+++ b/tests/getcwd.c
@@ -0,0 +1,40 @@
+#include "tests.h"
+
+#include <sys/syscall.h>
+
+#ifdef __NR_getcwd
+
+# include <stdio.h>
+# include <unistd.h>
+# include <errno.h>
+# include <sys/param.h>
+
+int
+main(void)
+{
+  long res;
+  char cur_dir[PATH_MAX + 1];
+
+  res = syscall(__NR_getcwd, cur_dir, sizeof(cur_dir));
+
+  if (res <= 0)
+    perror_msg_and_fail("getcwd");
+
+  printf("getcwd(\"");
+  print_quoted_string(cur_dir);
+  printf("\", %zu) = %ld\n", sizeof(cur_dir), res);
+
+  syscall(__NR_getcwd, cur_dir, 0);
+
+  printf("getcwd(%p, 0) = -1 ERANGE (%m)\n", cur_dir);
+
+  puts("+++ exited with 0 +++");
+
+  return 0;
+}
+
+#else
+
+SKIP_MAIN_UNDEFINED("__NR_getcwd");
+
+#endif
diff --git a/tests/getcwd.test b/tests/getcwd.test
new file mode 100755
index 0000000..1667d67
--- /dev/null
+++ b/tests/getcwd.test
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+# Check getcwd syscall decoding
+
+. "${srcdir=.}/init.sh"
+
+run_prog > /dev/null
+OUT="$LOG.out"
+run_strace -egetcwd -a18 $args > $OUT
+match_diff "$LOG" "$OUT"
+rm -f "$OUT"
+
+exit 0
diff --git a/tests/print_quoted_string.c b/tests/print_quoted_string.c
new file mode 100644
index 0000000..6455ae0
--- /dev/null
+++ b/tests/print_quoted_string.c
@@ -0,0 +1,62 @@
+#include "tests.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+/* Modified from string_quote() from util.c
+ * Assumes str is NUL-terminated.
+*/
+
+void
+print_quoted_string(const char *str)
+{
+	unsigned int i=0;
+	int c;
+
+	while ((c = str[i++]) != '\0') {
+		switch (c) {
+			case '\"':
+				printf("\\\"");
+				break;
+			case '\\':
+				printf("\\\\");
+				break;
+			case '\f':
+				printf("\\f");
+				break;
+			case '\n':
+				printf("\\n");
+				break;
+			case '\r':
+				printf("\\r");
+				break;
+			case '\t':
+				printf("\\t");
+				break;
+			case '\v':
+				printf("\\v");
+				break;
+			default:
+				if (c >= ' ' && c <= 0x7e)
+					printf("%c",c);
+				else {
+					printf("\\");
+					if (str[i + 1] >= '0' && str[i + 1] <= '9') {
+						/* Print \octal */
+						printf("%c%c", '0' + (c >> 6), '0' + ((c >> 3) & 0x7));
+					} else {
+						/* Print \[[o]o]o */
+						if ((c >> 3) != 0) {
+							if ((c >> 6) != 0)
+								printf("%c", '0' + (c >> 6));
+							printf("%c", '0' + ((c >> 3) & 0x7));
+						}
+					}
+					printf("%c", '0' + (c & 0x7));
+				}
+				break;
+		}
+	}
+
+  printf("\0");
+}
diff --git a/tests/tests.h b/tests/tests.h
index 826f8b2..4ee250e 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -75,6 +75,9 @@ const char *hexquote_strndup(const char *, size_t);
 /* Return inode number of socket descriptor. */
 unsigned long inode_of_sockfd(int);
 
+/* Print string in escaped format */
+void print_quoted_string(const char *str);
+
 # define ARRAY_SIZE(arg) ((unsigned int) (sizeof(arg) / sizeof((arg)[0])))
 # define LENGTH_OF(arg) ((unsigned int) sizeof(arg) - 1)
 
-- 
1.9.1

------------------------------------------------------------------------------
Transform Data into Opportunity.
Accelerate data analysis in your applications with
Intel Data Analytics Acceleration Library.
Click to learn more.
http://pubads.g.doubleclick.net/gampad/clk?id=278785231&iu=/4140
_______________________________________________
Strace-devel mailing list
Strace-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/strace-devel

Reply via email to