1272343Sngie/*	$NetBSD: t_dlopen.c,v 1.1 2013/03/21 16:50:21 christos Exp $ */
2272343Sngie/*-
3272343Sngie * Copyright (c) 2013 The NetBSD Foundation, Inc.
4272343Sngie * All rights reserved.
5272343Sngie *
6272343Sngie * This code is derived from software contributed to The NetBSD Foundation
7272343Sngie * by Emmanuel Dreyfus
8272343Sngie *
9272343Sngie * Redistribution and use in source and binary forms, with or without
10272343Sngie * modification, are permitted provided that the following conditions
11272343Sngie * are met:
12272343Sngie *
13272343Sngie * 1. Redistributions of source code must retain the above copyright
14272343Sngie *    notice, this list of conditions and the following disclaimer.
15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer in
17272343Sngie *    the documentation and/or other materials provided with the
18272343Sngie *    distribution.
19272343Sngie *
20272343Sngie * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22272343Sngie * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23272343Sngie * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24272343Sngie * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25272343Sngie * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26272343Sngie * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27272343Sngie * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28272343Sngie * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29272343Sngie * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30272343Sngie * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31272343Sngie * SUCH DAMAGE.
32272343Sngie */
33272343Sngie
34272343Sngie#include <sys/cdefs.h>
35272343Sngie__RCSID("$NetBSD: t_dlopen.c,v 1.1 2013/03/21 16:50:21 christos Exp $");
36272343Sngie
37272343Sngie#include <atf-c.h>
38272343Sngie#include <dlfcn.h>
39272343Sngie#include <pthread.h>
40272343Sngie#include <unistd.h>
41272343Sngie
42272343SngieATF_TC(dlopen);
43272343Sngie
44272343SngieATF_TC_HEAD(dlopen, tc)
45272343Sngie{
46272343Sngie	atf_tc_set_md_var(tc, "descr",
47272343Sngie	    "Test if dlopen can load -lpthread DSO");
48272343Sngie}
49272343Sngie
50272343Sngie#define DSO TESTDIR "/h_pthread_dlopen.so"
51272343Sngie
52272343SngieATF_TC_BODY(dlopen, tc)
53272343Sngie{
54272343Sngie	void *handle;
55272343Sngie	int (*testf_dso_null)(void);
56272343Sngie	handle = dlopen(DSO, RTLD_NOW | RTLD_LOCAL);
57272343Sngie	ATF_REQUIRE_MSG(handle != NULL, "dlopen fails: %s", dlerror());
58272343Sngie
59272343Sngie	testf_dso_null = dlsym(handle, "testf_dso_null");
60272343Sngie	ATF_REQUIRE_MSG(testf_dso_null != NULL, "dlsym fails: %s", dlerror());
61272343Sngie
62272343Sngie	ATF_REQUIRE(testf_dso_null() == 0xcafe);
63272343Sngie
64272343Sngie	ATF_REQUIRE(dlclose(handle) == 0);
65272343Sngie}
66272343Sngie
67272343SngieATF_TC(dlopen_mutex);
68272343Sngie
69272343SngieATF_TC_HEAD(dlopen_mutex, tc)
70272343Sngie{
71272343Sngie	atf_tc_set_md_var(tc, "descr",
72272343Sngie	    "Test if dlopen can load -lpthread DSO without breaking mutex");
73272343Sngie}
74272343Sngie
75272343SngieATF_TC_BODY(dlopen_mutex, tc)
76272343Sngie{
77272343Sngie	pthread_mutex_t mtx;
78272343Sngie	void *handle;
79272343Sngie	int (*testf_dso_null)(void);
80272343Sngie
81272343Sngie	ATF_REQUIRE(pthread_mutex_init(&mtx, NULL) == 0);
82272343Sngie	ATF_REQUIRE(pthread_mutex_lock(&mtx) == 0);
83272343Sngie
84272343Sngie	handle = dlopen(DSO, RTLD_NOW | RTLD_LOCAL);
85272343Sngie	ATF_REQUIRE_MSG(handle != NULL, "dlopen fails: %s", dlerror());
86272343Sngie
87272343Sngie	testf_dso_null = dlsym(handle, "testf_dso_null");
88272343Sngie	ATF_REQUIRE_MSG(testf_dso_null != NULL, "dlsym fails: %s", dlerror());
89272343Sngie
90272343Sngie	ATF_REQUIRE(testf_dso_null() == 0xcafe);
91272343Sngie
92272343Sngie	ATF_REQUIRE(pthread_mutex_unlock(&mtx) == 0);
93272343Sngie
94272343Sngie	ATF_REQUIRE(dlclose(handle) == 0);
95272343Sngie
96272343Sngie	pthread_mutex_destroy(&mtx);
97272343Sngie}
98272343Sngie
99272343SngieATF_TC(dlopen_mutex_libc);
100272343Sngie
101272343SngieATF_TC_HEAD(dlopen_mutex_libc, tc)
102272343Sngie{
103272343Sngie	atf_tc_set_md_var(tc, "descr",
104272343Sngie	    "Test if dlopen can load -lpthread DSO and use libc locked mutex");
105272343Sngie}
106272343Sngie
107272343SngieATF_TC_BODY(dlopen_mutex_libc, tc)
108272343Sngie{
109272343Sngie	pthread_mutex_t mtx;
110272343Sngie	void *handle;
111272343Sngie	int (*testf_dso_mutex_unlock)(pthread_mutex_t *);
112272343Sngie
113272343Sngie	ATF_REQUIRE(pthread_mutex_init(&mtx, NULL) == 0);
114272343Sngie	ATF_REQUIRE(pthread_mutex_lock(&mtx) == 0);
115272343Sngie
116272343Sngie	handle = dlopen(DSO, RTLD_NOW | RTLD_LOCAL);
117272343Sngie	ATF_REQUIRE_MSG(handle != NULL, "dlopen fails: %s", dlerror());
118272343Sngie
119272343Sngie	testf_dso_mutex_unlock = dlsym(handle, "testf_dso_mutex_unlock");
120272343Sngie	ATF_REQUIRE_MSG(testf_dso_mutex_unlock != NULL,
121272343Sngie			"dlsym fails: %s", dlerror());
122272343Sngie
123272343Sngie	ATF_REQUIRE(testf_dso_mutex_unlock(&mtx) == 0xcafe);
124272343Sngie
125272343Sngie	dlclose(handle);
126272343Sngie
127272343Sngie	pthread_mutex_destroy(&mtx);
128272343Sngie}
129272343Sngie
130272343SngieATF_TC(dlopen_mutex_libpthread);
131272343Sngie
132272343SngieATF_TC_HEAD(dlopen_mutex_libpthread, tc)
133272343Sngie{
134272343Sngie	atf_tc_set_md_var(tc, "descr",
135272343Sngie	    "Test if dlopen can load -lpthread DSO and use "
136272343Sngie	    "libpthread locked mutex");
137272343Sngie}
138272343Sngie
139272343SngieATF_TC_BODY(dlopen_mutex_libpthread, tc)
140272343Sngie{
141272343Sngie	pthread_mutex_t mtx;
142272343Sngie	void *handle;
143272343Sngie	int (*testf_dso_mutex_lock)(pthread_mutex_t *);
144272343Sngie
145272343Sngie	ATF_REQUIRE(pthread_mutex_init(&mtx, NULL) == 0);
146272343Sngie
147272343Sngie	handle = dlopen(DSO, RTLD_NOW | RTLD_LOCAL);
148272343Sngie	ATF_REQUIRE_MSG(handle != NULL, "dlopen fails: %s", dlerror());
149272343Sngie
150272343Sngie	testf_dso_mutex_lock = dlsym(handle, "testf_dso_mutex_lock");
151272343Sngie	ATF_REQUIRE_MSG(testf_dso_mutex_lock != NULL,
152272343Sngie			"dlsym fails: %s", dlerror());
153272343Sngie
154272343Sngie	ATF_REQUIRE(testf_dso_mutex_lock(&mtx) == 0xcafe);
155272343Sngie
156272343Sngie	ATF_REQUIRE(pthread_mutex_unlock(&mtx) == 0);
157272343Sngie
158272343Sngie	dlclose(handle);
159272343Sngie
160272343Sngie	pthread_mutex_destroy(&mtx);
161272343Sngie}
162272343Sngie
163272343SngieATF_TP_ADD_TCS(tp)
164272343Sngie{
165272343Sngie	ATF_TP_ADD_TC(tp, dlopen);
166272343Sngie	ATF_TP_ADD_TC(tp, dlopen_mutex);
167272343Sngie	ATF_TP_ADD_TC(tp, dlopen_mutex_libc);
168272343Sngie	ATF_TP_ADD_TC(tp, dlopen_mutex_libpthread);
169272343Sngie
170272343Sngie	return atf_no_error();
171272343Sngie}
172