Skip to content

Commit 28f6ee8

Browse files
authored
PHPLIB-892: Require crypt_shared and/or mongocryptd for tests utilizing enterprise CSFLE features (#1072)
1 parent ff6df9b commit 28f6ee8

File tree

6 files changed

+38
-142
lines changed

6 files changed

+38
-142
lines changed

tests/DocumentationExamplesTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,10 +1810,7 @@ public function testQueryableEncryption(): void
18101810
}
18111811

18121812
$this->skipIfServerVersion('<', '7.0.0', 'Explicit encryption tests require MongoDB 7.0 or later');
1813-
1814-
if (! $this->isEnterprise()) {
1815-
$this->markTestSkipped('Automatic encryption requires MongoDB Enterprise');
1816-
}
1813+
$this->skipIfClientSideEncryptionIsNotSupported();
18171814

18181815
// Fetch names for the database and collection under test
18191816
$collectionName = $this->getCollectionName();

tests/FunctionalTestCase.php

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@
2828
use function filter_var;
2929
use function getenv;
3030
use function implode;
31-
use function in_array;
3231
use function is_array;
3332
use function is_callable;
33+
use function is_executable;
3434
use function is_object;
35+
use function is_readable;
3536
use function is_string;
3637
use function key;
3738
use function ob_get_clean;
@@ -44,8 +45,10 @@
4445
use function sprintf;
4546
use function version_compare;
4647

48+
use const DIRECTORY_SEPARATOR;
4749
use const FILTER_VALIDATE_BOOLEAN;
4850
use const INFO_MODULES;
51+
use const PATH_SEPARATOR;
4952

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

400-
protected function isEnterprise(): bool
401-
{
402-
$buildInfo = $this->getPrimaryServer()->executeCommand(
403-
$this->getDatabaseName(),
404-
new Command(['buildInfo' => 1])
405-
)->toArray()[0];
406-
407-
if (isset($buildInfo->modules) && is_array($buildInfo->modules)) {
408-
return in_array('enterprise', $buildInfo->modules);
409-
}
410-
411-
throw new UnexpectedValueException('Could not determine server modules');
412-
}
413-
414403
protected function isLoadBalanced()
415404
{
416405
return $this->getPrimaryServer()->getType() == Server::TYPE_LOAD_BALANCER;
@@ -542,6 +531,10 @@ protected function skipIfClientSideEncryptionIsNotSupported(): void
542531
if (static::getModuleInfo('libmongocrypt') === 'disabled') {
543532
$this->markTestSkipped('Client Side Encryption is not enabled in the MongoDB extension');
544533
}
534+
535+
if (! static::isCryptSharedLibAvailable() && ! static::isMongocryptdAvailable()) {
536+
$this->markTestSkipped('Neither crypt_shared nor mongocryptd are available');
537+
}
545538
}
546539

547540
protected function skipIfGeoHaystackIndexIsNotSupported(): void
@@ -578,6 +571,32 @@ protected function skipIfTransactionsAreNotSupported(): void
578571
}
579572
}
580573

574+
/** @see https://www.mongodb.com/docs/manual/core/queryable-encryption/reference/shared-library/ */
575+
public static function isCryptSharedLibAvailable(): bool
576+
{
577+
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');
578+
579+
if ($cryptSharedLibPath === false) {
580+
return false;
581+
}
582+
583+
return is_readable($cryptSharedLibPath);
584+
}
585+
586+
/** @see https://www.mongodb.com/docs/manual/core/queryable-encryption/reference/mongocryptd/ */
587+
public static function isMongocryptdAvailable(): bool
588+
{
589+
$paths = explode(PATH_SEPARATOR, getenv("PATH"));
590+
591+
foreach ($paths as $path) {
592+
if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) {
593+
return true;
594+
}
595+
}
596+
597+
return false;
598+
}
599+
581600
private static function appendAuthenticationOptions(array $options): array
582601
{
583602
if (isset($options['username']) || isset($options['password'])) {

tests/Operation/CreateEncryptedCollectionFunctionalTest.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@
1212
use MongoDB\Operation\CreateEncryptedCollection;
1313

1414
use function base64_decode;
15-
use function explode;
1615
use function getenv;
17-
use function is_executable;
18-
use function is_readable;
19-
20-
use const DIRECTORY_SEPARATOR;
21-
use const PATH_SEPARATOR;
2216

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

218212
return parent::createTestClient($uri, $options, $driverOptions);
219213
}
220-
221-
private static function isCryptSharedLibAvailable(): bool
222-
{
223-
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');
224-
225-
if ($cryptSharedLibPath === false) {
226-
return false;
227-
}
228-
229-
return is_readable($cryptSharedLibPath);
230-
}
231-
232-
private static function isMongocryptdAvailable(): bool
233-
{
234-
$paths = explode(PATH_SEPARATOR, getenv("PATH"));
235-
236-
foreach ($paths as $path) {
237-
if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) {
238-
return true;
239-
}
240-
}
241-
242-
return false;
243-
}
244214
}

