- summary message changed
[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
30 #include "tester.h"
31 #include "linked_list.h"
32 #include "thread_pool.h"
33
34 /**
35 * @brief Private Variables and Functions of tester class
36 *
37 */
38 typedef struct private_tester_s private_tester_t;
39
40 struct private_tester_s {
41 tester_t tester;
42
43 /* Private values */
44 FILE* output;
45 int tests_count;
46 int failed_tests_count;
47 int failed_asserts_count;
48
49 /* Private functions */
50 /**
51 * @brief is called in a testcase to check a specific situation
52 *
53 * @param this tester object
54 * @param to_be_true assert which has to be true
55 * @param Name of the assertion
56 */
57 void (*assert_true) (private_tester_t *this, bool to_be_true, char *assert_name);
58
59 /**
60 * @brief run a specific test case
61 *
62 * @param this tester object
63 * @param test_function implements the test case
64 * @param Name of the Test
65 */
66 void (*run_test) (private_tester_t *this, void (*test_function) (private_tester_t * tester), char * test_name);
67 };
68
69 /**
70 * @brief Test function to test the linked list class
71 */
72 static void test_linked_list(private_tester_t *this)
73 {
74 void *test_value = NULL;
75
76 linked_list_t *linked_list = linked_list_create();
77 this->assert_true(this,(linked_list->count == 0), "count check");
78
79 linked_list->insert_first(linked_list,"one");
80 this->assert_true(this,(linked_list->count == 1), "count check");
81
82 linked_list->insert_first(linked_list,"two");
83 this->assert_true(this,(linked_list->count == 2), "count check");
84
85 linked_list->insert_first(linked_list,"three");
86 this->assert_true(this,(linked_list->count == 3), "count check");
87
88 linked_list->insert_first(linked_list,"four");
89 this->assert_true(this,(linked_list->count == 4), "count check");
90
91 linked_list->insert_first(linked_list,"five");
92 this->assert_true(this,(linked_list->count == 5), "count check");
93
94 this->assert_true(this,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check");
95 this->assert_true(this,(strcmp((char *) test_value,"five") == 0), "get_first value check");
96 this->assert_true(this,(linked_list->count == 5), "count check");
97
98 this->assert_true(this,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check");
99 this->assert_true(this,(strcmp((char *) test_value,"one") == 0), "get_last value check");
100 this->assert_true(this,(linked_list->count == 5), "count check");
101 this->assert_true(this,(linked_list->remove_first(linked_list,&test_value) == SUCCESS), "remove_first call check");
102 this->assert_true(this,(strcmp((char *) test_value,"five") == 0), "remove_first value check");
103 this->assert_true(this,(linked_list->count == 4), "count check");
104
105 this->assert_true(this,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check");
106 this->assert_true(this,(strcmp((char *) test_value,"four") == 0), "get_first value check");
107 this->assert_true(this,(linked_list->count == 4), "count check");
108
109 this->assert_true(this,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check");
110 this->assert_true(this,(strcmp((char *) test_value,"one") == 0), "get_last value check");
111 this->assert_true(this,(linked_list->count == 4), "count check");
112
113 this->assert_true(this,(linked_list->remove_last(linked_list,&test_value) == SUCCESS), "remove_last call check");
114 this->assert_true(this,(strcmp((char *) test_value,"one") == 0), "remove_last value check");
115 this->assert_true(this,(linked_list->count == 3), "count check");
116
117 this->assert_true(this,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check");
118 this->assert_true(this,(strcmp((char *) test_value,"two") == 0), "get_last value check");
119 this->assert_true(this,(linked_list->count == 3), "count check");
120
121 this->assert_true(this,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check");
122 this->assert_true(this,(strcmp((char *) test_value,"four") == 0), "get_first value check");
123 this->assert_true(this,(linked_list->count == 3), "count check");
124
125 this->assert_true(this,(linked_list->destroy(linked_list) == SUCCESS), "destroy call check");
126 }
127
128 /**
129 * @brief Test function to test the thread pool class
130 */
131 static void test_thread_pool(private_tester_t *this)
132 {
133 size_t desired_pool_size = 10;
134 size_t pool_size;
135
136 thread_pool_t *pool = thread_pool_create(desired_pool_size);
137 pool->get_pool_size(pool, &pool_size);
138 this->assert_true(this, (desired_pool_size == pool_size), "thread creation");
139 pool->destroy(pool);
140 }
141
142 /**
143 * @brief Testing of all registered tests
144 *
145 * New tests have to be added in this function
146 */
147 static status_t test_all(tester_t *tester)
148 {
149 private_tester_t *this =(private_tester_t*) tester;
150 fprintf(this->output,"Start testing\n");
151
152 /* Add new Tests here! */
153 this->run_test(this,test_linked_list,"Linked List");
154 this->run_test(this,test_thread_pool,"Thread Pool");
155
156 fprintf(this->output,"End testing. %d of %d tests succeeded\n",this->tests_count - this->failed_tests_count,this->tests_count);
157
158 #ifdef LEAK_DETECTIVE
159 /* Leaks are reported in log file */
160 report_leaks();
161 #endif
162 return SUCCESS;
163 }
164
165
166 /**
167 * @brief implements the private run_test-Function
168 *
169 */
170 static void run_test(private_tester_t *tester, void (*test_function) (private_tester_t * tester), char * test_name)
171 {
172 private_tester_t *this = tester;
173 this->tests_count++;
174 this->failed_asserts_count = 0;
175 fprintf(this->output,"Start Test '%s'\n", test_name);
176 test_function(this);
177 fprintf(this->output,"End Test '%s'\n", test_name);
178 if (this->failed_asserts_count > 0)
179 {
180 this->failed_tests_count++;
181 }
182 }
183
184 /**
185 * @brief implements the private assert_true-Function
186 *
187 */
188 static void assert_true(private_tester_t *tester, bool to_be_true,char * assert_name)
189 {
190 private_tester_t *this = tester;
191
192 if (assert_name == NULL)
193 {
194 assert_name = "unknown";
195 }
196
197 if (!to_be_true)
198 {
199 this->failed_asserts_count++;
200 fprintf(this->output," Assert '%s' failed!\n", assert_name);
201 }else
202 {
203 fprintf(this->output," Assert '%s' succeeded\n", assert_name);
204 }
205 }
206
207 /**
208 * Implements the destroy function
209 *
210 */
211 static status_t destroy(tester_t *this)
212 {
213 pfree(this);
214 return SUCCESS;
215 }
216
217 tester_t *tester_create(FILE *output)
218 {
219 private_tester_t *this = alloc_thing(private_tester_t, "private_tester_t");
220
221 this->tester.destroy = destroy;
222 this->tester.test_all = test_all;
223 this->run_test = run_test;
224 this->assert_true = assert_true;
225
226 this->failed_tests_count = 0;
227 this->tests_count = 0;
228 this->output = output;
229
230 return &(this->tester);
231 }