Skip to content

Commit

Permalink
Use local methods to "override" native PHP functions
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed Jan 13, 2025
1 parent af180a3 commit 08f109d
Show file tree
Hide file tree
Showing 12 changed files with 535 additions and 321 deletions.
82 changes: 82 additions & 0 deletions phpunit/functional/Glpi/System/Requirement/MysqliMysqlndTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2025 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program 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 3 of the License, 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 <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace tests\units\Glpi\System\Requirement;

use Glpi\System\Requirement\MysqliMysqlnd;

class MysqliMysqlndTest extends \GLPITestCase
{
public function testCheckUsingMysqlnd()
{
$instance = $this->getMockBuilder(MysqliMysqlnd::class)
->onlyMethods(['isExtensionLoaded', 'isMysqlND'])
->getMock();
$instance->method('isExtensionLoaded')->willReturn(true);
$instance->method('isMysqlND')->willReturn(true);
$this->assertTrue($instance->isValidated());
$this->assertEquals(
['mysqli extension is installed.'],
$instance->getValidationMessages()
);
}

public function testCheckUsingAlternativeDriver()
{
$instance = $this->getMockBuilder(MysqliMysqlnd::class)
->onlyMethods(['isExtensionLoaded', 'isMysqlND'])
->getMock();
$instance->method('isExtensionLoaded')->willReturn(true);
$instance->method('isMysqlND')->willReturn(false);
$this->assertFalse($instance->isValidated());
$this->assertEquals(
['mysqli extension is installed but is not using mysqlnd driver.'],
$instance->getValidationMessages()
);
}

public function testCheckOnMissingExtension()
{
$instance = $this->getMockBuilder(MysqliMysqlnd::class)
->onlyMethods(['isExtensionLoaded'])
->getMock();
$instance->method('isExtensionLoaded')->willReturn(false);
$this->assertFalse($instance->isValidated());
$this->assertEquals(
['mysqli extension is missing.'],
$instance->getValidationMessages()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@

namespace tests\units\Glpi\System\Requirement;

class PhpSupportedVersion extends \GLPITestCase
use Glpi\System\Requirement\PhpSupportedVersion;

class PhpSupportedVersionTest extends \GLPITestCase
{
protected function versionProvider(): iterable
public static function versionProvider(): iterable
{
yield [
'phpversion' => '7.4.0-rc1',
Expand Down Expand Up @@ -131,10 +133,11 @@ protected function versionProvider(): iterable
*/
public function testCheck(string $phpversion, bool $validated, array $messages): void
{
$this->function->phpversion = $phpversion;

$this->newTestedInstance();
$this->boolean($this->testedInstance->isValidated())->isEqualTo($validated);
$this->array($this->testedInstance->getValidationMessages())->isEqualTo($messages);
$instance = $this->getMockBuilder(PhpSupportedVersion::class)
->onlyMethods(['getPHPVersion'])
->getMock();
$instance->method('getPHPVersion')->willReturn($phpversion);
$this->assertSame($validated, $instance->isValidated());
$this->assertEquals($messages, $instance->getValidationMessages());
}
}
272 changes: 272 additions & 0 deletions phpunit/functional/Glpi/System/Requirement/SeLinuxTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2025 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program 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 3 of the License, 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 <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace tests\units\Glpi\System\Requirement;

use Glpi\System\Requirement\SeLinux;

class SeLinuxTest extends \GLPITestCase
{
public function testCheckOutOfContext()
{
$instance = $this->getMockBuilder(SeLinux::class)
->onlyMethods([
'doesSelinuxBinariesExists',
'doesSelinuxIsEnabledFunctionExists',
'doesSelinuxGetenforceFunctionExists',
'doesSelinuxBooleanFunctionExists',
])
->getMock();
$instance->method('doesSelinuxBinariesExists')->willReturn(false);
$instance->method('doesSelinuxIsEnabledFunctionExists')->willReturn(false);
$instance->method('doesSelinuxGetenforceFunctionExists')->willReturn(false);
$instance->method('doesSelinuxBooleanFunctionExists')->willReturn(false);

$this->assertFalse($instance->isValidated());
$this->assertTrue($instance->isOutOfContext());
}

public function testCheckWithEnforcesAndActiveBooleans()
{
$instance = $this->getMockBuilder(SeLinux::class)
->onlyMethods([
'doesSelinuxBinariesExists',
'doesSelinuxIsEnabledFunctionExists',
'isSelinuxEnabled',
'doesSelinuxGetenforceFunctionExists',
'getSelinxEnforceStatus',
'doesSelinuxBooleanFunctionExists',
'isSelinuxBooleanActive'
])
->getMock();
$instance->method('doesSelinuxBinariesExists')->willReturn(true);
$instance->method('doesSelinuxIsEnabledFunctionExists')->willReturn(true);
$instance->method('isSelinuxEnabled')->willReturn(true);
$instance->method('doesSelinuxGetenforceFunctionExists')->willReturn(true);
$instance->method('getSelinxEnforceStatus')->willReturn(1);
$instance->method('doesSelinuxBooleanFunctionExists')->willReturn(true);
$instance->method('isSelinuxBooleanActive')->willReturn(true);

$this->assertTrue($instance->isValidated());
$this->assertEquals(
$instance->getValidationMessages(),
['SELinux configuration is OK.']
);
}

public function testCheckWithEnforcesAndInactiveNetworkConnect()
{
$instance = $this->getMockBuilder(SeLinux::class)
->onlyMethods([
'doesSelinuxBinariesExists',
'doesSelinuxIsEnabledFunctionExists',
'isSelinuxEnabled',
'doesSelinuxGetenforceFunctionExists',
'getSelinxEnforceStatus',
'doesSelinuxBooleanFunctionExists',
'isSelinuxBooleanActive'
])
->getMock();
$instance->method('doesSelinuxBinariesExists')->willReturn(true);
$instance->method('doesSelinuxIsEnabledFunctionExists')->willReturn(true);
$instance->method('isSelinuxEnabled')->willReturn(true);
$instance->method('doesSelinuxGetenforceFunctionExists')->willReturn(true);
$instance->method('getSelinxEnforceStatus')->willReturn(1);
$instance->method('doesSelinuxBooleanFunctionExists')->willReturn(true);
$instance->method('isSelinuxBooleanActive')->willReturnCallback(
function ($bool) {
return $bool != 'httpd_can_network_connect';
}
);

$this->assertFalse($instance->isValidated());
$this->assertEquals(
['SELinux boolean httpd_can_network_connect is off, some features may require this to be on.'],
$instance->getValidationMessages()
);
}

public function testCheckWithEnforcesAndInactiveNetworkConnectDB()
{
$instance = $this->getMockBuilder(SeLinux::class)
->onlyMethods([
'doesSelinuxBinariesExists',
'doesSelinuxIsEnabledFunctionExists',
'isSelinuxEnabled',
'doesSelinuxGetenforceFunctionExists',
'getSelinxEnforceStatus',
'doesSelinuxBooleanFunctionExists',
'isSelinuxBooleanActive'
])
->getMock();
$instance->method('doesSelinuxBinariesExists')->willReturn(true);
$instance->method('doesSelinuxIsEnabledFunctionExists')->willReturn(true);
$instance->method('isSelinuxEnabled')->willReturn(true);
$instance->method('doesSelinuxGetenforceFunctionExists')->willReturn(true);
$instance->method('getSelinxEnforceStatus')->willReturn(1);
$instance->method('doesSelinuxBooleanFunctionExists')->willReturn(true);
$instance->method('isSelinuxBooleanActive')->willReturnCallback(
function ($bool) {
return $bool != 'httpd_can_network_connect_db';
}
);

$this->assertFalse($instance->isValidated());
$this->assertEquals(
['SELinux boolean httpd_can_network_connect_db is off, some features may require this to be on.'],
$instance->getValidationMessages()
);
}

public function testCheckWithEnforcesAndInactiveSendmail()
{
$instance = $this->getMockBuilder(SeLinux::class)
->onlyMethods([
'doesSelinuxBinariesExists',
'doesSelinuxIsEnabledFunctionExists',
'isSelinuxEnabled',
'doesSelinuxGetenforceFunctionExists',
'getSelinxEnforceStatus',
'doesSelinuxBooleanFunctionExists',
'isSelinuxBooleanActive'
])
->getMock();
$instance->method('doesSelinuxBinariesExists')->willReturn(true);
$instance->method('doesSelinuxIsEnabledFunctionExists')->willReturn(true);
$instance->method('isSelinuxEnabled')->willReturn(true);
$instance->method('doesSelinuxGetenforceFunctionExists')->willReturn(true);
$instance->method('getSelinxEnforceStatus')->willReturn(1);
$instance->method('doesSelinuxBooleanFunctionExists')->willReturn(true);
$instance->method('isSelinuxBooleanActive')->willReturnCallback(
function ($bool) {
return $bool != 'httpd_can_sendmail';
}
);

$this->assertFalse($instance->isValidated());
$this->assertEquals(
['SELinux boolean httpd_can_sendmail is off, some features may require this to be on.'],
$instance->getValidationMessages()
);
}

public function testCheckWithEnforcesAndInactiveBooleans()
{
$instance = $this->getMockBuilder(SeLinux::class)
->onlyMethods([
'doesSelinuxBinariesExists',
'doesSelinuxIsEnabledFunctionExists',
'isSelinuxEnabled',
'doesSelinuxGetenforceFunctionExists',
'getSelinxEnforceStatus',
'doesSelinuxBooleanFunctionExists',
'isSelinuxBooleanActive'
])
->getMock();
$instance->method('doesSelinuxBinariesExists')->willReturn(true);
$instance->method('doesSelinuxIsEnabledFunctionExists')->willReturn(true);
$instance->method('isSelinuxEnabled')->willReturn(true);
$instance->method('doesSelinuxGetenforceFunctionExists')->willReturn(true);
$instance->method('getSelinxEnforceStatus')->willReturn(1);
$instance->method('doesSelinuxBooleanFunctionExists')->willReturn(true);
$instance->method('isSelinuxBooleanActive')->willReturn(false);

$this->assertFalse($instance->isValidated());
$this->assertEquals(
[
'SELinux boolean httpd_can_network_connect is off, some features may require this to be on.',
'SELinux boolean httpd_can_network_connect_db is off, some features may require this to be on.',
'SELinux boolean httpd_can_sendmail is off, some features may require this to be on.',
],
$instance->getValidationMessages()
);
}

public function testCheckWithPermissiveSeLinux()
{
$instance = $this->getMockBuilder(SeLinux::class)
->onlyMethods([
'doesSelinuxBinariesExists',
'doesSelinuxIsEnabledFunctionExists',
'isSelinuxEnabled',
'doesSelinuxGetenforceFunctionExists',
'getSelinxEnforceStatus',
'doesSelinuxBooleanFunctionExists',
'isSelinuxBooleanActive'
])
->getMock();
$instance->method('doesSelinuxBinariesExists')->willReturn(true);
$instance->method('doesSelinuxIsEnabledFunctionExists')->willReturn(true);
$instance->method('isSelinuxEnabled')->willReturn(true);
$instance->method('doesSelinuxGetenforceFunctionExists')->willReturn(true);
$instance->method('getSelinxEnforceStatus')->willReturn(0);
$instance->method('doesSelinuxBooleanFunctionExists')->willReturn(true);
$instance->method('isSelinuxBooleanActive')->willReturn(true);

$this->assertFalse($instance->isValidated());
$this->assertEquals(
['For security reasons, SELinux mode should be Enforcing.'],
$instance->getValidationMessages()
);
}

public function testCheckWithDisabledSeLinux()
{
$instance = $this->getMockBuilder(SeLinux::class)
->onlyMethods([
'doesSelinuxBinariesExists',
'doesSelinuxIsEnabledFunctionExists',
'isSelinuxEnabled',
'doesSelinuxGetenforceFunctionExists',
'getSelinxEnforceStatus',
'doesSelinuxBooleanFunctionExists',
'isSelinuxBooleanActive'
])
->getMock();
$instance->method('doesSelinuxBinariesExists')->willReturn(true);
$instance->method('doesSelinuxIsEnabledFunctionExists')->willReturn(true);
$instance->method('isSelinuxEnabled')->willReturn(false);
$instance->method('doesSelinuxGetenforceFunctionExists')->willReturn(true);
$instance->method('getSelinxEnforceStatus')->willReturn(1);
$instance->method('doesSelinuxBooleanFunctionExists')->willReturn(true);
$instance->method('isSelinuxBooleanActive')->willReturn(true);

$this->assertFalse($instance->isValidated());
$this->assertEquals(
['For security reasons, SELinux mode should be Enforcing.'],
$instance->getValidationMessages()
);
}
}
Loading

0 comments on commit 08f109d

Please sign in to comment.