Skip to content

Commit

Permalink
tests/xtouch: New utility
Browse files Browse the repository at this point in the history
POSIX touch(1) doesn't include the -h option, and indeed OpenBSD doesn't
implement it.  Making our own utility also lets us add some handy
extensions like -p (create parents) and -M (set permissions).
  • Loading branch information
tavianator committed Nov 7, 2022
1 parent 3139cbc commit 2d5edb3
Show file tree
Hide file tree
Showing 23 changed files with 263 additions and 90 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,12 @@ DISTCHECK_FLAGS := -s TEST_FLAGS="--sudo --verbose=skipped"
bfs: $(BIN)/bfs
.PHONY: bfs

all: $(BIN)/bfs $(BIN)/tests/mksock $(BIN)/tests/trie $(BIN)/tests/xtimegm
all: \
$(BIN)/bfs \
$(BIN)/tests/mksock \
$(BIN)/tests/trie \
$(BIN)/tests/xtimegm \
$(BIN)/tests/xtouch
.PHONY: all

$(BIN)/bfs: \
Expand Down Expand Up @@ -230,6 +235,7 @@ $(BIN)/bfs: \
$(BIN)/tests/mksock: $(OBJ)/tests/mksock.o
$(BIN)/tests/trie: $(OBJ)/src/trie.o $(OBJ)/tests/trie.o
$(BIN)/tests/xtimegm: $(OBJ)/src/xtime.o $(OBJ)/tests/xtimegm.o
$(BIN)/tests/xtouch: $(OBJ)/src/xtime.o $(OBJ)/tests/xtouch.o

$(BIN)/%:
@$(MKDIR) $(@D)
Expand Down Expand Up @@ -257,7 +263,7 @@ $(FLAG_GOALS): $(FLAG_PREREQS)
check: $(CHECKS)
.PHONY: check $(CHECKS)

$(STRATEGY_CHECKS): check-%: $(BIN)/bfs $(BIN)/tests/mksock
$(STRATEGY_CHECKS): check-%: $(BIN)/bfs $(BIN)/tests/mksock $(BIN)/tests/xtouch
./tests/tests.sh --bfs="$(BIN)/bfs -S $*" $(TEST_FLAGS)

check-trie check-xtimegm: check-%: $(BIN)/tests/%
Expand Down
2 changes: 1 addition & 1 deletion tests/bfs/L_capable.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ clean_scratch

skip_unless invoke_bfs scratch -quit -capable

$TOUCH scratch/{normal,capable}
"$XTOUCH" scratch/{normal,capable}
sudo setcap all+ep scratch/capable
ln -s capable scratch/link

Expand Down
2 changes: 1 addition & 1 deletion tests/bfs/capable.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ clean_scratch

skip_unless invoke_bfs scratch -quit -capable

$TOUCH scratch/{normal,capable}
"$XTOUCH" scratch/{normal,capable}
sudo setcap all+ep scratch/capable
ln -s capable scratch/link

Expand Down
2 changes: 1 addition & 1 deletion tests/bfs/color_ls.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
clean_scratch
touchp scratch/foo/bar/baz
"$XTOUCH" -p scratch/foo/bar/baz
ln -s foo/bar/baz scratch/link
ln -s foo/bar/nowhere scratch/broken
ln -s foo/bar/nowhere/nothing scratch/nested
Expand Down
2 changes: 1 addition & 1 deletion tests/bsd/L_acl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ clean_scratch

skip_unless invoke_bfs scratch -quit -acl

$TOUCH scratch/{normal,acl}
"$XTOUCH" scratch/{normal,acl}
skip_unless set_acl scratch/acl
ln -s acl scratch/link

Expand Down
2 changes: 1 addition & 1 deletion tests/bsd/acl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ clean_scratch

skip_unless invoke_bfs scratch -quit -acl

$TOUCH scratch/{normal,acl}
"$XTOUCH" scratch/{normal,acl}
skip_unless set_acl scratch/acl
ln -s acl scratch/link

Expand Down
2 changes: 1 addition & 1 deletion tests/bsd/flags.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ skip_unless invoke_bfs scratch -quit -flags offline

clean_scratch

$TOUCH scratch/{foo,bar}
"$XTOUCH" scratch/{foo,bar}
skip_unless chflags offline scratch/bar

