summaryrefslogtreecommitdiff
path: root/tests/usbg-test.c
blob: 4f3cb8e25b76ce98646066b90ce31f3c8dbcc9dc (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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include <usbg/usbg.h>
#include <stdio.h>
#include <stdarg.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <limits.h>

#include "usbg-test.h"

static struct simple_stack{
	void *ptr;
	struct simple_stack *next;
} *cleanup_top = NULL;

void free_later(void *ptr)
{
	struct simple_stack *t;

	t = malloc(sizeof(*t));
	t->ptr = ptr;
	t->next = cleanup_top;
	cleanup_top = t;
}

void cleanup_stack()
{
	struct simple_stack *t;

	while (cleanup_top) {
		free(cleanup_top->ptr);
		t = cleanup_top->next;
		free(cleanup_top);
		cleanup_top = t;
	}
}


/* Represent last file/dir opened, next should have bigger numbers.*/
static int file_id = 0;
static int dir_id = 0;

#define PUSH_FILE(file, content) do {\
	file_id++;\
	expect_path(fopen, path, file);\
	will_return(fopen, file_id);\
	expect_value(fgets, stream, file_id);\
	will_return(fgets, content);\
	expect_value(fclose, fp, file_id);\
	will_return(fclose, 0);\
} while(0)

#define PUSH_FILE_ALWAYS(dflt) do {\
	expect_any_count(fopen, path, -1);\
	will_return_always(fopen, 1);\
	expect_any_count(fgets, stream, 1);\
	will_return_always(fgets, dflt);\
	expect_any_count(fclose, fp, 1);\
	will_return_always(fclose, 0);\
} while(0)

#define PUSH_EMPTY_DIR(p) do {\
	expect_string(scandir, dirp, p);\
	will_return(scandir, 0);\
} while(0)

#define EXPECT_OPENDIR(n) do {\
	dir_id++;\
	expect_path(opendir, name, n);\
	will_return(opendir, dir_id);\
	expect_value(closedir, dirp, dir_id);\
	will_return(closedir, 0);\
} while(0)

#define PUSH_DIR(p, c) do {\
	expect_path(scandir, dirp, p);\
	will_return(scandir, c);\
} while(0)

#define PUSH_DIR_ENTRY(name, type) do {\
	will_return(scandir, name);\
	will_return(scandir, type);\
	will_return(scandir, 1);\
} while(0)

#define PUSH_LINK(p, c, len) do {\
	expect_path(readlink, path, p);\
	expect_in_range(readlink, bufsiz, len, INT_MAX);\
	will_return(readlink, c);\
} while(0)

/**
 * @brief Compare test gadgets' names
 */
static int test_gadget_cmp(struct test_gadget *a, struct test_gadget *b)
{
	return strcoll(a->name, b->name);
}

/**
 * @brief Compare test functions' names
 */
static int test_function_cmp(struct test_function *a, struct test_function *b)
{
	return strcoll(a->name, b->name);
}

/**
 * @brief Compare test configs' names
 */
static int test_config_cmp(struct test_config *a, struct test_config *b)
{
	return strcoll(a->name, b->name);
}

void prepare_config(struct test_config *c, char *path)
{
	int tmp;
	int count = 0;
	struct test_function *b;

	tmp = asprintf(&c->name, "%s.%d",
			c->label, c->id);
	if (tmp < 0)
		fail();
	free_later(c->name);

	c->path = path;

	for (b = c->bindings; b->instance; b++)
		count++;

	qsort(c->bindings, count, sizeof(*c->bindings),
		(int (*)(const void *, const void *))test_function_cmp);

}

void prepare_function(struct test_function *f, char *path)
{
	const char *func_type;
	int tmp;

	func_type = usbg_get_function_type_str(f->type);
	if (func_type == NULL)
		fail();

	tmp = asprintf(&f->name, "%s.%s",
			func_type, f->instance);
	if (tmp < 0)
		fail();
	free_later(f->name);

	f->path = path;
}

void prepare_gadget(struct test_state *state, struct test_gadget *g)
{
	struct test_config *c;
	struct test_function *f;
	char *path;
	int tmp;
	int count;

	g->path = strdup(state->path);
	if (!g->path)
		fail();

	free_later(g->path);

	tmp = asprintf(&path, "%s/%s/functions",
			g->path, g->name);
	if (tmp < 0)
		fail();
	free_later(path);

	count = 0;
	for (f = g->functions; f->instance; f++) {
		prepare_function(f, path);
		count++;
	}

	/* Path needs to be known somehow when list is empty */
	f->path = path;

	qsort(g->functions, count, sizeof(*g->functions),
		(int (*)(const void *, const void *))test_function_cmp);

	tmp = asprintf(&path, "%s/%s/configs",
			g->path, g->name);
	if (tmp < 0)
		fail();
	free_later(path);

	count = 0;
	for (c = g->configs; c->label; c++) {
		prepare_config(c, path);
		count++;
	}

	/* Path needs to be known somehow when list is empty */
	c->path = path;

	qsort(g->configs, count, sizeof(*g->configs),
		(int (*)(const void *, const void *))test_config_cmp);

}

void prepare_state(struct test_state *state)
{
	struct test_gadget *g;
	int count = 0;
	int tmp;

	tmp = asprintf(&(state->path), "%s/usb_gadget", state->configfs_path);
	if (tmp < 0)
		fail();
	free_later(state->path);


	for (g = state->gadgets; g->name; g++) {
		prepare_gadget(state, g);
		count++;
	}

	qsort(state->gadgets, count, sizeof(*state->gadgets),
		(int (*)(const void *, const void *))test_gadget_cmp);

}

