summaryrefslogtreecommitdiff
path: root/corebinutils/pwd/tests/test.sh
blob: 28114c1339d6b336d18a5d7c3262fdf1844a153b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#!/bin/sh
#
# tests/test.sh — pwd(1) Linux port test suite
#
# Environment:
#   PWD_BIN   path to the pwd binary under test
#   LC_ALL    must be C (set below)
#
# Contract per test:
#   Every test asserts exit status + stdout + stderr.
#   "It ran" alone is never sufficient.
#
# Robustness:
#   - Works inside containers and PID namespaces
#   - No assumption about the runner's $PWD value
#   - TZ and LC_ALL forced to avoid locale/timezone noise
#   - TMPDIR-based isolation; cleaned up on exit

set -eu

export LC_ALL=C
export TZ=UTC

ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd -P)
PWD_BIN="${PWD_BIN:-$ROOT/out/pwd}"

TMPDIR="${TMPDIR:-/tmp}"
WORKDIR=$(mktemp -d "$TMPDIR/pwd-test.XXXXXX")
STDOUT_FILE="$WORKDIR/stdout"
STDERR_FILE="$WORKDIR/stderr"
trap 'rm -rf "$WORKDIR"' EXIT INT TERM

# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------

fail() {
	printf 'FAIL: %s\n' "$1" >&2
	exit 1
}

pass() {
	printf 'PASS: %s\n' "$1"
}

# run_capture cmd [args...]
# Captures stdout → STDOUT_FILE, stderr → STDERR_FILE, status → LAST_STATUS.
# Never aborts due to nonzero exit before capture.
run_capture() {
	if "$@" >"$STDOUT_FILE" 2>"$STDERR_FILE"; then
		LAST_STATUS=0
	else
		LAST_STATUS=$?
	fi
	LAST_STDOUT=$(cat "$STDOUT_FILE")
	LAST_STDERR=$(cat "$STDERR_FILE")
}

assert_status() {
	name=$1
	expected=$2
	actual=$3
	if [ "$expected" -ne "$actual" ]; then
		printf 'FAIL: %s\n' "$name" >&2
		printf '  expected status: %d\n' "$expected" >&2
		printf '  actual   status: %d\n' "$actual" >&2
		exit 1
	fi
}

assert_eq() {
	name=$1
	expected=$2
	actual=$3
	if [ "$expected" != "$actual" ]; then
		printf 'FAIL: %s\n' "$name" >&2
		printf '  expected: %s\n' "$expected" >&2
		printf '  actual:   %s\n' "$actual" >&2
		exit 1
	fi
}

assert_empty() {
	name=$1
	text=$2
	if [ -n "$text" ]; then
		printf 'FAIL: %s (expected empty, got: %s)\n' "$name" "$text" >&2
		exit 1
	fi
}

assert_contains() {
	name=$1
	text=$2
	pattern=$3
	case $text in
		*"$pattern"*) ;;
		*) fail "$name: expected '$pattern' in '$text'" ;;
	esac
}

# ---------------------------------------------------------------------------
# Prerequisite
# ---------------------------------------------------------------------------

[ -x "$PWD_BIN" ] || fail "missing or non-executable binary: $PWD_BIN"

# ---------------------------------------------------------------------------
# 1. Basic invocation — no flags
# ---------------------------------------------------------------------------

(
	cd "$WORKDIR"
	run_capture "$PWD_BIN"
	assert_status "basic no-flag status" 0 "$LAST_STATUS"
	assert_eq "basic no-flag stdout" "$WORKDIR" "$LAST_STDOUT"
	assert_empty "basic no-flag stderr" "$LAST_STDERR"
)
pass "basic no-flag"

# ---------------------------------------------------------------------------
# 2. Explicit -L (logical, $PWD matches physical — simplest case)
# ---------------------------------------------------------------------------

(
	cd "$WORKDIR"
	run_capture "$PWD_BIN" -L
	assert_status "explicit -L status" 0 "$LAST_STATUS"
	assert_eq "explicit -L stdout" "$WORKDIR" "$LAST_STDOUT"
	assert_empty "explicit -L stderr" "$LAST_STDERR"
)
pass "explicit -L"

# ---------------------------------------------------------------------------
# 3. Explicit -P (physical, no symlinks → same as logical in WORKDIR)
# ---------------------------------------------------------------------------

(
	cd "$WORKDIR"
	run_capture "$PWD_BIN" -P
	assert_status "explicit -P status" 0 "$LAST_STATUS"
	assert_eq "explicit -P stdout" "$WORKDIR" "$LAST_STDOUT"
	assert_empty "explicit -P stderr" "$LAST_STDERR"
)
pass "explicit -P"

