xref: /netbsd-src/tests/rump/rumpkern/t_copy.c (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /*	$NetBSD: t_copy.c,v 1.1 2010/11/09 15:25:20 pooka 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 (copy_fn)(const void *, void *, size_t, size_t *);
60 
61 extern copy_fn rumpns_copystr, rumpns_copyinstr, rumpns_copyoutstr;
62 
63 #define TESTSTR "jippii, lisaa puuroa"
64 
65 static void
66 dotest(copy_fn *thefun)
67 {
68 	char buf[sizeof(TESTSTR)+1];
69 	size_t len;
70 
71 	rump_init();
72 	rump_schedule();
73 
74 	/* larger buffer */
75 	memset(buf, 0xaa, sizeof(buf));
76 	ATF_REQUIRE_EQ(thefun(TESTSTR, buf, sizeof(buf), &len), 0);
77 	ATF_REQUIRE_EQ(len, sizeof(TESTSTR));
78 	ATF_REQUIRE_STREQ(TESTSTR, buf);
79 
80 	/* just large enough */
81 	memset(buf, 0xaa, sizeof(buf));
82 	ATF_REQUIRE_EQ(thefun(TESTSTR, buf, sizeof(buf)-1, &len), 0);
83 	ATF_REQUIRE_EQ(len, sizeof(TESTSTR));
84 	ATF_REQUIRE_STREQ(TESTSTR, buf);
85 
86 	/* one too small */
87 	memset(buf, 0xaa, sizeof(buf));
88 	ATF_REQUIRE_EQ(thefun(TESTSTR, buf, sizeof(buf)-2, NULL), ENAMETOOLONG);
89 
90 	rump_unschedule();
91 }
92 
93 ATF_TC_BODY(copystr, tc)
94 {
95 
96 	dotest(rumpns_copystr);
97 }
98 
99 ATF_TC_BODY(copyinstr, tc)
100 {
101 
102 	dotest(rumpns_copyinstr);
103 }
104 
105 ATF_TC_BODY(copyoutstr, tc)
106 {
107 
108 	dotest(rumpns_copyoutstr);
109 }
110 
111 ATF_TP_ADD_TCS(tp)
112 {
113 
114 	ATF_TP_ADD_TC(tp, copystr);
115 	ATF_TP_ADD_TC(tp, copyinstr);
116 	ATF_TP_ADD_TC(tp, copyoutstr);
117 
118 	return atf_no_error();
119 }
120