test.c revision 150305
1/*-
2 * Copyright (c) 2005 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/tools/regression/geom_gpt/test.c 150305 2005-09-19 02:59:09Z marcel $");
29
30#include <errno.h>
31#include <libgeom.h>
32#include <limits.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <unistd.h>
37
38int verbose;
39
40static void
41usage()
42{
43	fprintf(stdout, "usage: %s [-v] param ...\n", getprogname());
44	exit(1);
45}
46
47static int
48parse(char *arg, char **param, char **value)
49{
50	char *e;
51
52	*param = arg;
53	e = strchr(arg, '=');
54	if (e != NULL) {
55		*e = '\0';
56		*value = e + 1;
57	} else
58		*value = NULL;
59	return (0);
60}
61
62static int
63add_int32(struct gctl_req *req, char *arg)
64{
65	char *param, *nptr, *endptr;
66	uint32_t value;
67	int error;
68
69	error = parse(arg, &param, &nptr);
70	if (error)
71		return (error);
72	if (nptr == NULL)
73		return (EINVAL);
74	value = strtol(nptr, &endptr, 0);
75	if (*endptr)
76		return (EINVAL);
77	gctl_ro_param(req, param, sizeof(value), &value);
78	return (0);
79}
80
81static int
82add_string(struct gctl_req *req, char *arg)
83{
84	char *param, *value;
85	int error;
86
87	error = parse(arg, &param, &value);
88	if (error)
89		return (error);
90	if (value == NULL)
91		return (EINVAL);
92
93	gctl_ro_param(req, param, -1, value);
94	return (0);
95}
96
97int main(int argc, char *argv[])
98{
99	struct gctl_req *req;
100	const char *s;
101	int c;
102
103	req = gctl_get_handle();
104	gctl_ro_param(req, "class", -1, "GPT");
105
106	while ((c = getopt(argc, argv, "4:s:v")) != -1) {
107		switch (c) {
108		case '4':	/* uint32_t */
109			add_int32(req, optarg);
110			break;
111		case 's':	/* string */
112			add_string(req, optarg);
113			break;
114		case 'v':
115			verbose = 1;
116			break;
117		case '?':
118		default:
119			usage();
120			/* NOTREACHED */
121			break;
122		}
123	}
124	if (argc != optind)
125		usage();
126
127	if (verbose)
128		gctl_dump(req, stdout);
129
130	s = gctl_issue(req);
131	if (s != NULL)
132		printf("FAIL %s\n", s);
133	else
134		printf("PASS\n");
135	gctl_free(req);
136	return (0);
137}
138