1272343Sngie/*	$NetBSD: t_copy.c,v 1.2 2013/07/26 16:09:48 njoly Exp $	*/
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2010 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
17272343Sngie * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18272343Sngie * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19272343Sngie * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20272343Sngie * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21272343Sngie * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22272343Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23272343Sngie * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25272343Sngie * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26272343Sngie * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27272343Sngie * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28272343Sngie */
29272343Sngie
30272343Sngie#include <sys/types.h>
31272343Sngie
32272343Sngie#include <atf-c.h>
33272343Sngie#include <errno.h>
34272343Sngie#include <string.h>
35272343Sngie
36272343Sngie#include <rump/rump.h>
37272343Sngie
38272343SngieATF_TC(copystr);
39272343SngieATF_TC_HEAD(copystr, tc)
40272343Sngie{
41272343Sngie
42272343Sngie	atf_tc_set_md_var(tc, "descr", "Tests copystr()");
43272343Sngie}
44272343Sngie
45272343SngieATF_TC(copyinstr);
46272343SngieATF_TC_HEAD(copyinstr, tc)
47272343Sngie{
48272343Sngie
49272343Sngie	atf_tc_set_md_var(tc, "descr", "Tests copyinstr()");
50272343Sngie}
51272343Sngie
52272343SngieATF_TC(copyoutstr);
53272343SngieATF_TC_HEAD(copyoutstr, tc)
54272343Sngie{
55272343Sngie
56272343Sngie	atf_tc_set_md_var(tc, "descr", "Tests copyoutstr()");
57272343Sngie}
58272343Sngie
59272343Sngietypedef int (copystr_fn)(const void *, void *, size_t, size_t *);
60272343Sngietypedef int (copy_fn)(const void *, void *, size_t);
61272343Sngie
62272343Sngieextern copystr_fn rumpns_copystr, rumpns_copyinstr, rumpns_copyoutstr;
63272343Sngieextern copy_fn rumpns_copyin, rumpns_copyout;
64272343Sngie
65272343Sngie#define TESTSTR "jippii, lisaa puuroa"
66272343Sngie
67272343Sngiestatic void
68272343Sngiedotest(copystr_fn *thefun)
69272343Sngie{
70272343Sngie	char buf[sizeof(TESTSTR)+1];
71272343Sngie	size_t len;
72272343Sngie
73272343Sngie	rump_init();
74272343Sngie	rump_schedule();
75272343Sngie
76272343Sngie	/* larger buffer */
77272343Sngie	memset(buf, 0xaa, sizeof(buf));
78272343Sngie	ATF_REQUIRE_EQ(thefun(TESTSTR, buf, sizeof(buf), &len), 0);
79272343Sngie	ATF_REQUIRE_EQ(len, sizeof(TESTSTR));
80272343Sngie	ATF_REQUIRE_STREQ(TESTSTR, buf);
81272343Sngie
82272343Sngie	/* just large enough */
83272343Sngie	memset(buf, 0xaa, sizeof(buf));
84272343Sngie	ATF_REQUIRE_EQ(thefun(TESTSTR, buf, sizeof(buf)-1, &len), 0);
85272343Sngie	ATF_REQUIRE_EQ(len, sizeof(TESTSTR));
86272343Sngie	ATF_REQUIRE_STREQ(TESTSTR, buf);
87272343Sngie
88272343Sngie	/* one too small */
89272343Sngie	memset(buf, 0xaa, sizeof(buf));
90272343Sngie	ATF_REQUIRE_EQ(thefun(TESTSTR, buf, sizeof(buf)-2, NULL), ENAMETOOLONG);
91272343Sngie
92272343Sngie	rump_unschedule();
93272343Sngie}
94272343Sngie
95272343SngieATF_TC_BODY(copystr, tc)
96272343Sngie{
97272343Sngie
98272343Sngie	dotest(rumpns_copystr);
99272343Sngie}
100272343Sngie
101272343SngieATF_TC_BODY(copyinstr, tc)
102272343Sngie{
103272343Sngie
104272343Sngie	dotest(rumpns_copyinstr);
105272343Sngie}
106272343Sngie
107272343SngieATF_TC_BODY(copyoutstr, tc)
108272343Sngie{
109272343Sngie
110272343Sngie	dotest(rumpns_copyoutstr);
111272343Sngie}
112272343Sngie
113272343SngieATF_TC(copy_efault);
114272343SngieATF_TC_HEAD(copy_efault, tc)
115272343Sngie{
116272343Sngie
117272343Sngie	atf_tc_set_md_var(tc, "descr", "Tests that copy(9) functions can return EFAULT");
118272343Sngie}
119272343SngieATF_TC_BODY(copy_efault, tc)
120272343Sngie{
121272343Sngie	char buf[1024];
122272343Sngie
123272343Sngie	ATF_REQUIRE_EQ(rumpns_copyin(NULL, buf, sizeof(buf)), EFAULT);
124272343Sngie	ATF_REQUIRE_EQ(rumpns_copyout(buf, NULL, sizeof(buf)), EFAULT);
125272343Sngie
126272343Sngie	ATF_REQUIRE_EQ(rumpns_copyinstr(NULL, buf, sizeof(buf), NULL), EFAULT);
127272343Sngie	ATF_REQUIRE_EQ(rumpns_copyoutstr(buf, NULL, sizeof(buf), NULL), EFAULT);
128272343Sngie}
129272343Sngie
130272343SngieATF_TP_ADD_TCS(tp)
131272343Sngie{
132272343Sngie
133272343Sngie	ATF_TP_ADD_TC(tp, copystr);
134272343Sngie	ATF_TP_ADD_TC(tp, copyinstr);
135272343Sngie	ATF_TP_ADD_TC(tp, copyoutstr);
136272343Sngie	ATF_TP_ADD_TC(tp, copy_efault);
137272343Sngie
138272343Sngie	return atf_no_error();
139272343Sngie}
140