xref: /freebsd/contrib/netbsd-tests/lib/libpthread/t_name.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /* $NetBSD: t_name.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */
2 
3 /*
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __COPYRIGHT("@(#) Copyright (c) 2008\
31  The NetBSD Foundation, inc. All rights reserved.");
32 __RCSID("$NetBSD: t_name.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $");
33 
34 #include <errno.h>
35 #include <pthread.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include <atf-c.h>
42 
43 #include "h_common.h"
44 
45 #define NAME_TOO_LONG   "12345678901234567890123456789012"  /* 32 chars */
46 #define NAME_JUST_RIGHT "1234567890123456789012345678901"   /* 31 chars */
47 
48 #define CONST_NAME "xyzzy"
49 char non_const_name[] = CONST_NAME;
50 
51 static void *
52 threadfunc(void *arg)
53 {
54 	pthread_t self = pthread_self();
55 	char retname[32];
56 
57 	PTHREAD_REQUIRE(pthread_getname_np(self, retname, sizeof(retname)));
58 	ATF_REQUIRE_STREQ(retname, NAME_JUST_RIGHT);
59 
60 	PTHREAD_REQUIRE(pthread_setname_np(self, non_const_name, NULL));
61 	(void) memset(non_const_name, 0, sizeof(non_const_name));
62 	PTHREAD_REQUIRE(pthread_getname_np(self, retname, sizeof(retname)));
63 	ATF_REQUIRE_STREQ(retname, CONST_NAME);
64 
65 	return NULL;
66 }
67 
68 ATF_TC(name);
69 ATF_TC_HEAD(name, tc)
70 {
71 	atf_tc_set_md_var(tc, "descr",
72 		"Checks pthread_{,attr}_{get,set}name_np() API");
73 }
74 ATF_TC_BODY(name, tc)
75 {
76 	pthread_t thr, self = pthread_self();
77 	pthread_attr_t attr;
78 	char retname[32];
79 
80 	PTHREAD_REQUIRE(pthread_attr_init(&attr));
81 	PTHREAD_REQUIRE(pthread_attr_getname_np(&attr, retname,
82 		sizeof(retname), NULL));
83 	ATF_REQUIRE_EQ(retname[0], '\0');
84 	ATF_REQUIRE_EQ(pthread_attr_setname_np(&attr, NAME_TOO_LONG, NULL), EINVAL);
85 	PTHREAD_REQUIRE(pthread_attr_setname_np(&attr, "%s",
86 	    __UNCONST(NAME_JUST_RIGHT)));
87 
88 	(void) strcpy(retname, "foo");
89 	PTHREAD_REQUIRE(pthread_getname_np(self, retname, sizeof(retname)));
90 	ATF_REQUIRE_EQ(retname[0], '\0');
91 
92 	PTHREAD_REQUIRE(pthread_create(&thr, &attr, threadfunc, NULL));
93 	PTHREAD_REQUIRE(pthread_join(thr, NULL));
94 
95 	ATF_REQUIRE_EQ(pthread_getname_np(thr, retname, sizeof(retname)), ESRCH);
96 }
97 
98 ATF_TP_ADD_TCS(tp)
99 {
100 	ATF_TP_ADD_TC(tp, name);
101 
102 	return atf_no_error();
103 }
104