1748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat/*
2748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat     This file is part of libmicrohttpd
3748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat     Copyright (C) 2007 Christian Grothoff
4748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
5748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat     libmicrohttpd is free software; you can redistribute it and/or modify
6748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat     it under the terms of the GNU General Public License as published
7748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat     by the Free Software Foundation; either version 2, or (at your
8748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat     option) any later version.
9748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
10748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat     libmicrohttpd is distributed in the hope that it will be useful, but
11748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat     WITHOUT ANY WARRANTY; without even the implied warranty of
12748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat     General Public License for more details.
14748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
15748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat     You should have received a copy of the GNU General Public License
16748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat     along with libmicrohttpd; see the file COPYING.  If not, write to the
17748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat     Boston, MA 02111-1307, USA.
19748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat*/
20748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
21748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat/**
22748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat * @file test_daemon.c
23748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat * @brief  Testcase for libmicrohttpd starts and stops
24748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat * @author Christian Grothoff
25748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat */
26748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
27748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat#include "platform.h"
28748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat#include "microhttpd.h"
29748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat#include <stdlib.h>
30748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat#include <string.h>
31748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat#include <stdio.h>
32748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
33748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat#ifndef WINDOWS
34748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat#include <unistd.h>
35748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat#endif
36748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
37748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
38748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Eratstatic int
39748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel ErattestStartError ()
40748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat{
41748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  struct MHD_Daemon *d;
42748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
43748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  d = MHD_start_daemon (MHD_USE_DEBUG, 0, NULL, NULL, NULL, NULL);
44748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  if (d != NULL)
45748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat    return 1;
46748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  return 0;
47748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat}
48748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
49748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Eratstatic int
50748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Eratapc_nothing (void *cls, const struct sockaddr *addr, socklen_t addrlen)
51748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat{
52748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  return MHD_NO;
53748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat}
54748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
55748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Eratstatic int
56748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Eratapc_all (void *cls, const struct sockaddr *addr, socklen_t addrlen)
57748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat{
58748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  return MHD_YES;
59748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat}
60748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
61748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Eratstatic int
62748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Eratahc_nothing (void *cls,
63748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat             struct MHD_Connection *connection,
64748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat             const char *url,
65748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat             const char *method,
66748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat             const char *version,
67748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat             const char *upload_data, size_t *upload_data_size,
68748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat             void **unused)
69748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat{
70748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  return MHD_NO;
71748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat}
72748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
73748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Eratstatic int
74748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel ErattestStartStop ()
75748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat{
76748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  struct MHD_Daemon *d;
77748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
78748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
79748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat                        1080,
80748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat                        &apc_nothing,
81748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat                        NULL, &ahc_nothing, NULL, MHD_OPTION_END);
82748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  if (d == NULL)
83748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat    return 2;
84748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  MHD_stop_daemon (d);
85748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  return 0;
86748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat}
87748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
88748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Eratstatic int
89748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel ErattestExternalRun ()
90748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat{
91748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  struct MHD_Daemon *d;
92748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  fd_set rs;
93748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  MHD_socket maxfd;
94748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  int i;
95748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
96748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  d = MHD_start_daemon (MHD_USE_DEBUG,
97748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat                        1081,
98748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat                        &apc_all, NULL, &ahc_nothing, NULL, MHD_OPTION_END);
99748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
100748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  if (d == NULL)
101748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat    return 4;
102748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  i = 0;
103748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  while (i < 15)
104748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat    {
105748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat      maxfd = 0;
106748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat      FD_ZERO (&rs);
107748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat      if (MHD_YES != MHD_get_fdset (d, &rs, &rs, &rs, &maxfd))
108748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat	{
109748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat	  MHD_stop_daemon (d);
110748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat	  return 256;
111748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat	}
112748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat      if (MHD_run (d) == MHD_NO)
113748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat        {
114748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat          MHD_stop_daemon (d);
115748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat          return 8;
116748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat        }
117748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat      i++;
118748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat    }
119748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  MHD_stop_daemon (d);
120748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  return 0;
121748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat}
122748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
123748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Eratstatic int
124748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel ErattestThread ()
125748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat{
126748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  struct MHD_Daemon *d;
127748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  d = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SELECT_INTERNALLY,
128748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat                        1082,
129748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat                        &apc_all, NULL, &ahc_nothing, NULL, MHD_OPTION_END);
130748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
131748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  if (d == NULL)
132748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat    return 16;
133748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  if (MHD_run (d) != MHD_NO)
134748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat    return 32;
135748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  MHD_stop_daemon (d);
136748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  return 0;
137748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat}
138748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
139748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Eratstatic int
140748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel ErattestMultithread ()
141748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat{
142748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  struct MHD_Daemon *d;
143748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  d = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_THREAD_PER_CONNECTION,
144748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat                        1083,
145748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat                        &apc_all, NULL, &ahc_nothing, NULL, MHD_OPTION_END);
146748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
147748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  if (d == NULL)
148748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat    return 64;
149748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  if (MHD_run (d) != MHD_NO)
150748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat    return 128;
151748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  MHD_stop_daemon (d);
152748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  return 0;
153748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat}
154748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat
155748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Eratint
156748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Eratmain (int argc, char *const *argv)
157748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat{
158748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  int errorCount = 0;
159748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  errorCount += testStartError ();
160748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  errorCount += testStartStop ();
161748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  errorCount += testExternalRun ();
162748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  errorCount += testThread ();
163748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  errorCount += testMultithread ();
164748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  if (errorCount != 0)
165748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat    fprintf (stderr, "Error (code: %u)\n", errorCount);
166748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat  return errorCount != 0;       /* 0 == pass */
167748945ec6f1c67b7efc934ab0808e1d32f2fb98dDaniel Erat}
168