1315320Sngie/*- 2315320Sngie * Copyright (c) 2017 Ngie Cooper <ngie@freebsd.org> 3315320Sngie * All rights reserved. 4315320Sngie * 5315320Sngie * Redistribution and use in source and binary forms, with or without 6315320Sngie * modification, are permitted provided that the following conditions 7315320Sngie * are met: 8315320Sngie * 1. Redistributions of source code must retain the above copyright 9315320Sngie * notice, this list of conditions and the following disclaimer. 10315320Sngie * 2. Redistributions in binary form must reproduce the above copyright 11315320Sngie * notice, this list of conditions and the following disclaimer in the 12315320Sngie * documentation and/or other materials provided with the distribution. 13315320Sngie * 14315320Sngie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15315320Sngie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16315320Sngie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17315320Sngie * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18315320Sngie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19315320Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20315320Sngie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21315320Sngie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22315320Sngie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23315320Sngie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24315320Sngie * SUCH DAMAGE. 25315320Sngie */ 26315320Sngie 27321118Sngie#include <sys/cdefs.h> 28321118Sngie__FBSDID("$FreeBSD: stable/10/lib/libcam/tests/libcam_test.c 321122 2017-07-18 08:44:38Z ngie $"); 29321118Sngie 30315320Sngie#include <errno.h> 31315320Sngie#include <fcntl.h> 32315320Sngie#include <stdio.h> 33315320Sngie#include <camlib.h> 34315320Sngie 35315320Sngie#include <atf-c.h> 36315320Sngie 37315320Sngiestatic const char * 38315320Sngieget_cam_test_device(const atf_tc_t *tc) 39315320Sngie{ 40315320Sngie const char *cam_test_device; 41315320Sngie 42315320Sngie cam_test_device = atf_tc_get_config_var(tc, "cam_test_device"); 43315320Sngie 44315320Sngie return (cam_test_device); 45315320Sngie} 46315320Sngie 47315320Sngiestatic void 48315320Sngiecam_clear_error(void) 49315320Sngie{ 50315320Sngie 51315320Sngie strcpy(cam_errbuf, ""); 52315320Sngie} 53315320Sngie 54315320Sngiestatic bool 55315320Sngiecam_has_error(void) 56315320Sngie{ 57315320Sngie 58315320Sngie return (strlen(cam_errbuf) != 0); 59315320Sngie} 60315320Sngie 61321122SngieATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_NULL_path); 62321122SngieATF_TC_BODY(cam_get_device_negative_test_NULL_path, tc) 63321122Sngie{ 64321122Sngie char parsed_dev_name[DEV_IDLEN + 1]; 65321122Sngie int parsed_unit; 66321122Sngie 67321122Sngie ATF_REQUIRE_MSG(cam_get_device(NULL, parsed_dev_name, 68321122Sngie nitems(parsed_dev_name), &parsed_unit) == -1, 69321122Sngie "cam_get_device succeeded unexpectedly"); 70321122Sngie} 71321122Sngie 72321122SngieATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_bad_path); 73321122SngieATF_TC_BODY(cam_get_device_negative_test_bad_path, tc) 74321122Sngie{ 75321122Sngie char parsed_dev_name[DEV_IDLEN + 1]; 76321122Sngie int parsed_unit; 77321122Sngie 78321122Sngie ATF_REQUIRE_MSG(cam_get_device("1ada", parsed_dev_name, 79321122Sngie nitems(parsed_dev_name), &parsed_unit) == -1, 80321122Sngie "cam_get_device succeeded unexpectedly"); 81321122Sngie} 82321122Sngie 83321122SngieATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_nul_path); 84321122SngieATF_TC_BODY(cam_get_device_negative_test_nul_path, tc) 85321122Sngie{ 86321122Sngie char parsed_dev_name[DEV_IDLEN + 1]; 87321122Sngie int parsed_unit; 88321122Sngie 89321122Sngie ATF_REQUIRE_MSG(cam_get_device("", parsed_dev_name, 90321122Sngie nitems(parsed_dev_name), &parsed_unit) == -1, 91321122Sngie "cam_get_device succeeded unexpectedly"); 92321122Sngie} 93321122Sngie 94321122SngieATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_root); 95321122SngieATF_TC_BODY(cam_get_device_negative_test_root, tc) 96321122Sngie{ 97321122Sngie char parsed_dev_name[DEV_IDLEN + 1]; 98321122Sngie int parsed_unit; 99321122Sngie 100321122Sngie ATF_REQUIRE_MSG(cam_get_device("/", parsed_dev_name, 101321122Sngie nitems(parsed_dev_name), &parsed_unit) == -1, 102321122Sngie "cam_get_device succeeded unexpectedly"); 103321122Sngie} 104321122Sngie 105321122SngieATF_TC_WITHOUT_HEAD(cam_get_device_positive_test); 106321122SngieATF_TC_BODY(cam_get_device_positive_test, tc) 107321122Sngie{ 108321122Sngie char expected_dev_name[] = "foo"; 109321122Sngie char parsed_dev_name[DEV_IDLEN + 1]; 110321122Sngie int expected_unit, parsed_unit; 111321122Sngie 112321122Sngie expected_unit = 1; 113321122Sngie 114321122Sngie ATF_REQUIRE_MSG(cam_get_device("/dev/foo1", parsed_dev_name, 115321122Sngie nitems(parsed_dev_name), &parsed_unit) == 0, 116321122Sngie "cam_get_device failed"); 117321122Sngie ATF_REQUIRE_STREQ(parsed_dev_name, expected_dev_name); 118321122Sngie ATF_REQUIRE(parsed_unit == expected_unit); 119321122Sngie 120321122Sngie strcpy(parsed_dev_name, ""); 121321122Sngie parsed_unit = -1; 122321122Sngie 123321122Sngie ATF_REQUIRE_MSG(cam_get_device("foo1", parsed_dev_name, 124321122Sngie nitems(parsed_dev_name), &parsed_unit) == 0, 125321122Sngie "cam_get_device failed"); 126321122Sngie ATF_REQUIRE_STREQ(parsed_dev_name, expected_dev_name); 127321122Sngie ATF_REQUIRE(parsed_unit == expected_unit); 128321122Sngie} 129321122Sngie 130315320SngieATF_TC(cam_open_device_negative_test_O_RDONLY); 131315320SngieATF_TC_HEAD(cam_open_device_negative_test_O_RDONLY, tc) 132315320Sngie{ 133315320Sngie 134315320Sngie atf_tc_set_md_var(tc, "descr", 135315320Sngie "test that cam_open_device(`cam_device`, O_RDONLY) fails to open " 136315320Sngie "the underlying pass(4) device (bug 217649)"); 137315320Sngie atf_tc_set_md_var(tc, "require.config", "cam_test_device"); 138315320Sngie atf_tc_set_md_var(tc, "require.user", "root"); 139315320Sngie} 140315320Sngie 141315320SngieATF_TC_BODY(cam_open_device_negative_test_O_RDONLY, tc) 142315320Sngie{ 143315320Sngie const char *cam_test_device; 144315320Sngie 145315320Sngie cam_test_device = get_cam_test_device(tc); 146315320Sngie 147315320Sngie cam_clear_error(); 148315320Sngie ATF_CHECK(cam_open_device(cam_test_device, O_RDONLY) == NULL); 149315320Sngie ATF_REQUIRE(cam_has_error()); 150315320Sngie} 151315320Sngie 152315320SngieATF_TC(cam_open_device_negative_test_nonexistent); 153315320SngieATF_TC_HEAD(cam_open_device_negative_test_nonexistent, tc) 154315320Sngie{ 155315320Sngie 156315320Sngie atf_tc_set_md_var(tc, "require.user", "root"); 157315320Sngie} 158315320Sngie 159315320SngieATF_TC_BODY(cam_open_device_negative_test_nonexistent, tc) 160315320Sngie{ 161315320Sngie 162315320Sngie cam_clear_error(); 163315320Sngie ATF_REQUIRE(cam_open_device("/nonexistent", O_RDWR) == NULL); 164315320Sngie ATF_REQUIRE(cam_has_error()); 165315320Sngie} 166315320Sngie 167315320SngieATF_TC(cam_open_device_negative_test_unprivileged); 168315320SngieATF_TC_HEAD(cam_open_device_negative_test_unprivileged, tc) 169315320Sngie{ 170315320Sngie 171315320Sngie atf_tc_set_md_var(tc, "require.config", "cam_test_device"); 172315320Sngie atf_tc_set_md_var(tc, "require.user", "unprivileged"); 173315320Sngie} 174315320Sngie 175315320SngieATF_TC_BODY(cam_open_device_negative_test_unprivileged, tc) 176315320Sngie{ 177315320Sngie const char *cam_test_device; 178315320Sngie 179315320Sngie cam_test_device = get_cam_test_device(tc); 180315320Sngie 181315320Sngie cam_clear_error(); 182315320Sngie ATF_CHECK(cam_open_device(cam_test_device, O_RDONLY) == NULL); 183315320Sngie ATF_REQUIRE(cam_has_error()); 184315320Sngie 185315320Sngie cam_clear_error(); 186315320Sngie ATF_CHECK(cam_open_device(cam_test_device, O_RDWR) == NULL); 187315320Sngie ATF_REQUIRE(cam_has_error()); 188315320Sngie} 189315320Sngie 190315320SngieATF_TC(cam_open_device_positive_test); 191315320SngieATF_TC_HEAD(cam_open_device_positive_test, tc) 192315320Sngie{ 193315320Sngie 194315320Sngie atf_tc_set_md_var(tc, "require.config", "cam_test_device"); 195315320Sngie atf_tc_set_md_var(tc, "require.user", "root"); 196315320Sngie} 197315320Sngie 198315320SngieATF_TC_BODY(cam_open_device_positive_test, tc) 199315320Sngie{ 200315320Sngie struct cam_device *cam_dev; 201315320Sngie const char *cam_test_device; 202315320Sngie 203315320Sngie cam_test_device = get_cam_test_device(tc); 204315320Sngie 205315320Sngie cam_clear_error(); 206315320Sngie cam_dev = cam_open_device(cam_test_device, O_RDWR); 207315320Sngie ATF_CHECK_MSG(cam_dev != NULL, "cam_open_device failed: %s", 208315320Sngie cam_errbuf); 209315320Sngie ATF_REQUIRE(!cam_has_error()); 210315320Sngie cam_close_device(cam_dev); 211315320Sngie} 212315320Sngie 213315320SngieATF_TC(cam_close_device_negative_test_NULL); 214315320SngieATF_TC_HEAD(cam_close_device_negative_test_NULL, tc) 215315320Sngie{ 216315320Sngie 217315320Sngie atf_tc_set_md_var(tc, "descr", 218315320Sngie "test that cam_close_device(NULL) succeeds without error"); 219315320Sngie atf_tc_set_md_var(tc, "require.user", "root"); 220315320Sngie} 221315320Sngie 222315320SngieATF_TC_BODY(cam_close_device_negative_test_NULL, tc) 223315320Sngie{ 224315320Sngie 225315320Sngie cam_clear_error(); 226315320Sngie cam_close_device(NULL); 227315320Sngie ATF_REQUIRE(!cam_has_error()); 228315320Sngie} 229315320Sngie 230315320SngieATF_TC(cam_getccb_positive_test); 231315320SngieATF_TC_HEAD(cam_getccb_positive_test, tc) 232315320Sngie{ 233315320Sngie 234315320Sngie atf_tc_set_md_var(tc, "require.config", "cam_test_device"); 235315320Sngie atf_tc_set_md_var(tc, "require.user", "root"); 236315320Sngie} 237315320Sngie 238315320SngieATF_TC_BODY(cam_getccb_positive_test, tc) 239315320Sngie{ 240315320Sngie union ccb *cam_ccb; 241315320Sngie struct cam_device *cam_dev; 242315320Sngie const char *cam_test_device; 243315320Sngie 244315320Sngie cam_test_device = get_cam_test_device(tc); 245315320Sngie 246315320Sngie cam_clear_error(); 247315320Sngie cam_dev = cam_open_device(cam_test_device, O_RDWR); 248315320Sngie ATF_CHECK_MSG(cam_dev != NULL, "cam_open_device failed: %s", 249315320Sngie cam_errbuf); 250315320Sngie ATF_REQUIRE(!cam_has_error()); 251315320Sngie cam_ccb = cam_getccb(cam_dev); 252315320Sngie ATF_CHECK_MSG(cam_ccb != NULL, "get_camccb failed: %s", cam_errbuf); 253315320Sngie ATF_REQUIRE(!cam_has_error()); 254315320Sngie cam_freeccb(cam_ccb); 255315320Sngie cam_close_device(cam_dev); 256315320Sngie} 257315320Sngie 258315320SngieATF_TC(cam_freeccb_negative_test_NULL); 259315320SngieATF_TC_HEAD(cam_freeccb_negative_test_NULL, tc) 260315320Sngie{ 261315320Sngie 262315320Sngie atf_tc_set_md_var(tc, "descr", 263315320Sngie "test that cam_freeccb(NULL) succeeds without error"); 264315320Sngie atf_tc_set_md_var(tc, "require.user", "root"); 265315320Sngie} 266315320Sngie 267315320SngieATF_TC_BODY(cam_freeccb_negative_test_NULL, tc) 268315320Sngie{ 269315320Sngie 270315320Sngie cam_clear_error(); 271315320Sngie cam_freeccb(NULL); 272315320Sngie ATF_REQUIRE(!cam_has_error()); 273315320Sngie} 274315320Sngie 275315320SngieATF_TP_ADD_TCS(tp) 276315320Sngie{ 277315320Sngie 278321122Sngie ATF_TP_ADD_TC(tp, cam_get_device_negative_test_NULL_path); 279321122Sngie ATF_TP_ADD_TC(tp, cam_get_device_negative_test_bad_path); 280321122Sngie ATF_TP_ADD_TC(tp, cam_get_device_negative_test_nul_path); 281321122Sngie ATF_TP_ADD_TC(tp, cam_get_device_negative_test_root); 282321122Sngie ATF_TP_ADD_TC(tp, cam_get_device_positive_test); 283315320Sngie ATF_TP_ADD_TC(tp, cam_open_device_negative_test_O_RDONLY); 284315320Sngie ATF_TP_ADD_TC(tp, cam_open_device_negative_test_nonexistent); 285315320Sngie ATF_TP_ADD_TC(tp, cam_open_device_negative_test_unprivileged); 286315320Sngie ATF_TP_ADD_TC(tp, cam_open_device_positive_test); 287315320Sngie ATF_TP_ADD_TC(tp, cam_close_device_negative_test_NULL); 288315320Sngie ATF_TP_ADD_TC(tp, cam_getccb_positive_test); 289315320Sngie ATF_TP_ADD_TC(tp, cam_freeccb_negative_test_NULL); 290315320Sngie 291315320Sngie return (atf_no_error()); 292315320Sngie} 293