1 /*-------------------------------------------------------------------------
2 * C-Pluff, a plug-in framework for C
3 * Copyright 2007 Johannes Lehtinen
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *-----------------------------------------------------------------------*/
28 #include "../libcpluff/internal.h"
30 static const char *argv0;
32 CP_HIDDEN void fail(const char *func, const char *file, int line, const char *msg) {
33 fprintf(stderr, "%s: %s:%d: %s: %s\n", argv0, file, line, func, msg);
37 static void full_logger(cp_log_severity_t severity, const char *msg, const char *apid, void *user_data) {
53 check((sevstr = "UNKNOWN", 0));
57 fprintf(stderr, "testsuite: %s: [%s] %s\n", sevstr, apid, msg);
59 fprintf(stderr, "testsuite: %s: [testsuite] %s\n", sevstr, msg);
61 if (severity >= CP_LOG_ERROR && user_data != NULL) {
62 (*((int *) user_data))++;
66 static void counting_logger(cp_log_severity_t severity, const char *msg, const char *apid, void *user_data) {
67 (*((int *) user_data))++;
70 CP_HIDDEN cp_context_t *init_context(cp_log_severity_t min_disp_sev, int *error_counter) {
74 check(cp_init() == CP_OK);
75 check((ctx = cp_create_context(&status)) != NULL && status == CP_OK);
76 if (error_counter != NULL) {
79 if (error_counter != NULL || min_disp_sev <= CP_LOG_ERROR) {
80 if (min_disp_sev <= CP_LOG_ERROR) {
81 check(cp_register_logger(ctx, full_logger, error_counter, min_disp_sev) == CP_OK);
83 check(cp_register_logger(ctx, counting_logger, error_counter, CP_LOG_ERROR) == CP_OK);
89 static char *plugindir_buffer = NULL;
91 CP_HIDDEN const char *plugindir(const char *plugin) {
94 if (plugindir_buffer != NULL) {
95 free(plugindir_buffer);
96 plugindir_buffer = NULL;
98 if ((srcdir = getenv("srcdir")) == NULL) {
101 if ((plugindir_buffer = malloc((strlen(srcdir) + strlen("/plugins/") + strlen(plugin) + 1) * sizeof(char))) == NULL) {
102 fputs("testsuite: ERROR: Insufficient memory.\n", stderr);
105 strcpy(plugindir_buffer, srcdir);
106 strcat(plugindir_buffer, CP_FNAMESEP_STR "plugins" CP_FNAMESEP_STR);
107 strcat(plugindir_buffer, plugin);
108 return plugindir_buffer;
111 static char *pcollectiondir_buffer = NULL;
113 CP_HIDDEN const char *pcollectiondir(const char *collection) {
116 if (pcollectiondir_buffer != NULL) {
117 free(pcollectiondir_buffer);
118 pcollectiondir_buffer = NULL;
120 if ((srcdir = getenv("srcdir")) == NULL) {
123 if ((pcollectiondir_buffer = malloc((strlen(srcdir) + strlen("/pcollections/") + strlen(collection) + 1) * sizeof(char))) == NULL) {
124 fputs("testsuite: ERROR: Insufficient memory.\n", stderr);
127 strcpy(pcollectiondir_buffer, srcdir);
128 strcat(pcollectiondir_buffer, CP_FNAMESEP_STR "pcollections" CP_FNAMESEP_STR);
129 strcat(pcollectiondir_buffer, collection);
130 return pcollectiondir_buffer;
133 CP_HIDDEN void free_test_resources(void) {
134 if (plugindir_buffer != NULL) {
135 free(plugindir_buffer);
136 plugindir_buffer = NULL;
138 if (pcollectiondir_buffer != NULL) {
139 free(pcollectiondir_buffer);
140 pcollectiondir_buffer = NULL;
144 int main(int argc, char *argv[]) {
150 fputs("testsuite: ERROR: Usage: testsuite <test>\n", stderr);
153 if ((argv0 = argv[0]) == NULL) {
158 if ((dh = DLOPEN(NULL)) == NULL) {
159 fputs("testsuite: ERROR: Could not open the testsuite binary for symbols.\n", stderr);
162 if ((ptr = DLSYM(dh, argv[1])) == NULL) {
163 fprintf(stderr, "testsuite: ERROR: Could not resolve symbol %s.\n", argv[1]);
168 // (NOTE: This conversion is not ANSI C compatible)
169 ((void (*)(void)) ptr)();
171 // Free test resources
172 free_test_resources();
174 // Successfully completed