aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/testcases/ike_sa_manager_test.c
blob: b6fcdc4df3d1a1520676fb229755585098334c33 (plain)
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
/**
 * @file ike_sa_manager_test.c
 * 
 * @brief Tests for the ike_sa_manager_t class.
 * 
 */

/*
 * Copyright (C) 2005 Jan Hutter, Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * 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 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * 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.
 */

#include <string.h>
#include <pthread.h>
#include <unistd.h>

#include "ike_sa_manager_test.h"

#include <types.h>
#include <sa/ike_sa_manager.h>


static struct ike_sa_manager_test_struct_s {
	tester_t *tester;
	ike_sa_manager_t *isam;
} td;

static void test1_thread(ike_sa_id_t *ike_sa_id)
{
	ike_sa_t *ike_sa;
	status_t status;
	
	status = td.isam->checkout(td.isam, ike_sa_id, &ike_sa);
	td.tester->assert_true(td.tester, (status == SUCCESS), "checkout of a blocked ike_sa");
	usleep(10000);
	status = td.isam->checkin(td.isam, ike_sa);
	td.tester->assert_true(td.tester, (status == SUCCESS), "checkin of a requested ike_sa");
}


static void test2_thread(ike_sa_id_t *ike_sa_id)
{
	ike_sa_t *ike_sa;
	status_t status;
	
	status = td.isam->checkout(td.isam, ike_sa_id, &ike_sa);
	td.tester->assert_true(td.tester, (status == NOT_FOUND), "IKE_SA already deleted");	
}

static void test3_thread(ike_sa_id_t *ike_sa_id)
{
	ike_sa_t *ike_sa;
	status_t status;
	
	status = td.isam->checkout(td.isam, ike_sa_id, &ike_sa);
	td.tester->assert_true(td.tester, (status == NOT_FOUND), "IKE_SA already deleted");
	
	ike_sa_id->destroy(ike_sa_id);
}


	

void test_ike_sa_manager(tester_t *tester)
{
	status_t status;
	u_int64_t initiator, responder;
	ike_sa_id_t *ike_sa_id, *sa_id;
	ike_sa_t *ike_sa;
	int thread_count = 200;
	int sa_count = 100;
	int i;
	pthread_t threads[thread_count];
	
	td.tester = tester;
	td.isam = ike_sa_manager_create();
	tester->assert_true(tester, (td.isam != NULL), "ike_sa_manager creation");
	
	
	
	
	/* First Test:
	 * we play initiator for IKE_SA_INIT first 
	 * create an IKE_SA, 
	 * 
	 */

	td.isam->create_and_checkout(td.isam, &ike_sa);
	/* for testing purposes, we manipulate the responder spi.
	 * this is usually done be the response from the communication partner, 
	 * but we don't have one...
	 */
	responder = 123;

	sa_id = ike_sa->get_id(ike_sa);
	sa_id->set_responder_spi(sa_id, responder);	
	
	ike_sa_id = sa_id->clone(sa_id);
		
	/* check in, so we should have a "completed" sa, specified by ike_sa_id */
	status = td.isam->checkin(td.isam, ike_sa);
	tester->assert_true(tester, (status == SUCCESS), "checkin modified IKE_SA");
	
	/* now we check it out and start some other threads */
	status = td.isam->checkout(td.isam, ike_sa_id, &ike_sa);
	tester->assert_true(tester, (status == SUCCESS), "checkout existing IKE_SA 1");
		
	for (i = 0; i < thread_count; i++) 
	{
		if (pthread_create(&threads[i], NULL, (void*(*)(void*))test1_thread, (void*)ike_sa_id))
		{
			/* failed, decrease list */
			thread_count--;
			i--;	
		}
	}
	sleep(1);
	
	
	status = td.isam->checkin(td.isam, ike_sa);
	tester->assert_true(tester, (status == SUCCESS), "checkin IKE_SA");
	
		
	sleep(1);
	/* we now delete the IKE_SA, while it is requested by the threads.
	 * this should block until the have done their work.*/
	status = td.isam->delete(td.isam, ike_sa_id);
	tester->assert_true(tester, (status == SUCCESS), "delete IKE_SA by id");


	for (i = 0; i < thread_count; i++) 
	{
		pthread_join(threads[i], NULL);
	}
	
	ike_sa_id->destroy(ike_sa_id);
	
	
 	/* Second Test:
	 * now we simulate our partner initiates an IKE_SA_INIT,
	 * so we are the responder.
	 * 
	 */
	memset(&initiator, 0, sizeof(initiator));
	memset(&responder, 0, sizeof(responder));
	
	initiator = 123;
	ike_sa_id = ike_sa_id_create(initiator, responder, TRUE);
	
	status = td.isam->checkout(td.isam, ike_sa_id, &ike_sa);
	tester->assert_true(tester, (status == SUCCESS), "checkout unexisting IKE_SA 2");
	for (i = 0; i < thread_count; i++) 
	{
		if (pthread_create(&threads[i], NULL, (void*(*)(void*))test2_thread, (void*)ike_sa_id))
		{
			/* failed, decrease list */
			thread_count--;
			i--;	
		}
	}
	/* let them go acquiring */
	sleep(1);
	
	/* this time, we delete the ike_sa while its checked out */
	td.isam->checkin_and_delete(td.isam, ike_sa);
	tester->assert_true(tester, (status == SUCCESS), "delete IKE_SA by SA");
	
	for (i = 0; i < thread_count; i++) 
	{
		pthread_join(threads[i], NULL);
	}
	
	ike_sa_id->destroy(ike_sa_id);
	
	/* Third Test:
	 * put in a lot of IKE_SAs, check it out, set a thread waiting
	 * and destroy the manager...
	 */
	
	memset(&initiator, 0, sizeof(initiator));
	memset(&responder, 0, sizeof(responder));
	
	thread_count = sa_count;
	
	for (i = 0; i < sa_count; i++) 
	{
		initiator = i + 1;
		ike_sa_id = ike_sa_id_create(initiator, responder, FALSE);
		
		status = td.isam->checkout(td.isam, ike_sa_id, &ike_sa);
		tester->assert_true(tester, (status == SUCCESS), "checkout unexisting IKE_SA 3");

		if (pthread_create(&threads[i], NULL, (void*(*)(void*))test3_thread, (void*)ike_sa_id))
		{
			/* failed, decrease list */
			thread_count--;
		}
	}
	
	/* let them go acquiring */
	sleep(1);
	
	td.isam->destroy(td.isam);
	
	for (i = 0; i < thread_count; i++) 
	{
		pthread_join(threads[i], NULL);
	}
	
	
}