1*10f81a9bSEnji Cooper /*- 2*10f81a9bSEnji Cooper * Copyright (c) 2017 Ngie Cooper <ngie@freebsd.org> 3*10f81a9bSEnji Cooper * All rights reserved. 4*10f81a9bSEnji Cooper * 5*10f81a9bSEnji Cooper * Redistribution and use in source and binary forms, with or without 6*10f81a9bSEnji Cooper * modification, are permitted provided that the following conditions 7*10f81a9bSEnji Cooper * are met: 8*10f81a9bSEnji Cooper * 1. Redistributions of source code must retain the above copyright 9*10f81a9bSEnji Cooper * notice, this list of conditions and the following disclaimer. 10*10f81a9bSEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright 11*10f81a9bSEnji Cooper * notice, this list of conditions and the following disclaimer in the 12*10f81a9bSEnji Cooper * documentation and/or other materials provided with the distribution. 13*10f81a9bSEnji Cooper * 14*10f81a9bSEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15*10f81a9bSEnji Cooper * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16*10f81a9bSEnji Cooper * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17*10f81a9bSEnji Cooper * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18*10f81a9bSEnji Cooper * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19*10f81a9bSEnji Cooper * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20*10f81a9bSEnji Cooper * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21*10f81a9bSEnji Cooper * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22*10f81a9bSEnji Cooper * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23*10f81a9bSEnji Cooper * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24*10f81a9bSEnji Cooper * SUCH DAMAGE. 25*10f81a9bSEnji Cooper */ 26*10f81a9bSEnji Cooper 27*10f81a9bSEnji Cooper #include <sys/cdefs.h> 28*10f81a9bSEnji Cooper __FBSDID("$FreeBSD$"); 29*10f81a9bSEnji Cooper 30*10f81a9bSEnji Cooper #include <sys/param.h> 31*10f81a9bSEnji Cooper #include <errno.h> 32*10f81a9bSEnji Cooper #include <fcntl.h> 33*10f81a9bSEnji Cooper #include <kvm.h> 34*10f81a9bSEnji Cooper #include <limits.h> 35*10f81a9bSEnji Cooper #include <paths.h> 36*10f81a9bSEnji Cooper #include <signal.h> 37*10f81a9bSEnji Cooper #include <stdio.h> 38*10f81a9bSEnji Cooper #include <stdlib.h> 39*10f81a9bSEnji Cooper #include <string.h> 40*10f81a9bSEnji Cooper 41*10f81a9bSEnji Cooper #include <atf-c.h> 42*10f81a9bSEnji Cooper 43*10f81a9bSEnji Cooper #include "kvm_test_common.h" 44*10f81a9bSEnji Cooper 45*10f81a9bSEnji Cooper ATF_TC_WITHOUT_HEAD(kvm_open2_negative_test_nonexistent_corefile); 46*10f81a9bSEnji Cooper ATF_TC_BODY(kvm_open2_negative_test_nonexistent_corefile, tc) 47*10f81a9bSEnji Cooper { 48*10f81a9bSEnji Cooper 49*10f81a9bSEnji Cooper errbuf_clear(); 50*10f81a9bSEnji Cooper ATF_CHECK(kvm_open2(NULL, "/nonexistent", O_RDONLY, NULL, NULL) == NULL); 51*10f81a9bSEnji Cooper ATF_CHECK(!errbuf_has_error(errbuf)); 52*10f81a9bSEnji Cooper errbuf_clear(); 53*10f81a9bSEnji Cooper ATF_CHECK(kvm_open2(NULL, "/nonexistent", O_RDONLY, 54*10f81a9bSEnji Cooper errbuf, NULL) == NULL); 55*10f81a9bSEnji Cooper ATF_CHECK(errbuf_has_error(errbuf)); 56*10f81a9bSEnji Cooper } 57*10f81a9bSEnji Cooper 58*10f81a9bSEnji Cooper ATF_TC_WITHOUT_HEAD(kvm_open2_negative_test_nonexistent_execfile); 59*10f81a9bSEnji Cooper ATF_TC_BODY(kvm_open2_negative_test_nonexistent_execfile, tc) 60*10f81a9bSEnji Cooper { 61*10f81a9bSEnji Cooper 62*10f81a9bSEnji Cooper errbuf_clear(); 63*10f81a9bSEnji Cooper ATF_CHECK(kvm_open2("/nonexistent", _PATH_DEVZERO, O_RDONLY, 64*10f81a9bSEnji Cooper NULL, NULL) == NULL); 65*10f81a9bSEnji Cooper ATF_CHECK(strlen(errbuf) == 0); 66*10f81a9bSEnji Cooper errbuf_clear(); 67*10f81a9bSEnji Cooper ATF_CHECK(kvm_open2("/nonexistent", _PATH_DEVZERO, O_RDONLY, 68*10f81a9bSEnji Cooper errbuf, NULL) == NULL); 69*10f81a9bSEnji Cooper ATF_CHECK(errbuf_has_error(errbuf)); 70*10f81a9bSEnji Cooper } 71*10f81a9bSEnji Cooper 72*10f81a9bSEnji Cooper ATF_TC(kvm_open2_negative_test_invalid_corefile); 73*10f81a9bSEnji Cooper ATF_TC_HEAD(kvm_open2_negative_test_invalid_corefile, tc) 74*10f81a9bSEnji Cooper { 75*10f81a9bSEnji Cooper 76*10f81a9bSEnji Cooper atf_tc_set_md_var(tc, "require.user", "root"); 77*10f81a9bSEnji Cooper } 78*10f81a9bSEnji Cooper 79*10f81a9bSEnji Cooper ATF_TC_BODY(kvm_open2_negative_test_invalid_corefile, tc) 80*10f81a9bSEnji Cooper { 81*10f81a9bSEnji Cooper kvm_t *kd; 82*10f81a9bSEnji Cooper 83*10f81a9bSEnji Cooper errbuf_clear(); 84*10f81a9bSEnji Cooper atf_utils_create_file("some-file", "this is a text file"); 85*10f81a9bSEnji Cooper kd = kvm_open2(NULL, "some-file", O_RDONLY, errbuf, NULL); 86*10f81a9bSEnji Cooper ATF_CHECK(errbuf_has_error(errbuf)); 87*10f81a9bSEnji Cooper ATF_REQUIRE_MSG(kd == NULL, "kvm_open2 succeeded"); 88*10f81a9bSEnji Cooper } 89*10f81a9bSEnji Cooper 90*10f81a9bSEnji Cooper ATF_TC(kvm_open2_negative_test_invalid_execfile); 91*10f81a9bSEnji Cooper ATF_TC_HEAD(kvm_open2_negative_test_invalid_execfile, tc) 92*10f81a9bSEnji Cooper { 93*10f81a9bSEnji Cooper 94*10f81a9bSEnji Cooper atf_tc_set_md_var(tc, "require.user", "root"); 95*10f81a9bSEnji Cooper } 96*10f81a9bSEnji Cooper 97*10f81a9bSEnji Cooper ATF_TC_BODY(kvm_open2_negative_test_invalid_execfile, tc) 98*10f81a9bSEnji Cooper { 99*10f81a9bSEnji Cooper kvm_t *kd; 100*10f81a9bSEnji Cooper 101*10f81a9bSEnji Cooper errbuf_clear(); 102*10f81a9bSEnji Cooper atf_utils_create_file("some-file", "this is a text file"); 103*10f81a9bSEnji Cooper kd = kvm_open2("some-file", "/bin/sh", O_RDONLY, errbuf, NULL); 104*10f81a9bSEnji Cooper ATF_CHECK(errbuf_has_error(errbuf)); 105*10f81a9bSEnji Cooper ATF_REQUIRE_MSG(kd == NULL, "kvm_open2 succeeded unexpectedly"); 106*10f81a9bSEnji Cooper } 107*10f81a9bSEnji Cooper 108*10f81a9bSEnji Cooper ATF_TP_ADD_TCS(tp) 109*10f81a9bSEnji Cooper { 110*10f81a9bSEnji Cooper 111*10f81a9bSEnji Cooper ATF_TP_ADD_TC(tp, kvm_open2_negative_test_invalid_corefile); 112*10f81a9bSEnji Cooper ATF_TP_ADD_TC(tp, kvm_open2_negative_test_invalid_execfile); 113*10f81a9bSEnji Cooper ATF_TP_ADD_TC(tp, kvm_open2_negative_test_nonexistent_corefile); 114*10f81a9bSEnji Cooper ATF_TP_ADD_TC(tp, kvm_open2_negative_test_nonexistent_execfile); 115*10f81a9bSEnji Cooper 116*10f81a9bSEnji Cooper return (atf_no_error()); 117*10f81a9bSEnji Cooper } 118