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