xref: /freebsd/contrib/netbsd-tests/rump/rumpkern/t_copy.c (revision 9268022b74279434ed6300244e3f977e56a8ceb5)
1*57718be8SEnji Cooper /*	$NetBSD: t_copy.c,v 1.2 2013/07/26 16:09:48 njoly Exp $	*/
2*57718be8SEnji Cooper 
3*57718be8SEnji Cooper /*-
4*57718be8SEnji Cooper  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5*57718be8SEnji Cooper  * All rights reserved.
6*57718be8SEnji Cooper  *
7*57718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
8*57718be8SEnji Cooper  * modification, are permitted provided that the following conditions
9*57718be8SEnji Cooper  * are met:
10*57718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
11*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
12*57718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
13*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
14*57718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
15*57718be8SEnji Cooper  *
16*57718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*57718be8SEnji Cooper  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*57718be8SEnji Cooper  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*57718be8SEnji Cooper  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*57718be8SEnji Cooper  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*57718be8SEnji Cooper  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*57718be8SEnji Cooper  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*57718be8SEnji Cooper  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*57718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*57718be8SEnji Cooper  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*57718be8SEnji Cooper  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*57718be8SEnji Cooper  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*57718be8SEnji Cooper  */
29*57718be8SEnji Cooper 
30*57718be8SEnji Cooper #include <sys/types.h>
31*57718be8SEnji Cooper 
32*57718be8SEnji Cooper #include <atf-c.h>
33*57718be8SEnji Cooper #include <errno.h>
34*57718be8SEnji Cooper #include <string.h>
35*57718be8SEnji Cooper 
36*57718be8SEnji Cooper #include <rump/rump.h>
37*57718be8SEnji Cooper 
38*57718be8SEnji Cooper ATF_TC(copystr);
ATF_TC_HEAD(copystr,tc)39*57718be8SEnji Cooper ATF_TC_HEAD(copystr, tc)
40*57718be8SEnji Cooper {
41*57718be8SEnji Cooper 
42*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Tests copystr()");
43*57718be8SEnji Cooper }
44*57718be8SEnji Cooper 
45*57718be8SEnji Cooper ATF_TC(copyinstr);
ATF_TC_HEAD(copyinstr,tc)46*57718be8SEnji Cooper ATF_TC_HEAD(copyinstr, tc)
47*57718be8SEnji Cooper {
48*57718be8SEnji Cooper 
49*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Tests copyinstr()");
50*57718be8SEnji Cooper }
51*57718be8SEnji Cooper 
52*57718be8SEnji Cooper ATF_TC(copyoutstr);
ATF_TC_HEAD(copyoutstr,tc)53*57718be8SEnji Cooper ATF_TC_HEAD(copyoutstr, tc)
54*57718be8SEnji Cooper {
55*57718be8SEnji Cooper 
56*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Tests copyoutstr()");
57*57718be8SEnji Cooper }
58*57718be8SEnji Cooper 
59*57718be8SEnji Cooper typedef int (copystr_fn)(const void *, void *, size_t, size_t *);
60*57718be8SEnji Cooper typedef int (copy_fn)(const void *, void *, size_t);
61*57718be8SEnji Cooper 
62*57718be8SEnji Cooper extern copystr_fn rumpns_copystr, rumpns_copyinstr, rumpns_copyoutstr;
63*57718be8SEnji Cooper extern copy_fn rumpns_copyin, rumpns_copyout;
64*57718be8SEnji Cooper 
65*57718be8SEnji Cooper #define TESTSTR "jippii, lisaa puuroa"
66*57718be8SEnji Cooper 
67*57718be8SEnji Cooper static void
dotest(copystr_fn * thefun)68*57718be8SEnji Cooper dotest(copystr_fn *thefun)
69*57718be8SEnji Cooper {
70*57718be8SEnji Cooper 	char buf[sizeof(TESTSTR)+1];
71*57718be8SEnji Cooper 	size_t len;
72*57718be8SEnji Cooper 
73*57718be8SEnji Cooper 	rump_init();
74*57718be8SEnji Cooper 	rump_schedule();
75*57718be8SEnji Cooper 
76*57718be8SEnji Cooper 	/* larger buffer */
77*57718be8SEnji Cooper 	memset(buf, 0xaa, sizeof(buf));
78*57718be8SEnji Cooper 	ATF_REQUIRE_EQ(thefun(TESTSTR, buf, sizeof(buf), &len), 0);
79*57718be8SEnji Cooper 	ATF_REQUIRE_EQ(len, sizeof(TESTSTR));
80*57718be8SEnji Cooper 	ATF_REQUIRE_STREQ(TESTSTR, buf);
81*57718be8SEnji Cooper 
82*57718be8SEnji Cooper 	/* just large enough */
83*57718be8SEnji Cooper 	memset(buf, 0xaa, sizeof(buf));
84*57718be8SEnji Cooper 	ATF_REQUIRE_EQ(thefun(TESTSTR, buf, sizeof(buf)-1, &len), 0);
85*57718be8SEnji Cooper 	ATF_REQUIRE_EQ(len, sizeof(TESTSTR));
86*57718be8SEnji Cooper 	ATF_REQUIRE_STREQ(TESTSTR, buf);
87*57718be8SEnji Cooper 
88*57718be8SEnji Cooper 	/* one too small */
89*57718be8SEnji Cooper 	memset(buf, 0xaa, sizeof(buf));
90*57718be8SEnji Cooper 	ATF_REQUIRE_EQ(thefun(TESTSTR, buf, sizeof(buf)-2, NULL), ENAMETOOLONG);
91*57718be8SEnji Cooper 
92*57718be8SEnji Cooper 	rump_unschedule();
93*57718be8SEnji Cooper }
94*57718be8SEnji Cooper 
ATF_TC_BODY(copystr,tc)95*57718be8SEnji Cooper ATF_TC_BODY(copystr, tc)
96*57718be8SEnji Cooper {
97*57718be8SEnji Cooper 
98*57718be8SEnji Cooper 	dotest(rumpns_copystr);
99*57718be8SEnji Cooper }
100*57718be8SEnji Cooper 
ATF_TC_BODY(copyinstr,tc)101*57718be8SEnji Cooper ATF_TC_BODY(copyinstr, tc)
102*57718be8SEnji Cooper {
103*57718be8SEnji Cooper 
104*57718be8SEnji Cooper 	dotest(rumpns_copyinstr);
105*57718be8SEnji Cooper }
106*57718be8SEnji Cooper 
ATF_TC_BODY(copyoutstr,tc)107*57718be8SEnji Cooper ATF_TC_BODY(copyoutstr, tc)
108*57718be8SEnji Cooper {
109*57718be8SEnji Cooper 
110*57718be8SEnji Cooper 	dotest(rumpns_copyoutstr);
111*57718be8SEnji Cooper }
112*57718be8SEnji Cooper 
113*57718be8SEnji Cooper ATF_TC(copy_efault);
ATF_TC_HEAD(copy_efault,tc)114*57718be8SEnji Cooper ATF_TC_HEAD(copy_efault, tc)
115*57718be8SEnji Cooper {
116*57718be8SEnji Cooper 
117*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Tests that copy(9) functions can return EFAULT");
118*57718be8SEnji Cooper }
ATF_TC_BODY(copy_efault,tc)119*57718be8SEnji Cooper ATF_TC_BODY(copy_efault, tc)
120*57718be8SEnji Cooper {
121*57718be8SEnji Cooper 	char buf[1024];
122*57718be8SEnji Cooper 
123*57718be8SEnji Cooper 	ATF_REQUIRE_EQ(rumpns_copyin(NULL, buf, sizeof(buf)), EFAULT);
124*57718be8SEnji Cooper 	ATF_REQUIRE_EQ(rumpns_copyout(buf, NULL, sizeof(buf)), EFAULT);
125*57718be8SEnji Cooper 
126*57718be8SEnji Cooper 	ATF_REQUIRE_EQ(rumpns_copyinstr(NULL, buf, sizeof(buf), NULL), EFAULT);
127*57718be8SEnji Cooper 	ATF_REQUIRE_EQ(rumpns_copyoutstr(buf, NULL, sizeof(buf), NULL), EFAULT);
128*57718be8SEnji Cooper }
129*57718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)130*57718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
131*57718be8SEnji Cooper {
132*57718be8SEnji Cooper 
133*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, copystr);
134*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, copyinstr);
135*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, copyoutstr);
136*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, copy_efault);
137*57718be8SEnji Cooper 
138*57718be8SEnji Cooper 	return atf_no_error();
139*57718be8SEnji Cooper }
140