xref: /freebsd/usr.bin/touch/tests/touch_test.sh (revision dafba19e42e78cd3d7c9264ece49ddd3d7d70da5)
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_check_atime()
16{
17	local atime=$1 filename=$2
18	atf_check -o inline:"$((atime))\n" stat -f%a "$filename"
19}
20
21atf_test_case touch_none
22touch_none_head()
23{
24	atf_set descr "No arguments"
25}
26touch_none_body()
27{
28	atf_check -s exit:1 -e match:"^usage" touch
29}
30
31atf_test_case touch_one
32touch_one_head()
33{
34	atf_set descr "One argument"
35}
36touch_one_body()
37{
38	atf_check touch foo
39	atf_check test -f foo
40}
41
42atf_test_case touch_multiple
43touch_multiple_head()
44{
45	atf_set descr "Multiple arguments"
46}
47touch_multiple_body()
48{
49	atf_check touch foo bar baz
50	atf_check test -f foo -a -f bar -a -f baz
51}
52
53atf_test_case touch_absolute
54touch_absolute_head()
55{
56	atf_set descr "Absolute date / time"
57}
58touch_absolute_body()
59{
60	atf_check touch -t 7001010101 foo
61	atf_check_mtime 3660 foo
62	atf_check rm foo
63
64	atf_check touch -t 7001010101.01 foo
65	atf_check_mtime 3661 foo
66	atf_check rm foo
67
68	atf_check touch -t 196912312359 foo
69	atf_check_mtime -60 foo
70	atf_check rm foo
71
72	atf_check touch -t 196912312359.58 foo
73	atf_check_mtime -2 foo
74	atf_check rm foo
75
76	atf_check touch -t 196912312359.59 foo
77	atf_expect_fail "VFS interprets -1 as “do not set”"
78	atf_check_mtime -1 foo
79	atf_check rm foo
80
81	atf_check touch -d1969-12-31T23:59:58 foo
82	atf_check_mtime -2 foo
83	atf_check rm foo
84
85	atf_check touch -d1969-12-31\ 23:59:58 foo
86	atf_check_mtime -2 foo
87	atf_check rm foo
88
89	atf_check env TZ=CET touch -d1970-01-01T00:59:58 foo
90	atf_check_mtime -2 foo
91	atf_check rm foo
92
93	atf_check env TZ=CET touch -d1970-01-01T00:59:58Z foo
94	atf_check_mtime 3598 foo
95	atf_check rm foo
96
97	atf_check touch -d1969-12-31T23:59:59Z foo
98	atf_expect_fail "VFS interprets -1 as “do not set”"
99	atf_check_mtime -1 foo
100	atf_check rm foo
101}
102
103atf_test_case touch_relative
104touch_relative_head()
105{
106	atf_set descr "Relative date / time"
107}
108touch_relative_body()
109{
110	atf_check touch -t 202403241234.56 foo
111	atf_check_mtime 1711283696 foo
112	atf_check touch -A -36 foo
113	atf_check_mtime 1711283660 foo
114	atf_check touch -A -0100 foo
115	atf_check_mtime 1711283600 foo
116	atf_check touch -A -010000 foo
117	atf_check_mtime 1711280000 foo
118	atf_check touch -A 010136 foo
119	atf_check_mtime 1711283696 foo
120}
121
122atf_test_case touch_copy
123touch_copy_head()
124{
125	atf_set descr "Copy time from another file"
126}
127touch_copy_body()
128{
129	atf_check touch -t 202403241234.56 foo
130	atf_check_mtime 1711283696 foo
131	atf_check touch -t 7001010000 bar
132	atf_check_mtime 0 bar
133	atf_check touch -r foo bar
134	atf_check_mtime 1711283696 bar
135}
136
137atf_test_case touch_nocreate
138touch_nocreate_head()
139{
140	atf_set descr "Do not create file"
141}
142touch_nocreate_body()
143{
144	atf_check touch -t 202403241234.56 foo
145	atf_check_mtime 1711283696 foo
146	atf_check touch -c -t 7001010000 foo bar
147	atf_check_mtime 0 foo
148	atf_check -s exit:1 test -f bar
149	atf_check touch -c bar
150	atf_check -s exit:1 test -f bar
151}
152
153atf_test_case touch_symlink_h_flag
154touch_symlink_h_flag_head()
155{
156	atf_set descr "Update time of symlink but not file pointed to"
157}
158touch_symlink_h_flag_body()
159{
160	atf_check touch -t 200406151337 pointed
161	atf_check ln -s pointed symlink
162	atf_check touch -t 197209071337 -h symlink
163	atf_check_mtime 1087306620 pointed
164	atf_check_mtime 84721020 symlink
165}
166
167atf_test_case touch_symlink_no_h_flag
168touch_symlink_no_h_flag_head()
169{
170	atf_set descr "Update time of file pointed to but not symlink"
171}
172touch_symlink_no_h_flag_body()
173{
174	atf_check touch -t 200406151337 pointed
175	atf_check ln -s pointed symlink
176	local orig_mtime=$(stat -f %m symlink)
177	atf_check touch -t 197209071337 symlink
178	atf_check_mtime 84721020 pointed
179	atf_check_mtime $orig_mtime symlink
180}
181
182atf_test_case touch_just_atime
183touch_just_atime_head()
184{
185	atf_set descr "Update just access time of file (-a)"
186}
187touch_just_atime_body()
188{
189	atf_check touch -t 200406151337 file
190	atf_check touch -at 197209071337 file
191	atf_check_mtime 1087306620 file
192	atf_check_atime 84721020 file
193}
194
195atf_test_case touch_just_mtime
196touch_just_mtime_head()
197{
198	atf_set descr "Update just modify time of file (-m)"
199}
200touch_just_mtime_body()
201{
202	atf_check touch -t 200406151337 file
203	atf_check touch -mt 197209071337 file
204	atf_check_mtime 84721020 file
205	atf_check_atime 1087306620 file
206}
207
208atf_init_test_cases()
209{
210	atf_add_test_case touch_none
211	atf_add_test_case touch_one
212	atf_add_test_case touch_multiple
213	atf_add_test_case touch_absolute
214	atf_add_test_case touch_relative
215	atf_add_test_case touch_copy
216	atf_add_test_case touch_nocreate
217	atf_add_test_case touch_symlink_h_flag
218	atf_add_test_case touch_symlink_no_h_flag
219	atf_add_test_case touch_just_atime
220	atf_add_test_case touch_just_mtime
221}
222