4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * diagcode library unit test
30 * usually run from "make test" target. takes a single argument
31 * which is the directory where the test dictionaries are found.
32 * this test driver scans the dictionaries for comments of the form:
33 * #TEST:<routine>:<errno>:<input>:<output>
34 * and executes that test.
36 * exit 0 and an "All tests passed" message means no failures. otherwise
37 * error messages are spewed as appropriate and exit value is non-zero.
40 #pragma ident "%Z%%M% %I% %E% SMI"
48 #include <sys/types.h>
52 #include <fm/diagcode.h>
63 static fm_dc_handle_t
*Dhp
;
67 err(const char *fmt
, ...)
72 (void) fprintf(stderr
, "%s: %s:%d ", Myname
, Dict
, Line
);
73 (void) vfprintf(stderr
, fmt
, ap
);
74 (void) fprintf(stderr
, "\n");
78 /* parse an expected errno value from test line (numeric or some symbolic) */
80 geterrno(const char *s
)
82 if (*s
== '\0' || isspace(*s
))
86 else if (strcmp(s
, "EPERM") == 0)
88 else if (strcmp(s
, "ENOENT") == 0)
90 else if (strcmp(s
, "ESRCH") == 0)
92 else if (strcmp(s
, "ENOMEM") == 0)
94 else if (strcmp(s
, "EACCES") == 0)
96 else if (strcmp(s
, "EINVAL") == 0)
98 else if (strcmp(s
, "ERANGE") == 0)
100 else if (strcmp(s
, "ENOMSG") == 0)
102 else if (strcmp(s
, "ENOTSUP") == 0)
105 err("geterrno: don't know errno \"%s\"", s
);
111 /* call fm_dc_opendict() as part of a test */
113 do_open(const char *dirpath
, const char *dictname
, char *argv
[], int argc
)
122 experrno
= geterrno(argv
[1]);
124 if ((Dhp
= fm_dc_opendict(FM_DC_VERSION
, dirpath
, dictname
)) == NULL
)
129 if (reterrno
!= experrno
)
130 err("opendict errno %d, expected %d", reterrno
, experrno
);
133 /* call fm_dc_closedict() as part of a test */
135 do_close(const char *dirpath
, const char *dictname
, char *argv
[], int argc
)
138 fm_dc_closedict(Dhp
);
143 /* call fm_dc_codelen() as part of a test */
145 do_codelen(const char *dirpath
, const char *dictname
, char *argv
[], int argc
)
154 expcodelen
= geterrno(argv
[2]);
157 err("codelen NULL handle");
161 retcodelen
= fm_dc_codelen(Dhp
);
163 if (retcodelen
!= expcodelen
)
164 err("codelen %d, expected %d", retcodelen
, expcodelen
);
167 /* call fm_dc_maxkey() as part of a test */
169 do_maxkey(const char *dirpath
, const char *dictname
, char *argv
[], int argc
)
178 expmaxkey
= geterrno(argv
[2]);
181 err("maxkey NULL handle");
185 retmaxkey
= fm_dc_maxkey(Dhp
);
187 if (retmaxkey
!= expmaxkey
)
188 err("maxkey %d, expected %d", retmaxkey
, expmaxkey
);
191 /* call fm_dc_key2code() as part of a test */
193 do_key2code(const char *dirpath
, const char *dictname
, char *argv
[], int argc
)
197 const char *key
[MAXKEY
];
211 experrno
= geterrno(argv
[1]);
213 /* convert key into array */
216 while (nel
< MAXKEY
- 1) {
218 if ((endp
= strchr(beginp
, ' ')) != NULL
) {
227 err("key2code NULL handle");
231 if (fm_dc_key2code(Dhp
, key
, code
, MAXCODE
) < 0)
236 if (reterrno
!= experrno
) {
237 err("key2code errno %d, expected %d", reterrno
, experrno
);
241 if (reterrno
== 0 && argc
> 3 && strcmp(code
, argv
[3]))
242 err("code \"%s\", expected \"%s\"", code
, argv
[3]);
245 /* call fm_dc_code2key() as part of a test */
247 do_code2key(const char *dirpath
, const char *dictname
, char *argv
[], int argc
)
251 char keystr
[MAXLINE
];
263 experrno
= geterrno(argv
[1]);
266 err("code2key NULL handle");
270 if (fm_dc_code2key(Dhp
, argv
[2], key
, fm_dc_maxkey(Dhp
)) < 0)
275 if (reterrno
!= experrno
) {
276 err("errno %d, expected %d", reterrno
, experrno
);
284 /* convert key into string */
286 for (nel
= 0; key
[nel
]; nel
++) {
288 (void) strcat(keystr
, " ");
289 (void) strcat(keystr
, key
[nel
]);
292 if (strcmp(keystr
, argv
[3]))
293 err("key \"%s\", expected \"%s\"", keystr
, argv
[3]);
295 for (nel
= 0; key
[nel
]; nel
++)
299 /* call fm_dc_getprop() as part of a test */
301 do_getprop(const char *dirpath
, const char *dictname
, char *argv
[], int argc
)
311 experrno
= geterrno(argv
[1]);
314 err("getprop NULL handle");
318 if ((val
= fm_dc_getprop(Dhp
, argv
[2])) == NULL
)
323 if (reterrno
!= experrno
) {
324 err("getprop errno %d, expected %d", reterrno
, experrno
);
328 if (reterrno
== 0 && strcmp(val
, argv
[3]))
329 err("val \"%s\", expected \"%s\"", val
, argv
[3]);
332 /* scan a dictionary, looking for test directives embedded in the comments */
334 testdict(const char *dirpath
, const char *dictname
)
336 char linebuf
[MAXLINE
];
340 (void) snprintf(fname
, MAXLINE
, "%s/%s.dict", dirpath
, dictname
);
342 if ((fp
= fopen(fname
, "r")) == NULL
) {
351 while (fgets(linebuf
, MAXLINE
, fp
) != NULL
) {
358 if (strncmp(linebuf
, "#TEST:", 6))
361 if ((endp
= strchr(linebuf
, '\n')) != NULL
)
364 beginp
= &linebuf
[6];
365 while (argc
< MAXARG
- 1) {
366 argv
[argc
++] = beginp
;
367 if ((endp
= strchr(beginp
, ':')) != NULL
) {
375 if (strcmp(argv
[0], "open") == 0)
376 do_open(dirpath
, dictname
, argv
, argc
);
377 else if (strcmp(argv
[0], "close") == 0)
378 do_close(dirpath
, dictname
, argv
, argc
);
379 else if (strcmp(argv
[0], "codelen") == 0)
380 do_codelen(dirpath
, dictname
, argv
, argc
);
381 else if (strcmp(argv
[0], "maxkey") == 0)
382 do_maxkey(dirpath
, dictname
, argv
, argc
);
383 else if (strcmp(argv
[0], "key2code") == 0)
384 do_key2code(dirpath
, dictname
, argv
, argc
);
385 else if (strcmp(argv
[0], "code2key") == 0)
386 do_code2key(dirpath
, dictname
, argv
, argc
);
387 else if (strcmp(argv
[0], "getprop") == 0)
388 do_getprop(dirpath
, dictname
, argv
, argc
);
390 err("unknown TEST command: \"%s\"", argv
[0]);
398 fm_dc_closedict(Dhp
);
403 /* scan a directory, looking for dictionaries to test against */
405 main(int argc
, char *argv
[])
410 if ((Myname
= strrchr(argv
[0], '/')) == NULL
)
416 (void) fprintf(stderr
, "usage: %s test-directory\n", argv
[0]);
420 if ((dirp
= opendir(argv
[1])) == NULL
) {
425 while ((dp
= readdir(dirp
)) != NULL
) {
428 if (dp
->d_name
[0] == '.')
431 if ((ptr
= strrchr(dp
->d_name
, '.')) == NULL
||
432 strcmp(ptr
, ".dict"))
435 *ptr
= '\0'; /* remove the extension */
436 testdict(argv
[1], dp
->d_name
);
438 (void) closedir(dirp
);
441 (void) printf("%s: All tests passed.\n", Myname
);