#!/usr/bin/perl -w
# This script (sophomorix-test-errorfiles) is maintained by Rüdiger Beck
# It is Free Software (License GPLv3)
# If you find errors, contact the author
# jeffbeck@web.de  or  jeffbeck@linuxmuster.net

# modules
use strict;
use Getopt::Long;
Getopt::Long::Configure ("bundling");
use Sophomorix::SophomorixBase qw(
                                 print_line
                                 print_title
                                 check_options
                                 );
use Sophomorix::SophomorixSambaAD qw(
                                 AD_bind_admin
                                 AD_unbind_admin
                                 AD_dns_get
                                 AD_object_search
                                    );
use Sophomorix::SophomorixTest qw(
                                 AD_object_nonexist
                                 AD_dn_nonexist
                                 AD_test_object
                                 directory_tree_test
                                 start_fs_test
                                 end_fs_test
                                 run_command
                                 AD_test_session_count
                                 ACL_test
                                 file_test_lines
                                 AD_user_timeupdate
                                 AD_get_samaccountname
                                 cat_wcl_test
                                 );

$Conf::log_level=1;
my $help=0;
my $json=0;
my $ntacl=1;
my $nontacl=0;

my $all_options="";
my $full=0;

my $class=0;
my $last=0;
my $first=0;
my $birthdate=0;

my $testopt=GetOptions(
           "help|h" => \$help,
           "verbose|v+" => \$Conf::log_level,
           "json|j+" => \$json,
           "nontacl|n" => \$nontacl,
           "class|c" => \$class,
           "last|l" => \$last,
           "first" => \$first,
           "birthdate|b" => \$birthdate,
           "full|f" => \$full,
          );

# Prüfen, ob Optionen erkannt wurden, sonst Abbruch
&check_options($testopt);

if ($nontacl==1){
    $ntacl=0;
}

if ($Conf::log_level==1){
    $all_options="";
} elsif ($Conf::log_level==2){
    $all_options="-v";
} elsif ($Conf::log_level==3){
    $all_options="-vv";
}

if ($json==0){
    # nothing to do
} elsif ($json==1){
    $all_options=$all_options." -j";
} elsif ($json==2){
    $all_options=$all_options." -jj";
} elsif ($json==3){
    $all_options=$all_options." -jjj";
} 

my ($ldap,$root_dse) = &AD_bind_admin();
my $root_dns=&AD_dns_get($root_dse);

# --help
if ($help==1) {
   # Scriptname ermitteln
   my @list = split(/\//,$0);
   my $scriptname = pop @list;
   # Befehlsbeschreibung
   print('
sophomorix-test-errorfiles uses files with errors and tests if sophomorix-check filters them out


Options
  -h  / --help

Full automated test:

  -f, --full 

Step by step Test:

   --class
   --last
   --first
   --birthdate

');
   print "\n";
   exit;
}




############################################################
# Prepare
############################################################

&run_command("mkdir -p /var/lib/sophomorix/check-result");


# --full
if ($full==1){
    $class=1;
    $last=1;
    $first=1;
    $birthdate=1;
}



############################################################
# class
############################################################
if ($class==1){
    &run_command("cp -v /usr/share/sophomorix-developer/testdata/students.csv-errors-class /etc/linuxmuster/sophomorix/bsz/bsz.students.csv");

    &run_command("sophomorix-check");


    &file_test_lines("/var/lib/sophomorix/check-result/sophomorix.add",0);
    &file_test_lines("/var/lib/sophomorix/check-result/sophomorix.update",0);
    &file_test_lines("/var/lib/sophomorix/check-result/sophomorix.kill",0);
    &file_test_lines("/var/lib/sophomorix/check-result/sophomorix.nochange",0);
    &file_test_lines("/var/lib/sophomorix/check-result/sophomorix.error",4);

    &cat_wcl_test("/var/lib/sophomorix/check-result/report.office.bsz.students.csv","begins not with a-z or 0-9",2); 
    &cat_wcl_test("/var/lib/sophomorix/check-result/report.office.bsz.students.csv","special char in class",2); 

    # clean up
    &run_command("rm /etc/linuxmuster/sophomorix/bsz/bsz.students.csv");
}



############################################################
# last
############################################################
if ($last==1){
    &run_command("cp -v /usr/share/sophomorix-developer/testdata/students.csv-errors-lastname /etc/linuxmuster/sophomorix/bsz/bsz.students.csv");

    &run_command("sophomorix-check");


    &file_test_lines("/var/lib/sophomorix/check-result/sophomorix.add",0);
    &file_test_lines("/var/lib/sophomorix/check-result/sophomorix.update",0);
    &file_test_lines("/var/lib/sophomorix/check-result/sophomorix.kill",0);
    &file_test_lines("/var/lib/sophomorix/check-result/sophomorix.nochange",0);
    &file_test_lines("/var/lib/sophomorix/check-result/sophomorix.error",24);

    &cat_wcl_test("/var/lib/sophomorix/check-result/report.office.bsz.students.csv","special char in surname",24); 

    # clean up
    &run_command("rm /etc/linuxmuster/sophomorix/bsz/bsz.students.csv");
}



############################################################
# first
############################################################
if ($first==1){
    &run_command("cp -v /usr/share/sophomorix-developer/testdata/students.csv-errors-firstname /etc/linuxmuster/sophomorix/bsz/bsz.students.csv");

    &run_command("sophomorix-check");


    &file_test_lines("/var/lib/sophomorix/check-result/sophomorix.add",1);
    &file_test_lines("/var/lib/sophomorix/check-result/sophomorix.update",0);
    &file_test_lines("/var/lib/sophomorix/check-result/sophomorix.kill",0);
    &file_test_lines("/var/lib/sophomorix/check-result/sophomorix.nochange",0);
    &file_test_lines("/var/lib/sophomorix/check-result/sophomorix.error",24);

    &cat_wcl_test("/var/lib/sophomorix/check-result/report.office.bsz.students.csv","special char in given name",24); 

    # clean up
    &run_command("rm /etc/linuxmuster/sophomorix/bsz/bsz.students.csv");
}



############################################################
# birthdate
############################################################
if ($birthdate==1){
    &run_command("cp -v /usr/share/sophomorix-developer/testdata/students.csv-errors-birthdate /etc/linuxmuster/sophomorix/bsz/bsz.students.csv");

    &run_command("sophomorix-check");


    &file_test_lines("/var/lib/sophomorix/check-result/sophomorix.add",0);
    &file_test_lines("/var/lib/sophomorix/check-result/sophomorix.update",0);
    &file_test_lines("/var/lib/sophomorix/check-result/sophomorix.kill",0);
    &file_test_lines("/var/lib/sophomorix/check-result/sophomorix.nochange",0);
    &file_test_lines("/var/lib/sophomorix/check-result/sophomorix.error",8);

    &cat_wcl_test("/var/lib/sophomorix/check-result/report.office.bsz.students.csv","Day is not valid",2); 
    &cat_wcl_test("/var/lib/sophomorix/check-result/report.office.bsz.students.csv","Month is not valid",2); 
    &cat_wcl_test("/var/lib/sophomorix/check-result/report.office.bsz.students.csv","Year is not valid",2); 
    &cat_wcl_test("/var/lib/sophomorix/check-result/report.office.bsz.students.csv","Birthdate is empty",1); 
    &cat_wcl_test("/var/lib/sophomorix/check-result/report.office.bsz.students.csv","Birthdate does not have 2 dots",1); 

    # clean up
    &run_command("rm /etc/linuxmuster/sophomorix/bsz/bsz.students.csv");
}
