31
31
use function glob ;
32
32
use function in_array ;
33
33
use function is_executable ;
34
+ use function is_readable ;
34
35
use function iterator_to_array ;
35
36
use function json_decode ;
36
37
use function sprintf ;
46
47
* Client-side encryption spec tests.
47
48
*
48
49
* @see https://github.com/mongodb/specifications/tree/master/source/client-side-encryption
50
+ * @group csfle
49
51
*/
50
52
class ClientSideEncryptionSpecTest extends FunctionalTestCase
51
53
{
@@ -65,7 +67,10 @@ public function setUp(): void
65
67
parent ::setUp ();
66
68
67
69
$ this ->skipIfClientSideEncryptionIsNotSupported ();
68
- $ this ->skipIfLocalMongocryptdIsUnavailable ();
70
+
71
+ if (! static ::isCryptSharedLibAvailable () && ! static ::isMongocryptdAvailable ()) {
72
+ $ this ->markTestSkipped ('Neither crypt_shared nor mongocryptd are available ' );
73
+ }
69
74
}
70
75
71
76
/**
@@ -79,6 +84,15 @@ public static function assertCommandMatches(stdClass $expected, stdClass $actual
79
84
static ::assertDocumentsMatch ($ expected , $ actual );
80
85
}
81
86
87
+ public static function createTestClient (?string $ uri = null , array $ options = [], array $ driverOptions = []): Client
88
+ {
89
+ if (isset ($ driverOptions ['autoEncryption ' ]) && getenv ('CRYPT_SHARED_LIB_PATH ' )) {
90
+ $ driverOptions ['autoEncryption ' ]['extraOptions ' ]['cryptSharedLibPath ' ] = getenv ('CRYPT_SHARED_LIB_PATH ' );
91
+ }
92
+
93
+ return parent ::createTestClient ($ uri , $ options , $ driverOptions );
94
+ }
95
+
82
96
/**
83
97
* Execute an individual test case from the specification.
84
98
*
@@ -825,6 +839,14 @@ static function (self $test, ClientEncryption $clientEncryption, ClientEncryptio
825
839
*/
826
840
public function testBypassSpawningMongocryptdViaBypassSpawn (): void
827
841
{
842
+ /* If crypt_shared is available it will likely already have been loaded
843
+ * by a previous test so there is no way to prevent it from being used.
844
+ * Since CSFLE prefers crypt_shared to mongocryptd there is reason to
845
+ * run any of the "bypass spawning" tests (see also: MONGOCRYPT-421). */
846
+ if (static ::isCryptSharedLibAvailable ()) {
847
+ $ this ->markTestSkipped ('Bypass spawning of mongocryptd cannot be tested when crypt_shared is available ' );
848
+ }
849
+
828
850
$ autoEncryptionOpts = [
829
851
'keyVaultNamespace ' => 'keyvault.datakeys ' ,
830
852
'kmsProviders ' => [
@@ -840,6 +862,7 @@ public function testBypassSpawningMongocryptdViaBypassSpawn(): void
840
862
],
841
863
];
842
864
865
+ // Disable adding cryptSharedLibPath, as it may interfere with this test
843
866
$ clientEncrypted = static ::createTestClient (null , [], ['autoEncryption ' => $ autoEncryptionOpts ]);
844
867
845
868
try {
@@ -860,6 +883,10 @@ public function testBypassSpawningMongocryptdViaBypassSpawn(): void
860
883
*/
861
884
public function testBypassSpawningMongocryptdViaBypassAutoEncryption (): void
862
885
{
886
+ if (static ::isCryptSharedLibAvailable ()) {
887
+ $ this ->markTestSkipped ('Bypass spawning of mongocryptd cannot be tested when crypt_shared is available ' );
888
+ }
889
+
863
890
$ autoEncryptionOpts = [
864
891
'keyVaultNamespace ' => 'keyvault.datakeys ' ,
865
892
'kmsProviders ' => [
@@ -871,6 +898,7 @@ public function testBypassSpawningMongocryptdViaBypassAutoEncryption(): void
871
898
],
872
899
];
873
900
901
+ // Disable adding cryptSharedLibPath, as it may interfere with this test
874
902
$ clientEncrypted = static ::createTestClient (null , [], ['autoEncryption ' => $ autoEncryptionOpts ]);
875
903
876
904
$ clientEncrypted ->selectCollection ('db ' , 'coll ' )->insertOne (['unencrypted ' => 'test ' ]);
@@ -888,6 +916,10 @@ public function testBypassSpawningMongocryptdViaBypassAutoEncryption(): void
888
916
*/
889
917
public function testBypassSpawningMongocryptdViaBypassQueryAnalysis (): void
890
918
{
919
+ if (static ::isCryptSharedLibAvailable ()) {
920
+ $ this ->markTestSkipped ('Bypass spawning of mongocryptd cannot be tested when crypt_shared is available ' );
921
+ }
922
+
891
923
$ autoEncryptionOpts = [
892
924
'keyVaultNamespace ' => 'keyvault.datakeys ' ,
893
925
'kmsProviders ' => [
@@ -899,6 +931,7 @@ public function testBypassSpawningMongocryptdViaBypassQueryAnalysis(): void
899
931
],
900
932
];
901
933
934
+ // Disable adding cryptSharedLibPath, as it may interfere with this test
902
935
$ clientEncrypted = static ::createTestClient (null , [], ['autoEncryption ' => $ autoEncryptionOpts ]);
903
936
904
937
$ clientEncrypted ->selectCollection ('db ' , 'coll ' )->insertOne (['unencrypted ' => 'test ' ]);
@@ -1525,16 +1558,27 @@ private function prepareEncryptedFieldsMap(stdClass $encryptedFieldsMap): stdCla
1525
1558
return $ encryptedFieldsMap ;
1526
1559
}
1527
1560
1528
- private function skipIfLocalMongocryptdIsUnavailable (): void
1561
+ private static function isCryptSharedLibAvailable (): bool
1562
+ {
1563
+ $ cryptSharedLibPath = getenv ('CRYPT_SHARED_LIB_PATH ' );
1564
+
1565
+ if ($ cryptSharedLibPath === false ) {
1566
+ return false ;
1567
+ }
1568
+
1569
+ return is_readable ($ cryptSharedLibPath );
1570
+ }
1571
+
1572
+ private static function isMongocryptdAvailable (): bool
1529
1573
{
1530
1574
$ paths = explode (PATH_SEPARATOR , getenv ("PATH " ));
1531
1575
1532
1576
foreach ($ paths as $ path ) {
1533
1577
if (is_executable ($ path . DIRECTORY_SEPARATOR . 'mongocryptd ' )) {
1534
- return ;
1578
+ return true ;
1535
1579
}
1536
1580
}
1537
1581
1538
- $ this -> markTestSkipped ( ' Mongocryptd is not available on the localhost ' ) ;
1582
+ return false ;
1539
1583
}
1540
1584
}
0 commit comments