File: igt_ktap.c

package info (click to toggle)
intel-gpu-tools 2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 62,024 kB
  • sloc: xml: 769,439; ansic: 348,692; python: 8,307; yacc: 2,781; perl: 1,196; sh: 1,178; lex: 487; asm: 227; makefile: 27; lisp: 11
file content (317 lines) | stat: -rw-r--r-- 9,168 bytes parent folder | download
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// SPDX-License-Identifier: MIT
/*
 * Copyright © 2023 Isabella Basso do Amaral <isabbasso@riseup.net>
 * Copyright © 2023 Intel Corporation
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "igt_core.h"
#include "igt_ktap.h"
#include "igt_list.h"

enum ktap_phase {
	KTAP_START,
	SUITE_COUNT,
	SUITE_START,
	SUITE_NAME,
	CASE_COUNT,
	CASE_NAME,
	SUB_RESULT,
	CASE_RESULT,
	SUITE_RESULT,
};

struct igt_ktap_results {
	enum ktap_phase expect;
	unsigned int suite_count;
	unsigned int suite_last;
	char *suite_name;
	unsigned int case_count;
	unsigned int case_last;
	char *case_name;
	unsigned int sub_last;
	struct igt_list_head *results;
};

/**
 * igt_ktap_parse:
 *
 * This function parses a line of text for KTAP report data
 * and passes results back to IGT kunit layer.
 * https://kernel.org/doc/html/latest/dev-tools/ktap.html
 */
