bson 4.5.0 → 4.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,11 +2,14 @@ require 'spec_helper'
2
2
 
3
3
  describe 'Driver BSON Corpus spec tests' do
4
4
 
5
- specs = BSON_CORPUS_TESTS.map { |file| BSON::Corpus::Spec.new(file) }
5
+ BSON_CORPUS_TESTS.each do |path|
6
+ basename = File.basename(path)
7
+ # All of the tests in the failures subdir are failing apparently
8
+ #basename = path.sub(/.*corpus-tests\//, '')
6
9
 
7
- specs.each do |spec|
10
+ spec = BSON::Corpus::Spec.new(path)
8
11
 
9
- context(spec.description) do
12
+ context("(#{basename}): #{spec.description}") do
10
13
 
11
14
  spec.valid_tests.each do |test|
12
15
 
@@ -57,9 +60,9 @@ describe 'Driver BSON Corpus spec tests' do
57
60
 
58
61
  it 'raises an error' do
59
62
  skip 'This test case does not raise and error but should' unless error
60
- expect {
63
+ expect do
61
64
  test.reencoded_bson
62
- }.to raise_error
65
+ end.to raise_error(error.class)
63
66
  end
64
67
  end
65
68
  end
@@ -163,6 +163,26 @@ describe BSON::Document do
163
163
  it "sets the value" do
164
164
  expect(doc[key]).to eq(val)
165
165
  end
166
+
167
+ context 'when value is a hash' do
168
+ let(:val) do
169
+ {'foo' => {'bar' => 'baz'}}
170
+ end
171
+
172
+ it 'converts value to indifferent access' do
173
+ expect(doc[key][:foo][:bar]).to eq('baz')
174
+ end
175
+ end
176
+
177
+ context 'when value is an array with hash element' do
178
+ let(:val) do
179
+ [42, {'foo' => {'bar' => 'baz'}}]
180
+ end
181
+
182
+ it 'converts hash element to indifferent access' do
183
+ expect(doc[key][1][:foo][:bar]).to eq('baz')
184
+ end
185
+ end
166
186
  end
167
187
 
168
188
  if described_class.instance_methods.include?(:dig)
@@ -998,51 +1018,31 @@ describe BSON::Document do
998
1018
  it_behaves_like "a document able to handle utf-8"
999
1019
  end
1000
1020
 
1001
- context "when utf-8 values exist in wrong encoding" do
1021
+ context "given a utf-8-encodable string in another encoding" do
1002
1022
 
1003
1023
  let(:string) { "gültig" }
1004
1024
  let(:document) do
1005
1025
  described_class["type", string.encode("iso-8859-1")]
1006
1026
  end
1007
1027
 
1008
- it "raises an exception", unless: BSON::Environment.jruby? do
1009
- expect {
1010
- document.to_bson
1011
- }.to raise_error(ArgumentError)
1012
- end
1013
-
1014
- it 'converts the values', if: BSON::Environment.jruby? do
1015
- expect(
1016
- BSON::Document.from_bson(BSON::ByteBuffer.new(document.to_bson.to_s))
1017
- ).to eq({ "type" => string })
1018
- end
1019
- end
1020
-
1021
- context "when binary strings with utf-8 values exist", if: BSON::Environment.jruby? && (JRUBY_VERSION < '9') do
1022
-
1023
- let(:string) { "europäisch" }
1024
- let(:document) do
1025
- described_class["type", string.encode("binary")]
1026
- end
1027
-
1028
- it "encodes and decodes the document properly" do
1028
+ it 'converts the values to utf-8' do
1029
1029
  expect(
1030
1030
  BSON::Document.from_bson(BSON::ByteBuffer.new(document.to_bson.to_s))
1031
1031
  ).to eq({ "type" => string })
1032
1032
  end
1033
1033
  end
1034
1034
 
1035
- context "when binary strings with utf-8 values exist", unless: BSON::Environment.jruby? do
1035
+ context "given a binary string with utf-8 values" do
1036
1036
 
