populate.pl 3.73 KB
Newer Older
1
#!/usr/bin/perl
Xavier Guimard's avatar
Xavier Guimard committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#
# Session Backend Populate Tool
# -----------------------------
#
# This script populates a running LLNG instance (launched by
# "make start_web_server") with dumy sessions.
#
#
# (c) Copyright: 2017, LemonLDAP::NG team
#
#This library is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2, or (at your option)
#any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see L<http://www.gnu.org/licenses/>.
24 25 26 27 28 29 30 31 32 33 34 35

package Test::Request;

use Mouse;

has userData    => ( is => 'rw' );
has sessionInfo => ( is => 'rw' );
has id          => ( is => 'rw' );

package main;
use Test::More;
use strict;
36
use POSIX qw(mktime strftime);
37 38 39 40 41 42
use lib 'lemonldap-ng-common/blib/lib';
use lib 'lemonldap-ng-handler/blib/lib';
use lib 'lemonldap-ng-portal/blib/lib';
use lib 'lemonldap-ng-manager/blib/lib';
require './lemonldap-ng-portal/t/test-lib.pm';

43
use constant COUNT => 1000;
44

45 46 47 48 49 50 51
my $portal = LLNG::Manager::Test->new(
    {
        ini => {
            configStorage => {
                type    => 'File',
                dirName => 'e2e-tests/conf',
            },
52
            localSessionStorage => undef,
53
            localStorage        => undef,
54 55 56 57 58
        }
    }
)->p;

my @chars = ( "A" .. "Z", "a" .. "z" );
Xavier Guimard's avatar
Xavier Guimard committed
59

60
foreach my $i ( 1 .. COUNT() ) {
61 62
    my $string;
    $string .= $chars[ rand @chars ] for 1 .. 8;
63

64 65
    #$string = 'dwho';
    my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
66
      localtime( time - int( rand( 4 * 3600 ) ) );
67
    my $ipAddr = (
Xavier Guimard's avatar
Xavier Guimard committed
68
        int( rand(2) ) == 1
69 70 71 72 73 74 75 76 77 78 79
        ? join( '.',
            int( rand(256) ),
            int( rand(256) ),
            int( rand(256) ),
            int( rand(256) ) )
        : join(
            ':',

            # Mix routable IPv6 addresses (2000::/3) and local ones (fe:80::/10)
            (
                int( rand(2) ) == 1
80 81 82 83
                ? (
                    sprintf( "%X", int( rand(8192) + 8192 ) ),
                    sprintf( "%X", int( rand(65536) ) )
                  )
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
                : sprintf( "fe80:%X", int( rand(16384) ) )
            ),
            sprintf( "%X", int( rand(65536) ) ),
            sprintf( "%X", int( rand(65536) ) ),
            sprintf( "%X", int( rand(65536) ) ),
            sprintf( "%X", int( rand(65536) ) ),
            sprintf( "%X", int( rand(65536) ) ),
            sprintf( "%X", int( rand(65536) ) )
        )
    );

    # Replace 0 in IPv6 addresses
    if ( $ipAddr =~ ':0:' ) {
        my @t   = ( $ipAddr =~ /\:(?:0\:)+/g );
        my $ind = -1;
        my $len = 0;
        for ( my $i = 0 ; $i < @t ; $i++ ) {
            $ind = $i if ( length( $t[$i] ) > $len );
        }
        $ipAddr =~ s/$t[$ind]/::/;
        $ipAddr =~ s/^0//;
    }
106 107 108
    my $req = Test::Request->new(
        {
            sessionInfo => {
109 110 111 112 113 114 115 116 117 118 119
                _user     => $string,
                uid       => $string,
                cn        => uc($string),
                sn        => $string,
                mail      => "$string\@badwolf.org",
                _utime    => mktime( $sec, $min, $hour, $mday, $mon, $year ),
                startTime => strftime(
                    "%Y%m%d%H%M%S",
                    $sec,  $min,  $hour, $mday, $mon,
                    $year, $wday, $yday, $isdst
                ),
120
                ipAddr => $ipAddr,
121 122 123 124 125 126 127 128
            }
        }
    );

    $portal->setMacros($req);
    $portal->store($req);
}
done_testing( count() );