Skip to content
18 changes: 9 additions & 9 deletions phongo_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,22 +143,22 @@
# define ADD_ASSOC_INT64(zval, key, value) add_assoc_long(zval, key, value)
#elif SIZEOF_PHONGO_LONG == 4
# define ADD_INDEX_INT64(zval, index, value) \
if (value > INT32_MAX || value < INT32_MIN) { \
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Integer overflow detected on your platform: %lld", value); \
if ((value) > INT32_MAX || (value) < INT32_MIN) { \
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Integer overflow detected on your platform: %lld", (value)); \
} else { \
add_index_long(zval, index, value); \
add_index_long(zval, index, (value)); \
}
# define ADD_NEXT_INDEX_INT64(zval, value) \
if (value > INT32_MAX || value < INT32_MIN) { \
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Integer overflow detected on your platform: %lld", value); \
if ((value) > INT32_MAX || (value) < INT32_MIN) { \
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Integer overflow detected on your platform: %lld", (value)); \
} else { \
add_next_index_long(zval, value); \
add_next_index_long(zval, (value)); \
}
# define ADD_ASSOC_INT64(zval, key, value) \
if (value > INT32_MAX || value < INT32_MIN) { \
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Integer overflow detected on your platform: %lld", value); \
if ((value) > INT32_MAX || (value) < INT32_MIN) { \
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Integer overflow detected on your platform: %lld", (value)); \
} else { \
add_assoc_long(zval, key, value); \
add_assoc_long(zval, key, (value)); \
}
#else
# error Unsupported architecture (integers are neither 32-bit nor 64-bit)
Expand Down
2 changes: 1 addition & 1 deletion php_phongo.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ static void php_phongo_log(mongoc_log_level_t log_level, const char *log_domain,

dt = php_format_date((char *) ZEND_STRL("Y-m-d\\TH:i:s"), t, 0 TSRMLS_CC);

fprintf(MONGODB_G(debug_fd), "[%s.%06lu+00:00] %10s: %-8s> %s\n", ZSTR_VAL(dt), tu, log_domain, mongoc_log_level_str(log_level), message);
fprintf(MONGODB_G(debug_fd), "[%s.%06" PHONGO_LONG_FORMAT "+00:00] %10s: %-8s> %s\n", ZSTR_VAL(dt), tu, log_domain, mongoc_log_level_str(log_level), message);
fflush(MONGODB_G(debug_fd));
efree(dt);
}
Expand Down
18 changes: 13 additions & 5 deletions scripts/convert-bson-corpus-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
'Top-level document validity: Bad $date (number, not string or hash)' => 'Legacy extended JSON $date syntax uses numbers (CDRIVER-2223)',
];

$for64bitOnly = [
'Int64 type: MinValue' => "Can't represent 64-bit ints on a 32-bit platform",
'Int64 type: MaxValue' => "Can't represent 64-bit ints on a 32-bit platform",
];

$outputPath = realpath(__DIR__ . '/../tests') . '/bson-corpus/';

