aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/test_upgrade_ssl.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/microhttpd/test_upgrade_ssl.c')
-rw-r--r--src/microhttpd/test_upgrade_ssl.c236
1 files changed, 0 insertions, 236 deletions
diff --git a/src/microhttpd/test_upgrade_ssl.c b/src/microhttpd/test_upgrade_ssl.c
deleted file mode 100644
index bb3d2c3b..00000000
--- a/src/microhttpd/test_upgrade_ssl.c
+++ /dev/null
@@ -1,236 +0,0 @@
1/*
2 This file is part of libmicrohttpd
3 Copyright (C) 2016 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 3, 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21/**
22 * @file test_upgrade_ssl.c
23 * @brief Testcase for libmicrohttpd upgrading a connection
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#include <sys/types.h>
33#include <sys/wait.h>
34
35#ifndef WINDOWS
36#include <unistd.h>
37#endif
38
39#include <pthread.h>
40#include "mhd_sockets.h"
41#ifdef HAVE_NETINET_IP_H
42#include <netinet/ip.h>
43#endif /* HAVE_NETINET_IP_H */
44#include "mhd_sockets.h"
45#include "test_upgrade_common.c"
46
47#include "../testcurl/https/tls_test_keys.h"
48
49
50enum tls_cli_tool
51{
52 TLS_CLI_NO_TOOL = 0,
53 TLS_CLI_GNUTLS,
54 TLS_CLI_OPENSSL
55};
56
57enum tls_cli_tool use_tool;
58
59/**
60 * Fork child that connects via OpenSSL to our @a port. Allows us to
61 * talk to our port over a socket in @a sp without having to worry
62 * about TLS.
63 *
64 * @param location where the socket is returned
65 * @return -1 on error, otherwise PID of SSL child process
66 */
67static pid_t
68openssl_connect (int *sock,
69 uint16_t port)
70{
71 pid_t chld;
72 int sp[2];
73 char destination[30];
74
75 if (0 != socketpair (AF_UNIX,
76 SOCK_STREAM,
77 0,
78 sp))
79 return -1;
80 chld = fork ();
81 if (0 != chld)
82 {
83 *sock = sp[1];
84 MHD_socket_close_chk_ (sp[0]);
85 return chld;
86 }
87 MHD_socket_close_chk_ (sp[1]);
88 (void) close (0);
89 (void) close (1);
90 dup2 (sp[0], 0);
91 dup2 (sp[0], 1);
92 MHD_socket_close_chk_ (sp[0]);
93 if (TLS_CLI_GNUTLS == use_tool)
94 {
95 snprintf (destination,
96 sizeof(destination),
97 "%u",
98 (unsigned int) port);
99 execlp ("gnutls-cli",
100 "gnutls-cli",
101 "--insecure",
102 "-p",
103 destination,
104 "localhost",
105 (char *) NULL);
106 }
107 else if (TLS_CLI_OPENSSL == use_tool)
108 {
109 snprintf (destination,
110 sizeof(destination),
111 "localhost:%u",
112 (unsigned int) port);
113 execlp ("openssl",
114 "openssl",
115 "s_client",
116 "-connect",
117 destination,
118 "-verify",
119 "0",
120 (char *) NULL);
121 }
122 _exit (1);
123}
124
125
126/**
127 * Test upgrading a connection.
128 *
129 * @param flags which event loop style should be tested
130 * @param pool size of the thread pool, 0 to disable
131 */
132static int
133test_upgrade (int flags,
134 unsigned int pool)
135{
136 struct MHD_Daemon *d;
137 MHD_socket sock;
138 pid_t pid;
139
140 done = 0;
141
142 d = MHD_start_daemon (flags | MHD_USE_DEBUG | MHD_USE_UPGRADE | MHD_USE_TLS,
143 1080,
144 NULL, NULL,
145 &ahc_upgrade, NULL,
146 MHD_OPTION_URI_LOG_CALLBACK, &log_cb, NULL,
147 MHD_OPTION_NOTIFY_COMPLETED, &notify_completed_cb, NULL,
148 MHD_OPTION_NOTIFY_CONNECTION, &notify_connection_cb, NULL,
149 MHD_OPTION_HTTPS_MEM_KEY, srv_signed_key_pem,
150 MHD_OPTION_HTTPS_MEM_CERT, srv_signed_cert_pem,
151 MHD_OPTION_THREAD_POOL_SIZE, pool,
152 MHD_OPTION_END);
153 if (NULL == d)
154 return 2;
155 if (-1 == (pid = openssl_connect (&sock, 1080)))
156 {
157 MHD_stop_daemon (d);
158 return 4;
159 }
160
161 pthread_create (&pt_client,
162 NULL,
163 &run_usock_client,
164 &sock);
165 if (0 == (flags & (MHD_USE_SELECT_INTERNALLY |
166 MHD_USE_THREAD_PER_CONNECTION)) )
167 run_mhd_loop (d, flags);
168 pthread_join (pt_client,
169 NULL);
170 if (0 == (flags & (MHD_USE_SELECT_INTERNALLY |
171 MHD_USE_THREAD_PER_CONNECTION)) )
172 run_mhd_loop (d, flags);
173 pthread_join (pt,
174 NULL);
175 waitpid (pid,
176 NULL,
177 0);
178 MHD_stop_daemon (d);
179 return 0;
180}
181
182
183int
184main (int argc,
185 char *const *argv)
186{
187 int error_count = 0;
188
189 use_tool = TLS_CLI_NO_TOOL;
190 if (0 == system ("gnutls-cli --version 1> /dev/null"))
191 use_tool = TLS_CLI_GNUTLS;
192 else if (0 == system ("openssl version 1> /dev/null"))
193 use_tool = TLS_CLI_OPENSSL;
194 else
195 return 77; /* not possible to test */
196
197 /* try external select */
198 error_count += test_upgrade (0,
199 0);
200#ifdef EPOLL_SUPPORT
201 error_count += test_upgrade (MHD_USE_EPOLL | MHD_USE_TLS,
202 0);
203#endif
204
205 /* Test thread-per-connection */
206 error_count += test_upgrade (MHD_USE_THREAD_PER_CONNECTION,
207 0);
208 error_count += test_upgrade (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL,
209 0);
210
211 /* Test different event loops, with and without thread pool */
212 error_count += test_upgrade (MHD_USE_SELECT_INTERNALLY,
213 0);
214 error_count += test_upgrade (MHD_USE_SELECT_INTERNALLY,
215 2);
216#ifdef HAVE_POLL
217 error_count += test_upgrade (MHD_USE_POLL_INTERNALLY,
218 0);
219 error_count += test_upgrade (MHD_USE_POLL_INTERNALLY,
220 2);
221#endif
222#ifdef EPOLL_SUPPORT
223 error_count += test_upgrade (MHD_USE_EPOLL_INTERNALLY |
224 MHD_USE_TLS,
225 0);
226 error_count += test_upgrade (MHD_USE_EPOLL_INTERNALLY |
227 MHD_USE_TLS,
228 2);
229#endif
230 /* report result */
231 if (0 != error_count)
232 fprintf (stderr,
233 "Error (code: %u)\n",
234 error_count);
235 return error_count != 0; /* 0 == pass */
236}