# ---------------------------------------------------------------------------
# 4. Symlink: -L shows logical path, -P shows physical path
# ---------------------------------------------------------------------------

PHYBASE="$WORKDIR/phy"
LOGBASE="$WORKDIR/log"
PHYDIR="$PHYBASE/sub"
mkdir -p "$PHYDIR"
ln -s "$PHYBASE" "$LOGBASE"

(
	cd "$LOGBASE/sub"
	export PWD="$LOGBASE/sub"
	run_capture "$PWD_BIN" -L
	assert_status "symlink -L status" 0 "$LAST_STATUS"
	assert_eq "symlink -L stdout" "$LOGBASE/sub" "$LAST_STDOUT"
	assert_empty "symlink -L stderr" "$LAST_STDERR"
)
pass "symlink -L"

(
	cd "$LOGBASE/sub"
	export PWD="$LOGBASE/sub"
	run_capture "$PWD_BIN" -P
	assert_status "symlink -P status" 0 "$LAST_STATUS"
	assert_eq "symlink -P stdout" "$PHYDIR" "$LAST_STDOUT"
	assert_empty "symlink -P stderr" "$LAST_STDERR"
)
pass "symlink -P"

# ---------------------------------------------------------------------------
# 5. Last flag wins: -L -P should behave as -P
# ---------------------------------------------------------------------------

(
	cd "$LOGBASE/sub"
	export PWD="$LOGBASE/sub"
	run_capture "$PWD_BIN" -L -P
	assert_status "last-flag -L -P status" 0 "$LAST_STATUS"
	assert_eq "last-flag -L -P stdout" "$PHYDIR" "$LAST_STDOUT"
	assert_empty "last-flag -L -P stderr" "$LAST_STDERR"
)
pass "last-flag -L -P is physical"

(
	cd "$LOGBASE/sub"
	export PWD="$LOGBASE/sub"
	run_capture "$PWD_BIN" -P -L
	assert_status "last-flag -P -L status" 0 "$LAST_STATUS"
	assert_eq "last-flag -P -L stdout" "$LOGBASE/sub" "$LAST_STDOUT"
	assert_empty "last-flag -P -L stderr" "$LAST_STDERR"
)
pass "last-flag -P -L is logical"

# ---------------------------------------------------------------------------
# 6. Relative $PWD → logical fallback to physical
# ---------------------------------------------------------------------------

(
	cd "$LOGBASE/sub"
	export PWD="log/sub"    # relative — must be rejected
	run_capture "$PWD_BIN" -L
	assert_status "relative PWD -L status" 0 "$LAST_STATUS"
	# Must fall back to physical
	assert_eq "relative PWD -L stdout" "$PHYDIR" "$LAST_STDOUT"
	assert_empty "relative PWD -L stderr" "$LAST_STDERR"
)
pass "relative PWD fallback to physical"

# ---------------------------------------------------------------------------
# 7. $PWD contains /./   → must reject it (POSIX 4.13)
# ---------------------------------------------------------------------------

(
	cd "$PHYDIR"
	export PWD="$PHYBASE/./sub"
	run_capture "$PWD_BIN" -L
	assert_status "dot-component PWD -L status" 0 "$LAST_STATUS"
	assert_eq "dot-component PWD -L stdout" "$PHYDIR" "$LAST_STDOUT"
	assert_empty "dot-component PWD -L stderr" "$LAST_STDERR"
)
pass "PWD with /./  rejected, fallback to physical"

# ---------------------------------------------------------------------------
# 8. $PWD contains /../  → must reject it
# ---------------------------------------------------------------------------

(
	cd "$PHYDIR"
	export PWD="$PHYBASE/../phy/sub"
	run_capture "$PWD_BIN" -L
	assert_status "dotdot-component PWD -L status" 0 "$LAST_STATUS"
	assert_eq "dotdot-component PWD -L stdout" "$PHYDIR" "$LAST_STDOUT"
	assert_empty "dotdot-component PWD -L stderr" "$LAST_STDERR"
)
pass "PWD with /../ rejected, fallback to physical"

# ---------------------------------------------------------------------------
# 9. $PWD points to wrong inode → fallback to physical
# ---------------------------------------------------------------------------

(
	cd "$PHYDIR"
	export PWD="$PHYBASE"    # $PWD is a parent dir, not the same inode
	run_capture "$PWD_BIN" -L
	assert_status "wrong-inode PWD -L status" 0 "$LAST_STATUS"
	assert_eq "wrong-inode PWD -L stdout" "$PHYDIR" "$LAST_STDOUT"
	assert_empty "wrong-inode PWD -L stderr" "$LAST_STDERR"
)
pass "PWD inode mismatch fallback to physical"

