aboutsummaryrefslogtreecommitdiff
path: root/src/testspdy/test_session_timeout.c
blob: a89933b492aed84c27481a8fb0b9ec551ee916ab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
    This file is part of libmicrospdy
    Copyright (C) 2013 Andrey Uzunov

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file session_timeout.c
 * @brief  tests closing sessions after set timeout. Openssl is used for
 * 			client
 * @author Andrey Uzunov
 */

#include "platform.h"
#include "microspdy.h"
#include "stdio.h"
#include <sys/wait.h>
#include <ctype.h>
#include "common.h"
#include <sys/time.h>
#include <sys/stat.h>

#define TIMEOUT 2
#define SELECT_MS_TIMEOUT 20

int port;

pid_t parent;
pid_t child;

int run = 1;
int chunk_size=1;
int new_session;
int closed_session;
int do_sleep;

void
killchild(char *msg)
{
	printf("%s\n",msg);
	kill(child, SIGKILL);
	exit(1);
}

void
killparent(char *msg)
{
	printf("%s\n",msg);
	kill(parent, SIGKILL);
	_exit(1);
}

void
new_session_cb (void *cls,
						struct SPDY_Session * session)
{
  (void)cls;
  (void)session;
  
	if(!new_session)do_sleep = 1;
	new_session = 1;
	printf("new session\n");
}

void
closed_session_cb (void *cls,
						struct SPDY_Session * session,
						int by_client)
{
  (void)cls;
  (void)session;
  
	printf("closed_session_cb called\n");
	
	if(SPDY_YES == by_client)
	{
		killchild("closed by the client");
	}
	if(closed_session)
	{
		killchild("closed_session_cb called twice");
	}
	
	closed_session = 1;
}

int
parentproc()
{	
	int childstatus;
	unsigned long long timeoutlong=0;
	struct timeval timeout;
	struct timespec ts;
	int ret;
	fd_set read_fd_set;
	fd_set write_fd_set;
	fd_set except_fd_set;
	int maxfd = -1;
	struct SPDY_Daemon *daemon;
  unsigned long long  beginning = 0;
	unsigned long long now;
  
	SPDY_init();
	
	daemon = SPDY_start_daemon(port,
								DATA_DIR "cert-and-key.pem",
								DATA_DIR "cert-and-key.pem",
								&new_session_cb,
								&closed_session_cb,
								NULL,
								NULL,
								NULL,
								SPDY_DAEMON_OPTION_SESSION_TIMEOUT,
								TIMEOUT,
								SPDY_DAEMON_OPTION_END);
	
	if(NULL==daemon){
		printf("no daemon\n");
		return 1;
	}

	do
	{
		do_sleep=0;
		FD_ZERO(&read_fd_set);
		FD_ZERO(&write_fd_set);
		FD_ZERO(&except_fd_set);

		ret = SPDY_get_timeout(daemon, &timeoutlong);
		
		if(new_session && !closed_session)
		{
			if(SPDY_NO == ret)
			{
				killchild("SPDY_get_timeout returned wrong SPDY_NO");
			}
			/*if(timeoutlong)
			{
				killchild("SPDY_get_timeout returned wrong timeout");
			}*/
      if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
        now = ts.tv_nsec / 1000000 + ts.tv_sec*1000;
      else
				killchild("clock_gettime returned wrong value");
      if(now - beginning > TIMEOUT*1000 + SELECT_MS_TIMEOUT)
      {
        printf("Started at: %llums\n",beginning);
        printf("Now is: %llums\n",now);
        printf("Timeout is: %i\n",TIMEOUT);
        printf("Select Timeout is: %ims\n",SELECT_MS_TIMEOUT);
        printf("SPDY_get_timeout gave: %llums\n",timeoutlong);
				killchild("Timeout passed but session was not closed");
      }
      if(timeoutlong > beginning + TIMEOUT *1000)
      {
        printf("Started at: %llums\n",beginning);
        printf("Now is: %llums\n",now);
        printf("Timeout is: %i\n",TIMEOUT);
        printf("Select Timeout is: %ims\n",SELECT_MS_TIMEOUT);
        printf("SPDY_get_timeout gave: %llums\n",timeoutlong);
				killchild("SPDY_get_timeout returned wrong timeout");
      }
		}
		else
		{
			if(SPDY_YES == ret)
			{
				killchild("SPDY_get_timeout returned wrong SPDY_YES");
			}
		}
		
		if(SPDY_NO == ret || timeoutlong >= 1000)
		{
			timeout.tv_sec = 1;
      timeout.tv_usec = 0;
		}
		else
		{
			timeout.tv_sec = timeoutlong / 1000;
      timeout.tv_usec = (timeoutlong % 1000) * 1000;
		}
    
    //ignore values
    timeout.tv_sec = 0;
    timeout.tv_usec = SELECT_MS_TIMEOUT * 1000;
		
		maxfd = SPDY_get_fdset (daemon,
								&read_fd_set,
								&write_fd_set, 
								&except_fd_set);
								
		ret = select(maxfd+1, &read_fd_set, &write_fd_set, &except_fd_set, &timeout);
		
		switch(ret) {
			case -1:
				printf("select error: %i\n", errno);
				break;
			case 0:
				/*if(new_session)
				{
					killchild("select returned wrong number");
				}*/
				break;
			default:
				SPDY_run(daemon);
        if(0 == beginning)
        {
          if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
            beginning = ts.tv_nsec / 1000000 + ts.tv_sec*1000;
          else
            killchild("clock_gettime returned wrong number");
        }
				/*if(do_sleep)
				{
					sleep(TIMEOUT);
					do_sleep = 0;
				}*/
			break;
		}
	}
	while(waitpid(child,&childstatus,WNOHANG) != child);
	
	if(!new_session || !closed_session)
	{
		killchild("child is dead, callback wasn't called");
	}

	ret = SPDY_get_timeout(daemon, &timeoutlong);
		
	if(SPDY_YES == ret)
	{
		killchild("SPDY_get_timeout returned wrong SPDY_YES after child died");
	}
			
	SPDY_stop_daemon(daemon);
	
	SPDY_deinit();
	
	return 0;
}

int
childproc()
{
	pid_t devnull;
	pid_t out;

	out=dup(1);
	//close(0);
	close(1);
	close(2);
	/*devnull = open("/dev/null", O_RDONLY);
	if (0 != devnull)
	{
		dup2(devnull, 0);
		close(devnull);
	}*/
	devnull = open("/dev/null", O_WRONLY);
	if (1 != devnull)
	{
		dup2(devnull, 1);
		close(devnull);
	}
	devnull = open("/dev/null", O_WRONLY);
	if (2 != devnull)
	{
		dup2(devnull, 2);
		close(devnull);
	}
	char *uri;
	asprintf (&uri, "127.0.0.1:%i", port);
	execlp ("openssl", "openssl", "s_client", "-connect", uri, NULL);
	close(1);
	dup2(out,1);
	close(out);
	killparent ("executing openssl failed");
	return 1;
}


int
main()
{
	port = get_port(11123);
	parent = getpid();

	child = fork();
	if (-1 == child)
	{   
		fprintf(stderr, "can't fork, error %d\n", errno);
		exit(EXIT_FAILURE);
	}

	if (child == 0)
	{
		int ret = childproc();
		_exit(ret);
	}
	else
	{ 
		int ret = parentproc();
		exit(ret);
	}
	return 1;
}