summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/bfs/execdir_path_relative_slash.out19
-rw-r--r--tests/bfs/execdir_path_relative_slash.sh1
-rw-r--r--tests/bfstd.c84
-rw-r--r--tests/getopts.sh6
-rw-r--r--tests/gnu/execdir_self.out1
-rw-r--r--tests/gnu/execdir_self.sh9
-rw-r--r--tests/main.c8
-rw-r--r--tests/posix/exec_sigmask.sh4
-rw-r--r--tests/posix/group_o_group.out19
-rw-r--r--tests/posix/group_o_group.sh3
-rw-r--r--tests/posix/root_order.out4
-rw-r--r--tests/posix/root_order.sh6
-rw-r--r--tests/posix/user_o_user.out19
-rw-r--r--tests/posix/user_o_user.sh3
-rw-r--r--tests/ptyx.c36
-rw-r--r--tests/run.sh7
-rw-r--r--tests/util.sh25
-rw-r--r--tests/xspawn.c31
-rw-r--r--tests/xtouch.c4
19 files changed, 218 insertions, 71 deletions
diff --git a/tests/bfs/execdir_path_relative_slash.out b/tests/bfs/execdir_path_relative_slash.out
new file mode 100644
index 0000000..62b31f6
--- /dev/null
+++ b/tests/bfs/execdir_path_relative_slash.out
@@ -0,0 +1,19 @@
+./a
+./b
+./bar
+./bar
+./basic
+./baz
+./c
+./d
+./e
+./f
+./foo
+./foo
+./foo
+./g
+./h
+./i
+./j
+./k
+./l
diff --git a/tests/bfs/execdir_path_relative_slash.sh b/tests/bfs/execdir_path_relative_slash.sh
new file mode 100644
index 0000000..fb5a924
--- /dev/null
+++ b/tests/bfs/execdir_path_relative_slash.sh
@@ -0,0 +1 @@
+PATH="foo:$PATH" bfs_diff basic -execdir /bin/sh -c 'printf "%s\\n" "$@"' sh {} +
diff --git a/tests/bfstd.c b/tests/bfstd.c
index 685aa6c..6e15e2b 100644
--- a/tests/bfstd.c
+++ b/tests/bfstd.c
@@ -4,7 +4,6 @@
#include "tests.h"
#include "bfstd.h"
-#include "bit.h"
#include "diag.h"
#include <errno.h>
@@ -95,37 +94,74 @@ static void check_wordescs(void) {
/** xstrto*() test cases. */
static void check_strtox(void) {
+ short s;
+ unsigned short us;
+ int i;
+ unsigned int ui;
long l;
+ unsigned long ul;
long long ll;
+ unsigned long long ull;
char *end;
+#define check_strtouerr(err, str, end, base) \
+ do { \
+ bfs_echeck(xstrtous(str, end, base, &us) != 0 && errno == err); \
+ bfs_echeck(xstrtoui(str, end, base, &ui) != 0 && errno == err); \
+ bfs_echeck(xstrtoul(str, end, base, &ul) != 0 && errno == err); \
+ bfs_echeck(xstrtoull(str, end, base, &ull) != 0 && errno == err); \
+ } while (0)
+
+ check_strtouerr(ERANGE, "-1", NULL, 0);
+ check_strtouerr(ERANGE, "-0x1", NULL, 0);
+
+ check_strtouerr(EINVAL, "-", NULL, 0);
+ check_strtouerr(EINVAL, "-q", NULL, 0);
+ check_strtouerr(EINVAL, "-1q", NULL, 0);
+ check_strtouerr(EINVAL, "-0x", NULL, 0);
+
#define check_strtoerr(err, str, end, base) \
- bfs_echeck(xstrtol(str, end, base, &l) != 0 && errno == err); \
- bfs_echeck(xstrtoll(str, end, base, &ll) != 0 && errno == err)
+ do { \
+ bfs_echeck(xstrtos(str, end, base, &s) != 0 && errno == err); \
+ bfs_echeck(xstrtoi(str, end, base, &i) != 0 && errno == err); \
+ bfs_echeck(xstrtol(str, end, base, &l) != 0 && errno == err); \
+ bfs_echeck(xstrtoll(str, end, base, &ll) != 0 && errno == err); \
+ check_strtouerr(err, str, end, base); \
+ } while (0)
check_strtoerr(EINVAL, "", NULL, 0);
check_strtoerr(EINVAL, "", &end, 0);
check_strtoerr(EINVAL, " 1 ", &end, 0);
+ check_strtoerr(EINVAL, " -1", NULL, 0);
check_strtoerr(EINVAL, " 123", NULL, 0);
check_strtoerr(EINVAL, "123 ", NULL, 0);
check_strtoerr(EINVAL, "0789", NULL, 0);
check_strtoerr(EINVAL, "789A", NULL, 0);
check_strtoerr(EINVAL, "0x", NULL, 0);
check_strtoerr(EINVAL, "0x789A", NULL, 10);
-
- if (LLONG_WIDTH == 64) {
- check_strtoerr(ERANGE, "9223372036854775808", NULL, 0);
- }
+ check_strtoerr(EINVAL, "0x-1", NULL, 0);
+
+#define check_strtotype(type, min, max, fmt, fn, str, base, v, n) \
+ do { \
+ if ((n) >= min && (n) <= max) { \
+ bfs_echeck(fn(str, NULL, base, &v) == 0); \
+ bfs_check(v == (type)(n), "%s('%s') == " fmt " (!= " fmt ")", #fn, str, v, (type)(n)); \
+ } else { \
+ bfs_echeck(fn(str, NULL, base, &v) != 0 && errno == ERANGE); \
+ } \
+ } while (0)
#define check_strtoint(str, base, n) \
- if ((n) >= LONG_MIN && (n) <= LONG_MAX) { \
- bfs_echeck(xstrtol(str, NULL, base, &l) == 0); \
- bfs_check(l == (n), "xstrtol('%s') == %ld (!= %ld)", str, l, (long)(n)); \
- } else { \
- bfs_echeck(xstrtol(str, NULL, base, &l) != 0 && errno == ERANGE); \
- } \
- bfs_echeck(xstrtoll(str, NULL, base, &ll) == 0); \
- bfs_check(ll == (n), "xstrtoll('%s') == %lld (!= %lld)", str, ll, (long long)(n)) \
+ do { \
+ check_strtotype( signed short, SHRT_MIN, SHRT_MAX, "%d", xstrtos, str, base, s, n); \
+ check_strtotype( signed int, INT_MIN, INT_MAX, "%d", xstrtoi, str, base, i, n); \
+ check_strtotype( signed long, LONG_MIN, LONG_MAX, "%ld", xstrtol, str, base, l, n); \
+ check_strtotype( signed long long, LLONG_MIN, LLONG_MAX, "%lld", xstrtoll, str, base, ll, n); \
+ check_strtotype(unsigned short, 0, USHRT_MAX, "%u", xstrtous, str, base, us, n); \
+ check_strtotype(unsigned int, 0, UINT_MAX, "%u", xstrtoui, str, base, ui, n); \
+ check_strtotype(unsigned long, 0, ULONG_MAX, "%lu", xstrtoul, str, base, ul, n); \
+ check_strtotype(unsigned long long, 0, ULLONG_MAX, "%llu", xstrtoull, str, base, ull, n); \
+ } while (0)
check_strtoint("123", 0, 123);
check_strtoint("+123", 0, 123);
@@ -139,13 +175,21 @@ static void check_strtox(void) {
check_strtoint("123", 16, 0x123);
- check_strtoint("9223372036854775807", 0, 9223372036854775807LL);
- check_strtoint("-9223372036854775808", 0, -9223372036854775807LL - 1);
+ check_strtoint("0x7FFF", 0, 0x7FFF);
+ check_strtoint("-0x8000", 0, -0x8000);
+
+ check_strtoint("0x7FFFFFFF", 0, 0x7FFFFFFFL);
+ check_strtoint("-0x80000000", 0, -0x7FFFFFFFL - 1);
+
+ check_strtoint("0x7FFFFFFFFFFFFFFF", 0, 0x7FFFFFFFFFFFFFFFLL);
+ check_strtoint("-0x8000000000000000", 0, -0x7FFFFFFFFFFFFFFFLL - 1);
#define check_strtoend(str, estr, base, n) \
- bfs_echeck(xstrtoll(str, &end, base, &ll) == 0); \
- bfs_check(ll == (n), "xstrtoll('%s') == %lld (!= %lld)", str, ll, (long long)(n)); \
- bfs_check(strcmp(end, estr) == 0, "xstrtoll('%s'): end == '%s' (!= '%s')", str, end, estr) \
+ do { \
+ bfs_echeck(xstrtoll(str, &end, base, &ll) == 0); \
+ bfs_check(ll == (n), "xstrtoll('%s') == %lld (!= %lld)", str, ll, (long long)(n)); \
+ bfs_check(strcmp(end, estr) == 0, "xstrtoll('%s'): end == '%s' (!= '%s')", str, end, estr); \
+ } while (0)
check_strtoend("123 ", " ", 0, 123);
check_strtoend("0789", "89", 0, 07);
diff --git a/tests/getopts.sh b/tests/getopts.sh
index 255f2fa..a16511f 100644
--- a/tests/getopts.sh
+++ b/tests/getopts.sh
@@ -5,11 +5,7 @@
## Argument parsing
-if command -v nproc &>/dev/null; then
- JOBS=$(nproc)
-else
- JOBS=1
-fi
+JOBS=$(_nproc)
MAKE=
PATTERNS=()
SUDO=()
diff --git a/tests/gnu/execdir_self.out b/tests/gnu/execdir_self.out
new file mode 100644
index 0000000..3ad0640
--- /dev/null
+++ b/tests/gnu/execdir_self.out
@@ -0,0 +1 @@
+./bar.sh
diff --git a/tests/gnu/execdir_self.sh b/tests/gnu/execdir_self.sh
new file mode 100644
index 0000000..1fc5d04
--- /dev/null
+++ b/tests/gnu/execdir_self.sh
@@ -0,0 +1,9 @@
+cd "$TEST"
+mkdir foo
+cat >foo/bar.sh <<EOF
+#!/bin/sh
+printf '%s\n' "\$@"
+EOF
+chmod +x foo/bar.sh
+
+bfs_diff . -name bar.sh -execdir {} {} \;
diff --git a/tests/main.c b/tests/main.c
index 4c770bd..9240e1c 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -222,15 +222,15 @@ int main(int argc, char *argv[]) {
}
tzset();
- long jobs = 0;
+ unsigned int jobs = 0;
const char *cmd = argc > 0 ? argv[0] : "units";
int c;
while (c = getopt(argc, argv, ":j:"), c != -1) {
switch (c) {
case 'j':
- if (xstrtol(optarg, NULL, 10, &jobs) != 0 || jobs <= 0) {
- fprintf(stderr, "%s: Bad job count '%s'\n", cmd, optarg);
+ if (xstrtoui(optarg, NULL, 10, &jobs) != 0) {
+ fprintf(stderr, "%s: Bad job count '%s': %s\n", cmd, optarg, errstr());
return EXIT_FAILURE;
}
break;
@@ -243,7 +243,7 @@ int main(int argc, char *argv[]) {
}
}
- if (jobs == 0) {
+ if (!jobs) {
jobs = nproc();
}
diff --git a/tests/posix/exec_sigmask.sh b/tests/posix/exec_sigmask.sh
index d1192a4..2907458 100644
--- a/tests/posix/exec_sigmask.sh
+++ b/tests/posix/exec_sigmask.sh
@@ -11,6 +11,6 @@ mkfifo p1 p2
} &
# Write the `sh` PID to p1, then hang reading p2 until we're killed
-! invoke_bfs p1 -exec sh -c 'echo $$ >p1 && read -r _ <p2' {} + || fail
+! invoke_bfs p1 -exec bash -c 'echo $$ >p1 && read -r _ <p2' bash {} + || fail
-wait
+_wait
diff --git a/tests/posix/group_o_group.out b/tests/posix/group_o_group.out
new file mode 100644
index 0000000..a7ccfe4
--- /dev/null
+++ b/tests/posix/group_o_group.out
@@ -0,0 +1,19 @@
+basic
+basic/a
+basic/b
+basic/c
+basic/c/d
+basic/e
+basic/e/f
+basic/g
+basic/g/h
+basic/i
+basic/j
+basic/j/foo
+basic/k
+basic/k/foo
+basic/k/foo/bar
+basic/l
+basic/l/foo
+basic/l/foo/bar
+basic/l/foo/bar/baz
diff --git a/tests/posix/group_o_group.sh b/tests/posix/group_o_group.sh
new file mode 100644
index 0000000..60aefc0
--- /dev/null
+++ b/tests/posix/group_o_group.sh
@@ -0,0 +1,3 @@
+# Regression test for
+# https://github.com/tavianator/bfs/issues/155
+bfs_diff basic -group 0 -o -group "$(id -g)"
diff --git a/tests/posix/root_order.out b/tests/posix/root_order.out
new file mode 100644
index 0000000..ea94276
--- /dev/null
+++ b/tests/posix/root_order.out
@@ -0,0 +1,4 @@
+basic/a
+basic/b
+basic/c/d
+basic/e/f
diff --git a/tests/posix/root_order.sh b/tests/posix/root_order.sh
new file mode 100644
index 0000000..86adf20
--- /dev/null
+++ b/tests/posix/root_order.sh
@@ -0,0 +1,6 @@
+# Root paths must be processed in order
+# https://www.austingroupbugs.net/view.php?id=1859
+
+# -size forces a stat(), which we don't want to be async
+invoke_bfs basic/{a,b,c/d,e/f} -size -1000 >"$OUT"
+diff_output
diff --git a/tests/posix/user_o_user.out b/tests/posix/user_o_user.out
new file mode 100644
index 0000000..a7ccfe4
--- /dev/null
+++ b/tests/posix/user_o_user.out
@@ -0,0 +1,19 @@
+basic
+basic/a
+basic/b
+basic/c
+basic/c/d
+basic/e
+basic/e/f
+basic/g
+basic/g/h
+basic/i
+basic/j
+basic/j/foo
+basic/k
+basic/k/foo
+basic/k/foo/bar
+basic/l
+basic/l/foo
+basic/l/foo/bar
+basic/l/foo/bar/baz
diff --git a/tests/posix/user_o_user.sh b/tests/posix/user_o_user.sh
new file mode 100644
index 0000000..7c143ae
--- /dev/null
+++ b/tests/posix/user_o_user.sh
@@ -0,0 +1,3 @@
+# Regression test for
+# https://github.com/tavianator/bfs/issues/155
+bfs_diff basic -user 0 -o -user "$(id -u)"
diff --git a/tests/ptyx.c b/tests/ptyx.c
index 4d89581..59292df 100644
--- a/tests/ptyx.c
+++ b/tests/ptyx.c
@@ -65,25 +65,21 @@ int main(int argc, char *argv[]) {
exit(EXIT_FAILURE); \
} while (0)
- long width = -1;
- long height = -1;
+ unsigned short width = 0;
+ unsigned short height = 0;
// Parse the command line
int c;
while (c = getopt(argc, argv, "+:w:h:"), c != -1) {
switch (c) {
case 'w':
- if (xstrtol(optarg, NULL, 10, &width) != 0) {
+ if (xstrtous(optarg, NULL, 10, &width) != 0) {
edie("Bad width '%s'", optarg);
- } else if (width < 0 || width > (long)USHRT_MAX) {
- die("Bad width '%s'", optarg);
}
break;
case 'h':
- if (xstrtol(optarg, NULL, 10, &height) != 0) {
+ if (xstrtous(optarg, NULL, 10, &height) != 0) {
edie("Bad height '%s'", optarg);
- } else if (height < 0 || height > (long)USHRT_MAX) {
- die("Bad height '%s'", optarg);
}
break;
case ':':
@@ -135,36 +131,36 @@ int main(int argc, char *argv[]) {
// A new pty starts at 0x0, which is not very useful. Instead, grab the
// default size from the current controlling terminal, if possible.
- if (width < 0 || height < 0) {
+ if (!width || !height) {
int tty = open_cterm(O_RDONLY | O_CLOEXEC);
if (tty >= 0) {
struct winsize ws;
if (xtcgetwinsize(tty, &ws) != 0) {
edie("tcgetwinsize()");
}
- if (width < 0) {
+ if (!width) {
width = ws.ws_col;
}
- if (height < 0) {
+ if (!height) {
height = ws.ws_row;
}
xclose(tty);
}
}
+ if (!width) {
+ width = 80;
+ }
+ if (!height) {
+ height = 24;
+ }
- // Get the current pty size
+ // Update the pty size
struct winsize ws;
if (xtcgetwinsize(pts, &ws) != 0) {
edie("tcgetwinsize()");
}
-
- // Apply our options
- if (width >= 0) {
- ws.ws_col = width;
- }
- if (height >= 0) {
- ws.ws_row = height;
- }
+ ws.ws_col = width;
+ ws.ws_row = height;
if (xtcsetwinsize(pts, &ws) != 0) {
edie("tcsetwinsize()");
}
diff --git a/tests/run.sh b/tests/run.sh
index 8d3a5d2..3ed2a9c 100644
--- a/tests/run.sh
+++ b/tests/run.sh
@@ -96,16 +96,13 @@ reap_test() {
wait_test() {
local pid line ret
- while true; do
+ while :; do
line=$((LINENO + 1))
- wait -n -ppid
+ _wait -n -ppid
ret=$?
if [ "${pid:-}" ]; then
break
- elif ((ret > 128)); then
- # Interrupted by signal
- continue
else
debug "${BASH_SOURCE[0]}" $line "${RED}error $ret${RST}" >&$DUPERR
exit 1
diff --git a/tests/util.sh b/tests/util.sh
index b846d45..1718a1a 100644
--- a/tests/util.sh
+++ b/tests/util.sh
@@ -190,3 +190,28 @@ pop_defers() {
return $ret
}
+
+## Parallelism
+
+# Get the number of processors
+_nproc() {
+ {
+ nproc \
+ || sysctl -n hw.ncpu \
+ || getconf _NPROCESSORS_ONLN \
+ || echo 1
+ } 2>/dev/null
+}
+
+# Run wait, looping if interrupted
+_wait() {
+ local ret=130
+
+ # "If wait is interrupted by a signal, the return status will be greater than 128"
+ while ((ret > 128)); do
+ ret=0
+ wait "$@" || ret=$?
+ done
+
+ return $ret
+}
diff --git a/tests/xspawn.c b/tests/xspawn.c
index 0244006..6864192 100644
--- a/tests/xspawn.c
+++ b/tests/xspawn.c
@@ -99,6 +99,22 @@ static int reset_path(char *old_path) {
return ret;
}
+/** Spawn the test binary and check for success. */
+static void check_spawnee(const char *exe, const struct bfs_spawn *ctx, char **argv, char **envp) {
+ pid_t pid = bfs_spawn(exe, ctx, argv, envp);
+ if (!bfs_echeck(pid >= 0, "bfs_spawn('%s')", exe)) {
+ return;
+ }
+
+ int wstatus;
+ bool exited = bfs_echeck(xwaitpid(pid, &wstatus, 0) == pid)
+ && bfs_check(WIFEXITED(wstatus));
+ if (exited) {
+ int wexit = WEXITSTATUS(wstatus);
+ bfs_check(wexit == EXIT_SUCCESS, "xspawnee: exit(%d)", wexit);
+ }
+}
+
/** Check that we resolve executables in $PATH correctly. */
static void check_use_path(bool use_posix) {
struct bfs_spawn spawn;
@@ -133,20 +149,9 @@ static void check_use_path(bool use_posix) {
}
char *argv[] = {"xspawnee", old_path, NULL};
- pid_t pid = bfs_spawn("xspawnee", &spawn, argv, envp);
- if (!bfs_echeck(pid >= 0, "bfs_spawn()")) {
- goto path;
- }
-
- int wstatus;
- bool exited = bfs_echeck(xwaitpid(pid, &wstatus, 0) == pid)
- && bfs_check(WIFEXITED(wstatus));
- if (exited) {
- int wexit = WEXITSTATUS(wstatus);
- bfs_check(wexit == EXIT_SUCCESS, "xspawnee: exit(%d)", wexit);
- }
+ check_spawnee("xspawnee", &spawn, argv, envp);
+ check_spawnee("tests/xspawnee", &spawn, argv, envp);
-path:
bfs_echeck(reset_path(old_path) == 0);
env:
for (char **var = envp; *var; ++var) {
diff --git a/tests/xtouch.c b/tests/xtouch.c
index 5d65a4c..f33c573 100644
--- a/tests/xtouch.c
+++ b/tests/xtouch.c
@@ -217,8 +217,8 @@ int main(int argc, char *argv[]) {
}
if (marg) {
- long mode;
- if (xstrtol(marg, NULL, 8, &mode) == 0 && mode >= 0 && mode < 01000) {
+ unsigned int mode;
+ if (xstrtoui(marg, NULL, 8, &mode) == 0 && mode < 01000) {
args.fmode = args.dmode = mode;
} else {
fprintf(stderr, "%s: Invalid mode '%s'\n", cmd, marg);