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