Skip to content

Commit

Permalink
PHPLIB-892: Require crypt_shared and/or mongocryptd for tests utilizi…
Browse files Browse the repository at this point in the history
…ng enterprise CSFLE features (#1072)
  • Loading branch information
GromNaN committed Jun 26, 2023
1 parent ff6df9b commit 28f6ee8
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 142 deletions.
5 changes: 1 addition & 4 deletions tests/DocumentationExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1810,10 +1810,7 @@ public function testQueryableEncryption(): void
}

$this->skipIfServerVersion('<', '7.0.0', 'Explicit encryption tests require MongoDB 7.0 or later');

if (! $this->isEnterprise()) {
$this->markTestSkipped('Automatic encryption requires MongoDB Enterprise');
}
$this->skipIfClientSideEncryptionIsNotSupported();

// Fetch names for the database and collection under test
$collectionName = $this->getCollectionName();
Expand Down
49 changes: 34 additions & 15 deletions tests/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
use function filter_var;
use function getenv;
use function implode;
use function in_array;
use function is_array;
use function is_callable;
use function is_executable;
use function is_object;
use function is_readable;
use function is_string;
use function key;
use function ob_get_clean;
Expand All @@ -44,8 +45,10 @@
use function sprintf;
use function version_compare;

use const DIRECTORY_SEPARATOR;
use const FILTER_VALIDATE_BOOLEAN;
use const INFO_MODULES;
use const PATH_SEPARATOR;

abstract class FunctionalTestCase extends TestCase
{
Expand Down Expand Up @@ -397,20 +400,6 @@ protected function isApiVersionRequired(): bool
return isset($document->requireApiVersion) && $document->requireApiVersion === true;
}

protected function isEnterprise(): bool
{
$buildInfo = $this->getPrimaryServer()->executeCommand(
$this->getDatabaseName(),
new Command(['buildInfo' => 1])
)->toArray()[0];

if (isset($buildInfo->modules) && is_array($buildInfo->modules)) {
return in_array('enterprise', $buildInfo->modules);
}

throw new UnexpectedValueException('Could not determine server modules');
}

protected function isLoadBalanced()
{
return $this->getPrimaryServer()->getType() == Server::TYPE_LOAD_BALANCER;
Expand Down Expand Up @@ -542,6 +531,10 @@ protected function skipIfClientSideEncryptionIsNotSupported(): void
if (static::getModuleInfo('libmongocrypt') === 'disabled') {
$this->markTestSkipped('Client Side Encryption is not enabled in the MongoDB extension');
}

if (! static::isCryptSharedLibAvailable() && ! static::isMongocryptdAvailable()) {
$this->markTestSkipped('Neither crypt_shared nor mongocryptd are available');
}
}

protected function skipIfGeoHaystackIndexIsNotSupported(): void
Expand Down Expand Up @@ -578,6 +571,32 @@ protected function skipIfTransactionsAreNotSupported(): void
}
}

/** @see https://www.mongodb.com/docs/manual/core/queryable-encryption/reference/shared-library/ */
public static function isCryptSharedLibAvailable(): bool
{
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');

if ($cryptSharedLibPath === false) {
return false;
}

return is_readable($cryptSharedLibPath);
}

/** @see https://www.mongodb.com/docs/manual/core/queryable-encryption/reference/mongocryptd/ */
public static function isMongocryptdAvailable(): bool
{
$paths = explode(PATH_SEPARATOR, getenv("PATH"));

foreach ($paths as $path) {
if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) {
return true;
}
}

return false;
}