bfs_diff scratch -flags -offline,nohidden
2 changes: 1 addition & 1 deletion tests/bsd/rm.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
clean_scratch
touchp scratch/foo/bar/baz
"$XTOUCH" -p scratch/foo/bar/baz

(cd scratch && invoke_bfs . -rm)

Expand Down
2 changes: 1 addition & 1 deletion tests/common/L_mount.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ clean_scratch
mkdir scratch/{foo,mnt}
sudo mount -t tmpfs tmpfs scratch/mnt
ln -s ../mnt scratch/foo/bar
$TOUCH scratch/mnt/baz
"$XTOUCH" scratch/mnt/baz
ln -s ../mnt/baz scratch/foo/qux

bfs_diff -L scratch -mount
Expand Down
2 changes: 1 addition & 1 deletion tests/common/delete.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
clean_scratch
touchp scratch/foo/bar/baz
"$XTOUCH" -p scratch/foo/bar/baz

# Don't try to delete '.'
(cd scratch && invoke_bfs . -delete)
Expand Down
2 changes: 1 addition & 1 deletion tests/common/delete_many.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

clean_scratch
mkdir scratch/foo
$TOUCH scratch/foo/{1..256}
"$XTOUCH" scratch/foo/{1..256}

invoke_bfs scratch/foo -delete
bfs_diff scratch
2 changes: 1 addition & 1 deletion tests/common/inum_bind_mount.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ skip_unless test "$SUDO"
skip_unless test "$UNAME" = "Linux"

clean_scratch
$TOUCH scratch/{foo,bar}
"$XTOUCH" scratch/{foo,bar}
sudo mount --bind scratch/{foo,bar}

bfs_diff scratch -inum "$(inum scratch/bar)"
Expand Down
2 changes: 1 addition & 1 deletion tests/common/mount.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ skip_if test "$UNAME" = "Darwin"
clean_scratch
mkdir scratch/{foo,mnt}
sudo mount -t tmpfs tmpfs scratch/mnt
$TOUCH scratch/foo/bar scratch/mnt/baz
"$XTOUCH" scratch/foo/bar scratch/mnt/baz

bfs_diff scratch -mount
ret=$?
Expand Down
2 changes: 1 addition & 1 deletion tests/gnu/fprint_duplicate.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
touchp scratch/foo.out
"$XTOUCH" -p scratch/foo.out
ln scratch/foo.out scratch/foo.hard
ln -s foo.out scratch/foo.soft

Expand Down
2 changes: 1 addition & 1 deletion tests/gnu/ignore_readdir_race.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
clean_scratch
$TOUCH scratch/{foo,bar}
"$XTOUCH" scratch/{foo,bar}

# -links 1 forces a stat() call, which will fail for the second file
invoke_bfs scratch -mindepth 1 -ignore_readdir_race -links 1 -exec "$TESTS/remove-sibling.sh" {} \;
4 changes: 2 additions & 2 deletions tests/gnu/ignore_readdir_race_notdir.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Check -ignore_readdir_race handling when a directory is replaced with a file
clean_scratch
touchp scratch/foo/bar
"$XTOUCH" -p scratch/foo/bar

invoke_bfs scratch -mindepth 1 -ignore_readdir_race -execdir rm -r {} \; -execdir $TOUCH {} \;
invoke_bfs scratch -mindepth 1 -ignore_readdir_race -execdir rm -r {} \; -execdir "$XTOUCH" {} \;
2 changes: 1 addition & 1 deletion tests/gnu/xtype_bind_mount.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ skip_unless test "$SUDO"
skip_unless test "$UNAME" = "Linux"

clean_scratch
$TOUCH scratch/{file,null}
"$XTOUCH" scratch/{file,null}
sudo mount --bind /dev/null scratch/null
ln -s /dev/null scratch/link

Expand Down
2 changes: 1 addition & 1 deletion tests/posix/L_xdev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ clean_scratch
mkdir scratch/{foo,mnt}
sudo mount -t tmpfs tmpfs scratch/mnt
ln -s ../mnt scratch/foo/bar
$TOUCH scratch/mnt/baz
"$XTOUCH" scratch/mnt/baz
ln -s ../mnt/baz scratch/foo/qux

