t_name.c revision 272345
1219019Sgabor/* $NetBSD: t_name.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */
2219019Sgabor
3219019Sgabor/*
4219019Sgabor * Copyright (c) 2008 The NetBSD Foundation, Inc.
5219019Sgabor * All rights reserved.
6219019Sgabor *
7219019Sgabor * Redistribution and use in source and binary forms, with or without
8219019Sgabor * modification, are permitted provided that the following conditions
9219019Sgabor * are met:
10219019Sgabor * 1. Redistributions of source code must retain the above copyright
11219019Sgabor *    notice, this list of conditions and the following disclaimer.
12219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright
13219019Sgabor *    notice, this list of conditions and the following disclaimer in the
14219019Sgabor *    documentation and/or other materials provided with the distribution.
15219019Sgabor *
16219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17219019Sgabor * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18219019Sgabor * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19219019Sgabor * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20219019Sgabor * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21219019Sgabor * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22219019Sgabor * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23219019Sgabor * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24219019Sgabor * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25219019Sgabor * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26219019Sgabor * POSSIBILITY OF SUCH DAMAGE.
27219019Sgabor */
28219019Sgabor
29219019Sgabor#include <sys/cdefs.h>
30219019Sgabor__COPYRIGHT("@(#) Copyright (c) 2008\
31219019Sgabor The NetBSD Foundation, inc. All rights reserved.");
32219019Sgabor__RCSID("$NetBSD: t_name.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $");
33219019Sgabor
34219019Sgabor#include <errno.h>
35219019Sgabor#include <pthread.h>
36219019Sgabor#include <stdio.h>
37219019Sgabor#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"
49char non_const_name[] = CONST_NAME;
50
51static void *
52threadfunc(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
68ATF_TC(name);
69ATF_TC_HEAD(name, tc)
70{
71	atf_tc_set_md_var(tc, "descr",
72		"Checks pthread_{,attr}_{get,set}name_np() API");
73}
74ATF_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
98ATF_TP_ADD_TCS(tp)
99{
100	ATF_TP_ADD_TC(tp, name);
101
102	return atf_no_error();
103}
104