xref: /illumos-gate/usr/src/test/os-tests/tests/ksensor/ksensor_basic.c (revision 3ce5372277f4657ad0e52d36c979527c4ca22de2)
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 
30 static const char *ksensor_path = "/dev/sensors/test/test.temp.0.1";
31 
32 int
33 main(void)
34 {
35 	sensor_ioctl_kind_t kind;
36 	sensor_ioctl_temperature_t temp;
37 	int ret = 0;
38 
39 	int fd = open(ksensor_path, O_RDONLY);
40 	if (fd < 0) {
41 		err(EXIT_FAILURE, "TEST FAILED: failed to open %s",
42 		    ksensor_path);
43 	}
44 
45 	arc4random_buf(&kind, sizeof (kind));
46 	arc4random_buf(&temp, sizeof (temp));
47 
48 	if (ioctl(fd, SENSOR_IOCTL_TYPE, &kind) != 0) {
49 		warn("TEST FAILED: failed to get sensor type");
50 		ret = EXIT_FAILURE;
51 	}
52 
53 	if (kind.sik_kind != SENSOR_KIND_TEMPERATURE) {
54 		warnx("TEST FAILED: expected temperature sensor, found kind %d",
55 		    kind);
56 		ret = EXIT_FAILURE;
57 	}
58 
59 	if (ioctl(fd, SENSOR_IOCTL_TEMPERATURE, &temp) != 0) {
60 		warn("TEST FAILED: failed to get sensor temperature");
61 		ret = EXIT_FAILURE;
62 	}
63 
64 	/*
65 	 * These values come from the dummy temperature sensor in ksensor_test.
66 	 */
67 	if (temp.sit_unit != SENSOR_UNIT_CELSIUS) {
68 		warnx("TEST FAILED: expected temp unit %" PRIu32 ", but found "
69 		    "%" PRIu32, SENSOR_UNIT_CELSIUS, temp.sit_unit);
70 		ret = EXIT_FAILURE;
71 	}
72 
73 	if (temp.sit_gran != 4) {
74 		warnx("TEST FAILED: expected temp gran %" PRId32 ", but found "
75 		    "%" PRId32, 4, temp.sit_gran);
76 		ret = EXIT_FAILURE;
77 	}
78 
79 	if (temp.sit_prec != -2) {
80 		warnx("TEST FAILED: expected temp prec %" PRId32 ", but found "
81 		    "%" PRId32, -2, temp.sit_prec);
82 		ret = EXIT_FAILURE;
83 	}
84 
85 	if (temp.sit_temp != 23) {
86 		warnx("TEST FAILED: expected temp %" PRId64 ", but found "
87 		    "%" PRId64, 23, temp.sit_temp);
88 		ret = EXIT_FAILURE;
89 	}
90 
91 	return (ret);
92 }
93