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