1#	$NetBSD: t_hello.sh,v 1.7 2022/06/12 15:08:38 skrll Exp $
2#
3# Copyright (c) 2011 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
28atf_test_case hello
29hello_head() {
30	atf_set "descr" "compile and run \"hello world\""
31	atf_set "require.progs" "c++"
32}
33
34atf_test_case hello_profile
35hello_profile_head() {
36	atf_set "descr" "compile and run \"hello world\" with profiling option"
37	atf_set "require.progs" "c++"
38}
39
40atf_test_case hello_profile
41hello_profile_32_head() {
42	atf_set "descr" "compile and run 32-bit \"hello world\" with profiling option"
43	atf_set "require.progs" "c++"
44}
45
46atf_test_case hello_static
47hello_static_head() {
48	atf_set "descr" "compile and run \"hello world\" with static option"
49	atf_set "require.progs" "c++"
50}
51
52atf_test_case hello_pic
53hello_pic_head() {
54	atf_set "descr" "compile and run PIC \"hello world\""
55	atf_set "require.progs" "c++"
56}
57
58atf_test_case hello_pic_32
59hello_pic_32_head() {
60	atf_set "descr" "compile and run 32-bit PIC \"hello world\""
61	atf_set "require.progs" "c++"
62}
63
64atf_test_case hello_pic_profile
65hello_pic_profile_head() {
66	atf_set "descr" "compile and run PIC \"hello world\" with profiling option"
67	atf_set "require.progs" "c++"
68}
69
70atf_test_case hello_pic_profile_32
71hello_pic_profile_32_head() {
72	atf_set "descr" "compile and run 32-bit PIC \"hello world\" with profiling option"
73	atf_set "require.progs" "c++"
74}
75
76atf_test_case hello_pie
77hello_pie_head() {
78	atf_set "descr" "compile and run position independent (PIE) \"hello world\""
79	atf_set "require.progs" "c++"
80}
81
82atf_test_case hello32
83hello32_head() {
84	atf_set "descr" "compile and run \"hello world\" for/in netbsd32 emulation"
85	atf_set "require.progs" "c++ file diff cat"
86}
87
88hello_body() {
89	cat > test.cpp << EOF
90#include <stdio.h>
91#include <stdlib.h>
92int main(void) {printf("hello world\n");exit(0);}
93EOF
94	atf_check -s exit:0 -o ignore -e ignore c++ -o hello test.cpp
95	atf_check -s exit:0 -o inline:"hello world\n" ./hello
96}
97
98hello_profile_body() {
99	cat > test.cpp << EOF
100#include <stdio.h>
101#include <stdlib.h>
102int main(void) {printf("hello world\n");exit(0);}
103EOF
104	atf_check -s exit:0 -o ignore -e ignore c++ -static -pg -o hello test.cpp
105	atf_check -s exit:0 -o inline:"hello world\n" ./hello
106}
107
108hello_profile_32_body() {
109	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
110		atf_skip "this is not a 64 bit architecture"
111	fi
112	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
113		atf_skip "c++ -m32 not supported on this architecture"
114	else
115		if fgrep -q _LP64 ./def32; then
116			atf_fail "c++ -m32 does not generate netbsd32 binaries"
117		fi
118	fi
119
120	cat > test.cpp << EOF
121#include <stdio.h>
122#include <stdlib.h>
123int main(void) {printf("hello world\n");exit(0);}
124EOF
125	atf_check -s exit:0 -o ignore -e ignore c++ -static  -m32 -pg -o hello test.cpp
126	atf_check -s exit:0 -o inline:"hello world\n" ./hello
127}
128
129
130hello_static_body() {
131	cat > test.cpp << EOF
132#include <stdio.h>
133#include <stdlib.h>
134int main(void) {printf("hello world\n");exit(0);}
135EOF
136	atf_check -s exit:0 -o ignore -e ignore c++ -static -o hello test.cpp
137	atf_check -s exit:0 -o inline:"hello world\n" ./hello
138}
139
140hello_pic_body() {
141	cat > test.cpp << EOF
142#include <stdlib.h>
143int callpic(void);
144int main(void) {callpic();exit(0);}
145EOF
146	cat > pic.cpp << EOF
147#include <stdio.h>
148int callpic(void) {printf("hello world\n");return 0;}
149EOF
150
151	atf_check -s exit:0 -o ignore -e ignore \
152	    c++ -fPIC -shared -o libtest.so pic.cpp
153	atf_check -s exit:0 -o ignore -e ignore \
154	    c++ -o hello test.cpp -L. -ltest
155
156	export LD_LIBRARY_PATH=.
157	atf_check -s exit:0 -o inline:"hello world\n" ./hello
158}
159
160hello_pic_32_body() {
161	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
162		atf_skip "this is not a 64 bit architecture"
163	fi
164	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
165		atf_skip "c++ -m32 not supported on this architecture"
166	else
167		if fgrep -q _LP64 ./def32; then
168			atf_fail "c++ -m32 does not generate netbsd32 binaries"
169		fi
170	fi
171	cat > test.cpp << EOF
172#include <stdlib.h>
173int callpic(void);
174int main(void) {callpic();exit(0);}
175EOF
176	cat > pic.cpp << EOF
177#include <stdio.h>
178int callpic(void) {printf("hello world\n");return 0;}
179EOF
180
181	atf_check -s exit:0 -o ignore -e ignore \
182	    c++ -m32 -fPIC -shared -o libtest.so pic.cpp
183	atf_check -s exit:0 -o ignore -e ignore \
184	    c++ -m32 -o hello test.cpp -L. -ltest
185
186	export LD_LIBRARY_PATH=.
187	atf_check -s exit:0 -o inline:"hello world\n" ./hello
188}
189
190hello_pic_profile_body() {
191	cat > test.cpp << EOF
192#include <stdlib.h>
193int callpic(void);
194int main(void) {callpic();exit(0);}
195EOF
196	cat > pic.cpp << EOF
197#include <stdio.h>
198int callpic(void) {printf("hello world\n");return 0;}
199EOF
200
201	atf_check -s exit:0 -o ignore -e ignore \
202	    c++ -pg -fPIC -shared -o libtest.so pic.cpp
203	atf_check -s exit:0 -o ignore -e ignore \
204	    c++ -pg -o hello test.cpp -L. -ltest
205
206	export LD_LIBRARY_PATH=.
207	atf_check -s exit:0 -o inline:"hello world\n" ./hello
208}
209
210hello_pic_profile_32_body() {
211	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
212		atf_skip "this is not a 64 bit architecture"
213	fi
214	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
215		atf_skip "c++ -m32 not supported on this architecture"
216	else
217		if fgrep -q _LP64 ./def32; then
218			atf_fail "c++ -m32 does not generate netbsd32 binaries"
219		fi
220	fi
221
222	cat > test.cpp << EOF
223#include <stdlib.h>
224int callpic(void);
225int main(void) {callpic();exit(0);}
226EOF
227	cat > pic.cpp << EOF
228#include <stdio.h>
229int callpic(void) {printf("hello world\n");return 0;}
230EOF
231
232	atf_check -s exit:0 -o ignore -e ignore \
233	    c++ -m32 -pg -fPIC -shared -o libtest.so pic.cpp
234	atf_check -s exit:0 -o ignore -e ignore \
235	    c++ -m32 -pg -o hello test.cpp -L. -ltest
236
237	export LD_LIBRARY_PATH=.
238	atf_check -s exit:0 -o inline:"hello world\n" ./hello
239}
240
241hello_pie_body() {
242	# check whether this arch supports -pie
243	if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
244		atf_skip "c++ -pie not supported on this architecture"
245	fi
246	cat > test.cpp << EOF
247#include <stdio.h>
248#include <stdlib.h>
249int main(void) {printf("hello world\n");exit(0);}
250EOF
251	atf_check -s exit:0 -o ignore -e ignore c++ -fpie -pie -o hello test.cpp
252	atf_check -s exit:0 -o inline:"hello world\n" ./hello
253}
254
255hello32_body() {
256	# check whether this arch is 64bit
257	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
258		atf_skip "this is not a 64 bit architecture"
259	fi
260	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
261		atf_skip "c++ -m32 not supported on this architecture"
262	else
263		if fgrep -q _LP64 ./def32; then
264			atf_fail "c++ -m32 does not generate netbsd32 binaries"
265		fi
266	fi
267
268	cat > test.cpp << EOF
269#include <stdio.h>
270#include <stdlib.h>
271int main(void) {printf("hello world\n");exit(0);}
272EOF
273	atf_check -s exit:0 -o ignore -e ignore c++ -o hello32 -m32 test.cpp
274	atf_check -s exit:0 -o ignore -e ignore c++ -o hello64 test.cpp
275	file -b ./hello32 > ./ftype32
276	file -b ./hello64 > ./ftype64
277	if diff ./ftype32 ./ftype64 >/dev/null; then
278		atf_fail "generated binaries do not differ"
279	fi
280	echo "32bit binaries on this platform are:"
281	cat ./ftype32
282	echo "While native (64bit) binaries are:"
283	cat ./ftype64
284	atf_check -s exit:0 -o inline:"hello world\n" ./hello32
285
286	# do another test with static 32bit binaries
287	cat > test.cpp << EOF
288#include <stdio.h>
289#include <stdlib.h>
290int main(void) {printf("hello static world\n");exit(0);}
291EOF
292	atf_check -s exit:0 -o ignore -e ignore c++ -o hello -m32 \
293	    -static test.cpp
294	atf_check -s exit:0 -o inline:"hello static world\n" ./hello
295}
296
297atf_init_test_cases()
298{
299
300	atf_add_test_case hello
301	atf_add_test_case hello_profile
302	atf_add_test_case hello_pic
303	atf_add_test_case hello_pie
304	atf_add_test_case hello32
305	atf_add_test_case hello_static
306	atf_add_test_case hello_pic_32
307	atf_add_test_case hello_pic_profile
308	atf_add_test_case hello_pic_profile_32
309	atf_add_test_case hello_profile_32
310}
311