FSGetErrorText.c revision 1bedbe3f
1/*
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24/*
25  Simple test case for FSGetErrorText.
26  When run with arguments, prints messages for the given codes.
27  When run with no arguments, prints messages for known valid error codes
28  and some invalid codes.
29*/
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <errno.h>
34#include <X11/fonts/FSlib.h>
35
36static int
37CheckErrorMessage(FSServer *srv, int code, const char *codestr)
38{
39    char buf[128] = "";
40
41    if (!FSGetErrorText(srv, code, buf, sizeof(buf))) {
42	fprintf(stderr, "FSGetErrorText(srv, %s (%d), buf, %d) failed",
43		codestr, code, (int) sizeof(buf));
44	return 0;
45    }
46
47    printf("FSGetErrorText for code %s (%d) returned:\n|%s|\n\n",
48	   codestr, code, buf);
49    return 1;
50}
51
52#define CheckNamedErrorMessage(s, e) \
53    CheckErrorMessage(s, e, #e)
54
55int main(int argc, char **argv)
56{
57    FSServer *srv = FSOpenServer(NULL);
58
59    if (srv == NULL) {
60	fprintf(stderr, "Failed to open fontserver connection to: %s\n"
61	"Aborting test. Make sure FONTSERVER is set to a valid xfs server.\n",
62		FSServerName(NULL));
63	exit(1);
64    }
65
66    if (argc > 1) {
67	int i;
68
69	for (i = 1; i < argc; i++) {
70	    int c;
71	    errno = 0;
72	    c = strtol(argv[i], NULL, 0);
73	    if (errno != 0) {
74		perror(argv[i]);
75		exit(1);
76	    }
77	    CheckErrorMessage (srv, c, "");
78	}
79	exit (0);
80    }
81
82    /* Default list to check if no arguments specified */
83    CheckNamedErrorMessage(srv, FSSuccess);
84    CheckNamedErrorMessage(srv, FSBadRequest);
85    CheckNamedErrorMessage(srv, FSBadFormat);
86    CheckNamedErrorMessage(srv, FSBadFont);
87    CheckNamedErrorMessage(srv, FSBadRange);
88    CheckNamedErrorMessage(srv, FSBadEventMask);
89    CheckNamedErrorMessage(srv, FSBadAccessContext);
90    CheckNamedErrorMessage(srv, FSBadIDChoice);
91    CheckNamedErrorMessage(srv, FSBadName);
92    CheckNamedErrorMessage(srv, FSBadResolution);
93    CheckNamedErrorMessage(srv, FSBadAlloc);
94    CheckNamedErrorMessage(srv, FSBadLength);
95    CheckNamedErrorMessage(srv, FSBadImplementation);
96    CheckErrorMessage (srv, 12, "<out of range value>");
97    CheckErrorMessage (srv, 256, "<out of range value>");
98    CheckErrorMessage (srv, 0xffff, "<out of range value>");
99
100    exit(0);
101}
102