You need to sign in or sign up before continuing.
bson 5.2.0-java → 5.2.1-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/bson/array.rb +13 -8
- data/lib/bson/binary.rb +14 -1
- data/lib/bson/db_pointer.rb +16 -0
- data/lib/bson/dbref.rb +0 -15
- data/lib/bson/environment.rb +0 -13
- data/lib/bson/ext_json.rb +33 -4
- data/lib/bson/float.rb +3 -0
- data/lib/bson/hash.rb +13 -8
- data/lib/bson/int32.rb +6 -0
- data/lib/bson/int64.rb +3 -0
- data/lib/bson/integer.rb +9 -0
- data/lib/bson/open_struct.rb +1 -5
- data/lib/bson/regexp.rb +0 -7
- data/lib/bson/string.rb +0 -5
- data/lib/bson/timestamp.rb +0 -1
- data/lib/bson/version.rb +1 -1
- data/lib/bson-ruby.jar +0 -0
- data/lib/bson.rb +34 -6
- data/spec/bson/array_spec.rb +24 -0
- data/spec/bson/binary_spec.rb +52 -0
- data/spec/bson/byte_buffer_write_spec.rb +15 -15
- data/spec/bson/date_spec.rb +2 -2
- data/spec/bson/date_time_spec.rb +2 -2
- data/spec/bson/db_pointer_spec.rb +163 -0
- data/spec/bson/decimal128_spec.rb +1 -1
- data/spec/bson/ext_json_parse_spec.rb +114 -0
- data/spec/bson/float_spec.rb +1 -1
- data/spec/bson/int32_spec.rb +4 -4
- data/spec/bson/int64_spec.rb +2 -2
- data/spec/bson/integer_spec.rb +2 -2
- data/spec/bson/max_nesting_depth_spec.rb +198 -0
- data/spec/bson/string_length_overflow_spec.rb +64 -0
- data/spec/bson/time_spec.rb +3 -3
- data/spec/bson/time_with_zone_spec.rb +2 -2
- data/spec/bson/timestamp_spec.rb +1 -1
- data/spec/bson_spec.rb +16 -4
- data/spec/support/pack_directives.rb +18 -0
- metadata +52 -3
- data/lib/bson/error/illegal_key.rb +0 -23
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2dad8d22ed422afa8d51368ad5a93e2fb5b2a5ddaf2822043836fa9f81259fd1
|
|
4
|
+
data.tar.gz: ebf85e5493a00488b29af2eba30c8bb50e0ecede517e1a0c28b8d4761859950e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2b03d02b139a8b590e0d681c0936e4945904d5db24bdcc373cb732f42f279cf1269095e839734dd7dd65d0b92362db107cfc529b4e2c4fb1123f45c947e767a9
|
|
7
|
+
data.tar.gz: 12881ee33bff7fe2de9691320e2a8be51d55c363e8900739388ca80872d150e5b79500d688ad7405a9c34b01857664048d0db9071ebb2ed0fa86dc97b4a99a67
|
data/lib/bson/array.rb
CHANGED
|
@@ -143,15 +143,20 @@ module BSON
|
|
|
143
143
|
# @raise [ BSON::Error::BSONDecodeError ] if the expected number of
|
|
144
144
|
# bytes were not read from the buffer
|
|
145
145
|
def parse_array_from_buffer(buffer, **options)
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
146
|
+
BSON.enter_nesting_depth
|
|
147
|
+
begin
|
|
148
|
+
new.tap do |array|
|
|
149
|
+
start_position = buffer.read_position
|
|
150
|
+
expected_byte_size = buffer.get_int32
|
|
151
|
+
parse_array_elements_from_buffer(array, buffer, **options)
|
|
152
|
+
actual_byte_size = buffer.read_position - start_position
|
|
153
|
+
if actual_byte_size != expected_byte_size
|
|
154
|
+
raise Error::BSONDecodeError,
|
|
155
|
+
"Expected array to take #{expected_byte_size} bytes but it took #{actual_byte_size} bytes"
|
|
156
|
+
end
|
|
154
157
|
end
|
|
158
|
+
ensure
|
|
159
|
+
BSON.leave_nesting_depth
|
|
155
160
|
end
|
|
156
161
|
end
|
|
157
162
|
|
data/lib/bson/binary.rb
CHANGED
|
@@ -306,7 +306,20 @@ module BSON
|
|
|
306
306
|
type = type_byte
|
|
307
307
|
end
|
|
308
308
|
|
|
309
|
-
|
|
309
|
+
if type == :old
|
|
310
|
+
inner_length = buffer.get_int32
|
|
311
|
+
unless inner_length == length - 4
|
|
312
|
+
raise Error::BSONDecodeError,
|
|
313
|
+
"BSON binary subtype 0x02 length mismatch: outer=#{length}, inner=#{inner_length}"
|
|
314
|
+
end
|
|
315
|
+
length = inner_length
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
if length.negative?
|
|
319
|
+
raise Error::BSONDecodeError,
|
|
320
|
+
"BSON binary length is negative: #{length}"
|
|
321
|
+
end
|
|
322
|
+
|
|
310
323
|
data = buffer.get_bytes(length)
|
|
311
324
|
new(data, type)
|
|
312
325
|
end
|
data/lib/bson/db_pointer.rb
CHANGED
|
@@ -28,9 +28,25 @@ module BSON
|
|
|
28
28
|
|
|
29
29
|
# Create a new DBPointer object.
|
|
30
30
|
#
|
|
31
|
+
# The types are enforced because the DBPointer wire format is a string
|
|
32
|
+
# followed by exactly 12 raw bytes. #to_bson writes the id duck-typed, so
|
|
33
|
+
# an id of any other type emits the wrong number of bytes and reframes the
|
|
34
|
+
# enclosing document for every conforming reader.
|
|
35
|
+
#
|
|
31
36
|
# @param [ String ] ref The database collection name.
|
|
32
37
|
# @param [ BSON::ObjectId ] id The DBPointer id.
|
|
38
|
+
#
|
|
39
|
+
# @raise [ ArgumentError ] If ref is not a String or id is not a
|
|
40
|
+
# BSON::ObjectId.
|
|
33
41
|
def initialize(ref, id)
|
|
42
|
+
unless ref.is_a?(::String)
|
|
43
|
+
raise ArgumentError, "DBPointer ref must be a String: #{ref.inspect}"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
unless id.is_a?(ObjectId)
|
|
47
|
+
raise ArgumentError, "DBPointer id must be a BSON::ObjectId: #{id.inspect}"
|
|
48
|
+
end
|
|
49
|
+
|
|
34
50
|
@ref = ref
|
|
35
51
|
@id = id
|
|
36
52
|
end
|
data/lib/bson/dbref.rb
CHANGED
|
@@ -21,21 +21,6 @@ module BSON
|
|
|
21
21
|
class DBRef < Document
|
|
22
22
|
include JSON
|
|
23
23
|
|
|
24
|
-
# The constant for the collection reference field.
|
|
25
|
-
#
|
|
26
|
-
# @deprecated
|
|
27
|
-
COLLECTION = '$ref'.freeze
|
|
28
|
-
|
|
29
|
-
# The constant for the id field.
|
|
30
|
-
#
|
|
31
|
-
# @deprecated
|
|
32
|
-
ID = '$id'.freeze
|
|
33
|
-
|
|
34
|
-
# The constant for the database field.
|
|
35
|
-
#
|
|
36
|
-
# @deprecated
|
|
37
|
-
DATABASE = '$db'.freeze
|
|
38
|
-
|
|
39
24
|
# @return [ String ] collection The collection name.
|
|
40
25
|
def collection
|
|
41
26
|
self['$ref']
|
data/lib/bson/environment.rb
CHANGED
|
@@ -34,18 +34,5 @@ module BSON
|
|
|
34
34
|
def jruby?
|
|
35
35
|
@jruby ||= defined?(JRUBY_VERSION)
|
|
36
36
|
end
|
|
37
|
-
|
|
38
|
-
# Determine if we are using Ruby version 1.9.
|
|
39
|
-
#
|
|
40
|
-
# @example Are we running with Ruby version 1.9?
|
|
41
|
-
# Environment.ruby_1_9?
|
|
42
|
-
#
|
|
43
|
-
# @return [ true, false ] If the Ruby version is 1.9.
|
|
44
|
-
#
|
|
45
|
-
# @since 4.2.0
|
|
46
|
-
# @deprecated
|
|
47
|
-
def ruby_1_9?
|
|
48
|
-
@ruby_1_9 ||= RUBY_VERSION < '2.0.0'
|
|
49
|
-
end
|
|
50
37
|
end
|
|
51
38
|
end
|
data/lib/bson/ext_json.rb
CHANGED
|
@@ -115,8 +115,13 @@ module BSON
|
|
|
115
115
|
when Hash
|
|
116
116
|
parse_hash(value, **options)
|
|
117
117
|
when Array
|
|
118
|
-
|
|
119
|
-
|
|
118
|
+
BSON.enter_nesting_depth
|
|
119
|
+
begin
|
|
120
|
+
value.map do |item|
|
|
121
|
+
parse_obj(item, **options)
|
|
122
|
+
end
|
|
123
|
+
ensure
|
|
124
|
+
BSON.leave_nesting_depth
|
|
120
125
|
end
|
|
121
126
|
else
|
|
122
127
|
raise Error::ExtJSONParseError, "Unknown value type: #{value}"
|
|
@@ -136,6 +141,15 @@ module BSON
|
|
|
136
141
|
end].freeze
|
|
137
142
|
|
|
138
143
|
module_function def parse_hash(hash, **options)
|
|
144
|
+
BSON.enter_nesting_depth
|
|
145
|
+
begin
|
|
146
|
+
return parse_hash_body(hash, **options)
|
|
147
|
+
ensure
|
|
148
|
+
BSON.leave_nesting_depth
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
module_function def parse_hash_body(hash, **options)
|
|
139
153
|
if hash.empty?
|
|
140
154
|
return {}
|
|
141
155
|
end
|
|
@@ -246,10 +260,25 @@ module BSON
|
|
|
246
260
|
# TODO consider returning Ruby regular expression object here
|
|
247
261
|
create_regexp(value['pattern'], value['options'])
|
|
248
262
|
when '$dbPointer'
|
|
249
|
-
unless value.keys.sort == %w($id $ref)
|
|
263
|
+
unless value.is_a?(Hash) && value.keys.sort == %w($id $ref)
|
|
250
264
|
raise Error::ExtJSONParseError, "Invalid $dbPointer value: #{value}"
|
|
251
265
|
end
|
|
252
|
-
|
|
266
|
+
ref = value['$ref']
|
|
267
|
+
unless ref.is_a?(String)
|
|
268
|
+
raise Error::ExtJSONParseError, "Invalid $dbPointer, $ref must be a string: #{value}"
|
|
269
|
+
end
|
|
270
|
+
# The DBPointer wire format is a string followed by exactly 12 raw
|
|
271
|
+
# bytes, so the id must be an ObjectId. Any other type serializes to
|
|
272
|
+
# a different number of bytes and reframes the enclosing document.
|
|
273
|
+
id = value['$id']
|
|
274
|
+
unless id.is_a?(Hash)
|
|
275
|
+
raise Error::ExtJSONParseError, "Invalid $dbPointer, $id must be an ObjectId: #{value}"
|
|
276
|
+
end
|
|
277
|
+
id = parse_hash(id, **options)
|
|
278
|
+
unless id.is_a?(ObjectId)
|
|
279
|
+
raise Error::ExtJSONParseError, "Invalid $dbPointer, $id must be an ObjectId: #{value}"
|
|
280
|
+
end
|
|
281
|
+
DbPointer.new(ref, id)
|
|
253
282
|
when '$date'
|
|
254
283
|
case value
|
|
255
284
|
when String
|
data/lib/bson/float.rb
CHANGED
|
@@ -32,7 +32,10 @@ module BSON
|
|
|
32
32
|
# The pack directive is for 8 byte floating points.
|
|
33
33
|
#
|
|
34
34
|
# @since 2.0.0
|
|
35
|
+
# @deprecated No longer used internally; retained for backward
|
|
36
|
+
# compatibility and slated for removal in a future major version.
|
|
35
37
|
PACK = "E"
|
|
38
|
+
deprecate_constant :PACK
|
|
36
39
|
|
|
37
40
|
# Get the floating point as encoded BSON.
|
|
38
41
|
#
|
data/lib/bson/hash.rb
CHANGED
|
@@ -167,17 +167,22 @@ module BSON
|
|
|
167
167
|
#
|
|
168
168
|
# @return [ Hash ] the hash parsed from the buffer
|
|
169
169
|
def parse_hash_from_buffer(buffer, **options)
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
170
|
+
BSON.enter_nesting_depth
|
|
171
|
+
begin
|
|
172
|
+
hash = Document.allocate
|
|
173
|
+
start_position = buffer.read_position
|
|
174
|
+
expected_byte_size = buffer.get_int32
|
|
173
175
|
|
|
174
|
-
|
|
176
|
+
parse_hash_contents(hash, buffer, **options)
|
|
175
177
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
+
actual_byte_size = buffer.read_position - start_position
|
|
179
|
+
return hash unless actual_byte_size != expected_byte_size
|
|
178
180
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
+
raise Error::BSONDecodeError,
|
|
182
|
+
"Expected hash to take #{expected_byte_size} bytes but it took #{actual_byte_size} bytes"
|
|
183
|
+
ensure
|
|
184
|
+
BSON.leave_nesting_depth
|
|
185
|
+
end
|
|
181
186
|
end
|
|
182
187
|
|
|
183
188
|
# Given an empty hash and a byte buffer, parse the key/value pairs from
|
data/lib/bson/int32.rb
CHANGED
|
@@ -32,12 +32,18 @@ module BSON
|
|
|
32
32
|
# The number of bytes constant.
|
|
33
33
|
#
|
|
34
34
|
# @since 4.0.0
|
|
35
|
+
# @deprecated No longer used internally; retained for backward
|
|
36
|
+
# compatibility and slated for removal in a future major version.
|
|
35
37
|
BYTES_LENGTH = 4
|
|
38
|
+
deprecate_constant :BYTES_LENGTH
|
|
36
39
|
|
|
37
40
|
# Constant for the int 32 pack directive.
|
|
38
41
|
#
|
|
39
42
|
# @since 2.0.0
|
|
43
|
+
# @deprecated No longer used internally; retained for backward
|
|
44
|
+
# compatibility and slated for removal in a future major version.
|
|
40
45
|
PACK = "l<"
|
|
46
|
+
deprecate_constant :PACK
|
|
41
47
|
|
|
42
48
|
# Deserialize an Integer from BSON.
|
|
43
49
|
#
|
data/lib/bson/int64.rb
CHANGED
|
@@ -32,7 +32,10 @@ module BSON
|
|
|
32
32
|
# Constant for the int 64 pack directive.
|
|
33
33
|
#
|
|
34
34
|
# @since 2.0.0
|
|
35
|
+
# @deprecated No longer used internally; retained for backward
|
|
36
|
+
# compatibility and slated for removal in a future major version.
|
|
35
37
|
PACK = "q<"
|
|
38
|
+
deprecate_constant :PACK
|
|
36
39
|
|
|
37
40
|
# Deserialize an Integer from BSON.
|
|
38
41
|
#
|
data/lib/bson/integer.rb
CHANGED
|
@@ -47,15 +47,24 @@ module BSON
|
|
|
47
47
|
# The BSON index size.
|
|
48
48
|
#
|
|
49
49
|
# @since 2.0.0
|
|
50
|
+
# @deprecated No longer used internally; retained for backward
|
|
51
|
+
# compatibility and slated for removal in a future major version.
|
|
50
52
|
BSON_INDEX_SIZE = 1024
|
|
51
53
|
|
|
52
54
|
# A hash of index values for array optimization.
|
|
53
55
|
#
|
|
54
56
|
# @since 2.0.0
|
|
57
|
+
# @deprecated No longer used internally; retained for backward
|
|
58
|
+
# compatibility and slated for removal in a future major version.
|
|
55
59
|
BSON_ARRAY_INDEXES = ::Array.new(BSON_INDEX_SIZE) do |i|
|
|
56
60
|
(i.to_s.b << NULL_BYTE).freeze
|
|
57
61
|
end.freeze
|
|
58
62
|
|
|
63
|
+
# Deprecated after BSON_ARRAY_INDEXES is built, since that (also
|
|
64
|
+
# deprecated) constant references BSON_INDEX_SIZE at load time.
|
|
65
|
+
deprecate_constant :BSON_INDEX_SIZE
|
|
66
|
+
deprecate_constant :BSON_ARRAY_INDEXES
|
|
67
|
+
|
|
59
68
|
# Is this integer a valid BSON 32 bit value?
|
|
60
69
|
#
|
|
61
70
|
# @example Is the integer a valid 32 bit value?
|
data/lib/bson/open_struct.rb
CHANGED
|
@@ -35,11 +35,7 @@ module BSON
|
|
|
35
35
|
#
|
|
36
36
|
# @since 4.2.0
|
|
37
37
|
def to_bson(buffer = ByteBuffer.new)
|
|
38
|
-
|
|
39
|
-
marshal_dump.dup
|
|
40
|
-
else
|
|
41
|
-
to_h
|
|
42
|
-
end.to_bson(buffer)
|
|
38
|
+
to_h.to_bson(buffer)
|
|
43
39
|
end
|
|
44
40
|
|
|
45
41
|
# The BSON type for OpenStruct objects is the Hash type of 0x03.
|
data/lib/bson/regexp.rb
CHANGED
|
@@ -50,13 +50,6 @@ module BSON
|
|
|
50
50
|
# @since 3.2.6
|
|
51
51
|
NEWLINE_VALUE = 's'
|
|
52
52
|
|
|
53
|
-
# Ruby multiline constant.
|
|
54
|
-
#
|
|
55
|
-
# @since 3.2.6
|
|
56
|
-
#
|
|
57
|
-
# @deprecated Will be removed in 5.0
|
|
58
|
-
RUBY_MULTILINE_VALUE = 'ms'
|
|
59
|
-
|
|
60
53
|
# Get the regexp as JSON hash data.
|
|
61
54
|
#
|
|
62
55
|
# @example Get the regexp as a JSON hash.
|
data/lib/bson/string.rb
CHANGED
|
@@ -30,11 +30,6 @@ module BSON
|
|
|
30
30
|
# @since 2.0.0
|
|
31
31
|
BSON_TYPE = ::String.new(2.chr, encoding: BINARY).freeze
|
|
32
32
|
|
|
33
|
-
# Regex for matching illegal BSON keys.
|
|
34
|
-
#
|
|
35
|
-
# @since 4.1.0
|
|
36
|
-
ILLEGAL_KEY = /(\A[$])|(\.)/
|
|
37
|
-
|
|
38
33
|
# Get the string as encoded BSON.
|
|
39
34
|
#
|
|
40
35
|
# @example Get the string as encoded BSON.
|
data/lib/bson/timestamp.rb
CHANGED
data/lib/bson/version.rb
CHANGED
data/lib/bson-ruby.jar
CHANGED
|
Binary file
|
data/lib/bson.rb
CHANGED
|
@@ -21,20 +21,25 @@ require "bson/environment"
|
|
|
21
21
|
# @since 0.0.0
|
|
22
22
|
module BSON
|
|
23
23
|
|
|
24
|
-
#
|
|
24
|
+
# Interprets the argument as an ObjectId. If given a BSON::ObjectId, this method
|
|
25
|
+
# returns it directly without creating a new one. Otherwise, the argument is
|
|
26
|
+
# interpreted as a string and parsed accordingly.
|
|
25
27
|
#
|
|
26
28
|
# @example Create an object id from the string.
|
|
27
|
-
# BSON::ObjectId(
|
|
29
|
+
# BSON::ObjectId('...')
|
|
28
30
|
#
|
|
29
|
-
# @param [ String ]
|
|
31
|
+
# @param [ String | BSON::ObjectId ] string_or_object_id The string to create the id from,
|
|
32
|
+
# or the existing object id to return.
|
|
30
33
|
#
|
|
31
34
|
# @raise [ BSON::Error::InvalidObjectId ] If the provided string is invalid.
|
|
32
35
|
#
|
|
33
|
-
# @return [ BSON::ObjectId ] The new object id.
|
|
36
|
+
# @return [ BSON::ObjectId ] The new or existing object id.
|
|
34
37
|
#
|
|
35
38
|
# @see ObjectId.from_string
|
|
36
|
-
def self.ObjectId(
|
|
37
|
-
self::ObjectId
|
|
39
|
+
def self.ObjectId(string_or_object_id)
|
|
40
|
+
return string_or_object_id if string_or_object_id.is_a?(self::ObjectId)
|
|
41
|
+
|
|
42
|
+
self::ObjectId.from_string(string_or_object_id)
|
|
38
43
|
end
|
|
39
44
|
|
|
40
45
|
# Constant for binary string encoding.
|
|
@@ -56,6 +61,29 @@ module BSON
|
|
|
56
61
|
#
|
|
57
62
|
# @since 2.0.0
|
|
58
63
|
UTF8 = "UTF-8"
|
|
64
|
+
|
|
65
|
+
# Maximum number of nested BSON documents or arrays the decoder will accept.
|
|
66
|
+
# Prevents stack-overflow DoS on adversarial input. Matches libbson and the
|
|
67
|
+
# Go driver's ExtJSON parser.
|
|
68
|
+
MAX_NESTING_DEPTH = 200
|
|
69
|
+
|
|
70
|
+
# Bump the per-thread BSON decode nesting counter and raise if it exceeds
|
|
71
|
+
# MAX_NESTING_DEPTH. Pair every call with `leave_nesting_depth` in an
|
|
72
|
+
# `ensure` block. The bump is inlined at each callsite (rather than a
|
|
73
|
+
# block-yielding helper) to keep JRuby JVM stack frame counts low enough
|
|
74
|
+
# that the check fires before the JVM stack overflows on adversarial input.
|
|
75
|
+
def self.enter_nesting_depth
|
|
76
|
+
depth = (Thread.current[:_bson_nesting_depth] ||= 0) + 1
|
|
77
|
+
if depth > MAX_NESTING_DEPTH
|
|
78
|
+
raise Error::BSONDecodeError,
|
|
79
|
+
"BSON document nesting depth exceeds maximum of #{MAX_NESTING_DEPTH}"
|
|
80
|
+
end
|
|
81
|
+
Thread.current[:_bson_nesting_depth] = depth
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def self.leave_nesting_depth
|
|
85
|
+
Thread.current[:_bson_nesting_depth] -= 1
|
|
86
|
+
end
|
|
59
87
|
end
|
|
60
88
|
|
|
61
89
|
require "bson/config"
|
data/spec/bson/array_spec.rb
CHANGED
|
@@ -75,6 +75,30 @@ describe Array do
|
|
|
75
75
|
end
|
|
76
76
|
end
|
|
77
77
|
|
|
78
|
+
context 'when an array element mutates the array size during serialization',
|
|
79
|
+
if: BSON::ByteBuffer.new.respond_to?(:put_array) do
|
|
80
|
+
let(:array) { Array.new(10, 1) }
|
|
81
|
+
|
|
82
|
+
before do
|
|
83
|
+
evil = Class.new do
|
|
84
|
+
define_method(:initialize) { |a| @array = a; @mutated = false }
|
|
85
|
+
define_method(:bson_type) do
|
|
86
|
+
unless @mutated
|
|
87
|
+
@mutated = true
|
|
88
|
+
@array.replace(Array.new(200_000, 0))
|
|
89
|
+
end
|
|
90
|
+
BSON::Int32::BSON_TYPE
|
|
91
|
+
end
|
|
92
|
+
define_method(:to_bson) { |buffer| 123.to_bson(buffer) }
|
|
93
|
+
end
|
|
94
|
+
array[0] = evil.new(array)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it 'raises a RuntimeError' do
|
|
98
|
+
expect { array.to_bson }.to raise_error(RuntimeError, /array modified during BSON serialization/)
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
78
102
|
context 'when array contains value of an unserializable class' do
|
|
79
103
|
class ArraySpecUnserializableClass
|
|
80
104
|
end
|
data/spec/bson/binary_spec.rb
CHANGED
|
@@ -204,6 +204,58 @@ describe BSON::Binary do
|
|
|
204
204
|
/BSON data contains unsupported binary subtype 0x10/)
|
|
205
205
|
end
|
|
206
206
|
end
|
|
207
|
+
|
|
208
|
+
context 'when subtype 0x02 inner length is too long' do
|
|
209
|
+
let(:bson) do
|
|
210
|
+
([6].pack('l<') + 2.chr + [3].pack('l<') + 'xxx').force_encoding('BINARY')
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
it 'raises BSONDecodeError' do
|
|
214
|
+
expect { obj }.to raise_error(
|
|
215
|
+
BSON::Error::BSONDecodeError,
|
|
216
|
+
/length mismatch.*outer=6.*inner=3/
|
|
217
|
+
)
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
context 'when subtype 0x02 inner length is too short' do
|
|
222
|
+
let(:bson) do
|
|
223
|
+
([6].pack('l<') + 2.chr + [1].pack('l<') + 'xxx').force_encoding('BINARY')
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
it 'raises BSONDecodeError' do
|
|
227
|
+
expect { obj }.to raise_error(
|
|
228
|
+
BSON::Error::BSONDecodeError,
|
|
229
|
+
/length mismatch.*outer=6.*inner=1/
|
|
230
|
+
)
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
context 'when subtype 0x02 inner length is negative' do
|
|
235
|
+
let(:bson) do
|
|
236
|
+
([6].pack('l<') + 2.chr + [-1].pack('l<') + 'xx').force_encoding('BINARY')
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
it 'raises BSONDecodeError' do
|
|
240
|
+
expect { obj }.to raise_error(
|
|
241
|
+
BSON::Error::BSONDecodeError,
|
|
242
|
+
/length mismatch.*outer=6.*inner=-1/
|
|
243
|
+
)
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
context 'when non-old subtype outer length is negative' do
|
|
248
|
+
let(:bson) do
|
|
249
|
+
([-1].pack('l<') + 0.chr).force_encoding('BINARY')
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
it 'raises BSONDecodeError' do
|
|
253
|
+
expect { obj }.to raise_error(
|
|
254
|
+
BSON::Error::BSONDecodeError,
|
|
255
|
+
/length is negative.*-1/
|
|
256
|
+
)
|
|
257
|
+
end
|
|
258
|
+
end
|
|
207
259
|
end
|
|
208
260
|
|
|
209
261
|
describe "#to_bson/#from_bson" do
|
|
@@ -238,7 +238,7 @@ describe BSON::ByteBuffer do
|
|
|
238
238
|
|
|
239
239
|
it 'appends the string to the byte buffer' do
|
|
240
240
|
expect(modified.to_s).to eq(
|
|
241
|
-
"#{[ 4 ].pack(
|
|
241
|
+
"#{[ 4 ].pack(PackDirectives::INT32)}#{(string.bytesize + 1).to_bson.to_s}#{string}#{BSON::NULL_BYTE}"
|
|
242
242
|
)
|
|
243
243
|
end
|
|
244
244
|
|
|
@@ -457,7 +457,7 @@ describe BSON::ByteBuffer do
|
|
|
457
457
|
end
|
|
458
458
|
|
|
459
459
|
it 'appends the double to the buffer' do
|
|
460
|
-
expect(modified.to_s).to eq([ 1.2332 ].pack(
|
|
460
|
+
expect(modified.to_s).to eq([ 1.2332 ].pack(PackDirectives::FLOAT))
|
|
461
461
|
end
|
|
462
462
|
|
|
463
463
|
it 'increments the write position by 8' do
|
|
@@ -471,7 +471,7 @@ describe BSON::ByteBuffer do
|
|
|
471
471
|
end
|
|
472
472
|
|
|
473
473
|
it 'writes a double' do
|
|
474
|
-
expect(modified.to_s).to eq([ 3 ].pack(
|
|
474
|
+
expect(modified.to_s).to eq([ 3 ].pack(PackDirectives::FLOAT))
|
|
475
475
|
end
|
|
476
476
|
|
|
477
477
|
it 'increments the write position by 8' do
|
|
@@ -508,7 +508,7 @@ describe BSON::ByteBuffer do
|
|
|
508
508
|
it 'raises TypeError' do
|
|
509
509
|
expect do
|
|
510
510
|
modified
|
|
511
|
-
end.to raise_error(TypeError, /no implicit conversion to float from string|ClassCastException:.*RubyString cannot be cast to.*RubyFloat/)
|
|
511
|
+
end.to raise_error(TypeError, /no implicit conversion to float from string|no implicit conversion of String into Float|ClassCastException:.*RubyString cannot be cast to.*RubyFloat/)
|
|
512
512
|
expect(buffer.write_position).to eq(0)
|
|
513
513
|
end
|
|
514
514
|
end
|
|
@@ -525,7 +525,7 @@ describe BSON::ByteBuffer do
|
|
|
525
525
|
end
|
|
526
526
|
|
|
527
527
|
let(:expected) do
|
|
528
|
-
[ Integer::MAX_32BIT - 1 ].pack(
|
|
528
|
+
[ Integer::MAX_32BIT - 1 ].pack(PackDirectives::INT32)
|
|
529
529
|
end
|
|
530
530
|
|
|
531
531
|
it 'appends the int32 to the byte buffer' do
|
|
@@ -544,7 +544,7 @@ describe BSON::ByteBuffer do
|
|
|
544
544
|
end
|
|
545
545
|
|
|
546
546
|
let(:expected) do
|
|
547
|
-
[ Integer::MIN_32BIT + 1 ].pack(
|
|
547
|
+
[ Integer::MIN_32BIT + 1 ].pack(PackDirectives::INT32)
|
|
548
548
|
end
|
|
549
549
|
|
|
550
550
|
it 'appends the int32 to the byte buffer' do
|
|
@@ -573,7 +573,7 @@ describe BSON::ByteBuffer do
|
|
|
573
573
|
end
|
|
574
574
|
|
|
575
575
|
let(:expected) do
|
|
576
|
-
[ 4 ].pack(
|
|
576
|
+
[ 4 ].pack(PackDirectives::INT32)
|
|
577
577
|
end
|
|
578
578
|
|
|
579
579
|
it 'appends the int32 to the byte buffer' do
|
|
@@ -627,7 +627,7 @@ describe BSON::ByteBuffer do
|
|
|
627
627
|
end
|
|
628
628
|
|
|
629
629
|
let(:expected) do
|
|
630
|
-
[ 4294967295 ].pack(
|
|
630
|
+
[ 4294967295 ].pack(PackDirectives::INT32)
|
|
631
631
|
end
|
|
632
632
|
|
|
633
633
|
it 'appends the int32 to the byte buffer' do
|
|
@@ -693,7 +693,7 @@ describe BSON::ByteBuffer do
|
|
|
693
693
|
end
|
|
694
694
|
|
|
695
695
|
let(:expected) do
|
|
696
|
-
[ Integer::MAX_64BIT - 1 ].pack(
|
|
696
|
+
[ Integer::MAX_64BIT - 1 ].pack(PackDirectives::INT64)
|
|
697
697
|
end
|
|
698
698
|
|
|
699
699
|
it 'appends the int64 to the byte buffer' do
|
|
@@ -712,7 +712,7 @@ describe BSON::ByteBuffer do
|
|
|
712
712
|
end
|
|
713
713
|
|
|
714
714
|
let(:expected) do
|
|
715
|
-
[ Integer::MIN_64BIT + 1 ].pack(
|
|
715
|
+
[ Integer::MIN_64BIT + 1 ].pack(PackDirectives::INT64)
|
|
716
716
|
end
|
|
717
717
|
|
|
718
718
|
it 'appends the int64 to the byte buffer' do
|
|
@@ -751,7 +751,7 @@ describe BSON::ByteBuffer do
|
|
|
751
751
|
end
|
|
752
752
|
|
|
753
753
|
let(:expected) do
|
|
754
|
-
[ 4 ].pack(
|
|
754
|
+
[ 4 ].pack(PackDirectives::INT64)
|
|
755
755
|
end
|
|
756
756
|
|
|
757
757
|
it 'appends the int64 to the byte buffer' do
|
|
@@ -767,19 +767,19 @@ describe BSON::ByteBuffer do
|
|
|
767
767
|
describe '#replace_int32' do
|
|
768
768
|
|
|
769
769
|
let(:exp_0) do
|
|
770
|
-
[ 0 ].pack(
|
|
770
|
+
[ 0 ].pack(PackDirectives::INT32)
|
|
771
771
|
end
|
|
772
772
|
|
|
773
773
|
let(:exp_first) do
|
|
774
|
-
[ 5 ].pack(
|
|
774
|
+
[ 5 ].pack(PackDirectives::INT32)
|
|
775
775
|
end
|
|
776
776
|
|
|
777
777
|
let(:exp_second) do
|
|
778
|
-
[ 4 ].pack(
|
|
778
|
+
[ 4 ].pack(PackDirectives::INT32)
|
|
779
779
|
end
|
|
780
780
|
|
|
781
781
|
let(:exp_42) do
|
|
782
|
-
[ 42 ].pack(
|
|
782
|
+
[ 42 ].pack(PackDirectives::INT32)
|
|
783
783
|
end
|
|
784
784
|
|
|
785
785
|
let(:modified) do
|
data/spec/bson/date_spec.rb
CHANGED
|
@@ -25,7 +25,7 @@ describe Date do
|
|
|
25
25
|
|
|
26
26
|
let(:obj) { Date.new(2012, 1, 1) }
|
|
27
27
|
let(:time) { Time.utc(2012, 1, 1) }
|
|
28
|
-
let(:bson) { [ (time.to_f * 1000).to_i ].pack(
|
|
28
|
+
let(:bson) { [ (time.to_f * 1000).to_i ].pack(PackDirectives::INT64) }
|
|
29
29
|
|
|
30
30
|
it_behaves_like "a serializable bson element"
|
|
31
31
|
end
|
|
@@ -34,7 +34,7 @@ describe Date do
|
|
|
34
34
|
|
|
35
35
|
let(:obj) { Date.new(1969, 1, 1) }
|
|
36
36
|
let(:time) { Time.utc(1969, 1, 1) }
|
|
37
|
-
let(:bson) { [ (time.to_f * 1000).to_i ].pack(
|
|
37
|
+
let(:bson) { [ (time.to_f * 1000).to_i ].pack(PackDirectives::INT64) }
|
|
38
38
|
|
|
39
39
|
it_behaves_like "a serializable bson element"
|
|
40
40
|
end
|
data/spec/bson/date_time_spec.rb
CHANGED
|
@@ -24,7 +24,7 @@ describe DateTime do
|
|
|
24
24
|
context "when the date time is post epoch" do
|
|
25
25
|
|
|
26
26
|
let(:obj) { DateTime.new(2012, 1, 1, 0, 0, 0) }
|
|
27
|
-
let(:bson) { [ (obj.to_time.to_f * 1000).to_i ].pack(
|
|
27
|
+
let(:bson) { [ (obj.to_time.to_f * 1000).to_i ].pack(PackDirectives::INT64) }
|
|
28
28
|
|
|
29
29
|
it_behaves_like "a serializable bson element"
|
|
30
30
|
end
|
|
@@ -32,7 +32,7 @@ describe DateTime do
|
|
|
32
32
|
context "when the date time is pre epoch" do
|
|
33
33
|
|
|
34
34
|
let(:obj) { DateTime.new(1969, 1, 1, 0, 0, 0) }
|
|
35
|
-
let(:bson) { [ (obj.to_time.to_f * 1000).to_i ].pack(
|
|
35
|
+
let(:bson) { [ (obj.to_time.to_f * 1000).to_i ].pack(PackDirectives::INT64) }
|
|
36
36
|
|
|
37
37
|
it_behaves_like "a serializable bson element"
|
|
38
38
|
end
|