#!perl

use Test::More tests => 38;

BEGIN {
  require "t/common.pl";
  start_server();
}

$ldap = client();
if (!$ldap) {
  die "can't contact ldap server";
}

$mesg = $ldap->bind($MANAGERDN, password => $PASSWD);

if($mesg->code) {
  die "can't bind: ".$mesg->error;
}

ldif_populate($ldap, "data/00-in.ldif");

# Add
$mesg = $ldap->search(base => $BASEDN, filter => 'objectClass=*');
compare_ldif("0001",$mesg,$mesg->sorted);


$dn = "uid=user0001,ou=Tech,ou=IT,ou=People,$MASTERDN";

# Modify
$mesg = $ldap->modify($dn, replace => {'mail' => 'user0001@lism.org'});
$mesg = $ldap->search(base => $BASEDN, filter => 'uid=user0001');
compare_ldif("0002",$mesg,$mesg->sorted);

$mesg = $ldap->modify("uid=user0005,ou=Tech,ou=Machine,ou=People,$MASTERDN", add => {'mail' => 'user0005@lism.org'});
$mesg = $ldap->search(base => $BASEDN, filter => 'uid=user0005');
compare_ldif("0003",$mesg,$mesg->sorted);

$mesg = $ldap->modify("uid=user0005,ou=Tech,ou=Machine,ou=People,$MASTERDN", delete => {'mail' => 'user0005@lism.org'});
$mesg = $ldap->search(base => $BASEDN, filter => 'uid=user0005');
compare_ldif("0004",$mesg,$mesg->sorted);


# Rewrite
# test move to 12rewrite-handler.t
#ok(!compare("$TESTCSV/group.csv","data/0005-cmp.csv"), "data/0005-cmp.csv");
ok(1);

# Sync Information
## Synchronization
$mesg = $ldap->search(base => "cn=sync,$BASEDN", filter => 'objectClass=*' , scope => 'base');
compare_ldif("0006",$mesg,$mesg->sorted);

$mesg = $ldap->modify("cn=sync,$BASEDN", replace => {'description' => 'sync'});

$mesg = $ldap->search(base => $BASEDN, filter => '(|(uid=user0001)(uid=user0004))');
compare_ldif("0007",$mesg,$mesg->sorted);

$mesg = $ldap->search(base => "cn=sync,$BASEDN", filter => 'objectClass=*' , scope => 'base');
compare_ldif("0008",$mesg,$mesg->sorted);

## Add entry
ldif_populate($ldap, "data/0009-in.ldif");

# master sync
$mesg = $ldap->search(base => "cn=master-sync,$BASEDN", filter => 'objectClass=*' , scope => 'base');
compare_ldif("0009",$mesg,$mesg->sorted);

$mesg = $ldap->modify("cn=master-sync,$BASEDN", replace => {'description' => 'sync'});

$mesg = $ldap->search(base => $BASEDN, filter => '(|(uid=user0004)(|(uid=user0006)(uid=user0007)))');
compare_ldif("0010",$mesg,$mesg->sorted);

$mesg = $ldap->search(base => "cn=master-sync,$BASEDN", filter => 'objectClass=*' , scope => 'base');
compare_ldif("0011",$mesg,$mesg->sorted);

# cluster sync
$mesg = $ldap->search(base => "cn=cluster-sync,$BASEDN", filter => 'objectClass=*' , scope => 'base');
compare_ldif("0012",$mesg,$mesg->sorted);

$mesg = $ldap->modify("cn=cluster-sync,$BASEDN", replace => {'description' => 'sync'});

$mesg = $ldap->search(base => $BASEDN, filter => '(|(uid=user0006)(uid=user0007))');
compare_ldif("0013",$mesg,$mesg->sorted);

$mesg = $ldap->search(base => "cn=cluster-sync,$BASEDN", filter => 'objectClass=*' , scope => 'base');
compare_ldif("0014",$mesg,$mesg->sorted);

# Delete entry
$mesg = $ldap->delete("uid=user0002,ou=Tech,ou=IT,ou=People,$CSVDN");

$mesg = $ldap->search(base => "cn=sync,$BASEDN", filter => 'objectClass=*' , scope => 'base');
compare_ldif("0015",$mesg,$mesg->sorted);

$mesg = $ldap->modify("cn=sync,$BASEDN", replace => {'description' => 'sync'});

$mesg = $ldap->search(base => $BASEDN, filter => 'uid=user0002');
ok($mesg->count == 0, "delete uid=user0002");

$mesg = $ldap->search(base => "cn=sync,$BASEDN", filter => 'objectClass=*' , scope => 'base');
compare_ldif("0017",$mesg,$mesg->sorted);

$mesg = $ldap->delete("uid=user0003,ou=Sales,ou=IT,ou=People,$SQLDN");

$mesg = $ldap->search(base => "cn=sync,$BASEDN", filter => 'objectClass=*' , scope => 'base');
compare_ldif("0018",$mesg,$mesg->sorted);

$mesg = $ldap->modify("cn=sync,$BASEDN", replace => {'description' => 'sync'});