if ( ! is_dir($outputPath) && ! mkdir($outputPath, 0755, true)) {
Expand Down Expand Up @@ -42,7 +47,7 @@
foreach ($test['valid'] as $i => $case) {
$outputFile = sprintf('%s-valid-%03d.phpt', pathinfo($inputFile, PATHINFO_FILENAME), $i + 1);
try {
$output = renderPhpt(getParamsForValid($test, $case), $expectedFailures);
$output = renderPhpt(getParamsForValid($test, $case), $expectedFailures, $for64bitOnly);
} catch (Exception $e) {
printf("Error processing valid[%d] in %s: %s\n", $i, $inputFile, $e->getMessage());
continue;
Expand All @@ -59,7 +64,7 @@
foreach ($test['decodeErrors'] as $i => $case) {
$outputFile = sprintf('%s-decodeError-%03d.phpt', pathinfo($inputFile, PATHINFO_FILENAME), $i + 1);
try {
$output = renderPhpt(getParamsForDecodeError($test, $case), $expectedFailures);
$output = renderPhpt(getParamsForDecodeError($test, $case), $expectedFailures, $for64bitOnly);
} catch (Exception $e) {
printf("Error processing decodeErrors[%d] in %s: %s\n", $i, $inputFile, $e->getMessage());
continue;
Expand All @@ -76,7 +81,7 @@
foreach ($test['parseErrors'] as $i => $case) {
$outputFile = sprintf('%s-parseError-%03d.phpt', pathinfo($inputFile, PATHINFO_FILENAME), $i + 1);
try {
$output = renderPhpt(getParamsForParseError($test, $case), $expectedFailures);
$output = renderPhpt(getParamsForParseError($test, $case), $expectedFailures, $for64bitOnly);
} catch (Exception $e) {
printf("Error processing parseErrors[%d] in %s: %s\n", $i, $inputFile, $e->getMessage());
continue;
Expand Down Expand Up @@ -263,16 +268,19 @@ function getParamsForParseError(array $test, array $case)
];
}

function renderPhpt(array $params, array $expectedFailures)
function renderPhpt(array $params, array $expectedFailures, array $for64bitOnly)
{
$params['%XFAIL%'] = isset($expectedFailures[$params['%NAME%']])
? "--XFAIL--\n" . $expectedFailures[$params['%NAME%']] . "\n"
: '';
$params['%SKIPIF%'] = isset($for64bitOnly[$params['%NAME%']])
? "--SKIPIF--\n" . "<?php if (PHP_INT_SIZE !== 8) { die(\"skip {$for64bitOnly[$params['%NAME%']]}\"); } ?>" . "\n"
: '';

$template = <<< 'TEMPLATE'
--TEST--
%NAME%
%XFAIL%--DESCRIPTION--
%XFAIL%%SKIPIF%--DESCRIPTION--
Generated by scripts/convert-bson-corpus-tests.php

DO NOT EDIT THIS FILE
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB/Monitoring/CommandFailedEvent.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ static HashTable *php_phongo_commandfailedevent_get_debug_info(zval *object, int
array_init_size(&retval, 6);

ADD_ASSOC_STRING(&retval, "commandName", intern->command_name);
ADD_ASSOC_INT64(&retval, "durationMicros", intern->duration_micros);
ADD_ASSOC_INT64(&retval, "durationMicros", (int64_t) intern->duration_micros);

#if PHP_VERSION_ID >= 70000
ADD_ASSOC_ZVAL_EX(&retval, "error", &intern->z_error);
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB/Monitoring/CommandSucceededEvent.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ static HashTable *php_phongo_commandsucceededevent_get_debug_info(zval *object,
array_init_size(&retval, 6);

ADD_ASSOC_STRING(&retval, "commandName", intern->command_name);
ADD_ASSOC_INT64(&retval, "durationMicros", intern->duration_micros);
ADD_ASSOC_INT64(&retval, "durationMicros", (int64_t) intern->duration_micros);

sprintf(operation_id, "%" PHONGO_LONG_FORMAT, intern->operation_id);
ADD_ASSOC_STRING(&retval, "operationId", operation_id);
Expand Down
2 changes: 2 additions & 0 deletions tests/bson-corpus/int64-valid-001.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
--TEST--
Int64 type: MinValue
--SKIPIF--
<?php if (PHP_INT_SIZE !== 8) { die("skip Can't represent 64-bit ints on a 32-bit platform"); } ?>
--DESCRIPTION--
Generated by scripts/convert-bson-corpus-tests.php

Expand Down
2 changes: 2 additions & 0 deletions tests/bson-corpus/int64-valid-002.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
--TEST--
Int64 type: MaxValue
--SKIPIF--
<?php if (PHP_INT_SIZE !== 8) { die("skip Can't represent 64-bit ints on a 32-bit platform"); } ?>
--DESCRIPTION--
Generated by scripts/convert-bson-corpus-tests.php

Expand Down
7 changes: 5 additions & 2 deletions tests/bson/bson-timestamp_error-003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ echo throws(function() {
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";

echo throws(function() {
new MongoDB\BSON\Timestamp(-2147483648, 0);
/* I realise that "-2147483647 - 1" could be written as "-2147483648", *however*, PHP considers
* the latter a floating point number, as it parses "-" and "2147483648" separately, and
* "2147483648" doesn't fit in the 32-bit signed range. */
new MongoDB\BSON\Timestamp(-2147483647 - 1, 0);
Copy link
Member

@jmikola jmikola Jan 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still going to produce an exception with "Expected increment to be an unsigned 32-bit integer, -2147483648 given" on 32-bit systems?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That's exactly what this test tests :-)

}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";

echo throws(function() {
new MongoDB\BSON\Timestamp(0, -1);
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";

echo throws(function() {
new MongoDB\BSON\Timestamp(0, -2147483648);
new MongoDB\BSON\Timestamp(0, -2147483647 - 1);
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";

?>
Expand Down
3 changes: 1 addition & 2 deletions tests/causal-consistency/causal-consistency-011.phpt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
--TEST--
Causal consistency: $clusterTime is not sent in commands to unsupported deployments
--SKIPIF--
<?php if (getenv("TRAVIS")) exit("skip This currently fails on Travis because it doesn't run 3.6 yet"); ?>
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php NEEDS('STANDALONE'); ?>
<?php NEEDS('STANDALONE'); NEEDS_ATLEAST_MONGODB_VERSION(STANDALONE, "3.6"); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";
Expand Down
1 change: 1 addition & 0 deletions tests/cursor/cursor-destruct-001.phpt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
--TEST--
MongoDB\Driver\Cursor destruct should kill a live cursor
--SKIPIF--
<?php if (PHP_INT_SIZE !== 8) { die('skip Only for 64-bit platform'); } ?>
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php NEEDS('STANDALONE'); CLEANUP(STANDALONE); ?>
--FILE--
Expand Down
1 change: 0 additions & 1 deletion tests/manager/manager-executeBulkWrite-008.phpt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
--TEST--
MongoDB\Driver\Manager::executeBulkWrite() update multiple documents with no upsert
--SKIPIF--
<?php if (getenv("TRAVIS")) exit("skip This oddly enough fails on travis and I cannot figureout why") ?>
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php NEEDS('STANDALONE'); CLEANUP(STANDALONE); ?>
--FILE--
Expand Down
3 changes: 1 addition & 2 deletions tests/manager/manager-executeCommand-004.phpt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
--TEST--
MongoDB\Driver\Manager::executeCommand() options (MONGOC_CMD_RAW)
--SKIPIF--
<?php if (getenv("TRAVIS")) exit("skip This currently tails on Travis because it doesn't run 3.6 yet"); ?>
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php NEEDS('STANDALONE'); CLEANUP(STANDALONE); ?>
<?php NEEDS('STANDALONE'); NEEDS_ATLEAST_MONGODB_VERSION(STANDALONE, "3.6"); CLEANUP(STANDALONE); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";
Expand Down
3 changes: 1 addition & 2 deletions tests/manager/manager-executeReadCommand-001.phpt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
--TEST--
MongoDB\Driver\Manager::executeReadCommand()
--SKIPIF--
<?php if (getenv("TRAVIS")) exit("skip This currently tails on Travis because it doesn't run 3.6 yet"); ?>
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php NEEDS('STANDALONE'); CLEANUP(STANDALONE); ?>
<?php NEEDS('STANDALONE'); CLEANUP(STANDALONE); NEEDS_ATLEAST_MONGODB_VERSION(STANDALONE, "3.6"); NEEDS_STORAGE_ENGINE(STANDALONE, "wiredTiger"); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";
Expand Down
3 changes: 1 addition & 2 deletions tests/manager/manager-executeWriteCommand-001.phpt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
--TEST--
MongoDB\Driver\Manager::executeWriteCommand()
--SKIPIF--
<?php if (getenv("TRAVIS")) exit("skip This currently tails on Travis because it doesn't run 3.6 yet"); ?>
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php NEEDS('STANDALONE'); CLEANUP(STANDALONE); ?>
<?php NEEDS('STANDALONE'); CLEANUP(STANDALONE); NEEDS_ATLEAST_MONGODB_VERSION(STANDALONE, "3.6"); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";
Expand Down
3 changes: 1 addition & 2 deletions tests/server/server-executeCommand-006.phpt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
--TEST--
MongoDB\Driver\Server::executeCommand() options (MONGO_CMD_RAW)
--SKIPIF--
<?php if (getenv("TRAVIS")) exit("skip This currently tails on Travis because it doesn't run 3.6 yet"); ?>
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php NEEDS('STANDALONE'); CLEANUP(STANDALONE); ?>
<?php NEEDS('STANDALONE'); NEEDS_ATLEAST_MONGODB_VERSION(STANDALONE, "3.6"); CLEANUP(STANDALONE); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";
Expand Down
3 changes: 1 addition & 2 deletions tests/server/server-executeReadCommand-001.phpt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
--TEST--
MongoDB\Driver\Server::executeReadCommand()
--SKIPIF--
<?php if (getenv("TRAVIS")) exit("skip This currently tails on Travis because it doesn't run 3.6 yet"); ?>
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php NEEDS('STANDALONE'); CLEANUP(STANDALONE); ?>
<?php NEEDS('STANDALONE'); CLEANUP(STANDALONE); NEEDS_ATLEAST_MONGODB_VERSION(STANDALONE, "3.6"); NEEDS_STORAGE_ENGINE(STANDALONE, "wiredTiger"); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";
Expand Down
3 changes: 1 addition & 2 deletions tests/server/server-executeWriteCommand-001.phpt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
--TEST--
MongoDB\Driver\Server::executeWriteCommand()
--SKIPIF--
<?php if (getenv("TRAVIS")) exit("skip This currently tails on Travis because it doesn't run 3.6 yet"); ?>
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php NEEDS('STANDALONE'); CLEANUP(STANDALONE); ?>
<?php NEEDS('STANDALONE'); CLEANUP(STANDALONE); NEEDS_ATLEAST_MONGODB_VERSION(STANDALONE, "3.6"); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";
Expand Down
3 changes: 1 addition & 2 deletions tests/session/session-001.phpt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
--TEST--
MongoDB\Driver\Session spec test: Pool is LIFO
--SKIPIF--
<?php if (getenv("TRAVIS")) exit("skip This currently fails on Travis because it doesn't run 3.6 yet"); ?>
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php NEEDS('STANDALONE'); ?>
<?php NEEDS('STANDALONE'); NEEDS_ATLEAST_MONGODB_VERSION(STANDALONE, "3.6"); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";
Expand Down
3 changes: 1 addition & 2 deletions tests/session/session-003.phpt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
--TEST--
MongoDB\Driver\Session spec test: session cannot be used for different clients
--SKIPIF--
<?php if (getenv("TRAVIS")) exit("skip This currently fails on Travis because it doesn't run 3.6 yet"); ?>
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php NEEDS('STANDALONE'); CLEANUP(STANDALONE); ?>
<?php NEEDS('STANDALONE'); NEEDS_ATLEAST_MONGODB_VERSION(STANDALONE, "3.6"); CLEANUP(STANDALONE); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";
Expand Down
3 changes: 1 addition & 2 deletions tests/session/session-debug-001.phpt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
--TEST--
MongoDB\Driver\Session debug output (before an operation)
--SKIPIF--
<?php if (getenv("TRAVIS")) exit("skip This currently fails on Travis because it doesn't run 3.6 yet"); ?>
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php NEEDS('STANDALONE'); ?>
<?php NEEDS('STANDALONE'); NEEDS_ATLEAST_MONGODB_VERSION(STANDALONE, "3.6"); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";
Expand Down
3 changes: 1 addition & 2 deletions tests/session/session-debug-003.phpt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
--TEST--
MongoDB\Driver\Session debug output (causalConsistency=false)
--SKIPIF--
<?php if (getenv("TRAVIS")) exit("skip This currently fails on Travis because it doesn't run 3.6 yet"); ?>
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php NEEDS('STANDALONE'); ?>
<?php NEEDS('STANDALONE'); NEEDS_ATLEAST_MONGODB_VERSION(STANDALONE, "3.6"); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";
Expand Down
3 changes: 1 addition & 2 deletions tests/session/session-getLogicalSessionId-001.phpt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
--TEST--
MongoDB\Driver\Session::getLogicalSessionId()
--SKIPIF--
<?php if (getenv("TRAVIS")) exit("skip This currently fails on Travis because it doesn't run 3.6 yet"); ?>
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php NEEDS('STANDALONE'); ?>
<?php NEEDS('STANDALONE'); NEEDS_ATLEAST_MONGODB_VERSION(STANDALONE, "3.6"); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";
Expand Down
44 changes: 41 additions & 3 deletions tests/utils/tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ function TESTCOMMANDS($uri) {
}
}
}
function NEEDS($uri) {
if (!constant($uri)) {
exit("skip -- need '$uri' defined");
function NEEDS($configuration) {
if (!constant($configuration)) {
exit("skip -- need '$configuration' defined");
}
}
function PREDICTABLE() {
Expand Down Expand Up @@ -137,6 +137,44 @@ function LOAD($uri, $dbname = DATABASE_NAME, $collname = COLLECTION_NAME, $filen
}
}

function NEEDS_ATLEAST_MONGODB_VERSION($uri, $version) {
$manager = new MongoDB\Driver\Manager($uri);
$cmd = new MongoDB\Driver\Command(["buildInfo" => 1]);
$rp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);

try {
$cursor = $manager->executeCommand("admin", $cmd, $rp);
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
$document = current($cursor->toArray());

if (version_compare($document['version'], $version, '<')) {
echo "skip Needs version >= $version, but is {$document['version']}";
}
} catch(Exception $e) {
echo "skip (needs version); $uri ($version): " . $e->getCode(), ": ", $e->getMessage();
exit(1);
}
}

function NEEDS_STORAGE_ENGINE($uri, $engine) {
$manager = new MongoDB\Driver\Manager($uri);
$cmd = new MongoDB\Driver\Command(["serverStatus" => 1]);
$rp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);

try {
$cursor = $manager->executeCommand("admin", $cmd, $rp);
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
$document = current($cursor->toArray());

if ($document['storageEngine']['name'] != $engine) {
echo "skip Needs storage engine '$engine', but is '{$document['storageEngine']['name']}'";
}
} catch(Exception $e) {
echo "skip (needs version); $uri ($version): " . $e->getCode(), ": ", $e->getMessage();
exit(1);
}
}

function CLEANUP($uri, $dbname = DATABASE_NAME, $collname = COLLECTION_NAME) {
try {
$manager = new MongoDB\Driver\Manager($uri);
Expand Down