1037
- let(:string) { "europäisch" }
1037
+ let(:string) { "europäisch".force_encoding('binary') }
1038
1038
  let(:document) do
1039
- described_class["type", string.encode("binary", "binary")]
1039
+ described_class["type", string]
1040
1040
  end
1041
1041
 
1042
- it "encodes and decodes the document properly" do
1043
- expect(
1044
- BSON::Document.from_bson(BSON::ByteBuffer.new(document.to_bson.to_s))
1045
- ).to eq({ "type" => string })
1042
+ it "raises encoding error" do
1043
+ expect do
1044
+ document.to_bson
1045
+ end.to raise_error(Encoding::UndefinedConversionError, /from ASCII-8BIT to UTF-8/)
1046
1046
  end
1047
1047
  end
1048
1048
  end
@@ -158,4 +158,69 @@ describe Hash do
158
158
  it_behaves_like "a deserializable bson element"
159
159
  end
160
160
  end
161
+
162
+ describe '#to_bson' do
163
+ context 'when a key is not valid utf-8' do
164
+ let(:key) { Utils.make_byte_string([254, 253, 255]) }
165
+ let(:hash) do
166
+ {key => 'foo'}
167
+ end
168
+
169
+ let(:expected_message) do
170
+ if BSON::Environment.jruby?
171
+ # Uses JRE conversion to another encoding
172
+ /Error serializing key.*Encoding::UndefinedConversionError/
173
+ else
174
+ # Uses our validator
175
+ /Key.*is not valid UTF-8/
176
+ end
177
+ end
178
+
179
+ it 'raises EncodingError' do
180
+ expect do
181
+ hash.to_bson
182
+ end.to raise_error(EncodingError, expected_message)
183
+ end
184
+ end
185
+
186
+ context 'when a key contains null bytes' do
187
+ let(:hash) do
188
+ {"\x00".force_encoding('BINARY') => 'foo'}
189
+ end
190
+
191
+ it 'raises ArgumentError' do
192
+ expect do
193
+ hash.to_bson
194
+ end.to raise_error(ArgumentError, /[Kk]ey.*contains null bytes/)
195
+ end
196
+ end
197
+
198
+ context 'when a value is not valid utf-8' do
199
+ let(:hash) do
200
+ {'foo' => [254, 253, 255].map(&:chr).join.force_encoding('BINARY')}
201
+ end
202
+
203
+ let(:expected_message) do
204
+ /from ASCII-8BIT to UTF-8/
205
+ end
206
+
207
+ it 'raises EncodingError' do
208
+ expect do
209
+ hash.to_bson
210
+ end.to raise_error(EncodingError, expected_message)
211
+ end
212
+ end
213
+
214
+ context 'when a value contains null bytes' do
215
+ let(:hash) do
216
+ {'foo' => "\x00".force_encoding('BINARY')}
217
+ end
218
+
219
+ it 'works' do
220
+ expect do
221
+ hash.to_bson
222
+ end.not_to raise_error
223
+ end
224
+ end
225
+ end
161
226
  end
@@ -25,7 +25,7 @@ describe BSON::Int32 do
25
25
  let(:integer) { Integer::MAX_32BIT }
26
26
 
27
27
  it "wraps the integer" do
28
- expect(obj.instance_variable_get(:@integer)).to be(integer)
28
+ expect(obj.value).to be(integer)
29
29
  end
30
30
  end
31
31
 
@@ -34,9 +34,9 @@ describe BSON::Int32 do
34
34
  let(:integer) { Integer::MAX_32BIT + 1 }
35
35
 
36
36
  it "raises an out of range error" do
37
- expect {
37
+ expect do
38
38
  obj
39
- }.to raise_error(RangeError)
39
+ end.to raise_error(RangeError, /#{integer} cannot be stored in 32 bits/)
40
40
  end
41
41
  end
42
42
 
@@ -50,6 +50,16 @@ describe BSON::Int32 do
50
50
  }.to raise_error(RangeError)
51
51
  end