$mesg = $ldap->search(base => $BASEDN, filter => 'uid=user0003');
compare_ldif("0019",$mesg,$mesg->sorted);

ldif_populate($ldap, "data/0020-in.ldif");

$mesg = $ldap->search(base => "cn=sync,$BASEDN", filter => 'objectClass=*' , scope => 'base');
compare_ldif("0020",$mesg,$mesg->sorted);

$mesg = $ldap->modify("cn=sync,$BASEDN", replace => {'description' => 'sync'});
$mesg = $ldap->search(base => $BASEDN, filter => 'uid=admin');
ok($mesg->count == 1, "admin");

# Modify passowrd
$mesg = $ldap->modify("uid=user0001,ou=Tech,ou=IT,ou=People,$SQLDN", replace => {'userpassword' => 'sql0001'});

$mesg = $ldap->search(base => "cn=sync,$BASEDN", filter => 'objectClass=*' , scope => 'base');
compare_ldif("0022",$mesg,$mesg->sorted);

$mesg = $ldap->modify("cn=sync,$BASEDN", replace => {'description' => 'sync'});

$mesg = $ldap->search(base => $BASEDN, filter => 'uid=user0001');
compare_ldif("0023",$mesg,$mesg->sorted);

$mesg = $ldap->search(base => "cn=sync,$BASEDN", filter => 'objectClass=*' , scope => 'base');
compare_ldif("0024",$mesg,$mesg->sorted);

# synchronize specified data
ldif_populate($ldap, "data/0025-in.ldif");

$mesg = $ldap->search(base => "cn=sync,$BASEDN", filter => 'ou=CSV' , scope => 'base');
compare_ldif("0025",$mesg,$mesg->sorted);

$mesg = $ldap->search(base => "cn=sync,$BASEDN", filter => 'ou=SQL' , scope => 'base');
compare_ldif("0026",$mesg,$mesg->sorted);

$mesg = $ldap->modify("cn=sync,$BASEDN", delete => {'ou' => 'CSV'});

$mesg = $ldap->search(base => $BASEDN, filter => '(uid=user0001)');
compare_ldif("0027",$mesg,$mesg->sorted);

$mesg = $ldap->search(base => "cn=sync,$BASEDN", filter => 'objectClass=*' , scope => 'base');
compare_ldif("0028",$mesg,$mesg->sorted);

$mesg = $ldap->modify("cn=sync,$BASEDN", delete => {'ou' => 'SQL'});

$mesg = $ldap->search(base => $BASEDN, filter => '(uid=user0004)');
compare_ldif("0029",$mesg,$mesg->sorted);

$mesg = $ldap->search(base => "cn=sync,$BASEDN", filter => 'objectClass=*' , scope => 'base');
compare_ldif("0030",$mesg,$mesg->sorted);

# syncfail log
# Add
ldif_populate($ldap, "data/0031-in.ldif");

$mesg = $ldap->delete("uid=user0008,ou=Tech,ou=IT,ou=people,$CSVDN");
# Modify
$mesg = $ldap->modify("uid=user0008,ou=Tech,ou=IT,ou=people,$MASTERDN",
                      add => {'telephoneNumber' => '01-2345-6789'},
                      replace => {'userPassword' => 'passwd0008',
                                  'mail' => 'user0008@lism.org'});

# Delete
$mesg = $ldap->delete("uid=user0008,ou=Tech,ou=IT,ou=people,$MASTERDN");

ok(!compare_log("$TEMPDIR/syncfail-CSV.log", "data/0031-cmp.log"), "data/0031-cmp.log");

# Allow bind
ldif_populate($ldap, "data/0032-in.ldif");

$mesg = $ldap->bind("uid=user0009,ou=Tech,ou=IT,ou=People,$LDAPDN", password => 'user0009');
ok($mesg->code == 0, "bind uid=user0009 success");

$mesg = $ldap->bind("uid=nobind0001,ou=Tech,ou=IT,ou=People,$LDAPDN", password => 'nobind0001');
ok($mesg->code == 50, "bind uid=nobind0001 failure");

$mesg = $ldap->bind("uid=bind0001,ou=Tech,ou=IT,ou=People,$LDAPDN", password => 'bind0001');
ok($mesg->code == 0, "bind uid=bind0001 success");

$mesg = $ldap->bind("uid=nobind0002,ou=Tech,ou=IT,ou=People,$LDAPDN", password => 'nobind0002');
ok($mesg->code == 50, "bind uid=nobind0002 failure");

# Delete
$mesg = $ldap->bind($MANAGERDN, password => $PASSWD);
$mesg = $ldap->delete($dn);
$mesg = $ldap->search(base => $SQLDN, filter => 'uid=user0001');
ok($mesg->count == 0, "delete uid=user0001");

$mesg = $ldap->search(base => $LDAPDN, filter => 'uid=user0001');
ok($mesg->count == 0, "delete uid=user0001");

$mesg = $ldap->search(base => $CSVDN, filter => 'uid=user0001');
ok($mesg->count == 0, "delete uid=user0001");
