2 * Copyright (c) 2017 Ngie Cooper <ngie@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
41 #include "kvm_private.h"
43 #include "kvm_test_common.h"
45 ATF_TC(kvm_geterr_negative_test_NULL);
46 ATF_TC_HEAD(kvm_geterr_negative_test_NULL, tc)
49 atf_tc_set_md_var(tc, "descr",
50 "test that kvm_geterr(NULL) returns NULL");
53 ATF_TC_BODY(kvm_geterr_negative_test_NULL, tc)
56 ATF_REQUIRE(!errbuf_has_error(kvm_geterr(NULL)));
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)
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");
70 ATF_TC_BODY(kvm_geterr_positive_test_error, tc)
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",
87 ATF_TC(kvm_geterr_positive_test_no_error);
88 ATF_TC_HEAD(kvm_geterr_positive_test_no_error, tc)
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");
96 ATF_TC_BODY(kvm_geterr_positive_test_no_error, tc)
98 #define ALL_IS_WELL "that ends well"
101 struct nlist nl[] = {
102 #define SYMNAME "_mp_maxcpus"
104 { SYMNAME, 0, 0, 0, 0 },
105 { NULL, 0, 0, 0, 0 },
108 int mp_maxcpus, retcode;
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,
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",
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);
141 return (atf_no_error());