1 /*
2  * Copyright (c) 2019 Alexander Wachter
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <zephyr/drivers/hwinfo.h>
7 #include <zephyr/ztest.h>
8 #include <strings.h>
9 #include <errno.h>
10 
11 /*
12  * @addtogroup t_hwinfo_get_device_id_api
13  * @{
14  * @defgroup t_hwinfo_get_device_id test_hwinfo_get_device_id
15  * @brief TestPurpose: verify device id get works
16  * @details
17  * - Test Steps
18  *   -# Read the ID
19  *   -# Check if to many bytes are written to the buffer
20  *   -# Check if UID is plausible
21  * - Expected Results
22  *   -# Device uid with correct length should be written to the buffer.
23  * @}
24  */
25 
26 #define BUFFER_LENGTH 17
27 #define BUFFER_CANARY 0xFF
28 
ZTEST(hwinfo_device_id_api,test_device_id_get)29 ZTEST(hwinfo_device_id_api, test_device_id_get)
30 {
31 	uint8_t buffer_1[BUFFER_LENGTH];
32 	uint8_t buffer_2[BUFFER_LENGTH];
33 	ssize_t length_read_1, length_read_2;
34 	int i;
35 
36 	length_read_1 = hwinfo_get_device_id(buffer_1, 1);
37 	if (length_read_1 == -ENOSYS) {
38 		ztest_test_skip();
39 		return;
40 	}
41 
42 	zassert_false((length_read_1 < 0),
43 		      "Unexpected negative return value: %d", length_read_1);
44 	zassert_not_equal(length_read_1, 0, "Zero bytes read");
45 	zassert_equal(length_read_1, 1, "Length not adhered");
46 
47 	memset(buffer_1, BUFFER_CANARY, sizeof(buffer_1));
48 
49 	length_read_1 = hwinfo_get_device_id(buffer_1, BUFFER_LENGTH - 1);
50 	zassert_equal(buffer_1[length_read_1], BUFFER_CANARY,
51 		      "Too many bytes are written");
52 
53 	memcpy(buffer_2, buffer_1, length_read_1);
54 
55 	for (i = 0; i < BUFFER_LENGTH; i++) {
56 		buffer_1[i] ^= 0xA5;
57 	}
58 
59 	length_read_2 = hwinfo_get_device_id(buffer_1, BUFFER_LENGTH - 1);
60 	zassert_equal(length_read_1, length_read_2, "Length varied");
61 
62 	zassert_equal(buffer_1[length_read_1], (BUFFER_CANARY ^ 0xA5),
63 		      "Too many bytes are written");
64 
65 	for (i = 0; i < length_read_1; i++) {
66 		zassert_equal(buffer_1[i], buffer_2[i],
67 			      "Two consecutively readings don't match");
68 	}
69 }
70 
71 /*
72  * @addtogroup t_hwinfo_get_reset_cause_api
73  * @{
74  * @defgroup t_hwinfo_get_reset_cause test_hwinfo_get_reset_cause
75  * @brief TestPurpose: verify get reset cause works.
76  * @details
77  * - Test Steps
78  *   -# Set target buffer to a known value
79  *   -# Read the reset cause
80  *   -# Check if target buffer has been altered
81  * - Expected Results
82  *   -# Target buffer contents should be changed after the call.
83  * @}
84  */
85 
ZTEST(hwinfo_device_id_api,test_get_reset_cause)86 ZTEST(hwinfo_device_id_api, test_get_reset_cause)
87 {
88 	uint32_t cause;
89 	ssize_t ret;
90 
91 	/* Set `cause` to a known value prior to call. */
92 	cause = 0xDEADBEEF;
93 
94 	ret = hwinfo_get_reset_cause(&cause);
95 	if (ret == -ENOSYS) {
96 		ztest_test_skip();
97 		return;
98 	}
99 
100 	zassert_false((ret < 0),
101 		      "Unexpected negative return value: %d", ret);
102 
103 	/* Verify that `cause` has been changed. */
104 	zassert_not_equal(cause, 0xDEADBEEF, "Reset cause not written.");
105 }
106 
107 /*
108  * @addtogroup t_hwinfo_clear_reset_cause_api
109  * @{
110  * @defgroup t_hwinfo_clear_reset_cause test_hwinfo_clear_reset_cause
111  * @brief TestPurpose: verify clear reset cause works. This may
112  *        not work on some platforms, depending on how reset cause register
113  *        works on that SoC.
114  * @details
115  * - Test Steps
116  *   -# Read the reset cause and store the result
117  *   -# Call clear reset cause
118  *   -# Read the reset cause again
119  *   -# Check if the two readings match
120  * - Expected Results
121  *   -# Reset cause value should change after calling clear reset cause.
122  * @}
123  */
ZTEST(hwinfo_device_id_api,test_clear_reset_cause)124 ZTEST(hwinfo_device_id_api, test_clear_reset_cause)
125 {
126 	uint32_t cause_1, cause_2;
127 	ssize_t ret;
128 
129 	ret = hwinfo_get_reset_cause(&cause_1);
130 	if (ret == -ENOSYS) {
131 		ztest_test_skip();
132 		return;
133 	}
134 
135 	zassert_false((ret < 0),
136 		      "Unexpected negative return value: %d", ret);
137 
138 	ret = hwinfo_clear_reset_cause();
139 	if (ret == -ENOSYS) {
140 		ztest_test_skip();
141 		return;
142 	}
143 
144 	zassert_false((ret < 0),
145 		      "Unexpected negative return value: %d", ret);
146 
147 	ret = hwinfo_get_reset_cause(&cause_2);
148 	if (ret == -ENOSYS) {
149 		ztest_test_skip();
150 		return;
151 	}
152 
153 	zassert_false((ret < 0),
154 		      "Unexpected negative return value: %d", ret);
155 
156 	/* Verify that `cause` has been changed. */
157 	zassert_not_equal(cause_1, cause_2,
158 		"Reset cause did not change after clearing");
159 }
160 
161 /*
162  * @addtogroup t_hwinfo_get_reset_cause_api
163  * @{
164  * @defgroup t_hwinfo_get_reset_cause test_hwinfo_get_supported_reset_cause
165  * @brief TestPurpose: verify get supported reset cause works.
166  * @details
167  * - Test Steps
168  *   -# Set target buffer to a known value
169  *   -# Read the reset cause
170  *   -# Check if target buffer has been altered
171  * - Expected Results
172  *   -# Target buffer contents should be changed after the call.
173  * @}
174  */
ZTEST(hwinfo_device_id_api,test_get_supported_reset_cause)175 ZTEST(hwinfo_device_id_api, test_get_supported_reset_cause)
176 {
177 	uint32_t supported;
178 	ssize_t ret;
179 
180 	/* Set `supported` to a known value prior to call. */
181 	supported = 0xDEADBEEF;
182 
183 	ret = hwinfo_get_supported_reset_cause(&supported);
184 	if (ret == -ENOSYS) {
185 		ztest_test_skip();
186 		return;
187 	}
188 
189 	zassert_false((ret < 0),
190 		      "Unexpected negative return value: %d", ret);
191 
192 	/* Verify that `supported` has been changed. */
193 	zassert_not_equal(supported, 0xDEADBEEF,
194 					"Supported reset cause not written.");
195 }
196 
197 
198 ZTEST_SUITE(hwinfo_device_id_api, NULL, NULL, NULL, NULL, NULL);
199