xref: /freebsd/usr.bin/tee/tests/tee_test.sh (revision ee3960cba1068e12fb032a68c46d74841d9edab3)
1#
2# Copyright (c) 2024 Kyle Evans <kevans@FreeBSD.org>
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6
7atf_test_case single_file
8single_file_body()
9{
10	atf_check -o inline:"text\n" -x "echo text | tee file"
11	atf_check -o inline:"text\n" cat file
12}
13
14atf_test_case device
15device_body()
16{
17	atf_check -e inline:"text\n" -o inline:"text\n" -x \
18	    "echo text | tee /dev/stderr"
19}
20
21atf_test_case multiple_file
22multiple_file_body()
23{
24	atf_check -o inline:"text\n" -x "echo text | tee file1 file2"
25	atf_check -o inline:"text\n" cat file1
26	atf_check -o inline:"text\n" cat file2
27}
28
29atf_test_case append
30append_body()
31{
32	atf_check -o ignore -x "echo text | tee file"
33	atf_check -o inline:"text\n" cat file
34
35	# Should overwrite if done again
36	atf_check -o ignore -x "echo text | tee file"
37	atf_check -o inline:"text\n" cat file
38
39	# Should duplicate if we use -a
40	atf_check -o ignore -x "echo text | tee -a file"
41	atf_check -o inline:"text\ntext\n" cat file
42}
43
44atf_test_case sigint_ignored
45sigint_ignored_head()
46{
47	# This is most cleanly tested with interactive input, to avoid adding
48	# a lot of complexity in trying to manage an input and signal delivery
49	# dance purely in shell.
50	atf_set "require.progs" "porch"
51}
52sigint_ignored_body()
53{
54
55	# sigint.orch will write "text" to the file twice if we're properly
56	# ignoring SIGINT, so we'll do one test to confirm that SIGINT is not
57	# being ignored by porch(1), then another to confirm that tee(1) will
58	# ignore SIGINT when instructed to.
59	atf_check -s exit:1 -e ignore \
60	    porch -f $(atf_get_srcdir)/sigint.orch tee file
61	atf_check -o inline:"text\n" cat file
62
63	atf_check porch -f $(atf_get_srcdir)/sigint.orch tee -i file
64	atf_check -o inline:"text\ntext\n" cat file
65}
66
67atf_test_case unixsock "cleanup"
68unixsock_pidfile="nc.pid"
69
70unixsock_body()
71{
72	outfile=out.log
73
74	nc -lU logger.sock > "$outfile" &
75	npid=$!
76
77	atf_check -o save:"$unixsock_pidfile" echo "$npid"
78
79	# Wait for the socket to come online, just in case.
80	while [ ! -S logger.sock ]; do
81		sleep 0.1
82	done
83
84	atf_check -o inline:"text over socket\n" -x \
85	    'echo "text over socket" | tee logger.sock'
86
87	atf_check rm "$unixsock_pidfile"
88	atf_check -o inline:"text over socket\n" cat "$outfile"
89}
90unixsock_cleanup()
91{
92	if [ -s "$unixsock_pidfile" ]; then
93		read npid < "$unixsock_pidfile"
94		kill "$npid"
95	fi
96}
97
98atf_init_test_cases()
99{
100	atf_add_test_case single_file
101	atf_add_test_case device
102	atf_add_test_case multiple_file
103	atf_add_test_case append
104	atf_add_test_case sigint_ignored
105	atf_add_test_case unixsock
106}
107