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