Skip to content

Commit

Permalink
RUBY-1152 Implement BSON corpus tests
Browse files Browse the repository at this point in the history
  • Loading branch information
estolfo committed Nov 3, 2016
1 parent 2b8e3aa commit 9def3d2
Show file tree
Hide file tree
Showing 24 changed files with 1,161 additions and 0 deletions.
50 changes: 50 additions & 0 deletions spec/bson/corpus_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'spec_helper'

describe 'Driver BSON Corpus spec tests' do

specs = BSON_CORPUS_TESTS.map { |file| BSON::Corpus::Spec.new(file) }

specs.each do |spec|

context(spec.description) do

spec.valid_tests.each do |test|

context(test.description) do

it 'roundtrips the bson correctly' do
expect(test.reencoded_bson).to eq(test.correct_bson)
end

context 'when the bson provided results from an incorrect encoder', if: test.test_canonical_bson? do

it 'encodes the canonical bson correctly' do
expect(test.reencoded_canonical_bson).to eq(test.correct_bson)
end
end

context 'when the document can be represented as extended json', if: test.test_extjson? do

it 'decodes from bson, then encodes the document as extended json correctly' do
expect(test.extjson_from_bson).to eq(test.correct_extjson)
expect(test.extjson_from_bson[test.test_key]).to eq(test.correct_extjson[test.test_key])
end

it 'decodes from extended json, then encodes the document as extended json correctly' do
expect(test.extjson_from_encoded_extjson).to eq(test.correct_extjson)
expect(test.extjson_from_encoded_extjson[test.test_key]).to eq(test.correct_extjson[test.test_key])
end

context 'when the canonical bson can be represented as extended json', if: (test.test_canonical_bson? && test.test_extjson?) do

it 'encodes the canonical bson correctly as extended json' do
expect(test.extjson_from_canonical_bson).to eq(test.correct_extjson)
expect(test.extjson_from_canonical_bson[test.test_key]).to eq(test.correct_extjson[test.test_key])
end
end
end
end
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

CURRENT_PATH = File.expand_path(File.dirname(__FILE__))
DRIVER_COMMON_BSON_TESTS = Dir.glob("#{CURRENT_PATH}/support/driver-spec-tests/**/*.json")
BSON_CORPUS_TESTS = Dir.glob("#{CURRENT_PATH}/support/corpus-tests/*.json")

