summaryrefslogtreecommitdiff
path: root/test/usb_sm_checks.c
blob: 498d56e32323b6c515223d72297595243955c103 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* Copyright 2019 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Test USB Type-C VPD and CTVPD module.
 */
#include "common.h"
#include "task.h"
#include "test_util.h"
#include "timer.h"
#include "usb_pd.h"
#include "usb_sm.h"
#include "usb_tc_sm.h"
#include "util.h"
#include "usb_pd_test_util.h"
#include "vpd_api.h"

#ifdef CONFIG_USB_TYPEC_SM
extern const struct test_sm_data test_tc_sm_data[];
extern const int test_tc_sm_data_size;
#else
const struct test_sm_data test_tc_sm_data[] = {};
const int test_tc_sm_data_size;
#endif

#ifdef CONFIG_USB_PRL_SM
extern const struct test_sm_data test_prl_sm_data[];
extern const int test_prl_sm_data_size;
#else
const struct test_sm_data test_prl_sm_data[] = {};
const int test_prl_sm_data_size;
#endif

#ifdef CONFIG_USB_PE_SM
extern const struct test_sm_data test_pe_sm_data[];
extern const int test_pe_sm_data_size;
#else
const struct test_sm_data test_pe_sm_data[] = {};
const int test_pe_sm_data_size;
#endif

test_static int test_no_parent_cycles(const struct test_sm_data * const sm_data)
{
	int i;

	for (i = 0; i < sm_data->size; ++i) {
		int depth = 0;
		usb_state_ptr current = &sm_data->base[i];

		while (current != NULL && ++depth <= sm_data->size)
			current = current->parent;

		if (depth > sm_data->size)
			break;
	}

	/* Ensure all states end, otherwise the ith state has a cycle. */
	TEST_EQ(i, sm_data->size, "%d");

	return EC_SUCCESS;
}

int test_tc_no_parent_cycles(void)
{
	int i;

	for (i = 0; i < test_tc_sm_data_size; ++i) {
		const int rv = test_no_parent_cycles(&test_tc_sm_data[i]);

		if (rv) {
			ccprintf("TC State machine %d has a cycle!\n", i);
			TEST_ASSERT(0);
		}
	}

	return EC_SUCCESS;
}

int test_prl_no_parent_cycles(void)
{
	int i;

	for (i = 0; i < test_prl_sm_data_size; ++i) {
		const int rv = test_no_parent_cycles(&test_prl_sm_data[i]);

		if (rv) {
			ccprintf("PRL State machine %d has a cycle!\n", i);
			TEST_ASSERT(0);
		}
	}

	return EC_SUCCESS;
}

int test_pe_no_parent_cycles(void)
{
	int i;

	for (i = 0; i < test_pe_sm_data_size; ++i) {
		const int rv = test_no_parent_cycles(&test_pe_sm_data[i]);

		if (rv) {
			ccprintf("PE State machine %d has a cycle!\n", i);
			TEST_ASSERT(0);
		}
	}

	return EC_SUCCESS;
}

static int test_no_empty_state(const struct test_sm_data * const sm_data)
{
	int i;
	const struct usb_state empty = { 0 };

	for (i = 0; i < sm_data->size; ++i) {
		if (memcmp(&sm_data->base[i], &empty, sizeof(empty)) == 0)
			break;
	}

	/* Ensure all states had data, otherwise the ith state is empty. */
	TEST_EQ(i, sm_data->size, "%d");

	return EC_SUCCESS;
}

int test_tc_no_empty_state(void)
{
	int i;

	for (i = 0; i < test_tc_sm_data_size; ++i) {
		const int rv = test_no_empty_state(&test_tc_sm_data[i]);

		if (rv) {
			ccprintf("TC State machine %d has empty state!\n", i);
			TEST_ASSERT(0);
		}
	}

	return EC_SUCCESS;
}

int test_prl_no_empty_state(void)
{
	int i;

	for (i = 0; i < test_prl_sm_data_size; ++i) {
		const int rv = test_no_empty_state(&test_prl_sm_data[i]);

		if (rv) {
			ccprintf("PRL State machine %d has empty state!\n", i);
			TEST_ASSERT(0);
		}
	}

	return EC_SUCCESS;
}

int test_pe_no_empty_state(void)
{
	int i;

	for (i = 0; i < test_pe_sm_data_size; ++i) {
		const int rv = test_no_empty_state(&test_pe_sm_data[i]);

		if (rv) {
			ccprintf("PE State machine %d has empty state!\n", i);
			TEST_ASSERT(0);
		}
	}

	return EC_SUCCESS;
}

static volatile int state_printed;

/* Override the implement version of print */
__override void print_current_state(const int port)
{
	state_printed = 1;
}

static int test_all_states_named(const struct test_sm_data * const sm_data)
{
	int i;

	for (i = 0; i < sm_data->size; ++i) {
		usb_state_ptr current = &sm_data->base[i];

		state_printed = 0;

		if (current->entry)
			current->entry(0);

		if (state_printed) {
			if (i >= sm_data->names_size ||
			    sm_data->names[i] == NULL) {
				ccprintf("State %d does not have a name!\n", i);
				TEST_ASSERT(0);
			}
		}
	}

	return EC_SUCCESS;
}

int test_tc_all_states_named(void)
{
	int i;

	for (i = 0; i < test_tc_sm_data_size; ++i) {
		const int rv = test_all_states_named(&test_tc_sm_data[i]);

		if (rv) {
			ccprintf("TC State machine %d has empty name!\n", i);
			TEST_ASSERT(0);
		}
	}

	return EC_SUCCESS;
}

int test_prl_all_states_named(void)
{
	int i;

	for (i = 0; i < test_prl_sm_data_size; ++i) {
		const int rv = test_all_states_named(&test_prl_sm_data[i]);

		if (rv) {
			ccprintf("PRL State machine %d has empty name!\n", i);
			TEST_ASSERT(0);
		}
	}

	return EC_SUCCESS;
}

int test_pe_all_states_named(void)
{
	int i;

	for (i = 0; i < test_pe_sm_data_size; ++i) {
		const int rv = test_all_states_named(&test_pe_sm_data[i]);

		if (rv) {
			ccprintf("PE State machine %d has empty name!\n", i);
			TEST_ASSERT(0);
		}
	}

	return EC_SUCCESS;
}