[PATCH v1] tests: getcwd.test

Dmitry V. Levin ldv at altlinux.org
Fri Mar 18 23:37:06 UTC 2016


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

getcwd.test seems to be popular nowadays. :)
See also my comments on another getcwd.test submission.

> --- /dev/null
> +++ b/tests/getcwd.c
> @@ -0,0 +1,50 @@
> +#include "tests.h"
> +
> +#include <sys/stat.h>
> +#include <sys/syscall.h>
> +

<nitpick> You can include <sys/stat.h> after the check. </nitpick>

> +#ifdef __NR_getcwd
> +
> +#include <stdio.h>
> +#include <unistd.h>
> +#include <errno.h>

<nitpick> ifdef'ed includes look better when indented. </nitpick>

> +
> +#define sample_dir "getcwd.sample_dir"
> +
> +int
> +main(void)
> +{
> +  long res;
> +  char cur_dir[2048];

2048 is definitely less than current PATH_MAX value.

> +  if (mkdir(sample_dir, S_IRWXU))
> +    perror_msg_and_fail("mkdir");
> +
> +  if (chdir(sample_dir))
> +    perror_msg_and_skip("chdir");

Why mkdir error is fatal but chdir is not?

> +  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.

> +  } 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.


-- 
ldv
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20160319/2457b967/attachment.bin>


More information about the Strace-devel mailing list