bson 5.2.0 → 5.2.1
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/ext/bson/read.c +36 -7
- data/ext/bson/write.c +36 -14
- 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.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 +449 -392
- data/lib/bson/error/illegal_key.rb +0 -23
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
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
# rubocop:todo all
|
|
2
|
+
# Copyright (C) 2020 MongoDB Inc.
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
|
|
16
|
+
require "spec_helper"
|
|
17
|
+
|
|
18
|
+
describe BSON::DbPointer do
|
|
19
|
+
|
|
20
|
+
let(:ref) { 'db.coll' }
|
|
21
|
+
let(:id) { BSON::ObjectId.from_string('56e1fc72e0c917e9c4714161') }
|
|
22
|
+
let(:object) { described_class.new(ref, id) }
|
|
23
|
+
|
|
24
|
+
describe "#initialize" do
|
|
25
|
+
|
|
26
|
+
it "stores the ref and the id" do
|
|
27
|
+
expect(object.ref).to eq(ref)
|
|
28
|
+
expect(object.id).to eq(id)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# RUBY-3936. The DBPointer wire format is a string followed by exactly 12
|
|
32
|
+
# raw bytes, and #to_bson writes the id duck-typed. An id of any other type
|
|
33
|
+
# emits the wrong number of bytes, which reframes the enclosing document
|
|
34
|
+
# for every conforming reader. Reject it at construction instead.
|
|
35
|
+
context "when the id is not an ObjectId" do
|
|
36
|
+
|
|
37
|
+
[
|
|
38
|
+
[ 'a binary', BSON::Binary.new('12345678901234567890', :generic) ],
|
|
39
|
+
[ 'a string', '56e1fc72e0c917e9c4714161' ],
|
|
40
|
+
[ 'a hash', { 'a' => 1 } ],
|
|
41
|
+
[ 'an integer', 1 ],
|
|
42
|
+
[ 'nil', nil ],
|
|
43
|
+
].each do |desc, bad_id|
|
|
44
|
+
|
|
45
|
+
context "when the id is #{desc}" do
|
|
46
|
+
|
|
47
|
+
it "raises an error" do
|
|
48
|
+
expect do
|
|
49
|
+
described_class.new(ref, bad_id)
|
|
50
|
+
end.to raise_error(ArgumentError, /id must be a BSON::ObjectId/)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
context "when the ref is not a string" do
|
|
57
|
+
|
|
58
|
+
[
|
|
59
|
+
[ 'nil', nil ],
|
|
60
|
+
[ 'an integer', 5 ],
|
|
61
|
+
[ 'a symbol', :coll ],
|
|
62
|
+
].each do |desc, bad_ref|
|
|
63
|
+
|
|
64
|
+
context "when the ref is #{desc}" do
|
|
65
|
+
|
|
66
|
+
it "raises an error" do
|
|
67
|
+
expect do
|
|
68
|
+
described_class.new(bad_ref, id)
|
|
69
|
+
end.to raise_error(ArgumentError, /ref must be a String/)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
describe "#==" do
|
|
77
|
+
|
|
78
|
+
context "when the objects are equal" do
|
|
79
|
+
let(:other) { described_class.new(ref, id) }
|
|
80
|
+
|
|
81
|
+
it "returns true" do
|
|
82
|
+
expect(object).to eq(other)
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
context "when the refs are not equal" do
|
|
87
|
+
let(:other) { described_class.new('db.other', id) }
|
|
88
|
+
|
|
89
|
+
it "returns false" do
|
|
90
|
+
expect(object).to_not eq(other)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
context "when the ids are not equal" do
|
|
95
|
+
let(:other) { described_class.new(ref, BSON::ObjectId.new) }
|
|
96
|
+
|
|
97
|
+
it "returns false" do
|
|
98
|
+
expect(object).to_not eq(other)
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
context "when the other object is not a DbPointer" do
|
|
103
|
+
|
|
104
|
+
it "returns false" do
|
|
105
|
+
expect(object).to_not eq('foo')
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
describe "#as_extended_json" do
|
|
111
|
+
|
|
112
|
+
it "returns the canonical extended json representation" do
|
|
113
|
+
expect(object.as_extended_json).to eq(
|
|
114
|
+
{ '$dbPointer' => { '$ref' => ref, '$id' => { '$oid' => '56e1fc72e0c917e9c4714161' } } }
|
|
115
|
+
)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
it_behaves_like 'an Extended JSON serializable object'
|
|
119
|
+
it_behaves_like '#as_json calls #as_extended_json'
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
describe "#to_bson" do
|
|
123
|
+
|
|
124
|
+
let(:type) { 12.chr }
|
|
125
|
+
let(:obj) { object }
|
|
126
|
+
let(:bson) do
|
|
127
|
+
"#{(ref.length + 1).to_bson.to_s}#{ref}#{BSON::NULL_BYTE}#{id.to_bson.to_s}"
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
it_behaves_like "a bson element"
|
|
131
|
+
it_behaves_like "a serializable bson element"
|
|
132
|
+
|
|
133
|
+
it "writes exactly twelve bytes for the id" do
|
|
134
|
+
encoded = obj.to_bson.to_s
|
|
135
|
+
expect(encoded.bytesize - (4 + ref.length + 1)).to eq(12)
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
describe "#from_bson" do
|
|
140
|
+
|
|
141
|
+
let(:type) { 12.chr }
|
|
142
|
+
let(:obj) { object }
|
|
143
|
+
let(:bson) do
|
|
144
|
+
"#{(ref.length + 1).to_bson.to_s}#{ref}#{BSON::NULL_BYTE}#{id.to_bson.to_s}"
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
it_behaves_like "a deserializable bson element"
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
describe "round trip through a document" do
|
|
151
|
+
|
|
152
|
+
# RUBY-3936. Guards the framing itself: a document containing a DBPointer
|
|
153
|
+
# must read back with exactly the keys the application wrote, with no
|
|
154
|
+
# extra elements recovered from a mis-sized id.
|
|
155
|
+
it "reads back only the fields that were written" do
|
|
156
|
+
doc = BSON::Document.new('a' => object, 'z' => 1)
|
|
157
|
+
round = BSON::Document.from_bson(BSON::ByteBuffer.new(doc.to_bson.to_s))
|
|
158
|
+
expect(round.keys).to eq([ 'a', 'z' ])
|
|
159
|
+
expect(round['a']).to eq(object)
|
|
160
|
+
expect(round['z']).to eq(1)
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
end
|
|
@@ -51,7 +51,7 @@ describe BSON::Decimal128 do
|
|
|
51
51
|
end
|
|
52
52
|
|
|
53
53
|
let(:expected_bson) do
|
|
54
|
-
[expected_low_bits].pack(
|
|
54
|
+
[expected_low_bits].pack(PackDirectives::INT64) + [expected_high_bits].pack(PackDirectives::INT64)
|
|
55
55
|
end
|
|
56
56
|
|
|
57
57
|
it 'sets the correct high order bits' do
|
|
@@ -343,4 +343,118 @@ describe "BSON::ExtJSON.parse" do
|
|
|
343
343
|
end
|
|
344
344
|
end
|
|
345
345
|
end
|
|
346
|
+
|
|
347
|
+
context 'when input is a DBPointer' do
|
|
348
|
+
|
|
349
|
+
context 'when $id is an ObjectId' do
|
|
350
|
+
let(:input) do
|
|
351
|
+
{ '$dbPointer' => { '$ref' => 'coll', '$id' => { '$oid' => '56e1fc72e0c917e9c4714161' } } }
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
it 'returns a BSON::DbPointer instance' do
|
|
355
|
+
expect(parsed).to be_a(BSON::DbPointer)
|
|
356
|
+
expect(parsed.ref).to eq('coll')
|
|
357
|
+
expect(parsed.id).to eq(BSON::ObjectId.from_string('56e1fc72e0c917e9c4714161'))
|
|
358
|
+
end
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
context 'when $id is a binary' do
|
|
362
|
+
let(:input) do
|
|
363
|
+
{ '$dbPointer' => { '$ref' => 'coll',
|
|
364
|
+
'$id' => { '$binary' => { 'base64' => '//8=', 'subType' => '00' } } } }
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
it 'raises an exception' do
|
|
368
|
+
expect do
|
|
369
|
+
parsed
|
|
370
|
+
end.to raise_error(BSON::Error::ExtJSONParseError, /\$id must be an ObjectId/)
|
|
371
|
+
end
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
context 'when $id is a plain object' do
|
|
375
|
+
let(:input) do
|
|
376
|
+
{ '$dbPointer' => { '$ref' => 'coll', '$id' => { 'a' => 1 } } }
|
|
377
|
+
end
|
|
378
|
+
|
|
379
|
+
it 'raises an exception' do
|
|
380
|
+
expect do
|
|
381
|
+
parsed
|
|
382
|
+
end.to raise_error(BSON::Error::ExtJSONParseError, /\$id must be an ObjectId/)
|
|
383
|
+
end
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
context 'when $id is an integer' do
|
|
387
|
+
let(:input) do
|
|
388
|
+
{ '$dbPointer' => { '$ref' => 'coll', '$id' => { '$numberInt' => '1' } } }
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
it 'raises an exception' do
|
|
392
|
+
expect do
|
|
393
|
+
parsed
|
|
394
|
+
end.to raise_error(BSON::Error::ExtJSONParseError, /\$id must be an ObjectId/)
|
|
395
|
+
end
|
|
396
|
+
end
|
|
397
|
+
|
|
398
|
+
context 'when $id is a hex string rather than an $oid wrapper' do
|
|
399
|
+
let(:input) do
|
|
400
|
+
{ '$dbPointer' => { '$ref' => 'coll', '$id' => '56e1fc72e0c917e9c4714161' } }
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
it 'raises a parse error rather than a NoMethodError' do
|
|
404
|
+
expect do
|
|
405
|
+
parsed
|
|
406
|
+
end.to raise_error(BSON::Error::ExtJSONParseError, /\$id must be an ObjectId/)
|
|
407
|
+
end
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
context 'when $ref is null' do
|
|
411
|
+
let(:input) do
|
|
412
|
+
{ '$dbPointer' => { '$ref' => nil, '$id' => { '$oid' => '56e1fc72e0c917e9c4714161' } } }
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
it 'raises a parse error rather than a NoMethodError' do
|
|
416
|
+
expect do
|
|
417
|
+
parsed
|
|
418
|
+
end.to raise_error(BSON::Error::ExtJSONParseError, /\$ref must be a string/)
|
|
419
|
+
end
|
|
420
|
+
end
|
|
421
|
+
|
|
422
|
+
context 'when $ref is not a string' do
|
|
423
|
+
let(:input) do
|
|
424
|
+
{ '$dbPointer' => { '$ref' => 5, '$id' => { '$oid' => '56e1fc72e0c917e9c4714161' } } }
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
it 'raises a parse error rather than a NoMethodError' do
|
|
428
|
+
expect do
|
|
429
|
+
parsed
|
|
430
|
+
end.to raise_error(BSON::Error::ExtJSONParseError, /\$ref must be a string/)
|
|
431
|
+
end
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
# RUBY-3936. The DBPointer wire format is a string followed by exactly 12
|
|
435
|
+
# raw bytes. A $binary id makes DbPointer#to_bson emit an int32 length, a
|
|
436
|
+
# subtype byte and a payload of any length instead. A conforming reader
|
|
437
|
+
# consumes 12 of those bytes and reads whatever follows as further elements
|
|
438
|
+
# of the enclosing document, so the value escapes its own slot: the payload
|
|
439
|
+
# below is 7 bytes of filler followed by a forged int32 element named
|
|
440
|
+
# "injected". Before this was validated, the document serialized from this
|
|
441
|
+
# input read back as {"a" => ..., "injected" => 42}.
|
|
442
|
+
context 'when $id is a binary crafted to reframe the enclosing document' do
|
|
443
|
+
let(:payload) do
|
|
444
|
+
("\x00" * 7) + "\x10injected\x00" + [ 42 ].pack('l<')
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
let(:input) do
|
|
448
|
+
{ 'a' => { '$dbPointer' => { '$ref' => 'coll',
|
|
449
|
+
'$id' => { '$binary' => { 'base64' => Base64.strict_encode64(payload),
|
|
450
|
+
'subType' => '00' } } } } }
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
it 'rejects the input instead of emitting a document with forged fields' do
|
|
454
|
+
expect do
|
|
455
|
+
parsed
|
|
456
|
+
end.to raise_error(BSON::Error::ExtJSONParseError, /\$id must be an ObjectId/)
|
|
457
|
+
end
|
|
458
|
+
end
|
|
459
|
+
end
|
|
346
460
|
end
|
data/spec/bson/float_spec.rb
CHANGED
data/spec/bson/int32_spec.rb
CHANGED
|
@@ -67,7 +67,7 @@ describe BSON::Int32 do
|
|
|
67
67
|
|
|
68
68
|
let(:type) { 16.chr }
|
|
69
69
|
let(:obj) { 123 }
|
|
70
|
-
let(:bson) { [ obj ].pack(
|
|
70
|
+
let(:bson) { [ obj ].pack(PackDirectives::INT32) }
|
|
71
71
|
|
|
72
72
|
it_behaves_like "a bson element"
|
|
73
73
|
it_behaves_like "a deserializable bson element"
|
|
@@ -76,9 +76,9 @@ describe BSON::Int32 do
|
|
|
76
76
|
describe "when the integer is negative" do
|
|
77
77
|
|
|
78
78
|
let(:decoded) { -1 }
|
|
79
|
-
let(:encoded) { BSON::ByteBuffer.new([ -1 ].pack(
|
|
79
|
+
let(:encoded) { BSON::ByteBuffer.new([ -1 ].pack(PackDirectives::INT32)) }
|
|
80
80
|
let(:decoded_2) { -50 }
|
|
81
|
-
let(:encoded_2) { BSON::ByteBuffer.new([ -50 ].pack(
|
|
81
|
+
let(:encoded_2) { BSON::ByteBuffer.new([ -50 ].pack(PackDirectives::INT32)) }
|
|
82
82
|
|
|
83
83
|
it "decodes a -1 correctly" do
|
|
84
84
|
expect(BSON::Int32.from_bson(encoded)).to eq(decoded)
|
|
@@ -95,7 +95,7 @@ describe BSON::Int32 do
|
|
|
95
95
|
|
|
96
96
|
let(:type) { 16.chr }
|
|
97
97
|
let(:obj) { BSON::Int32.new(Integer::MAX_32BIT - 1) }
|
|
98
|
-
let(:bson) { [ Integer::MAX_32BIT - 1 ].pack(
|
|
98
|
+
let(:bson) { [ Integer::MAX_32BIT - 1 ].pack(PackDirectives::INT32) }
|
|
99
99
|
|
|
100
100
|
it_behaves_like "a serializable bson element"
|
|
101
101
|
end
|
data/spec/bson/int64_spec.rb
CHANGED
|
@@ -67,7 +67,7 @@ describe BSON::Int64 do
|
|
|
67
67
|
|
|
68
68
|
let(:type) { 18.chr }
|
|
69
69
|
let(:obj) { 1325376000000 }
|
|
70
|
-
let(:bson) { [ obj ].pack(
|
|
70
|
+
let(:bson) { [ obj ].pack(PackDirectives::INT64) }
|
|
71
71
|
|
|
72
72
|
it_behaves_like "a bson element"
|
|
73
73
|
it_behaves_like "a deserializable bson element"
|
|
@@ -139,7 +139,7 @@ describe BSON::Int64 do
|
|
|
139
139
|
|
|
140
140
|
let(:type) { 18.chr }
|
|
141
141
|
let(:obj) { BSON::Int64.new(Integer::MAX_64BIT - 1) }
|
|
142
|
-
let(:bson) { [ Integer::MAX_64BIT - 1 ].pack(
|
|
142
|
+
let(:bson) { [ Integer::MAX_64BIT - 1 ].pack(PackDirectives::INT64) }
|
|
143
143
|
|
|
144
144
|
it_behaves_like "a serializable bson element"
|
|
145
145
|
end
|
data/spec/bson/integer_spec.rb
CHANGED
|
@@ -23,7 +23,7 @@ describe Integer do
|
|
|
23
23
|
|
|
24
24
|
let(:type) { 16.chr }
|
|
25
25
|
let(:obj) { Integer::MAX_32BIT - 1 }
|
|
26
|
-
let(:bson) { [ obj ].pack(
|
|
26
|
+
let(:bson) { [ obj ].pack(PackDirectives::INT32) }
|
|
27
27
|
|
|
28
28
|
it_behaves_like "a serializable bson element"
|
|
29
29
|
end
|
|
@@ -32,7 +32,7 @@ describe Integer do
|
|
|
32
32
|
|
|
33
33
|
let(:type) { 18.chr }
|
|
34
34
|
let(:obj) { Integer::MAX_64BIT - 1 }
|
|
35
|
-
let(:bson) { [ obj ].pack(
|
|
35
|
+
let(:bson) { [ obj ].pack(PackDirectives::INT64) }
|
|
36
36
|
|
|
37
37
|
it_behaves_like "a serializable bson element"
|
|
38
38
|
end
|