52
52
  end
53
+
54
+ context 'when argument is an Int32' do
55
+ let(:integer) do
56
+ described_class.new(described_class.new(50))
57
+ end
58
+
59
+ it 'works' do
60
+ expect(integer.value).to be 50
61
+ end
62
+ end
53
63
  end
54
64
 
55
65
  describe "#from_bson" do
@@ -235,4 +245,12 @@ describe BSON::Int32 do
235
245
  end
236
246
  end
237
247
  end
248
+
249
+ describe '#value' do
250
+ let(:obj) { described_class.new(12345) }
251
+
252
+ it 'returns value passed to initializer' do
253
+ expect(obj.value).to eq(12345)
254
+ end
255
+ end
238
256
  end
@@ -25,7 +25,7 @@ describe BSON::Int64 do
25
25
  let(:integer) { Integer::MAX_64BIT - 1 }
26
26
 
27
27
  it "wraps the integer" do
28
- expect(obj.instance_variable_get(:@integer)).to be(integer)
28
+ expect(obj.value).to be(integer)
29
29
  end
30
30
  end
31
31
 
@@ -34,9 +34,9 @@ describe BSON::Int64 do
34
34
  let(:integer) { Integer::MAX_64BIT + 1 }
35
35
 
36
36
  it "raises an out of range error" do
37
- expect {
37
+ expect do
38
38
  obj
39
- }.to raise_error(RangeError)
39
+ end.to raise_error(RangeError, /#{integer} cannot be stored in 64 bits/)
40
40
  end
41
41
  end
42
42
 
@@ -50,6 +50,16 @@ describe BSON::Int64 do
50
50
  }.to raise_error(RangeError)
51
51
  end
52
52
  end
53
+
54
+ context 'when argument is an Int64' do
55
+ let(:integer) do
56
+ described_class.new(described_class.new(50))
57
+ end
58
+
59
+ it 'works' do
60
+ expect(integer.value).to be 50
61
+ end
62
+ end
53
63
  end
54
64
 
55
65
  describe "#from_bson" do
@@ -280,4 +290,13 @@ describe BSON::Int64 do
280
290
  end
281
291
  end
282
292
  end
293
+
294
+ describe '#value' do
295
+ let(:obj) { described_class.new(12345) }
296
+
297
+ it 'returns value passed to initializer' do
298
+ expect(obj.value).to eq(12345)
299
+ end
300
+ end
301
+
283
302
  end
@@ -130,4 +130,22 @@ describe String do
130
130
  end
131
131
  end
132
132
  end
133
+
134
+ describe '#to_bson' do
135
+ context 'when string is not valid utf-8' do
136
+ let(:string) do
137
+ "\xfe\x00\xff".force_encoding('BINARY')
138
+ end
139
+
140
+ let(:expected_message) do
141
+ /from ASCII-8BIT to UTF-8/
142
+ end
143
+
144
+ it 'raises EncodingError' do
145
+ expect do
146
+ string.to_bson
147
+ end.to raise_error(EncodingError, expected_message)
148
+ end
149
+ end
150
+ end
133
151
  end
@@ -14,16 +14,22 @@
14
14
  "extjson": "{\"a\" : [10]}"
15
15
  },
16
16
  {
17
- "description": "Single Element Array with index set incorrectly",
17
+ "description": "Single Element Array with index set incorrectly to empty string",
18
18
  "bson": "130000000461000B00000010000A0000000000",
19
19
  "canonical_bson": "140000000461000C0000001030000A0000000000",
20
20
  "extjson": "{\"a\" : [10]}"
21
21
  },
22
22
  {
23
- "description": "Single Element Array with index set incorrectly",
23
+ "description": "Single Element Array with index set incorrectly to ab",
24
24
  "bson": "150000000461000D000000106162000A0000000000",
25
25
  "canonical_bson": "140000000461000C0000001030000A0000000000",
26
26
  "extjson": "{\"a\" : [10]}"
27
+ },
28
+ {
29
+ "description": "Multi Element Array with duplicate indexes",
30
+ "bson": "1b000000046100130000001030000a000000103000140000000000",
31
+ "canonical_bson": "1b000000046100130000001030000a000000103100140000000000",
32
+ "extjson": "{\"a\" : [10, 20]}"
27
33
  }
