test_options.c (7964B)
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., 51 Franklin Street, Fifth Floor, 18 Boston, MA 02110-1301, USA. 19 */ 20 21 /** 22 * @file test_options.c 23 * @brief Testcase for libmicrohttpd HTTPS GET operations 24 * @author Sagie Amir 25 */ 26 27 #include "platform.h" 28 #include "microhttpd.h" 29 #include "mhd_sockets.h" 30 31 static enum MHD_Result 32 ahc_echo (void *cls, 33 struct MHD_Connection *connection, 34 const char *url, 35 const char *method, 36 const char *version, 37 const char *upload_data, size_t *upload_data_size, 38 void **req_cls) 39 { 40 (void) cls; 41 (void) connection; 42 (void) url; 43 (void) method; 44 (void) version; 45 (void) upload_data; 46 (void) upload_data_size; 47 (void) req_cls; 48 49 return 0; 50 } 51 52 53 static unsigned int 54 test_wrap_loc (const char *test_name, unsigned int (*test)(void)) 55 { 56 unsigned int ret; 57 58 fprintf (stdout, "running test: %s ", test_name); 59 ret = test (); 60 if (ret == 0) 61 { 62 fprintf (stdout, "[pass]\n"); 63 } 64 else 65 { 66 fprintf (stdout, "[fail]\n"); 67 } 68 return ret; 69 } 70 71 72 /** 73 * Test daemon initialization with the MHD_OPTION_SOCK_ADDR or 74 * the MHD_OPTION_SOCK_ADDR_LEN options 75 */ 76 static unsigned int 77 test_ip_addr_option (void) 78 { 79 struct MHD_Daemon *d; 80 const union MHD_DaemonInfo *dinfo; 81 struct sockaddr_in daemon_ip_addr; 82 uint16_t port4; 83 #if defined(HAVE_INET6) && defined(USE_IPV6_TESTING) 84 struct sockaddr_in6 daemon_ip_addr6; 85 uint16_t port6; 86 #endif 87 unsigned int ret; 88 89 memset (&daemon_ip_addr, 0, sizeof (struct sockaddr_in)); 90 daemon_ip_addr.sin_family = AF_INET; 91 daemon_ip_addr.sin_port = 0; 92 daemon_ip_addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); 93 port4 = 0; 94 95 #if defined(HAVE_INET6) && defined(USE_IPV6_TESTING) 96 memset (&daemon_ip_addr6, 0, sizeof (struct sockaddr_in6)); 97 daemon_ip_addr6.sin6_family = AF_INET6; 98 daemon_ip_addr6.sin6_port = 0; 99 daemon_ip_addr6.sin6_addr = in6addr_loopback; 100 port6 = 0; 101 #endif 102 103 ret = 0; 104 105 d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_NO_THREAD_SAFETY, 0, 106 NULL, NULL, &ahc_echo, NULL, 107 MHD_OPTION_SOCK_ADDR, &daemon_ip_addr, 108 MHD_OPTION_END); 109 110 if (d == 0) 111 return 1; 112 113 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 114 if (NULL == dinfo) 115 ret |= 1 << 1; 116 else 117 port4 = dinfo->port; 118 119 MHD_stop_daemon (d); 120 121 122 daemon_ip_addr.sin_port = htons (port4); 123 d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_NO_THREAD_SAFETY, 0, 124 NULL, NULL, &ahc_echo, NULL, 125 MHD_OPTION_SOCK_ADDR_LEN, 126 (socklen_t) sizeof(daemon_ip_addr), &daemon_ip_addr, 127 MHD_OPTION_END); 128 129 if (d == 0) 130 return 1; 131 132 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 133 if (NULL == dinfo) 134 ret |= 1 << 1; 135 else if (port4 != dinfo->port) 136 ret |= 1 << 2; 137 138 MHD_stop_daemon (d); 139 140 141 d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_NO_THREAD_SAFETY, 0, 142 NULL, NULL, &ahc_echo, NULL, 143 MHD_OPTION_SOCK_ADDR_LEN, 144 (socklen_t) (sizeof(daemon_ip_addr) / 2), 145 &daemon_ip_addr, 146 MHD_OPTION_END); 147 148 if (NULL != d) 149 { 150 MHD_stop_daemon (d); 151 return 1 << 3; 152 } 153 154 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN 155 156 daemon_ip_addr.sin_len = (socklen_t) sizeof(daemon_ip_addr); 157 d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_NO_THREAD_SAFETY, 0, 158 NULL, NULL, &ahc_echo, NULL, 159 MHD_OPTION_SOCK_ADDR_LEN, 160 (socklen_t) sizeof(daemon_ip_addr), &daemon_ip_addr, 161 MHD_OPTION_END); 162 163 if (d == 0) 164 return 1; 165 166 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 167 if (NULL == dinfo) 168 ret |= 1 << 1; 169 else if (port4 != dinfo->port) 170 ret |= 1 << 2; 171 172 MHD_stop_daemon (d); 173 174 175 daemon_ip_addr.sin_len = (socklen_t) (sizeof(daemon_ip_addr) / 2); 176 d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_NO_THREAD_SAFETY, 0, 177 NULL, NULL, &ahc_echo, NULL, 178 MHD_OPTION_SOCK_ADDR_LEN, 179 (socklen_t) sizeof(daemon_ip_addr), &daemon_ip_addr, 180 MHD_OPTION_END); 181 182 if (NULL != d) 183 { 184 MHD_stop_daemon (d); 185 return 1 << 3; 186 } 187 188 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ 189 190 191 #if defined(HAVE_INET6) && defined(USE_IPV6_TESTING) 192 d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_IPv6 193 | MHD_USE_NO_THREAD_SAFETY, 0, 194 NULL, NULL, &ahc_echo, NULL, 195 MHD_OPTION_SOCK_ADDR, &daemon_ip_addr6, 196 MHD_OPTION_END); 197 198 if (d == 0) 199 return 1; 200 201 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 202 if (NULL == dinfo) 203 ret |= 1 << 1; 204 else 205 port6 = dinfo->port; 206 207 MHD_stop_daemon (d); 208 209 210 daemon_ip_addr6.sin6_port = htons (port6); 211 d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_NO_THREAD_SAFETY, 0, 212 NULL, NULL, &ahc_echo, NULL, 213 MHD_OPTION_SOCK_ADDR_LEN, 214 (socklen_t) sizeof(daemon_ip_addr6), &daemon_ip_addr6, 215 MHD_OPTION_END); 216 217 if (d == 0) 218 return 1; 219 220 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 221 if (NULL == dinfo) 222 ret |= 1 << 1; 223 else if (port6 != dinfo->port) 224 ret |= 1 << 2; 225 226 MHD_stop_daemon (d); 227 228 229 d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_NO_THREAD_SAFETY, 0, 230 NULL, NULL, &ahc_echo, NULL, 231 MHD_OPTION_SOCK_ADDR_LEN, 232 (socklen_t) (sizeof(daemon_ip_addr6) / 2), 233 &daemon_ip_addr6, 234 MHD_OPTION_END); 235 236 if (NULL != d) 237 { 238 MHD_stop_daemon (d); 239 return 1 << 3; 240 } 241 242 #if defined(HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN) 243 244 daemon_ip_addr6.sin6_len = (socklen_t) sizeof(daemon_ip_addr6); 245 d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_NO_THREAD_SAFETY, 0, 246 NULL, NULL, &ahc_echo, NULL, 247 MHD_OPTION_SOCK_ADDR_LEN, 248 (socklen_t) sizeof(daemon_ip_addr6), &daemon_ip_addr6, 249 MHD_OPTION_END); 250 251 if (d == 0) 252 return 1; 253 254 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 255 if (NULL == dinfo) 256 ret |= 1 << 1; 257 else if (port6 != dinfo->port) 258 ret |= 1 << 2; 259 260 MHD_stop_daemon (d); 261 262 263 daemon_ip_addr6.sin6_len = (socklen_t) (sizeof(daemon_ip_addr6) / 2); 264 d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_NO_THREAD_SAFETY, 0, 265 NULL, NULL, &ahc_echo, NULL, 266 MHD_OPTION_SOCK_ADDR_LEN, 267 (socklen_t) sizeof(daemon_ip_addr6), &daemon_ip_addr6, 268 MHD_OPTION_END); 269 270 if (NULL != d) 271 { 272 MHD_stop_daemon (d); 273 return 1 << 3; 274 } 275 276 #endif /* HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN */ 277 #endif /* HAVE_INET6 && USE_IPV6_TESTING */ 278 279 return ret; 280 } 281 282 283 /* setup a temporary transfer test file */ 284 int 285 main (int argc, char *const *argv) 286 { 287 unsigned int errorCount = 0; 288 (void) argc; (void) argv; /* Unused. Silent compiler warning. */ 289 290 errorCount += test_wrap_loc ("ip addr option", &test_ip_addr_option); 291 292 return errorCount != 0; 293 }