Imported Upstream version 2.4.2
[platform/upstream/libtool.git] / tests / loadlibrary.at
1 # loadlibrary.at -- test loadlibrary functionality          -*- Autotest -*-
2 #
3 #   Copyright (C) 2010 Free Software Foundation, Inc.
4 #   This file is part of GNU Libtool.
5 #
6 # GNU Libtool is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; either version 2 of
9 # the License, or (at your option) any later version.
10 #
11 # GNU Libtool is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Libtool; see the file COPYING.  If not, a copy
18 # can be downloaded from  http://www.gnu.org/licenses/gpl.html,
19 # or obtained by writing to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 ####
22
23 AT_SETUP([loadlibrary error messages])
24 AT_KEYWORDS([libltdl])
25
26 eval "`$LIBTOOL --config | $EGREP '^(objdir)='`"
27
28 AT_DATA([main.c],
29 [[#include <ltdl.h>
30 #include <stdio.h>
31
32 static int
33 standard_error_message(const char *error)
34 {
35   int error_number;
36
37   for (error_number = 0; error_number < LT_ERROR_MAX; ++error_number)
38     {
39       lt_dlseterror (error_number);
40       if (error == lt_dlerror ())
41         {
42           return 1;
43         }
44     }
45
46   lt_dlseterror (LT_ERROR_UNKNOWN);
47   return 0;
48 }
49
50 int
51 main (int argc, char* argv[])
52 {
53   int err = 0;
54   lt_dlhandle module = NULL;
55   const lt_dlvtable *loadlibrary;
56   const lt_dlvtable *preopen;
57   lt_dlloader loader = NULL;
58   lt_dlloader next;
59   const lt_dlvtable *vtable;
60   void *symbol;
61   const char *error;
62
63   if (argc < 2)
64     {
65       fprintf (stderr, "usage: %s plugin [symbol]\n", argv[0]);
66       return 1;
67     }
68
69   lt_dlinit ();
70
71   loadlibrary = lt_dlloader_find ("lt_loadlibrary");
72   if (!loadlibrary)
73     {
74       /* Skip if the loadlibrary loader isn't supported */
75       printf ("loadlibrary loader not found\n");
76       err = 77;
77       goto cleanup;
78     }
79
80   preopen = lt_dlloader_find ("lt_preopen");
81   if (!loadlibrary)
82     {
83       printf ("preopen loader not found\n");
84       err = 2;
85       goto cleanup;
86     }
87
88   /* Remove all loaders except the preopen and loadlibrary loaders. */
89   while (next = lt_dlloader_next (loader))
90     {
91       if (lt_dlloader_get (next) == loadlibrary)
92         {
93           loader = next;
94           continue;
95         }
96
97       if (lt_dlloader_get (next) == preopen)
98         {
99           loader = next;
100           continue;
101         }
102
103       lt_dlloader_remove (lt_dlloader_get (next)->name);
104     }
105
106   module = lt_dlopen (argv[1]);
107   error = lt_dlerror ();
108
109   if (module)
110     {
111       printf ("lt_dlopen: success!\n");
112
113       if (argc == 2)
114         {
115           /* But failure was the desired result... */
116           printf ("expected failure\n");
117           err = 2;
118           goto cleanup;
119         }
120     }
121   else if (argc > 2)
122     {
123       /* Didn't expect failure... */
124       printf ("lt_dlopen: failure: %s\n", error);
125       err = 2;
126       goto cleanup;
127     }
128   else if (standard_error_message (error))
129     {
130       /* Expected custom error message... */
131       printf ("lt_dlopen: standard error (bad): %s\n", error);
132       err = 1;
133       goto cleanup;
134     }
135   else
136     {
137       printf ("lt_dlopen: custom error (good): %s\n", error);
138       goto cleanup;
139     }
140
141   symbol = lt_dlsym (module, argv[2]);
142   error = lt_dlerror ();
143
144   if (symbol)
145     {
146       printf ("lt_dlsym: success!\n");
147     }
148   else if (standard_error_message (error))
149     {
150       /* Expected custom failure message... */
151       printf ("lt_dlsym: standard error (bad): %s\n", error);
152       err = 1;
153     }
154   else
155     {
156       printf ("lt_dlsym: custom error (good): %s\n", error);
157     }
158
159 cleanup:
160   if (module)
161     {
162       lt_dlclose (module);
163     }
164   lt_dlexit ();
165   return err;
166 }
167 ]])
168
169 AT_DATA([foomod.c],
170 [[
171 #ifdef __cplusplus
172 extern "C"
173 #endif
174 int foosym (void);
175 int
176 foosym (void)
177 {
178   return 0;
179 }
180 ]])
181
182 AT_DATA([bardep.c],
183 [[
184 #ifdef __cplusplus
185 extern "C"
186 #endif
187 int bardep (void);
188 int
189 bardep (void)
190 {
191   return 0;
192 }
193 ]])
194
195 AT_DATA([barmod.c],
196 [[
197 #ifdef __cplusplus
198 extern "C" {
199 #endif
200 int bardep (void);
201 int barsym (void);
202 #ifdef __cplusplus
203 }
204 #endif
205 int
206 barsym (void)
207 {
208   return bardep ();
209 }
210 ]])
211
212 : ${LTDLINCL="-I$abs_top_srcdir/libltdl"}
213 : ${LIBLTDL="$abs_builddir/../libltdl/libltdlc.la"}
214
215 # Skip this test when called from:
216 #    make distcheck DISTCHECK_CONFIGURE_FLAGS=--disable-ltdl-install
217 AT_CHECK([case $LIBLTDL in #(
218  */_inst/lib/*) test -f $LIBLTDL || (exit 77) ;;
219 esac], [], [ignore])
220
221 CPPFLAGS="$LTDLINCL $CPPFLAGS"
222 inst=`pwd`/inst
223 libdir=$inst/lib
224
225 AT_CHECK([$CC $CPPFLAGS $CFLAGS -c main.c], [], [ignore], [ignore])
226 for file in foomod.c bardep.c barmod.c; do
227   AT_CHECK([$LIBTOOL --mode=compile $CC $CPPFLAGS $CFLAGS -c $file],
228            [], [ignore], [ignore])
229 done
230 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o foomod.la ]dnl
231          [-rpath $libdir -module -avoid-version -no-undefined ]dnl
232          [foomod.lo],
233          [], [ignore], [ignore])
234 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o libbardep.la ]dnl
235          [-rpath $libdir -avoid-version -no-undefined ]dnl
236          [bardep.lo],
237          [], [ignore], [ignore])
238 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o barmod.la ]dnl
239          [-rpath $libdir -module -avoid-version -no-undefined ]dnl
240          [barmod.lo ./libbardep.la],
241          [], [ignore], [ignore])
242
243 AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o main$EXEEXT ]dnl
244          [main.$OBJEXT $LIBLTDL],
245          [], [ignore], [ignore])
246
247 . ./foomod.la
248 AT_CHECK([test -n "$dlname" || (exit 77)])
249
250 LT_AT_EXEC_CHECK([./main], [], [ignore], [ignore], [./foomod.la no_symbol])
251
252 #   chmod -x doesn't appear to work in MSYS, and Wine can load no-exec dlls.
253 dnl chmod -x $objdir/$dlname
254 dnl LT_AT_EXEC_CHECK([./main], [], [ignore], [ignore], [./foomod.la])
255
256 rm -f $objdir/$dlname
257 LT_AT_EXEC_CHECK([./main], [], [ignore], [ignore], [./foomod.la])
258
259 LT_AT_EXEC_CHECK([./main], [], [ignore], [ignore], [./barmod.la no_symbol])
260
261 . ./libbardep.la
262 #   chmod -x doesn't appear to work in MSYS, and Wine can load no-exec dlls.
263 dnl chmod -x $objdir/$dlname
264 dnl LT_AT_EXEC_CHECK([./main], [], [ignore], [ignore], [./barmod.la])
265
266 rm -f $objdir/$dlname
267 LT_AT_EXEC_CHECK([./main], [], [ignore], [ignore], [./barmod.la])
268
269 AT_CLEANUP