From 4956b57028c3627b201b8e23720aa70206c13e13 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Fri, 14 Jun 2024 10:17:36 -0400 Subject: [PATCH] PHPC-2401: Support QEv2 range protocol (#1583) Existing RANGE_PREVIEW constants are deprecated in favor of new RANGE constants introduced in libmongoc 1.28.0. PHP 8.3+ emits deprecation messages when accessing the RANGE_PREVIEW constants, so we ignore such output in the constants test. It wasn't worth splitting that test by PHP version. Support trimFactor range option. Specify default value for trimFactor range option in tests. This is temporary until sparsity and trimFactor are made optional prior to the GA release for range indexes (PHPC-2403). --- src/MongoDB/ClientEncryption.c | 11 ++++++++++ src/MongoDB/ClientEncryption.stub.php | 14 +++++++++++++ src/MongoDB/ClientEncryption_arginfo.h | 20 ++++++++++++++++--- .../clientEncryption-constants.phpt | 10 +++++++--- ...lientEncryption-encryptExpression-001.phpt | 6 +++--- ...ncryption-encryptExpression_error-002.phpt | 6 +++--- 6 files changed, 55 insertions(+), 12 deletions(-) diff --git a/src/MongoDB/ClientEncryption.c b/src/MongoDB/ClientEncryption.c index ee5ef3bd7..8dc85edaa 100644 --- a/src/MongoDB/ClientEncryption.c +++ b/src/MongoDB/ClientEncryption.c @@ -850,6 +850,17 @@ static mongoc_client_encryption_encrypt_range_opts_t* phongo_clientencryption_en return opts; } + if (php_array_existsc(options, "trimFactor")) { + int64_t trimfactor = php_array_fetchc_long(options, "trimFactor"); + + if (trimfactor < 0 || trimfactor > INT32_MAX) { + phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected \"trimFactor\" range option to be a positive 32-bit integer, %" PRId64 " given", trimfactor); + goto cleanup; + } + + mongoc_client_encryption_encrypt_range_opts_set_trim_factor(opts, (int32_t) trimfactor); + } + if (php_array_existsc(options, "sparsity")) { int64_t sparsity = php_array_fetchc_long(options, "sparsity"); diff --git a/src/MongoDB/ClientEncryption.stub.php b/src/MongoDB/ClientEncryption.stub.php index d64527f27..b39fccfbc 100644 --- a/src/MongoDB/ClientEncryption.stub.php +++ b/src/MongoDB/ClientEncryption.stub.php @@ -34,6 +34,13 @@ final class ClientEncryption public const ALGORITHM_UNINDEXED = UNKNOWN; /** + * @var string + * @cvalue MONGOC_ENCRYPT_ALGORITHM_RANGE + */ + public const ALGORITHM_RANGE = UNKNOWN; + + /** + * @deprecated * @var string * @cvalue MONGOC_ENCRYPT_ALGORITHM_RANGEPREVIEW */ @@ -46,6 +53,13 @@ final class ClientEncryption public const QUERY_TYPE_EQUALITY = UNKNOWN; /** + * @var string + * @cvalue MONGOC_ENCRYPT_QUERY_TYPE_RANGE + */ + public const QUERY_TYPE_RANGE = UNKNOWN; + + /** + * @deprecated * @var string * @cvalue MONGOC_ENCRYPT_QUERY_TYPE_RANGEPREVIEW */ diff --git a/src/MongoDB/ClientEncryption_arginfo.h b/src/MongoDB/ClientEncryption_arginfo.h index 0df229f5b..9e779d592 100644 --- a/src/MongoDB/ClientEncryption_arginfo.h +++ b/src/MongoDB/ClientEncryption_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: ec6f4a230cb96e8985f250b7d716dd86fbc5e0ea */ + * Stub hash: 6b4f3a99f4dd97b08ee22a15985f0ffc4fe34ace */ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_MongoDB_Driver_ClientEncryption___construct, 0, 0, 1) ZEND_ARG_TYPE_INFO(0, options, IS_ARRAY, 0) @@ -193,11 +193,18 @@ static zend_class_entry *register_class_MongoDB_Driver_ClientEncryption(void) zend_declare_class_constant_ex(class_entry, const_ALGORITHM_UNINDEXED_name, &const_ALGORITHM_UNINDEXED_value, ZEND_ACC_PUBLIC, NULL); zend_string_release(const_ALGORITHM_UNINDEXED_name); + zval const_ALGORITHM_RANGE_value; + zend_string *const_ALGORITHM_RANGE_value_str = zend_string_init(MONGOC_ENCRYPT_ALGORITHM_RANGE, strlen(MONGOC_ENCRYPT_ALGORITHM_RANGE), 1); + ZVAL_STR(&const_ALGORITHM_RANGE_value, const_ALGORITHM_RANGE_value_str); + zend_string *const_ALGORITHM_RANGE_name = zend_string_init_interned("ALGORITHM_RANGE", sizeof("ALGORITHM_RANGE") - 1, 1); + zend_declare_class_constant_ex(class_entry, const_ALGORITHM_RANGE_name, &const_ALGORITHM_RANGE_value, ZEND_ACC_PUBLIC, NULL); + zend_string_release(const_ALGORITHM_RANGE_name); + zval const_ALGORITHM_RANGE_PREVIEW_value; zend_string *const_ALGORITHM_RANGE_PREVIEW_value_str = zend_string_init(MONGOC_ENCRYPT_ALGORITHM_RANGEPREVIEW, strlen(MONGOC_ENCRYPT_ALGORITHM_RANGEPREVIEW), 1); ZVAL_STR(&const_ALGORITHM_RANGE_PREVIEW_value, const_ALGORITHM_RANGE_PREVIEW_value_str); zend_string *const_ALGORITHM_RANGE_PREVIEW_name = zend_string_init_interned("ALGORITHM_RANGE_PREVIEW", sizeof("ALGORITHM_RANGE_PREVIEW") - 1, 1); - zend_declare_class_constant_ex(class_entry, const_ALGORITHM_RANGE_PREVIEW_name, &const_ALGORITHM_RANGE_PREVIEW_value, ZEND_ACC_PUBLIC, NULL); + zend_declare_class_constant_ex(class_entry, const_ALGORITHM_RANGE_PREVIEW_name, &const_ALGORITHM_RANGE_PREVIEW_value, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED, NULL); zend_string_release(const_ALGORITHM_RANGE_PREVIEW_name); zval const_QUERY_TYPE_EQUALITY_value; @@ -207,11 +214,18 @@ static zend_class_entry *register_class_MongoDB_Driver_ClientEncryption(void) zend_declare_class_constant_ex(class_entry, const_QUERY_TYPE_EQUALITY_name, &const_QUERY_TYPE_EQUALITY_value, ZEND_ACC_PUBLIC, NULL); zend_string_release(const_QUERY_TYPE_EQUALITY_name); + zval const_QUERY_TYPE_RANGE_value; + zend_string *const_QUERY_TYPE_RANGE_value_str = zend_string_init(MONGOC_ENCRYPT_QUERY_TYPE_RANGE, strlen(MONGOC_ENCRYPT_QUERY_TYPE_RANGE), 1); + ZVAL_STR(&const_QUERY_TYPE_RANGE_value, const_QUERY_TYPE_RANGE_value_str); + zend_string *const_QUERY_TYPE_RANGE_name = zend_string_init_interned("QUERY_TYPE_RANGE", sizeof("QUERY_TYPE_RANGE") - 1, 1); + zend_declare_class_constant_ex(class_entry, const_QUERY_TYPE_RANGE_name, &const_QUERY_TYPE_RANGE_value, ZEND_ACC_PUBLIC, NULL); + zend_string_release(const_QUERY_TYPE_RANGE_name); + zval const_QUERY_TYPE_RANGE_PREVIEW_value; zend_string *const_QUERY_TYPE_RANGE_PREVIEW_value_str = zend_string_init(MONGOC_ENCRYPT_QUERY_TYPE_RANGEPREVIEW, strlen(MONGOC_ENCRYPT_QUERY_TYPE_RANGEPREVIEW), 1); ZVAL_STR(&const_QUERY_TYPE_RANGE_PREVIEW_value, const_QUERY_TYPE_RANGE_PREVIEW_value_str); zend_string *const_QUERY_TYPE_RANGE_PREVIEW_name = zend_string_init_interned("QUERY_TYPE_RANGE_PREVIEW", sizeof("QUERY_TYPE_RANGE_PREVIEW") - 1, 1); - zend_declare_class_constant_ex(class_entry, const_QUERY_TYPE_RANGE_PREVIEW_name, &const_QUERY_TYPE_RANGE_PREVIEW_value, ZEND_ACC_PUBLIC, NULL); + zend_declare_class_constant_ex(class_entry, const_QUERY_TYPE_RANGE_PREVIEW_name, &const_QUERY_TYPE_RANGE_PREVIEW_value, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED, NULL); zend_string_release(const_QUERY_TYPE_RANGE_PREVIEW_name); return class_entry; diff --git a/tests/clientEncryption/clientEncryption-constants.phpt b/tests/clientEncryption/clientEncryption-constants.phpt index f03d462c6..cdbaba805 100644 --- a/tests/clientEncryption/clientEncryption-constants.phpt +++ b/tests/clientEncryption/clientEncryption-constants.phpt @@ -7,19 +7,23 @@ var_dump(MongoDB\Driver\ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMIN var_dump(MongoDB\Driver\ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_RANDOM); var_dump(MongoDB\Driver\ClientEncryption::ALGORITHM_INDEXED); var_dump(MongoDB\Driver\ClientEncryption::ALGORITHM_UNINDEXED); +var_dump(MongoDB\Driver\ClientEncryption::ALGORITHM_RANGE); var_dump(MongoDB\Driver\ClientEncryption::ALGORITHM_RANGE_PREVIEW); var_dump(MongoDB\Driver\ClientEncryption::QUERY_TYPE_EQUALITY); +var_dump(MongoDB\Driver\ClientEncryption::QUERY_TYPE_RANGE); var_dump(MongoDB\Driver\ClientEncryption::QUERY_TYPE_RANGE_PREVIEW); ?> ===DONE=== ---EXPECT-- +--EXPECTF-- string(43) "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic" string(36) "AEAD_AES_256_CBC_HMAC_SHA_512-Random" string(7) "Indexed" string(9) "Unindexed" -string(12) "RangePreview" +string(5) "Range" +%Astring(12) "RangePreview" string(8) "equality" -string(12) "rangePreview" +string(5) "range" +%Astring(12) "rangePreview" ===DONE=== diff --git a/tests/clientEncryption/clientEncryption-encryptExpression-001.phpt b/tests/clientEncryption/clientEncryption-encryptExpression-001.phpt index f3d61ee49..24246fd18 100644 --- a/tests/clientEncryption/clientEncryption-encryptExpression-001.phpt +++ b/tests/clientEncryption/clientEncryption-encryptExpression-001.phpt @@ -22,10 +22,10 @@ $keyId = $clientEncryption->createDataKey('local'); $encryptOpts = [ 'keyId' => $keyId, - 'algorithm' => MongoDB\Driver\ClientEncryption::ALGORITHM_RANGE_PREVIEW, - 'queryType' => MongoDB\Driver\ClientEncryption::QUERY_TYPE_RANGE_PREVIEW, + 'algorithm' => MongoDB\Driver\ClientEncryption::ALGORITHM_RANGE, + 'queryType' => MongoDB\Driver\ClientEncryption::QUERY_TYPE_RANGE, 'contentionFactor' => 0, - 'rangeOpts' => ['min' => 0, 'max' => 200, 'sparsity' => 1], + 'rangeOpts' => ['min' => 0, 'max' => 200, 'sparsity' => 1, 'trimFactor' => 1], ]; $expr = [ diff --git a/tests/clientEncryption/clientEncryption-encryptExpression_error-002.phpt b/tests/clientEncryption/clientEncryption-encryptExpression_error-002.phpt index b65da9f2a..e8029bfcd 100644 --- a/tests/clientEncryption/clientEncryption-encryptExpression_error-002.phpt +++ b/tests/clientEncryption/clientEncryption-encryptExpression_error-002.phpt @@ -22,10 +22,10 @@ $keyId = $clientEncryption->createDataKey('local'); $encryptOpts = [ 'keyId' => $keyId, - 'algorithm' => MongoDB\Driver\ClientEncryption::ALGORITHM_RANGE_PREVIEW, - 'queryType' => MongoDB\Driver\ClientEncryption::QUERY_TYPE_RANGE_PREVIEW, + 'algorithm' => MongoDB\Driver\ClientEncryption::ALGORITHM_RANGE, + 'queryType' => MongoDB\Driver\ClientEncryption::QUERY_TYPE_RANGE, 'contentionFactor' => 0, - 'rangeOpts' => ['min' => 0, 'max' => 200, 'sparsity' => 1], + 'rangeOpts' => ['min' => 0, 'max' => 200, 'sparsity' => 1, 'trimFactor' => 1], ]; echo throws(function() use ($clientEncryption, $encryptOpts) {