xref: /freebsd/usr.bin/head/tests/head_test.sh (revision 5f4c09dd85bff675e0ca63c55ea3c517e0fddfcc)
1# Copyright (c) 2017 Fred Schlechter
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7# 1. Redistributions of source code must retain the above copyright
8#    notice, this list of conditions and the following disclaimer.
9# 2. Redistributions in binary form must reproduce the above copyright
10#    notice, this list of conditions and the following disclaimer in the
11#    documentation and/or other materials provided with the distribution.
12#
13# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23# SUCH DAMAGE.
24#
25
26atf_test_case empty_file
27empty_file_head() {
28	atf_set "descr" "Test head(1)'s handling of an empty file"
29}
30empty_file_body() {
31	touch infile expectfile
32	head infile > outfile
33	head < infile > outpipe
34	atf_check cmp expectfile outfile
35	atf_check cmp expectfile outpipe
36}
37
38atf_test_case default_no_options
39default_no_options_head() {
40	atf_set "descr" "Test head(1)'s default mode"
41}
42default_no_options_body() {
43	#head(1) is supposed to default to 10 lines of output. Verify that it does that.
44	jot -b test 10 > expectfile
45	jot -b test 100 > infile
46	head infile > outfile
47	atf_check -e empty cmp expectfile outfile
48}
49
50atf_test_case line_count
51line_count_head() {
52	atf_set "descr" "Test head(1)'s -n option"
53}
54line_count_body() {
55	jot -b test 100 > outfile
56	head -n 50 outfile > expectfile
57	atf_check -o inline:"      50 expectfile\n" wc -l expectfile
58}
59
60atf_test_case byte_count
61byte_count_head() {
62	atf_set "descr" "Test head(1)'s -c option"
63}
64byte_count_body() {
65	jot -b test 100 > outfile
66	head -c 50 outfile > expectfile
67	atf_check -o inline:"      50 expectfile\n" wc -c expectfile
68}
69
70atf_test_case sparse_file_text_at_beginning
71sparse_file_text_at_beginning_head() {
72	atf_set "descr" "Test head(1)'s handling of a sparse file with text at the beginning of the file"
73}
74sparse_file_text_at_beginning_body () {
75	jot -b test 10 > outfile
76	truncate -s +1K outfile
77	head -c 512 outfile > expectfile
78	atf_check -o inline:"     512 expectfile\n" wc -c expectfile
79}
80
81atf_test_case sparse_file_text_at_end
82sparse_file_text_at_end_head() {
83	atf_set "descr" "Test head(1)'s handling of a sparse file with text at the end of the file"
84}
85sparse_file_text_at_end_body () {
86	truncate -s +1K infile
87	echo test >> infile
88	head -c 4096 < infile > outpipe
89	atf_check cmp infile outpipe
90}
91
92atf_test_case missing_line_count
93missing_line_count_head() {
94	atf_set "descr" "Test head(1)'s handling of a missing line count arg"
95}
96missing_line_count_body () {
97	jot -b test 100 > outfile
98	atf_check -s not-exit:0 -e not-empty head -n outfile
99}
100
101atf_test_case invalid_line_count
102invalid_line_count_head() {
103	atf_set "descr" "Test head(1)'s handling of an invalid line count arg"
104}
105invalid_line_count_body () {
106	jot -b test 100 > outfile
107	atf_check -s not-exit:0 -e not-empty head -n -10 outfile
108}
109
110atf_test_case read_from_stdin
111read_from_stdin_head() {
112	atf_set "descr" "Test head(1)'s reading of stdin"
113}
114read_from_stdin_body() {
115	#head(1) defaults to head -n 10 if no args are given.
116	jot -b test 10 > outfile
117	jot -b test 20 | head > expectfile
118	atf_check cmp outfile expectfile
119}
120
121atf_test_case silent_header
122silent_header_head() {
123	atf_set "descr" "Test head(1)'s silent header feature"
124}
125silent_header_body() {
126	#head(1) defaults to head -n 10 if no args are given.
127	jot 11 1 11 > file1
128	jot 11 2 12 > file2
129	jot 10 1 10 > expectfile
130	jot 10 2 11 >> expectfile
131	head -q file1 file2 > outfile
132	atf_check cmp outfile expectfile
133}
134
135atf_test_case verbose_header
136verbose_header_head() {
137	atf_set "descr" "Test head(1)'s verbose header feature"
138}
139verbose_header_body() {
140	#head(1) defaults to head -n 10 if no args are given.
141	jot -b test 10 > file1
142	echo '==> file1 <==' > expectfile
143	cat file1 >> expectfile
144	head -v file1 > outfile
145	atf_check cmp outfile expectfile
146}
147
148atf_test_case si_number
149si_number_head() {
150	atf_set "descr" "Test head(1)'s SI number feature"
151}
152si_number_body() {
153	jot -b aaaaaaa 129 > file1
154	jot -b aaaaaaa 128 > expectfile
155	head -c 1k file1 > outfile
156	atf_check cmp outfile expectfile
157	jot 1025 1 1025 > file1
158	jot 1024 1 1024 > expectfile
159	head -n 1k file1 > outfile
160	atf_check cmp outfile expectfile
161}
162
163atf_init_test_cases() {
164	atf_add_test_case empty_file
165	atf_add_test_case default_no_options
166	atf_add_test_case line_count
167	atf_add_test_case byte_count
168	atf_add_test_case sparse_file_text_at_beginning
169	atf_add_test_case sparse_file_text_at_end
170	atf_add_test_case missing_line_count
171	atf_add_test_case invalid_line_count
172	atf_add_test_case read_from_stdin
173	atf_add_test_case silent_header
174	atf_add_test_case verbose_header
175	atf_add_test_case si_number
176}
177