File: 14-Lemonldap-NG-Handler-Rule-Building.t

package info (click to toggle)
lemonldap-ng 2.21.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 28,024 kB
  • sloc: perl: 77,414; javascript: 25,284; xml: 6,473; makefile: 1,303; sh: 453; sql: 159; python: 53; php: 26
file content (195 lines) | stat: -rw-r--r-- 5,887 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl Lemonldap-NG-Handler-SharedConf.t'

#########################

# change 'tests => 1' to 'tests => last_test_to_print';

package main;
use strict;
use warnings;
require 't/test.pm';

use Test::More tests => 4;
BEGIN { use_ok('Lemonldap::NG::Handler::Main') }

# get a standard basic configuration in $args hashref
use Cwd 'abs_path';
use File::Basename;
use lib dirname( abs_path $0 );

#########################

# Insert your test code below, the Test::More module is used here so read
# its man page (perldoc Test::More) for help writing this test script.
my $h;
$h = 'Lemonldap::NG::Handler::Test';
$ENV{SERVER_NAME} = "test1.example.com";

#open STDERR, '>/dev/null';

my $conf = {
    cfgNum        => 1,
    logLevel      => 'error',
    portal        => 'http://auth.example.com/',
    globalStorage => 'Apache::Session::File',
    post          => {},
    key           => 1,
    useSafeJail   => 0,
    locationRules => {
        'test1.example.com' => {

            # Basic rules
            'default' => 'accept',
            '^/no'    => 'deny',
            'test'    => '$groups =~ /\badmin\b/',

            # Bad ordered rules
            '^/a/a' => 'deny',
            '^/a'   => 'accept',

            # Good ordered rules
            '(?#1 first)^/b/a' => 'deny',
            '(?#2 second)^/b'  => 'accept',
        },
    },
};

#########################
# Make sure that the rule building idiom (substitute/buildRule) yields
# consistent results in edge cases see #2595

sub compileRule {
    my $rule = shift;
    return $h->buildSub( $h->substitute($rule) );
}

sub runTests {
    my ($h) = @_;

    # Undef expression yields a sub that returns undef
    my $r = compileRule(undef);
    is( ref($r), "CODE", "Returned code ref" );
    is( $r->(),  undef,  "Returned undef" );

    # empty expression yields a sub that returns undef
    $r = compileRule("");
    is( ref($r), "CODE", "Returned code ref" );
    is( $r->(),  undef,  "Returned undef" );

    # empty string yields a sub that returns undef
    $r = compileRule("\"\"");
    is( ref($r), "CODE", "Returned code ref" );
    is( $r->(),  "",     "Returned empty string" );

    # 0 expression returns a sub that returns 0
    $r = compileRule("0");
    is( ref($r), "CODE", "Returned code ref" );
    is( $r->(),  0,      "Returned 0" );

    # string expression returns a sub that returns the string
    #
    $r = compileRule("\"abc def\"");
    is( ref($r), "CODE",    "Returned code ref" );
    is( $r->(),  "abc def", "Returned abc def" );

    # Access sessionInfo
    $r = compileRule('$foo');
    is( ref($r),                      "CODE", "Returned code ref" );
    is( $r->( {}, { foo => "bar" } ), "bar",  "Returned bar" );

    # Access request
    $r = compileRule('$ENV{foo}');
    is( ref($r),                                 "CODE", "Returned code ref" );
    is( $r->( { env => { foo => "bar" } }, {} ), "bar",  "Returned bar" );

    # inSubnet
    $r = compileRule("inSubnet('127.0.0.1/8')");
    is( ref($r), "CODE", "Returned code ref" );

    is( $r->( { env => { REMOTE_ADDR => "127.0.0.1" } }, {} ),
        "1", "ipInSubnet works" );
    is( $r->( { env => { REMOTE_ADDR => "192.168.0.1" } }, {} ),
        "0", "ipInSubnet works" );

    $r = compileRule("inSubnet('127.0.0.1/8', '192.168.0.0/16')");
    is( ref($r), "CODE", "Returned code ref" );

    is( $r->( { env => { REMOTE_ADDR => "192.168.0.1" } }, {} ),
        "1", "ipInSubnet works" );

    # ipInSubnet
    $r = compileRule("ipInSubnet(\$ENV{X_FORWARDED_FOR},'127.0.0.1/8')");
    is( ref($r), "CODE", "Returned code ref" );

    is( $r->( { env => { X_FORWARDED_FOR => "127.0.0.1" } }, {} ),
        "1", "ipInSubnet works" );
    is( $r->( { env => { X_FORWARDED_FOR => "192.168.0.1" } }, {} ),
        "0", "ipInSubnet works" );

    $r = compileRule(
        "ipInSubnet(\$ENV{X_FORWARDED_FOR}, '127.0.0.1/8', '192.168.0.0/16')");
    is( ref($r), "CODE", "Returned code ref" );

    is( $r->( { env => { X_FORWARDED_FOR => "192.168.0.1" } }, {} ),
        "1", "ipInSubnet works" );

    # inDomain
    $r = compileRule("inDomain('example.com') || 0");
    is( ref($r), "CODE", "Returned code ref" );

    is( $r->( { env => { HTTP_HOST => "AUTH.EXAMPLE.COM" } }, {} ),
        "1", "inDomain works for AUTH.EXAMPLE.COM" );

    is( $r->( { env => { HTTP_HOST => "auth.example.com" } }, {} ),
        "1", "inDomain works for auth.example.com" );

    is( $r->( { env => { HTTP_HOST => "example.com" } }, {} ),
        "1", "inDomain works for example.com" );

    is( $r->( { env => { HTTP_HOST => "cda.com" } }, {} ),
        "0", "inDomain works for cda.com" );

    is( $r->( { env => { HTTP_HOST => "notexample.com" } }, {} ),
        "0", "inDomain works for notexample.com" );
    is( $r->( { env => { HTTP_HOST => "exampleacom" } }, {} ),
        "0", "inDomain works for exampleacom" );
}

sub runUnsafeTests {
    my ($h) = @_;
    my $r;

    $r = compileRule('basic($user, $_password)');
    is( ref($r), "CODE", "Returned code ref" );
    is(
        $r->( { env => {} }, { user => "aa", _password => "bc" } ),
        "Basic YWE6YmM=",
        "Returned correct Basic header"
    );
}

eval { $h->localConfig($conf); $h->logLevelInit() };
ok( !$@, 'init' );

subtest "Safe jail off" => sub {
    plan tests => 35;
    ok( $h->configReload($conf), 'Load conf' );
    is(
        ref( $h->tsv->{jail}->jail ),
        "Lemonldap::NG::Handler::Main::Jail",
        "Safe jail is disabled"
    );

    runTests($h);
    runUnsafeTests($h);
};

subtest "Safe jail on" => sub {
    plan tests => 33;
    ok( $h->configReload( { %$conf, useSafeJail => 1 } ), 'Load conf' );
    is( ref( $h->tsv->{jail}->jail ), "Safe", "Safe jail is enabled" );

    runTests($h);
};