diff --git a/.gitignore b/.gitignore index c5b9a6b81..fbc591860 100644 --- a/.gitignore +++ b/.gitignore @@ -41,6 +41,7 @@ mongocryptd.pid *.out *.mem *.php +!scripts/*php !*.stub.php tests/*/*.sh tests/*/*/*/*.sh diff --git a/scripts/convert-bson-binary-vector-tests.php b/scripts/convert-bson-binary-vector-tests.php new file mode 100644 index 000000000..b4e93ec16 --- /dev/null +++ b/scripts/convert-bson-binary-vector-tests.php @@ -0,0 +1,225 @@ + 'Document memory leak (PHPC-2648)', + 'Tests of Binary subtype 9, Vectors, with dtype FLOAT32: Insufficient vector data with 3 bytes FLOAT32' => 'Document memory leak (PHPC-2648)', + 'Tests of Binary subtype 9, Vectors, with dtype FLOAT32: Insufficient vector data with 5 bytes FLOAT32' => 'Document memory leak (PHPC-2648)', + 'Tests of Binary subtype 9, Vectors, with dtype INT8: INT8 with padding' => 'Document memory leak (PHPC-2648)', + 'Tests of Binary subtype 9, Vectors, with dtype PACKED_BIT: Padding specified with no vector data PACKED_BIT' => 'Document memory leak (PHPC-2648)', + 'Tests of Binary subtype 9, Vectors, with dtype PACKED_BIT: Exceeding maximum padding PACKED_BIT' => 'Document memory leak (PHPC-2648)', +]; + +$outputPath = realpath(__DIR__ . '/../tests') . '/bson-binary-vector/'; + +if ( ! is_dir($outputPath) && ! mkdir($outputPath, 0755, true)) { + printf("Error creating output path: %s\n", $outputPath); +} + +foreach (array_slice($argv, 1) as $inputFile) { + if ( ! is_readable($inputFile) || ! is_file($inputFile)) { + printf("Error reading %s\n", $inputFile); + continue; + } + + try { + $test = Document::fromJSON(file_get_contents($inputFile))->toPHP(['root' => 'array', 'document' => 'array']); + } catch (UnexpectedValueException $e) { + printf("Error decoding %s: %s\n", $inputFile, $e->getMessage()); + continue; + } + + if ( ! isset($test['description'])) { + printf("Skipping test file without \"description\" field: %s\n", $inputFile); + continue; + } + + if ( ! isset($test['test_key'])) { + printf("Skipping test file without \"test_key\" field: %s\n", $inputFile); + continue; + } + + if ( ! empty($test['tests'])) { + foreach ($test['tests'] as $i => $case) { + $outputFile = sprintf('%s-%03d.phpt', pathinfo($inputFile, PATHINFO_FILENAME), $i + 1); + try { + $output = renderPhpt(getParamsForTestCase($test, $case), $expectedFailures); + } catch (Exception $e) { + printf("Error processing tests[%d] in %s: %s\n", $i, $inputFile, $e->getMessage()); + continue; + } + + if (false === file_put_contents($outputPath . '/' . $outputFile, $output)) { + printf("Error writing tests[%d] in %s\n", $i, $inputFile); + continue; + } + } + } +} + +function convertVector(array $input, VectorType $vectorType, int $padding): array +{ + if (!array_is_list($input)) { + throw new InvalidArgumentException('Vector is not a list'); + } + + if ($vectorType != VectorType::PackedBit) { + if ($padding !== 0) { + throw new InvalidArgumentException(sprintf('Vector type %s does not support padding: %d', $vectorType->name, $padding)); + } + + return $input; + } + + if ($padding < 0 || $padding > 7) { + throw new InvalidArgumentException(sprintf('Expected padding [0..7], %d given', $padding)); + } + + if (count($input) === 0 && $padding > 0) { + throw new InvalidArgumentException(sprintf('Non-zero padding (%d) given for empty vector', $padding)); + } + + $vector = []; + + foreach ($input as $i => $byte) { + if (!is_int($byte)) { + throw new InvalidArgumentException(sprintf('Expected vector[%d] to be an int, %s given', $i, get_debug_type($byte))); + } + + if ($byte < 0 || $byte > 255) { + throw new InvalidArgumentException(sprintf('Expected vector[%d] to be an unsigned int8, %d given', $i, $byte)); + } + + // decbin() may return fewer than 8 binary digits, so left-pad its output with zeroes + $digits = str_pad(decbin($byte), 8, '0', STR_PAD_LEFT); + + $bits = array_map(intval(...), str_split($digits)); + + array_push($vector, ...$bits); + } + + // Remove trailing zeroes from the final byte's digits according to padding + if ($padding > 0) { + $removed = array_splice($vector, -$padding); + + // Assert that only zeroes were removed + if ($removed !== array_fill(0, $padding, 0)) { + throw new InvalidArgumentException(sprintf('Application of padding %d would remove non-zero digits: %s', $padding, json_encode($removed))); + } + } + + return $vector; +} + +function getParamsForTestCase(array $test, array $case): array +{ + foreach (['description', 'valid', 'dtype_hex'] as $field) { + if (!isset($case[$field])) { + throw new InvalidArgumentException(sprintf('Missing "%s" field', $field)); + } + } + + $code = ''; + $expect = ''; + + $vectorType = match ($case['dtype_hex']) { + '0x27' => VectorType::Float32, + '0x03' => VectorType::Int8, + '0x10' => VectorType::PackedBit, + }; + + $padding = $case['padding'] ?? 0; + + if ($case['valid']) { + $code .= sprintf('// Vector %s with padding %d', json_encode($case['vector']), $padding) . "\n"; + + /* encode a document from the numeric values, dtype, and padding, along + * with the "test_key", and assert this matches the canonical_bson + * string. */ + $vector = convertVector($case['vector'], $vectorType, $padding); + $code .= sprintf('$vector = %s;', var_export($vector, true)) . "\n\n"; + $code .= sprintf('$binary = MongoDB\BSON\Binary::fromVector($vector, %s);', var_export($vectorType, true)) . "\n"; + $code .= sprintf('echo bin2hex((string) MongoDB\BSON\Document::fromPHP([%s => $binary])), "\n";', var_export($test['test_key'], true)) . "\n\n"; + $expect .= strtolower($case['canonical_bson']) . "\n"; + + /* decode the canonical_bson into its binary form, and then assert that + * the numeric values, dtype, and padding all match those provided in + * the JSON. */ + $code .= sprintf('$bson = MongoDB\BSON\Document::fromBSON(hex2bin(%s));', var_export($case['canonical_bson'], true)) . "\n"; + $code .= sprintf('var_dump($binary == $bson[%s]);', var_export($test['test_key'], true)) . "\n"; + $expect .= 'bool(true)' . "\n"; + } else /* not valid */ { + /* if the vector field is present, raise an exception when attempting + * to encode a document from the numeric values, dtype, and padding. */ + if (isset($case['vector'])) { + $code .= sprintf('// Vector %s with padding %d', json_encode($case['vector']), $padding) . "\n"; + + try { + $vector = convertVector($case['vector'], $vectorType, $padding); + + $code .= sprintf('$vector = %s;', var_export($vector, true)) . "\n\n"; + $code .= "throws(function() use (\$vector) {\n"; + $code .= sprintf(" var_dump(MongoDB\BSON\Binary::fromVector(\$vector, %s));\n", var_export($vectorType, true)); + $code .= "}, 'MongoDB\Driver\Exception\InvalidArgumentException');" . "\n\n"; + $expect .= 'OK: Got MongoDB\Driver\Exception\InvalidArgumentException' . "\n"; + } catch (InvalidArgumentException $e) { + $code .= sprintf('echo %s, "\n";', var_export($e->getMessage(), true)) . "\n\n"; + $expect .= $e->getMessage() . "\n"; + } + } + + /* if the canonical_bson field is present, raise an exception when + * attempting to deserialize it into the corresponding numeric values, + * as the field contains corrupted data. */ + if (isset($case['canonical_bson'])) { + $code .= "throws(function() {\n"; + $code .= sprintf(' var_dump(MongoDB\BSON\Document::fromBSON(hex2bin(%s)));', var_export($case['canonical_bson'], true)) . "\n"; + $code .= "}, 'MongoDB\Driver\Exception\InvalidArgumentException');"; + $expect .= 'OK: Got MongoDB\Driver\Exception\InvalidArgumentException' . "\n"; + } + } + + return [ + '%NAME%' => sprintf('%s: %s', trim($test['description']), trim($case['description'])), + '%CODE%' => trim($code), + '%EXPECT%' => trim($expect), + ]; +} + +function renderPhpt(array $params, array $expectedFailures): string +{ + $params['%XFAIL%'] = isset($expectedFailures[$params['%NAME%']]) + ? "--XFAIL--\n" . $expectedFailures[$params['%NAME%']] . "\n" + : ''; + + $params['%SKIPIF%'] = ''; + + $template = <<< 'TEMPLATE' +--TEST-- +%NAME% +%XFAIL%%SKIPIF%--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + +===DONE=== + +--EXPECT-- +%EXPECT% +===DONE=== +TEMPLATE; + + return str_replace(array_keys($params), array_values($params), $template); +} diff --git a/tests/bson-binary-vector/float32-001.phpt b/tests/bson-binary-vector/float32-001.phpt new file mode 100644 index 000000000..79642ef93 --- /dev/null +++ b/tests/bson-binary-vector/float32-001.phpt @@ -0,0 +1,30 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype FLOAT32: Simple Vector FLOAT32 +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + 127.0, + 1 => 7.0, +); + +$binary = MongoDB\BSON\Binary::fromVector($vector, \MongoDB\BSON\VectorType::Float32); +echo bin2hex((string) MongoDB\BSON\Document::fromPHP(['vector' => $binary])), "\n"; + +$bson = MongoDB\BSON\Document::fromBSON(hex2bin('1C00000005766563746F72000A0000000927000000FE420000E04000')); +var_dump($binary == $bson['vector']); + +?> +===DONE=== + +--EXPECT-- +1c00000005766563746f72000a0000000927000000fe420000e04000 +bool(true) +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/float32-002.phpt b/tests/bson-binary-vector/float32-002.phpt new file mode 100644 index 000000000..b0e07df36 --- /dev/null +++ b/tests/bson-binary-vector/float32-002.phpt @@ -0,0 +1,30 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype FLOAT32: Vector with decimals and negative value FLOAT32 +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + 127.7, + 1 => -7.7, +); + +$binary = MongoDB\BSON\Binary::fromVector($vector, \MongoDB\BSON\VectorType::Float32); +echo bin2hex((string) MongoDB\BSON\Document::fromPHP(['vector' => $binary])), "\n"; + +$bson = MongoDB\BSON\Document::fromBSON(hex2bin('1C00000005766563746F72000A0000000927006666FF426666F6C000')); +var_dump($binary == $bson['vector']); + +?> +===DONE=== + +--EXPECT-- +1c00000005766563746f72000a0000000927006666ff426666f6c000 +bool(true) +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/float32-003.phpt b/tests/bson-binary-vector/float32-003.phpt new file mode 100644 index 000000000..e200000fe --- /dev/null +++ b/tests/bson-binary-vector/float32-003.phpt @@ -0,0 +1,28 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype FLOAT32: Empty Vector FLOAT32 +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + $binary])), "\n"; + +$bson = MongoDB\BSON\Document::fromBSON(hex2bin('1400000005766563746F72000200000009270000')); +var_dump($binary == $bson['vector']); + +?> +===DONE=== + +--EXPECT-- +1400000005766563746f72000200000009270000 +bool(true) +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/float32-004.phpt b/tests/bson-binary-vector/float32-004.phpt new file mode 100644 index 000000000..e63785844 --- /dev/null +++ b/tests/bson-binary-vector/float32-004.phpt @@ -0,0 +1,31 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype FLOAT32: Infinity Vector FLOAT32 +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + -INF, + 1 => 0.0, + 2 => INF, +); + +$binary = MongoDB\BSON\Binary::fromVector($vector, \MongoDB\BSON\VectorType::Float32); +echo bin2hex((string) MongoDB\BSON\Document::fromPHP(['vector' => $binary])), "\n"; + +$bson = MongoDB\BSON\Document::fromBSON(hex2bin('2000000005766563746F72000E000000092700000080FF000000000000807F00')); +var_dump($binary == $bson['vector']); + +?> +===DONE=== + +--EXPECT-- +2000000005766563746f72000e000000092700000080ff000000000000807f00 +bool(true) +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/float32-005.phpt b/tests/bson-binary-vector/float32-005.phpt new file mode 100644 index 000000000..ab6ab2dee --- /dev/null +++ b/tests/bson-binary-vector/float32-005.phpt @@ -0,0 +1,27 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype FLOAT32: FLOAT32 with padding +--XFAIL-- +Document memory leak (PHPC-2648) +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + +===DONE=== + +--EXPECT-- +Vector type Float32 does not support padding: 3 +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/float32-006.phpt b/tests/bson-binary-vector/float32-006.phpt new file mode 100644 index 000000000..d2d62eb5e --- /dev/null +++ b/tests/bson-binary-vector/float32-006.phpt @@ -0,0 +1,23 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype FLOAT32: Insufficient vector data with 3 bytes FLOAT32 +--XFAIL-- +Document memory leak (PHPC-2648) +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + +===DONE=== + +--EXPECT-- +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/float32-007.phpt b/tests/bson-binary-vector/float32-007.phpt new file mode 100644 index 000000000..eeefaef77 --- /dev/null +++ b/tests/bson-binary-vector/float32-007.phpt @@ -0,0 +1,23 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype FLOAT32: Insufficient vector data with 5 bytes FLOAT32 +--XFAIL-- +Document memory leak (PHPC-2648) +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + +===DONE=== + +--EXPECT-- +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/int8-001.phpt b/tests/bson-binary-vector/int8-001.phpt new file mode 100644 index 000000000..2a642d993 --- /dev/null +++ b/tests/bson-binary-vector/int8-001.phpt @@ -0,0 +1,30 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype INT8: Simple Vector INT8 +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + 127, + 1 => 7, +); + +$binary = MongoDB\BSON\Binary::fromVector($vector, \MongoDB\BSON\VectorType::Int8); +echo bin2hex((string) MongoDB\BSON\Document::fromPHP(['vector' => $binary])), "\n"; + +$bson = MongoDB\BSON\Document::fromBSON(hex2bin('1600000005766563746F7200040000000903007F0700')); +var_dump($binary == $bson['vector']); + +?> +===DONE=== + +--EXPECT-- +1600000005766563746f7200040000000903007f0700 +bool(true) +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/int8-002.phpt b/tests/bson-binary-vector/int8-002.phpt new file mode 100644 index 000000000..93b4c6b1f --- /dev/null +++ b/tests/bson-binary-vector/int8-002.phpt @@ -0,0 +1,28 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype INT8: Empty Vector INT8 +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + $binary])), "\n"; + +$bson = MongoDB\BSON\Document::fromBSON(hex2bin('1400000005766563746F72000200000009030000')); +var_dump($binary == $bson['vector']); + +?> +===DONE=== + +--EXPECT-- +1400000005766563746f72000200000009030000 +bool(true) +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/int8-003.phpt b/tests/bson-binary-vector/int8-003.phpt new file mode 100644 index 000000000..6e8c2a848 --- /dev/null +++ b/tests/bson-binary-vector/int8-003.phpt @@ -0,0 +1,26 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype INT8: Overflow Vector INT8 +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + 128, +); + +throws(function() use ($vector) { + var_dump(MongoDB\BSON\Binary::fromVector($vector, \MongoDB\BSON\VectorType::Int8)); +}, 'MongoDB\Driver\Exception\InvalidArgumentException'); + +?> +===DONE=== + +--EXPECT-- +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/int8-004.phpt b/tests/bson-binary-vector/int8-004.phpt new file mode 100644 index 000000000..15f397e78 --- /dev/null +++ b/tests/bson-binary-vector/int8-004.phpt @@ -0,0 +1,26 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype INT8: Underflow Vector INT8 +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + -129, +); + +throws(function() use ($vector) { + var_dump(MongoDB\BSON\Binary::fromVector($vector, \MongoDB\BSON\VectorType::Int8)); +}, 'MongoDB\Driver\Exception\InvalidArgumentException'); + +?> +===DONE=== + +--EXPECT-- +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/int8-005.phpt b/tests/bson-binary-vector/int8-005.phpt new file mode 100644 index 000000000..b0064fd51 --- /dev/null +++ b/tests/bson-binary-vector/int8-005.phpt @@ -0,0 +1,27 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype INT8: INT8 with padding +--XFAIL-- +Document memory leak (PHPC-2648) +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + +===DONE=== + +--EXPECT-- +Vector type Int8 does not support padding: 3 +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/int8-006.phpt b/tests/bson-binary-vector/int8-006.phpt new file mode 100644 index 000000000..8b9f1f440 --- /dev/null +++ b/tests/bson-binary-vector/int8-006.phpt @@ -0,0 +1,27 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype INT8: INT8 with float inputs +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + 127.77, + 1 => 7.77, +); + +throws(function() use ($vector) { + var_dump(MongoDB\BSON\Binary::fromVector($vector, \MongoDB\BSON\VectorType::Int8)); +}, 'MongoDB\Driver\Exception\InvalidArgumentException'); + +?> +===DONE=== + +--EXPECT-- +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/packed_bit-001.phpt b/tests/bson-binary-vector/packed_bit-001.phpt new file mode 100644 index 000000000..678d8d859 --- /dev/null +++ b/tests/bson-binary-vector/packed_bit-001.phpt @@ -0,0 +1,27 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype PACKED_BIT: Padding specified with no vector data PACKED_BIT +--XFAIL-- +Document memory leak (PHPC-2648) +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + +===DONE=== + +--EXPECT-- +Non-zero padding (1) given for empty vector +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/packed_bit-002.phpt b/tests/bson-binary-vector/packed_bit-002.phpt new file mode 100644 index 000000000..588a943b4 --- /dev/null +++ b/tests/bson-binary-vector/packed_bit-002.phpt @@ -0,0 +1,44 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype PACKED_BIT: Simple Vector PACKED_BIT +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + 0, + 1 => 1, + 2 => 1, + 3 => 1, + 4 => 1, + 5 => 1, + 6 => 1, + 7 => 1, + 8 => 0, + 9 => 0, + 10 => 0, + 11 => 0, + 12 => 0, + 13 => 1, + 14 => 1, + 15 => 1, +); + +$binary = MongoDB\BSON\Binary::fromVector($vector, \MongoDB\BSON\VectorType::PackedBit); +echo bin2hex((string) MongoDB\BSON\Document::fromPHP(['vector' => $binary])), "\n"; + +$bson = MongoDB\BSON\Document::fromBSON(hex2bin('1600000005766563746F7200040000000910007F0700')); +var_dump($binary == $bson['vector']); + +?> +===DONE=== + +--EXPECT-- +1600000005766563746f7200040000000910007f0700 +bool(true) +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/packed_bit-003.phpt b/tests/bson-binary-vector/packed_bit-003.phpt new file mode 100644 index 000000000..b374312f3 --- /dev/null +++ b/tests/bson-binary-vector/packed_bit-003.phpt @@ -0,0 +1,41 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype PACKED_BIT: PACKED_BIT with padding +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + 0, + 1 => 1, + 2 => 1, + 3 => 1, + 4 => 1, + 5 => 1, + 6 => 1, + 7 => 1, + 8 => 0, + 9 => 0, + 10 => 0, + 11 => 0, + 12 => 1, +); + +$binary = MongoDB\BSON\Binary::fromVector($vector, \MongoDB\BSON\VectorType::PackedBit); +echo bin2hex((string) MongoDB\BSON\Document::fromPHP(['vector' => $binary])), "\n"; + +$bson = MongoDB\BSON\Document::fromBSON(hex2bin('1600000005766563746F7200040000000910037F0800')); +var_dump($binary == $bson['vector']); + +?> +===DONE=== + +--EXPECT-- +1600000005766563746f7200040000000910037f0800 +bool(true) +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/packed_bit-004.phpt b/tests/bson-binary-vector/packed_bit-004.phpt new file mode 100644 index 000000000..fe0399ffb --- /dev/null +++ b/tests/bson-binary-vector/packed_bit-004.phpt @@ -0,0 +1,28 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype PACKED_BIT: Empty Vector PACKED_BIT +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + $binary])), "\n"; + +$bson = MongoDB\BSON\Document::fromBSON(hex2bin('1400000005766563746F72000200000009100000')); +var_dump($binary == $bson['vector']); + +?> +===DONE=== + +--EXPECT-- +1400000005766563746f72000200000009100000 +bool(true) +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/packed_bit-005.phpt b/tests/bson-binary-vector/packed_bit-005.phpt new file mode 100644 index 000000000..34a59e8a8 --- /dev/null +++ b/tests/bson-binary-vector/packed_bit-005.phpt @@ -0,0 +1,20 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype PACKED_BIT: Overflow Vector PACKED_BIT +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + +===DONE=== + +--EXPECT-- +Expected vector[0] to be an unsigned int8, 256 given +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/packed_bit-006.phpt b/tests/bson-binary-vector/packed_bit-006.phpt new file mode 100644 index 000000000..71d5e4418 --- /dev/null +++ b/tests/bson-binary-vector/packed_bit-006.phpt @@ -0,0 +1,20 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype PACKED_BIT: Underflow Vector PACKED_BIT +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + +===DONE=== + +--EXPECT-- +Expected vector[0] to be an unsigned int8, -1 given +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/packed_bit-007.phpt b/tests/bson-binary-vector/packed_bit-007.phpt new file mode 100644 index 000000000..2e68b9273 --- /dev/null +++ b/tests/bson-binary-vector/packed_bit-007.phpt @@ -0,0 +1,20 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype PACKED_BIT: Vector with float values PACKED_BIT +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + +===DONE=== + +--EXPECT-- +Expected vector[0] to be an int, float given +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/packed_bit-008.phpt b/tests/bson-binary-vector/packed_bit-008.phpt new file mode 100644 index 000000000..585c898fe --- /dev/null +++ b/tests/bson-binary-vector/packed_bit-008.phpt @@ -0,0 +1,27 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype PACKED_BIT: Exceeding maximum padding PACKED_BIT +--XFAIL-- +Document memory leak (PHPC-2648) +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + +===DONE=== + +--EXPECT-- +Expected padding [0..7], 8 given +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/packed_bit-009.phpt b/tests/bson-binary-vector/packed_bit-009.phpt new file mode 100644 index 000000000..e81d6812c --- /dev/null +++ b/tests/bson-binary-vector/packed_bit-009.phpt @@ -0,0 +1,20 @@ +--TEST-- +Tests of Binary subtype 9, Vectors, with dtype PACKED_BIT: Negative padding PACKED_BIT +--DESCRIPTION-- +Generated by scripts/convert-bson-corpus-tests.php + +DO NOT EDIT THIS FILE +--FILE-- + +===DONE=== + +--EXPECT-- +Expected padding [0..7], -1 given +===DONE=== \ No newline at end of file diff --git a/tests/bson-binary-vector/prose_test-001.phpt b/tests/bson-binary-vector/prose_test-001.phpt new file mode 100644 index 000000000..eb06cb80a --- /dev/null +++ b/tests/bson-binary-vector/prose_test-001.phpt @@ -0,0 +1,27 @@ +--TEST-- +Binary vector: Encoding PackedBit vector with non-zero, padded bits fails +--DESCRIPTION-- +Binary vector prose test #1 +https://github.com/mongodb/specifications/blob/master/source/bson-binary-vector/tests/README.md#prose-tests +--FILE-- + +===DONE=== + +--EXPECT-- +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +Binary vector data is invalid +===DONE=== diff --git a/tests/bson-binary-vector/prose_test-003.phpt b/tests/bson-binary-vector/prose_test-003.phpt new file mode 100644 index 000000000..cb070ad56 --- /dev/null +++ b/tests/bson-binary-vector/prose_test-003.phpt @@ -0,0 +1,28 @@ +--TEST-- +Binary vector: PackedBit vector comparisons +--DESCRIPTION-- +Binary vector prose test #3 +https://github.com/mongodb/specifications/blob/master/source/bson-binary-vector/tests/README.md#prose-tests +--FILE-- +toArray() === $b2->toArray()); + +?> +===DONE=== + +--EXPECT-- +bool(true) +bool(true) +===DONE=== \ No newline at end of file