Skip to content

Commit

Permalink
Refactor isCryptSharedLibAvailable & isMongocryptdAvailable in the ba…
Browse files Browse the repository at this point in the history
…se FunctionalTestCase
  • Loading branch information
GromNaN committed Jun 20, 2023
1 parent 492e9a2 commit 9202f0c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 119 deletions.
4 changes: 3 additions & 1 deletion tests/DocumentationExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1821,7 +1821,9 @@ public function testQueryableEncryption(): void
$this->markTestSkipped('Explicit encryption tests require MongoDB 7.0 or later');
}

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

// Fetch names for the database and collection under test
$collectionName = $this->getCollectionName();
Expand Down
38 changes: 38 additions & 0 deletions tests/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
use function implode;
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 @@ -43,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 @@ -556,6 +560,40 @@ protected function skipIfTransactionsAreNotSupported(): void
}
}

/**
* The Automatic Encryption Shared Library is installed with MongoDB Enterprise or Atlas (version 6.0 and later).
*
* @see https://www.mongodb.com/docs/manual/core/queryable-encryption/reference/shared-library/
*/
protected static function isCryptSharedLibAvailable(): bool
{
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');

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

return is_readable($cryptSharedLibPath);
}

/**
* mongocryptd is installed with MongoDB Enterprise Server (version 4.2 and later).
*
* @see https://www.mongodb.com/docs/manual/core/queryable-encryption/reference/mongocryptd/
*/
protected 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,15 +12,9 @@
use MongoDB\Operation\CreateEncryptedCollection;

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

use const DIRECTORY_SEPARATOR;
use const PATH_SEPARATOR;

class CreateEncryptedCollectionFunctionalTest extends FunctionalTestCase
{
public const LOCAL_MASTERKEY = 'Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk';
Expand Down Expand Up @@ -220,28 +214,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 @@ -9,17 +9,11 @@
use PHPUnit\Framework\Assert;
use stdClass;

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

use const DIRECTORY_SEPARATOR;
use const PATH_SEPARATOR;

/**
* Base class for client-side encryption prose tests.
*
Expand Down Expand Up @@ -103,28 +97,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;
}
}
30 changes: 0 additions & 30 deletions tests/SpecTests/ClientSideEncryptionSpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,10 @@
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;
Expand All @@ -48,9 +45,6 @@
use function unserialize;
use function version_compare;

use const DIRECTORY_SEPARATOR;
use const PATH_SEPARATOR;

/**
* Client-side encryption spec tests.
*
Expand Down Expand Up @@ -2001,28 +1995,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;
}
}
28 changes: 0 additions & 28 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 @@ -357,30 +353,6 @@ private function isClientSideEncryptionSupported(): bool
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 whether serverless (i.e. proxy as mongos) is being utilized.
*/
Expand Down

0 comments on commit 9202f0c

Please sign in to comment.