28
34
  ],
29
35
  "decodeErrors": [
@@ -18,10 +18,8 @@ shared_examples_for "a binary encoded string" do
18
18
  Encoding.find(BSON::BINARY)
19
19
  end
20
20
 
21
- unless RUBY_VERSION < "1.9"
22
- it "returns the string with binary encoding" do
23
- expect(encoded.encoding).to eq(binary_encoding)
24
- end
21
+ it "returns the string with binary encoding" do
22
+ expect(encoded.encoding).to eq(binary_encoding)
25
23
  end
26
24
  end
27
25
 
@@ -0,0 +1,10 @@
1
+ module Utils
2
+ # JRuby chokes when strings like "\xfe\x00\xff", which are not valid UTF-8,
3
+ # appear in the source. Use this method to build such strings.
4
+ # char_array is an array of byte values to use for the string.
5
+ module_function def make_byte_string(char_array, encoding = 'BINARY')
6
+ char_array.map do |char|
7
+ char.chr.force_encoding('BINARY')
8
+ end.join.force_encoding(encoding)
9
+ end
10
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bson
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.5.0
4
+ version: 4.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Brock
@@ -33,9 +33,9 @@ cert_chain:
33
33
  bMYVwXXhV8czdzgkQB/ZPWHSbEWXnmkze1mzvqWBCPOVXYrcnL9cnEl/RoxtS1hr
34
34
  Db6Ac6mCUSYfYHBWpWqxjc45n70i5Xi1
35
35
  -----END CERTIFICATE-----
36
- date: 2019-05-23 00:00:00.000000000 Z
36
+ date: 2019-10-30 00:00:00.000000000 Z
37
37
  dependencies: []
38
- description: A full featured BSON specification implementation, in Ruby
38
+ description: A fully featured BSON specification implementation in Ruby
39
39
  email:
40
40
  - mongodb-dev@googlegroups.com
41
41
  executables: []
@@ -49,9 +49,16 @@ files:
49
49
  - NOTICE
50
50
  - README.md
51
51
  - Rakefile
52
- - ext/bson/bson_native.c
52
+ - ext/bson/bson-endian.h
53
+ - ext/bson/bson-native.h
54
+ - ext/bson/bytebuf.c
55
+ - ext/bson/endian.c
53
56
  - ext/bson/extconf.rb
54
- - ext/bson/native-endian.h
57
+ - ext/bson/init.c
58
+ - ext/bson/libbson-utf8.c
59
+ - ext/bson/read.c
60
+ - ext/bson/util.c
61
+ - ext/bson/write.c
55
62
  - lib/bson.rb
56
63
  - lib/bson/active_support.rb
57
64
  - lib/bson/array.rb
@@ -93,7 +100,9 @@ files:
93
100
  - spec/bson/array_spec.rb
94
101
  - spec/bson/binary_spec.rb
95
102
  - spec/bson/boolean_spec.rb
103
+ - spec/bson/byte_buffer_read_spec.rb
96
104
  - spec/bson/byte_buffer_spec.rb
105
+ - spec/bson/byte_buffer_write_spec.rb
97
106
  - spec/bson/code_spec.rb
98
107
  - spec/bson/code_with_scope_spec.rb
99
108
  - spec/bson/config_spec.rb
@@ -160,10 +169,17 @@ files:
160
169
  - spec/support/driver-spec-tests/decimal128/decimal128-7.json
161
170
  - spec/support/shared_examples.rb
162
171
  - spec/support/spec_config.rb
163
- homepage: http://bsonspec.org
172
+ - spec/support/utils.rb
173
+ homepage: https://docs.mongodb.com/ruby-driver/current/tutorials/bson-v4/
164
174
  licenses:
165
175
  - Apache-2.0
166
- metadata: {}
176
+ metadata:
177
+ bug_tracker_uri: https://jira.mongodb.org/projects/RUBY
178
+ changelog_uri: https://github.com/mongodb/bson-ruby/releases
179
+ documentation_uri: https://docs.mongodb.com/ruby-driver/current/tutorials/bson-v4/
180
+ homepage_uri: https://docs.mongodb.com/ruby-driver/current/tutorials/bson-v4/
181
+ mailing_list_uri: https://groups.google.com/group/mongodb-user
182
+ source_code_uri: https://github.com/mongodb/bson-ruby
167
183
  post_install_message:
168
184
  rdoc_options: []
169
185
  require_paths:
@@ -172,7 +188,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
172
188
  requirements:
173
189
  - - ">="
174
190
  - !ruby/object:Gem::Version
175
- version: 1.9.3
191
+ version: '2.3'
176
192
  required_rubygems_version: !ruby/object:Gem::Requirement
177
193
  requirements:
178
194
  - - ">="
@@ -182,75 +198,78 @@ requirements: []
182
198
  rubygems_version: 3.0.1
183
199
  signing_key:
184
200
  specification_version: 4
185
- summary: Ruby Implementation of the BSON specification
201
+ summary: Ruby implementation of the BSON specification
186
202
  test_files:
187
- - spec/bson/date_spec.rb
188
- - spec/bson/config_spec.rb
189
- - spec/bson/int64_spec.rb
203
+ - spec/bson/min_key_spec.rb
190
204
  - spec/bson/undefined_spec.rb
191
- - spec/bson/corpus_spec.rb
205
+ - spec/bson/regexp_spec.rb
206
+ - spec/bson/decimal128_spec.rb
192
207
  - spec/bson/code_spec.rb
193
- - spec/bson/driver_bson_spec.rb
194
- - spec/bson/object_spec.rb
195
- - spec/bson/date_time_spec.rb
196
- - spec/bson/raw_spec.rb
197
- - spec/bson/min_key_spec.rb
198
- - spec/bson/time_with_zone_spec.rb
199
208
  - spec/bson/time_spec.rb
200
- - spec/bson/hash_spec.rb
201
- - spec/bson/timestamp_spec.rb
202
- - spec/bson/int32_spec.rb
203
- - spec/bson/byte_buffer_spec.rb
204
- - spec/bson/registry_spec.rb
205
- - spec/bson/regexp_spec.rb
206
- - spec/bson/symbol_spec.rb
209
+ - spec/bson/object_spec.rb
210
+ - spec/bson/integer_spec.rb
207
211
  - spec/bson/string_spec.rb
212
+ - spec/bson/byte_buffer_read_spec.rb
213
+ - spec/bson/config_spec.rb
214
+ - spec/bson/json_spec.rb
208
215
  - spec/bson/boolean_spec.rb
216
+ - spec/bson/corpus_spec.rb
217
+ - spec/bson/hash_spec.rb
209
218
  - spec/bson/open_struct_spec.rb
210
- - spec/bson/decimal128_spec.rb
211
- - spec/bson/json_spec.rb
219
+ - spec/bson/nil_class_spec.rb
212
220
  - spec/bson/array_spec.rb
213
221
  - spec/bson/false_class_spec.rb
214
222
  - spec/bson/true_class_spec.rb
215
- - spec/bson/binary_spec.rb
216
- - spec/bson/object_id_spec.rb
217
- - spec/bson/code_with_scope_spec.rb
218
- - spec/bson/nil_class_spec.rb
219
- - spec/bson/integer_spec.rb
220
- - spec/bson/document_spec.rb
223
+ - spec/bson/date_spec.rb
224
+ - spec/bson/byte_buffer_write_spec.rb
225
+ - spec/bson/int64_spec.rb
226
+ - spec/bson/timestamp_spec.rb
221
227
  - spec/bson/max_key_spec.rb
228
+ - spec/bson/raw_spec.rb
229
+ - spec/bson/byte_buffer_spec.rb
230
+ - spec/bson/document_spec.rb
231
+ - spec/bson/code_with_scope_spec.rb
222
232
  - spec/bson/float_spec.rb
233
+ - spec/bson/time_with_zone_spec.rb
234
+ - spec/bson/binary_spec.rb
235
+ - spec/bson/registry_spec.rb
236
+ - spec/bson/symbol_spec.rb
237
+ - spec/bson/driver_bson_spec.rb
238
+ - spec/bson/date_time_spec.rb
239
+ - spec/bson/object_id_spec.rb
240
+ - spec/bson/int32_spec.rb
241
+ - spec/support/utils.rb
242
+ - spec/support/common_driver.rb
243
+ - spec/support/corpus-tests/code_w_scope.json
244
+ - spec/support/corpus-tests/code.json
245
+ - spec/support/corpus-tests/oid.json
246
+ - spec/support/corpus-tests/timestamp.json
223
247
  - spec/support/corpus-tests/string.json
224
248
  - spec/support/corpus-tests/null.json
225
249
  - spec/support/corpus-tests/int32.json
226
250
  - spec/support/corpus-tests/maxkey.json
227
- - spec/support/corpus-tests/timestamp.json
251
+ - spec/support/corpus-tests/array.json
252
+ - spec/support/corpus-tests/regex.json
253
+ - spec/support/corpus-tests/minkey.json
254
+ - spec/support/corpus-tests/top.json
255
+ - spec/support/corpus-tests/failures/undefined.json
228
256
  - spec/support/corpus-tests/failures/binary.json
229
- - spec/support/corpus-tests/failures/dbpointer.json
230
257
  - spec/support/corpus-tests/failures/symbol.json
231
- - spec/support/corpus-tests/failures/undefined.json
232
- - spec/support/corpus-tests/failures/datetime.json
233
258
  - spec/support/corpus-tests/failures/int64.json
234
- - spec/support/corpus-tests/code.json
235
- - spec/support/corpus-tests/document.json
236
- - spec/support/corpus-tests/double.json
237
- - spec/support/corpus-tests/regex.json
238
- - spec/support/corpus-tests/oid.json
239
- - spec/support/corpus-tests/top.json
259
+ - spec/support/corpus-tests/failures/dbpointer.json
260
+ - spec/support/corpus-tests/failures/datetime.json
240
261
  - spec/support/corpus-tests/boolean.json
241
- - spec/support/corpus-tests/array.json
242
- - spec/support/corpus-tests/code_w_scope.json
243
- - spec/support/corpus-tests/minkey.json
244
- - spec/support/corpus.rb
262
+ - spec/support/corpus-tests/double.json
263
+ - spec/support/corpus-tests/document.json
245
264
  - spec/support/shared_examples.rb
246
- - spec/support/driver-spec-tests/decimal128/decimal128-4.json
247
- - spec/support/driver-spec-tests/decimal128/decimal128-2.json
248
- - spec/support/driver-spec-tests/decimal128/decimal128-5.json
249
- - spec/support/driver-spec-tests/decimal128/decimal128-3.json
265
+ - spec/support/spec_config.rb
250
266
  - spec/support/driver-spec-tests/decimal128/decimal128-6.json
267
+ - spec/support/driver-spec-tests/decimal128/decimal128-3.json
251
268
  - spec/support/driver-spec-tests/decimal128/decimal128-1.json
269
+ - spec/support/driver-spec-tests/decimal128/decimal128-4.json
252
270
  - spec/support/driver-spec-tests/decimal128/decimal128-7.json
253
- - spec/support/spec_config.rb
254
- - spec/support/common_driver.rb
255
- - spec/spec_helper.rb
271
+ - spec/support/driver-spec-tests/decimal128/decimal128-5.json
272
+ - spec/support/driver-spec-tests/decimal128/decimal128-2.json
273
+ - spec/support/corpus.rb
256
274
  - spec/bson_spec.rb
275
+ - spec/spec_helper.rb