[PATCH] tests: chdir.test

Jay Joshi jay.r.joshi100 at gmail.com
Fri Mar 11 17:52:23 UTC 2016


Hello,

This is my first attempt to add test for syscall (here, chdir). Any
suggestion, comment, criticism is welcomed.

>From e989b62ccb2095b2eb7f7f494b19e218398fecaf Mon Sep 17 00:00:00 2001
From: JayRJoshi <jay.r.joshi100 at gmail.com>
Date: Fri, 11 Mar 2016 23:17:48 +0530
Subject: [PATCH] Test for chdir

---
 tests/.gitignore  |  1 +
 tests/Makefile.am |  2 ++
 tests/chdir.c     | 60
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/chdir.test  | 11 ++++++++++
 4 files changed, 74 insertions(+)
 create mode 100644 tests/chdir.c
 create mode 100755 tests/chdir.test

diff --git a/tests/.gitignore b/tests/.gitignore
index e882463..8fd0723 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -15,6 +15,7 @@ attach-p-cmd-cmd
 attach-p-cmd-p
 bpf
 caps
+chdir
 chmod
 clock_nanosleep
 clock_xettime
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 4b48b3f..3ac26a2 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -65,6 +65,7 @@ check_PROGRAMS = \
  attach-p-cmd-p \
  bpf \
  caps \
+ chdir \
  chmod \
  clock_nanosleep \
  clock_xettime \
@@ -235,6 +236,7 @@ TESTS = \
  bexecve.test \
  bpf.test \
  caps.test \
+ chdir.test \
  chmod.test \
  clock_nanosleep.test \
  clock_xettime.test \
diff --git a/tests/chdir.c b/tests/chdir.c
new file mode 100644
index 0000000..fc2ce4e
--- /dev/null
+++ b/tests/chdir.c
@@ -0,0 +1,60 @@
+#include "tests.h"
+
+#include <sys/syscall.h>
+#include <sys/stat.h>
+
+#if defined __NR_chdir
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
+int
+main(void)
+{
+static const char sample_dir[] = "chdir.sample";
+char abs_sample_dir[1024];
+char cur_dir[1024];
+
+if (getcwd(cur_dir,sizeof(cur_dir)) == NULL)
+  perror_msg_and_skip("getcwd");
+if (mkdir(sample_dir, S_IRWXU) == -1)
+  perror_msg_and_fail("mkdir");
+
+int chdir_res = syscall(__NR_chdir, sample_dir);
+
+if (chdir_res == 0) {
+  printf("chdir(\"%s\") = 0\n", sample_dir);
+} else {
+  if (errno == ENOSYS) {
+    printf("chdir(\"%s\") = -1 ENOSYS (%m)\n", sample_dir);
+  } else {
+    perror_msg_and_fail("chdir");
+  }
+}
+
+if (sprintf(abs_sample_dir,"%s/%s",cur_dir,sample_dir) < 0)
+  perror_msg_and_skip("sprintf");
+
+if (rmdir(abs_sample_dir) == -1)
+  perror_msg_and_skip("rmdir");
+
+if (chdir_res == 0) {
+  if (syscall(__NR_chdir, sample_dir) != -1)
+    perror_msg_and_fail("chdir");
+
+  printf("chdir(\"%s\") = -1 ENOENT (%m)\n", sample_dir);
+}
+
+puts("+++ exited with 0 +++");
+return 0;
+}
+
+#else
+
+SKIP_MAIN_UNDEFINED("__NR_chdir")
+
+#endif
diff --git a/tests/chdir.test b/tests/chdir.test
new file mode 100755
index 0000000..aab18dd
--- /dev/null
+++ b/tests/chdir.test
@@ -0,0 +1,11 @@
+#!/bin/bash
+
+. "${srcdir=.}/init.sh"
+
+run_prog > /dev/null
+OUT="$LOG.out"
+run_strace -a8 -echdir $args > "$OUT"
+match_diff "$LOG" "$OUT"
+rm -f "$OUT"
+
+exit 0
-- 
1.9.1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20160311/724a86ee/attachment.html>


More information about the Strace-devel mailing list