diff --git a/UPGRADE-2.0.md b/UPGRADE-2.0.md index 19fff408e..f9392dc97 100644 --- a/UPGRADE-2.0.md +++ b/UPGRADE-2.0.md @@ -38,3 +38,6 @@ UPGRADE FROM 1.x to 2.0 `allowPartialResults` instead), `maxScan`, `modifiers` (use alternative top-level options instead), `oplogReplay`, and `snapshot`. Support for negative `limit` values has been removed (use `singleBatch` instead). + * The `MongoDB\Driver\Manager` constructor now throws if the URI options array + includes a non-boolean value for an option expecting a boolean. This behavior + is now consistent with validation for other option types. diff --git a/src/phongo_client.c b/src/phongo_client.c index bbe0250fa..70e545920 100644 --- a/src/phongo_client.c +++ b/src/phongo_client.c @@ -143,10 +143,12 @@ static bool php_phongo_apply_options_to_uri(mongoc_uri_t* uri, bson_t* options) } if (mongoc_uri_option_is_bool(key)) { - /* The option's type is not validated because bson_iter_as_bool() is - * used to cast the value to a boolean. Validation may be introduced - * in PHPC-990. */ - if (!mongoc_uri_set_option_as_bool(uri, key, bson_iter_as_bool(&iter))) { + if (!BSON_ITER_HOLDS_BOOL(&iter)) { + PHONGO_URI_INVALID_TYPE(iter, "boolean"); + return false; + } + + if (!mongoc_uri_set_option_as_bool(uri, key, bson_iter_bool(&iter))) { phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse \"%s\" URI option", key); return false; } @@ -154,6 +156,10 @@ static bool php_phongo_apply_options_to_uri(mongoc_uri_t* uri, bson_t* options) continue; } + /* Note: mongoc_uri_option_is_int32 also accepts int64 options, but + * BSON_ITER_HOLDS_INT32 would reject a 64-bit value. This is not a + * problem as MONGOC_URI_WTIMEOUTMS is the only 64-bit option and it is + * handled explicitly in php_phongo_apply_wc_options_to_uri. */ if (mongoc_uri_option_is_int32(key)) { if (!BSON_ITER_HOLDS_INT32(&iter)) { PHONGO_URI_INVALID_TYPE(iter, "32-bit integer"); diff --git a/tests/manager/manager-ctor_error-003.phpt b/tests/manager/manager-ctor_error-003.phpt index a4fbfb797..f0f447986 100644 --- a/tests/manager/manager-ctor_error-003.phpt +++ b/tests/manager/manager-ctor_error-003.phpt @@ -5,13 +5,47 @@ MongoDB\Driver\Manager::__construct(): invalid types in URI options arrays require_once __DIR__ . '/../utils/basic.inc'; -/* Note: generic boolean options (e.g. "ssl") are not tested because the driver - * uses bson_iter_as_bool() to cast the value to a boolean for assignment. - * - * Read concern, read preference, and write concern options are tested in their - * respective test files. */ +/* Note: read concern, read preference, and write concern options are tested in + * their respective test files. */ -echo "Testing 32-bit integer options:\n"; +echo "Testing boolean options:\n"; + +$booleanOptions = [ + 'canonicalizeHostname', + 'directConnection', + 'journal', + 'loadBalanced', + 'retryReads', + 'retryWrites', + 'safe', + 'serverSelectionTryOnce', + 'ssl', + 'tls', + 'tlsAllowInvalidCertificates', + 'tlsAllowInvalidHostnames', + 'tlsDisableCertificateRevocationCheck', + 'tlsDisableOCSPEndpointCheck', + 'tlsInsecure', +]; + +$invalidBooleanValues = [ + 1.0, + 42, + 'string', + new MongoDB\BSON\ObjectId, + [ 1, 2, 3 ], + ['x' => 1], +]; + +foreach ($booleanOptions as $option) { + foreach ($invalidBooleanValues as $value) { + echo throws(function() use ($option, $value) { + create_test_manager(null, [$option => $value]); + }, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n"; + } +} + +echo "\nTesting 32-bit integer options:\n"; $integerOptions = [ 'connectTimeoutMS', @@ -20,6 +54,8 @@ $integerOptions = [ 'serverSelectionTimeoutMS', 'socketCheckIntervalMS', 'socketTimeoutMS', + 'srvMaxHosts', + 'zlibCompressionLevel', ]; $invalidIntegerValues = [ @@ -35,7 +71,7 @@ foreach ($integerOptions as $option) { foreach ($invalidIntegerValues as $value) { echo throws(function() use ($option, $value) { create_test_manager(null, [$option => $value]); - }, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n"; + }, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n"; } } @@ -45,9 +81,14 @@ $stringOptions = [ 'appname', 'authMechanism', 'authSource', + 'compressors', 'gssapiServiceName', 'password', 'replicaSet', + 'srvServiceName', + 'tlsCAFile', + 'tlsCertificateKeyFile', + 'tlsCertificateKeyFilePassword', 'username', ]; @@ -64,7 +105,7 @@ foreach ($stringOptions as $option) { foreach ($invalidStringValues as $value) { echo throws(function() use ($option, $value) { create_test_manager(null, [$option => $value]); - }, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n"; + }, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n"; } } @@ -82,13 +123,195 @@ $invalidDocumentValues = [ foreach ($invalidDocumentValues as $value) { echo throws(function() use ($value) { create_test_manager(null, ['authMechanismProperties' => $value]); - }, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n"; + }, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n"; } ?> ===DONE=== --EXPECT-- +Testing boolean options: +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "canonicalizeHostname" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "canonicalizeHostname" URI option, 32-bit integer given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "canonicalizeHostname" URI option, string given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "canonicalizeHostname" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "canonicalizeHostname" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "canonicalizeHostname" URI option, document given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "directConnection" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "directConnection" URI option, 32-bit integer given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "directConnection" URI option, string given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "directConnection" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "directConnection" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "directConnection" URI option, document given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "journal" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "journal" URI option, 32-bit integer given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "journal" URI option, string given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "journal" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "journal" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "journal" URI option, document given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "loadBalanced" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "loadBalanced" URI option, 32-bit integer given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "loadBalanced" URI option, string given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "loadBalanced" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "loadBalanced" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "loadBalanced" URI option, document given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "retryReads" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "retryReads" URI option, 32-bit integer given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "retryReads" URI option, string given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "retryReads" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "retryReads" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "retryReads" URI option, document given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "retryWrites" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "retryWrites" URI option, 32-bit integer given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "retryWrites" URI option, string given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "retryWrites" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "retryWrites" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "retryWrites" URI option, document given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "safe" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "safe" URI option, 32-bit integer given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "safe" URI option, string given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "safe" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "safe" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "safe" URI option, document given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "serverSelectionTryOnce" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "serverSelectionTryOnce" URI option, 32-bit integer given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "serverSelectionTryOnce" URI option, string given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "serverSelectionTryOnce" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "serverSelectionTryOnce" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "serverSelectionTryOnce" URI option, document given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "ssl" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "ssl" URI option, 32-bit integer given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "ssl" URI option, string given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "ssl" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "ssl" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "ssl" URI option, document given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tls" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tls" URI option, 32-bit integer given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tls" URI option, string given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tls" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tls" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tls" URI option, document given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsAllowInvalidCertificates" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsAllowInvalidCertificates" URI option, 32-bit integer given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsAllowInvalidCertificates" URI option, string given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsAllowInvalidCertificates" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsAllowInvalidCertificates" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsAllowInvalidCertificates" URI option, document given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsAllowInvalidHostnames" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsAllowInvalidHostnames" URI option, 32-bit integer given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsAllowInvalidHostnames" URI option, string given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsAllowInvalidHostnames" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsAllowInvalidHostnames" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsAllowInvalidHostnames" URI option, document given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsDisableCertificateRevocationCheck" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsDisableCertificateRevocationCheck" URI option, 32-bit integer given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsDisableCertificateRevocationCheck" URI option, string given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsDisableCertificateRevocationCheck" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsDisableCertificateRevocationCheck" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsDisableCertificateRevocationCheck" URI option, document given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsDisableOCSPEndpointCheck" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsDisableOCSPEndpointCheck" URI option, 32-bit integer given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsDisableOCSPEndpointCheck" URI option, string given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsDisableOCSPEndpointCheck" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsDisableOCSPEndpointCheck" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsDisableOCSPEndpointCheck" URI option, document given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsInsecure" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsInsecure" URI option, 32-bit integer given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsInsecure" URI option, string given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsInsecure" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsInsecure" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected boolean for "tlsInsecure" URI option, document given + Testing 32-bit integer options: OK: Got MongoDB\Driver\Exception\InvalidArgumentException Expected 32-bit integer for "connectTimeoutMS" URI option, boolean given @@ -162,6 +385,30 @@ OK: Got MongoDB\Driver\Exception\InvalidArgumentException Expected 32-bit integer for "socketTimeoutMS" URI option, array given OK: Got MongoDB\Driver\Exception\InvalidArgumentException Expected 32-bit integer for "socketTimeoutMS" URI option, document given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected 32-bit integer for "srvMaxHosts" URI option, boolean given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected 32-bit integer for "srvMaxHosts" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected 32-bit integer for "srvMaxHosts" URI option, string given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected 32-bit integer for "srvMaxHosts" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected 32-bit integer for "srvMaxHosts" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected 32-bit integer for "srvMaxHosts" URI option, document given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected 32-bit integer for "zlibCompressionLevel" URI option, boolean given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected 32-bit integer for "zlibCompressionLevel" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected 32-bit integer for "zlibCompressionLevel" URI option, string given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected 32-bit integer for "zlibCompressionLevel" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected 32-bit integer for "zlibCompressionLevel" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected 32-bit integer for "zlibCompressionLevel" URI option, document given Testing string options: OK: Got MongoDB\Driver\Exception\InvalidArgumentException @@ -201,6 +448,18 @@ Expected string for "authSource" URI option, array given OK: Got MongoDB\Driver\Exception\InvalidArgumentException Expected string for "authSource" URI option, document given OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "compressors" URI option, boolean given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "compressors" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "compressors" URI option, 32-bit integer given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "compressors" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "compressors" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "compressors" URI option, document given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException Expected string for "gssapiServiceName" URI option, boolean given OK: Got MongoDB\Driver\Exception\InvalidArgumentException Expected string for "gssapiServiceName" URI option, double given @@ -237,6 +496,54 @@ Expected string for "replicaSet" URI option, array given OK: Got MongoDB\Driver\Exception\InvalidArgumentException Expected string for "replicaSet" URI option, document given OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "srvServiceName" URI option, boolean given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "srvServiceName" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "srvServiceName" URI option, 32-bit integer given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "srvServiceName" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "srvServiceName" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "srvServiceName" URI option, document given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "tlsCAFile" URI option, boolean given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "tlsCAFile" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "tlsCAFile" URI option, 32-bit integer given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "tlsCAFile" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "tlsCAFile" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "tlsCAFile" URI option, document given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "tlsCertificateKeyFile" URI option, boolean given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "tlsCertificateKeyFile" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "tlsCertificateKeyFile" URI option, 32-bit integer given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "tlsCertificateKeyFile" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "tlsCertificateKeyFile" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "tlsCertificateKeyFile" URI option, document given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "tlsCertificateKeyFilePassword" URI option, boolean given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "tlsCertificateKeyFilePassword" URI option, double given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "tlsCertificateKeyFilePassword" URI option, 32-bit integer given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "tlsCertificateKeyFilePassword" URI option, ObjectId given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "tlsCertificateKeyFilePassword" URI option, array given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Expected string for "tlsCertificateKeyFilePassword" URI option, document given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException Expected string for "username" URI option, boolean given OK: Got MongoDB\Driver\Exception\InvalidArgumentException Expected string for "username" URI option, double given