t_getcontext.c revision 272343
1272343Sngie/* $NetBSD: t_getcontext.c,v 1.3 2011/07/14 04:59:14 jruoho Exp $ */
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2008 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * Redistribution and use in source and binary forms, with or without
8272343Sngie * modification, are permitted provided that the following conditions
9272343Sngie * are met:
10272343Sngie * 1. Redistributions of source code must retain the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer.
12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer in the
14272343Sngie *    documentation and/or other materials provided with the distribution.
15272343Sngie *
16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26272343Sngie * POSSIBILITY OF SUCH DAMAGE.
27272343Sngie */
28272343Sngie
29272343Sngie#include <sys/cdefs.h>
30272343Sngie__COPYRIGHT("@(#) Copyright (c) 2008\
31272343Sngie The NetBSD Foundation, inc. All rights reserved.");
32272343Sngie__RCSID("$NetBSD: t_getcontext.c,v 1.3 2011/07/14 04:59:14 jruoho Exp $");
33272343Sngie
34272343Sngie#include <atf-c.h>
35272343Sngie#include <errno.h>
36272343Sngie#include <stdarg.h>
37272343Sngie#include <stdlib.h>
38272343Sngie#include <ucontext.h>
39272343Sngie
40272343Sngie#define STACKSZ (10*1024)
41272343Sngie#define DEPTH 3
42272343Sngie
43272343Sngiestatic int calls;
44272343Sngie
45272343Sngiestatic void
46272343Sngierun(int n, ...)
47272343Sngie{
48272343Sngie	va_list va;
49272343Sngie	int i, ia;
50272343Sngie
51272343Sngie	ATF_REQUIRE_EQ(n, DEPTH - calls - 1);
52272343Sngie
53272343Sngie	va_start(va, n);
54272343Sngie	for (i = 0; i < 9; i++) {
55272343Sngie		ia = va_arg(va, int);
56272343Sngie		ATF_REQUIRE_EQ(i, ia);
57272343Sngie	}
58272343Sngie	va_end(va);
59272343Sngie
60272343Sngie	calls++;
61272343Sngie}
62272343Sngie
63272343SngieATF_TC(getcontext_err);
64272343SngieATF_TC_HEAD(getcontext_err, tc)
65272343Sngie{
66272343Sngie	atf_tc_set_md_var(tc, "descr", "Test errors from getcontext(2)");
67272343Sngie}
68272343Sngie
69272343SngieATF_TC_BODY(getcontext_err, tc)
70272343Sngie{
71272343Sngie
72272343Sngie	errno = 0;
73272343Sngie	ATF_REQUIRE_ERRNO(EFAULT, getcontext((void *)-1) == -1);
74272343Sngie}
75272343Sngie
76272343SngieATF_TC(setcontext_err);
77272343SngieATF_TC_HEAD(setcontext_err, tc)
78272343Sngie{
79272343Sngie	atf_tc_set_md_var(tc, "descr", "Test errors from setcontext(2)");
80272343Sngie}
81272343Sngie
82272343SngieATF_TC_BODY(setcontext_err, tc)
83272343Sngie{
84272343Sngie
85272343Sngie	errno = 0;
86272343Sngie	ATF_REQUIRE_ERRNO(EFAULT, setcontext((void *)-1) == -1);
87272343Sngie}
88272343Sngie
89272343SngieATF_TC(setcontext_link);
90272343SngieATF_TC_HEAD(setcontext_link, tc)
91272343Sngie{
92272343Sngie
93272343Sngie	atf_tc_set_md_var(tc, "descr",
94272343Sngie	"Checks get/make/setcontext(), context linking via uc_link(), "
95272343Sngie	    "and argument passing to the new context");
96272343Sngie}
97272343Sngie
98272343SngieATF_TC_BODY(setcontext_link, tc)
99272343Sngie{
100272343Sngie	ucontext_t uc[DEPTH];
101272343Sngie	ucontext_t save;
102272343Sngie	volatile int i = 0; /* avoid longjmp clobbering */
103272343Sngie
104272343Sngie	for (i = 0; i < DEPTH; ++i) {
105272343Sngie		ATF_REQUIRE_EQ(getcontext(&uc[i]), 0);
106272343Sngie
107272343Sngie		uc[i].uc_stack.ss_sp = malloc(STACKSZ);
108272343Sngie		uc[i].uc_stack.ss_size = STACKSZ;
109272343Sngie		uc[i].uc_link = (i > 0) ? &uc[i - 1] : &save;
110272343Sngie
111272343Sngie		makecontext(&uc[i], (void *)run, 10, i,
112272343Sngie			0, 1, 2, 3, 4, 5, 6, 7, 8);
113272343Sngie	}
114272343Sngie
115272343Sngie	ATF_REQUIRE_EQ(getcontext(&save), 0);
116272343Sngie
117272343Sngie	if (calls == 0)
118272343Sngie		ATF_REQUIRE_EQ(setcontext(&uc[DEPTH-1]), 0);
119272343Sngie}
120272343Sngie
121272343SngieATF_TP_ADD_TCS(tp)
122272343Sngie{
123272343Sngie
124272343Sngie	ATF_TP_ADD_TC(tp, getcontext_err);
125272343Sngie	ATF_TP_ADD_TC(tp, setcontext_err);
126272343Sngie	ATF_TP_ADD_TC(tp, setcontext_link);
127272343Sngie
128272343Sngie	return atf_no_error();
129272343Sngie}
130