summaryrefslogtreecommitdiff
path: root/test-common.c
blob: adc3cd4458eed080b10f18c6fbbbb33f97edf81d (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
/****************************************************************************
 * Common test functions for ethtool
 * Copyright 2011 Solarflare Communications Inc.
 *
 * Partly derived from kernel <linux/list.h>.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, incorporated herein by reference.
 */

#include <assert.h>
#include <errno.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/fcntl.h>
#include <unistd.h>
#define TEST_NO_WRAPPERS
#include "internal.h"

/* List utilities */

struct list_head {
	struct list_head *next, *prev;
};

#define LIST_HEAD_INIT(name) { &(name), &(name) }

static void init_list_head(struct list_head *list)
{
	list->next = list;
	list->prev = list;
}

static void list_add(struct list_head *new, struct list_head *head)
{
	head->next->prev = new;
	new->next = head->next;
	new->prev = head;
	head->next = new;
}

static void list_del(struct list_head *entry)
{
	entry->next->prev = entry->prev;
	entry->prev->next = entry->next;
	entry->next = NULL;
	entry->prev = NULL;
}

#define list_for_each_safe(pos, n, head) \
	for (pos = (head)->next, n = pos->next; pos != (head); \
		pos = n, n = pos->next)

/* Free memory at end of test */

static struct list_head malloc_list = LIST_HEAD_INIT(malloc_list);

void *test_malloc(size_t size)
{
	struct list_head *block = malloc(sizeof(*block) + size);

	if (!block)
		return NULL;
	list_add(block, &malloc_list);
	return block + 1;
}

void *test_calloc(size_t nmemb, size_t size)
{
	void *ptr = test_malloc(nmemb * size);

	if (ptr)
		memset(ptr, 0, nmemb * size);
	return ptr;
}

char *test_strdup(const char *s)
{
	size_t size = strlen(s) + 1;
	char *dup = test_malloc(size);

	if (dup)
		memcpy(dup, s, size);
	return dup;
}

void test_free(void *ptr)
{
	struct list_head *block;

	if (!ptr)
		return;
	block = (struct list_head *)ptr - 1;
	list_del(block);
	free(block);
}

void *test_realloc(void *ptr, size_t size)
{
	struct list_head *block;

	if (ptr) {
		block = (struct list_head *)ptr - 1;
		list_del(block);
	}
	block = realloc(block, sizeof(*block) + size);
	if (!block)
		return NULL;
	list_add(block, &malloc_list);
	return block + 1;
}

static void test_free_all(void)
{
	struct list_head *block, *next;

	list_for_each_safe(block, next, &malloc_list)
		free(block);
	init_list_head(&malloc_list);
}

/* Close files at end of test */

struct file_node {
	struct list_head link;
	FILE *fh;
	int fd;
};

static struct list_head file_list = LIST_HEAD_INIT(file_list);

int test_open(const char *pathname, int flag, ...)
{
	struct file_node *node;
	mode_t mode;

	if (flag & O_CREAT) {
		va_list ap;
		va_start(ap, flag);
		mode = va_arg(ap, mode_t);
		va_end(ap);
	} else {
		mode = 0;
	}

	node = malloc(sizeof(*node));
	if (!node)
		return -1;

	node->fd = open(pathname, flag, mode);
	if (node->fd < 0) {
		free(node);
		return -1;
	}

	node->fh = NULL;
	list_add(&node->link, &file_list);
	return node->fd;
}

int test_socket(int domain, int type, int protocol)
{
	struct file_node *node;

	node = malloc(sizeof(*node));
	if (!node)
		return -1;

	node->fd = socket(domain, type, protocol);
	if (node->fd < 0) {
		free(node);
		return -1;
	}

	node->fh = NULL;
	list_add(&node->link, &file_list);
	return node->fd;
}

int test_close(int fd)
{
	struct list_head *head, *next;

	if (fd >= 0) {
		list_for_each_safe(head, next, &file_list) {
			if (((struct file_node *)head)->fd == fd) {
				list_del(head);
				free(head);
				break;
			}
		}
	}

	return close(fd);
}

FILE *test_fopen(const char *path, const char *mode)
{
	struct file_node *node;

	node = malloc(sizeof(*node));
	if (!node)
		return NULL;

	node->fh = fopen(path, mode);
	if (!node->fh) {
		free(node);
		return NULL;
	}

	node->fd = -1;
	list_add(&node->link, &file_list);
	return node->fh;	
}

int test_fclose(FILE *fh)
{
	struct list_head *head, *next;

	assert(fh);

	list_for_each_safe(head, next, &file_list) {
		if (((struct file_node *)head)->fh == fh) {
			list_del(head);
			free(head);
			break;
		}
	}

	return fclose(fh);
}

static void test_close_all(void)
{
	struct list_head *head, *next;
	struct file_node *node;

	list_for_each_safe(head, next, &file_list) {
		node = (struct file_node *)head;
		if (node->fh)
			fclose(node->fh);
		else
			close(node->fd);
		free(node);
	}
	init_list_head(&file_list);
}

/* Wrap test main function */

static jmp_buf test_return;
static FILE *orig_stderr;

void test_exit(int rc)
{
	longjmp(test_return, rc + 1);
}

int test_ioctl(const struct cmd_expect *expect, void *cmd)
{
	int rc;

	if (!expect->cmd || *(u32 *)cmd != *(const u32 *)expect->cmd) {
		/* We have no idea how long this command structure is */
		fprintf(orig_stderr, "Unexpected ioctl: cmd=%#10x\n",
			*(u32 *)cmd);
		return TEST_IOCTL_MISMATCH;
	}

	if (memcmp(cmd, expect->cmd, expect->cmd_len)) {
		fprintf(orig_stderr, "Expected ioctl structure:\n");
		dump_hex(orig_stderr, expect->cmd, expect->cmd_len, 0);
		fprintf(orig_stderr, "Actual ioctl structure:\n");
		dump_hex(orig_stderr, cmd, expect->cmd_len, 0);
		return TEST_IOCTL_MISMATCH;
	}

	if (expect->resp)
		memcpy(cmd, expect->resp, expect->resp_len);
	rc = expect->rc;

	/* Convert kernel return code according to libc convention */
	if (rc >= 0) {
		return rc;
	} else {
		errno = -rc;
		return -1;
	}
}

int test_cmdline(const char *args)
{
	int argc, i;
	char **argv;
	const char *arg;
	size_t len;
	int dev_null = -1, orig_stdout_fd = -1, orig_stderr_fd = -1;
	int rc;

	/* Convert line to argv */
	argc = 1;
	arg = args;
	for (;;) {
		len = strcspn(arg, " ");
		if (len == 0)
			break;
		argc++;
		if (arg[len] == 0)
			break;
		arg += len + 1;
	}
	argv = test_calloc(argc + 1, sizeof(argv[0]));
	argv[0] = test_strdup("ethtool");
	arg = args;
	for (i = 1; i < argc; i++) {
		len = strcspn(arg, " ");
		argv[i] = test_malloc(len + 1);
		memcpy(argv[i], arg, len);
		argv[i][len] = 0;
		arg += len + 1;
	}

	dev_null = open("/dev/null", O_RDWR);
	if (dev_null < 0) {
		perror("open /dev/null");
		rc = -1;
		goto out;
	}

	fflush(NULL);
	dup2(dev_null, STDIN_FILENO);
	if (getenv("TEST_TEST_VERBOSE")) {
		orig_stderr = stderr;
	} else {
		orig_stdout_fd = dup(STDOUT_FILENO);
		if (orig_stdout_fd < 0) {
			perror("dup stdout");
			rc = -1;
			goto out;
		}
		dup2(dev_null, STDOUT_FILENO);
		orig_stderr_fd = dup(STDERR_FILENO);
		if (orig_stderr_fd < 0) {
			perror("dup stderr");
			rc = -1;
			goto out;
		}
		orig_stderr = fdopen(orig_stderr_fd, "w");
		if (orig_stderr == NULL) {
			perror("fdopen orig_stderr_fd");
			rc = -1;
			goto out;
		}
		dup2(dev_null, STDERR_FILENO);
	}

	rc = setjmp(test_return);
	rc = rc ? rc - 1 : test_main(argc, argv);

out:
	fflush(NULL);
	if (orig_stderr_fd >= 0) {
		dup2(orig_stderr_fd, STDERR_FILENO);
		if (orig_stderr)
			fclose(orig_stderr);
		else
			close(orig_stderr_fd);
	}
	orig_stderr = NULL;
	if (orig_stdout_fd >= 0) {
		dup2(orig_stdout_fd, STDOUT_FILENO);
		close(orig_stdout_fd);
	}
	if (dev_null >= 0)
		close(dev_null);

	test_free_all();
	test_close_all();
	return rc;
}