]> git.ipfire.org Git - thirdparty/strongswan.git/blame - programs/charon/testing/ike_sa_manager_test.c
- import of strongswan-2.7.0
[thirdparty/strongswan.git] / programs / charon / testing / ike_sa_manager_test.c
CommitLineData
76b9517b
MW
1/**
2 * @file ike_sa_manager_test.c
3 *
ed37dee6 4 * @brief Tests for the ike_sa_manager_t class.
76b9517b
MW
5 *
6 */
7
8/*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23#include <string.h>
24#include <pthread.h>
25#include <unistd.h>
26
27#include "ike_sa_manager_test.h"
88878242 28
94852e75 29#include <types.h>
96f79ff1 30#include <sa/ike_sa_manager.h>
76b9517b
MW
31
32
33static struct ike_sa_manager_test_struct_s {
51d56047 34 protected_tester_t *tester;
76b9517b 35 ike_sa_manager_t *isam;
d4425c1b 36} td;
76b9517b 37
e395138b 38static void test1_thread(ike_sa_id_t *ike_sa_id)
76b9517b
MW
39{
40 ike_sa_t *ike_sa;
76b9517b
MW
41 status_t status;
42
d4425c1b
MW
43 status = td.isam->checkout(td.isam, ike_sa_id, &ike_sa);
44 td.tester->assert_true(td.tester, (status == SUCCESS), "checkout of a blocked ike_sa");
45 usleep(10000);
46 status = td.isam->checkin(td.isam, ike_sa);
47 td.tester->assert_true(td.tester, (status == SUCCESS), "checkin of a requested ike_sa");
48}
49
e395138b 50
e395138b 51static void test3_thread(ike_sa_id_t *ike_sa_id)
d4425c1b
MW
52{
53 ike_sa_t *ike_sa;
54 status_t status;
76b9517b 55
d4425c1b
MW
56 status = td.isam->checkout(td.isam, ike_sa_id, &ike_sa);
57 td.tester->assert_true(td.tester, (status == NOT_FOUND), "IKE_SA already deleted");
76b9517b
MW
58}
59
e395138b
MW
60
61
62
51d56047 63void test_ike_sa_manager(protected_tester_t *tester)
76b9517b
MW
64{
65 status_t status;
91443667 66 u_int64_t initiator, responder;
e395138b 67 ike_sa_id_t *ike_sa_id, *sa_id;
76b9517b 68 ike_sa_t *ike_sa;
d4425c1b 69 int thread_count = 200;
e395138b 70 int sa_count = 100;
d4425c1b
MW
71 int i;
72 pthread_t threads[thread_count];
73
74 td.tester = tester;
75 td.isam = ike_sa_manager_create();
33bb6b2a 76 tester->assert_true(tester, (td.isam != NULL), "ike_sa_manager creation");
d4425c1b 77
76b9517b 78
76b9517b 79
d4425c1b 80
d4425c1b
MW
81 /* First Test:
82 * we play initiator for IKE_SA_INIT first
83 * create an IKE_SA,
84 *
85 */
76b9517b 86
d048df5c 87 td.isam->create_and_checkout(td.isam, &ike_sa);
d4425c1b
MW
88 /* for testing purposes, we manipulate the responder spi.
89 * this is usually done be the response from the communication partner,
90 * but we don't have one...
91 */
66e379a2 92 responder = 123;
e395138b
MW
93
94 sa_id = ike_sa->get_id(ike_sa);
95 sa_id->set_responder_spi(sa_id, responder);
e829c4ae 96
d0cc48e5 97 ike_sa_id = sa_id->clone(sa_id);
e829c4ae 98
d4425c1b
MW
99 /* check in, so we should have a "completed" sa, specified by ike_sa_id */
100 status = td.isam->checkin(td.isam, ike_sa);
101 tester->assert_true(tester, (status == SUCCESS), "checkin modified IKE_SA");
76b9517b 102
d4425c1b
MW
103 /* now we check it out and start some other threads */
104 status = td.isam->checkout(td.isam, ike_sa_id, &ike_sa);
105 tester->assert_true(tester, (status == SUCCESS), "checkout existing IKE_SA 1");
e395138b 106
d4425c1b
MW
107 for (i = 0; i < thread_count; i++)
108 {
e395138b 109 if (pthread_create(&threads[i], NULL, (void*(*)(void*))test1_thread, (void*)ike_sa_id))
d4425c1b
MW
110 {
111 /* failed, decrease list */
112 thread_count--;
113 i--;
114 }
115 }
116 sleep(1);
76b9517b 117
c0c6490d 118
d4425c1b
MW
119 status = td.isam->checkin(td.isam, ike_sa);
120 tester->assert_true(tester, (status == SUCCESS), "checkin IKE_SA");
121
122
123 sleep(1);
124 /* we now delete the IKE_SA, while it is requested by the threads.
125 * this should block until the have done their work.*/
126 status = td.isam->delete(td.isam, ike_sa_id);
127 tester->assert_true(tester, (status == SUCCESS), "delete IKE_SA by id");
c0c6490d 128
d4425c1b
MW
129
130 for (i = 0; i < thread_count; i++)
131 {
132 pthread_join(threads[i], NULL);
133 }
134
e395138b 135 ike_sa_id->destroy(ike_sa_id);
d4425c1b
MW
136
137
138 /* Second Test:
139 * now we simulate our partner initiates an IKE_SA_INIT,
140 * so we are the responder.
141 *
142 */
d4425c1b
MW
143 memset(&initiator, 0, sizeof(initiator));
144 memset(&responder, 0, sizeof(responder));
145
66e379a2
MW
146 initiator = 123;
147 ike_sa_id = ike_sa_id_create(initiator, responder, TRUE);
d4425c1b
MW
148
149 status = td.isam->checkout(td.isam, ike_sa_id, &ike_sa);
c81eb6e7
JH
150 tester->assert_false(tester, (status == SUCCESS), "checkout unexisting IKE_SA 2");
151
d4425c1b
MW
152 /* let them go acquiring */
153 sleep(1);
154
d4425c1b 155
e395138b 156 ike_sa_id->destroy(ike_sa_id);
c0c6490d 157
d4425c1b
MW
158 /* Third Test:
159 * put in a lot of IKE_SAs, check it out, set a thread waiting
160 * and destroy the manager...
fb8cfc26 161 */
d4425c1b
MW
162 thread_count = sa_count;
163
164 for (i = 0; i < sa_count; i++)
fb8cfc26
MW
165 {
166 td.isam->create_and_checkout(td.isam, &ike_sa);
d4425c1b 167
fb8cfc26 168 if (pthread_create(&threads[i], NULL, (void*(*)(void*))test3_thread, (void*)ike_sa->get_id(ike_sa)))
d4425c1b
MW
169 {
170 /* failed, decrease list */
171 thread_count--;
172 }
d4425c1b
MW
173 }
174
175 /* let them go acquiring */
176 sleep(1);
177
d048df5c 178 td.isam->destroy(td.isam);
d4425c1b
MW
179
180 for (i = 0; i < thread_count; i++)
181 {
182 pthread_join(threads[i], NULL);
183 }
76b9517b 184}
d4425c1b 185