xref: /freebsd/lib/libkvm/tests/kvm_geterr_test.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
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/cdefs.h>
27 #include <sys/param.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <kvm.h>
31 #include <limits.h>
32 #include <signal.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 
36 #include <atf-c.h>
37 
38 #include "kvm_private.h"
39 
40 #include "kvm_test_common.h"
41 
42 ATF_TC(kvm_geterr_negative_test_NULL);
43 ATF_TC_HEAD(kvm_geterr_negative_test_NULL, tc)
44 {
45 
46 	atf_tc_set_md_var(tc, "descr",
47 	    "test that kvm_geterr(NULL) returns NULL");
48 }
49 
50 ATF_TC_BODY(kvm_geterr_negative_test_NULL, tc)
51 {
52 
53 	ATF_REQUIRE(!errbuf_has_error(kvm_geterr(NULL)));
54 }
55 
56 /* 1100090 was where kvm_open2(3) was introduced. */
57 #if __FreeBSD_version >= 1100091
58 ATF_TC(kvm_geterr_positive_test_error);
59 ATF_TC_HEAD(kvm_geterr_positive_test_error, tc)
60 {
61 
62 	atf_tc_set_md_var(tc, "descr",
63 	    "test that kvm_geterr(kd) when kd doesn't contain an error returns \"\"");
64 	atf_tc_set_md_var(tc, "require.user", "root");
65 }
66 
67 ATF_TC_BODY(kvm_geterr_positive_test_error, tc)
68 {
69 	kvm_t *kd;
70 	char *error_msg;
71 
72 	errbuf_clear();
73 	kd = kvm_open2(NULL, NULL, O_RDONLY, errbuf, NULL);
74 	ATF_CHECK(!errbuf_has_error(errbuf));
75 	ATF_REQUIRE_MSG(kd != NULL, "kvm_open2 failed: %s", errbuf);
76 	ATF_REQUIRE_MSG(kvm_write(kd, 0, NULL, 0) == -1,
77 	    "kvm_write succeeded unexpectedly on an O_RDONLY file descriptor");
78 	error_msg = kvm_geterr(kd);
79 	ATF_CHECK(errbuf_has_error(error_msg));
80 	ATF_REQUIRE_MSG(kvm_close(kd) == 0, "kvm_close failed: %s",
81 	    strerror(errno));
82 }
83 
84 ATF_TC(kvm_geterr_positive_test_no_error);
85 ATF_TC_HEAD(kvm_geterr_positive_test_no_error, tc)
86 {
87 
88 	atf_tc_set_md_var(tc, "descr",
89 	    "test that kvm_geterr(kd) when kd contains an error returns an error message");
90 	atf_tc_set_md_var(tc, "require.user", "root");
91 }
92 
93 ATF_TC_BODY(kvm_geterr_positive_test_no_error, tc)
94 {
95 #define	ALL_IS_WELL	"that ends well"
96 	kvm_t *kd;
97 	char *error_msg;
98 	struct nlist nl[] = {
99 #define	SYMNAME	"_mp_maxcpus"
100 #define	X_MAXCPUS	0
101 		{ SYMNAME, 0, 0, 0, 0 },
102 		{ NULL, 0, 0, 0, 0 },
103 	};
104 	ssize_t rc;
105 	int mp_maxcpus, retcode;
106 
107 	errbuf_clear();
108 	kd = kvm_open2(NULL, NULL, O_RDONLY, errbuf, NULL);
109 	ATF_CHECK(!errbuf_has_error(errbuf));
110 	ATF_REQUIRE_MSG(kd != NULL, "kvm_open2 failed: %s", errbuf);
111 	retcode = kvm_nlist(kd, nl);
112 	ATF_REQUIRE_MSG(retcode != -1,
113 	    "kvm_nlist failed (returned %d): %s", retcode, kvm_geterr(kd));
114 	if (nl[X_MAXCPUS].n_type == 0)
115 		atf_tc_skip("symbol (\"%s\") couldn't be found", SYMNAME);
116 	_kvm_err(kd, NULL, "%s", ALL_IS_WELL); /* XXX: internal API */
117 	rc = kvm_read(kd, nl[X_MAXCPUS].n_value, &mp_maxcpus,
118 	    sizeof(mp_maxcpus));
119 
120 	ATF_REQUIRE_MSG(rc != -1, "kvm_read failed: %s", kvm_geterr(kd));
121 	error_msg = kvm_geterr(kd);
122 	ATF_REQUIRE_MSG(strcmp(error_msg, ALL_IS_WELL) == 0,
123 	    "error message changed: %s", error_msg);
124 	ATF_REQUIRE_MSG(kvm_close(kd) == 0, "kvm_close failed: %s",
125 	    strerror(errno));
126 }
127 #endif
128 
129 ATF_TP_ADD_TCS(tp)
130 {
131 
132 	ATF_TP_ADD_TC(tp, kvm_geterr_negative_test_NULL);
133 #if __FreeBSD_version >= 1100091
134 	ATF_TP_ADD_TC(tp, kvm_geterr_positive_test_error);
135 	ATF_TP_ADD_TC(tp, kvm_geterr_positive_test_no_error);
136 #endif
137 
138 	return (atf_no_error());
139 }
140