afb-session: remove initial token
[src/app-framework-binder.git] / src / tests / session / test-session.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <stdarg.h>
4 #include <errno.h>
5 #include <string.h>
6
7 #include <check.h>
8
9 #include "afb-session.h"
10 #include "afb-hook.h"
11
12 #define GOOD_UUID  "123456789012345678901234567890123456"
13 #define BAD_UUID   "1234567890123456789012345678901234567"
14
15 /*********************************************************************/
16 /* check the initialisation */
17 START_TEST (check_initialisation)
18 {
19         ck_assert_int_eq(0, afb_session_init(0, 0));
20         ck_assert_int_eq(0, afb_session_init(200, 0));
21 }
22 END_TEST
23
24 /*********************************************************************/
25 /* check that NULL is a valid value for addref/unref */
26 START_TEST (check_sanity)
27 {
28         struct afb_session *s;
29         s = afb_session_addref(NULL);
30         ck_assert(!s);
31         afb_session_unref(NULL);
32         ck_assert(1);
33 }
34 END_TEST
35
36 /*********************************************************************/
37 /* check creation and retrieval of sessions */
38 START_TEST (check_creation)
39 {
40         char *uuid;
41         struct afb_session *s, *x;
42
43         /* init */
44         ck_assert_int_eq(0, afb_session_init(10, 3600));
45
46         /* create a session */
47         s = afb_session_create(AFB_SESSION_TIMEOUT_DEFAULT);
48         ck_assert(s);
49
50         /* the session is valid */
51         ck_assert(afb_session_uuid(s) != NULL);
52         ck_assert(!afb_session_is_closed(s));
53
54         /* query the session */
55         uuid = strdup(afb_session_uuid(s));
56         x = afb_session_search(uuid);
57         ck_assert(x == s);
58
59         /* still alive after search */
60         afb_session_unref(x);
61         afb_session_unref(s);
62         s = afb_session_search(uuid);
63         ck_assert(s);
64         ck_assert(x == s);
65
66         /* but not after closing */
67         afb_session_close(s);
68         ck_assert(afb_session_is_closed(s));
69         afb_session_unref(s);
70         afb_session_purge();
71         s = afb_session_search(uuid);
72         ck_assert(!s);
73         free(uuid);
74 }
75 END_TEST
76
77 /*********************************************************************/
78 /* check that the maximum capacity is ensured */
79 START_TEST (check_capacity)
80 {
81         struct afb_session *s[3];
82         ck_assert_int_eq(0, afb_session_init(2, 3600));
83         s[0] = afb_session_create(AFB_SESSION_TIMEOUT_DEFAULT);
84         ck_assert(s[0]);
85         s[1] = afb_session_create(AFB_SESSION_TIMEOUT_DEFAULT);
86         ck_assert(s[1]);
87         s[2] = afb_session_create(AFB_SESSION_TIMEOUT_DEFAULT);
88         ck_assert(!s[2]);
89         afb_session_close(s[0]);
90         afb_session_unref(s[0]);
91         s[2] = afb_session_create(AFB_SESSION_TIMEOUT_DEFAULT);
92         ck_assert(s[2]);
93         s[0] = afb_session_create(AFB_SESSION_TIMEOUT_DEFAULT);
94         ck_assert(!s[0]);
95         afb_session_unref(s[0]);
96         afb_session_unref(s[1]);
97         afb_session_unref(s[2]);
98 }
99 END_TEST
100
101 /*********************************************************************/
102 /* check the handling of cookies */
103 void *mkcookie_got;
104 void *mkcookie(void *closure)
105 {
106         mkcookie_got = closure;
107         return closure;
108 }
109
110 void *freecookie_got;
111 void freecookie(void *item)
112 {
113         freecookie_got = item;
114 }
115
116 START_TEST (check_cookies)
117 {
118         char *k[] = { "key1", "key2", "key3", NULL }, *p, *q, *d = "default";
119         struct afb_session *s;
120         int i, j;
121
122         /* init */
123         ck_assert_int_eq(0, afb_session_init(10, 3600));
124
125         /* create a session */
126         s = afb_session_create(AFB_SESSION_TIMEOUT_DEFAULT);
127         ck_assert(s);
128
129         /* set the cookie */
130         for (i = 0 ; k[i] ; i++) {
131                 for (j = 0 ; k[j] ; j++) {
132                         /* retrieve the previous value */
133                         mkcookie_got = freecookie_got = NULL;
134                         p = afb_session_cookie(s, k[j], NULL, NULL, NULL, 0);
135                         if (!p) {
136                                 /* never set (i = 0) */
137                                 q = afb_session_cookie(s, k[j], NULL, NULL, d, 0);
138                                 ck_assert(q == d);
139                                 p = afb_session_cookie(s, k[j], NULL, NULL, NULL, 0);
140                                 ck_assert(!p);
141                         }
142                         q = afb_session_cookie(s, k[j], mkcookie, freecookie, k[i], 1);
143                         ck_assert(q == k[i]);
144                         ck_assert(mkcookie_got == q);
145                         ck_assert(freecookie_got == p);
146                 }
147         }
148
149         /* drop cookies */
150         for (i = 1 ; k[i] ; i++) {
151                 mkcookie_got = freecookie_got = NULL;
152                 p = afb_session_cookie(s, k[i], NULL, NULL, NULL, 0);
153                 ck_assert(!freecookie_got);
154                 q = afb_session_cookie(s, k[i], NULL, NULL, NULL, 1);
155                 ck_assert(!q);
156                 ck_assert(freecookie_got == p);
157         }
158
159         /* closing session */
160         p = afb_session_cookie(s, k[0], NULL, NULL, NULL, 0);
161         mkcookie_got = freecookie_got = NULL;
162         afb_session_close(s);
163         ck_assert(freecookie_got == p);
164         p = afb_session_cookie(s, k[0], NULL, NULL, NULL, 0);
165         ck_assert(!p);
166         afb_session_unref(s);
167 }
168 END_TEST
169
170
171 /*********************************************************************/
172 /* check hooking */
173
174 #if WITH_AFB_HOOK
175 int hookflag;
176
177 void on_create(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
178 {
179         hookflag |= afb_hook_flag_session_create;
180 }
181
182 void on_close(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
183 {
184         hookflag |= afb_hook_flag_session_close;
185 }
186
187 void on_destroy(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
188 {
189         hookflag |= afb_hook_flag_session_destroy;
190 }
191
192 void on_renew(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
193 {
194         hookflag |= afb_hook_flag_session_renew;
195 }
196
197 void on_addref(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
198 {
199         hookflag |= afb_hook_flag_session_addref;
200 }
201
202 void on_unref(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
203 {
204         hookflag |= afb_hook_flag_session_unref;
205 }
206
207 struct afb_hook_session_itf hookitf = {
208         .hook_session_create = on_create,
209         .hook_session_close = on_close,
210         .hook_session_destroy = on_destroy,
211         .hook_session_renew = on_renew,
212         .hook_session_addref = on_addref,
213         .hook_session_unref = on_unref
214 };
215
216 extern void afb_hook_session_create(struct afb_session *session);
217 extern void afb_hook_session_close(struct afb_session *session);
218 extern void afb_hook_session_destroy(struct afb_session *session);
219 extern void afb_hook_session_renew(struct afb_session *session);
220 extern void afb_hook_session_addref(struct afb_session *session);
221 extern void afb_hook_session_unref(struct afb_session *session);
222
223 extern struct afb_hook_session *afb_hook_create_session(const char *pattern, int flags, struct afb_hook_session_itf *itf, void *closure);
224 extern struct afb_hook_session *afb_hook_addref_session(struct afb_hook_session *hook);
225 extern void afb_hook_unref_session(struct afb_hook_session *hook);
226
227
228 START_TEST (check_hooking)
229 {
230         struct afb_hook_session *hs;
231         struct afb_session *s;
232
233         /* init */
234         ck_assert_int_eq(0, afb_session_init(10, 3600));
235
236         /* create the hooking */
237         hs = afb_hook_create_session(NULL, afb_hook_flags_session_all, &hookitf, NULL);
238         ck_assert_ptr_ne(hs, 0);
239
240         /* create a session */
241         hookflag = 0;
242         s = afb_session_create(AFB_SESSION_TIMEOUT_DEFAULT);
243         ck_assert_ptr_ne(s, 0);
244         ck_assert_int_eq(hookflag, afb_hook_flag_session_create);
245
246         /* addref session */
247         hookflag = 0;
248         afb_session_addref(s);
249         ck_assert_int_eq(hookflag, afb_hook_flag_session_addref);
250
251         /* unref session */
252         hookflag = 0;
253         afb_session_unref(s);
254         ck_assert_int_eq(hookflag, afb_hook_flag_session_unref);
255
256         /* renew session token */
257         hookflag = 0;
258         afb_session_new_token(s);
259         ck_assert_int_eq(hookflag, afb_hook_flag_session_renew);
260
261         /* close session */
262         hookflag = 0;
263         afb_session_close(s);
264         ck_assert_int_eq(hookflag, afb_hook_flag_session_close);
265
266         /* unref session */
267         hookflag = 0;
268         afb_session_unref(s);
269         ck_assert_int_eq(hookflag, afb_hook_flag_session_unref);
270
271         /* purge */
272         hookflag = 0;
273         afb_session_purge();
274         ck_assert_int_eq(hookflag, afb_hook_flag_session_destroy);
275
276         /* drop hooks */
277         hookflag = 0;
278         afb_hook_unref_session(hs);
279         s = afb_session_create(AFB_SESSION_TIMEOUT_DEFAULT);
280         ck_assert_ptr_ne(s, 0);
281         ck_assert_int_eq(hookflag, 0);
282         afb_session_unref(s);
283         ck_assert_int_eq(hookflag, 0);
284 }
285 END_TEST
286
287 #endif
288
289 /*********************************************************************/
290
291
292 static Suite *suite;
293 static TCase *tcase;
294
295 void mksuite(const char *name) { suite = suite_create(name); }
296 void addtcase(const char *name) { tcase = tcase_create(name); suite_add_tcase(suite, tcase); }
297 void addtest(TFun fun) { tcase_add_test(tcase, fun); }
298 int srun()
299 {
300         int nerr;
301         SRunner *srunner = srunner_create(suite);
302         srunner_run_all(srunner, CK_NORMAL);
303         nerr = srunner_ntests_failed(srunner);
304         srunner_free(srunner);
305         return nerr;
306 }
307
308 int main(int ac, char **av)
309 {
310         mksuite("session");
311                 addtcase("session");
312                         addtest(check_initialisation);
313                         addtest(check_sanity);
314                         addtest(check_creation);
315                         addtest(check_capacity);
316                         addtest(check_cookies);
317 #if WITH_AFB_HOOK
318                         addtest(check_hooking);
319 #endif
320         return !!srun();
321 }