1# $NetBSD$
2#
3# Copyright (c) 2009 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# Postprocess mtree output, canonicalising portions that
29# are expected to differ from one run to another.
30h_postprocess()
31{
32	sed -e '
33		/^#	   user: /s/:.*/: x/
34		/^#	machine: /s/:.*/: x/
35		/^#	   tree: /s/:.*/: x/
36		/^#	   date: /s/:.*/: x/
37		'
38}
39
40h_check()
41{
42        diff -Nru "$1" "$2" || atf_fail "files $1 and $2 differ"
43}
44
45atf_test_case create
46create_head()
47{
48	atf_set "descr" "Create a specfile describing a directory tree"
49}
50create_setup()
51{
52	# create some directories
53	mkdir -p create/a/1 create/a/2 create/b
54	# create some files
55	for file in create/top.file.1 \
56		    create/a/a.file.1 \
57		    create/a/a.file.2 \
58		    create/a/1/a1.file.1 \
59		    create/b/b.file.1 \
60		    create/b/b.file.2
61	do
62		echo "$file" >$file
63	done
64	# hard link to file in same dir
65	ln create/b/b.file.1 create/b/b.hardlink.1
66	# hard link to file in another dir
67	ln create/b/b.file.2 create/a/a.hardlink.b2
68	# symlink to file
69	ln -s a.file.1 create/a.symlink.1
70	# symlink to dir
71	ln -s b create/top.symlink.b
72	# dangling symlink
73	ln -s nonexistent create/top.dangling
74}
75create_body()
76{
77	create_setup
78
79	# run mtree and check output
80	( cd create && mtree -c -k type,nlink,link,size,sha256 ) >output.raw \
81	|| atf_fail "mtree exit status $?"
82	h_postprocess <output.raw >output
83	h_check "$(atf_get_srcdir)/d_create.out" output
84}
85
86atf_test_case check
87check_head()
88{
89	atf_set "descr" "Check a directory tree against a specfile"
90}
91check_body()
92{
93	# we use the same directory tree and specfile as in the "create" test
94	create_setup
95
96	# run mtree and check output
97	( cd create && mtree ) <"$(atf_get_srcdir)/d_create.out" >output \
98	|| atf_fail "mtree exit status $?"
99	h_check /dev/null output
100}
101
102atf_test_case convert_C
103convert_C_head()
104{
105	atf_set "descr" "Convert a specfile to mtree -C format, unsorted"
106}
107convert_C_body()
108{
109	mtree -C -K all <"$(atf_get_srcdir)/d_convert.in" >output
110	h_check "$(atf_get_srcdir)/d_convert_C.out" output
111}
112
113atf_test_case convert_C_S
114convert_C_S_head()
115{
116	atf_set "descr" "Convert a specfile to mtree -C format, sorted"
117}
118convert_C_S_body()
119{
120	mtree -C -S -K all <"$(atf_get_srcdir)/d_convert.in" >output
121	h_check "$(atf_get_srcdir)/d_convert_C_S.out" output
122}
123
124atf_test_case convert_D
125convert_D_head()
126{
127	atf_set "descr" "Convert a specfile to mtree -D format, unsorted"
128}
129convert_D_body()
130{
131	mtree -D -K all <"$(atf_get_srcdir)/d_convert.in" >output
132	h_check "$(atf_get_srcdir)/d_convert_D.out" output
133}
134
135atf_test_case convert_D_S
136convert_D_S_head()
137{
138	atf_set "descr" "Convert a specfile to mtree -D format, sorted"
139}
140convert_D_S_body()
141{
142	mtree -D -S -K all <"$(atf_get_srcdir)/d_convert.in" >output
143	h_check "$(atf_get_srcdir)/d_convert_D_S.out" output
144}
145
146atf_test_case merge
147merge_head()
148{
149	atf_set "descr" "Merge records of different type"
150}
151merge_body()
152{
153	mtree -C -M -K all <"$(atf_get_srcdir)/d_merge.in" >output
154	h_check "$(atf_get_srcdir)/d_merge_C_M.out" output
155	# same again, with sorting
156	mtree -C -M -S -K all <"$(atf_get_srcdir)/d_merge.in" >output
157	h_check "$(atf_get_srcdir)/d_merge_C_M_S.out" output
158}
159
160atf_init_test_cases()
161{
162	atf_add_test_case create
163	atf_add_test_case check
164	atf_add_test_case convert_C
165	atf_add_test_case convert_C_S
166	atf_add_test_case convert_D
167	atf_add_test_case convert_D_S
168	atf_add_test_case merge
169}
170