blob: 5acc336104de98fd555436396293cc082127934d [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*********************************************************************************************************
2* Software License Agreement (BSD License) *
3* Author: Sebastien Decugis <sdecugis@freediameter.net> *
4* *
5* Copyright (c) 2013, WIDE Project and NICT *
6* All rights reserved. *
7* *
8* Redistribution and use of this software in source and binary forms, with or without modification, are *
9* permitted provided that the following conditions are met: *
10* *
11* * Redistributions of source code must retain the above *
12* copyright notice, this list of conditions and the *
13* following disclaimer. *
14* *
15* * Redistributions in binary form must reproduce the above *
16* copyright notice, this list of conditions and the *
17* following disclaimer in the documentation and/or other *
18* materials provided with the distribution. *
19* *
20* * Neither the name of the WIDE Project or NICT nor the *
21* names of its contributors may be used to endorse or *
22* promote products derived from this software without *
23* specific prior written permission of WIDE Project and *
24* NICT. *
25* *
26* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
27* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
28* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
29* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
32* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
33* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
34*********************************************************************************************************/
35
36#include "tests.h"
37
38#define TEST_DIAM_ID "testsess.myid"
39#define TEST_OPT_IN "suffix"
40#define TEST_OPT (os0_t)TEST_OPT_IN
41#define TEST_SID_IN TEST_DIAM_ID ";1234;5678;" TEST_OPT_IN
42#define TEST_SID (os0_t)TEST_SID_IN
43
44#define TEST_EYEC 0x7e57e1ec
45struct sess_state {
46 int eyec; /* TEST_EYEC */
47 os0_t sid; /* the session with which the data was registered */
48 int * freed; /* location where to write the freed status */
49 void * opaque; /* if opaque was provided, this is the value we expect */
50};
51
52static void mycleanup( struct sess_state * data, os0_t sid, void * opaque )
53{
54 /* sanity */
55 CHECK( 1, sid ? 1 : 0 );
56 CHECK( 1, data? 1 : 0 );
57 CHECK( TEST_EYEC, data->eyec );
58 CHECK( 0, strcmp((char *)sid, (char *)data->sid) );
59 if (data->freed)
60 *(data->freed) += 1;
61 if (data->opaque) {
62 CHECK( 1, opaque == data->opaque ? 1 : 0 );
63 }
64 /* Now, free the data */
65 free(data->sid);
66 free(data);
67}
68
69static __inline__ struct sess_state * new_state(os0_t sid, int *freed)
70{
71 struct sess_state *new;
72 new = malloc(sizeof(struct sess_state));
73 CHECK( 1, new ? 1 : 0 );
74 memset(new, 0, sizeof(struct sess_state));
75 new->eyec = TEST_EYEC;
76 new->sid = os0dup(sid, strlen((char *)sid));
77 CHECK( 1, new->sid ? 1 : 0 );
78 new->freed = freed;
79 return new;
80}
81
82void * g_opaque = (void *)"test";
83
84/* Avoid a lot of casts */
85#undef strlen
86#define strlen(s) strlen((char *)s)
87#undef strncmp
88#define strncmp(s1,s2,l) strncmp((char *)s1, (char *)s2, l)
89#undef strcmp
90#define strcmp(s1,s2) strcmp((char *)s1, (char *)s2)
91
92
93/* Main test routine */
94int main(int argc, char *argv[])
95{
96 struct session_handler * hdl1, *hdl2;
97 struct session *sess1, *sess2, *sess3;
98 os0_t str1, str2;
99 size_t str1len, str2len;
100 int new;
101
102 /* First, initialize the daemon modules */
103 INIT_FD();
104
105 /* Test functions related to handlers (simple situation) */
106 {
107 void * testptr = NULL;
108 CHECK( 0, fd_sess_handler_create ( &hdl1, mycleanup, NULL, NULL ) );
109 CHECK( 0, fd_sess_handler_create ( &hdl2, mycleanup, NULL, NULL ) );
110 CHECK( 0, fd_sess_handler_destroy( &hdl2, &testptr ) );
111 CHECK( 1, testptr == NULL ? 1 : 0 );
112 CHECK( 0, fd_sess_handler_create ( &hdl2, mycleanup, NULL, g_opaque ) );
113 #if 0
114 fd_log_debug("%s", fd_sess_dump_hdl(FD_DUMP_TEST_PARAMS, hdl1));
115 fd_log_debug("%s", fd_sess_dump_hdl(FD_DUMP_TEST_PARAMS, hdl2));
116 #endif
117 }
118
119 /* Test Session Id generation (fd_sess_new) */
120 {
121 /* DiamId is provided, not opt */
122 CHECK( 0, fd_sess_new( &sess1, TEST_DIAM_ID, CONSTSTRLEN(TEST_DIAM_ID), NULL, 0 ) );
123 CHECK( 0, fd_sess_new( &sess2, TEST_DIAM_ID, CONSTSTRLEN(TEST_DIAM_ID), NULL, 0 ) );
124 #if 0
125 fd_log_debug("%s", fd_sess_dump(FD_DUMP_TEST_PARAMS, sess1, 1));
126 fd_log_debug("%s", fd_sess_dump(FD_DUMP_TEST_PARAMS, sess2, 1));
127 #endif
128
129 /* Check both string start with the diameter Id, but are different */
130 CHECK( 0, fd_sess_getsid(sess1, &str1, &str1len) );
131 CHECK( 1, (strlen(str1) == str1len) ? 1 : 0 );
132 CHECK( 0, strncmp(str1, TEST_DIAM_ID ";", CONSTSTRLEN(TEST_DIAM_ID) + 1) );
133 CHECK( 0, fd_sess_getsid(sess2, &str2, &str2len) );
134 CHECK( 0, strncmp(str2, TEST_DIAM_ID ";", CONSTSTRLEN(TEST_DIAM_ID) + 1) );
135 CHECK( 1, strcmp(str1, str2) ? 1 : 0 );
136 CHECK( 0, fd_sess_destroy( &sess1 ) );
137 CHECK( 0, fd_sess_destroy( &sess2 ) );
138
139 /* diamId and opt */
140 CHECK( 0, fd_sess_new( &sess1, TEST_DIAM_ID, 0, TEST_OPT, 0 ) );
141 CHECK( 0, fd_sess_new( &sess2, TEST_DIAM_ID, CONSTSTRLEN(TEST_DIAM_ID), TEST_OPT, CONSTSTRLEN(TEST_OPT_IN) - 1 ) );
142 #if 0
143 fd_log_debug("%s", fd_sess_dump(FD_DUMP_TEST_PARAMS, sess1, 1));
144 fd_log_debug("%s", fd_sess_dump(FD_DUMP_TEST_PARAMS, sess2, 1));
145 #endif
146
147 CHECK( 0, fd_sess_getsid(sess1, &str1, &str1len) );
148 CHECK( 0, strncmp(str1, TEST_DIAM_ID ";", CONSTSTRLEN(TEST_DIAM_ID) + 1) );
149 CHECK( 0, strcmp(str1 + str1len - CONSTSTRLEN(TEST_OPT_IN) - 1, ";" TEST_OPT_IN) );
150
151 CHECK( 0, fd_sess_getsid(sess2, &str2, &str2len) );
152 CHECK( 0, strncmp(str2, TEST_DIAM_ID ";", CONSTSTRLEN(TEST_DIAM_ID) + 1) );
153 CHECK( 0, strncmp(str2 + str2len - CONSTSTRLEN(TEST_OPT_IN), ";" TEST_OPT_IN, CONSTSTRLEN(TEST_OPT_IN)) );
154
155 CHECK( 1, strcmp(str1, str2) ? 1 : 0 );
156 CHECK( 0, fd_sess_destroy( &sess1 ) );
157 CHECK( 0, fd_sess_destroy( &sess2 ) );
158
159 /* Now, only opt is provided */
160 CHECK( 0, fd_sess_new( &sess1, NULL, 0, TEST_SID, 0 ) );
161 CHECK( EALREADY, fd_sess_new( &sess2, NULL, 0, TEST_SID, 0 ) );
162 CHECK( sess2, sess1 );
163 CHECK( EALREADY, fd_sess_new( &sess3, NULL, 0, TEST_SID, CONSTSTRLEN(TEST_SID_IN) ) );
164 CHECK( sess3, sess1 );
165 CHECK( 0, fd_sess_new( &sess2, NULL, 0, TEST_SID, CONSTSTRLEN(TEST_SID_IN) - 1 ) );
166 #if 0
167 fd_log_debug("%s", fd_sess_dump(FD_DUMP_TEST_PARAMS, sess1, 1));
168 fd_log_debug("%s", fd_sess_dump(FD_DUMP_TEST_PARAMS, sess2, 1));
169 #endif
170 CHECK( 0, fd_sess_getsid(sess1, &str1, &str1len) );
171 CHECK( 0, fd_sess_getsid(sess2, &str2, &str2len) );
172 CHECK( 0, strncmp( str1, str2, CONSTSTRLEN(TEST_SID_IN) - 1 ) );
173 CHECK( 0, strcmp( str1, TEST_SID ) );
174 CHECK( 1, strcmp( str1, str2 ) ? 1 : 0 );
175
176 CHECK( 0, fd_sess_destroy( &sess2 ) );
177 CHECK( 0, fd_sess_destroy( &sess1 ) );
178 }
179
180 /* Test fd_sess_getcount */
181 {
182 uint32_t cnt;
183 CHECK( 0, fd_sess_new( &sess1, TEST_DIAM_ID, CONSTSTRLEN(TEST_DIAM_ID), NULL, 0 ) );
184 CHECK( 0, fd_sess_new( &sess2, TEST_DIAM_ID, CONSTSTRLEN(TEST_DIAM_ID), NULL, 0 ) );
185 CHECK( 0, fd_sess_getcount(&cnt));
186 CHECK( 2, cnt);
187 CHECK( 0, fd_sess_destroy( &sess2 ) );
188 CHECK( 0, fd_sess_getcount(&cnt));
189 CHECK( 1, cnt);
190 CHECK( 0, fd_sess_destroy( &sess1 ) );
191 CHECK( 0, fd_sess_getcount(&cnt));
192 CHECK( 0, cnt);
193
194 }
195
196 /* Test fd_sess_fromsid */
197 {
198 CHECK( 0, fd_sess_fromsid( TEST_SID, CONSTSTRLEN(TEST_SID_IN), &sess1, &new ) );
199 CHECK( 1, new ? 1 : 0 );
200
201 CHECK( 0, fd_sess_fromsid( TEST_SID, CONSTSTRLEN(TEST_SID_IN), &sess2, &new ) );
202 CHECK( 0, new );
203 CHECK( sess1, sess2 );
204
205 CHECK( 0, fd_sess_fromsid( TEST_SID, CONSTSTRLEN(TEST_SID_IN), &sess3, NULL ) );
206 CHECK( sess1, sess3 );
207
208 CHECK( 0, fd_sess_destroy( &sess1 ) );
209 }
210
211 /* Test fd_sess_reclaim */
212 {
213 struct sess_state *tms;
214
215 CHECK( 0, fd_sess_fromsid( TEST_SID, CONSTSTRLEN(TEST_SID_IN), &sess1, &new ) );
216 CHECK( 1, new ? 1 : 0 );
217
218 CHECK( 0, fd_sess_reclaim( &sess1 ) );
219 CHECK( NULL, sess1 );
220
221 CHECK( 0, fd_sess_fromsid( TEST_SID, CONSTSTRLEN(TEST_SID_IN), &sess1, &new ) );
222 CHECK( 1, new ? 1 : 0 );
223
224 tms = new_state(TEST_SID, NULL);
225 CHECK( 0, fd_sess_state_store ( hdl1, sess1, &tms ) );
226
227 CHECK( 0, fd_sess_reclaim( &sess1 ) );
228 CHECK( NULL, sess1 );
229
230 CHECK( 0, fd_sess_fromsid( TEST_SID, CONSTSTRLEN(TEST_SID_IN), &sess1, &new ) );
231 CHECK( 0, new );
232
233 CHECK( 0, fd_sess_destroy( &sess1 ) );
234
235 CHECK( 0, fd_sess_fromsid( TEST_SID, CONSTSTRLEN(TEST_SID_IN), &sess1, &new ) );
236 CHECK( 1, new ? 1 : 0 );
237
238 CHECK( 0, fd_sess_destroy( &sess1 ) );
239 }
240
241 /* Test timeout function */
242 {
243 struct timespec timeout;
244
245 CHECK( 0, fd_sess_fromsid( TEST_SID, CONSTSTRLEN(TEST_SID_IN), &sess1, &new ) );
246 CHECK( 1, new ? 1 : 0 );
247
248 CHECK( 0, clock_gettime(CLOCK_REALTIME, &timeout) );
249 CHECK( 0, fd_sess_settimeout( sess1, &timeout) ); /* expire now */
250 timeout.tv_sec = 0;
251 timeout.tv_nsec= 50000000; /* 50 ms */
252 CHECK( 0, nanosleep(&timeout, NULL) );
253
254 CHECK( 0, fd_sess_fromsid( TEST_SID, CONSTSTRLEN(TEST_SID_IN), &sess1, &new ) );
255 CHECK( 1, new ? 1 : 0 );
256
257 CHECK( 0, clock_gettime(CLOCK_REALTIME, &timeout) );
258 timeout.tv_sec += 2678500; /* longer that SESS_DEFAULT_LIFETIME */
259 CHECK( 0, fd_sess_settimeout( sess1, &timeout) );
260
261 /* Create a second session */
262 CHECK( 0, fd_sess_new( &sess2, TEST_DIAM_ID, 0, NULL, 0 ) );
263
264 /* We don't really have a way to verify the expiry list is in proper order automatically here... */
265
266 CHECK( 0, fd_sess_destroy( &sess2 ) );
267 CHECK( 0, fd_sess_destroy( &sess1 ) );
268 }
269
270
271 /* Test states operations */
272 {
273 struct sess_state * ms[6], *tms;
274 int freed[6];
275 struct timespec timeout;
276 void * testptr = NULL;
277
278 /* Create three sessions */
279 CHECK( 0, fd_sess_new( &sess1, TEST_DIAM_ID, 0, NULL, 0 ) );
280 CHECK( 0, fd_sess_new( &sess2, TEST_DIAM_ID, 0, NULL, 0 ) );
281 CHECK( 0, fd_sess_new( &sess3, TEST_DIAM_ID, 0, NULL, 0 ) );
282
283 /* Create 2 states */
284 CHECK( 0, fd_sess_getsid(sess1, &str1, &str1len) );
285 freed[0] = 0;
286 ms[0] = new_state(str1, &freed[0]);
287 ms[1] = new_state(str1, NULL);
288
289 tms = ms[0]; /* save a copy */
290 CHECK( 0, fd_sess_state_store ( hdl1, sess1, &ms[0] ) );
291 CHECK( NULL, ms[0] );
292 CHECK( EINVAL, fd_sess_state_store ( hdl1, sess1, NULL ) );
293 CHECK( EALREADY, fd_sess_state_store ( hdl1, sess1, &ms[1] ) );
294 CHECK( 1, ms[1] ? 1 : 0 );
295
296 #if 0
297 fd_log_debug("%s", fd_sess_dump(FD_DUMP_TEST_PARAMS, sess1, 1));
298 #endif
299
300 CHECK( 0, fd_sess_state_retrieve( hdl1, sess1, &ms[0] ) );
301 CHECK( tms, ms[0] );
302 CHECK( 0, freed[0] );
303
304 CHECK( 0, fd_sess_state_retrieve( hdl1, sess2, &tms ) );
305 CHECK( NULL, tms );
306
307 mycleanup(ms[0], str1, NULL);
308 mycleanup(ms[1], str1, NULL);
309
310 /* Now create 6 states */
311 memset(&freed[0], 0, sizeof(freed));
312 CHECK( 0, fd_sess_getsid(sess1, &str1, &str1len) );
313 ms[0] = new_state(str1, &freed[0]);
314 ms[1] = new_state(str1, &freed[1]);
315 CHECK( 0, fd_sess_getsid(sess2, &str1, &str1len) );
316 ms[2] = new_state(str1, &freed[2]);
317 ms[3] = new_state(str1, &freed[3]);
318 CHECK( 0, fd_sess_getsid(sess3, &str1, &str1len) );
319 ms[4] = new_state(str1, &freed[4]);
320 ms[5] = new_state(str1, &freed[5]);
321 ms[5]->opaque = g_opaque;
322 str2 = os0dup(str1, str1len);
323 CHECK( 1, str2 ? 1 : 0 );
324
325 /* Store the six states */
326 CHECK( 0, fd_sess_state_store ( hdl1, sess1, &ms[0] ) );
327 CHECK( 0, fd_sess_state_store ( hdl2, sess1, &ms[1] ) );
328 CHECK( 0, fd_sess_state_store ( hdl1, sess2, &ms[2] ) );
329 CHECK( 0, fd_sess_state_store ( hdl2, sess2, &ms[3] ) );
330 CHECK( 0, fd_sess_state_store ( hdl1, sess3, &ms[4] ) );
331 CHECK( 0, fd_sess_state_store ( hdl2, sess3, &ms[5] ) );
332
333 #if 0
334 fd_log_debug("%s", fd_sess_dump(FD_DUMP_TEST_PARAMS, sess1, 1));
335 fd_log_debug("%s", fd_sess_dump(FD_DUMP_TEST_PARAMS, sess2, 1));
336 fd_log_debug("%s", fd_sess_dump(FD_DUMP_TEST_PARAMS, sess3, 1));
337 #endif
338
339 /* Destroy session 3 */
340 CHECK( 0, fd_sess_destroy( &sess3 ) );
341 CHECK( 0, freed[0] );
342 CHECK( 0, freed[1] );
343 CHECK( 0, freed[2] );
344 CHECK( 0, freed[3] );
345 CHECK( 1, freed[4] );
346 CHECK( 1, freed[5] );
347
348 /* Destroy handler 2 */
349 CHECK( 0, fd_sess_handler_destroy( &hdl2, &testptr ) );
350 CHECK( 0, freed[0] );
351 CHECK( 1, freed[1] );
352 CHECK( 0, freed[2] );
353 CHECK( 1, freed[3] );
354 CHECK( 1, freed[4] );
355 CHECK( 1, freed[5] );
356 CHECK( 1, testptr == g_opaque ? 1 : 0 );
357
358 #if 1
359 fd_log_debug("%s", fd_sess_dump(FD_DUMP_TEST_PARAMS, sess1, 1));
360 fd_log_debug("%s", fd_sess_dump(FD_DUMP_TEST_PARAMS, sess2, 1));
361 #endif
362
363 /* Create again session 3, check that no data is associated to it */
364 CHECK( 0, fd_sess_fromsid( str2, str1len, &sess3, &new ) );
365 CHECK( 1, new ? 1 : 0 );
366 CHECK( 0, fd_sess_state_retrieve( hdl1, sess3, &tms ) );
367 CHECK( NULL, tms );
368 CHECK( 0, fd_sess_destroy( &sess3 ) );
369 free(str2);
370
371 /* Timeout does call cleanups */
372 CHECK( 0, clock_gettime(CLOCK_REALTIME, &timeout) );
373 CHECK( 0, fd_sess_settimeout( sess2, &timeout) );
374 #if 1
375 fd_log_debug("%s", fd_sess_dump(FD_DUMP_TEST_PARAMS, sess1, 1));
376 fd_log_debug("%s", fd_sess_dump(FD_DUMP_TEST_PARAMS, sess2, 1));
377 #endif
378 timeout.tv_sec = 0;
379 timeout.tv_nsec= 50000000; /* 50 ms */
380 CHECK( 0, nanosleep(&timeout, NULL) );
381 CHECK( 0, freed[0] );
382 CHECK( 1, freed[1] );
383 CHECK( 1, freed[2] );
384 CHECK( 1, freed[3] );
385 CHECK( 1, freed[4] );
386 CHECK( 1, freed[5] );
387
388 /* Check the last data can still be retrieved */
389 CHECK( 0, fd_sess_state_retrieve( hdl1, sess1, &tms ) );
390 CHECK( 1, tms ? 1 : 0);
391 CHECK( 0, fd_sess_getsid(sess1, &str1, &str1len) );
392 mycleanup(tms, str1, NULL);
393 }
394
395 /* TODO: add tests on messages referencing sessions */
396
397 /* That's all for the tests yet */
398 PASSTEST();
399}
400