# ---------------------------------------------------------------------------
# 10. $PWD does not exist → fallback to physical
# ---------------------------------------------------------------------------

(
	cd "$PHYDIR"
	export PWD="$WORKDIR/does-not-exist"
	run_capture "$PWD_BIN" -L
	assert_status "nonexistent PWD -L status" 0 "$LAST_STATUS"
	assert_eq "nonexistent PWD -L stdout" "$PHYDIR" "$LAST_STDOUT"
	assert_empty "nonexistent PWD -L stderr" "$LAST_STDERR"
)
pass "nonexistent PWD fallback to physical"

# ---------------------------------------------------------------------------
# 11. Unset $PWD → logical falls back to physical
# ---------------------------------------------------------------------------

(
	cd "$PHYDIR"
	unset PWD
	run_capture "$PWD_BIN" -L
	assert_status "unset PWD -L status" 0 "$LAST_STATUS"
	assert_eq "unset PWD -L stdout" "$PHYDIR" "$LAST_STDOUT"
	assert_empty "unset PWD -L stderr" "$LAST_STDERR"
)
pass "unset PWD fallback to physical"

# ---------------------------------------------------------------------------
# 12. Bad flag → usage written to stderr, exit 1
# ---------------------------------------------------------------------------

(
	cd "$WORKDIR"
	run_capture "$PWD_BIN" -x
	assert_status "bad-flag status" 1 "$LAST_STATUS"
	assert_empty "bad-flag stdout" "$LAST_STDOUT"
	assert_contains "bad-flag stderr has usage" "$LAST_STDERR" "usage:"
	assert_contains "bad-flag stderr has -L" "$LAST_STDERR" "-L"
	assert_contains "bad-flag stderr has -P" "$LAST_STDERR" "-P"
)
pass "bad flag exits 1 with usage on stderr"

# ---------------------------------------------------------------------------
# 13. Extra operand → usage on stderr, exit 1
# ---------------------------------------------------------------------------

(
	cd "$WORKDIR"
	run_capture "$PWD_BIN" extra-arg
	assert_status "extra-arg status" 1 "$LAST_STATUS"
	assert_empty "extra-arg stdout" "$LAST_STDOUT"
	assert_contains "extra-arg stderr has usage" "$LAST_STDERR" "usage:"
)
pass "extra operand exits 1 with usage"

# ---------------------------------------------------------------------------
# 14. stdout failure → exit non-zero, error on stderr
# ---------------------------------------------------------------------------
# Pipe into a program that exits immediately, causing SIGPIPE / EPIPE.
# We explicitly ignore PIPE so the subshell stays alive long enough to
# capture the exit status from $pwd.
#
# This test is done in a subshell; the outer set -e is irrelevant inside it.

(
	RESULT_FILE="$WORKDIR/pipe-result"
	ERR_FILE="$WORKDIR/pipe-err"
	# We run a pipeline in a subshell that traps PIPE so pwd can finish writing.
	#   - The left side: sleep 0 && "$PWD_BIN"  (so shell sets up pipe before exec)
	#   - The right side: true immediately exits → EPIPE on next write
	sh -c '
		trap "" PIPE
		sleep 0
		"$1" 2>"$2"
		echo $? >"$3"
	' -- "$PWD_BIN" "$ERR_FILE" "$RESULT_FILE" | true || true

	# Give the subshell a moment to finish writing result file
	# (In practice this is instantaneous on any real system)
	i=0
	while [ $i -lt 20 ] && [ ! -s "$RESULT_FILE" ]; do
		sleep 0.1
		i=$((i + 1))
	done

	if [ ! -s "$RESULT_FILE" ]; then
		# PIPE delivery timing is platform-dependent; skip rather than
		# produce a false negative in constrained environments.
		printf 'SKIP: stdout-failure test (PIPE timing uncertainty)\n' >&2
	else
		result=$(cat "$RESULT_FILE")
		err=$(cat "$ERR_FILE" 2>/dev/null || true)
		if [ "$result" -eq 0 ]; then
			# Some musl/kernel combinations silently succeed when output
			# is consumed before close. Accept skip here as well.
			printf 'SKIP: stdout-failure test (PIPE not delivered)\n' >&2
		else
			assert_contains "stdout-failure stderr" "$err" "stdout"
			pass "stdout failure exits non-zero with stderr message"
		fi
	fi
)

