Remove building with NOCRYPTO option
[minix.git] / tests / lib / libc / stdlib / t_getenv_thread.c
blobd935629339e89d0c89079102fbb1a3e441fb5aa3
1 /* $NetBSD: t_getenv_thread.c,v 1.2 2012/03/15 02:02:23 joerg Exp $ */
3 /*-
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matthias Scheler.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_getenv_thread.c,v 1.2 2012/03/15 02:02:23 joerg Exp $");
34 #include <atf-c.h>
35 #include <errno.h>
36 #include <pthread.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <time.h>
42 #define THREADED_NUM_THREADS 8
43 #define THREADED_NUM_VARS 16
44 #define THREADED_VAR_NAME "THREADED%zu"
45 #define THREADED_RUN_TIME 10
47 static void *thread_getenv_r(void *);
48 static void *thread_putenv(void *);
49 static void *thread_setenv(void *);
50 static void *thread_unsetenv(void *);
52 static void *
53 thread_getenv_r(void *arg)
55 time_t endtime;
57 endtime = *(time_t *)arg;
58 do {
59 size_t i;
60 char name[32], value[128];
62 i = lrand48() % THREADED_NUM_VARS;
63 (void)snprintf(name, sizeof(name), THREADED_VAR_NAME, i);
65 if (getenv_r(name, value, sizeof(value)) == -1) {
66 ATF_CHECK(errno == ENOENT);
68 } while (time(NULL) < endtime);
70 return NULL;
74 static void *
75 thread_putenv(void *arg)
77 time_t endtime;
78 size_t i;
79 static char vars[THREADED_NUM_VARS][128];
81 for (i = 0; i < THREADED_NUM_VARS; i++) {
82 (void)snprintf(vars[i], sizeof(vars[i]),
83 THREADED_VAR_NAME "=putenv %ld", i, lrand48());
86 endtime = *(time_t *)arg;
87 do {
88 char name[128];
90 i = lrand48() % THREADED_NUM_VARS;
91 (void)strlcpy(name, vars[i], sizeof(name));
92 *strchr(name, '=') = '\0';
94 ATF_CHECK(unsetenv(name) != -1);
95 ATF_CHECK(putenv(vars[i]) != -1);
96 } while (time(NULL) < endtime);
98 return NULL;
101 static void *
102 thread_setenv(void *arg)
104 time_t endtime;
106 endtime = *(time_t *)arg;
107 do {
108 size_t i;
109 char name[32], value[64];
111 i = lrand48() % THREADED_NUM_VARS;
112 (void)snprintf(name, sizeof(name), THREADED_VAR_NAME, i);
113 (void)snprintf(value, sizeof(value), "setenv %ld", lrand48());
115 ATF_CHECK(setenv(name, value, 1) != -1);
116 } while (time(NULL) < endtime);
118 return NULL;
121 static void *
122 thread_unsetenv(void *arg)
124 time_t endtime;
126 endtime = *(time_t *)arg;
127 do {
128 size_t i;
129 char name[32];
131 i = lrand48() % THREADED_NUM_VARS;
132 (void)snprintf(name, sizeof(name), THREADED_VAR_NAME, i);
134 ATF_CHECK(unsetenv(name) != -1);
135 } while (time(NULL) < endtime);
137 return NULL;
140 ATF_TC(getenv_r_thread);
141 ATF_TC_HEAD(getenv_r_thread, tc)
144 atf_tc_set_md_var(tc, "descr", "Test getenv_r(3) with threads");
145 atf_tc_set_md_var(tc, "timeout", "%d", THREADED_RUN_TIME + 5);
148 ATF_TC_BODY(getenv_r_thread, tc)
150 pthread_t threads[THREADED_NUM_THREADS];
151 time_t endtime;
152 size_t i, j;
154 endtime = time(NULL) + THREADED_RUN_TIME;
156 for (i = j = 0; j < 2; j++) {
158 ATF_CHECK(pthread_create(&threads[i++], NULL, thread_getenv_r,
159 &endtime) == 0);
162 for (j = 0; j < i; j++)
163 ATF_CHECK(pthread_join(threads[j], NULL) == 0);
166 ATF_TC(putenv_thread);
167 ATF_TC_HEAD(putenv_thread, tc)
169 atf_tc_set_md_var(tc, "descr", "Test concurrent access by putenv(3)");
170 atf_tc_set_md_var(tc, "timeout", "%d", THREADED_RUN_TIME + 5);
173 ATF_TC_BODY(putenv_thread, tc)
175 pthread_t threads[THREADED_NUM_THREADS];
176 time_t endtime;
177 size_t i, j;
179 endtime = time(NULL) + THREADED_RUN_TIME;
181 for (i = j = 0; j < 2; j++) {
183 ATF_CHECK(pthread_create(&threads[i++], NULL, thread_putenv,
184 &endtime) == 0);
187 for (j = 0; j < i; j++)
188 ATF_CHECK(pthread_join(threads[j], NULL) == 0);
191 ATF_TC(setenv_thread);
192 ATF_TC_HEAD(setenv_thread, tc)
194 atf_tc_set_md_var(tc, "descr", "Test concurrent access by setenv(3)");
195 atf_tc_set_md_var(tc, "timeout", "%d", THREADED_RUN_TIME + 5);
198 ATF_TC_BODY(setenv_thread, tc)
200 pthread_t threads[THREADED_NUM_THREADS];
201 time_t endtime;
202 size_t i, j;
204 endtime = time(NULL) + THREADED_RUN_TIME;
206 for (i = j = 0; j < 2; j++) {
208 ATF_CHECK(pthread_create(&threads[i++], NULL, thread_setenv,
209 &endtime) == 0);
212 for (j = 0; j < i; j++)
213 ATF_CHECK(pthread_join(threads[j], NULL) == 0);
216 ATF_TC(unsetenv_thread);
217 ATF_TC_HEAD(unsetenv_thread, tc)
219 atf_tc_set_md_var(tc, "descr", "Test unsetenv(3) with threads");
220 atf_tc_set_md_var(tc, "timeout", "%d", THREADED_RUN_TIME + 5);
223 ATF_TC_BODY(unsetenv_thread, tc)
225 pthread_t threads[THREADED_NUM_THREADS];
226 time_t endtime;
227 size_t i, j;
229 endtime = time(NULL) + THREADED_RUN_TIME;
231 for (i = j = 0; j < 2; j++) {
233 ATF_CHECK(pthread_create(&threads[i++], NULL, thread_unsetenv,
234 &endtime) == 0);
237 for (j = 0; j < i; j++)
238 ATF_CHECK(pthread_join(threads[j], NULL) == 0);
241 ATF_TP_ADD_TCS(tp)
244 ATF_TP_ADD_TC(tp, getenv_r_thread);
245 ATF_TP_ADD_TC(tp, putenv_thread);
246 ATF_TP_ADD_TC(tp, setenv_thread);
247 ATF_TP_ADD_TC(tp, unsetenv_thread);
249 return atf_no_error();