Skip to content

Commit aedff64

Browse files
authored
PHPC-2267: Support BSON Binary subtype 0x08 (sensitive) (#1488)
* PHPC-2267: Support BSON Binary subtype 0x08 (sensitive) * Update BSON corpus tests for "sensitive" binary subtype Synced with mongodb/specifications@db7eb39 * Test Binary constants
1 parent 28b5a41 commit aedff64

File tree

8 files changed

+126
-31
lines changed

8 files changed

+126
-31
lines changed

src/BSON/Binary.stub.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ final class Binary implements BinaryInterface, \JsonSerializable, Type, \Seriali
5757
*/
5858
public const TYPE_COLUMN = UNKNOWN;
5959

60+
/**
61+
* @var int
62+
* @cvalue BSON_SUBTYPE_SENSITIVE
63+
*/
64+
public const TYPE_SENSITIVE = UNKNOWN;
65+
6066
/**
6167
* @var int
6268
* @cvalue BSON_SUBTYPE_USER

src/BSON/Binary_arginfo.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 3ecb4933da21ef2b58ca95d74e2aac8d26ca6dc6 */
2+
* Stub hash: 5119314f018eb048882d59bd952648336a8aa5d2 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_MongoDB_BSON_Binary___construct, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0)
@@ -153,6 +153,12 @@ static zend_class_entry *register_class_MongoDB_BSON_Binary(zend_class_entry *cl
153153
zend_declare_class_constant_ex(class_entry, const_TYPE_COLUMN_name, &const_TYPE_COLUMN_value, ZEND_ACC_PUBLIC, NULL);
154154
zend_string_release(const_TYPE_COLUMN_name);
155155

156+
zval const_TYPE_SENSITIVE_value;
157+
ZVAL_LONG(&const_TYPE_SENSITIVE_value, BSON_SUBTYPE_SENSITIVE);
158+
zend_string *const_TYPE_SENSITIVE_name = zend_string_init_interned("TYPE_SENSITIVE", sizeof("TYPE_SENSITIVE") - 1, 1);
159+
zend_declare_class_constant_ex(class_entry, const_TYPE_SENSITIVE_name, &const_TYPE_SENSITIVE_value, ZEND_ACC_PUBLIC, NULL);
160+
zend_string_release(const_TYPE_SENSITIVE_name);
161+
156162
zval const_TYPE_USER_DEFINED_value;
157163
ZVAL_LONG(&const_TYPE_USER_DEFINED_value, BSON_SUBTYPE_USER);
158164
zend_string *const_TYPE_USER_DEFINED_name = zend_string_init_interned("TYPE_USER_DEFINED", sizeof("TYPE_USER_DEFINED") - 1, 1);

tests/bson-corpus/binary-valid-011.phpt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Binary type: subtype 0x80
2+
Binary type: subtype 0x08
33
--DESCRIPTION--
44
Generated by scripts/convert-bson-corpus-tests.php
55

@@ -9,8 +9,8 @@ DO NOT EDIT THIS FILE
99

1010
require_once __DIR__ . '/../utils/basic.inc';
1111

12-
$canonicalBson = hex2bin('0F0000000578000200000080FFFF00');
13-
$canonicalExtJson = '{"x" : { "$binary" : {"base64" : "//8=", "subType" : "80"}}}';
12+
$canonicalBson = hex2bin('1D000000057800100000000873FFD26444B34C6990E8E7D1DFC035D400');
13+
$canonicalExtJson = '{"x" : { "$binary" : {"base64" : "c//SZESzTGmQ6OfR38A11A==", "subType" : "08"}}}';
1414

1515
// Canonical BSON -> Native -> Canonical BSON
1616
echo bin2hex(fromPHP(toPHP($canonicalBson))), "\n";
@@ -34,10 +34,10 @@ echo bin2hex((string) MongoDB\BSON\Document::fromJSON($canonicalExtJson)), "\n";
3434
===DONE===
3535
<?php exit(0); ?>
3636
--EXPECT--
37-
0f0000000578000200000080ffff00
38-
0f0000000578000200000080ffff00
39-
{"x":{"$binary":{"base64":"\/\/8=","subType":"80"}}}
40-
{"x":{"$binary":{"base64":"\/\/8=","subType":"80"}}}
41-
0f0000000578000200000080ffff00
42-
0f0000000578000200000080ffff00
37+
1d000000057800100000000873ffd26444b34c6990e8e7d1dfc035d400
38+
1d000000057800100000000873ffd26444b34c6990e8e7d1dfc035d400
39+
{"x":{"$binary":{"base64":"c\/\/SZESzTGmQ6OfR38A11A==","subType":"08"}}}
40+
{"x":{"$binary":{"base64":"c\/\/SZESzTGmQ6OfR38A11A==","subType":"08"}}}
41+
1d000000057800100000000873ffd26444b34c6990e8e7d1dfc035d400
42+
1d000000057800100000000873ffd26444b34c6990e8e7d1dfc035d400
4343
===DONE===

tests/bson-corpus/binary-valid-012.phpt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Binary type: $type query operator (conflicts with legacy $binary form with $type field)
2+
Binary type: subtype 0x80
33
--DESCRIPTION--
44
Generated by scripts/convert-bson-corpus-tests.php
55

@@ -9,8 +9,8 @@ DO NOT EDIT THIS FILE
99

1010
require_once __DIR__ . '/../utils/basic.inc';
1111

12-
$canonicalBson = hex2bin('1F000000037800170000000224747970650007000000737472696E67000000');
13-
$canonicalExtJson = '{"x" : { "$type" : "string"}}';
12+
$canonicalBson = hex2bin('0F0000000578000200000080FFFF00');
13+
$canonicalExtJson = '{"x" : { "$binary" : {"base64" : "//8=", "subType" : "80"}}}';
1414

1515
// Canonical BSON -> Native -> Canonical BSON
1616
echo bin2hex(fromPHP(toPHP($canonicalBson))), "\n";
@@ -34,10 +34,10 @@ echo bin2hex((string) MongoDB\BSON\Document::fromJSON($canonicalExtJson)), "\n";
3434
===DONE===
3535
<?php exit(0); ?>
3636
--EXPECT--
37-
1f000000037800170000000224747970650007000000737472696e67000000
38-
1f000000037800170000000224747970650007000000737472696e67000000
39-
{"x":{"$type":"string"}}
40-
{"x":{"$type":"string"}}
41-
1f000000037800170000000224747970650007000000737472696e67000000
42-
1f000000037800170000000224747970650007000000737472696e67000000
37+
0f0000000578000200000080ffff00
38+
0f0000000578000200000080ffff00
39+
{"x":{"$binary":{"base64":"\/\/8=","subType":"80"}}}
40+
{"x":{"$binary":{"base64":"\/\/8=","subType":"80"}}}
41+
0f0000000578000200000080ffff00
42+
0f0000000578000200000080ffff00
4343
===DONE===

tests/bson-corpus/binary-valid-013.phpt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ DO NOT EDIT THIS FILE
99

1010
require_once __DIR__ . '/../utils/basic.inc';
1111

12-
$canonicalBson = hex2bin('180000000378001000000010247479706500020000000000');
13-
$canonicalExtJson = '{"x" : { "$type" : {"$numberInt": "2"}}}';
12+
$canonicalBson = hex2bin('1F000000037800170000000224747970650007000000737472696E67000000');
13+
$canonicalExtJson = '{"x" : { "$type" : "string"}}';
1414

1515
// Canonical BSON -> Native -> Canonical BSON
1616
echo bin2hex(fromPHP(toPHP($canonicalBson))), "\n";
@@ -34,10 +34,10 @@ echo bin2hex((string) MongoDB\BSON\Document::fromJSON($canonicalExtJson)), "\n";
3434
===DONE===
3535
<?php exit(0); ?>
3636
--EXPECT--
37-
180000000378001000000010247479706500020000000000
38-
180000000378001000000010247479706500020000000000
39-
{"x":{"$type":{"$numberInt":"2"}}}
40-
{"x":{"$type":{"$numberInt":"2"}}}
41-
180000000378001000000010247479706500020000000000
42-
180000000378001000000010247479706500020000000000
37+
1f000000037800170000000224747970650007000000737472696e67000000
38+
1f000000037800170000000224747970650007000000737472696e67000000
39+
{"x":{"$type":"string"}}
40+
{"x":{"$type":"string"}}
41+
1f000000037800170000000224747970650007000000737472696e67000000
42+
1f000000037800170000000224747970650007000000737472696e67000000
4343
===DONE===
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
Binary type: $type query operator (conflicts with legacy $binary form with $type field)
3+
--DESCRIPTION--
4+
Generated by scripts/convert-bson-corpus-tests.php
5+
6+
DO NOT EDIT THIS FILE
7+
--FILE--
8+
<?php
9+
10+
require_once __DIR__ . '/../utils/basic.inc';
11+
12+
$canonicalBson = hex2bin('180000000378001000000010247479706500020000000000');
13+
$canonicalExtJson = '{"x" : { "$type" : {"$numberInt": "2"}}}';
14+
15+
// Canonical BSON -> Native -> Canonical BSON
16+
echo bin2hex(fromPHP(toPHP($canonicalBson))), "\n";
17+
18+
// Canonical BSON -> BSON object -> Canonical BSON
19+
echo bin2hex((string) MongoDB\BSON\Document::fromBSON($canonicalBson)), "\n";
20+
21+
// Canonical BSON -> Canonical extJSON
22+
echo json_canonicalize(toCanonicalExtendedJSON($canonicalBson)), "\n";
23+
24+
// Canonical BSON -> BSON object -> Canonical extJSON
25+
echo json_canonicalize(MongoDB\BSON\Document::fromBSON($canonicalBson)->toCanonicalExtendedJSON()), "\n";
26+
27+
// Canonical extJSON -> Canonical BSON
28+
echo bin2hex(fromJSON($canonicalExtJson)), "\n";
29+
30+
// Canonical extJSON -> BSON object -> Canonical BSON
31+
echo bin2hex((string) MongoDB\BSON\Document::fromJSON($canonicalExtJson)), "\n";
32+
33+
?>
34+
===DONE===
35+
<?php exit(0); ?>
36+
--EXPECT--
37+
180000000378001000000010247479706500020000000000
38+
180000000378001000000010247479706500020000000000
39+
{"x":{"$type":{"$numberInt":"2"}}}
40+
{"x":{"$type":{"$numberInt":"2"}}}
41+
180000000378001000000010247479706500020000000000
42+
180000000378001000000010247479706500020000000000
43+
===DONE===

tests/bson/bson-binary-001.phpt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
MongoDB\BSON\Binary #001
2+
MongoDB\BSON\Binary construction from various types
33
--FILE--
44
<?php
55

@@ -14,6 +14,7 @@ $types = array(
1414
MongoDB\BSON\Binary::TYPE_MD5,
1515
MongoDB\BSON\Binary::TYPE_ENCRYPTED,
1616
MongoDB\BSON\Binary::TYPE_COLUMN,
17+
MongoDB\BSON\Binary::TYPE_SENSITIVE,
1718
MongoDB\BSON\Binary::TYPE_USER_DEFINED,
1819
MongoDB\BSON\Binary::TYPE_USER_DEFINED+5,
1920
);
@@ -22,7 +23,7 @@ foreach($types as $type) {
2223
// Use 16-byte data to satisfy UUID requirements
2324
$binary = new MongoDB\BSON\Binary('randomBinaryData', $type);
2425
var_dump($binary->getData() === 'randomBinaryData');
25-
var_dump($binary->getType() == $type);
26+
var_dump($binary->getType() === $type);
2627
$tests[] = array("binary" => $binary);
2728
}
2829

@@ -58,6 +59,8 @@ bool(true)
5859
bool(true)
5960
bool(true)
6061
bool(true)
62+
bool(true)
63+
bool(true)
6164
Test#0 { "binary" : { "$binary" : "cmFuZG9tQmluYXJ5RGF0YQ==", "$type" : "00" } }
6265
string(73) "{ "binary" : { "$binary" : "cmFuZG9tQmluYXJ5RGF0YQ==", "$type" : "00" } }"
6366
string(73) "{ "binary" : { "$binary" : "cmFuZG9tQmluYXJ5RGF0YQ==", "$type" : "00" } }"
@@ -90,11 +93,15 @@ Test#7 { "binary" : { "$binary" : "cmFuZG9tQmluYXJ5RGF0YQ==", "$type" : "07" } }
9093
string(73) "{ "binary" : { "$binary" : "cmFuZG9tQmluYXJ5RGF0YQ==", "$type" : "07" } }"
9194
string(73) "{ "binary" : { "$binary" : "cmFuZG9tQmluYXJ5RGF0YQ==", "$type" : "07" } }"
9295
bool(true)
93-
Test#8 { "binary" : { "$binary" : "cmFuZG9tQmluYXJ5RGF0YQ==", "$type" : "80" } }
96+
Test#8 { "binary" : { "$binary" : "cmFuZG9tQmluYXJ5RGF0YQ==", "$type" : "08" } }
97+
string(73) "{ "binary" : { "$binary" : "cmFuZG9tQmluYXJ5RGF0YQ==", "$type" : "08" } }"
98+
string(73) "{ "binary" : { "$binary" : "cmFuZG9tQmluYXJ5RGF0YQ==", "$type" : "08" } }"
99+
bool(true)
100+
Test#9 { "binary" : { "$binary" : "cmFuZG9tQmluYXJ5RGF0YQ==", "$type" : "80" } }
94101
string(73) "{ "binary" : { "$binary" : "cmFuZG9tQmluYXJ5RGF0YQ==", "$type" : "80" } }"
95102
string(73) "{ "binary" : { "$binary" : "cmFuZG9tQmluYXJ5RGF0YQ==", "$type" : "80" } }"
96103
bool(true)
97-
Test#9 { "binary" : { "$binary" : "cmFuZG9tQmluYXJ5RGF0YQ==", "$type" : "85" } }
104+
Test#10 { "binary" : { "$binary" : "cmFuZG9tQmluYXJ5RGF0YQ==", "$type" : "85" } }
98105
string(73) "{ "binary" : { "$binary" : "cmFuZG9tQmluYXJ5RGF0YQ==", "$type" : "85" } }"
99106
string(73) "{ "binary" : { "$binary" : "cmFuZG9tQmluYXJ5RGF0YQ==", "$type" : "85" } }"
100107
bool(true)

tests/bson/bson-binary-constants.phpt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
MongoDB\BSON\Binary constants
3+
--FILE--
4+
<?php
5+
6+
use MongoDB\BSON\Binary;
7+
8+
var_dump(Binary::TYPE_GENERIC);
9+
var_dump(Binary::TYPE_FUNCTION);
10+
var_dump(Binary::TYPE_OLD_BINARY);
11+
var_dump(Binary::TYPE_OLD_UUID);
12+
var_dump(Binary::TYPE_UUID);
13+
var_dump(Binary::TYPE_MD5);
14+
var_dump(Binary::TYPE_ENCRYPTED);
15+
var_dump(Binary::TYPE_COLUMN);
16+
var_dump(Binary::TYPE_SENSITIVE);
17+
var_dump(Binary::TYPE_USER_DEFINED);
18+
19+
?>
20+
===DONE===
21+
<?php exit(0); ?>
22+
--EXPECT--
23+
int(0)
24+
int(1)
25+
int(2)
26+
int(3)
27+
int(4)
28+
int(5)
29+
int(6)
30+
int(7)
31+
int(8)
32+
int(128)
33+
===DONE===

0 commit comments

Comments
 (0)