xref: /freebsd/usr.bin/touch/tests/touch_test.sh (revision 74a4aa9b1517d92bfa85b0b1cd7d4c1262bb1ef9)
1#
2# Copyright (c) 2024 Dag-Erling Smørgrav
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6
7export TZ=UTC
8
9atf_check_mtime()
10{
11	local mtime=$1 filename=$2
12	atf_check -o inline:"$((mtime))\n" stat -f%m "$filename"
13}
14
15atf_test_case touch_none
16touch_none_head()
17{
18	atf_set descr "No arguments"
19}
20touch_none_body()
21{
22	atf_check -s exit:1 -e match:"^usage" touch
23}
24
25atf_test_case touch_one
26touch_one_head()
27{
28	atf_set descr "One argument"
29}
30touch_one_body()
31{
32	atf_check touch foo
33	atf_check test -f foo
34}
35
36atf_test_case touch_multiple
37touch_multiple_head()
38{
39	atf_set descr "Multiple arguments"
40}
41touch_multiple_body()
42{
43	atf_check touch foo bar baz
44	atf_check test -f foo -a -f bar -a -f baz
45}
46
47atf_test_case touch_absolute
48touch_absolute_head()
49{
50	atf_set descr "Absolute date / time"
51}
52touch_absolute_body()
53{
54	atf_check touch -t 7001010101 foo
55	atf_check_mtime 3660 foo
56	atf_check rm foo
57
58	atf_check touch -t 7001010101.01 foo
59	atf_check_mtime 3661 foo
60	atf_check rm foo
61
62	atf_check touch -t 196912312359 foo
63	atf_check_mtime -60 foo
64	atf_check rm foo
65
66	atf_check touch -t 196912312359.58 foo
67	atf_check_mtime -2 foo
68	atf_check rm foo
69
70	atf_check touch -t 196912312359.59 foo
71	atf_expect_fail "VFS interprets -1 as “do not set”"
72	atf_check_mtime -1 foo
73	atf_check rm foo
74
75	atf_check touch -d1969-12-31T23:59:58 foo
76	atf_check_mtime -2 foo
77	atf_check rm foo
78
79	atf_check touch -d1969-12-31\ 23:59:58 foo
80	atf_check_mtime -2 foo
81	atf_check rm foo
82
83	atf_check env TZ=CET touch -d1970-01-01T00:59:58 foo
84	atf_check_mtime -2 foo
85	atf_check rm foo
86
87	atf_check env TZ=CET touch -d1970-01-01T00:59:58Z foo
88	atf_check_mtime 3598 foo
89	atf_check rm foo
90
91	atf_check touch -d1969-12-31T23:59:59Z foo
92	atf_expect_fail "VFS interprets -1 as “do not set”"
93	atf_check_mtime -1 foo
94	atf_check rm foo
95}
96
97atf_test_case touch_relative
98touch_relative_head()
99{
100	atf_set descr "Relative date / time"
101}
102touch_relative_body()
103{
104	atf_check touch -t 202403241234.56 foo
105	atf_check_mtime 1711283696 foo
106	atf_check touch -A -36 foo
107	atf_check_mtime 1711283660 foo
108	atf_check touch -A -0100 foo
109	atf_check_mtime 1711283600 foo
110	atf_check touch -A -010000 foo
111	atf_check_mtime 1711280000 foo
112	atf_check touch -A 010136 foo
113	atf_check_mtime 1711283696 foo
114}
115
116atf_test_case touch_copy
117touch_copy_head()
118{
119	atf_set descr "Copy time from another file"
120}
121touch_copy_body()
122{
123	atf_check touch -t 202403241234.56 foo
124	atf_check_mtime 1711283696 foo
125	atf_check touch -t 7001010000 bar
126	atf_check_mtime 0 bar
127	atf_check touch -r foo bar
128	atf_check_mtime 1711283696 bar
129}
130
131atf_test_case touch_nocreate
132touch_nocreate_head()
133{
134	atf_set descr "Do not create file"
135}
136touch_nocreate_body()
137{
138	atf_check touch -t 202403241234.56 foo
139	atf_check_mtime 1711283696 foo
140	atf_check touch -c -t 7001010000 foo bar
141	atf_check_mtime 0 foo
142	atf_check -s exit:1 test -f bar
143	atf_check touch -c bar
144	atf_check -s exit:1 test -f bar
145}
146
147atf_init_test_cases()
148{
149	atf_add_test_case touch_none
150	atf_add_test_case touch_one
151	atf_add_test_case touch_multiple
152	atf_add_test_case touch_absolute
153	atf_add_test_case touch_relative
154	atf_add_test_case touch_copy
155	atf_add_test_case touch_nocreate
156	# TODO: add test cases for -a, -h, -m
157}
158