1# $NetBSD: t_times.sh,v 1.8 2024/04/28 07:27:41 rillig Exp $
2#
3# Copyright (c) 2005, 2006, 2007, 2008 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26#
27
28#
29# Verifies that node times are properly handled.
30#
31
32atf_test_case empty
33empty_head() {
34          atf_set "descr" "Tests that creating an empty file and later" \
35                          "manipulating it updates times correctly"
36          atf_set "require.user" "root"
37}
38empty_body() {
39          test_mount
40
41          atf_check -s exit:0 -o empty -e empty touch a
42          eval $(stat -s a | sed -e 's|st_|ost_|g') || atf_fail "stat failed"
43          [ ${ost_birthtime} -eq ${ost_atime} ] || \
44              atf_fail "Incorrect atime: ${ost_birthtime} != ${ost_atime}"
45          [ ${ost_birthtime} -eq ${ost_ctime} ] || \
46              atf_fail "Incorrect ctime: ${ost_birthtime} != ${ost_ctime}"
47          [ ${ost_birthtime} -eq ${ost_mtime} ] || \
48              atf_fail "Incorrect mtime: ${ost_birthtime} != ${ost_mtime}"
49
50          sleep 1
51          atf_check -s exit:0 -o empty -e empty cat a
52          eval $(stat -s a) || atf_fail "stat failed"
53          [ ${st_atime} -gt ${ost_atime} ] || \
54              atf_fail "Incorrect atime: ${st_atime} <= ${ost_atime}"
55          [ ${st_ctime} -eq ${ost_ctime} ] || \
56              atf_fail "Incorrect ctime: ${st_ctime} != ${ost_ctime}"
57          [ ${st_mtime} -eq ${ost_mtime} ] || \
58              atf_fail "Incorrect mtime: ${st_mtime} != ${ost_mtime}"
59
60          sleep 1
61          echo foo >a || atf_fail "Write failed"
62          eval $(stat -s a) || atf_fail "stat failed"
63          [ ${st_atime} -gt ${ost_atime} ] || \
64              atf_fail "Incorrect atime: ${st_atime} <= ${ost_atime}"
65          [ ${st_ctime} -gt ${ost_ctime} ] || \
66              atf_fail "Incorrect ctime: ${st_ctime} <= ${ost_ctime}"
67          [ ${st_mtime} -gt ${ost_mtime} ] || \
68              atf_fail "Incorrect mtime: ${st_mtime} <= ${ost_mtime}"
69
70          test_unmount
71}
72
73atf_test_case non_empty
74non_empty_head() {
75          atf_set "descr" "Tests that creating a non-empty file and later" \
76                          "manipulating it updates times correctly"
77          atf_set "require.user" "root"
78}
79non_empty_body() {
80          test_mount
81
82          echo foo >b || atf_fail "Non-empty creation failed"
83          eval $(stat -s b | sed -e 's|st_|ost_|g') || atf_fail "stat failed"
84
85          sleep 1
86          atf_check -s exit:0 -o inline:"foo\n" -e empty cat b
87          eval $(stat -s b) || atf_fail "stat failed"
88          [ ${st_atime} -gt ${ost_atime} ] || \
89              atf_fail "Incorrect atime: ${st_atime} <= ${ost_atime}"
90          [ ${st_ctime} -eq ${ost_ctime} ] || \
91              atf_fail "Incorrect ctime: ${st_ctime} != ${ost_ctime}"
92          [ ${st_mtime} -eq ${ost_mtime} ] || \
93              atf_fail "Incorrect mtime: ${st_mtime} != ${ost_mtime}"
94
95          test_unmount
96}
97
98atf_test_case link
99link_head() {
100          atf_set "descr" "Tests that linking to an existing file updates" \
101                          "times correctly"
102          atf_set "require.user" "root"
103}
104link_body() {
105          test_mount
106
107          echo foo >c || atf_fail "Non-empty creation failed"
108          eval $(stat -s c | sed -e 's|st_|ost_|g') || atf_fail "stat failed"
109
110          sleep 1
111          atf_check -s exit:0 -o empty -e empty ln c d
112          eval $(stat -s c) || atf_fail "stat failed"
113          [ ${st_atime} -eq ${ost_atime} ] || \
114              atf_fail "Incorrect atime: ${st_atime} != ${ost_atime}"
115          [ ${st_ctime} -gt ${ost_ctime} ] || \
116              atf_fail "Incorrect ctime: ${st_ctime} <= ${ost_ctime}"
117          [ ${st_mtime} -eq ${ost_mtime} ] || \
118              atf_fail "Incorrect mtime: ${st_mtime} != ${ost_mtime}"
119
120          test_unmount
121}
122
123atf_test_case rename
124rename_head() {
125          atf_set "descr" "Tests that renaming an existing file updates" \
126                          "times correctly"
127          atf_set "require.user" "root"
128}
129rename_body() {
130          test_mount
131
132          atf_check -s exit:0 -o empty -e empty mkdir e
133          echo foo >e/a || atf_fail "Creation failed"
134          eval $(stat -s e | sed -e 's|st_|dost_|g') || atf_fail "stat failed"
135          eval $(stat -s e/a | sed -e 's|st_|ost_|g') || atf_fail "stat failed"
136          sleep 1
137          atf_check -s exit:0 -o empty -e empty mv e/a e/b
138          eval $(stat -s e | sed -e 's|st_|dst_|g') || atf_fail "stat failed"
139          eval $(stat -s e/b) || atf_fail "stat failed"
140          [ ${st_atime} -eq ${ost_atime} ] || \
141              atf_fail "Incorrect atime: ${st_atime} != ${ost_atime}"
142          [ ${st_ctime} -gt ${ost_ctime} ] || \
143              atf_fail "Incorrect ctime: ${st_ctime} <= ${ost_ctime}"
144          [ ${st_mtime} -eq ${ost_mtime} ] || \
145              atf_fail "Incorrect mtime: ${st_mtime} != ${ost_mtime}"
146          [ ${dst_mtime} -gt ${dost_mtime} ] || \
147              atf_fail "Incorrect mtime: ${dst_mtime} <= ${dost_mtime}"
148
149          test_unmount
150}
151
152atf_init_test_cases() {
153          . $(atf_get_srcdir)/../h_funcs.subr
154          . $(atf_get_srcdir)/h_funcs.subr
155
156          atf_add_test_case empty
157          atf_add_test_case non_empty
158          atf_add_test_case link
159          atf_add_test_case rename
160}
161