Index: head/lib/libcam/tests/libcam_test.c =================================================================== --- head/lib/libcam/tests/libcam_test.c (revision 346571) +++ head/lib/libcam/tests/libcam_test.c (revision 346572) @@ -1,322 +1,321 @@ /*- - * Copyright (c) 2017 Ngie Cooper - * All rights reserved. + * Copyright (c) 2017 Enji Cooper * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* Tests functions in lib/libcam/camlib.c */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include static const char * get_cam_test_device(const atf_tc_t *tc) { const char *cam_test_device; cam_test_device = atf_tc_get_config_var(tc, "cam_test_device"); return (cam_test_device); } static void cam_clear_error(void) { strcpy(cam_errbuf, ""); } static bool cam_has_error(void) { return (strlen(cam_errbuf) != 0); } ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_NULL_path); ATF_TC_BODY(cam_get_device_negative_test_NULL_path, tc) { char parsed_dev_name[DEV_IDLEN + 1]; int parsed_unit; ATF_REQUIRE_MSG(cam_get_device(NULL, parsed_dev_name, nitems(parsed_dev_name), &parsed_unit) == -1, "cam_get_device succeeded unexpectedly"); } ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_bad_path); ATF_TC_BODY(cam_get_device_negative_test_bad_path, tc) { char parsed_dev_name[DEV_IDLEN + 1]; int parsed_unit; ATF_REQUIRE_MSG(cam_get_device("1ada", parsed_dev_name, nitems(parsed_dev_name), &parsed_unit) == -1, "cam_get_device succeeded unexpectedly"); } ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_nul_path); ATF_TC_BODY(cam_get_device_negative_test_nul_path, tc) { char parsed_dev_name[DEV_IDLEN + 1]; int parsed_unit; ATF_REQUIRE_MSG(cam_get_device("", parsed_dev_name, nitems(parsed_dev_name), &parsed_unit) == -1, "cam_get_device succeeded unexpectedly"); } ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_root); ATF_TC_BODY(cam_get_device_negative_test_root, tc) { char parsed_dev_name[DEV_IDLEN + 1]; int parsed_unit; ATF_REQUIRE_MSG(cam_get_device("/", parsed_dev_name, nitems(parsed_dev_name), &parsed_unit) == -1, "cam_get_device succeeded unexpectedly"); } ATF_TC_WITHOUT_HEAD(cam_get_device_positive_test); ATF_TC_BODY(cam_get_device_positive_test, tc) { char expected_dev_name[] = "foo"; char parsed_dev_name[DEV_IDLEN + 1]; int expected_unit, parsed_unit; expected_unit = 1; ATF_REQUIRE_MSG(cam_get_device("/dev/foo1", parsed_dev_name, nitems(parsed_dev_name), &parsed_unit) == 0, "cam_get_device failed"); ATF_REQUIRE_STREQ(parsed_dev_name, expected_dev_name); ATF_REQUIRE(parsed_unit == expected_unit); strcpy(parsed_dev_name, ""); parsed_unit = -1; ATF_REQUIRE_MSG(cam_get_device("foo1", parsed_dev_name, nitems(parsed_dev_name), &parsed_unit) == 0, "cam_get_device failed"); ATF_REQUIRE_STREQ(parsed_dev_name, expected_dev_name); ATF_REQUIRE(parsed_unit == expected_unit); } /* * sa(4) uniquely creates nsa and esa device nodes for non-rewind operations * and eject-on-close operations. cam_get_device must special case these nodes * to always return the base device. */ ATF_TC_WITHOUT_HEAD(cam_get_device_sa_test); ATF_TC_BODY(cam_get_device_sa_test, tc) { char parsed_dev_name[DEV_IDLEN + 1]; int parsed_unit; ATF_REQUIRE_MSG(cam_get_device("nsa99", parsed_dev_name, nitems(parsed_dev_name), &parsed_unit) == 0, "cam_get_device failed"); ATF_REQUIRE_STREQ(parsed_dev_name, "sa"); ATF_REQUIRE(parsed_unit == 99); strcpy(parsed_dev_name, ""); parsed_unit = -1; ATF_REQUIRE_MSG(cam_get_device("esa99", parsed_dev_name, nitems(parsed_dev_name), &parsed_unit) == 0, "cam_get_device failed"); ATF_REQUIRE_STREQ(parsed_dev_name, "sa"); ATF_REQUIRE(parsed_unit == 99); } ATF_TC(cam_open_device_negative_test_O_RDONLY); ATF_TC_HEAD(cam_open_device_negative_test_O_RDONLY, tc) { atf_tc_set_md_var(tc, "descr", "test that cam_open_device(`cam_device`, O_RDONLY) fails to open " "the underlying pass(4) device (bug 217649)"); atf_tc_set_md_var(tc, "require.config", "cam_test_device"); atf_tc_set_md_var(tc, "require.user", "root"); } ATF_TC_BODY(cam_open_device_negative_test_O_RDONLY, tc) { const char *cam_test_device; cam_test_device = get_cam_test_device(tc); cam_clear_error(); ATF_CHECK(cam_open_device(cam_test_device, O_RDONLY) == NULL); ATF_REQUIRE(cam_has_error()); } ATF_TC(cam_open_device_negative_test_nonexistent); ATF_TC_HEAD(cam_open_device_negative_test_nonexistent, tc) { atf_tc_set_md_var(tc, "require.user", "root"); } ATF_TC_BODY(cam_open_device_negative_test_nonexistent, tc) { cam_clear_error(); ATF_REQUIRE(cam_open_device("/nonexistent", O_RDWR) == NULL); ATF_REQUIRE(cam_has_error()); } ATF_TC(cam_open_device_negative_test_unprivileged); ATF_TC_HEAD(cam_open_device_negative_test_unprivileged, tc) { atf_tc_set_md_var(tc, "require.config", "cam_test_device"); atf_tc_set_md_var(tc, "require.user", "unprivileged"); } ATF_TC_BODY(cam_open_device_negative_test_unprivileged, tc) { const char *cam_test_device; cam_test_device = get_cam_test_device(tc); cam_clear_error(); ATF_CHECK(cam_open_device(cam_test_device, O_RDONLY) == NULL); ATF_REQUIRE(cam_has_error()); cam_clear_error(); ATF_CHECK(cam_open_device(cam_test_device, O_RDWR) == NULL); ATF_REQUIRE(cam_has_error()); } ATF_TC(cam_open_device_positive_test); ATF_TC_HEAD(cam_open_device_positive_test, tc) { atf_tc_set_md_var(tc, "require.config", "cam_test_device"); atf_tc_set_md_var(tc, "require.user", "root"); } ATF_TC_BODY(cam_open_device_positive_test, tc) { struct cam_device *cam_dev; const char *cam_test_device; cam_test_device = get_cam_test_device(tc); cam_clear_error(); cam_dev = cam_open_device(cam_test_device, O_RDWR); ATF_CHECK_MSG(cam_dev != NULL, "cam_open_device failed: %s", cam_errbuf); ATF_REQUIRE(!cam_has_error()); cam_close_device(cam_dev); } ATF_TC(cam_close_device_negative_test_NULL); ATF_TC_HEAD(cam_close_device_negative_test_NULL, tc) { atf_tc_set_md_var(tc, "descr", "test that cam_close_device(NULL) succeeds without error"); atf_tc_set_md_var(tc, "require.user", "root"); } ATF_TC_BODY(cam_close_device_negative_test_NULL, tc) { cam_clear_error(); cam_close_device(NULL); ATF_REQUIRE(!cam_has_error()); } ATF_TC(cam_getccb_positive_test); ATF_TC_HEAD(cam_getccb_positive_test, tc) { atf_tc_set_md_var(tc, "require.config", "cam_test_device"); atf_tc_set_md_var(tc, "require.user", "root"); } ATF_TC_BODY(cam_getccb_positive_test, tc) { union ccb *cam_ccb; struct cam_device *cam_dev; const char *cam_test_device; cam_test_device = get_cam_test_device(tc); cam_clear_error(); cam_dev = cam_open_device(cam_test_device, O_RDWR); ATF_CHECK_MSG(cam_dev != NULL, "cam_open_device failed: %s", cam_errbuf); ATF_REQUIRE(!cam_has_error()); cam_ccb = cam_getccb(cam_dev); ATF_CHECK_MSG(cam_ccb != NULL, "get_camccb failed: %s", cam_errbuf); ATF_REQUIRE(!cam_has_error()); cam_freeccb(cam_ccb); cam_close_device(cam_dev); } ATF_TC(cam_freeccb_negative_test_NULL); ATF_TC_HEAD(cam_freeccb_negative_test_NULL, tc) { atf_tc_set_md_var(tc, "descr", "test that cam_freeccb(NULL) succeeds without error"); atf_tc_set_md_var(tc, "require.user", "root"); } ATF_TC_BODY(cam_freeccb_negative_test_NULL, tc) { cam_clear_error(); cam_freeccb(NULL); ATF_REQUIRE(!cam_has_error()); } ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, cam_get_device_negative_test_NULL_path); ATF_TP_ADD_TC(tp, cam_get_device_negative_test_bad_path); ATF_TP_ADD_TC(tp, cam_get_device_negative_test_nul_path); ATF_TP_ADD_TC(tp, cam_get_device_negative_test_root); ATF_TP_ADD_TC(tp, cam_get_device_positive_test); ATF_TP_ADD_TC(tp, cam_get_device_sa_test); ATF_TP_ADD_TC(tp, cam_open_device_negative_test_O_RDONLY); ATF_TP_ADD_TC(tp, cam_open_device_negative_test_nonexistent); ATF_TP_ADD_TC(tp, cam_open_device_negative_test_unprivileged); ATF_TP_ADD_TC(tp, cam_open_device_positive_test); ATF_TP_ADD_TC(tp, cam_close_device_negative_test_NULL); ATF_TP_ADD_TC(tp, cam_getccb_positive_test); ATF_TP_ADD_TC(tp, cam_freeccb_negative_test_NULL); return (atf_no_error()); } Index: head/lib/libkvm/tests/kvm_close_test.c =================================================================== --- head/lib/libkvm/tests/kvm_close_test.c (revision 346571) +++ head/lib/libkvm/tests/kvm_close_test.c (revision 346572) @@ -1,57 +1,56 @@ /*- - * Copyright (c) 2017 Ngie Cooper - * All rights reserved. + * Copyright (c) 2017 Enji Cooper * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include ATF_TC(kvm_close_negative_test_NULL); ATF_TC_HEAD(kvm_close_negative_test_NULL, tc) { atf_tc_set_md_var(tc, "descr", "test that kvm_close(NULL) succeeds without error"); } ATF_TC_BODY(kvm_close_negative_test_NULL, tc) { ATF_REQUIRE_ERRNO(EINVAL, kvm_close(NULL) == -1); } ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, kvm_close_negative_test_NULL); return (atf_no_error()); } Index: head/lib/libkvm/tests/kvm_geterr_test.c =================================================================== --- head/lib/libkvm/tests/kvm_geterr_test.c (revision 346571) +++ head/lib/libkvm/tests/kvm_geterr_test.c (revision 346572) @@ -1,142 +1,141 @@ /*- - * Copyright (c) 2017 Ngie Cooper - * All rights reserved. + * Copyright (c) 2017 Enji Cooper * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include "kvm_private.h" #include "kvm_test_common.h" ATF_TC(kvm_geterr_negative_test_NULL); ATF_TC_HEAD(kvm_geterr_negative_test_NULL, tc) { atf_tc_set_md_var(tc, "descr", "test that kvm_geterr(NULL) returns NULL"); } ATF_TC_BODY(kvm_geterr_negative_test_NULL, tc) { ATF_REQUIRE(!errbuf_has_error(kvm_geterr(NULL))); } /* 1100090 was where kvm_open2(3) was introduced. */ #if __FreeBSD_version >= 1100091 ATF_TC(kvm_geterr_positive_test_error); ATF_TC_HEAD(kvm_geterr_positive_test_error, tc) { atf_tc_set_md_var(tc, "descr", "test that kvm_geterr(kd) when kd doesn't contain an error returns \"\""); atf_tc_set_md_var(tc, "require.user", "root"); } ATF_TC_BODY(kvm_geterr_positive_test_error, tc) { kvm_t *kd; char *error_msg; errbuf_clear(); kd = kvm_open2(NULL, NULL, O_RDONLY, errbuf, NULL); ATF_CHECK(!errbuf_has_error(errbuf)); ATF_REQUIRE_MSG(kd != NULL, "kvm_open2 failed: %s", errbuf); ATF_REQUIRE_MSG(kvm_write(kd, 0, NULL, 0) == -1, "kvm_write succeeded unexpectedly on an O_RDONLY file descriptor"); error_msg = kvm_geterr(kd); ATF_CHECK(errbuf_has_error(error_msg)); ATF_REQUIRE_MSG(kvm_close(kd) == 0, "kvm_close failed: %s", strerror(errno)); } ATF_TC(kvm_geterr_positive_test_no_error); ATF_TC_HEAD(kvm_geterr_positive_test_no_error, tc) { atf_tc_set_md_var(tc, "descr", "test that kvm_geterr(kd) when kd contains an error returns an error message"); atf_tc_set_md_var(tc, "require.user", "root"); } ATF_TC_BODY(kvm_geterr_positive_test_no_error, tc) { #define ALL_IS_WELL "that ends well" kvm_t *kd; char *error_msg; struct nlist nl[] = { #define SYMNAME "_mp_maxcpus" #define X_MAXCPUS 0 { SYMNAME, 0, 0, 0, 0 }, { NULL, 0, 0, 0, 0 }, }; ssize_t rc; int mp_maxcpus, retcode; errbuf_clear(); kd = kvm_open2(NULL, NULL, O_RDONLY, errbuf, NULL); ATF_CHECK(!errbuf_has_error(errbuf)); ATF_REQUIRE_MSG(kd != NULL, "kvm_open2 failed: %s", errbuf); retcode = kvm_nlist(kd, nl); ATF_REQUIRE_MSG(retcode != -1, "kvm_nlist failed (returned %d): %s", retcode, kvm_geterr(kd)); if (nl[X_MAXCPUS].n_type == 0) atf_tc_skip("symbol (\"%s\") couldn't be found", SYMNAME); _kvm_err(kd, NULL, "%s", ALL_IS_WELL); /* XXX: internal API */ rc = kvm_read(kd, nl[X_MAXCPUS].n_value, &mp_maxcpus, sizeof(mp_maxcpus)); ATF_REQUIRE_MSG(rc != -1, "kvm_read failed: %s", kvm_geterr(kd)); error_msg = kvm_geterr(kd); ATF_REQUIRE_MSG(strcmp(error_msg, ALL_IS_WELL) == 0, "error message changed: %s", error_msg); ATF_REQUIRE_MSG(kvm_close(kd) == 0, "kvm_close failed: %s", strerror(errno)); } #endif ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, kvm_geterr_negative_test_NULL); #if __FreeBSD_version >= 1100091 ATF_TP_ADD_TC(tp, kvm_geterr_positive_test_error); ATF_TP_ADD_TC(tp, kvm_geterr_positive_test_no_error); #endif return (atf_no_error()); } Index: head/lib/libkvm/tests/kvm_open2_test.c =================================================================== --- head/lib/libkvm/tests/kvm_open2_test.c (revision 346571) +++ head/lib/libkvm/tests/kvm_open2_test.c (revision 346572) @@ -1,117 +1,116 @@ /*- - * Copyright (c) 2017 Ngie Cooper - * All rights reserved. + * Copyright (c) 2017 Enji Cooper * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include "kvm_test_common.h" ATF_TC_WITHOUT_HEAD(kvm_open2_negative_test_nonexistent_corefile); ATF_TC_BODY(kvm_open2_negative_test_nonexistent_corefile, tc) { errbuf_clear(); ATF_CHECK(kvm_open2(NULL, "/nonexistent", O_RDONLY, NULL, NULL) == NULL); ATF_CHECK(!errbuf_has_error(errbuf)); errbuf_clear(); ATF_CHECK(kvm_open2(NULL, "/nonexistent", O_RDONLY, errbuf, NULL) == NULL); ATF_CHECK(errbuf_has_error(errbuf)); } ATF_TC_WITHOUT_HEAD(kvm_open2_negative_test_nonexistent_execfile); ATF_TC_BODY(kvm_open2_negative_test_nonexistent_execfile, tc) { errbuf_clear(); ATF_CHECK(kvm_open2("/nonexistent", _PATH_DEVZERO, O_RDONLY, NULL, NULL) == NULL); ATF_CHECK(strlen(errbuf) == 0); errbuf_clear(); ATF_CHECK(kvm_open2("/nonexistent", _PATH_DEVZERO, O_RDONLY, errbuf, NULL) == NULL); ATF_CHECK(errbuf_has_error(errbuf)); } ATF_TC(kvm_open2_negative_test_invalid_corefile); ATF_TC_HEAD(kvm_open2_negative_test_invalid_corefile, tc) { atf_tc_set_md_var(tc, "require.user", "root"); } ATF_TC_BODY(kvm_open2_negative_test_invalid_corefile, tc) { kvm_t *kd; errbuf_clear(); atf_utils_create_file("some-file", "this is a text file"); kd = kvm_open2(NULL, "some-file", O_RDONLY, errbuf, NULL); ATF_CHECK(errbuf_has_error(errbuf)); ATF_REQUIRE_MSG(kd == NULL, "kvm_open2 succeeded"); } ATF_TC(kvm_open2_negative_test_invalid_execfile); ATF_TC_HEAD(kvm_open2_negative_test_invalid_execfile, tc) { atf_tc_set_md_var(tc, "require.user", "root"); } ATF_TC_BODY(kvm_open2_negative_test_invalid_execfile, tc) { kvm_t *kd; errbuf_clear(); atf_utils_create_file("some-file", "this is a text file"); kd = kvm_open2("some-file", "/bin/sh", O_RDONLY, errbuf, NULL); ATF_CHECK(errbuf_has_error(errbuf)); ATF_REQUIRE_MSG(kd == NULL, "kvm_open2 succeeded unexpectedly"); } ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, kvm_open2_negative_test_invalid_corefile); ATF_TP_ADD_TC(tp, kvm_open2_negative_test_invalid_execfile); ATF_TP_ADD_TC(tp, kvm_open2_negative_test_nonexistent_corefile); ATF_TP_ADD_TC(tp, kvm_open2_negative_test_nonexistent_execfile); return (atf_no_error()); } Index: head/lib/libkvm/tests/kvm_open_test.c =================================================================== --- head/lib/libkvm/tests/kvm_open_test.c (revision 346571) +++ head/lib/libkvm/tests/kvm_open_test.c (revision 346572) @@ -1,105 +1,104 @@ /*- - * Copyright (c) 2017 Ngie Cooper - * All rights reserved. + * Copyright (c) 2017 Enji Cooper * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include "kvm_test_common.h" ATF_TC_WITHOUT_HEAD(kvm_open_negative_test_nonexistent_corefile); ATF_TC_BODY(kvm_open_negative_test_nonexistent_corefile, tc) { ATF_CHECK(kvm_open(NULL, "/nonexistent", NULL, O_RDONLY, NULL) == NULL); ATF_CHECK(kvm_open(NULL, "/nonexistent", NULL, O_RDONLY, getprogname()) == NULL); } ATF_TC_WITHOUT_HEAD(kvm_open_negative_test_nonexistent_execfile); ATF_TC_BODY(kvm_open_negative_test_nonexistent_execfile, tc) { ATF_CHECK(kvm_open("/nonexistent", _PATH_DEVZERO, NULL, O_RDONLY, NULL) == NULL); ATF_CHECK(kvm_open("/nonexistent", _PATH_DEVZERO, NULL, O_RDONLY, getprogname()) == NULL); } ATF_TC(kvm_open_negative_test_invalid_corefile); ATF_TC_HEAD(kvm_open_negative_test_invalid_corefile, tc) { atf_tc_set_md_var(tc, "require.user", "root"); } ATF_TC_BODY(kvm_open_negative_test_invalid_corefile, tc) { kvm_t *kd; atf_utils_create_file("some-file", "this is a text file"); kd = kvm_open(NULL, "some-file", NULL, O_RDONLY, getprogname()); ATF_REQUIRE_MSG(kd == NULL, "kvm_open didn't return NULL on failure"); } ATF_TC(kvm_open_negative_test_invalid_execfile); ATF_TC_HEAD(kvm_open_negative_test_invalid_execfile, tc) { atf_tc_set_md_var(tc, "require.user", "root"); } ATF_TC_BODY(kvm_open_negative_test_invalid_execfile, tc) { kvm_t *kd; atf_utils_create_file("some-file", "this is a text file"); kd = kvm_open("some-file", "/bin/sh", NULL, O_RDONLY, getprogname()); ATF_REQUIRE_MSG(kd == NULL, "kvm_open succeeded unexpectedly"); } ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, kvm_open_negative_test_invalid_corefile); ATF_TP_ADD_TC(tp, kvm_open_negative_test_invalid_execfile); ATF_TP_ADD_TC(tp, kvm_open_negative_test_nonexistent_corefile); ATF_TP_ADD_TC(tp, kvm_open_negative_test_nonexistent_execfile); return (atf_no_error()); } Index: head/lib/libkvm/tests/kvm_test_common.c =================================================================== --- head/lib/libkvm/tests/kvm_test_common.c (revision 346571) +++ head/lib/libkvm/tests/kvm_test_common.c (revision 346572) @@ -1,49 +1,48 @@ /*- - * Copyright (c) 2017 Ngie Cooper - * All rights reserved. + * Copyright (c) 2017 Enji Cooper * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include "kvm_test_common.h" char errbuf[_POSIX2_LINE_MAX]; void errbuf_clear(void) { strcpy(errbuf, ""); } bool errbuf_has_error(const char *_errbuf) { return (strcmp(_errbuf, "")); } Index: head/lib/libkvm/tests/kvm_test_common.h =================================================================== --- head/lib/libkvm/tests/kvm_test_common.h (revision 346571) +++ head/lib/libkvm/tests/kvm_test_common.h (revision 346572) @@ -1,40 +1,39 @@ /*- - * Copyright (c) 2017 Ngie Cooper - * All rights reserved. + * Copyright (c) 2017 Enji Cooper * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef __KVM_TEST_COMMON_H__ #include #include #include extern char errbuf[_POSIX2_LINE_MAX]; void errbuf_clear(void); bool errbuf_has_error(const char *); #endif Index: head/lib/libsbuf/tests/sbuf_core_test.c =================================================================== --- head/lib/libsbuf/tests/sbuf_core_test.c (revision 346571) +++ head/lib/libsbuf/tests/sbuf_core_test.c (revision 346572) @@ -1,212 +1,211 @@ /*- - * Copyright (c) 2017 Ngie Cooper - * All rights reserved. + * Copyright (c) 2017 Enji Cooper * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include "sbuf_test_common.h" static char test_string[] = "this is a test string"; #define TEST_STRING_CHOP_COUNT 5 _Static_assert(nitems(test_string) > TEST_STRING_CHOP_COUNT, "test_string is too short"); ATF_TC_WITHOUT_HEAD(sbuf_clear_test); ATF_TC_BODY(sbuf_clear_test, tc) { struct sbuf *sb; ssize_t buf_len; pid_t child_proc; sb = sbuf_new_auto(); ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s", strerror(errno)); ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed"); /* * Cheat so we can get the contents of the buffer before calling * sbuf_finish(3) below, making additional sbuf changes impossible. */ child_proc = atf_utils_fork(); if (child_proc == 0) { sbuf_putbuf(sb); exit(0); } atf_utils_wait(child_proc, 0, test_string, ""); sbuf_clear(sb); ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s", strerror(errno)); buf_len = sbuf_len(sb); ATF_REQUIRE_MSG(buf_len == 0, "sbuf_len (%zd) != 0", buf_len); ATF_REQUIRE_STREQ_MSG(sbuf_data(sb), "", "sbuf (\"%s\") was not empty", sbuf_data(sb)); sbuf_delete(sb); } ATF_TC_WITHOUT_HEAD(sbuf_done_and_sbuf_finish_test); ATF_TC_BODY(sbuf_done_and_sbuf_finish_test, tc) { struct sbuf *sb; sb = sbuf_new_auto(); ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s", strerror(errno)); ATF_CHECK(sbuf_done(sb) == 0); ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s", strerror(errno)); ATF_CHECK(sbuf_done(sb) != 0); sbuf_delete(sb); } ATF_TC_WITHOUT_HEAD(sbuf_len_test); ATF_TC_BODY(sbuf_len_test, tc) { struct sbuf *sb; ssize_t buf_len, test_string_len; int i; sb = sbuf_new_auto(); ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s", strerror(errno)); test_string_len = strlen(test_string); for (i = 0; i < 20; i++) { buf_len = sbuf_len(sb); ATF_REQUIRE_MSG(buf_len == (ssize_t)(i * test_string_len), "sbuf_len (%zd) != %zu", buf_len, i * test_string_len); ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed"); } #ifdef HAVE_SBUF_SET_FLAGS sbuf_set_flags(sb, SBUF_INCLUDENUL); ATF_REQUIRE_MSG((ssize_t)(i * test_string_len + 1) == sbuf_len(sb), "sbuf_len(..) didn't report the NUL char"); #endif ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s", strerror(errno)); sbuf_delete(sb); } ATF_TC_WITHOUT_HEAD(sbuf_setpos_test); ATF_TC_BODY(sbuf_setpos_test, tc) { struct sbuf *sb; size_t test_string_chopped_len, test_string_len; ssize_t buf_len; sb = sbuf_new_auto(); ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s", strerror(errno)); /* * An obvious sanity check -- if sbuf_len(..) lies, these invariants * are impossible to test. */ ATF_REQUIRE(sbuf_len(sb) == 0); ATF_CHECK(sbuf_setpos(sb, -1) == -1); ATF_CHECK(sbuf_setpos(sb, 0) == 0); ATF_CHECK(sbuf_setpos(sb, 1) == -1); ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed"); buf_len = sbuf_len(sb); test_string_len = strlen(test_string); test_string_chopped_len = test_string_len - TEST_STRING_CHOP_COUNT; ATF_REQUIRE_MSG(buf_len == (ssize_t)test_string_len, "sbuf length (%zd) != test_string length (%zu)", buf_len, test_string_len); /* Out of bounds (under length) */ ATF_CHECK(sbuf_setpos(sb, -1) == -1); /* * Out of bounds (over length) * * Note: SBUF_INCLUDENUL not set, so take '\0' into account. */ ATF_CHECK(sbuf_setpos(sb, test_string_len + 2) == -1); /* Within bounds */ ATF_CHECK(sbuf_setpos(sb, test_string_chopped_len) == 0); ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s", strerror(errno)); buf_len = sbuf_len(sb); ATF_REQUIRE_MSG(buf_len == (ssize_t)test_string_chopped_len, "sbuf_setpos didn't truncate string as expected"); ATF_REQUIRE_MSG(strncmp(sbuf_data(sb), test_string, buf_len) == 0, "sbuf (\"%s\") != test string (\"%s\") for [0,%zd]", sbuf_data(sb), test_string, buf_len); sbuf_delete(sb); } ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, sbuf_clear_test); ATF_TP_ADD_TC(tp, sbuf_done_and_sbuf_finish_test); ATF_TP_ADD_TC(tp, sbuf_len_test); #if 0 /* TODO */ #ifdef HAVE_SBUF_CLEAR_FLAGS ATF_TP_ADD_TC(tp, sbuf_clear_flags_test); #endif #ifdef HAVE_SBUF_GET_FLAGS ATF_TP_ADD_TC(tp, sbuf_get_flags_test); #endif ATF_TP_ADD_TC(tp, sbuf_new_positive_test); ATF_TP_ADD_TC(tp, sbuf_new_negative_test); #ifdef HAVE_SBUF_SET_FLAGS ATF_TP_ADD_TC(tp, sbuf_set_flags_test); #endif #endif ATF_TP_ADD_TC(tp, sbuf_setpos_test); return (atf_no_error()); } Index: head/lib/libsbuf/tests/sbuf_stdio_test.c =================================================================== --- head/lib/libsbuf/tests/sbuf_stdio_test.c (revision 346571) +++ head/lib/libsbuf/tests/sbuf_stdio_test.c (revision 346572) @@ -1,161 +1,160 @@ /*- - * Copyright (c) 2017 Ngie Cooper - * All rights reserved. + * Copyright (c) 2017 Enji Cooper * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include "sbuf_test_common.h" static char test_string[] = "this is a test string"; #define MESSAGE_FORMAT "message: %s\n" #define MESSAGE_SEPARATOR ';' static int sbuf_vprintf_helper(struct sbuf *sb, const char * restrict format, ...) { va_list ap; int rc; va_start(ap, format); rc = sbuf_vprintf(sb, format, ap); va_end(ap); return (rc); } ATF_TC_WITHOUT_HEAD(sbuf_printf_test); ATF_TC_BODY(sbuf_printf_test, tc) { struct sbuf *sb; char *test_string_tmp; asprintf(&test_string_tmp, "%s%c" MESSAGE_FORMAT, test_string, MESSAGE_SEPARATOR, test_string); ATF_REQUIRE_MSG(test_string_tmp != NULL, "asprintf failed"); sb = sbuf_new_auto(); ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s", strerror(errno)); ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed"); ATF_REQUIRE_MSG(sbuf_putc(sb, MESSAGE_SEPARATOR) == 0, "sbuf_putc failed"); ATF_REQUIRE_MSG(sbuf_printf(sb, MESSAGE_FORMAT, test_string) == 0, "sbuf_printf failed"); ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s", strerror(errno)); ATF_REQUIRE_STREQ_MSG(sbuf_data(sb), test_string_tmp, "sbuf (\"%s\") != test string (\"%s\")", sbuf_data(sb), test_string_tmp); sbuf_delete(sb); free(test_string_tmp); } ATF_TC_WITHOUT_HEAD(sbuf_putbuf_test); ATF_TC_BODY(sbuf_putbuf_test, tc) { struct sbuf *sb; pid_t child_proc; sb = sbuf_new_auto(); ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s", strerror(errno)); ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed"); child_proc = atf_utils_fork(); if (child_proc == 0) { sbuf_putbuf(sb); exit(0); } atf_utils_wait(child_proc, 0, test_string, ""); ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s", strerror(errno)); sbuf_delete(sb); } ATF_TC_WITHOUT_HEAD(sbuf_vprintf_test); ATF_TC_BODY(sbuf_vprintf_test, tc) { struct sbuf *sb; char *test_string_tmp; int rc; asprintf(&test_string_tmp, "%s%c" MESSAGE_FORMAT, test_string, MESSAGE_SEPARATOR, test_string); ATF_REQUIRE_MSG(test_string_tmp != NULL, "asprintf failed"); sb = sbuf_new_auto(); ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s", strerror(errno)); ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed"); ATF_REQUIRE_MSG(sbuf_putc(sb, MESSAGE_SEPARATOR) == 0, "sbuf_putc failed"); rc = sbuf_vprintf_helper(sb, MESSAGE_FORMAT, test_string); ATF_REQUIRE_MSG(rc == 0, "sbuf_vprintf failed"); ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s", strerror(errno)); ATF_REQUIRE_STREQ_MSG(sbuf_data(sb), test_string_tmp, "sbuf (\"%s\") != test string (\"%s\")", sbuf_data(sb), test_string_tmp); sbuf_delete(sb); } ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, sbuf_printf_test); ATF_TP_ADD_TC(tp, sbuf_putbuf_test); ATF_TP_ADD_TC(tp, sbuf_vprintf_test); return (atf_no_error()); } Index: head/lib/libsbuf/tests/sbuf_string_test.c =================================================================== --- head/lib/libsbuf/tests/sbuf_string_test.c (revision 346571) +++ head/lib/libsbuf/tests/sbuf_string_test.c (revision 346572) @@ -1,289 +1,288 @@ /*- - * Copyright (c) 2017 Ngie Cooper - * All rights reserved. + * Copyright (c) 2017 Enji Cooper * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include "sbuf_test_common.h" static char test_string[] = "this is a test string"; static char test_whitespace_string[] = " \f\n\r\t\v "; static int test_buffer[] = { 0, 1, 2, 3, 4, 5, }; static void check_buffers_equal(const void *sb_buf, const void *test_buf, size_t len) { if (memcmp(sb_buf, test_buf, len) != 0) { printf("sbuf:\n"); hexdump(sb_buf, len, NULL, 0), printf("test_buf:\n"); hexdump(test_buf, len, NULL, 0); atf_tc_fail("contents of sbuf didn't match test_buf contents"); } } ATF_TC_WITHOUT_HEAD(sbuf_bcat_test); ATF_TC_BODY(sbuf_bcat_test, tc) { struct sbuf *sb; int *test_buffer_tmp; ssize_t test_sbuf_len; test_buffer_tmp = malloc(sizeof(test_buffer) * 2); ATF_REQUIRE_MSG(test_buffer_tmp != NULL, "malloc failed"); memcpy(test_buffer_tmp, test_buffer, sizeof(test_buffer)); memcpy(&test_buffer_tmp[nitems(test_buffer)], test_buffer, sizeof(test_buffer)); sb = sbuf_new_auto(); ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s", strerror(errno)); ATF_CHECK_MSG(sbuf_bcat(sb, test_buffer, sizeof(test_buffer)) == 0, "sbuf_bcat failed"); test_sbuf_len = sbuf_len(sb); ATF_REQUIRE_MSG(test_sbuf_len == (ssize_t)sizeof(test_buffer), "sbuf_len(..) => %zd (actual) != %zu (expected)", test_sbuf_len, sizeof(test_buffer)); ATF_CHECK_MSG(sbuf_bcat(sb, test_buffer, sizeof(test_buffer)) == 0, "sbuf_bcat failed"); test_sbuf_len = sbuf_len(sb); ATF_REQUIRE_MSG(test_sbuf_len == (ssize_t)(2 * sizeof(test_buffer)), "sbuf_len(..) => %zd (actual) != %zu (expected)", test_sbuf_len, 2 * sizeof(test_buffer)); ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s", strerror(errno)); check_buffers_equal(sbuf_data(sb), test_buffer_tmp, (size_t)test_sbuf_len); sbuf_delete(sb); free(test_buffer_tmp); } ATF_TC_WITHOUT_HEAD(sbuf_bcpy_test); ATF_TC_BODY(sbuf_bcpy_test, tc) { struct sbuf *sb; ssize_t test_sbuf_len; sb = sbuf_new_auto(); ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s", strerror(errno)); ATF_CHECK_MSG(sbuf_bcpy(sb, test_buffer, sizeof(test_buffer)) == 0, "sbuf_bcpy failed"); test_sbuf_len = sbuf_len(sb); ATF_REQUIRE_MSG(test_sbuf_len == (ssize_t)sizeof(test_buffer), "sbuf_len(..) => %zd (actual) != %zu (expected)", test_sbuf_len, sizeof(test_buffer)); ATF_CHECK_MSG(sbuf_bcpy(sb, test_buffer, sizeof(test_buffer)) == 0, "sbuf_bcpy failed"); test_sbuf_len = sbuf_len(sb); ATF_REQUIRE_MSG(test_sbuf_len == (ssize_t)sizeof(test_buffer), "sbuf_len(..) => %zd (actual) != %zu (expected)", test_sbuf_len, sizeof(test_buffer)); ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s", strerror(errno)); check_buffers_equal(sbuf_data(sb), test_buffer, (size_t)test_sbuf_len); sbuf_delete(sb); } ATF_TC_WITHOUT_HEAD(sbuf_cat_test); ATF_TC_BODY(sbuf_cat_test, tc) { struct sbuf *sb; char *test_string_tmp; ssize_t test_sbuf_len; asprintf(&test_string_tmp, "%s%s", test_string, test_string); ATF_REQUIRE_MSG(test_string_tmp != NULL, "asprintf failed"); sb = sbuf_new_auto(); ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s", strerror(errno)); ATF_CHECK_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed"); test_sbuf_len = sbuf_len(sb); ATF_REQUIRE_MSG(test_sbuf_len == (ssize_t)strlen(test_string), "sbuf_len(..) => %zd (actual) != %zu (expected)", test_sbuf_len, sizeof(test_string)); ATF_CHECK_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed"); test_sbuf_len = sbuf_len(sb); ATF_REQUIRE_MSG(test_sbuf_len == (ssize_t)strlen(test_string_tmp), "sbuf_len(..) => %zd (actual) != %zu (expected)", test_sbuf_len, strlen(test_string_tmp)); ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s", strerror(errno)); ATF_REQUIRE_STREQ_MSG(sbuf_data(sb), test_string_tmp, "sbuf (\"%s\") != test string (\"%s\")", sbuf_data(sb), test_string_tmp); sbuf_delete(sb); free(test_string_tmp); } ATF_TC_WITHOUT_HEAD(sbuf_cpy_test); ATF_TC_BODY(sbuf_cpy_test, tc) { struct sbuf *sb; ssize_t test_sbuf_len; sb = sbuf_new_auto(); ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s", strerror(errno)); ATF_CHECK_MSG(sbuf_cpy(sb, test_string) == 0, "sbuf_cpy failed"); test_sbuf_len = sbuf_len(sb); ATF_REQUIRE_MSG(test_sbuf_len == (ssize_t)strlen(test_string), "sbuf_len(..) => %zd (actual) != %zu (expected)", test_sbuf_len, strlen(test_string)); ATF_CHECK_MSG(sbuf_cpy(sb, test_string) == 0, "sbuf_cpy failed"); test_sbuf_len = sbuf_len(sb); ATF_REQUIRE_MSG(test_sbuf_len == (ssize_t)strlen(test_string), "sbuf_len(..) => %zd (actual) != %zu (expected)", test_sbuf_len, strlen(test_string)); ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s", strerror(errno)); ATF_REQUIRE_STREQ_MSG(sbuf_data(sb), test_string, "sbuf (\"%s\") != test string (\"%s\")", sbuf_data(sb), test_string); sbuf_delete(sb); } ATF_TC_WITHOUT_HEAD(sbuf_putc_test); ATF_TC_BODY(sbuf_putc_test, tc) { struct sbuf *sb; ssize_t test_sbuf_len; size_t i; sb = sbuf_new_auto(); ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s", strerror(errno)); for (i = 0; i <= strlen(test_string); i++) { /* Include the NUL */ ATF_REQUIRE_MSG(sbuf_putc(sb, test_string[i]) == 0, "sbuf_putc failed"); /* The best we can do until sbuf_finish(3) is called. */ test_sbuf_len = sbuf_len(sb); ATF_REQUIRE_MSG((ssize_t)(i + 1) == test_sbuf_len, "sbuf_len(..) => %zd (actual) != %zu (expected)", test_sbuf_len, i + 1); } ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s", strerror(errno)); ATF_REQUIRE_STREQ_MSG(sbuf_data(sb), test_string, "sbuf (\"%s\") != test string (\"%s\")", sbuf_data(sb), test_string); sbuf_delete(sb); } ATF_TC_WITHOUT_HEAD(sbuf_trim_test); ATF_TC_BODY(sbuf_trim_test, tc) { struct sbuf *sb; ssize_t exp_sbuf_len, test_sbuf_len; sb = sbuf_new_auto(); ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s", strerror(errno)); ATF_CHECK_MSG(sbuf_cpy(sb, test_string) == 0, "sbuf_cpy failed"); ATF_CHECK_MSG(sbuf_cat(sb, test_whitespace_string) == 0, "sbuf_cat failed"); /* The best we can do until sbuf_finish(3) is called. */ exp_sbuf_len = (ssize_t)(strlen(test_string) + strlen(test_whitespace_string)); test_sbuf_len = sbuf_len(sb); ATF_REQUIRE_MSG(exp_sbuf_len == test_sbuf_len, "sbuf_len(..) => %zd (actual) != %zu (expected)", test_sbuf_len, exp_sbuf_len); ATF_REQUIRE_MSG(sbuf_trim(sb) == 0, "sbuf_trim failed"); ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s", strerror(errno)); ATF_REQUIRE_STREQ_MSG(sbuf_data(sb), test_string, "sbuf (\"%s\") != test string (\"%s\") (trimmed)", sbuf_data(sb), test_string); sbuf_delete(sb); } ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, sbuf_bcat_test); ATF_TP_ADD_TC(tp, sbuf_bcpy_test); ATF_TP_ADD_TC(tp, sbuf_cat_test); ATF_TP_ADD_TC(tp, sbuf_cpy_test); ATF_TP_ADD_TC(tp, sbuf_putc_test); ATF_TP_ADD_TC(tp, sbuf_trim_test); return (atf_no_error()); } Index: head/lib/libsbuf/tests/sbuf_test_common.h =================================================================== --- head/lib/libsbuf/tests/sbuf_test_common.h (revision 346571) +++ head/lib/libsbuf/tests/sbuf_test_common.h (revision 346572) @@ -1,40 +1,39 @@ /*- - * Copyright (c) 2017 Ngie Cooper - * All rights reserved. + * Copyright (c) 2017 Enji Cooper * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef __LIBSBUF_TEST_COMMON_H__ #define __LIBSBUF_TEST_COMMON_H__ #include #if __FreeBSD_version > 1100064 #define HAVE_SBUF_GET_FLAGS #define HAVE_SBUF_CLEAR_FLAGS #define HAVE_SBUF_SET_FLAGS #endif #endif