|
| 1 | +--TEST-- |
| 2 | +BSON\fromPHP(): Serializable returns document with null bytes in field name |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | + |
| 6 | +require_once __DIR__ . '/../utils/tools.php'; |
| 7 | + |
| 8 | +class MySerializable implements MongoDB\BSON\Serializable |
| 9 | +{ |
| 10 | + private $data; |
| 11 | + |
| 12 | + public function __construct($data) |
| 13 | + { |
| 14 | + $this->data = $data; |
| 15 | + } |
| 16 | + |
| 17 | + public function bsonSerialize() |
| 18 | + { |
| 19 | + return $this->data; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +echo "\nTesting array with one leading null byte in field name\n"; |
| 24 | +echo throws(function() { |
| 25 | + fromPHP(new MySerializable(["\0" => 1])); |
| 26 | +}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n"; |
| 27 | + |
| 28 | +echo "\nTesting array with one trailing null byte in field name\n"; |
| 29 | +echo throws(function() { |
| 30 | + fromPHP(new MySerializable(["a\0" => 1])); |
| 31 | +}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n"; |
| 32 | + |
| 33 | +echo "\nTesting array with multiple null bytes in field name\n"; |
| 34 | +echo throws(function() { |
| 35 | + fromPHP(new MySerializable(["\0\0\0" => 1])); |
| 36 | +}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n"; |
| 37 | + |
| 38 | +/* Per PHPC-884, field names with a leading null byte are ignored when encoding |
| 39 | + * a document from an object's property hash table, since PHP uses leading bytes |
| 40 | + * to denote protected and private properties. However, in this case the object |
| 41 | + * was returned from Serializable::bsonSerialize() and we skip the check for |
| 42 | + * protected and private properties. */ |
| 43 | +echo "\nTesting object with one leading null byte in field name\n"; |
| 44 | +echo throws(function() { |
| 45 | + fromPHP(new MySerializable((object) ["\0" => 1])); |
| 46 | +}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n"; |
| 47 | + |
| 48 | +echo "\nTesting object with one trailing null byte in field name\n"; |
| 49 | +echo throws(function() { |
| 50 | + fromPHP(new MySerializable((object) ["a\0" => 1])); |
| 51 | +}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n"; |
| 52 | + |
| 53 | +echo "\nTesting object with multiple null bytes in field name\n"; |
| 54 | +echo throws(function() { |
| 55 | + fromPHP(new MySerializable((object) ["\0\0\0" => 1])); |
| 56 | +}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n"; |
| 57 | + |
| 58 | +?> |
| 59 | +===DONE=== |
| 60 | +<?php exit(0); ?> |
| 61 | +--EXPECT-- |
| 62 | +Testing array with one leading null byte in field name |
| 63 | +OK: Got MongoDB\Driver\Exception\UnexpectedValueException |
| 64 | +BSON keys cannot contain null bytes. Unexpected null byte after "". |
| 65 | + |
| 66 | +Testing array with one trailing null byte in field name |
| 67 | +OK: Got MongoDB\Driver\Exception\UnexpectedValueException |
| 68 | +BSON keys cannot contain null bytes. Unexpected null byte after "a". |
| 69 | + |
| 70 | +Testing array with multiple null bytes in field name |
| 71 | +OK: Got MongoDB\Driver\Exception\UnexpectedValueException |
| 72 | +BSON keys cannot contain null bytes. Unexpected null byte after "". |
| 73 | + |
| 74 | +Testing object with one leading null byte in field name |
| 75 | +OK: Got MongoDB\Driver\Exception\UnexpectedValueException |
| 76 | +BSON keys cannot contain null bytes. Unexpected null byte after "". |
| 77 | + |
| 78 | +Testing object with one trailing null byte in field name |
| 79 | +OK: Got MongoDB\Driver\Exception\UnexpectedValueException |
| 80 | +BSON keys cannot contain null bytes. Unexpected null byte after "a". |
| 81 | + |
| 82 | +Testing object with multiple null bytes in field name |
| 83 | +OK: Got MongoDB\Driver\Exception\UnexpectedValueException |
| 84 | +BSON keys cannot contain null bytes. Unexpected null byte after "". |
| 85 | +===DONE=== |
0 commit comments