avoid memory leak
[gnutls.git] / tests / mini-handshake-timeout.c
blob30d899fb551fcde73709e648ea19d94bea43139a
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 int counter;
71 static int packet_to_lose;
72 gnutls_session_t session;
74 static void
75 client (int fd, int wait)
77 int ret;
78 gnutls_anon_client_credentials_t anoncred;
79 /* Need to enable anonymous KX specifically. */
81 gnutls_global_init ();
83 if (debug)
85 gnutls_global_set_log_function (client_log_func);
86 gnutls_global_set_log_level (4711);
89 gnutls_anon_allocate_client_credentials (&anoncred);
91 /* Initialize TLS session
93 gnutls_init (&session, GNUTLS_CLIENT);
94 gnutls_handshake_set_timeout( session, 10*1000);
96 /* Use default priorities */
97 gnutls_priority_set_direct (session, "NORMAL:+ANON-ECDH", NULL);
99 /* put the anonymous credentials to the current session
101 gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
103 counter = 0;
105 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) fd);
107 /* Perform the TLS handshake
111 ret = gnutls_handshake (session);
113 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
115 gnutls_deinit(session);
116 gnutls_anon_free_client_credentials(anoncred);
117 gnutls_global_deinit();
119 if (ret < 0)
121 if (ret != GNUTLS_E_TIMEDOUT || wait == 0)
123 if (debug) fail("client: unexpected error: %s\n", gnutls_strerror(ret));
124 exit(1);
126 if (debug) success("client: expected timeout occured\n");
127 exit(0);
129 else
131 if (wait != 0)
133 fail ("client: handshake was completed unexpectedly\n");
134 gnutls_perror (ret);
135 exit(1);
139 exit(0);
143 /* These are global */
144 gnutls_anon_server_credentials_t anoncred;
145 pid_t child;
147 static gnutls_session_t
148 initialize_tls_session (void)
150 gnutls_session_t session;
152 gnutls_init (&session, GNUTLS_SERVER);
154 /* avoid calling all the priority functions, since the defaults
155 * are adequate.
157 gnutls_priority_set_direct (session, "NORMAL:+ANON-ECDH", NULL);
159 gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
161 return session;
164 static void
165 server (int fd, int wait)
167 int ret;
168 /* this must be called once in the program
170 gnutls_global_init ();
172 if (debug)
174 gnutls_global_set_log_function (server_log_func);
175 gnutls_global_set_log_level (4711);
178 gnutls_anon_allocate_server_credentials (&anoncred);
180 session = initialize_tls_session ();
182 counter = 0;
184 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) fd);
186 if (wait) sleep(15);
187 else do
189 ret = gnutls_handshake (session);
191 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
193 gnutls_deinit (session);
194 gnutls_anon_free_server_credentials(anoncred);
195 gnutls_global_deinit();
197 if (ret < 0)
199 return;
203 static void start (int wait)
205 int fd[2];
206 int ret;
208 if (debug && wait)
209 fprintf(stderr, "\nWill test timeout\n");
211 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
212 if (ret < 0)
214 perror("socketpair");
215 exit(1);
218 child = fork ();
219 if (child < 0)
221 perror ("fork");
222 fail ("fork");
223 exit(1);
226 if (child)
228 /* parent */
229 close(fd[1]);
230 server (fd[0], wait);
231 close(fd[0]);
233 else
235 close(fd[0]);
236 client (fd[1], wait);
237 close(fd[1]);
238 exit(0);
242 static void ch_handler(int sig)
244 int status;
245 wait(&status);
246 if (WEXITSTATUS(status) != 0)
247 fail("Child died with status %d\n", WEXITSTATUS(status));
248 return;
251 void
252 doit (void)
254 signal(SIGCHLD, ch_handler);
256 /* make sure that normal handshake occurs */
257 start(0);
259 /* check the handshake with an expected timeout */
260 start(1);
263 #endif /* _WIN32 */