Debian Codegraph @

lemonldap-ng-common/t/60-Session-Cache.t

raw
1 use warnings;
2
3 use Apache::Session::File;
4 use Cache::FileCache;
5 use File::Find;
6 use File::Path;
7 use File::Temp;
8 use JSON;
9 use Test::More;
10
11 BEGIN {
12 use_ok('Lemonldap::NG::Common::Apache::Session');
13 use_ok('Lemonldap::NG::Common::Session');
14 }
15
16 my $dir = File::Temp::tempdir( CLEANUP => 1 );
17 my $sessionsdir = "$dir/sessions";
18 my $cachedir = "$dir/cache";
19
20 mkdir $sessionsdir;
21 mkdir "$sessionsdir/lock";
22 mkdir $cachedir;
23
24 my %session;
25
26 my $cacheOpts = {
27 namespace => 'llng',
28 cache_root => $cachedir,
29 allow_cache_for_root => 1,
30 };
31
32 my $args = {
33 localStorage => 'Cache::FileCache',
34 localStorageOptions => $cacheOpts,
35 backend => 'Apache::Session::File',
36 Directory => $sessionsdir,
37 LockDirectory => "$sessionsdir/lock",
38 };
39
40 ok( tie( %session, 'Lemonldap::NG::Common::Apache::Session', undef, $args ),
41 'Create session' );
42 my $id;
43 ok( $id = $session{_session_id}, "Get session id $id" );
44
45 $session{aa} = 'bb';
46 untie %session;
47
48 my $cache = Cache::FileCache->new($cacheOpts);
49 my $cacheData;
50
51 # Cache corruption
52 $cache->set( $id, '{"a":k' );
53 ok( tie( %session, 'Lemonldap::NG::Common::Apache::Session', $id, $args ),
54 'Get session' );
55 ok( $session{aa} eq 'bb', 'Session is restored' );
56
57 $session{bb} = 'cc';
58 untie %session;
59
60 # Cache deletion
61 rmtree $cachedir;
62 mkdir $cachedir;
63 chmod 0555, $cachedir;
64
65 ok( tie( %session, 'Lemonldap::NG::Common::Apache::Session', $id, $args ),
66 'Get session' );
67 ok( $session{bb} eq 'cc', 'Session is restored' );
68 untie %session;
69
70 chmod 0755, $cachedir;
71
72 ok( tie( %session, 'Lemonldap::NG::Common::Apache::Session', $id, $args ),
73 'Get session' );
74 untie %session;
75
76 ok( unlink("$sessionsdir/$id"), 'Drop session' );
77
78 ok( tie( %session, 'Lemonldap::NG::Common::Apache::Session', $id, $args ),
79 'get session from cache' );
80 ok( $session{bb} eq 'cc', 'cached session is valid' );
81 untie %session;
82
83 rmtree $cachedir;
84 mkdir $cachedir;
85
86 eval { tie( %session, 'Lemonldap::NG::Common::Apache::Session', $id, $args ) };
87 ok( $@, 'Error when session and cache are inexitent' );
88
89 rmtree $dir;
90 done_testing();