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