Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / protocol-plugin / lib / cpluff / test / testmain.c
1 /*-------------------------------------------------------------------------
2  * C-Pluff, a plug-in framework for C
3  * Copyright 2007 Johannes Lehtinen
4  * 
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:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
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  *-----------------------------------------------------------------------*/
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "test.h"
28 #include "../libcpluff/internal.h"
29
30 static const char *argv0;
31
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);
34         abort();
35 }
36
37 static void full_logger(cp_log_severity_t severity, const char *msg, const char *apid, void *user_data) {
38         const char *sevstr;
39         switch (severity) {
40                 case CP_LOG_DEBUG:
41                         sevstr = "DEBUG";
42                         break;
43                 case CP_LOG_INFO:
44                         sevstr = "INFO";
45                         break;
46                 case CP_LOG_WARNING:
47                         sevstr = "WARNING";
48                         break;
49                 case CP_LOG_ERROR:
50                         sevstr = "ERROR";
51                         break;
52                 default:
53                         check((sevstr = "UNKNOWN", 0));
54                         break;
55         }
56         if (apid != NULL) {
57                 fprintf(stderr, "testsuite: %s: [%s] %s\n", sevstr, apid, msg);
58         } else {
59                 fprintf(stderr, "testsuite: %s: [testsuite] %s\n", sevstr, msg);
60         }
61         if (severity >= CP_LOG_ERROR && user_data != NULL) {
62                 (*((int *) user_data))++;               
63         }
64 }
65
66 static void counting_logger(cp_log_severity_t severity, const char *msg, const char *apid, void *user_data) {
67         (*((int *) user_data))++;
68 }
69
70 CP_HIDDEN cp_context_t *init_context(cp_log_severity_t min_disp_sev, int *error_counter) {
71         cp_context_t *ctx;
72         cp_status_t status;
73         
74         check(cp_init() == CP_OK);
75         check((ctx = cp_create_context(&status)) != NULL && status == CP_OK);
76         if (error_counter != NULL) {
77                 *error_counter = 0;
78         }
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);
82                 } else {
83                         check(cp_register_logger(ctx, counting_logger, error_counter, CP_LOG_ERROR) == CP_OK);
84                 }
85         }
86         return ctx;
87 }
88
89 static char *plugindir_buffer = NULL;
90
91 CP_HIDDEN const char *plugindir(const char *plugin) {
92         const char *srcdir;
93         
94         if (plugindir_buffer != NULL) {
95                 free(plugindir_buffer);
96                 plugindir_buffer = NULL;
97         }
98         if ((srcdir = getenv("srcdir")) == NULL) {
99                 srcdir=".";
100         }
101         if ((plugindir_buffer = malloc((strlen(srcdir) + strlen("/plugins/") + strlen(plugin) + 1) * sizeof(char))) == NULL) {
102                 fputs("testsuite: ERROR: Insufficient memory.\n", stderr);
103                 exit(2);
104         }
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;
109 }
110
111 static char *pcollectiondir_buffer = NULL;
112
113 CP_HIDDEN const char *pcollectiondir(const char *collection) {
114         const char *srcdir;
115         
116         if (pcollectiondir_buffer != NULL) {
117                 free(pcollectiondir_buffer);
118                 pcollectiondir_buffer = NULL;
119         }
120         if ((srcdir = getenv("srcdir")) == NULL) {
121                 srcdir=".";
122         }
123         if ((pcollectiondir_buffer = malloc((strlen(srcdir) + strlen("/pcollections/") + strlen(collection) + 1) * sizeof(char))) == NULL) {
124                 fputs("testsuite: ERROR: Insufficient memory.\n", stderr);
125                 exit(2);
126         }
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;
131 }
132
133 CP_HIDDEN void free_test_resources(void) {
134         if (plugindir_buffer != NULL) {
135                 free(plugindir_buffer);
136                 plugindir_buffer = NULL;
137         }
138         if (pcollectiondir_buffer != NULL) {
139                 free(pcollectiondir_buffer);
140                 pcollectiondir_buffer = NULL;
141         }
142 }
143
144 int main(int argc, char *argv[]) {
145         DLHANDLE dh;
146         void *ptr;
147
148         // Check arguments
149         if (argc != 2) {
150                 fputs("testsuite: ERROR: Usage: testsuite <test>\n", stderr);
151                 exit(2);
152         }
153         if ((argv0 = argv[0]) == NULL) {
154                 argv0 = "testsuite";
155         }
156
157         // Find the test
158         if ((dh = DLOPEN(NULL)) == NULL) {
159                 fputs("testsuite: ERROR: Could not open the testsuite binary for symbols.\n", stderr);
160                 exit(2);
161         }
162         if ((ptr = DLSYM(dh, argv[1])) == NULL) {
163                 fprintf(stderr, "testsuite: ERROR: Could not resolve symbol %s.\n", argv[1]);
164                 exit(2);
165         }
166         
167         // Execute the test
168         // (NOTE: This conversion is not ANSI C compatible)
169         ((void (*)(void)) ptr)();
170         
171         // Free test resources
172         free_test_resources();
173         
174         // Successfully completed
175         exit(0);
176 }