removed StartLibCitadel()
[citadel.git] / webcit / tests / test_main.c
1 /*
2  * This contains a simple multithreaded TCP server manager.  It sits around
3  * waiting on the specified port for incoming HTTP connections.  When a
4  * connection is established, it calls context_loop() from context_loop.c.
5  *
6  * Copyright (c) 1996-2016 by the citadel.org developers.
7  * This program is released under the terms of the GNU General Public License v3.
8  */
9
10 #include "../webcit.h"
11 #include "../webserver.h"
12 #include "../modules_init.h"
13
14
15 #include <CUnit/CUnit.h>
16 #include <CUnit/Basic.h>
17 #include <CUnit/TestDB.h>
18
19
20 CU_EXPORT void         CU_automated_run_tests(void);
21 CU_EXPORT CU_ErrorCode CU_list_tests_to_file(void);
22 CU_EXPORT void         CU_set_output_filename(const char* szFilenameRoot);
23
24 extern int msock;                       /* master listening socket */
25 extern int verbosity;           /* Logging level */
26 extern char static_icon_dir[PATH_MAX];          /* where should we find our mime icons */
27
28 int is_https = 0;               /* Nonzero if I am an HTTPS service */
29 int follow_xff = 0;             /* Follow X-Forwarded-For: header */
30 int home_specified = 0;         /* did the user specify a homedir? */
31 int DisableGzip = 0;
32 extern pthread_mutex_t SessionListMutex;
33 extern pthread_key_t MyConKey;
34
35 extern void run_tests(void);
36 extern int ig_tcp_server(char *ip_addr, int port_number, int queue_len);
37 extern int ig_uds_server(char *sockpath, int queue_len);
38 extern void webcit_calc_dirs_n_files(int relh, const char *basedir, int home, char *webcitdir, char *relhome);
39
40
41 char socket_dir[PATH_MAX];                      /* where to talk to our citadel server */
42
43 char *server_cookie = NULL;     /* our Cookie connection to the client */
44 int http_port = PORT_NUM;       /* Port to listen on */
45 char *ctdlhost = DEFAULT_HOST;  /* our name */
46 char *ctdlport = DEFAULT_PORT;  /* our Port */
47 int running_as_daemon = 0;      /* should we deamonize on startup? */
48 char wizard_filename[PATH_MAX];
49
50 StrBuf *Username = NULL;                 /* the test user... */
51 StrBuf *Passvoid = NULL;                 /* the test user... */
52
53
54 extern int dbg_analyze_msg;
55 extern int dbg_bactrace_template_errors;
56 extern int DumpTemplateI18NStrings;
57 extern StrBuf *I18nDump;
58 void InitTemplateCache(void);
59 extern int LoadTemplates;
60
61
62
63 /*
64  * Here's where it all begins.
65  */
66 int main(int argc, char **argv)
67 {
68         pthread_attr_t attr;            /* Thread attributes */
69         int a;                  /* General-purpose variables */
70         char tracefile[PATH_MAX];
71         char ip_addr[256]="0.0.0.0";
72         int relh=0;
73         int home=0;
74         int home_specified=0;
75         char relhome[PATH_MAX]="";
76         char webcitdir[PATH_MAX] = DATADIR;
77         char *hdir;
78         const char *basedir = NULL;
79         char uds_listen_path[PATH_MAX]; /* listen on a unix domain socket? */
80         FILE *rvfp = NULL;
81         
82         WildFireInitBacktrace(argv[0], 2);
83
84         start_modules ();
85
86         /* Ensure that we are linked to the correct version of libcitadel */
87         if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
88                 fprintf(stderr, " You are running libcitadel version %d\n", (libcitadel_version_number()));
89                 fprintf(stderr, "WebCit was compiled against version %d\n", LIBCITADEL_VERSION_NUMBER);
90                 return(1);
91         }
92
93         strcpy(uds_listen_path, "");
94
95         /* Parse command line */
96         while ((a = getopt(argc, argv, "h:i:p:t:T:B:x:U:P:cf:Z")) != EOF)
97                 switch (a) {
98                 case 'U':
99                         Username = NewStrBufPlain(optarg, -1);
100                         break;
101                 case 'P':
102                         Passvoid = NewStrBufPlain(optarg, -1);
103                         break;
104                 case 'h':
105                         hdir = strdup(optarg);
106                         relh=hdir[0]!='/';
107                         if (!relh) {
108                                 safestrncpy(webcitdir, hdir, sizeof webcitdir);
109                         }
110                         else {
111                                 safestrncpy(relhome, relhome, sizeof relhome);
112                         }
113                         /* free(hdir); TODO: SHOULD WE DO THIS? */
114                         home_specified = 1;
115                         home=1;
116                         break;
117                 case 'B': /* deprecated */
118                         break;
119                 case 'i':
120                         safestrncpy(ip_addr, optarg, sizeof ip_addr);
121                         break;
122                 case 'p':
123                         http_port = atoi(optarg);
124                         if (http_port == 0) {
125                                 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
126                         }
127                         break;
128                 case 't':
129                         safestrncpy(tracefile, optarg, sizeof tracefile);
130                         rvfp = freopen(tracefile, "w", stdout);
131                         rvfp = freopen(tracefile, "w", stderr);
132                         rvfp = freopen(tracefile, "r", stdin);
133                         break;
134                 case 'T':
135                         LoadTemplates = atoi(optarg);
136                         dbg_analyze_msg = (LoadTemplates && (1<<1)) != 0;
137                         dbg_bactrace_template_errors = (LoadTemplates && (1<<2)) != 0;
138                         break;
139                 case 'Z':
140                         DisableGzip = 1;
141                         break;
142                 case 'x':
143                         verbosity = atoi(optarg);
144                         break;
145                 case 'f':
146                         follow_xff = 1;
147                         break;
148                 case 'c':
149                         server_cookie = malloc(256);
150                         if (server_cookie != NULL) {
151                                 safestrncpy(server_cookie,
152                                        "Set-cookie: wcserver=",
153                                         256);
154                                 if (gethostname
155                                     (&server_cookie[strlen(server_cookie)],
156                                      200) != 0) {
157                                         syslog(2, "gethostname: %s\n",
158                                                 strerror(errno));
159                                         free(server_cookie);
160                                 }
161                         }
162                         break;
163                 default:
164                         fprintf(stderr, "usage: webcit "
165                                 "[-i ip_addr] [-p http_port] "
166                                 "[-t tracefile] [-c] [-f] "
167                                 "[-T Templatedebuglevel] "
168                                 "[-Z] [-G i18ndumpfile] "
169 #ifdef HAVE_OPENSSL
170                                 "[-s] [-S cipher_suites]"
171 #endif
172                                 "[-U Username -P Password]"
173                                 ""
174                                 "[remotehost [remoteport]]\n");
175                         return 1;
176                 }
177
178         if (optind < argc) {
179                 ctdlhost = argv[optind];
180                 if (++optind < argc)
181                         ctdlport = argv[optind];
182         }
183
184         webcit_calc_dirs_n_files(relh, basedir, home, webcitdir, relhome);
185         LoadIconDir(static_icon_dir);
186
187         /* Tell 'em who's in da house */
188         syslog(1, PACKAGE_STRING "\n");
189         syslog(1, "Copyright (C) 1996-2009 by the Citadel development team.\n"
190                 "This software is distributed under the terms of the "
191                 "GNU General Public License.\n\n"
192         );
193
194
195         /* initialize the International Bright Young Thing */
196
197         initialise_modules();
198         initialize_viewdefs();
199         initialize_axdefs();
200
201         InitTemplateCache();
202
203         /* Use our own prefix on tzid's generated from system tzdata */
204         icaltimezone_set_tzid_prefix("/citadel.org/");
205
206         /*
207          * Set up a place to put thread-specific data.
208          * We only need a single pointer per thread - it points to the
209          * wcsession struct to which the thread is currently bound.
210          */
211         if (pthread_key_create(&MyConKey, NULL) != 0) {
212                 syslog(1, "Can't create TSD key: %s\n", strerror(errno));
213         }
214         InitialiseSemaphores ();
215
216
217         /*
218          * Start up the housekeeping thread
219          */
220         pthread_attr_init(&attr);
221         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
222
223         run_tests();
224
225         ShutDownWebcit();
226         FreeStrBuf(&Username);
227         FreeStrBuf(&Passvoid);
228
229         return 0;
230 }
231
232