267d0db876e1a8cd84a248c05ee489de657212aa
[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-2012 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         size_t basesize = 2;            /* how big should strbufs be on creation? */
69         pthread_attr_t attr;            /* Thread attributes */
70         int a;                  /* General-purpose variables */
71         char tracefile[PATH_MAX];
72         char ip_addr[256]="0.0.0.0";
73         int relh=0;
74         int home=0;
75         int home_specified=0;
76         char relhome[PATH_MAX]="";
77         char webcitdir[PATH_MAX] = DATADIR;
78         char *hdir;
79         const char *basedir = NULL;
80         char uds_listen_path[PATH_MAX]; /* listen on a unix domain socket? */
81         FILE *rvfp = NULL;
82         
83         WildFireInitBacktrace(argv[0], 2);
84
85         start_modules ();
86
87         /* Ensure that we are linked to the correct version of libcitadel */
88         if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
89                 fprintf(stderr, " You are running libcitadel version %d.%02d\n",
90                         (libcitadel_version_number() / 100), (libcitadel_version_number() % 100));
91                 fprintf(stderr, "WebCit was compiled against version %d.%02d\n",
92                         (LIBCITADEL_VERSION_NUMBER / 100), (LIBCITADEL_VERSION_NUMBER % 100));
93                 return(1);
94         }
95
96         strcpy(uds_listen_path, "");
97
98         /* Parse command line */
99         while ((a = getopt(argc, argv, "h:i:p:t:T:B:x:U:P:cf:Z")) != EOF)
100                 switch (a) {
101                 case 'U':
102                         Username = NewStrBufPlain(optarg, -1);
103                         break;
104                 case 'P':
105                         Passvoid = NewStrBufPlain(optarg, -1);
106                         break;
107                 case 'h':
108                         hdir = strdup(optarg);
109                         relh=hdir[0]!='/';
110                         if (!relh) {
111                                 safestrncpy(webcitdir, hdir, sizeof webcitdir);
112                         }
113                         else {
114                                 safestrncpy(relhome, relhome, sizeof relhome);
115                         }
116                         /* free(hdir); TODO: SHOULD WE DO THIS? */
117                         home_specified = 1;
118                         home=1;
119                         break;
120                 case 'B': /* Basesize */
121                         basesize = atoi(optarg);
122                         if (basesize > 2)
123                                 StartLibCitadel(basesize);
124                         break;
125                 case 'i':
126                         safestrncpy(ip_addr, optarg, sizeof ip_addr);
127                         break;
128                 case 'p':
129                         http_port = atoi(optarg);
130                         if (http_port == 0) {
131                                 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
132                         }
133                         break;
134                 case 't':
135                         safestrncpy(tracefile, optarg, sizeof tracefile);
136                         rvfp = freopen(tracefile, "w", stdout);
137                         rvfp = freopen(tracefile, "w", stderr);
138                         rvfp = freopen(tracefile, "r", stdin);
139                         break;
140                 case 'T':
141                         LoadTemplates = atoi(optarg);
142                         dbg_analyze_msg = (LoadTemplates && (1<<1)) != 0;
143                         dbg_bactrace_template_errors = (LoadTemplates && (1<<2)) != 0;
144                         break;
145                 case 'Z':
146                         DisableGzip = 1;
147                         break;
148                 case 'x':
149                         verbosity = atoi(optarg);
150                         break;
151                 case 'f':
152                         follow_xff = 1;
153                         break;
154                 case 'c':
155                         server_cookie = malloc(256);
156                         if (server_cookie != NULL) {
157                                 safestrncpy(server_cookie,
158                                        "Set-cookie: wcserver=",
159                                         256);
160                                 if (gethostname
161                                     (&server_cookie[strlen(server_cookie)],
162                                      200) != 0) {
163                                         syslog(2, "gethostname: %s\n",
164                                                 strerror(errno));
165                                         free(server_cookie);
166                                 }
167                         }
168                         break;
169                 default:
170                         fprintf(stderr, "usage: webcit "
171                                 "[-i ip_addr] [-p http_port] "
172                                 "[-t tracefile] [-c] [-f] "
173                                 "[-T Templatedebuglevel] "
174                                 "[-Z] [-G i18ndumpfile] "
175 #ifdef HAVE_OPENSSL
176                                 "[-s] [-S cipher_suites]"
177 #endif
178                                 "[-U Username -P Password]"
179                                 ""
180                                 "[remotehost [remoteport]]\n");
181                         return 1;
182                 }
183
184         if (optind < argc) {
185                 ctdlhost = argv[optind];
186                 if (++optind < argc)
187                         ctdlport = argv[optind];
188         }
189
190         webcit_calc_dirs_n_files(relh, basedir, home, webcitdir, relhome);
191         LoadIconDir(static_icon_dir);
192
193         /* Tell 'em who's in da house */
194         syslog(1, PACKAGE_STRING "\n");
195         syslog(1, "Copyright (C) 1996-2009 by the Citadel development team.\n"
196                 "This software is distributed under the terms of the "
197                 "GNU General Public License.\n\n"
198         );
199
200
201         /* initialize the International Bright Young Thing */
202
203         initialise_modules();
204         initialize_viewdefs();
205         initialize_axdefs();
206
207         InitTemplateCache();
208
209         /* Use our own prefix on tzid's generated from system tzdata */
210         icaltimezone_set_tzid_prefix("/citadel.org/");
211
212         /*
213          * Set up a place to put thread-specific data.
214          * We only need a single pointer per thread - it points to the
215          * wcsession struct to which the thread is currently bound.
216          */
217         if (pthread_key_create(&MyConKey, NULL) != 0) {
218                 syslog(1, "Can't create TSD key: %s\n", strerror(errno));
219         }
220         InitialiseSemaphores ();
221
222
223         /*
224          * Start up the housekeeping thread
225          */
226         pthread_attr_init(&attr);
227         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
228
229         run_tests();
230
231         ShutDownWebcit();
232         FreeStrBuf(&Username);
233         FreeStrBuf(&Passvoid);
234
235         return 0;
236 }
237
238