1*84d9c625SLionel Sambuc /* $NetBSD: t_copy.c,v 1.2 2013/07/26 16:09:48 njoly Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc * Copyright (c) 2010 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc * All rights reserved.
611be35a1SLionel Sambuc *
711be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc * are met:
1011be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc *
1611be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
1711be35a1SLionel Sambuc * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1811be35a1SLionel Sambuc * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1911be35a1SLionel Sambuc * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2011be35a1SLionel Sambuc * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
2111be35a1SLionel Sambuc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2211be35a1SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2311be35a1SLionel Sambuc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2411be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2511be35a1SLionel Sambuc * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2611be35a1SLionel Sambuc * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2711be35a1SLionel Sambuc * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811be35a1SLionel Sambuc */
2911be35a1SLionel Sambuc
3011be35a1SLionel Sambuc #include <sys/types.h>
3111be35a1SLionel Sambuc
3211be35a1SLionel Sambuc #include <atf-c.h>
3311be35a1SLionel Sambuc #include <errno.h>
3411be35a1SLionel Sambuc #include <string.h>
3511be35a1SLionel Sambuc
3611be35a1SLionel Sambuc #include <rump/rump.h>
3711be35a1SLionel Sambuc
3811be35a1SLionel Sambuc ATF_TC(copystr);
ATF_TC_HEAD(copystr,tc)3911be35a1SLionel Sambuc ATF_TC_HEAD(copystr, tc)
4011be35a1SLionel Sambuc {
4111be35a1SLionel Sambuc
4211be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Tests copystr()");
4311be35a1SLionel Sambuc }
4411be35a1SLionel Sambuc
4511be35a1SLionel Sambuc ATF_TC(copyinstr);
ATF_TC_HEAD(copyinstr,tc)4611be35a1SLionel Sambuc ATF_TC_HEAD(copyinstr, tc)
4711be35a1SLionel Sambuc {
4811be35a1SLionel Sambuc
4911be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Tests copyinstr()");
5011be35a1SLionel Sambuc }
5111be35a1SLionel Sambuc
5211be35a1SLionel Sambuc ATF_TC(copyoutstr);
ATF_TC_HEAD(copyoutstr,tc)5311be35a1SLionel Sambuc ATF_TC_HEAD(copyoutstr, tc)
5411be35a1SLionel Sambuc {
5511be35a1SLionel Sambuc
5611be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Tests copyoutstr()");
5711be35a1SLionel Sambuc }
5811be35a1SLionel Sambuc
59*84d9c625SLionel Sambuc typedef int (copystr_fn)(const void *, void *, size_t, size_t *);
60*84d9c625SLionel Sambuc typedef int (copy_fn)(const void *, void *, size_t);
6111be35a1SLionel Sambuc
62*84d9c625SLionel Sambuc extern copystr_fn rumpns_copystr, rumpns_copyinstr, rumpns_copyoutstr;
63*84d9c625SLionel Sambuc extern copy_fn rumpns_copyin, rumpns_copyout;
6411be35a1SLionel Sambuc
6511be35a1SLionel Sambuc #define TESTSTR "jippii, lisaa puuroa"
6611be35a1SLionel Sambuc
6711be35a1SLionel Sambuc static void
dotest(copystr_fn * thefun)68*84d9c625SLionel Sambuc dotest(copystr_fn *thefun)
6911be35a1SLionel Sambuc {
7011be35a1SLionel Sambuc char buf[sizeof(TESTSTR)+1];
7111be35a1SLionel Sambuc size_t len;
7211be35a1SLionel Sambuc
7311be35a1SLionel Sambuc rump_init();
7411be35a1SLionel Sambuc rump_schedule();
7511be35a1SLionel Sambuc
7611be35a1SLionel Sambuc /* larger buffer */
7711be35a1SLionel Sambuc memset(buf, 0xaa, sizeof(buf));
7811be35a1SLionel Sambuc ATF_REQUIRE_EQ(thefun(TESTSTR, buf, sizeof(buf), &len), 0);
7911be35a1SLionel Sambuc ATF_REQUIRE_EQ(len, sizeof(TESTSTR));
8011be35a1SLionel Sambuc ATF_REQUIRE_STREQ(TESTSTR, buf);
8111be35a1SLionel Sambuc
8211be35a1SLionel Sambuc /* just large enough */
8311be35a1SLionel Sambuc memset(buf, 0xaa, sizeof(buf));
8411be35a1SLionel Sambuc ATF_REQUIRE_EQ(thefun(TESTSTR, buf, sizeof(buf)-1, &len), 0);
8511be35a1SLionel Sambuc ATF_REQUIRE_EQ(len, sizeof(TESTSTR));
8611be35a1SLionel Sambuc ATF_REQUIRE_STREQ(TESTSTR, buf);
8711be35a1SLionel Sambuc
8811be35a1SLionel Sambuc /* one too small */
8911be35a1SLionel Sambuc memset(buf, 0xaa, sizeof(buf));
9011be35a1SLionel Sambuc ATF_REQUIRE_EQ(thefun(TESTSTR, buf, sizeof(buf)-2, NULL), ENAMETOOLONG);
9111be35a1SLionel Sambuc
9211be35a1SLionel Sambuc rump_unschedule();
9311be35a1SLionel Sambuc }
9411be35a1SLionel Sambuc
ATF_TC_BODY(copystr,tc)9511be35a1SLionel Sambuc ATF_TC_BODY(copystr, tc)
9611be35a1SLionel Sambuc {
9711be35a1SLionel Sambuc
9811be35a1SLionel Sambuc dotest(rumpns_copystr);
9911be35a1SLionel Sambuc }
10011be35a1SLionel Sambuc
ATF_TC_BODY(copyinstr,tc)10111be35a1SLionel Sambuc ATF_TC_BODY(copyinstr, tc)
10211be35a1SLionel Sambuc {
10311be35a1SLionel Sambuc
10411be35a1SLionel Sambuc dotest(rumpns_copyinstr);
10511be35a1SLionel Sambuc }
10611be35a1SLionel Sambuc
ATF_TC_BODY(copyoutstr,tc)10711be35a1SLionel Sambuc ATF_TC_BODY(copyoutstr, tc)
10811be35a1SLionel Sambuc {
10911be35a1SLionel Sambuc
11011be35a1SLionel Sambuc dotest(rumpns_copyoutstr);
11111be35a1SLionel Sambuc }
11211be35a1SLionel Sambuc
113*84d9c625SLionel Sambuc ATF_TC(copy_efault);
ATF_TC_HEAD(copy_efault,tc)114*84d9c625SLionel Sambuc ATF_TC_HEAD(copy_efault, tc)
115*84d9c625SLionel Sambuc {
116*84d9c625SLionel Sambuc
117*84d9c625SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Tests that copy(9) functions can return EFAULT");
118*84d9c625SLionel Sambuc }
ATF_TC_BODY(copy_efault,tc)119*84d9c625SLionel Sambuc ATF_TC_BODY(copy_efault, tc)
120*84d9c625SLionel Sambuc {
121*84d9c625SLionel Sambuc char buf[1024];
122*84d9c625SLionel Sambuc
123*84d9c625SLionel Sambuc ATF_REQUIRE_EQ(rumpns_copyin(NULL, buf, sizeof(buf)), EFAULT);
124*84d9c625SLionel Sambuc ATF_REQUIRE_EQ(rumpns_copyout(buf, NULL, sizeof(buf)), EFAULT);
125*84d9c625SLionel Sambuc
126*84d9c625SLionel Sambuc ATF_REQUIRE_EQ(rumpns_copyinstr(NULL, buf, sizeof(buf), NULL), EFAULT);
127*84d9c625SLionel Sambuc ATF_REQUIRE_EQ(rumpns_copyoutstr(buf, NULL, sizeof(buf), NULL), EFAULT);
128*84d9c625SLionel Sambuc }
129*84d9c625SLionel Sambuc
ATF_TP_ADD_TCS(tp)13011be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
13111be35a1SLionel Sambuc {
13211be35a1SLionel Sambuc
13311be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, copystr);
13411be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, copyinstr);
13511be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, copyoutstr);
136*84d9c625SLionel Sambuc ATF_TP_ADD_TC(tp, copy_efault);
13711be35a1SLionel Sambuc
13811be35a1SLionel Sambuc return atf_no_error();
13911be35a1SLionel Sambuc }
140