t_hello.sh revision 1.1
1#	$NetBSD: t_hello.sh,v 1.1 2017/05/14 00:07:07 kamil 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_pic
35hello_pic_head() {
36	atf_set "descr" "compile and run PIC \"hello world\""
37	atf_set "require.progs" "c++"
38}
39
40atf_test_case hello_pie
41hello_pie_head() {
42	atf_set "descr" "compile and run position independent (PIE) \"hello world\""
43	atf_set "require.progs" "c++"
44}
45
46atf_test_case hello32
47hello32_head() {
48	atf_set "descr" "compile and run \"hello world\" for/in netbsd32 emulation"
49	atf_set "require.progs" "c++ file diff cat"
50}
51
52hello_body() {
53	cat > test.cpp << EOF
54#include <stdio.h>
55#include <stdlib.h>
56int main(void) {printf("hello world\n");exit(0);}
57EOF
58	atf_check -s exit:0 -o ignore -e ignore c++ -o hello test.cpp
59	atf_check -s exit:0 -o inline:"hello world\n" ./hello
60}
61
62hello_pic_body() {
63	cat > test.cpp << EOF
64#include <stdlib.h>
65int callpic(void);
66int main(void) {callpic();exit(0);}
67EOF
68	cat > pic.cpp << EOF
69#include <stdio.h>
70int callpic(void) {printf("hello world\n");return 0;}
71EOF
72
73	atf_check -s exit:0 -o ignore -e ignore \
74	    c++ -fPIC -shared -o libtest.so pic.cpp
75	atf_check -s exit:0 -o ignore -e ignore \
76	    c++ -o hello test.cpp -L. -ltest
77
78	export LD_LIBRARY_PATH=.
79	atf_check -s exit:0 -o inline:"hello world\n" ./hello
80}
81
82hello_pie_body() {
83	# check whether this arch supports -pie
84	if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
85		atf_skip "c++ -pie not supported on this architecture"
86	fi
87	cat > test.cpp << EOF
88#include <stdio.h>
89#include <stdlib.h>
90int main(void) {printf("hello world\n");exit(0);}
91EOF
92	atf_check -s exit:0 -o ignore -e ignore c++ -fpie -pie -o hello test.cpp
93	atf_check -s exit:0 -o inline:"hello world\n" ./hello
94}
95
96hello32_body() {
97	# check whether this arch is 64bit
98	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
99		atf_skip "this is not a 64 bit architecture"
100	fi
101	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
102		atf_skip "c++ -m32 not supported on this architecture"
103	else
104		if fgrep -q _LP64 ./def32; then
105			atf_fail "c++ -m32 does not generate netbsd32 binaries"
106		fi
107	fi
108
109	cat > test.cpp << EOF
110#include <stdio.h>
111#include <stdlib.h>
112int main(void) {printf("hello world\n");exit(0);}
113EOF
114	atf_check -s exit:0 -o ignore -e ignore c++ -o hello32 -m32 test.cpp
115	atf_check -s exit:0 -o ignore -e ignore c++ -o hello64 test.cpp
116	file -b ./hello32 > ./ftype32
117	file -b ./hello64 > ./ftype64
118	if diff ./ftype32 ./ftype64 >/dev/null; then
119		atf_fail "generated binaries do not differ"
120	fi
121	echo "32bit binaries on this platform are:"
122	cat ./ftype32
123	echo "While native (64bit) binaries are:"
124	cat ./ftype64
125	atf_check -s exit:0 -o inline:"hello world\n" ./hello32
126
127	# do another test with static 32bit binaries
128	cat > test.cpp << EOF
129#include <stdio.h>
130#include <stdlib.h>
131int main(void) {printf("hello static world\n");exit(0);}
132EOF
133	atf_check -s exit:0 -o ignore -e ignore c++ -o hello -m32 \
134	    -static test.cpp
135	atf_check -s exit:0 -o inline:"hello static world\n" ./hello
136}
137
138atf_init_test_cases()
139{
140
141	atf_add_test_case hello
142	atf_add_test_case hello_pic
143	atf_add_test_case hello_pie
144	atf_add_test_case hello32
145}
146