Skip to content

Commit

Permalink
minor: add bson corpus tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saghm committed May 13, 2020
1 parent cf31511 commit a676305
Show file tree
Hide file tree
Showing 33 changed files with 5,599 additions and 0 deletions.
24 changes: 24 additions & 0 deletions etc/update-spec-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

# This script is used to fetch the latest JSON tests for the CRUD spec. It puts the tests in the
# direcory $reporoot/data/crud. It should be run from the root of the repository.

set -o errexit
set -o nounset

if [ ! -d ".git" ]; then
echo "$0: This script must be run from the root of the repository" >&2
exit 1
fi

if [ $# -ne 1 ]; then
echo "$0: This script must be passed exactly one argument for which tests to sync" >&2
exit 1
fi

tmpdir=`perl -MFile::Temp=tempdir -wle 'print tempdir(TMPDIR => 1, CLEANUP => 0)'`
curl -sL https://github.com/mongodb/specifications/archive/master.zip -o "$tmpdir/specs.zip"
unzip -d "$tmpdir" "$tmpdir/specs.zip" > /dev/null
mkdir -p "tests/spec/json/$1"
rsync -ah "$tmpdir/specifications-master/source/$1/tests/" "tests/spec/json/$1" --delete
rm -rf "$tmpdir"
49 changes: 49 additions & 0 deletions tests/spec/json/bson-corpus/array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"description": "Array",
"bson_type": "0x04",
"test_key": "a",
"valid": [
{
"description": "Empty",
"canonical_bson": "0D000000046100050000000000",
"canonical_extjson": "{\"a\" : []}"
},
{
"description": "Single Element Array",
"canonical_bson": "140000000461000C0000001030000A0000000000",
"canonical_extjson": "{\"a\" : [{\"$numberInt\": \"10\"}]}"
},
{
"description": "Single Element Array with index set incorrectly to empty string",
"degenerate_bson": "130000000461000B00000010000A0000000000",
"canonical_bson": "140000000461000C0000001030000A0000000000",
"canonical_extjson": "{\"a\" : [{\"$numberInt\": \"10\"}]}"
},
{
"description": "Single Element Array with index set incorrectly to ab",
"degenerate_bson": "150000000461000D000000106162000A0000000000",
"canonical_bson": "140000000461000C0000001030000A0000000000",
"canonical_extjson": "{\"a\" : [{\"$numberInt\": \"10\"}]}"
},
{
"description": "Multi Element Array with duplicate indexes",
"degenerate_bson": "1b000000046100130000001030000a000000103000140000000000",
"canonical_bson": "1b000000046100130000001030000a000000103100140000000000",
"canonical_extjson": "{\"a\" : [{\"$numberInt\": \"10\"}, {\"$numberInt\": \"20\"}]}"
}
],
"decodeErrors": [
{
"description": "Array length too long: eats outer terminator",
"bson": "140000000461000D0000001030000A0000000000"
},
{
"description": "Array length too short: leaks terminator",
"bson": "140000000461000B0000001030000A0000000000"
},
{
"description": "Invalid Array: bad string length in field",
"bson": "1A00000004666F6F00100000000230000500000062617A000000"
}
]
}
85 changes: 85 additions & 0 deletions tests/spec/json/bson-corpus/binary.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"description": "Binary type",
"bson_type": "0x05",
"test_key": "x",
"valid": [
{
"description": "subtype 0x00 (Zero-length)",
"canonical_bson": "0D000000057800000000000000",
"canonical_extjson": "{\"x\" : { \"$binary\" : {\"base64\" : \"\", \"subType\" : \"00\"}}}"
},
{
"description": "subtype 0x00 (Zero-length, keys reversed)",
"canonical_bson": "0D000000057800000000000000",
"canonical_extjson": "{\"x\" : { \"$binary\" : {\"base64\" : \"\", \"subType\" : \"00\"}}}",
"degenerate_extjson": "{\"x\" : { \"$binary\" : {\"subType\" : \"00\", \"base64\" : \"\"}}}"
},
{
"description": "subtype 0x00",
"canonical_bson": "0F0000000578000200000000FFFF00",
"canonical_extjson": "{\"x\" : { \"$binary\" : {\"base64\" : \"//8=\", \"subType\" : \"00\"}}}"
},
{
"description": "subtype 0x01",
"canonical_bson": "0F0000000578000200000001FFFF00",
"canonical_extjson": "{\"x\" : { \"$binary\" : {\"base64\" : \"//8=\", \"subType\" : \"01\"}}}"
},
{
"description": "subtype 0x02",
"canonical_bson": "13000000057800060000000202000000FFFF00",
"canonical_extjson": "{\"x\" : { \"$binary\" : {\"base64\" : \"//8=\", \"subType\" : \"02\"}}}"
},
{
"description": "subtype 0x03",
"canonical_bson": "1D000000057800100000000373FFD26444B34C6990E8E7D1DFC035D400",
"canonical_extjson": "{\"x\" : { \"$binary\" : {\"base64\" : \"c//SZESzTGmQ6OfR38A11A==\", \"subType\" : \"03\"}}}"
},
{
"description": "subtype 0x04",
"canonical_bson": "1D000000057800100000000473FFD26444B34C6990E8E7D1DFC035D400",
"canonical_extjson": "{\"x\" : { \"$binary\" : {\"base64\" : \"c//SZESzTGmQ6OfR38A11A==\", \"subType\" : \"04\"}}}"
},
{
"description": "subtype 0x05",
"canonical_bson": "1D000000057800100000000573FFD26444B34C6990E8E7D1DFC035D400",
"canonical_extjson": "{\"x\" : { \"$binary\" : {\"base64\" : \"c//SZESzTGmQ6OfR38A11A==\", \"subType\" : \"05\"}}}"
},
{
"description": "subtype 0x80",
"canonical_bson": "0F0000000578000200000080FFFF00",
"canonical_extjson": "{\"x\" : { \"$binary\" : {\"base64\" : \"//8=\", \"subType\" : \"80\"}}}"
},
{
"description": "$type query operator (conflicts with legacy $binary form with $type field)",
"canonical_bson": "1F000000037800170000000224747970650007000000737472696E67000000",
"canonical_extjson": "{\"x\" : { \"$type\" : \"string\"}}"
},
{
"description": "$type query operator (conflicts with legacy $binary form with $type field)",
"canonical_bson": "180000000378001000000010247479706500020000000000",
"canonical_extjson": "{\"x\" : { \"$type\" : {\"$numberInt\": \"2\"}}}"
}
],
"decodeErrors": [
{
"description": "Length longer than document",
"bson": "1D000000057800FF0000000573FFD26444B34C6990E8E7D1DFC035D400"
},
{
"description": "Negative length",
"bson": "0D000000057800FFFFFFFF0000"
},
{
"description": "subtype 0x02 length too long ",
"bson": "13000000057800060000000203000000FFFF00"
},
{
"description": "subtype 0x02 length too short",
"bson": "13000000057800060000000201000000FFFF00"
},
{
"description": "subtype 0x02 length negative one",
"bson": "130000000578000600000002FFFFFFFFFFFF00"
}
]
}
27 changes: 27 additions & 0 deletions tests/spec/json/bson-corpus/boolean.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"description": "Boolean",
"bson_type": "0x08",
"test_key": "b",
"valid": [
{
"description": "True",
"canonical_bson": "090000000862000100",
"canonical_extjson": "{\"b\" : true}"
},
{
"description": "False",
"canonical_bson": "090000000862000000",
"canonical_extjson": "{\"b\" : false}"
}
],
"decodeErrors": [
{
"description": "Invalid boolean value of 2",
"bson": "090000000862000200"
},
{
"description": "Invalid boolean value of -1",
"bson": "09000000086200FF00"
}
]
}
Loading

0 comments on commit a676305

Please sign in to comment.