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