1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2020 Oxide Computer Company
14  */
15 
16 /*
17  * Basic ksensor functionality test
18  */
19 
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <stdlib.h>
24 #include <err.h>
25 #include <sys/sensors.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <sys/sysmacros.h>
30 
31 typedef struct sensor_test {
32 	const char *st_path;
33 	uint64_t st_kind;
34 	uint32_t st_unit;
35 	int32_t st_gran;
36 	uint32_t st_prec;
37 	int64_t st_val;
38 } sensor_test_t;
39 
40 /*
41  * These values come from the dummy sensors in the ksensor_test driver.
42  */
43 static sensor_test_t ksensor_basic_tests[] = {
44 	{ "/dev/sensors/test/test.temp.0.1", SENSOR_KIND_TEMPERATURE,
45 	    SENSOR_UNIT_CELSIUS, 4, -2, 23 },
46 	{ "/dev/sensors/test/test.volt.0.1", SENSOR_KIND_VOLTAGE,
47 	    SENSOR_UNIT_VOLTS, 1000, 0, 3300 },
48 	{ "/dev/sensors/test/test.current.0.1", SENSOR_KIND_CURRENT,
49 	    SENSOR_UNIT_AMPS, 10, 0, 5 },
50 };
51 
52 static boolean_t
53 ksensor_basic(sensor_test_t *st)
54 {
55 	sensor_ioctl_kind_t kind;
56 	sensor_ioctl_scalar_t scalar;
57 	int fd;
58 
59 	fd = open(st->st_path, O_RDONLY);
60 	if (fd < 0) {
61 		warn("TEST FAILED: failed to open %s", st->st_path);
62 		return (B_FALSE);
63 	}
64 
65 	arc4random_buf(&kind, sizeof (kind));
66 	arc4random_buf(&scalar, sizeof (scalar));
67 
68 	if (ioctl(fd, SENSOR_IOCTL_KIND, &kind) != 0) {
69 		warn("TEST FAILED: %s: failed to get sensor kind", st->st_path);
70 		goto err;
71 	}
72 
73 	if (kind.sik_kind != st->st_kind) {
74 		warnx("TEST FAILED: %s: expected kind %" PRIu64 ", found kind %"
75 		    PRIu64, st->st_path, st->st_kind, kind);
76 		goto err;
77 	}
78 
79 	if (ioctl(fd, SENSOR_IOCTL_SCALAR, &scalar) != 0) {
80 		warn("TEST FAILED: %s: failed to read sensor", st->st_path);
81 		goto err;
82 	}
83 
84 	if (scalar.sis_unit != st->st_unit) {
85 		warnx("TEST FAILED: %s: expected unit %" PRIu32 ", but found "
86 		    "%" PRIu32, st->st_path, st->st_unit, scalar.sis_unit);
87 		goto err;
88 	}
89 
90 	if (scalar.sis_gran != st->st_gran) {
91 		warnx("TEST FAILED: %s: expected gran %" PRId32 ", but found "
92 		    "%" PRId32, st->st_path, st->st_gran, scalar.sis_gran);
93 		goto err;
94 	}
95 
96 	if (scalar.sis_prec != st->st_prec) {
97 		warnx("TEST FAILED: %s: expected prec %" PRIu32 ", but found "
98 		    "%" PRIu32, st->st_path, st->st_prec, scalar.sis_prec);
99 		goto err;
100 	}
101 
102 	if (scalar.sis_value != st->st_val) {
103 		warnx("TEST FAILED: %s: expected value %" PRId64 ", but found "
104 		    "%" PRId64, st->st_path, st->st_val, scalar.sis_value);
105 		goto err;
106 	}
107 
108 	return (B_TRUE);
109 err:
110 	(void) close(fd);
111 	return (B_FALSE);
112 }
113 
114 int
115 main(void)
116 {
117 	size_t i;
118 	int ret = EXIT_SUCCESS;
119 
120 	for (i = 0; i < ARRAY_SIZE(ksensor_basic_tests); i++) {
121 		if (!ksensor_basic(&ksensor_basic_tests[i])) {
122 			ret = EXIT_FAILURE;
123 		}
124 	}
125 
126 	return (ret);
127 }
128