summaryrefslogtreecommitdiffstats
path: root/libblkid/libfdisk/src/test.c
blob: 31ed7e03ada1d0f1410259a7f717ee3740ec0f26 (plain) (blame)
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
/*
 * Copyright (C) 2008-2009 Karel Zak <kzak@redhat.com>
 *
 * This file may be redistributed under the terms of the
 * GNU Lesser General Public License.
 *
 * Routines for TEST_PROGRAMs
 */

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

#ifndef TEST_PROGRAM
#define TEST_PROGRAM
#endif

#include "fdiskP.h"

int fdisk_run_test(struct fdisk_test *tests, int argc, char *argv[])
{
	int rc = -1;
	struct fdisk_test *ts;

	assert(tests);
	assert(argc);
	assert(argv);

	if (argc < 2 ||
	    strcmp(argv[1], "--help") == 0 ||
	    strcmp(argv[1], "-h") == 0)
		goto usage;

	fdisk_init_debug(0);

	for (ts = tests; ts->name; ts++) {
		if (strcmp(ts->name, argv[1]) == 0) {
			rc = ts->body(ts, argc - 1, argv + 1);
			if (rc)
				printf("FAILED [rc=%d]", rc);
			break;
		}
	}

	if (rc < 0 && ts->name == NULL)
		goto usage;

	return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
usage:
	printf("\nUsage:\n\t%s <test> [testoptions]\nTests:\n",
			program_invocation_short_name);
	for (ts = tests; ts->name; ts++) {
		printf("\t%-15s", ts->name);
		if (ts->usage)
			printf(" %s\n", ts->usage);
	}
	printf("\n");
	return EXIT_FAILURE;
}