private static function appendAuthenticationOptions(array $options): array
{
if (isset($options['username']) || isset($options['password'])) {
Expand Down
30 changes: 0 additions & 30 deletions tests/Operation/CreateEncryptedCollectionFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@
use MongoDB\Operation\CreateEncryptedCollection;

use function base64_decode;
use function explode;
use function getenv;
use function is_executable;
use function is_readable;

use const DIRECTORY_SEPARATOR;
use const PATH_SEPARATOR;

class CreateEncryptedCollectionFunctionalTest extends FunctionalTestCase
{
Expand Down Expand Up @@ -217,28 +211,4 @@ public static function createTestClient(?string $uri = null, array $options = []

return parent::createTestClient($uri, $options, $driverOptions);
}

private static function isCryptSharedLibAvailable(): bool
{
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');

if ($cryptSharedLibPath === false) {
return false;
}

return is_readable($cryptSharedLibPath);
}

private static function isMongocryptdAvailable(): bool
{
$paths = explode(PATH_SEPARATOR, getenv("PATH"));

foreach ($paths as $path) {
if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) {
return true;
}
}

return false;
}
}
30 changes: 0 additions & 30 deletions tests/SpecTests/ClientSideEncryption/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@
use PHPUnit\Framework\Assert;
use stdClass;

use function explode;
use function getenv;
use function is_executable;
use function is_readable;
use function sprintf;

use const DIRECTORY_SEPARATOR;
use const PATH_SEPARATOR;

/**
* Base class for client-side encryption prose tests.
*
Expand Down Expand Up @@ -92,28 +86,4 @@ private static function getEnv(string $name): string

return $value;
}

private static function isCryptSharedLibAvailable(): bool
{
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');

if ($cryptSharedLibPath === false) {
return false;
}

return is_readable($cryptSharedLibPath);
}

private static function isMongocryptdAvailable(): bool
{
$paths = explode(PATH_SEPARATOR, getenv("PATH"));

foreach ($paths as $path) {
if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) {
return true;
}
}

return false;
}
}
34 changes: 0 additions & 34 deletions tests/SpecTests/ClientSideEncryptionSpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,16 @@
use function base64_decode;
use function basename;
use function count;
use function explode;
use function file_get_contents;
use function getenv;
use function glob;
use function in_array;
use function is_executable;
use function is_readable;
use function iterator_to_array;
use function json_decode;
use function sprintf;
use function str_repeat;
use function substr;

use const DIRECTORY_SEPARATOR;
use const PATH_SEPARATOR;

/**
* Client-side encryption spec tests.
*
Expand Down Expand Up @@ -123,10 +117,6 @@ public function setUp(): void
parent::setUp();

$this->skipIfClientSideEncryptionIsNotSupported();

if (! static::isCryptSharedLibAvailable() && ! static::isMongocryptdAvailable()) {
$this->markTestSkipped('Neither crypt_shared nor mongocryptd are available');
}
}

/**
Expand Down Expand Up @@ -1988,28 +1978,4 @@ private function prepareCorpusData(string $fieldName, stdClass $data, ClientEncr

return $data->allowed ? $returnData : $data;
}

private static function isCryptSharedLibAvailable(): bool
{
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');

if ($cryptSharedLibPath === false) {
return false;
}

return is_readable($cryptSharedLibPath);
}

private static function isMongocryptdAvailable(): bool
{
$paths = explode(PATH_SEPARATOR, getenv("PATH"));

foreach ($paths as $path) {
if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) {
return true;
}
}

return false;
}
}
32 changes: 3 additions & 29 deletions tests/UnifiedSpecTests/UnifiedTestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
use function getenv;
use function implode;
use function in_array;
use function is_executable;
use function is_readable;
use function is_string;
use function parse_url;
use function PHPUnit\Framework\assertContainsOnly;
Expand All @@ -42,9 +40,7 @@
use function substr_replace;
use function version_compare;

use const DIRECTORY_SEPARATOR;
use const FILTER_VALIDATE_BOOLEAN;
use const PATH_SEPARATOR;

/**
* Unified test runner.
Expand Down Expand Up @@ -341,6 +337,8 @@ private function isAuthenticated(): bool

/**
* Return whether client-side encryption is supported.
*
* @see FunctionalTestCase::skipIfClientSideEncryptionIsNotSupported()
*/
private function isClientSideEncryptionSupported(): bool
{
Expand All @@ -354,31 +352,7 @@ private function isClientSideEncryptionSupported(): bool
return false;
}

return static::isCryptSharedLibAvailable() || static::isMongocryptdAvailable();
}

private static function isCryptSharedLibAvailable(): bool
{
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');

if ($cryptSharedLibPath === false) {
return false;
}

return is_readable($cryptSharedLibPath);
}

private static function isMongocryptdAvailable(): bool
{
$paths = explode(PATH_SEPARATOR, getenv("PATH"));

foreach ($paths as $path) {
if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) {
return true;
}
}

return false;
return FunctionalTestCase::isCryptSharedLibAvailable() || FunctionalTestCase::isMongocryptdAvailable();
}

/**
Expand Down

0 comments on commit 28f6ee8

Please sign in to comment.