# ---------------------------------------------------------------------------
# 15. Deep path — no static PATH_MAX limit
# ---------------------------------------------------------------------------
#
# Verify that pwd -P handles deep directory trees correctly via the dynamic
# getcwd(NULL, 0) interface rather than a fixed-size stack buffer.
#
# We use a C helper (deep_helper) that calls chdir(2) directly, bypassing
# the bash/dash shell builtin cd which enforces PATH_MAX even for relative
# single-component changes.  Only the C layer's getcwd(3) call inside pwd
# is asked to assemble the full path.
#
# The helper creates 90 levels × 50-char segment = 4500+ bytes of path
# (plus the WORKDIR prefix), then exec(2)s "$PWD_BIN" -P from inside.

DEEP_HELPER_BIN="${DEEP_HELPER_BIN:-}"
SEGMENT="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"   # 50 chars

if [ -z "$DEEP_HELPER_BIN" ] || [ ! -x "$DEEP_HELPER_BIN" ]; then
	printf 'SKIP: deep-path test (DEEP_HELPER_BIN not set or not executable)\n' >&2
else
	DEEPBASE="$WORKDIR/deep"
	mkdir -p "$DEEPBASE"
	# run_capture is called inside a subshell (cd changes dir), so
	# LAST_* variables won't propagate.  Instead we rely on $STDOUT_FILE
	# and $STDERR_FILE (persistent files in $WORKDIR) and the subshell
	# exit status to carry the result back.
	set +e
	(cd "$DEEPBASE" && "$DEEP_HELPER_BIN" 70 "$SEGMENT" "$PWD_BIN" \
	    >"$STDOUT_FILE" 2>"$STDERR_FILE")
	deep_exit=$?
	set -e
	deep_stdout=$(cat "$STDOUT_FILE")
	deep_stderr=$(cat "$STDERR_FILE")

	if [ "$deep_exit" -ne 0 ]; then
		fail "deep-path: helper/pwd exited $deep_exit; stderr: $deep_stderr"
	fi

	case "$deep_stdout" in
		/*)
			;;
		*)
			fail "deep-path: output is not absolute: $deep_stdout"
			;;
	esac

	deeplength=$(printf '%s' "$deep_stdout" | wc -c)
	if [ "$deeplength" -lt 3500 ]; then
		fail "deep-path: path not long enough to test getcwd(NULL,0) ($deeplength < 3500)"
	fi

	assert_empty "deep-path stderr" "$deep_stderr"
	pass "deep path (${deeplength} bytes) with -P via getcwd(NULL,0)"
fi

# ---------------------------------------------------------------------------
# 16. Path output ends with newline (no trailing garbage)
# ---------------------------------------------------------------------------

(
	cd "$WORKDIR"
	run_capture "$PWD_BIN"
	# Ensure the captured stdout is exactly WORKDIR (shell strips trailing newlines
	# from $(...), but our assert_eq checks LAST_STDOUT which is set via $(...),
	# so both sides will have trailing newlines stripped identically).
	assert_eq "newline-terminated stdout" "$WORKDIR" "$LAST_STDOUT"
	assert_empty "newline-terminated stderr" "$LAST_STDERR"
)
pass "output is newline-terminated"

# ---------------------------------------------------------------------------
# 17. Symlink traversal depth: $PWD via multiple layers
# ---------------------------------------------------------------------------

LAYERED1="$WORKDIR/layer1"
LAYERED2="$WORKDIR/layer2"
LAYERED_PHY="$WORKDIR/layerphy/deep"
mkdir -p "$LAYERED_PHY"
ln -s "$LAYERED_PHY" "$LAYERED1"
ln -s "$LAYERED1"    "$LAYERED2"   # layer2 → layer1 → layerphy/deep

(
	cd "$LAYERED2"
	export PWD="$LAYERED2"
	run_capture "$PWD_BIN" -L
	assert_status "multilayer -L status" 0 "$LAST_STATUS"
	assert_eq "multilayer -L stdout" "$LAYERED2" "$LAST_STDOUT"
	assert_empty "multilayer -L stderr" "$LAST_STDERR"
)
pass "multilayer symlink -L"

(
	cd "$LAYERED2"
	export PWD="$LAYERED2"
	run_capture "$PWD_BIN" -P
	assert_status "multilayer -P status" 0 "$LAST_STATUS"
	assert_eq "multilayer -P stdout" "$LAYERED_PHY" "$LAST_STDOUT"
	assert_empty "multilayer -P stderr" "$LAST_STDERR"
)
pass "multilayer symlink -P"

# ---------------------------------------------------------------------------
printf '\nALL TESTS PASSED.\n'
exit 0