xref: /freebsd/usr.bin/diff/tests/diff_test.sh (revision b4c3e9b5b09c829b4135aff738bd2893ed052377)
1
2atf_test_case simple
3atf_test_case unified
4atf_test_case header
5atf_test_case header_ns
6atf_test_case ifdef
7atf_test_case group_format
8atf_test_case side_by_side
9atf_test_case side_by_side_tabbed
10atf_test_case brief_format
11atf_test_case b230049
12atf_test_case stripcr_o
13atf_test_case b252515
14atf_test_case b278988
15atf_test_case Bflag
16atf_test_case Nflag
17atf_test_case tabsize
18atf_test_case conflicting_format
19atf_test_case label
20atf_test_case report_identical
21atf_test_case non_regular_file
22atf_test_case binary
23atf_test_case functionname
24atf_test_case noderef
25atf_test_case ignorecase
26atf_test_case dirloop
27atf_test_case bigc
28atf_test_case bigu
29atf_test_case prleak
30atf_test_case same
31
32simple_body()
33{
34	atf_check -o file:$(atf_get_srcdir)/simple.out -s eq:1 \
35		diff "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
36
37	atf_check -o file:$(atf_get_srcdir)/simple_e.out -s eq:1 \
38		diff -e "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
39
40	atf_check -o file:$(atf_get_srcdir)/simple_u.out -s eq:1 \
41		diff -u -L input1 -L input2 "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
42
43	atf_check -o file:$(atf_get_srcdir)/simple_n.out -s eq:1 \
44		diff -n "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
45
46	atf_check -o inline:"Files $(atf_get_srcdir)/input1.in and $(atf_get_srcdir)/input2.in differ\n" -s eq:1 \
47		diff -q "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
48
49	atf_check \
50		diff -q "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input1.in"
51
52	atf_check -o file:$(atf_get_srcdir)/simple_i.out -s eq:1 \
53		diff -i "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
54
55	atf_check -o file:$(atf_get_srcdir)/simple_w.out -s eq:1 \
56		diff -w "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
57
58	atf_check -o file:$(atf_get_srcdir)/simple_b.out -s eq:1 \
59		diff -b "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
60
61	atf_check -o file:$(atf_get_srcdir)/simple_p.out -s eq:1 \
62		diff --label input_c1.in --label input_c2.in -p "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
63}
64
65unified_body()
66{
67	atf_check -o file:$(atf_get_srcdir)/unified_p.out -s eq:1 \
68		diff -up -L input_c1.in -L input_c2.in  "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
69	atf_check -o file:$(atf_get_srcdir)/unified_9999.out -s eq:1 \
70		diff -u9999 -L input_c1.in -L input_c2.in "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
71}
72
73b230049_body()
74{
75	printf 'a\nb\r\nc\n' > b230049_a.in
76	printf 'a\r\nb\r\nc\r\n' > b230049_b.in
77	atf_check -o empty -s eq:0 \
78		diff -up --strip-trailing-cr -L b230049_a.in -L b230049_b.in \
79		    b230049_a.in b230049_b.in
80}
81
82stripcr_o_body()
83{
84	printf 'a\nX\nc\n' > stripcr_o_X.in
85	printf 'a\r\nY\r\nc\r\n' > stripcr_o_Y.in
86	atf_check -o "file:$(atf_get_srcdir)/strip_o.out" -s eq:1 \
87		diff -L1 -L2 -u --strip-trailing-cr stripcr_o_X.in stripcr_o_Y.in
88}
89
90b252515_body()
91{
92	printf 'a b\n' > b252515_a.in
93	printf 'a  b\n' > b252515_b.in
94	atf_check -o empty -s eq:0 \
95		diff -qw b252515_a.in b252515_b.in
96}
97
98b278988_body()
99{
100	printf 'a\nb\nn' > b278988.a.in
101	printf 'a\n\nb\nn' > b278988.b.in
102	atf_check -o empty -s eq:0 \
103		diff -Bw b278988.a.in b278988.b.in
104}
105
106header_body()
107{
108	export TZ=UTC
109	: > empty
110	echo hello > hello
111	touch -d 2015-04-03T01:02:03 empty
112	touch -d 2016-12-22T11:22:33 hello
113	atf_check -o "file:$(atf_get_srcdir)/header.out" -s eq:1 \
114		diff -u empty hello
115}
116
117header_ns_body()
118{
119	export TZ=UTC
120	: > empty
121	echo hello > hello
122	touch -d 2015-04-03T01:02:03.123456789 empty
123	touch -d 2016-12-22T11:22:33.987654321 hello
124	atf_check -o "file:$(atf_get_srcdir)/header_ns.out" -s eq:1 \
125		diff -u empty hello
126}
127
128ifdef_body()
129{
130	atf_check -o file:$(atf_get_srcdir)/ifdef.out -s eq:1 \
131		diff -D PLOP "$(atf_get_srcdir)/input_c1.in" \
132		"$(atf_get_srcdir)/input_c2.in"
133}
134
135group_format_body()
136{
137	atf_check -o file:$(atf_get_srcdir)/group-format.out -s eq:1 \
138		diff --changed-group-format='<<<<<<< (local)
139%<=======
140%>>>>>>>> (stock)
141' "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
142}
143
144side_by_side_body()
145{
146	atf_check -o save:A printf "A\nB\nC\n"
147	atf_check -o save:B printf "D\nB\nE\n"
148
149	exp_output=$(printf "A[[:space:]]+|[[:space:]]+D\nB[[:space:]]+B\nC[[:space:]]+|[[:space:]]+E")
150	exp_output_suppressed=$(printf "A[[:space:]]+|[[:space:]]+D\nC[[:space:]]+|[[:space:]]+E")
151
152	atf_check -o match:"$exp_output" -s exit:1 \
153	    diff --side-by-side A B
154	atf_check -o match:"$exp_output" -s exit:1 \
155	    diff -y A B
156	atf_check -o match:"$exp_output_suppressed" -s exit:1 \
157	    diff -y --suppress-common-lines A B
158	atf_check -o match:"$exp_output_suppressed" -s exit:1 \
159	    diff -W 65 -y --suppress-common-lines A B
160}
161
162side_by_side_tabbed_body()
163{
164	file_a=$(atf_get_srcdir)/side_by_side_tabbed_a.in
165	file_b=$(atf_get_srcdir)/side_by_side_tabbed_b.in
166
167	atf_check -o save:diffout -s not-exit:0 \
168	    diff -y ${file_a} ${file_b}
169	atf_check -o save:diffout_expanded -s not-exit:0 \
170	    diff -yt ${file_a} ${file_b}
171
172	atf_check -o not-empty grep -Ee 'file A.+file B' diffout
173	atf_check -o not-empty grep -Ee 'file A.+file B' diffout_expanded
174
175	atf_check -o not-empty grep -Ee 'tabs.+tabs' diffout
176	atf_check -o not-empty grep -Ee 'tabs.+tabs' diffout_expanded
177}
178
179brief_format_body()
180{
181	atf_check mkdir A B
182
183	atf_check -x "echo 1 > A/test-file"
184	atf_check -x "echo 2 > B/test-file"
185
186	atf_check cp -Rf A C
187	atf_check cp -Rf A D
188
189	atf_check -x "echo 3 > D/another-test-file"
190
191	atf_check \
192	    -s exit:1 \
193	    -o inline:"Files A/test-file and B/test-file differ\n" \
194	    diff -rq A B
195
196	atf_check diff -rq A C
197
198	atf_check \
199	    -s exit:1 \
200	    -o inline:"Only in D: another-test-file\n" \
201	    diff -rq A D
202
203	atf_check \
204	    -s exit:1 \
205	    -o inline:"Files A/another-test-file and D/another-test-file differ\n" \
206	    diff -Nrq A D
207}
208
209Bflag_body()
210{
211	atf_check -x 'printf "A\nB\n" > A'
212	atf_check -x 'printf "A\n\nB\n" > B'
213	atf_check -x 'printf "A\n \nB\n" > C'
214	atf_check -x 'printf "A\nC\nB\n" > D'
215	atf_check -x 'printf "A\nB\nC\nD\nE\nF\nG\nH" > E'
216	atf_check -x 'printf "A\n\nB\nC\nD\nE\nF\nX\nH" > F'
217
218	atf_check -s exit:0 -o inline:"" diff -B A B
219	atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_C.out" diff -B A C
220	atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_D.out" diff -B A D
221	atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_F.out" diff -B E F
222}
223
224Nflag_body()
225{
226	atf_check -x 'printf "foo" > A'
227
228	atf_check -s exit:1 -o ignore -e ignore diff -N A NOFILE
229	atf_check -s exit:1 -o ignore -e ignore diff -N NOFILE A
230	atf_check -s exit:2 -o ignore -e ignore diff -N NOFILE1 NOFILE2
231}
232
233tabsize_body()
234{
235	printf "\tA\n" > A
236	printf "\tB\n" > B
237
238	atf_check -s exit:1 \
239	    -o inline:"1c1\n<  A\n---\n>  B\n" \
240	    diff -t --tabsize 1 A B
241}
242
243conflicting_format_body()
244{
245	printf "\tA\n" > A
246	printf "\tB\n" > B
247
248	atf_check -s exit:2 -e ignore diff -c -u A B
249	atf_check -s exit:2 -e ignore diff -e -f A B
250	atf_check -s exit:2 -e ignore diff -y -q A B
251	atf_check -s exit:2 -e ignore diff -q -u A B
252	atf_check -s exit:2 -e ignore diff -q -c A B
253	atf_check -s exit:2 -e ignore diff --normal -c A B
254	atf_check -s exit:2 -e ignore diff -c --normal A B
255
256	atf_check -s exit:1 -o ignore -e ignore diff -u -u A B
257	atf_check -s exit:1 -o ignore -e ignore diff -e -e A B
258	atf_check -s exit:1 -o ignore -e ignore diff -y -y A B
259	atf_check -s exit:1 -o ignore -e ignore diff -q -q A B
260	atf_check -s exit:1 -o ignore -e ignore diff -c -c A B
261	atf_check -s exit:1 -o ignore -e ignore diff --normal --normal A B
262}
263
264label_body()
265{
266	printf "\tA\n" > A
267
268	atf_check -o inline:"Files hello and world are identical\n" \
269		-s exit:0 diff --label hello --label world -s A A
270
271	atf_check -o inline:"Binary files hello and world differ\n" \
272		-s exit:1 diff --label hello --label world `which diff` `which ls`
273}
274
275report_identical_head()
276{
277	atf_set "require.user" unprivileged
278}
279report_identical_body()
280{
281	printf "\tA\n" > A
282	printf "\tB\n" > B
283	atf_check -s exit:0 -o match:"are identical" \
284		  diff -s A A
285	atf_check -s exit:1 -o not-match:"are identical" \
286		  diff -s A B
287	chmod -r B
288	atf_check -s exit:2 -e inline:"diff: B: Permission denied\n" \
289		-o empty diff -s A B
290}
291
292non_regular_file_body()
293{
294	printf "\tA\n" > A
295	mkfifo B
296	printf "\tA\n" > B &
297
298	atf_check diff A B
299	printf "\tB\n" > B &
300	atf_check -s exit:1 \
301		-o inline:"--- A\n+++ B\n@@ -1 +1 @@\n-\tA\n+\tB\n" \
302		diff --label A --label B -u A B
303}
304
305binary_body()
306{
307	# the NUL byte has to be after at least BUFSIZ bytes to trick asciifile()
308	yes 012345678901234567890123456789012345678901234567890 | head -n 174 > A
309	cp A B
310	printf '\n\0\n' >> A
311	printf '\nx\n' >> B
312
313	atf_check -o inline:"Binary files A and B differ\n" -s exit:1 diff A B
314	atf_check -o inline:"176c\nx\n.\n" -s exit:1 diff -ae A B
315}
316
317functionname_body()
318{
319	atf_check -o file:$(atf_get_srcdir)/functionname_c.out -s exit:1 \
320		diff -u -p -L functionname.in -L functionname_c.in \
321		"$(atf_get_srcdir)/functionname.in" "$(atf_get_srcdir)/functionname_c.in"
322
323	atf_check -o file:$(atf_get_srcdir)/functionname_objcm.out -s exit:1 \
324		diff -u -p -L functionname.in -L functionname_objcm.in \
325		"$(atf_get_srcdir)/functionname.in" "$(atf_get_srcdir)/functionname_objcm.in"
326
327	atf_check -o file:$(atf_get_srcdir)/functionname_objcclassm.out -s exit:1 \
328		diff -u -p -L functionname.in -L functionname_objcclassm.in \
329		"$(atf_get_srcdir)/functionname.in" "$(atf_get_srcdir)/functionname_objcclassm.in"
330}
331
332noderef_body()
333{
334	atf_check mkdir A B
335
336	atf_check -x "echo 1 > A/test-file"
337	atf_check -x "echo 1 > test-file"
338	atf_check -x "echo 1 > test-file2"
339
340	atf_check ln -s $(pwd)/test-file B/test-file
341
342	atf_check -o empty -s exit:0 diff -r A B
343	atf_check -o inline:"File A/test-file is a file while file B/test-file is a symbolic link\n" \
344		-s exit:1 diff -r --no-dereference A B
345
346	# both test files are now the same symbolic link
347	atf_check rm A/test-file
348
349	atf_check ln -s $(pwd)/test-file A/test-file
350	atf_check -o empty -s exit:0 diff -r A B
351	atf_check -o empty -s exit:0 diff -r --no-dereference A B
352
353	# make test files different symbolic links, but same contents
354	atf_check unlink A/test-file
355	atf_check ln -s $(pwd)/test-file2 A/test-file
356
357	atf_check -o empty -s exit:0 diff -r A B
358	atf_check -o inline:"Symbolic links A/test-file and B/test-file differ\n" -s exit:1 diff -r --no-dereference A B
359}
360
361ignorecase_body()
362{
363	atf_check mkdir A
364	atf_check mkdir B
365
366	atf_check -x "echo hello > A/foo"
367	atf_check -x "echo hello > B/FOO"
368
369	atf_check -o empty -s exit:0 diff -u -r --ignore-file-name-case A B
370}
371
372dirloop_head()
373{
374	atf_set "timeout" "10"
375}
376dirloop_body()
377{
378	atf_check mkdir -p a/foo/bar
379	atf_check ln -s .. a/foo/bar/up
380	atf_check cp -a a b
381	atf_check \
382	    -e match:"a/foo/bar/up: Directory loop detected" \
383	    -e match:"b/foo/bar/up: Directory loop detected" \
384	    diff -r a b
385}
386
387bigc_head()
388{
389	atf_set "descr" "Context diff with very large context"
390}
391bigc_body()
392{
393	echo $'x\na\ny' >a
394	echo $'x\nb\ny' >b
395	atf_check -s exit:2 -e ignore diff -C$(((1<<31)-1)) a b
396	atf_check -s exit:1 -o match:'--- 1,3 ---' \
397	    diff -C$(((1<<31)-2)) a b
398	atf_check -s exit:1 -o match:'--- 1,3 ---' \
399	    diff -Astone -C$(((1<<31)-2)) a b
400}
401
402bigu_head()
403{
404	atf_set "descr" "Unified diff with very large context"
405}
406bigu_body()
407{
408	echo $'x\na\ny' >a
409	echo $'x\nb\ny' >b
410	atf_check -s exit:2 -e ignore diff -U$(((1<<31)-1)) a b
411	atf_check -s exit:1 -o match:'^@@ -1,3 \+1,3 @@$' \
412	    diff -U$(((1<<31)-2)) a b
413	atf_check -s exit:1 -o match:'^@@ -1,3 \+1,3 @@$' \
414	    diff -Astone -U$(((1<<31)-2)) a b
415}
416
417prleak_head()
418{
419	atf_set "descr" "Verify that pagination does not leak resources"
420}
421prleak_body()
422{
423	local n=32
424	mkdir a b
425	for hi in $(jot -w%02x $n 0) ; do
426		mkdir a/$hi b/$hi
427		for lo in $(jot -w%02x $n 0) ; do
428			echo "$hi$lo" >a/$hi/$lo
429			echo "$hi$lo" >b/$hi/$lo
430		done
431	done
432	ulimit -n 1000
433	ulimit -u 1000
434	atf_check diff -rul a b
435	atf_check diff -Astone -rul a b
436}
437
438same_head()
439{
440	atf_set "descr" "Don't diff a file or directory with itself"
441}
442same_body()
443{
444	local n=256
445	mkdir a
446	for hi in $(jot -w%02x $n 0) ; do
447		mkdir a/$hi
448		for lo in $(jot -w%02x $n 0) ; do
449			echo "$hi$lo" >a/$hi/$lo
450		done
451	done
452	ln -s a b
453	atf_check timeout 1s diff -rqs a b
454}
455
456atf_init_test_cases()
457{
458	atf_add_test_case simple
459	atf_add_test_case unified
460	atf_add_test_case header
461	atf_add_test_case header_ns
462	atf_add_test_case ifdef
463	atf_add_test_case group_format
464	atf_add_test_case side_by_side
465	atf_add_test_case side_by_side_tabbed
466	atf_add_test_case brief_format
467	atf_add_test_case b230049
468	atf_add_test_case stripcr_o
469	atf_add_test_case b252515
470	atf_add_test_case b278988
471	atf_add_test_case Bflag
472	atf_add_test_case Nflag
473	atf_add_test_case tabsize
474	atf_add_test_case conflicting_format
475	atf_add_test_case label
476	atf_add_test_case report_identical
477	atf_add_test_case non_regular_file
478	atf_add_test_case binary
479	atf_add_test_case functionname
480	atf_add_test_case noderef
481	atf_add_test_case ignorecase
482	atf_add_test_case dirloop
483	atf_add_test_case bigc
484	atf_add_test_case bigu
485	atf_add_test_case prleak
486	atf_add_test_case same
487}
488