From 4ea8b11a0e031ba3ba6c010390ee74235682e065 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20OUDOT?= Date: Tue, 17 Sep 2024 18:34:35 +0200 Subject: [PATCH] Tests for resetAtNextConnection and some fixes in code --- src/Ltb/Directory/ActiveDirectory.php | 8 +- src/Ltb/Directory/OpenLDAP.php | 2 +- tests/Ltb/DirectoryTest.php | 128 ++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 5 deletions(-) diff --git a/src/Ltb/Directory/ActiveDirectory.php b/src/Ltb/Directory/ActiveDirectory.php index 8c00070..be9a4c0 100644 --- a/src/Ltb/Directory/ActiveDirectory.php +++ b/src/Ltb/Directory/ActiveDirectory.php @@ -55,7 +55,7 @@ public function getLockDate($ldap, $dn) : ?DateTime { # Get lockoutTime $lockoutTime = $entry[0]['lockouttime'][0]; - if ( !$lockoutTime or $lockoutTime == 0) { + if ( !$lockoutTime or $lockoutTime === 0) { return $lockDate; } @@ -157,7 +157,7 @@ public function getPasswordExpirationDate($ldap, $dn, $config) : ?DateTime { # Get pwdLastSet $pwdLastSet = $entry[0]['pwdlastset'][0]; - if ( !$pwdLastSet or $pwdLastSet == 0) { + if ( !$pwdLastSet or $pwdLastSet === 0) { return $expirationDate; } @@ -201,12 +201,12 @@ public function resetAtNextConnection($ldap, $dn) : bool { if ( $errno ) { error_log("LDAP - Search error $errno (".ldap_error($ldap).")"); - return $expirationDate; + return false; } else { $entry = \Ltb\PhpLDAP::ldap_get_entries($ldap, $search); } - if ($entry[0]['pwdlastset'] and $entry[0]['pwdlastset'][0] == 0) { + if ($entry[0]['pwdlastset'] and $entry[0]['pwdlastset'][0] === 0) { return true; } else { return false; diff --git a/src/Ltb/Directory/OpenLDAP.php b/src/Ltb/Directory/OpenLDAP.php index 61e57f9..1a4bbb2 100644 --- a/src/Ltb/Directory/OpenLDAP.php +++ b/src/Ltb/Directory/OpenLDAP.php @@ -208,7 +208,7 @@ public function resetAtNextConnection($ldap, $dn) : bool { if ( $errno ) { error_log("LDAP - Search error $errno (".ldap_error($ldap).")"); - return $expirationDate; + return false; } else { $entry = \Ltb\PhpLDAP::ldap_get_entries($ldap, $search); } diff --git a/tests/Ltb/DirectoryTest.php b/tests/Ltb/DirectoryTest.php index db234d4..264f552 100644 --- a/tests/Ltb/DirectoryTest.php +++ b/tests/Ltb/DirectoryTest.php @@ -288,6 +288,69 @@ public function test_openldap_getpasswordexpirationdate_notempty(): void $this->assertEquals($dt->modify("+1 day")->format("Y/m/d - h:i:s"), $passwordExpirationDate->format("Y/m/d - h:i:s"), "Password expiration date is correct"); } + public function test_openldap_reset_true(): void + { + $phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP'); + $phpLDAPMock->shouldreceive([ + 'ldap_read' => null, + 'ldap_errno' => 0, + 'ldap_get_entries' => [ + 'count' => 1, + 0 => [ + 'pwdreset' => [ + 'count' => 1, + 0 => "TRUE", + ] + ] + ] + ]); + + $reset = (new Ltb\Directory\OpenLDAP)->resetAtNextConnection(null, null); + $this->assertTrue($reset, "Reset should be true"); + } + + public function test_openldap_reset_false(): void + { + $phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP'); + $phpLDAPMock->shouldreceive([ + 'ldap_read' => null, + 'ldap_errno' => 0, + 'ldap_get_entries' => [ + 'count' => 1, + 0 => [ + 'pwdreset' => [ + 'count' => 1, + 0 => "FALSE", + ] + ] + ] + ]); + + $reset = (new Ltb\Directory\OpenLDAP)->resetAtNextConnection(null, null); + $this->assertFalse($reset, "Reset should be false"); + } + + public function test_openldap_reset_false_empty(): void + { + $phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP'); + $phpLDAPMock->shouldreceive([ + 'ldap_read' => null, + 'ldap_errno' => 0, + 'ldap_get_entries' => [ + 'count' => 1, + 0 => [ + 'pwdreset' => [ + 'count' => 1, + 0 => null, + ] + ] + ] + ]); + + $reset = (new Ltb\Directory\OpenLDAP)->resetAtNextConnection(null, null); + $this->assertFalse($reset, "Reset should be false"); + } + public function test_activedirectory_islocked_locked_forever(): void { $ad_date = ((int)time() + 11644473600) * 10000000; @@ -557,4 +620,69 @@ public function test_activedirectory_getpasswordexpirationdate_notempty(): void $this->assertEquals($dt->modify("+1 day")->format("Y/m/d - h:i:s"), $passwordExpirationDate->format("Y/m/d - h:i:s"), "Password expiration date is correct"); } + public function test_activedirectory_reset_true(): void + { + $phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP'); + $phpLDAPMock->shouldreceive([ + 'ldap_read' => null, + 'ldap_errno' => 0, + 'ldap_get_entries' => [ + 'count' => 1, + 0 => [ + 'pwdlastset' => [ + 'count' => 1, + 0 => 0, + ] + ] + ] + ]); + + $reset = (new Ltb\Directory\ActiveDirectory)->resetAtNextConnection(null, null); + $this->assertTrue($reset, "Reset should be true"); + } + + public function test_activedirectory_reset_false(): void + { + $dt = new DateTime; + $ad_date = ((int)$dt->getTimestamp() + 11644473600) * 10000000; + $phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP'); + $phpLDAPMock->shouldreceive([ + 'ldap_read' => null, + 'ldap_errno' => 0, + 'ldap_get_entries' => [ + 'count' => 1, + 0 => [ + 'pwdlastset' => [ + 'count' => 1, + 0 => $ad_date, + ] + ] + ] + ]); + + $reset = (new Ltb\Directory\ActiveDirectory)->resetAtNextConnection(null, null); + $this->assertFalse($reset, "Reset should be false"); + } + + public function test_activedirectory_reset_false_empty(): void + { + $phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP'); + $phpLDAPMock->shouldreceive([ + 'ldap_read' => null, + 'ldap_errno' => 0, + 'ldap_get_entries' => [ + 'count' => 1, + 0 => [ + 'pwdlastset' => [ + 'count' => 1, + 0 => null, + ] + ] + ] + ]); + + $reset = (new Ltb\Directory\ActiveDirectory)->resetAtNextConnection(null, null); + $this->assertFalse($reset, "Reset should be false"); + } + }