db2676e43a2be6dab95014d8455fe24785b18185
[strongswan.git] / Source / charon / tester.c
1 /**
2 * @file tester.c
3 *
4 * @brief Test module for automatic testing
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
24 #include <stdlib.h>
25 #include <freeswan.h>
26 #include <pluto/constants.h>
27 #include <pluto/defs.h>
28 #include <string.h>
29 #include <pthread.h>
30
31 #include "tester.h"
32 #include "linked_list.h"
33 #include "thread_pool.h"
34 #include "job_queue.h"
35
36 /**
37 * @brief Private Variables and Functions of tester class
38 *
39 */
40 typedef struct private_tester_s private_tester_t;
41
42 struct private_tester_s {
43 tester_t tester;
44
45 /* Private values */
46 FILE* output;
47 int tests_count;
48 int failed_tests_count;
49 int failed_asserts_count;
50 pthread_mutex_t mutex;
51
52 /* Private functions */
53 /**
54 * @brief is called in a testcase to check a specific situation
55 *
56 * @param this tester object
57 * @param to_be_true assert which has to be true
58 * @param Name of the assertion
59 */
60 void (*assert_true) (private_tester_t *this, bool to_be_true, char *assert_name);
61
62 /**
63 * @brief run a specific test case
64 *
65 * @param this tester object
66 * @param test_function implements the test case
67 * @param Name of the Test
68 */
69 void (*run_test) (private_tester_t *this, void (*test_function) (private_tester_t * tester), char * test_name);
70 };
71
72 /**
73 * @brief Test function to test the linked list class
74 */
75 static void test_linked_list(private_tester_t *this)
76 {
77 void *test_value = NULL;
78
79 linked_list_t *linked_list = linked_list_create();
80 this->assert_true(this,(linked_list->count == 0), "count check");
81
82 linked_list->insert_first(linked_list,"one");
83 this->assert_true(this,(linked_list->count == 1), "count check");
84
85 linked_list->insert_first(linked_list,"two");
86 this->assert_true(this,(linked_list->count == 2), "count check");
87
88 linked_list->insert_first(linked_list,"three");
89 this->assert_true(this,(linked_list->count == 3), "count check");
90
91 linked_list->insert_first(linked_list,"four");
92 this->assert_true(this,(linked_list->count == 4), "count check");
93
94 linked_list->insert_first(linked_list,"five");
95 this->assert_true(this,(linked_list->count == 5), "count check");
96
97 this->assert_true(this,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check");
98 this->assert_true(this,(strcmp((char *) test_value,"five") == 0), "get_first value check");
99 this->assert_true(this,(linked_list->count == 5), "count check");
100
101 this->assert_true(this,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check");
102 this->assert_true(this,(strcmp((char *) test_value,"one") == 0), "get_last value check");
103 this->assert_true(this,(linked_list->count == 5), "count check");
104 this->assert_true(this,(linked_list->remove_first(linked_list,&test_value) == SUCCESS), "remove_first call check");
105 this->assert_true(this,(strcmp((char *) test_value,"five") == 0), "remove_first value check");
106 this->assert_true(this,(linked_list->count == 4), "count check");
107
108 this->assert_true(this,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check");
109 this->assert_true(this,(strcmp((char *) test_value,"four") == 0), "get_first value check");
110 this->assert_true(this,(linked_list->count == 4), "count check");
111
112 this->assert_true(this,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check");
113 this->assert_true(this,(strcmp((char *) test_value,"one") == 0), "get_last value check");
114 this->assert_true(this,(linked_list->count == 4), "count check");
115
116 this->assert_true(this,(linked_list->remove_last(linked_list,&test_value) == SUCCESS), "remove_last call check");
117 this->assert_true(this,(strcmp((char *) test_value,"one") == 0), "remove_last value check");
118 this->assert_true(this,(linked_list->count == 3), "count check");
119
120 this->assert_true(this,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check");
121 this->assert_true(this,(strcmp((char *) test_value,"two") == 0), "get_last value check");
122 this->assert_true(this,(linked_list->count == 3), "count check");
123
124 this->assert_true(this,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check");
125 this->assert_true(this,(strcmp((char *) test_value,"four") == 0), "get_first value check");
126 this->assert_true(this,(linked_list->count == 3), "count check");
127
128 this->assert_true(this,(linked_list->destroy(linked_list) == SUCCESS), "destroy call check");
129 }
130
131 /**
132 * @brief Test function to test the thread pool class
133 */
134 static void test_thread_pool(private_tester_t *this)
135 {
136 size_t desired_pool_size = 10;
137 size_t pool_size;
138
139 thread_pool_t *pool = thread_pool_create(desired_pool_size);
140 pool->get_pool_size(pool, &pool_size);
141 this->assert_true(this, (desired_pool_size == pool_size), "thread creation");
142 pool->destroy(pool);
143 }
144
145 typedef struct job_queue_test_s job_queue_test_t;
146
147 struct job_queue_test_s{
148 private_tester_t *tester;
149 job_queue_t *job_queue;
150 int max_queue_item_count;
151 };
152
153 /**
154 * @brief sender thread used in the the job_queue test function
155 */
156 static void test_job_queue_sender(job_queue_test_t * testinfo)
157 {
158
159 int i;
160
161 for (i = 0; i < testinfo->max_queue_item_count; i++)
162 {
163 int *value = alloc_thing(int,"int");
164 *value = i;
165 job_t *job = job_create(INCOMING_PACKET,value);
166 testinfo->job_queue->add(testinfo->job_queue,job);
167 }
168 }
169
170 /**
171 * @brief receiver thread used in the the job_queue test function
172 */
173 static void test_job_queue_receiver(job_queue_test_t * testinfo)
174 {
175 int i;
176
177 for (i = 0; i < testinfo->max_queue_item_count; i++)
178 {
179 job_t *job;
180 testinfo->tester->assert_true(testinfo->tester,(testinfo->job_queue->get(testinfo->job_queue,&job) == SUCCESS), "get job call check");
181 testinfo->tester->assert_true(testinfo->tester,(job->type == INCOMING_PACKET), "job type check");
182 testinfo->tester->assert_true(testinfo->tester,((*((int *) (job->assigned_data))) == i), "job value check");
183
184 pfree(job->assigned_data);
185 testinfo->tester->assert_true(testinfo->tester,(job->destroy(job) == SUCCESS), "job destroy call check");
186 }
187 }
188
189 /**
190 * @brief Test function test the job_queue functionality
191 */
192 static void test_job_queue(private_tester_t *this)
193 {
194 pthread_t sender_thread, receiver_thread;
195 job_queue_t *job_queue = job_queue_create();
196 job_queue_test_t test_infos;
197 test_infos.tester = this;
198 test_infos.job_queue = job_queue;
199 test_infos.max_queue_item_count = 100;
200
201 pthread_create( &receiver_thread, NULL,(void*(*)(void*)) &test_job_queue_receiver, (void*) &test_infos);
202 pthread_create( &sender_thread, NULL,(void*(*)(void*)) &test_job_queue_sender, (void*) &test_infos);
203
204 pthread_join(sender_thread, NULL);
205 pthread_join(receiver_thread, NULL);
206
207 this->assert_true(this,(job_queue->destroy(job_queue) == SUCCESS), "destroy call check");
208 }
209
210 /**
211 * @brief Testing of all registered tests
212 *
213 * New tests have to be added in this function
214 */
215 static status_t test_all(tester_t *tester)
216 {
217 private_tester_t *this =(private_tester_t*) tester;
218 fprintf(this->output,"Start testing\n");
219
220 /* Add new Tests here! */
221 this->run_test(this,test_linked_list,"Linked List");
222 this->run_test(this,test_thread_pool,"Thread Pool");
223 this->run_test(this,test_job_queue,"Job-Queue");
224
225 fprintf(this->output,"End testing. %d of %d tests succeeded\n",this->tests_count - this->failed_tests_count,this->tests_count);
226
227 return SUCCESS;
228 }
229
230
231 /**
232 * @brief implements the private run_test-Function
233 *
234 */
235 static void run_test(private_tester_t *tester, void (*test_function) (private_tester_t * tester), char * test_name)
236 {
237 private_tester_t *this = tester;
238 this->tests_count++;
239 this->failed_asserts_count = 0;
240 fprintf(this->output,"Start Test '%s'\n", test_name);
241 test_function(this);
242 fprintf(this->output,"End Test '%s'\n", test_name);
243 if (this->failed_asserts_count > 0)
244 {
245 this->failed_tests_count++;
246 }
247 }
248
249 /**
250 * @brief implements the private assert_true-Function
251 *
252 */
253 static void assert_true(private_tester_t *tester, bool to_be_true,char * assert_name)
254 {
255 private_tester_t *this = tester;
256
257 if (assert_name == NULL)
258 {
259 assert_name = "unknown";
260 }
261
262 pthread_mutex_lock(&(this->mutex));
263 if (!to_be_true)
264 {
265 this->failed_asserts_count++;
266 fprintf(this->output," Assert '%s' failed!\n", assert_name);
267 }else
268 {
269 fprintf(this->output," Assert '%s' succeeded\n", assert_name);
270 }
271 pthread_mutex_unlock(&(this->mutex));
272 }
273
274 /**
275 * Implements the destroy function
276 *
277 */
278 static status_t destroy(tester_t *tester)
279 {
280 private_tester_t *this = (private_tester_t*) tester;
281 pthread_mutex_destroy(&(this->mutex));
282 pfree(this);
283 return SUCCESS;
284 }
285
286 tester_t *tester_create(FILE *output)
287 {
288 private_tester_t *this = alloc_thing(private_tester_t, "private_tester_t");
289
290 this->tester.destroy = destroy;
291 this->tester.test_all = test_all;
292 this->run_test = run_test;
293 this->assert_true = assert_true;
294
295 this->failed_tests_count = 0;
296 this->tests_count = 0;
297 this->output = output;
298 pthread_mutex_init(&(this->mutex),NULL);
299
300 return &(this->tester);
301 }