gnutls-cli-debug uses server name indication.
[gnutls.git] / tests / mini-handshake-timeout.c
blobee4541bb2dfd693aae1c58090e48dde0fd23a57d
1 /*
2 * Copyright (C) 2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * GnuTLS is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * GnuTLS is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with GnuTLS; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #if defined(_WIN32)
33 int main()
35 exit(77);
38 #else
40 #include <sys/types.h>
41 #include <netinet/in.h>
42 #include <sys/socket.h>
43 #include <sys/wait.h>
44 #include <arpa/inet.h>
45 #include <unistd.h>
46 #include <gnutls/gnutls.h>
47 #include <gnutls/dtls.h>
48 #include <signal.h>
50 #include "utils.h"
52 /* This program tests whether the handshake timeout value is enforced.
55 static void
56 server_log_func (int level, const char *str)
58 fprintf (stderr, "server|<%d>| %s", level, str);
61 static void
62 client_log_func (int level, const char *str)
64 fprintf (stderr, "client|<%d>| %s", level, str);
67 /* A very basic TLS client, with anonymous authentication.
70 static void
71 client (int fd, int wait)
73 int ret;
74 gnutls_anon_client_credentials_t anoncred;
75 gnutls_session_t session;
76 /* Need to enable anonymous KX specifically. */
78 gnutls_global_init ();
80 if (debug)
82 gnutls_global_set_log_function (client_log_func);
83 gnutls_global_set_log_level (4711);
86 gnutls_anon_allocate_client_credentials (&anoncred);
88 /* Initialize TLS session
90 gnutls_init (&session, GNUTLS_CLIENT);
91 gnutls_handshake_set_timeout( session, 20*1000);
93 /* Use default priorities */
94 gnutls_priority_set_direct (session, "NORMAL:+ANON-ECDH", NULL);
96 /* put the anonymous credentials to the current session
98 gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
100 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) fd);
102 /* Perform the TLS handshake
106 ret = gnutls_handshake (session);
108 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
110 gnutls_deinit(session);
111 gnutls_anon_free_client_credentials(anoncred);
112 gnutls_global_deinit();
114 if (ret < 0)
116 if (ret != GNUTLS_E_TIMEDOUT || wait == 0)
118 if (debug) fail("client: unexpected error: %s\n", gnutls_strerror(ret));
119 exit(1);
121 if (debug) success("client: expected timeout occured\n");
122 exit(0);
124 else
126 if (wait != 0)
128 fail ("client: handshake was completed unexpectedly\n");
129 gnutls_perror (ret);
130 exit(1);
134 exit(0);
137 static void
138 initialize_tls_session (gnutls_session_t * session)
140 gnutls_init (session, GNUTLS_SERVER);
142 /* avoid calling all the priority functions, since the defaults
143 * are adequate.
145 gnutls_priority_set_direct (*session, "NORMAL:+ANON-ECDH", NULL);
148 static void
149 server (int fd, int wait)
151 int ret;
152 gnutls_session_t session;
153 gnutls_anon_server_credentials_t anoncred;
155 /* this must be called once in the program
157 gnutls_global_init ();
159 if (debug)
161 gnutls_global_set_log_function (server_log_func);
162 gnutls_global_set_log_level (4711);
165 gnutls_anon_allocate_server_credentials (&anoncred);
167 initialize_tls_session (&session);
168 gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
170 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) fd);
172 if (wait) sleep(25);
173 else do
175 ret = gnutls_handshake (session);
177 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
179 gnutls_deinit (session);
180 gnutls_anon_free_server_credentials(anoncred);
181 gnutls_global_deinit();
184 static void start (int wait)
186 int fd[2];
187 int ret;
188 pid_t child;
190 if (debug && wait)
191 fprintf(stderr, "\nWill test timeout\n");
193 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
194 if (ret < 0)
196 perror("socketpair");
197 exit(1);
200 child = fork ();
201 if (child < 0)
203 perror ("fork");
204 fail ("fork");
205 exit(1);
208 if (child)
210 /* parent */
211 close(fd[1]);
212 server (fd[0], wait);
213 close(fd[0]);
215 else
217 close(fd[0]);
218 client (fd[1], wait);
219 close(fd[1]);
220 exit(0);
224 static void ch_handler(int sig)
226 int status;
227 wait(&status);
228 if (WEXITSTATUS(status) != 0)
229 fail("Child died with status %d\n", WEXITSTATUS(status));
230 return;
233 void
234 doit (void)
236 signal(SIGCHLD, ch_handler);
238 /* make sure that normal handshake occurs */
239 start(0);
241 /* check the handshake with an expected timeout */
242 start(1);
245 #endif /* _WIN32 */