[PATCH v1] tests: getcwd.test

Jay Joshi jay.r.joshi100 at gmail.com
Sat Mar 19 13:04:34 UTC 2016


On Sat, Mar 19, 2016 at 5:07 AM, Dmitry V. Levin <ldv at 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.joshi100 at gmail.com>
>> Date: Tue, 15 Mar 2016 16:41:39 +0530
>> Subject: [PATCH] tests: add getcwd.test
>
>> +  res = syscall(__NR_getcwd, cur_dir, sizeof(cur_dir));
>> +
>> +  if (res != 0) {
>> +    printf("getcwd(\"%s\", %zu) = %ld\n", cur_dir, sizeof(cur_dir), res);
>
> If cur_dir contains special characters, then this output won't match the
> output produced by strace.
>

Yes, thanks.
It seems that "uname" will have similar problems.

>> +  } else {
>> +    perror_msg_and_fail("getcwd");
>> +  }
>> +
>> +  if (rmdir(cur_dir))
>> +    perror_msg_and_fail("rmdir");
>> +
>> +  syscall(__NR_getcwd, cur_dir, sizeof(cur_dir));
>> +
>> +  printf("getcwd(%p, %zu) = -1 ENOENT (%m)\n", cur_dir, sizeof(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..c744c0e
>> --- /dev/null
>> +++ b/tests/getcwd.test
>> @@ -0,0 +1,11 @@
>> +#!/bin/sh
>
> It would be a good idea to add a comment what's being tested here.
>
> This is much better than the first edition, but still looks a bit
> overcomplicated.  For example, wouldn't a check for ERANGE instead of
> ENOENT make the whole test simpler?  You won't need all these
> mkdir/chdir/rmdir calls.

Indeed. Updated patch is pasted below.

>From 21f07a4453e72fd1e54b5147e3af0dd813f2657a Mon Sep 17 00:00:00 2001
From: JayRJoshi <jay.r.joshi100 at gmail.com>
Date: Sat, 19 Mar 2016 18:15:53 +0530
Subject: [PATCH] tests: getcwd.test added and print_escaped_string function
 added to libtests

---
 tests/.gitignore             |  1 +
 tests/Makefile.am            |  3 ++
 tests/getcwd.c               | 43 ++++++++++++++++++++++++
 tests/getcwd.test            | 11 ++++++
 tests/print_escaped_string.c | 80 ++++++++++++++++++++++++++++++++++++++++++++
 tests/tests.h                |  3 ++
 6 files changed, 141 insertions(+)
 create mode 100644 tests/getcwd.c
 create mode 100755 tests/getcwd.test
 create mode 100644 tests/print_escaped_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..7108177 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -49,6 +49,7 @@ libtests_a_SOURCES = \
  tail_alloc.c \
  tests.h \
  tprintf.c \
+ print_escaped_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..7d1ea5b
--- /dev/null
+++ b/tests/getcwd.c
@@ -0,0 +1,43 @@
+#include "tests.h"
+
+#include <sys/syscall.h>
+
+#ifdef __NR_getcwd
+
+# include <stdio.h>
+# include <unistd.h>
+# include <errno.h>
+# include <linux/limits.h>
+
+#define sample_dir "getcwd.sample_dir"
+
+int
+main(void)
+{
+  long res;
+  char cur_dir[PATH_MAX + 1];
+
+  res = syscall(__NR_getcwd, cur_dir, sizeof(cur_dir));
+
+  if (res != 0) {
+    printf("getcwd(\"");
+    print_escaped_string(cur_dir);
+    printf("\", %zu) = %ld\n", sizeof(cur_dir), res);
+  } else {
+    perror_msg_and_fail("getcwd");
+  }
+
+  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..c744c0e
--- /dev/null
+++ b/tests/getcwd.test
@@ -0,0 +1,11 @@
+#!/bin/sh
+
+. "${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_escaped_string.c b/tests/print_escaped_string.c
new file mode 100644
index 0000000..68a097c
--- /dev/null
+++ b/tests/print_escaped_string.c
@@ -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);
+}
diff --git a/tests/tests.h b/tests/tests.h
index 826f8b2..0811b11 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 escaped string */
+void print_escaped_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




More information about the Strace-devel mailing list