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