Skip to content

Commit a89fac3

Browse files
committed
Merge pull request #130
2 parents ee8d224 + 920e836 commit a89fac3

7 files changed

+226
-2
lines changed

src/bson.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,7 @@ PHP_FUNCTION(toJSON)
10961096
char *data;
10971097
int data_len;
10981098
const bson_t *b;
1099+
bool eof = false;
10991100
bson_reader_t *reader;
11001101

11011102
(void)return_value_ptr; (void)this_ptr; (void)return_value_used; /* We don't use these */
@@ -1114,8 +1115,13 @@ PHP_FUNCTION(toJSON)
11141115
RETVAL_STRINGL(str, str_len, 1);
11151116
bson_free(str);
11161117
} else {
1117-
RETURN_NULL();
1118+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Could not read document from BSON reader");
11181119
}
1120+
1121+
if (bson_reader_read(reader, &eof) || !eof) {
1122+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Reading document did not exhaust input buffer");
1123+
}
1124+
11191125
bson_reader_destroy(reader);
11201126
}
11211127
/* }}} */
@@ -1139,7 +1145,7 @@ PHP_FUNCTION(fromJSON)
11391145
RETVAL_STRINGL((const char *) bson_get_data(&b), b.len, 1);
11401146
bson_destroy(&b);
11411147
} else {
1142-
RETURN_NULL();
1148+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "%s", error.message ? error.message : "Error parsing JSON");
11431149
}
11441150
}
11451151
/* }}} */

tests/bson/bson-fromJSON-001.phpt

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
BSON\fromJSON(): Decoding JSON
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
8+
require_once __DIR__ . "/../utils/basic.inc";
9+
10+
$tests = [
11+
'{}',
12+
'{ "foo": "bar" }',
13+
'{ "foo": [ 1, 2, 3 ]}',
14+
'{ "foo": { "bar": 1 }}',
15+
];
16+
17+
foreach ($tests as $json) {
18+
printf("Test %s\n", $json);
19+
$bson = fromJSON($json);
20+
hex_dump($bson);
21+
}
22+
23+
?>
24+
===DONE===
25+
<?php exit(0); ?>
26+
--EXPECT--
27+
Test {}
28+
0 : 05 00 00 00 00 [.....]
29+
Test { "foo": "bar" }
30+
0 : 12 00 00 00 02 66 6f 6f 00 04 00 00 00 62 61 72 [.....foo.....bar]
31+
10 : 00 00 [..]
32+
Test { "foo": [ 1, 2, 3 ]}
33+
0 : 24 00 00 00 04 66 6f 6f 00 1a 00 00 00 10 30 00 [$....foo......0.]
34+
10 : 01 00 00 00 10 31 00 02 00 00 00 10 32 00 03 00 [.....1......2...]
35+
20 : 00 00 00 00 [....]
36+
Test { "foo": { "bar": 1 }}
37+
0 : 18 00 00 00 03 66 6f 6f 00 0e 00 00 00 10 62 61 [.....foo......ba]
38+
10 : 72 00 01 00 00 00 00 00 [r.......]
39+
===DONE===

