• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2      This file is part of libmicrohttpd
3      Copyright (C) 2007 Christian Grothoff
4 
5      libmicrohttpd is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9 
10      libmicrohttpd is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14 
15      You should have received a copy of the GNU General Public License
16      along with libmicrohttpd; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 
21 /**
22  * @file test_daemon.c
23  * @brief  Testcase for libmicrohttpd starts and stops
24  * @author Christian Grothoff
25  */
26 
27 #include "platform.h"
28 #include "microhttpd.h"
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32 
33 #ifndef WINDOWS
34 #include <unistd.h>
35 #endif
36 
37 
38 static int
testStartError()39 testStartError ()
40 {
41   struct MHD_Daemon *d;
42 
43   d = MHD_start_daemon (MHD_USE_DEBUG, 0, NULL, NULL, NULL, NULL);
44   if (d != NULL)
45     return 1;
46   return 0;
47 }
48 
49 static int
apc_nothing(void * cls,const struct sockaddr * addr,socklen_t addrlen)50 apc_nothing (void *cls, const struct sockaddr *addr, socklen_t addrlen)
51 {
52   return MHD_NO;
53 }
54 
55 static int
apc_all(void * cls,const struct sockaddr * addr,socklen_t addrlen)56 apc_all (void *cls, const struct sockaddr *addr, socklen_t addrlen)
57 {
58   return MHD_YES;
59 }
60 
61 static int
ahc_nothing(void * cls,struct MHD_Connection * connection,const char * url,const char * method,const char * version,const char * upload_data,size_t * upload_data_size,void ** unused)62 ahc_nothing (void *cls,
63              struct MHD_Connection *connection,
64              const char *url,
65              const char *method,
66              const char *version,
67              const char *upload_data, size_t *upload_data_size,
68              void **unused)
69 {
70   return MHD_NO;
71 }
72 
73 static int
testStartStop()74 testStartStop ()
75 {
76   struct MHD_Daemon *d;
77 
78   d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
79                         1080,
80                         &apc_nothing,
81                         NULL, &ahc_nothing, NULL, MHD_OPTION_END);
82   if (d == NULL)
83     return 2;
84   MHD_stop_daemon (d);
85   return 0;
86 }
87 
88 static int
testExternalRun()89 testExternalRun ()
90 {
91   struct MHD_Daemon *d;
92   fd_set rs;
93   MHD_socket maxfd;
94   int i;
95 
96   d = MHD_start_daemon (MHD_USE_DEBUG,
97                         1081,
98                         &apc_all, NULL, &ahc_nothing, NULL, MHD_OPTION_END);
99 
100   if (d == NULL)
101     return 4;
102   i = 0;
103   while (i < 15)
104     {
105       maxfd = 0;
106       FD_ZERO (&rs);
107       if (MHD_YES != MHD_get_fdset (d, &rs, &rs, &rs, &maxfd))
108 	{
109 	  MHD_stop_daemon (d);
110 	  return 256;
111 	}
112       if (MHD_run (d) == MHD_NO)
113         {
114           MHD_stop_daemon (d);
115           return 8;
116         }
117       i++;
118     }
119   MHD_stop_daemon (d);
120   return 0;
121 }
122 
123 static int
testThread()124 testThread ()
125 {
126   struct MHD_Daemon *d;
127   d = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SELECT_INTERNALLY,
128                         1082,
129                         &apc_all, NULL, &ahc_nothing, NULL, MHD_OPTION_END);
130 
131   if (d == NULL)
132     return 16;
133   if (MHD_run (d) != MHD_NO)
134     return 32;
135   MHD_stop_daemon (d);
136   return 0;
137 }
138 
139 static int
testMultithread()140 testMultithread ()
141 {
142   struct MHD_Daemon *d;
143   d = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_THREAD_PER_CONNECTION,
144                         1083,
145                         &apc_all, NULL, &ahc_nothing, NULL, MHD_OPTION_END);
146 
147   if (d == NULL)
148     return 64;
149   if (MHD_run (d) != MHD_NO)
150     return 128;
151   MHD_stop_daemon (d);
152   return 0;
153 }
154 
155 int
main(int argc,char * const * argv)156 main (int argc, char *const *argv)
157 {
158   int errorCount = 0;
159   errorCount += testStartError ();
160   errorCount += testStartStop ();
161   errorCount += testExternalRun ();
162   errorCount += testThread ();
163   errorCount += testMultithread ();
164   if (errorCount != 0)
165     fprintf (stderr, "Error (code: %u)\n", errorCount);
166   return errorCount != 0;       /* 0 == pass */
167 }
168