t_hello.sh revision 1.4 1 # $NetBSD: t_hello.sh,v 1.4 2017/05/13 23:51:39 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
28 atf_test_case hello
29 hello_head() {
30 atf_set "descr" "compile and run \"hello world\""
31 atf_set "require.progs" "cc"
32 }
33
34 atf_test_case hello_pic
35 hello_pic_head() {
36 atf_set "descr" "compile and run PIC \"hello world\""
37 atf_set "require.progs" "cc"
38 }
39
40 atf_test_case hello_pie
41 hello_pie_head() {
42 atf_set "descr" "compile and run position independent (PIE) \"hello world\""
43 atf_set "require.progs" "cc"
44 }
45
46 atf_test_case hello32
47 hello32_head() {
48 atf_set "descr" "compile and run \"hello world\" for/in netbsd32 emulation"
49 atf_set "require.progs" "cc file diff cat"
50 }
51
52 hello_body() {
53 cat > test.c << EOF
54 #include <stdio.h>
55 #include <stdlib.h>
56 int main(void) {printf("hello world\n");exit(0);}
57 EOF
58 atf_check -s exit:0 -o ignore -e ignore cc -o hello test.c
59 atf_check -s exit:0 -o inline:"hello world\n" ./hello
60 }
61
62 hello_pic_body() {
63 cat > test.c << EOF
64 #include <stdlib.h>
65 int callpic(void);
66 int main(void) {callpic();exit(0);}
67 EOF
68 cat > pic.c << EOF
69 #include <stdio.h>
70 int callpic(void) {printf("hello world\n");return 0;}
71 EOF
72
73 atf_check -s exit:0 -o ignore -e ignore \
74 cc -fPIC -shared -o libtest.so pic.c
75 atf_check -s exit:0 -o ignore -e ignore \
76 cc -o hello test.c -L. -ltest
77
78 export LD_LIBRARY_PATH=.
79 atf_check -s exit:0 -o inline:"hello world\n" ./hello
80 }
81
82 hello_pie_body() {
83 # check whether this arch supports -pie
84 if ! cc -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
85 atf_skip "cc -pie not supported on this architecture"
86 fi
87 cat > test.c << EOF
88 #include <stdio.h>
89 #include <stdlib.h>
90 int main(void) {printf("hello world\n");exit(0);}
91 EOF
92 atf_check -s exit:0 -o ignore -e ignore cc -fpie -pie -o hello test.c
93 atf_check -s exit:0 -o inline:"hello world\n" ./hello
94 }
95
96 hello32_body() {
97 # check whether this arch is 64bit
98 if ! cc -dM -E - < /dev/null | fgrep -q _LP64; then
99 atf_skip "this is not a 64 bit architecture"
100 fi
101 if ! cc -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
102 atf_skip "cc -m32 not supported on this architecture"
103 else
104 if fgrep -q _LP64 ./def32; then
105 atf_fail "cc -m32 does not generate netbsd32 binaries"
106 fi
107 fi
108
109 cat > test.c << EOF
110 #include <stdio.h>
111 #include <stdlib.h>
112 int main(void) {printf("hello world\n");exit(0);}
113 EOF
114 atf_check -s exit:0 -o ignore -e ignore cc -o hello32 -m32 test.c
115 atf_check -s exit:0 -o ignore -e ignore cc -o hello64 test.c
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.c << EOF
129 #include <stdio.h>
130 #include <stdlib.h>
131 int main(void) {printf("hello static world\n");exit(0);}
132 EOF
133 atf_check -s exit:0 -o ignore -e ignore cc -o hello -m32 \
134 -static test.c
135 atf_check -s exit:0 -o inline:"hello static world\n" ./hello
136 }
137
138 atf_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