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