$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
Expand Down
43 changes: 43 additions & 0 deletions spec/support/corpus-tests/array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"description": "Array",
"bson_type": "0x04",
"test_key": "a",
"valid": [
{
"description": "Empty",
"bson": "0D000000046100050000000000",
"extjson": "{\"a\" : []}"
},
{
"description": "Single Element Array",
"bson": "140000000461000C0000001030000A0000000000",
"extjson": "{\"a\" : [10]}"
},
{
"description": "Single Element Array with index set incorrectly",
"bson": "130000000461000B00000010000A0000000000",
"canonical_bson": "140000000461000C0000001030000A0000000000",
"extjson": "{\"a\" : [10]}"
},
{
"description": "Single Element Array with index set incorrectly",
"bson": "150000000461000D000000106162000A0000000000",
"canonical_bson": "140000000461000C0000001030000A0000000000",
"extjson": "{\"a\" : [10]}"
}
],
"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"
}
]
}
27 changes: 27 additions & 0 deletions spec/support/corpus-tests/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",
"bson": "090000000862000100",
"extjson": "{\"b\" : true}"
},
{
"description": "False",
"bson": "090000000862000000",
"extjson": "{\"b\" : false}"
}
],
"decodeErrors": [
{
"description": "Invalid boolean value of 2",
"bson": "090000000862000200"
},
{
"description": "Invalid boolean value of -1",
"bson": "09000000086200FF00"
}
]
}
67 changes: 67 additions & 0 deletions spec/support/corpus-tests/code.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"description": "Javascript Code",
"bson_type": "0x0D",
"test_key": "a",
"valid": [
{
"description": "Empty string",
"bson": "0D0000000D6100010000000000",
"extjson": "{\"a\" : {\"$code\" : \"\"}}"
},
{
"description": "Single character",
"bson": "0E0000000D610002000000620000",
"extjson": "{\"a\" : {\"$code\" : \"b\"}}"
},
{
"description": "Multi-character",
"bson": "190000000D61000D0000006162616261626162616261620000",
"extjson": "{\"a\" : {\"$code\" : \"abababababab\"}}"
},
{
"description": "two-byte UTF-8 (\u00e9)",
"bson": "190000000261000D000000C3A9C3A9C3A9C3A9C3A9C3A90000",
"extjson": "{\"a\" : \"\\u00e9\\u00e9\\u00e9\\u00e9\\u00e9\\u00e9\"}"
},
{
"description": "three-byte UTF-8 (\u2606)",
"bson": "190000000261000D000000E29886E29886E29886E298860000",
"extjson": "{\"a\" : \"\\u2606\\u2606\\u2606\\u2606\"}"
},
{
"description": "Embedded nulls",
"bson": "190000000261000D0000006162006261620062616261620000",
"extjson": "{\"a\" : \"ab\\u0000bab\\u0000babab\"}"
}
],
"decodeErrors": [
{
"description": "bad code string length: 0 (but no 0x00 either)",
"bson": "0C0000000261000000000000"
},
{
"description": "bad code string length: -1",
"bson": "0C000000026100FFFFFFFF00"
},
{
"description": "bad code string length: eats terminator",
"bson": "10000000026100050000006200620000"
},
{
"description": "bad code string length: longer than rest of document",
"bson": "120000000200FFFFFF00666F6F6261720000"
},
{
"description": "code string is not null-terminated",
"bson": "1000000002610004000000616263FF00"
},
{
"description": "empty code string, but extra null",
"bson": "0E00000002610001000000000000"
},
{
"description": "invalid UTF-8",
"bson": "0E00000002610002000000E90000"
}
]
}
78 changes: 78 additions & 0 deletions spec/support/corpus-tests/code_w_scope.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"description": "Javascript Code with Scope",
"bson_type": "0x0F",
"test_key": "a",
"valid": [
{
"description": "Empty code string, empty scope",
"bson": "160000000F61000E0000000100000000050000000000",
"extjson": "{\"a\" : {\"$code\" : \"\", \"$scope\" : {}}}"
},
{
"description": "Non-empty code string, empty scope",
"bson": "1A0000000F610012000000050000006162636400050000000000",
"extjson": "{\"a\" : {\"$code\" : \"abcd\", \"$scope\" : {}}}"
},
{
"description": "Empty code string, non-empty scope",
"bson": "1D0000000F61001500000001000000000C000000107800010000000000",
"extjson": "{\"a\" : {\"$code\" : \"\", \"$scope\" : {\"x\" : 1}}}"
},
{
"description": "Non-empty code string and non-empty scope",
"bson": "210000000F6100190000000500000061626364000C000000107800010000000000",
"extjson": "{\"a\" : {\"$code\" : \"abcd\", \"$scope\" : {\"x\" : 1}}}"
},
{
"description": "Unicode and embedded null in code string, empty scope",
"bson": "1A0000000F61001200000005000000C3A9006400050000000000",
"extjson": "{\"a\" : {\"$code\" : \"\\u00e9\\u0000d\", \"$scope\" : {}}}"
}
],
"decodeErrors": [
{
"description": "field length zero",
"bson": "280000000F6100000000000500000061626364001300000010780001000000107900010000000000"
},
{
"description": "field length negative",
"bson": "280000000F6100FFFFFFFF0500000061626364001300000010780001000000107900010000000000"
},
{
"description": "field length too short (less than minimum size)",
"bson": "160000000F61000D0000000100000000050000000000"
},
{
"description": "field length too short (truncates scope)",
"bson": "280000000F61001F0000000500000061626364001300000010780001000000107900010000000000"
},
{
"description": "field length too long (clips outer doc)",
"bson": "280000000F6100210000000500000061626364001300000010780001000000107900010000000000"
},
{
"description": "field length too long (longer than outer doc)",
"bson": "280000000F6100FF0000000500000061626364001300000010780001000000107900010000000000"
},
{
"description": "bad code string: length too short",
"bson": "280000000F6100200000000400000061626364001300000010780001000000107900010000000000"
},
{
"description": "bad code string: length too long (clips scope)",
"bson": "280000000F6100200000000600000061626364001300000010780001000000107900010000000000"
},
{
"description": "bad code string: negative length",
"bson": "280000000F610020000000FFFFFFFF61626364001300000010780001000000107900010000000000"
},
{
"description": "bad code string: length longer than field",
"bson": "280000000F610020000000FF00000061626364001300000010780001000000107900010000000000"
},
{
"description": "bad scope doc (field has bad string length)",
"bson": "1C0000000F001500000001000000000C000000020000000000000000"
}
]
}
36 changes: 36 additions & 0 deletions spec/support/corpus-tests/document.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"description": "Document type (sub-documents)",
"bson_type": "0x03",
"test_key": "x",
"valid": [
{
"description": "Empty subdoc",
"bson": "0D000000037800050000000000",
"extjson": "{\"x\" : {}}"
},
{
"description": "Empty-string key subdoc",
"bson": "150000000378000D00000002000200000062000000",
"extjson": "{\"x\" : {\"\" : \"b\"}}"
},
{
"description": "Single-character key subdoc",
"bson": "160000000378000E0000000261000200000062000000",
"extjson": "{\"x\" : {\"a\" : \"b\"}}"
}
],
"decodeErrors": [
{
"description": "Subdocument length too long: eats outer terminator",
"bson": "1800000003666F6F000F0000001062617200FFFFFF7F0000"
},
{
"description": "Subdocument length too short: leaks terminator",
"bson": "1500000003666F6F000A0000000862617200010000"
},
{
"description": "Invalid subdocument: bad string length in field",
"bson": "1C00000003666F6F001200000002626172000500000062617A000000"
}
]
}
69 changes: 69 additions & 0 deletions spec/support/corpus-tests/double.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"description": "Double type",
"bson_type": "0x01",
"test_key": "d",
"valid": [
{
"description": "+1.0",
"bson": "10000000016400000000000000F03F00",
"extjson": "{\"d\" : 1.0}"
},
{
"description": "-1.0",
"bson": "10000000016400000000000000F0BF00",
"extjson": "{\"d\" : -1.0}"
},
{
"description": "+1.0001220703125",
"bson": "10000000016400000000008000F03F00",
"extjson": "{\"d\" : 1.0001220703125}"
},
{
"description": "-1.0001220703125",
"bson": "10000000016400000000008000F0BF00",
"extjson": "{\"d\" : -1.0001220703125}"
},
{
"description": "+2.0001220703125e10",
"bson": "1000000001640000807ca1a9a0124200",
"extjson": "{\"d\" : 2.0001220703125e10}"
},
{
"description": "-2.0001220703125e10",
"bson": "1000000001640000807ca1a9a012c200",
"extjson": "{\"d\" : -2.0001220703125e10}"
},
{
"description": "0.0",
"bson": "10000000016400000000000000000000",
"extjson": "{\"d\" : 0.0}"
},
{
"description": "-0.0",
"bson": "10000000016400000000000000008000",
"extjson": "{\"d\" : -0.0}"
},
{
"description": "NaN",
"bson": "10000000016400000000000000F87F00"
},
{
"description": "NaN with payload",
"bson": "10000000016400120000000000F87F00"
},
{
"description": "Inf",
"bson": "10000000016400000000000000F07F00"
},
{
"description": "-Inf",
"bson": "10000000016400000000000000F0FF00"
}
],
"decodeErrors": [
{
"description": "double truncated",
"bson": "0B0000000164000000F03F00"
}
]
}
Loading

0 comments on commit 9def3d2

Please sign in to comment.