int igt_ktap_parse(const char *buf, struct igt_ktap_results *ktap)
{
	char *suite_name = NULL, *case_name = NULL, *msg = NULL;
	struct igt_ktap_result *result;
	int code = IGT_EXIT_INVALID;
	unsigned int n, len;
	char s[2];

	/* KTAP report header */
	if (igt_debug_on(sscanf(buf, "KTAP%*[ ]version%*[ ]%u %n",
				&n, &len) == 1 && len == strlen(buf))) {
		if (igt_debug_on(ktap->expect != KTAP_START))
			return -EPROTO;

		ktap->suite_count = 0;
		ktap->expect = SUITE_COUNT;

	/* malformed TAP test plan? */
	} else if (len = 0,
		   igt_debug_on(sscanf(buf, " 1..%1[ ]", s) == 1)) {
		return -EINPROGRESS;

	/* valid test plan of a KTAP report */
	} else if (igt_debug_on(sscanf(buf, "1..%u %n", &n, &len) == 1 &&
				len == strlen(buf))) {
		if (igt_debug_on(ktap->expect != SUITE_COUNT))
			return -EPROTO;

		if (!n)
			return 0;

		ktap->suite_count = n;
		ktap->suite_last = 0;
		ktap->suite_name = NULL;
		ktap->expect = SUITE_START;

	/* KTAP test suite header */
	} else if (len = 0,
		   igt_debug_on(sscanf(buf,
				       "%*1[ ]%*1[ ]%*1[ ]%*1[ ]KTAP%*[ ]version%*[ ]%u %n",
				       &n, &len) == 1 && len == strlen(buf))) {
		/*
		 * TODO: drop the following workaround, which addresses a kernel
		 * side issue of missing lines that provide top level KTAP
		 * version and test suite plan, as soon as no longer needed.
		 *
		 * The issue has been fixed in v6.6-rc1, commit c95e7c05c139
		 * ("kunit: Report the count of test suites in a module"),
		 * but we still need this workaround for as long as LTS kernel
		 * version 6.1, with DRM selftests already converted to Kunit,
		 * but without that missing Kunit headers issue fixed, is used
		 * by major Linux distributions.
		 */
		if (ktap->expect == KTAP_START) {
			ktap->suite_count = 1;
			ktap->suite_last = 0;
			ktap->suite_name = NULL;
			ktap->expect = SUITE_START;
		}

		if (igt_debug_on(ktap->expect != SUITE_START))
			return -EPROTO;

		ktap->expect = SUITE_NAME;

	/* KTAP test suite name */
	} else if (len = 0,
		   igt_debug_on(sscanf(buf,
				       "%*1[ ]%*1[ ]%*1[ ]%*1[ ]#%*[ ]Subtest:%*[ ]%ms %n",
				       &suite_name, &len) == 1 && len == strlen(buf))) {
		if (igt_debug_on(ktap->expect != SUITE_NAME))
			return -EPROTO;

		ktap->suite_name = suite_name;
		suite_name = NULL;
		ktap->case_count = 0;
		ktap->expect = CASE_COUNT;

	/* valid test plan of a KTAP test suite */
	} else if (len = 0, free(suite_name), suite_name = NULL,
		   igt_debug_on(sscanf(buf,
				       "%*1[ ]%*1[ ]%*1[ ]%*1[ ]1..%u %n",
				       &n, &len) == 1 && len == strlen(buf))) {
		if (igt_debug_on(ktap->expect != CASE_COUNT))
			return -EPROTO;

		if (n) {
			ktap->case_count = n;
			ktap->case_last = 0;
			ktap->case_name = NULL;
			ktap->expect = CASE_RESULT;
		} else {
			ktap->expect = SUITE_RESULT;
		}

	/* KTAP parametrized test case header */
	} else if (len = 0,
		   igt_debug_on(sscanf(buf,
				       "%*1[ ]%*1[ ]%*1[ ]%*1[ ]%*1[ ]%*1[ ]%*1[ ]%*1[ ]KTAP%*[ ]version%*[ ]%u %n",
				       &n, &len) == 1 && len == strlen(buf))) {
		if (igt_debug_on(ktap->expect != CASE_RESULT))
			return -EPROTO;

		ktap->sub_last = 0;
		ktap->expect = CASE_NAME;

	/* KTAP parametrized test case name */
	} else if (len = 0,
		   igt_debug_on(sscanf(buf,
				       "%*1[ ]%*1[ ]%*1[ ]%*1[ ]%*1[ ]%*1[ ]%*1[ ]%*1[ ]#%*[ ]Subtest:%*[ ]%ms %n",
				       &case_name, &len) == 1 && len == strlen(buf))) {
		if (igt_debug_on(ktap->expect != CASE_NAME))
			return -EPROTO;

		n = ktap->case_last + 1;
		ktap->expect = SUB_RESULT;

	/* KTAP parametrized subtest result */
	} else if (len = 0, free(case_name), case_name = NULL,
		   igt_debug_on(sscanf(buf,
				       "%*1[ ]%*1[ ]%*1[ ]%*1[ ]%*1[ ]%*1[ ]%*1[ ]%*1[ ]ok%*[ ]%u%*[ ]%*[^#\n]%1[#\n]",
				       &n, s) == 2) ||
		   igt_debug_on(sscanf(buf,
				       "%*1[ ]%*1[ ]%*1[ ]%*1[ ]%*1[ ]%*1[ ]%*1[ ]%*1[ ]not%*1[ ]ok%*[ ]%u%*[ ]%*[^#\n]%1[#\n]",
				       &n, s) == 2)) {
		/* at lease one result of a parametrised subtest expected */
		if (igt_debug_on(ktap->expect == SUB_RESULT &&
				 ktap->sub_last == 0))
			ktap->expect = CASE_RESULT;

		if (igt_debug_on(ktap->expect != CASE_RESULT) ||
		    igt_debug_on(n != ++ktap->sub_last))
			return -EPROTO;

	/* KTAP test case skip result */
	} else if ((igt_debug_on(sscanf(buf,
					"%*1[ ]%*1[ ]%*1[ ]%*1[ ]ok%*[ ]%u%*[ ]%ms%*[ ]#%*[ ]SKIP %n",
					&n, &case_name, &len) == 2 &&
				 len == strlen(buf))) ||
		   (len = 0, free(case_name), case_name = NULL,
		    igt_debug_on(sscanf(buf,
					"%*1[ ]%*1[ ]%*1[ ]%*1[ ]ok%*[ ]%u%*[ ]%ms%*[ ]#%*[ ]SKIP%*[ ]%m[^\n]",
					&n, &case_name, &msg) == 3))) {
		code = IGT_EXIT_SKIP;

	/* KTAP test case pass result */
	} else if ((free(case_name), case_name = NULL, free(msg), msg = NULL,
		    igt_debug_on(sscanf(buf,
					"%*1[ ]%*1[ ]%*1[ ]%*1[ ]ok%*[ ]%u%*[ ]%ms %n",
					&n, &case_name, &len) == 2 &&
				 len == strlen(buf))) ||
		   (len = 0, free(case_name), case_name = NULL,
		    igt_debug_on(sscanf(buf,
					"%*1[ ]%*1[ ]%*1[ ]%*1[ ]ok%*[ ]%u%*[ ]%ms%*[ ]#%*[ ]%m[^\n]",
					&n, &case_name, &msg) == 3))) {
		code = IGT_EXIT_SUCCESS;

	/* KTAP test case fail result */
	} else if ((free(case_name), case_name = NULL, free(msg), msg = NULL,
		    igt_debug_on(sscanf(buf,
					"%*1[ ]%*1[ ]%*1[ ]%*1[ ]not%*1[ ]ok%*[ ]%u%*[ ]%ms %n",
					&n, &case_name, &len) == 2 &&
				 len == strlen(buf))) ||
		   (len = 0, free(case_name), case_name = NULL,
		    igt_debug_on(sscanf(buf,
					"%*1[ ]%*1[ ]%*1[ ]%*1[ ]not%*1[ ]ok%*[ ]%u%*[ ]%ms%*[ ]#%*[ ]%m[^\n]",
					&n, &case_name, &msg) == 3))) {
		code = IGT_EXIT_FAILURE;

	/* KTAP test suite result */
	} else if ((free(case_name), free(msg),
		    igt_debug_on(sscanf(buf, "ok%*[ ]%u%*[ ]%ms %n",
					&n, &suite_name, &len) == 2 &&
				 len == strlen(buf))) ||
		   (len = 0, free(suite_name), suite_name = NULL,
		    igt_debug_on(sscanf(buf, "ok%*[ ]%u%*[ ]%ms%*[ ]%1[#]",
					&n, &suite_name, s) == 3)) ||
		   (free(suite_name), suite_name = NULL,
		    igt_debug_on(sscanf(buf,
					"not%*[ ]ok%*[ ]%u%*[ ]%ms %n",
					&n, &suite_name, &len) == 2 &&
				 len == strlen(buf))) ||
		   (len = 0, free(suite_name), suite_name = NULL,
		    igt_debug_on(sscanf(buf,
					"not%*[ ]ok%*[ ]%u%*[ ]%ms%*[ ]%1[#]",
					&n, &suite_name, s) == 3))) {
		if (igt_debug_on(ktap->expect != SUITE_RESULT) ||
		    igt_debug_on(strcmp(suite_name, ktap->suite_name)) ||
		    igt_debug_on(n != ++ktap->suite_last) ||
		    igt_debug_on(n > ktap->suite_count)) {
			free(suite_name);
			return -EPROTO;
		}
		free(suite_name);

		/* last test suite? */
		if (igt_debug_on(n == ktap->suite_count))
			return 0;

		ktap->suite_name = NULL;
		ktap->expect = SUITE_START;

	} else {
		return -EINPROGRESS;
	}

	/* neither a test case name nor result */
	if (ktap->expect != SUB_RESULT && code == IGT_EXIT_INVALID)
		return -EINPROGRESS;

	if (igt_debug_on(ktap->expect == SUB_RESULT &&
			 code != IGT_EXIT_INVALID) ||
	    igt_debug_on(code != IGT_EXIT_INVALID &&
			 ktap->expect != CASE_RESULT) ||
	    igt_debug_on(!ktap->suite_name) || igt_debug_on(!case_name) ||
	    igt_debug_on(ktap->expect == CASE_RESULT && ktap->case_name &&
			 strcmp(case_name, ktap->case_name)) ||
	    igt_debug_on(n > ktap->case_count) ||
	    igt_debug_on(n != (ktap->expect == SUB_RESULT ?
			       ktap->case_last + 1: ++ktap->case_last))) {
		free(case_name);
		free(msg);
		return -EPROTO;
	}

	if (ktap->expect == SUB_RESULT) {
		/* KTAP parametrized test case name */
		ktap->case_name = case_name;

	} else {
		/* KTAP test case result */
		ktap->case_name = NULL;

		/* last test case in a suite */
		if (n == ktap->case_count)
			ktap->expect = SUITE_RESULT;
	}

	if (igt_debug_on((result = calloc(1, sizeof(*result)), !result))) {
		free(case_name);
		free(msg);
		return -ENOMEM;
	}

	result->suite_name = ktap->suite_name;
	result->case_name = case_name;
	result->code = code;
	result->msg = msg;
	igt_list_add_tail(&result->link, ktap->results);

	return -EINPROGRESS;
}

struct igt_ktap_results *igt_ktap_alloc(struct igt_list_head *results)
{
	struct igt_ktap_results *ktap = calloc(1, sizeof(*ktap));

	if (!ktap)
		return NULL;

	ktap->expect = KTAP_START;
	ktap->results = results;

	return ktap;
}

void igt_ktap_free(struct igt_ktap_results **ktap)
{
	free(*ktap);
	*ktap = NULL;
}