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