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