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