/* Simulation of configfs for init */

static void push_binding(struct test_config *conf, struct test_function *binding)
{
	int tmp;
	char *s_path;
	char *d_path;

	tmp = asprintf(&s_path, "%s/%s/%s", conf->path, conf->name, binding->name);
	if (tmp < 0)
		fail();
	free_later(s_path);

	tmp = asprintf(&d_path, "%s/%s", binding->path, binding->name);
	if (tmp < 0)
		fail();
	free_later(d_path);

	PUSH_LINK(s_path, d_path, USBG_MAX_PATH_LENGTH - 1);
}

static void push_config(struct test_config *c)
{
	struct test_function *b;
	int count = 0;
	int tmp;
	char *path;

	tmp = asprintf(&path, "%s/%s", c->path, c->name);
	if (tmp < 0)
		fail();
	free_later(path);

	for (b = c->bindings; b->instance; b++)
		count++;

	PUSH_DIR(path, count);
	for (b = c->bindings; b->instance; b++) {
		PUSH_DIR_ENTRY(b->name, DT_LNK);
		push_binding(c, b);
	}
}

static void push_gadget(struct test_gadget *g)
{
	int count;
	struct test_config *c;
	struct test_function *f;
	int tmp;
	char *path;

	tmp = asprintf(&path, "%s/%s/UDC", g->path, g->name);
	if (tmp < 0)
		fail();
	free_later(path);
	PUSH_FILE(path, g->udc);

	count = 0;
	for (f = g->functions; f->instance; f++)
		count++;

	PUSH_DIR(f->path, count);
	for (f = g->functions; f->instance; f++)
		PUSH_DIR_ENTRY(f->name, DT_DIR);

	count = 0;
	for (c = g->configs; c->label; c++)
		count++;

	PUSH_DIR(c->path, count);
	for (c = g->configs; c->label; c++)
		PUSH_DIR_ENTRY(c->name, DT_DIR);

	for (c = g->configs; c->label; c++)
		push_config(c);
}

void push_init(struct test_state *state)
{
	char **udc;
	struct test_gadget *g;
	int count = 0;

	EXPECT_OPENDIR(state->path);

	for (udc = state->udcs; *udc; udc++)
		count++;

	PUSH_DIR("/sys/class/udc", count);
	for (udc = state->udcs; *udc; udc++)
		PUSH_DIR_ENTRY(*udc, DT_REG);

	count = 0;
	for (g = state->gadgets; g->name; g++)
		count++;

	PUSH_DIR(state->path, count);
	for (g = state->gadgets; g->name; g++) {
		PUSH_DIR_ENTRY(g->name, DT_DIR);
	}

	for (g = state->gadgets; g->name; g++)
		push_gadget(g);
}

void init_with_state(struct test_state *in, usbg_state **out)
{
	int usbg_ret;

	push_init(in);
	usbg_ret = usbg_init(in->configfs_path, out);
	assert_int_equal(usbg_ret, USBG_SUCCESS);
}

void assert_func_equal(usbg_function *f, struct test_function *expected)
{
	assert_string_equal(f->instance, expected->instance);
	assert_int_equal(f->type, expected->type);
	assert_path_equal(f->path, expected->path);
}

void assert_config_equal(usbg_config *c, struct test_config *expected)
{
	int i = 0;
	usbg_binding *b;

	assert_int_equal(c->id, expected->id);
	assert_string_equal(c->label, expected->label);
	assert_path_equal(c->path, expected->path);
	usbg_for_each_binding(b, c)
		assert_func_equal(b->target, &expected->bindings[i++]);
}

void assert_gadget_equal(usbg_gadget *g, struct test_gadget *expected)
{
	usbg_config *c;
	usbg_function *f;
	int i;

	assert_string_equal(g->name, expected->name);
	assert_path_equal(g->path, expected->path);

	i = 0;
	usbg_for_each_function(f, g)
		assert_func_equal(f, &expected->functions[i++]);

	i = 0;
	usbg_for_each_config(c, g)
		assert_config_equal(c, &expected->configs[i++]);
}

void assert_state_equal(usbg_state *s, struct test_state *expected)
{
	usbg_gadget *g;
	int i = 0;

	assert_path_equal(s->path, expected->path);
	assert_path_equal(s->configfs_path, expected->configfs_path);

	usbg_for_each_gadget(g, s)
		assert_gadget_equal(g, &expected->gadgets[i++]);
}

#define SIGNUM(x) (((x) > 0) - ((x) < 0))

int path_cmp(const char *actual, const char *expected)
{
	const char *a = actual;
	const char *b = expected;

	while (*a != '\0' && *b != '\0') {
		if (*a != *b)
			break;
		do
			++a;
		while (*a == '/');

		do
			++b;
		while (*b == '/');
	}

	return SIGNUM(*a - *b);
}

int path_equal_display_error(const LargestIntegralType actual, const LargestIntegralType expected)
{
	if (path_cmp((const char *)actual, (const char *)expected) == 0) {
		return 1;
	}

	fprintf(stderr, "%s != %s\n", (const char *)actual, (const char *)expected);
	return 0;
}

void assert_path_equal(const char *actual, const char *expected)
{
	if (path_equal_display_error((const LargestIntegralType)actual, (const LargestIntegralType)expected) == 0)
		fail();
}