font-test-utils.c revision 60da515c
1/* Common utility code for interacting with libXfont from test utilities 2 * 3 * Note that this code is designed for test programs, and thus uses assert() 4 * and fatal err() calls in places that real code would do error handling, 5 * since the goal is to catch bugs faster, not help users get past problems. 6 */ 7 8/* 9 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. 10 * 11 * Permission is hereby granted, free of charge, to any person obtaining a 12 * copy of this software and associated documentation files (the "Software"), 13 * to deal in the Software without restriction, including without limitation 14 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 15 * and/or sell copies of the Software, and to permit persons to whom the 16 * Software is furnished to do so, subject to the following conditions: 17 * 18 * The above copyright notice and this permission notice (including the next 19 * paragraph) shall be included in all copies or substantial portions of the 20 * Software. 21 * 22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 28 * DEALINGS IN THE SOFTWARE. 29 */ 30 31/* Based on code from xorg-server/dix/dixfont.c covered by this notice: 32 33Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts. 34 35 All Rights Reserved 36 37Permission to use, copy, modify, and distribute this software and its 38documentation for any purpose and without fee is hereby granted, 39provided that the above copyright notice appear in all copies and that 40both that copyright notice and this permission notice appear in 41supporting documentation, and that the name of Digital not be 42used in advertising or publicity pertaining to distribution of the 43software without specific, written prior permission. 44 45DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 46ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL 47DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR 48ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 49WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 50ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 51SOFTWARE. 52 53 */ 54 55#include "font-test-utils.h" 56#include "src/util/replace.h" 57#include <stdio.h> 58#include <stdlib.h> 59#include <limits.h> 60#include <assert.h> 61#ifdef HAVE_ERR_H 62#include <err.h> 63#endif 64#include "src/util/replace.h" 65#include <X11/X.h> 66 67static unsigned long server_generation; 68static xfont2_fpe_funcs_rec const **fpe_functions; 69static int num_fpe_types; 70 71static int 72test_client_auth_generation(ClientPtr client) 73{ 74 err(BadImplementation, "%s called but not yet implemented", __func__); 75} 76 77static Bool 78test_client_signal(ClientPtr client) 79{ 80 err(BadImplementation, "%s called but not yet implemented", __func__); 81} 82 83static void 84test_delete_font_client_id(Font id) 85{ 86 err(BadImplementation, "%s called but not yet implemented", __func__); 87} 88 89static void _X_ATTRIBUTE_PRINTF(1,0) 90test_verrorf(const char *f, va_list ap) 91{ 92 vwarn(f, ap); 93} 94 95static FontPtr 96test_find_old_font(FSID id) 97{ 98 err(BadImplementation, "%s called but not yet implemented", __func__); 99} 100 101static FontResolutionPtr 102test_get_client_resolutions(int *num) 103{ 104 *num = 0; 105 return NULL; 106} 107 108static int 109test_get_default_point_size(void) 110{ 111 return 120; 112} 113 114static Font 115test_get_new_font_client_id(void) 116{ 117 err(BadImplementation, "%s called but not yet implemented", __func__); 118} 119 120static uint32_t 121test_get_time_in_millis(void) 122{ 123 err(BadImplementation, "%s called but not yet implemented", __func__); 124} 125 126static int 127test_init_fs_handlers(FontPathElementPtr fpe, 128 FontBlockHandlerProcPtr block_handler) 129{ 130 err(BadImplementation, "%s called but not yet implemented", __func__); 131} 132 133/* Callback from libXfont when each fpe handler is registered */ 134static int 135test_register_fpe_funcs(const xfont2_fpe_funcs_rec *funcs) 136{ 137 xfont2_fpe_funcs_rec const **new; 138 139 /* grow the list */ 140 new = reallocarray(fpe_functions, (num_fpe_types + 1), 141 sizeof(xfont2_fpe_funcs_ptr)); 142 assert (new != NULL); 143 fpe_functions = new; 144 145 fpe_functions[num_fpe_types] = funcs; 146 147 return num_fpe_types++; 148} 149 150static void 151test_remove_fs_handlers(FontPathElementPtr fpe, 152 FontBlockHandlerProcPtr block_handler, Bool all) 153{ 154 err(BadImplementation, "%s called but not yet implemented", __func__); 155} 156 157static void * 158test_get_server_client(void) 159{ 160 err(BadImplementation, "%s called but not yet implemented", __func__); 161} 162 163static int 164test_set_font_authorizations(char **authorizations, int *authlen, void *client) 165{ 166 err(BadImplementation, "%s called but not yet implemented", __func__); 167} 168 169static int 170test_store_font_client_font(FontPtr pfont, Font id) 171{ 172 err(BadImplementation, "%s called but not yet implemented", __func__); 173} 174 175static Atom 176test_make_atom(const char *string, unsigned len, int makeit) 177{ 178 err(BadImplementation, "%s called but not yet implemented", __func__); 179} 180 181static int 182test_valid_atom(Atom atom) 183{ 184 err(BadImplementation, "%s called but not yet implemented", __func__); 185} 186 187static const char * 188test_name_for_atom(Atom atom) 189{ 190 err(BadImplementation, "%s called but not yet implemented", __func__); 191} 192 193static unsigned long 194test_get_server_generation(void) 195{ 196 return server_generation; 197} 198 199static int 200test_add_fs_fd(int fd, FontFdHandlerProcPtr handler, void *data) 201{ 202 err(BadImplementation, "%s called but not yet implemented", __func__); 203} 204 205static void 206test_remove_fs_fd(int fd) 207{ 208 err(BadImplementation, "%s called but not yet implemented", __func__); 209} 210 211static void 212test_adjust_fs_wait_for_delay(void *wt, unsigned long newdelay) 213{ 214 err(BadImplementation, "%s called but not yet implemented", __func__); 215} 216 217static const xfont2_client_funcs_rec xfont2_client_funcs = { 218 .version = XFONT2_CLIENT_FUNCS_VERSION, 219 .client_auth_generation = test_client_auth_generation, 220 .client_signal = test_client_signal, 221 .delete_font_client_id = test_delete_font_client_id, 222 .verrorf = test_verrorf, 223 .find_old_font = test_find_old_font, 224 .get_client_resolutions = test_get_client_resolutions, 225 .get_default_point_size = test_get_default_point_size, 226 .get_new_font_client_id = test_get_new_font_client_id, 227 .get_time_in_millis = test_get_time_in_millis, 228 .init_fs_handlers = test_init_fs_handlers, 229 .register_fpe_funcs = test_register_fpe_funcs, 230 .remove_fs_handlers = test_remove_fs_handlers, 231 .get_server_client = test_get_server_client, 232 .set_font_authorizations = test_set_font_authorizations, 233 .store_font_client_font = test_store_font_client_font, 234 .make_atom = test_make_atom, 235 .valid_atom = test_valid_atom, 236 .name_for_atom = test_name_for_atom, 237 .get_server_generation = test_get_server_generation, 238 .add_fs_fd = test_add_fs_fd, 239 .remove_fs_fd = test_remove_fs_fd, 240 .adjust_fs_wait_for_delay = test_adjust_fs_wait_for_delay, 241}; 242 243 244xfont2_fpe_funcs_rec const ** 245init_font_handlers(int *fpe_function_count) 246{ 247 server_generation++; 248 xfont2_init(&xfont2_client_funcs); 249 /* make sure our callbacks were called & worked */ 250 assert (fpe_functions != NULL); 251 assert (num_fpe_types > 0); 252 *fpe_function_count = num_fpe_types; 253 return fpe_functions; 254} 255 256 257/* does the necessary magic to figure out the fpe type */ 258static int 259DetermineFPEType(const char *pathname) 260{ 261 int i; 262 263 /* make sure init_font_handlers was called first */ 264 assert (num_fpe_types > 0); 265 266 for (i = 0; i < num_fpe_types; i++) { 267 if ((*fpe_functions[i]->name_check) (pathname)) 268 return i; 269 } 270 return -1; 271} 272 273 274static const char * const default_fpes[] = { 275 "catalogue:/etc/X11/fontpath.d", 276 "built-ins" 277}; 278#define num_default_fpes (sizeof(default_fpes) / sizeof(*default_fpes)) 279 280FontPathElementPtr * 281init_font_paths(const char * const *font_paths, int *num_fpes) 282{ 283 FontPathElementPtr *fpe_list; 284 int i; 285 286 /* make sure init_font_handlers was called first */ 287 assert (num_fpe_types > 0); 288 289 /* Use default if caller didn't supply any */ 290 if (*num_fpes == 0) { 291 font_paths = default_fpes; 292 *num_fpes = num_default_fpes; 293 } 294 295 fpe_list = calloc(*num_fpes, sizeof(FontPathElementPtr)); 296 assert(fpe_list != NULL); 297 298 for (i = 0; i < *num_fpes; i++) { 299 int result; 300 FontPathElementPtr fpe = calloc(1, sizeof(FontPathElementRec)); 301 assert(fpe != NULL); 302 303 fpe->name = strdup(font_paths[i]); 304 assert(fpe->name != NULL); 305 fpe->name_length = strlen(fpe->name); 306 assert(fpe->name_length > 0); 307 /* If path is to fonts.dir file, trim it off and use the full 308 directory path instead. Simplifies testing with afl. */ 309 if (fpe->name_length > (int) sizeof("/fonts.dir")) { 310 char *tail = fpe->name + fpe->name_length - 311 (sizeof("/fonts.dir") - 1); 312 313 if (strcmp(tail, "/fonts.dir") == 0) { 314 char *fullpath; 315 316 *tail = '\0'; 317 fullpath = realpath(fpe->name, NULL); 318 assert(fullpath != NULL); 319 free(fpe->name); 320 fpe->name = fullpath; 321 fpe->name_length = strlen(fpe->name); 322 assert(fpe->name_length > 0); 323 } 324 } 325 fpe->type = DetermineFPEType(fpe->name); 326 if (fpe->type == -1) 327 err(BadFontPath, "Unable to find handler for font path %s", 328 fpe->name); 329 result = (*fpe_functions[fpe->type]->init_fpe) (fpe); 330 if (result != Successful) 331 err(result, "init_fpe failed for font path %s: error %d", 332 fpe->name, result); 333 334 printf("Initialized font path element #%d: %s\n", i, fpe->name); 335 fpe_list[i] = fpe; 336 } 337 printf("\n"); 338 339 return fpe_list; 340} 341