bfs_diff -L scratch -xdev
Expand Down
2 changes: 1 addition & 1 deletion tests/posix/depth_error.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
clean_scratch
touchp scratch/foo/bar
"$XTOUCH" -p scratch/foo/bar
chmod a-r scratch/foo

bfs_diff scratch -depth
Expand Down
2 changes: 1 addition & 1 deletion tests/posix/type_bind_mount.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ skip_unless test "$SUDO"
skip_unless test "$UNAME" = "Linux"

clean_scratch
$TOUCH scratch/{file,null}
"$XTOUCH" scratch/{file,null}
sudo mount --bind /dev/null scratch/null

bfs_diff scratch -type c
Expand Down
2 changes: 1 addition & 1 deletion tests/posix/xdev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ skip_if test "$UNAME" = "Darwin"
clean_scratch
mkdir scratch/{foo,mnt}
sudo mount -t tmpfs tmpfs scratch/mnt
$TOUCH scratch/foo/bar scratch/mnt/baz
"$XTOUCH" scratch/foo/bar scratch/mnt/baz

bfs_diff scratch -xdev
ret=$?
Expand Down
106 changes: 39 additions & 67 deletions tests/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ if [ "${BUILDDIR-}" ]; then
else
BIN=$(_realpath "$TESTS/../bin")
fi
MKSOCK="$BIN/tests/mksock"
XTOUCH="$BIN/tests/xtouch"

# Try to resolve the path to $BFS before we cd, while also supporting
# --bfs="./bin/bfs -S ids"
Expand Down Expand Up @@ -311,63 +313,34 @@ else
echo "Test files saved to $TMP"
fi

# Install a file, creating any parent directories
function installp() {
local target="${@: -1}"
mkdir -p "${target%/*}"
install "$@"
}

# Prefer GNU touch to work around https://apple.stackexchange.com/a/425730/397839
if command -v gtouch &>/dev/null; then
TOUCH=gtouch
else
TOUCH=touch
fi

# Like a mythical touch -p
function touchp() {
for arg; do
installp -m644 /dev/null "$arg"
done
}

# Creates a simple file+directory structure for tests
function make_basic() {
touchp "$1/a"
touchp "$1/b"
touchp "$1/c/d"
touchp "$1/e/f"
mkdir -p "$1/g/h"
mkdir -p "$1/i"
touchp "$1/j/foo"
touchp "$1/k/foo/bar"
touchp "$1/l/foo/bar/baz"
"$XTOUCH" -p "$1"/{a,b,c/d,e/f,g/h/,i/}
"$XTOUCH" -p "$1"/{j/foo,k/foo/bar,l/foo/bar/baz}
echo baz >"$1/l/foo/bar/baz"
}
make_basic "$TMP/basic"

# Creates a file+directory structure with various permissions for tests
function make_perms() {
installp -m000 /dev/null "$1/0"
installp -m444 /dev/null "$1/r"
installp -m222 /dev/null "$1/w"
installp -m644 /dev/null "$1/rw"
installp -m555 /dev/null "$1/rx"
installp -m311 /dev/null "$1/wx"
installp -m755 /dev/null "$1/rwx"
"$XTOUCH" -p -M000 "$1/0"
"$XTOUCH" -p -M444 "$1/r"
"$XTOUCH" -p -M222 "$1/w"
"$XTOUCH" -p -M644 "$1/rw"
"$XTOUCH" -p -M555 "$1/rx"
"$XTOUCH" -p -M311 "$1/wx"
"$XTOUCH" -p -M755 "$1/rwx"
}
make_perms "$TMP/perms"