tests/bson/bson-fromJSON-002.phpt

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
--TEST--
2+
BSON\fromJSON(): Decoding extended JSON types
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
8+
require_once __DIR__ . "/../utils/basic.inc";
9+
10+
$tests = [
11+
'{ "_id": { "$oid": "56315a7c6118fd1b920270b1" }}',
12+
'{ "binary": { "$binary": "Zm9v", "$type": "00" }}',
13+
'{ "date": { "$date": "2015-10-28T00:00:00Z" }}',
14+
'{ "timestamp": { "$timestamp": { "t": 1446084619, "i": 0 }}}',
15+
'{ "regex": { "$regex": "pattern", "$options": "i" }}',
16+
'{ "undef": { "$undefined": true }}',
17+
'{ "minkey": { "$minKey": 1 }}',
18+
'{ "maxkey": { "$maxKey": 1 }}',
19+
'{ "long": { "$numberLong": "1234" }}',
20+
];
21+
22+
foreach ($tests as $json) {
23+
printf("Test %s\n", $json);
24+
$bson = fromJSON($json);
25+
hex_dump($bson);
26+
}
27+
28+
?>
29+
===DONE===
30+
<?php exit(0); ?>
31+
--EXPECT--
32+
Test { "_id": { "$oid": "56315a7c6118fd1b920270b1" }}
33+
0 : 16 00 00 00 07 5f 69 64 00 56 31 5a 7c 61 18 fd [....._id.V1Z|a..]
34+
10 : 1b 92 02 70 b1 00 [...p..]
35+
Test { "binary": { "$binary": "Zm9v", "$type": "00" }}
36+
0 : 15 00 00 00 05 62 69 6e 61 72 79 00 03 00 00 00 [.....binary.....]
37+
10 : 00 66 6f 6f 00 [.foo.]
38+
Test { "date": { "$date": "2015-10-28T00:00:00Z" }}
39+
0 : 13 00 00 00 09 64 61 74 65 00 00 80 be ab 50 01 [.....date.....P.]
40+
10 : 00 00 00 [...]
41+
Test { "timestamp": { "$timestamp": { "t": 1446084619, "i": 0 }}}
42+
0 : 18 00 00 00 11 74 69 6d 65 73 74 61 6d 70 00 00 [.....timestamp..]
43+
10 : 00 00 00 0b 80 31 56 00 [.....1V.]
44+
Test { "regex": { "$regex": "pattern", "$options": "i" }}
45+
0 : 16 00 00 00 0b 72 65 67 65 78 00 70 61 74 74 65 [.....regex.patte]
46+
10 : 72 6e 00 69 00 00 [rn.i..]
47+
Test { "undef": { "$undefined": true }}
48+
0 : 0c 00 00 00 06 75 6e 64 65 66 00 00 [.....undef..]
49+
Test { "minkey": { "$minKey": 1 }}
50+
0 : 0d 00 00 00 ff 6d 69 6e 6b 65 79 00 00 [.....minkey..]
51+
Test { "maxkey": { "$maxKey": 1 }}
52+
0 : 0d 00 00 00 7f 6d 61 78 6b 65 79 00 00 [.....maxkey..]
53+
Test { "long": { "$numberLong": "1234" }}
54+
0 : 13 00 00 00 12 6c 6f 6e 67 00 d2 04 00 00 00 00 [.....long.......]
55+
10 : 00 00 00 [...]
56+
===DONE===
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
BSON\fromJSON(): invalid JSON
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
8+
require_once __DIR__ . "/../utils/basic.inc";
9+
10+
echo throws(function() {
11+
fromJSON('foo');
12+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
13+
14+
?>
15+
===DONE===
16+
<?php exit(0); ?>
17+
--EXPECT--
18+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
19+
lexical error: invalid string in json text.
20+
foo
21+
(right here) ------^
22+
23+
===DONE===

tests/bson/bson-toJSON-001.phpt

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
BSON\toJSON(): Encoding JSON
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
8+
require_once __DIR__ . "/../utils/basic.inc";
9+
10+
$tests = [
11+
[],
12+
[ 'foo' => 'bar' ],
13+
[ 'foo' => [ 1, 2, 3 ]],
14+
[ 'foo' => [ 'bar' => 1 ]],
15+
];
16+
17+
foreach ($tests as $value) {
18+
$bson = fromPHP($value);
19+
echo toJSON($bson), "\n";
20+
}
21+
22+
?>
23+
===DONE===
24+
<?php exit(0); ?>
25+
--EXPECT--
26+
{ }
27+
{ "foo" : "bar" }
28+
{ "foo" : [ 1, 2, 3 ] }
29+
{ "foo" : { "bar" : 1 } }
30+
===DONE===

tests/bson/bson-toJSON-002.phpt

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
BSON\toJSON(): Encoding extended JSON types
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
use MongoDB\BSON as BSON;
8+
9+
require_once __DIR__ . "/../utils/basic.inc";
10+
11+
$tests = [
12+
['_id' => new BSON\ObjectId('56315a7c6118fd1b920270b1')],
13+
['binary' => new BSON\Binary('foo', BSON\Binary::TYPE_GENERIC)],
14+
['date' => new BSON\UTCDateTime(1445990400000)],
15+
['timestamp' => new BSON\Timestamp(BSON\Binary::TYPE_GENERIC, 1446084619)],
16+
['regex' => new BSON\Regex('pattern', 'i')],
17+
['minkey' => new BSON\MinKey],
18+
['maxkey' => new BSON\MaxKey],
19+
['long' => 1234],
20+
];
21+
22+
foreach ($tests as $value) {
23+
$bson = fromPHP($value);
24+
echo toJSON($bson), "\n";
25+
}
26+
27+
?>
28+
===DONE===
29+
<?php exit(0); ?>
30+
--EXPECT--
31+
{ "_id" : { "$oid" : "56315a7c6118fd1b920270b1" } }
32+
{ "binary" : { "$type" : "00", "$binary" : "Zm9v" } }
33+
{ "date" : { "$date" : 1445990400000 } }
34+
{ "timestamp" : { "$timestamp" : { "t" : 1446084619, "i" : 0 } } }
35+
{ "regex" : { "$regex" : "pattern", "$options" : "i" } }
36+
{ "minkey" : { "$minKey" : 1 } }
37+
{ "maxkey" : { "$maxKey" : 1 } }
38+
{ "long" : 1234 }
39+
===DONE===

tests/bson/bson-toJSON_error-001.phpt

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
BSON\toJSON(): BSON decoding exceptions
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
/* We can't really test for bson_iter_init() failure, since bson_reader_read()
10+
* already checks that the buffer is at least 5 bytes.
11+
*/
12+
$invalidBson = array(
13+
'',
14+
str_repeat(fromJSON('{"x": "y"}'), 2),
15+
);
16+
17+
foreach ($invalidBson as $bson) {
18+
echo throws(function() use ($bson) {
19+
toJSON($bson);
20+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
21+
}
22+
23+
?>
24+
===DONE===
25+
<?php exit(0); ?>
26+
--EXPECTF--
27+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
28+
Could not read document from BSON reader
29+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
30+
Reading document did not exhaust input buffer
31+
===DONE===

0 commit comments

Comments
 (0)