diff --git a/src/bson.c b/src/bson.c index 3aac72ca7..7e979c820 100644 --- a/src/bson.c +++ b/src/bson.c @@ -1096,6 +1096,7 @@ PHP_FUNCTION(toJSON) char *data; int data_len; const bson_t *b; + bool eof = false; bson_reader_t *reader; (void)return_value_ptr; (void)this_ptr; (void)return_value_used; /* We don't use these */ @@ -1114,8 +1115,13 @@ PHP_FUNCTION(toJSON) RETVAL_STRINGL(str, str_len, 1); bson_free(str); } else { - RETURN_NULL(); + phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Could not read document from BSON reader"); } + + if (bson_reader_read(reader, &eof) || !eof) { + phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Reading document did not exhaust input buffer"); + } + bson_reader_destroy(reader); } /* }}} */ @@ -1139,7 +1145,7 @@ PHP_FUNCTION(fromJSON) RETVAL_STRINGL((const char *) bson_get_data(&b), b.len, 1); bson_destroy(&b); } else { - RETURN_NULL(); + phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "%s", error.message ? error.message : "Error parsing JSON"); } } /* }}} */ diff --git a/tests/bson/bson-fromJSON-001.phpt b/tests/bson/bson-fromJSON-001.phpt new file mode 100644 index 000000000..e1694daf0 --- /dev/null +++ b/tests/bson/bson-fromJSON-001.phpt @@ -0,0 +1,39 @@ +--TEST-- +BSON\fromJSON(): Decoding JSON +--SKIPIF-- + +--FILE-- + +===DONE=== + +--EXPECT-- +Test {} + 0 : 05 00 00 00 00 [.....] +Test { "foo": "bar" } + 0 : 12 00 00 00 02 66 6f 6f 00 04 00 00 00 62 61 72 [.....foo.....bar] + 10 : 00 00 [..] +Test { "foo": [ 1, 2, 3 ]} + 0 : 24 00 00 00 04 66 6f 6f 00 1a 00 00 00 10 30 00 [$....foo......0.] + 10 : 01 00 00 00 10 31 00 02 00 00 00 10 32 00 03 00 [.....1......2...] + 20 : 00 00 00 00 [....] +Test { "foo": { "bar": 1 }} + 0 : 18 00 00 00 03 66 6f 6f 00 0e 00 00 00 10 62 61 [.....foo......ba] + 10 : 72 00 01 00 00 00 00 00 [r.......] +===DONE=== diff --git a/tests/bson/bson-fromJSON-002.phpt b/tests/bson/bson-fromJSON-002.phpt new file mode 100644 index 000000000..2892c3b63 --- /dev/null +++ b/tests/bson/bson-fromJSON-002.phpt @@ -0,0 +1,56 @@ +--TEST-- +BSON\fromJSON(): Decoding extended JSON types +--SKIPIF-- + +--FILE-- + +===DONE=== + +--EXPECT-- +Test { "_id": { "$oid": "56315a7c6118fd1b920270b1" }} + 0 : 16 00 00 00 07 5f 69 64 00 56 31 5a 7c 61 18 fd [....._id.V1Z|a..] + 10 : 1b 92 02 70 b1 00 [...p..] +Test { "binary": { "$binary": "Zm9v", "$type": "00" }} + 0 : 15 00 00 00 05 62 69 6e 61 72 79 00 03 00 00 00 [.....binary.....] + 10 : 00 66 6f 6f 00 [.foo.] +Test { "date": { "$date": "2015-10-28T00:00:00Z" }} + 0 : 13 00 00 00 09 64 61 74 65 00 00 80 be ab 50 01 [.....date.....P.] + 10 : 00 00 00 [...] +Test { "timestamp": { "$timestamp": { "t": 1446084619, "i": 0 }}} + 0 : 18 00 00 00 11 74 69 6d 65 73 74 61 6d 70 00 00 [.....timestamp..] + 10 : 00 00 00 0b 80 31 56 00 [.....1V.] +Test { "regex": { "$regex": "pattern", "$options": "i" }} + 0 : 16 00 00 00 0b 72 65 67 65 78 00 70 61 74 74 65 [.....regex.patte] + 10 : 72 6e 00 69 00 00 [rn.i..] +Test { "undef": { "$undefined": true }} + 0 : 0c 00 00 00 06 75 6e 64 65 66 00 00 [.....undef..] +Test { "minkey": { "$minKey": 1 }} + 0 : 0d 00 00 00 ff 6d 69 6e 6b 65 79 00 00 [.....minkey..] +Test { "maxkey": { "$maxKey": 1 }} + 0 : 0d 00 00 00 7f 6d 61 78 6b 65 79 00 00 [.....maxkey..] +Test { "long": { "$numberLong": "1234" }} + 0 : 13 00 00 00 12 6c 6f 6e 67 00 d2 04 00 00 00 00 [.....long.......] + 10 : 00 00 00 [...] +===DONE=== diff --git a/tests/bson/bson-fromJSON_error-001.phpt b/tests/bson/bson-fromJSON_error-001.phpt new file mode 100644 index 000000000..38010d3a8 --- /dev/null +++ b/tests/bson/bson-fromJSON_error-001.phpt @@ -0,0 +1,23 @@ +--TEST-- +BSON\fromJSON(): invalid JSON +--SKIPIF-- + +--FILE-- + +===DONE=== + +--EXPECT-- +OK: Got MongoDB\Driver\Exception\UnexpectedValueException +lexical error: invalid string in json text. + foo + (right here) ------^ + +===DONE=== diff --git a/tests/bson/bson-toJSON-001.phpt b/tests/bson/bson-toJSON-001.phpt new file mode 100644 index 000000000..48d881a25 --- /dev/null +++ b/tests/bson/bson-toJSON-001.phpt @@ -0,0 +1,30 @@ +--TEST-- +BSON\toJSON(): Encoding JSON +--SKIPIF-- + +--FILE-- + 'bar' ], + [ 'foo' => [ 1, 2, 3 ]], + [ 'foo' => [ 'bar' => 1 ]], +]; + +foreach ($tests as $value) { + $bson = fromPHP($value); + echo toJSON($bson), "\n"; +} + +?> +===DONE=== + +--EXPECT-- +{ } +{ "foo" : "bar" } +{ "foo" : [ 1, 2, 3 ] } +{ "foo" : { "bar" : 1 } } +===DONE=== diff --git a/tests/bson/bson-toJSON-002.phpt b/tests/bson/bson-toJSON-002.phpt new file mode 100644 index 000000000..a4fb61fb0 --- /dev/null +++ b/tests/bson/bson-toJSON-002.phpt @@ -0,0 +1,39 @@ +--TEST-- +BSON\toJSON(): Encoding extended JSON types +--SKIPIF-- + +--FILE-- + new BSON\ObjectId('56315a7c6118fd1b920270b1')], + ['binary' => new BSON\Binary('foo', BSON\Binary::TYPE_GENERIC)], + ['date' => new BSON\UTCDateTime(1445990400000)], + ['timestamp' => new BSON\Timestamp(BSON\Binary::TYPE_GENERIC, 1446084619)], + ['regex' => new BSON\Regex('pattern', 'i')], + ['minkey' => new BSON\MinKey], + ['maxkey' => new BSON\MaxKey], + ['long' => 1234], +]; + +foreach ($tests as $value) { + $bson = fromPHP($value); + echo toJSON($bson), "\n"; +} + +?> +===DONE=== + +--EXPECT-- +{ "_id" : { "$oid" : "56315a7c6118fd1b920270b1" } } +{ "binary" : { "$type" : "00", "$binary" : "Zm9v" } } +{ "date" : { "$date" : 1445990400000 } } +{ "timestamp" : { "$timestamp" : { "t" : 1446084619, "i" : 0 } } } +{ "regex" : { "$regex" : "pattern", "$options" : "i" } } +{ "minkey" : { "$minKey" : 1 } } +{ "maxkey" : { "$maxKey" : 1 } } +{ "long" : 1234 } +===DONE=== diff --git a/tests/bson/bson-toJSON_error-001.phpt b/tests/bson/bson-toJSON_error-001.phpt new file mode 100644 index 000000000..6cf4d347a --- /dev/null +++ b/tests/bson/bson-toJSON_error-001.phpt @@ -0,0 +1,31 @@ +--TEST-- +BSON\toJSON(): BSON decoding exceptions +--SKIPIF-- + +--FILE-- + +===DONE=== + +--EXPECTF-- +OK: Got MongoDB\Driver\Exception\UnexpectedValueException +Could not read document from BSON reader +OK: Got MongoDB\Driver\Exception\UnexpectedValueException +Reading document did not exhaust input buffer +===DONE===