xref: /freebsd/lib/libkvm/tests/kvm_open_test.c (revision 10f81a9b3b6549ff94936ab526d9155bd9384c98)
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_open_negative_test_nonexistent_corefile);
46*10f81a9bSEnji Cooper ATF_TC_BODY(kvm_open_negative_test_nonexistent_corefile, tc)
47*10f81a9bSEnji Cooper {
48*10f81a9bSEnji Cooper 
49*10f81a9bSEnji Cooper 	ATF_CHECK(kvm_open(NULL, "/nonexistent", NULL, O_RDONLY, NULL) == NULL);
50*10f81a9bSEnji Cooper 	ATF_CHECK(kvm_open(NULL, "/nonexistent", NULL, O_RDONLY,
51*10f81a9bSEnji Cooper 	    getprogname()) == NULL);
52*10f81a9bSEnji Cooper }
53*10f81a9bSEnji Cooper 
54*10f81a9bSEnji Cooper ATF_TC_WITHOUT_HEAD(kvm_open_negative_test_nonexistent_execfile);
55*10f81a9bSEnji Cooper ATF_TC_BODY(kvm_open_negative_test_nonexistent_execfile, tc)
56*10f81a9bSEnji Cooper {
57*10f81a9bSEnji Cooper 
58*10f81a9bSEnji Cooper 	ATF_CHECK(kvm_open("/nonexistent", _PATH_DEVZERO, NULL, O_RDONLY,
59*10f81a9bSEnji Cooper 	    NULL) == NULL);
60*10f81a9bSEnji Cooper 	ATF_CHECK(kvm_open("/nonexistent", _PATH_DEVZERO, NULL, O_RDONLY,
61*10f81a9bSEnji Cooper 	    getprogname()) == NULL);
62*10f81a9bSEnji Cooper }
63*10f81a9bSEnji Cooper 
64*10f81a9bSEnji Cooper ATF_TC(kvm_open_negative_test_invalid_corefile);
65*10f81a9bSEnji Cooper ATF_TC_HEAD(kvm_open_negative_test_invalid_corefile, tc)
66*10f81a9bSEnji Cooper {
67*10f81a9bSEnji Cooper 
68*10f81a9bSEnji Cooper 	atf_tc_set_md_var(tc, "require.user", "root");
69*10f81a9bSEnji Cooper }
70*10f81a9bSEnji Cooper 
71*10f81a9bSEnji Cooper ATF_TC_BODY(kvm_open_negative_test_invalid_corefile, tc)
72*10f81a9bSEnji Cooper {
73*10f81a9bSEnji Cooper 	kvm_t *kd;
74*10f81a9bSEnji Cooper 
75*10f81a9bSEnji Cooper 	atf_utils_create_file("some-file", "this is a text file");
76*10f81a9bSEnji Cooper 	kd = kvm_open(NULL, "some-file", NULL, O_RDONLY, getprogname());
77*10f81a9bSEnji Cooper 	ATF_REQUIRE_MSG(kd == NULL, "kvm_open didn't return NULL on failure");
78*10f81a9bSEnji Cooper }
79*10f81a9bSEnji Cooper 
80*10f81a9bSEnji Cooper ATF_TC(kvm_open_negative_test_invalid_execfile);
81*10f81a9bSEnji Cooper ATF_TC_HEAD(kvm_open_negative_test_invalid_execfile, tc)
82*10f81a9bSEnji Cooper {
83*10f81a9bSEnji Cooper 
84*10f81a9bSEnji Cooper 	atf_tc_set_md_var(tc, "require.user", "root");
85*10f81a9bSEnji Cooper }
86*10f81a9bSEnji Cooper 
87*10f81a9bSEnji Cooper ATF_TC_BODY(kvm_open_negative_test_invalid_execfile, tc)
88*10f81a9bSEnji Cooper {
89*10f81a9bSEnji Cooper 	kvm_t *kd;
90*10f81a9bSEnji Cooper 
91*10f81a9bSEnji Cooper 	atf_utils_create_file("some-file", "this is a text file");
92*10f81a9bSEnji Cooper 	kd = kvm_open("some-file", "/bin/sh", NULL, O_RDONLY, getprogname());
93*10f81a9bSEnji Cooper 	ATF_REQUIRE_MSG(kd == NULL, "kvm_open succeeded unexpectedly");
94*10f81a9bSEnji Cooper }
95*10f81a9bSEnji Cooper 
96*10f81a9bSEnji Cooper ATF_TP_ADD_TCS(tp)
97*10f81a9bSEnji Cooper {
98*10f81a9bSEnji Cooper 
99*10f81a9bSEnji Cooper 	ATF_TP_ADD_TC(tp, kvm_open_negative_test_invalid_corefile);
100*10f81a9bSEnji Cooper 	ATF_TP_ADD_TC(tp, kvm_open_negative_test_invalid_execfile);
101*10f81a9bSEnji Cooper 	ATF_TP_ADD_TC(tp, kvm_open_negative_test_nonexistent_corefile);
102*10f81a9bSEnji Cooper 	ATF_TP_ADD_TC(tp, kvm_open_negative_test_nonexistent_execfile);
103*10f81a9bSEnji Cooper 
104*10f81a9bSEnji Cooper 	return (atf_no_error());
105*10f81a9bSEnji Cooper }
106