Back to index

webcit  8.12-dfsg
test_main.c
Go to the documentation of this file.
00001 /*
00002  * This contains a simple multithreaded TCP server manager.  It sits around
00003  * waiting on the specified port for incoming HTTP connections.  When a
00004  * connection is established, it calls context_loop() from context_loop.c.
00005  *
00006  * Copyright (c) 1996-2012 by the citadel.org developers.
00007  * This program is released under the terms of the GNU General Public License v3.
00008  */
00009 
00010 #include "../webcit.h"
00011 #include "../webserver.h"
00012 #include "../modules_init.h"
00013 
00014 
00015 #include <CUnit/CUnit.h>
00016 #include <CUnit/Basic.h>
00017 #include <CUnit/TestDB.h>
00018 
00019 
00020 CU_EXPORT void         CU_automated_run_tests(void);
00021 CU_EXPORT CU_ErrorCode CU_list_tests_to_file(void);
00022 CU_EXPORT void         CU_set_output_filename(const char* szFilenameRoot);
00023 
00024 extern int msock;                  /* master listening socket */
00025 extern int verbosity;              /* Logging level */
00026 extern char static_icon_dir[PATH_MAX];          /* where should we find our mime icons */
00027 
00028 int is_https = 0;           /* Nonzero if I am an HTTPS service */
00029 int follow_xff = 0;         /* Follow X-Forwarded-For: header */
00030 int home_specified = 0;            /* did the user specify a homedir? */
00031 int DisableGzip = 0;
00032 extern pthread_mutex_t SessionListMutex;
00033 extern pthread_key_t MyConKey;
00034 
00035 extern void run_tests(void);
00036 extern int ig_tcp_server(char *ip_addr, int port_number, int queue_len);
00037 extern int ig_uds_server(char *sockpath, int queue_len);
00038 extern void webcit_calc_dirs_n_files(int relh, const char *basedir, int home, char *webcitdir, char *relhome);
00039 
00040 
00041 char socket_dir[PATH_MAX];                /* where to talk to our citadel server */
00042 
00043 char *server_cookie = NULL; /* our Cookie connection to the client */
00044 int http_port = PORT_NUM;   /* Port to listen on */
00045 char *ctdlhost = DEFAULT_HOST;     /* our name */
00046 char *ctdlport = DEFAULT_PORT;     /* our Port */
00047 int running_as_daemon = 0;  /* should we deamonize on startup? */
00048 char wizard_filename[PATH_MAX];
00049 
00050 StrBuf *Username = NULL;                 /* the test user... */
00051 StrBuf *Passvoid = NULL;                 /* the test user... */
00052 
00053 
00054 extern int dbg_analyze_msg;
00055 extern int dbg_bactrace_template_errors;
00056 extern int DumpTemplateI18NStrings;
00057 extern StrBuf *I18nDump;
00058 void InitTemplateCache(void);
00059 extern int LoadTemplates;
00060 
00061 
00062 
00063 /*
00064  * Here's where it all begins.
00065  */
00066 int main(int argc, char **argv)
00067 {
00068        size_t basesize = 2;            /* how big should strbufs be on creation? */
00069        pthread_attr_t attr;        /* Thread attributes */
00070        int a;               /* General-purpose variables */
00071        char tracefile[PATH_MAX];
00072        char ip_addr[256]="0.0.0.0";
00073        int relh=0;
00074        int home=0;
00075        int home_specified=0;
00076        char relhome[PATH_MAX]="";
00077        char webcitdir[PATH_MAX] = DATADIR;
00078        char *hdir;
00079        const char *basedir = NULL;
00080        char uds_listen_path[PATH_MAX];    /* listen on a unix domain socket? */
00081        FILE *rvfp = NULL;
00082        
00083        WildFireInitBacktrace(argv[0], 2);
00084 
00085        start_modules ();
00086 
00087        /* Ensure that we are linked to the correct version of libcitadel */
00088        if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
00089               fprintf(stderr, " You are running libcitadel version %d.%02d\n",
00090                      (libcitadel_version_number() / 100), (libcitadel_version_number() % 100));
00091               fprintf(stderr, "WebCit was compiled against version %d.%02d\n",
00092                      (LIBCITADEL_VERSION_NUMBER / 100), (LIBCITADEL_VERSION_NUMBER % 100));
00093               return(1);
00094        }
00095 
00096        strcpy(uds_listen_path, "");
00097 
00098        /* Parse command line */
00099        while ((a = getopt(argc, argv, "h:i:p:t:T:B:x:U:P:cf:Z")) != EOF)
00100               switch (a) {
00101               case 'U':
00102                      Username = NewStrBufPlain(optarg, -1);
00103                      break;
00104               case 'P':
00105                      Passvoid = NewStrBufPlain(optarg, -1);
00106                      break;
00107               case 'h':
00108                      hdir = strdup(optarg);
00109                      relh=hdir[0]!='/';
00110                      if (!relh) {
00111                             safestrncpy(webcitdir, hdir, sizeof webcitdir);
00112                      }
00113                      else {
00114                             safestrncpy(relhome, relhome, sizeof relhome);
00115                      }
00116                      /* free(hdir); TODO: SHOULD WE DO THIS? */
00117                      home_specified = 1;
00118                      home=1;
00119                      break;
00120               case 'B': /* Basesize */
00121                      basesize = atoi(optarg);
00122                      if (basesize > 2)
00123                             StartLibCitadel(basesize);
00124                      break;
00125               case 'i':
00126                      safestrncpy(ip_addr, optarg, sizeof ip_addr);
00127                      break;
00128               case 'p':
00129                      http_port = atoi(optarg);
00130                      if (http_port == 0) {
00131                             safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
00132                      }
00133                      break;
00134               case 't':
00135                      safestrncpy(tracefile, optarg, sizeof tracefile);
00136                      rvfp = freopen(tracefile, "w", stdout);
00137                      rvfp = freopen(tracefile, "w", stderr);
00138                      rvfp = freopen(tracefile, "r", stdin);
00139                      break;
00140               case 'T':
00141                      LoadTemplates = atoi(optarg);
00142                      dbg_analyze_msg = (LoadTemplates && (1<<1)) != 0;
00143                      dbg_bactrace_template_errors = (LoadTemplates && (1<<2)) != 0;
00144                      break;
00145               case 'Z':
00146                      DisableGzip = 1;
00147                      break;
00148               case 'x':
00149                      verbosity = atoi(optarg);
00150                      break;
00151               case 'f':
00152                      follow_xff = 1;
00153                      break;
00154               case 'c':
00155                      server_cookie = malloc(256);
00156                      if (server_cookie != NULL) {
00157                             safestrncpy(server_cookie,
00158                                    "Set-cookie: wcserver=",
00159                                    256);
00160                             if (gethostname
00161                                 (&server_cookie[strlen(server_cookie)],
00162                                  200) != 0) {
00163                                    syslog(2, "gethostname: %s\n",
00164                                           strerror(errno));
00165                                    free(server_cookie);
00166                             }
00167                      }
00168                      break;
00169               default:
00170                      fprintf(stderr, "usage: webcit "
00171                             "[-i ip_addr] [-p http_port] "
00172                             "[-t tracefile] [-c] [-f] "
00173                             "[-T Templatedebuglevel] "
00174                             "[-Z] [-G i18ndumpfile] "
00175 #ifdef HAVE_OPENSSL
00176                             "[-s] [-S cipher_suites]"
00177 #endif
00178                             "[-U Username -P Password]"
00179                             ""
00180                             "[remotehost [remoteport]]\n");
00181                      return 1;
00182               }
00183 
00184        if (optind < argc) {
00185               ctdlhost = argv[optind];
00186               if (++optind < argc)
00187                      ctdlport = argv[optind];
00188        }
00189 
00190        webcit_calc_dirs_n_files(relh, basedir, home, webcitdir, relhome);
00191        LoadIconDir(static_icon_dir);
00192 
00193        /* Tell 'em who's in da house */
00194        syslog(1, PACKAGE_STRING "\n");
00195        syslog(1, "Copyright (C) 1996-2009 by the Citadel development team.\n"
00196               "This software is distributed under the terms of the "
00197               "GNU General Public License.\n\n"
00198        );
00199 
00200 
00201        /* initialize the International Bright Young Thing */
00202 
00203        initialise_modules();
00204        initialize_viewdefs();
00205        initialize_axdefs();
00206 
00207        InitTemplateCache();
00208 
00209        /* Use our own prefix on tzid's generated from system tzdata */
00210        icaltimezone_set_tzid_prefix("/citadel.org/");
00211 
00212        /*
00213         * Set up a place to put thread-specific data.
00214         * We only need a single pointer per thread - it points to the
00215         * wcsession struct to which the thread is currently bound.
00216         */
00217        if (pthread_key_create(&MyConKey, NULL) != 0) {
00218               syslog(1, "Can't create TSD key: %s\n", strerror(errno));
00219        }
00220        InitialiseSemaphores ();
00221 
00222 
00223        /*
00224         * Start up the housekeeping thread
00225         */
00226        pthread_attr_init(&attr);
00227        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
00228 
00229        run_tests();
00230 
00231        ShutDownWebcit();
00232        FreeStrBuf(&Username);
00233        FreeStrBuf(&Passvoid);
00234 
00235        return 0;
00236 }
00237 
00238