tests/SpecTests/ClientSideEncryption/FunctionalTestCase.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,9 @@
88
use PHPUnit\Framework\Assert;
99
use stdClass;
1010

11-
use function explode;
1211
use function getenv;
13-
use function is_executable;
14-
use function is_readable;
1512
use function sprintf;
1613

17-
use const DIRECTORY_SEPARATOR;
18-
use const PATH_SEPARATOR;
19-
2014
/**
2115
* Base class for client-side encryption prose tests.
2216
*
@@ -92,28 +86,4 @@ private static function getEnv(string $name): string
9286

9387
return $value;
9488
}
95-
96-
private static function isCryptSharedLibAvailable(): bool
97-
{
98-
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');
99-
100-
if ($cryptSharedLibPath === false) {
101-
return false;
102-
}
103-
104-
return is_readable($cryptSharedLibPath);
105-
}
106-
107-
private static function isMongocryptdAvailable(): bool
108-
{
109-
$paths = explode(PATH_SEPARATOR, getenv("PATH"));
110-
111-
foreach ($paths as $path) {
112-
if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) {
113-
return true;
114-
}
115-
}
116-
117-
return false;
118-
}
11989
}

tests/SpecTests/ClientSideEncryptionSpecTest.php

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,16 @@
3232
use function base64_decode;
3333
use function basename;
3434
use function count;
35-
use function explode;
3635
use function file_get_contents;
3736
use function getenv;
3837
use function glob;
3938
use function in_array;
40-
use function is_executable;
41-
use function is_readable;
4239
use function iterator_to_array;
4340
use function json_decode;
4441
use function sprintf;
4542
use function str_repeat;
4643
use function substr;
4744

48-
use const DIRECTORY_SEPARATOR;
49-
use const PATH_SEPARATOR;
50-
5145
/**
5246
* Client-side encryption spec tests.
5347
*
@@ -123,10 +117,6 @@ public function setUp(): void
123117
parent::setUp();
124118

125119
$this->skipIfClientSideEncryptionIsNotSupported();
126-
127-
if (! static::isCryptSharedLibAvailable() && ! static::isMongocryptdAvailable()) {
128-
$this->markTestSkipped('Neither crypt_shared nor mongocryptd are available');
129-
}
130120
}
131121

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

19891979
return $data->allowed ? $returnData : $data;
19901980
}
1991-
1992-
private static function isCryptSharedLibAvailable(): bool
1993-
{
1994-
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');
1995-
1996-
if ($cryptSharedLibPath === false) {
1997-
return false;
1998-
}
1999-
2000-
return is_readable($cryptSharedLibPath);
2001-
}
2002-
2003-
private static function isMongocryptdAvailable(): bool
2004-
{
2005-
$paths = explode(PATH_SEPARATOR, getenv("PATH"));
2006-
2007-
foreach ($paths as $path) {
2008-
if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) {
2009-
return true;
2010-
}
2011-
}
2012-
2013-
return false;
2014-
}
20151981
}

tests/UnifiedSpecTests/UnifiedTestRunner.php

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
use function getenv;
2525
use function implode;
2626
use function in_array;
27-
use function is_executable;
28-
use function is_readable;
2927
use function is_string;
3028
use function parse_url;
3129
use function PHPUnit\Framework\assertContainsOnly;
@@ -42,9 +40,7 @@
4240
use function substr_replace;
4341
use function version_compare;
4442

45-
use const DIRECTORY_SEPARATOR;
4643
use const FILTER_VALIDATE_BOOLEAN;
47-
use const PATH_SEPARATOR;
4844

4945
/**
5046
* Unified test runner.
@@ -341,6 +337,8 @@ private function isAuthenticated(): bool
341337

342338
/**
343339
* Return whether client-side encryption is supported.
340+
*
341+
* @see FunctionalTestCase::skipIfClientSideEncryptionIsNotSupported()
344342
*/
345343
private function isClientSideEncryptionSupported(): bool
346344
{
@@ -354,31 +352,7 @@ private function isClientSideEncryptionSupported(): bool
354352
return false;
355353
}
356354

357-
return static::isCryptSharedLibAvailable() || static::isMongocryptdAvailable();
358-
}
359-
360-
private static function isCryptSharedLibAvailable(): bool
361-
{
362-
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');
363-
364-
if ($cryptSharedLibPath === false) {
365-
return false;
366-
}
367-
368-
return is_readable($cryptSharedLibPath);
369-
}
370-
371-
private static function isMongocryptdAvailable(): bool
372-
{
373-
$paths = explode(PATH_SEPARATOR, getenv("PATH"));
374-
375-
foreach ($paths as $path) {
376-
if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) {
377-
return true;
378-
}
379-
}
380-
381-
return false;
355+
return FunctionalTestCase::isCryptSharedLibAvailable() || FunctionalTestCase::isMongocryptdAvailable();
382356
}
383357

384358
/**

0 commit comments

Comments
 (0)