# Creates a file+directory structure with various symbolic and hard links
function make_links() {
touchp "$1/file"
"$XTOUCH" -p "$1/file"
ln -s file "$1/symlink"
ln "$1/file" "$1/hardlink"
ln -s nowhere "$1/broken"
ln -s symlink/file "$1/notdir"
mkdir -p "$1/deeply/nested/dir"
touchp "$1/deeply/nested/file"
"$XTOUCH" -p "$1/deeply/nested"/{dir/,file}
ln -s file "$1/deeply/nested/link"
ln -s nowhere "$1/deeply/nested/broken"
ln -s deeply/nested "$1/skip"
Expand All @@ -376,7 +349,7 @@ make_links "$TMP/links"

# Creates a file+directory structure with symbolic link loops
function make_loops() {
touchp "$1/file"
"$XTOUCH" -p "$1/file"
ln -s file "$1/symlink"
ln -s nowhere "$1/broken"
ln -s symlink/file "$1/notdir"
Expand All @@ -389,29 +362,28 @@ make_loops "$TMP/loops"

# Creates a file+directory structure with varying timestamps
function make_times() {
mkdir -p "$1"
$TOUCH -t 199112140000 "$1/a"
$TOUCH -t 199112140001 "$1/b"
$TOUCH -t 199112140002 "$1/c"
"$XTOUCH" -p -t 199112140000 "$1/a"
"$XTOUCH" -p -t 199112140001 "$1/b"
"$XTOUCH" -p -t 199112140002 "$1/c"
ln -s a "$1/l"
$TOUCH -h -t 199112140003 "$1/l"
$TOUCH -t 199112140004 "$1"
"$XTOUCH" -p -h -t 199112140003 "$1/l"
"$XTOUCH" -p -t 199112140004 "$1"
}
make_times "$TMP/times"

# Creates a file+directory structure with various weird file/directory names
function make_weirdnames() {
touchp "$1/-/a"
touchp "$1/(/b"
touchp "$1/(-/c"
touchp "$1/!/d"
touchp "$1/!-/e"
touchp "$1/,/f"
touchp "$1/)/g"
touchp "$1/.../h"
touchp "$1/\\/i"
touchp "$1/ /j"
touchp "$1/[/k"
"$XTOUCH" -p "$1/-/a"
"$XTOUCH" -p "$1/(/b"
"$XTOUCH" -p "$1/(-/c"
"$XTOUCH" -p "$1/!/d"
"$XTOUCH" -p "$1/!-/e"
"$XTOUCH" -p "$1/,/f"
"$XTOUCH" -p "$1/)/g"
"$XTOUCH" -p "$1/.../h"
"$XTOUCH" -p "$1/\\/i"
"$XTOUCH" -p "$1/ /j"
"$XTOUCH" -p "$1/[/k"
}
make_weirdnames "$TMP/weirdnames"

Expand Down Expand Up @@ -439,32 +411,32 @@ function make_deep() {
cd "$names"
done

$TOUCH "$name"
"$XTOUCH" "$name"
)
done
}
make_deep "$TMP/deep"

# Creates a directory structure with many different types, and therefore colors
function make_rainbow() {
touchp "$1/file.txt"
touchp "$1/file.dat"
touchp "$1/star".{gz,tar,tar.gz}
"$XTOUCH" -p "$1/file.txt"
"$XTOUCH" -p "$1/file.dat"
"$XTOUCH" -p "$1/star".{gz,tar,tar.gz}
ln -s file.txt "$1/link.txt"
touchp "$1/mh1"
"$XTOUCH" -p "$1/mh1"
ln "$1/mh1" "$1/mh2"
mkfifo "$1/pipe"
# TODO: block
ln -s /dev/null "$1/chardev_link"
ln -s nowhere "$1/broken"
"$BIN/tests/mksock" "$1/socket"
touchp "$1"/s{u,g,ug}id
"$MKSOCK" "$1/socket"
"$XTOUCH" -p "$1"/s{u,g,ug}id
chmod u+s "$1"/su{,g}id
chmod g+s "$1"/s{u,}gid
mkdir "$1/ow" "$1"/sticky{,_ow}
chmod o+w "$1"/*ow
chmod +t "$1"/sticky*
touchp "$1"/exec.sh
"$XTOUCH" -p "$1"/exec.sh
chmod +x "$1"/exec.sh
}
make_rainbow "$TMP/rainbow"
Expand Down Expand Up @@ -631,7 +603,7 @@ function set_acl() {
function make_xattrs() {
clean_scratch

$TOUCH scratch/{normal,xattr,xattr_2}
"$XTOUCH" scratch/{normal,xattr,xattr_2}
ln -s xattr scratch/link
ln -s normal scratch/xattr_link

Expand Down
Loading

0 comments on commit 2d5edb3

Please sign in to comment.