xref: /freebsd/lib/libkvm/tests/kvm_geterr_test.c (revision ef36b3f75658d201edb495068db5e1be49593de5)
1 /*-
2  * Copyright (c) 2017 Ngie Cooper <ngie@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <kvm.h>
34 #include <limits.h>
35 #include <signal.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 
39 #include <atf-c.h>
40 
41 #include "kvm_private.h"
42 
43 #include "kvm_test_common.h"
44 
45 ATF_TC(kvm_geterr_negative_test_NULL);
46 ATF_TC_HEAD(kvm_geterr_negative_test_NULL, tc)
47 {
48 
49 	atf_tc_set_md_var(tc, "descr",
50 	    "test that kvm_geterr(NULL) returns NULL");
51 }
52 
53 ATF_TC_BODY(kvm_geterr_negative_test_NULL, tc)
54 {
55 
56 	ATF_REQUIRE(!errbuf_has_error(kvm_geterr(NULL)));
57 }
58 
59 /* 1100090 was where kvm_open2(3) was introduced. */
60 #if __FreeBSD_version >= 1100091
61 ATF_TC(kvm_geterr_positive_test_error);
62 ATF_TC_HEAD(kvm_geterr_positive_test_error, tc)
63 {
64 
65 	atf_tc_set_md_var(tc, "descr",
66 	    "test that kvm_geterr(kd) when kd doesn't contain an error returns \"\"");
67 	atf_tc_set_md_var(tc, "require.user", "root");
68 }
69 
70 ATF_TC_BODY(kvm_geterr_positive_test_error, tc)
71 {
72 	kvm_t *kd;
73 	char *error_msg;
74 
75 	errbuf_clear();
76 	kd = kvm_open2(NULL, NULL, O_RDONLY, errbuf, NULL);
77 	ATF_CHECK(!errbuf_has_error(errbuf));
78 	ATF_REQUIRE_MSG(kd != NULL, "kvm_open2 failed: %s", errbuf);
79 	ATF_REQUIRE_MSG(kvm_write(kd, 0, NULL, 0) == -1,
80 	    "kvm_write succeeded unexpectedly on an O_RDONLY file descriptor");
81 	error_msg = kvm_geterr(kd);
82 	ATF_CHECK(errbuf_has_error(error_msg));
83 	ATF_REQUIRE_MSG(kvm_close(kd) == 0, "kvm_close failed: %s",
84 	    strerror(errno));
85 }
86 
87 ATF_TC(kvm_geterr_positive_test_no_error);
88 ATF_TC_HEAD(kvm_geterr_positive_test_no_error, tc)
89 {
90 
91 	atf_tc_set_md_var(tc, "descr",
92 	    "test that kvm_geterr(kd) when kd contains an error returns an error message");
93 	atf_tc_set_md_var(tc, "require.user", "root");
94 }
95 
96 ATF_TC_BODY(kvm_geterr_positive_test_no_error, tc)
97 {
98 #define	ALL_IS_WELL	"that ends well"
99 	kvm_t *kd;
100 	char *error_msg;
101 	struct nlist nl[] = {
102 #define	SYMNAME	"_mp_maxcpus"
103 #define	X_MAXCPUS	0
104 		{ SYMNAME, 0, 0, 0, 0 },
105 		{ NULL, 0, 0, 0, 0 },
106 	};
107 	ssize_t rc;
108 	int mp_maxcpus, retcode;
109 
110 	errbuf_clear();
111 	kd = kvm_open2(NULL, NULL, O_RDONLY, errbuf, NULL);
112 	ATF_CHECK(!errbuf_has_error(errbuf));
113 	ATF_REQUIRE_MSG(kd != NULL, "kvm_open2 failed: %s", errbuf);
114 	retcode = kvm_nlist(kd, nl);
115 	ATF_REQUIRE_MSG(retcode != -1,
116 	    "kvm_nlist failed (returned %d): %s", retcode, kvm_geterr(kd));
117 	if (nl[X_MAXCPUS].n_type == 0)
118 		atf_tc_skip("symbol (\"%s\") couldn't be found", SYMNAME);
119 	_kvm_err(kd, NULL, "%s", ALL_IS_WELL); /* XXX: internal API */
120 	rc = kvm_read(kd, nl[X_MAXCPUS].n_value, &mp_maxcpus,
121 	    sizeof(mp_maxcpus));
122 
123 	ATF_REQUIRE_MSG(rc != -1, "kvm_read failed: %s", kvm_geterr(kd));
124 	error_msg = kvm_geterr(kd);
125 	ATF_REQUIRE_MSG(strcmp(error_msg, ALL_IS_WELL) == 0,
126 	    "error message changed: %s", error_msg);
127 	ATF_REQUIRE_MSG(kvm_close(kd) == 0, "kvm_close failed: %s",
128 	    strerror(errno));
129 }
130 #endif
131 
132 ATF_TP_ADD_TCS(tp)
133 {
134 
135 	ATF_TP_ADD_TC(tp, kvm_geterr_negative_test_NULL);
136 #if __FreeBSD_version >= 1100091
137 	ATF_TP_ADD_TC(tp, kvm_geterr_positive_test_error);
138 	ATF_TP_ADD_TC(tp, kvm_geterr_positive_test_no_error);
139 #endif
140 
141 	return (atf_no_error());
142 }
143