bson 4.9.4 → 4.11.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d770bbf52ee70915f3f04b3831de6ef57c90ae131fcde462bb1925df609119e
4
- data.tar.gz: ea768461024fac1dabccae70d6a2b6a6d68c74be70eb581b5849c37d535b1fb3
3
+ metadata.gz: 6f2b7b7906a18c900a2cd45e1b884f3c84cf863234e703838ee752c6b87badcb
4
+ data.tar.gz: beb2dd92cc828d7db3084ff2095a8df28dcb6a18e89c699f2a6bd7b7d1acd376
5
5
  SHA512:
6
- metadata.gz: c85b9d81f0f849fca1f59ee23c239de332107c4758673e60ee4f160fc8c0ba4089de905bdf8024d755b005c8828043f80f6158de0e780dba02d459c17550cc34
7
- data.tar.gz: 9663f3b950a457c1143204ef24dd7aa418be6ea350c2a615fdf752b2d14482606133649d0b2d18549d5d9676f4df37f39d23a6b027b06365b1fbc7ebba3bfc8a
6
+ metadata.gz: 5e0359cf8b6632840e5ad9be71f5d350a53e0606d5fa5afa4706021f1083a9493a410bc8d9d5ecc24c6b3ba02f7ee482898db9dc190ff8620ad5dc9014441e90
7
+ data.tar.gz: beb107c6db4bd7f6ba32671d4d340f9593636fe490b69e6e60abc812c1698b4f203b800f2ea145af77a9c319b7c9f4dfa90bb22eea157d15828a09bc8fb388ae
@@ -1,2 +1,2 @@
1
- S�tDCiq<��E$��L?��)t
2
- �ɒ��[���E`�}��|$�lP�/RAg��)Q��P�ϼ�t :���XlFi��eoy/��uHM�� �j!\���ISL��B�0������c[D){n:��:Q�����Tm;�&�!�a�f�CÌd�%?ț����O�X���%��;<���.Y~�_{��'����?�,0p<���9H�����������q����L����#^�
1
+ Oi���aH�V /ZH���L�aØ��+�<�.}39X|7����yH��;Ңrs�A��y��/N�9�s��g-����6p{=��vJ1��{\3���쇮�E����� ��((�co)�~
2
+ vȿwy�jR�Ҏ~�$N���P�����Ӿhr�����fYk��J]υr5eyA���NV�ў�H���#^2���Xg����޸ *_�ߌ��hQY6D������pT�&�p�����?�
data.tar.gz.sig CHANGED
Binary file
@@ -76,6 +76,7 @@ VALUE rb_bson_byte_buffer_get_cstring(VALUE self);
76
76
  VALUE rb_bson_byte_buffer_get_decimal128_bytes(VALUE self);
77
77
  VALUE rb_bson_byte_buffer_get_double(VALUE self);
78
78
  VALUE rb_bson_byte_buffer_get_int32(VALUE self);
79
+ VALUE rb_bson_byte_buffer_get_uint32(VALUE self);
79
80
  VALUE rb_bson_byte_buffer_get_int64(VALUE self);
80
81
  VALUE rb_bson_byte_buffer_get_string(VALUE self);
81
82
  VALUE rb_bson_byte_buffer_get_hash(int argc, VALUE *argv, VALUE self);
@@ -86,6 +87,7 @@ VALUE rb_bson_byte_buffer_put_cstring(VALUE self, VALUE string);
86
87
  VALUE rb_bson_byte_buffer_put_decimal128(VALUE self, VALUE low, VALUE high);
87
88
  VALUE rb_bson_byte_buffer_put_double(VALUE self, VALUE f);
88
89
  VALUE rb_bson_byte_buffer_put_int32(VALUE self, VALUE i);
90
+ VALUE rb_bson_byte_buffer_put_uint32(VALUE self, VALUE i);
89
91
  VALUE rb_bson_byte_buffer_put_int64(VALUE self, VALUE i);
90
92
  VALUE rb_bson_byte_buffer_put_string(VALUE self, VALUE string);
91
93
  VALUE rb_bson_byte_buffer_put_symbol(VALUE self, VALUE symbol);
@@ -56,6 +56,16 @@ void Init_bson_native()
56
56
 
57
57
  rb_define_alloc_func(rb_byte_buffer_class, rb_bson_byte_buffer_allocate);
58
58
  rb_define_method(rb_byte_buffer_class, "initialize", rb_bson_byte_buffer_initialize, -1);
59
+
60
+ /*
61
+ * call-seq:
62
+ * buffer.length -> Fixnum
63
+ *
64
+ * Returns the number of bytes available to be read in the buffer.
65
+ *
66
+ * When a buffer is being written to, each added byte increases its length.
67
+ * When a buffer is being read from, each read byte decreases its length.
68
+ */
59
69
  rb_define_method(rb_byte_buffer_class, "length", rb_bson_byte_buffer_length, 0);
60
70
 
61
71
  /*
@@ -88,7 +98,7 @@ void Init_bson_native()
88
98
  * call-seq:
89
99
  * buffer.get_array(**options) -> Array
90
100
  *
91
- * Reads an array from the byte buffer..
101
+ * Reads an array from the byte buffer.
92
102
  *
93
103
  * @option options [ nil | :bson ] :mode Decoding mode to use.
94
104
  *
@@ -97,6 +107,18 @@ void Init_bson_native()
97
107
  rb_define_method(rb_byte_buffer_class, "get_array", rb_bson_byte_buffer_get_array, -1);
98
108
 
99
109
  rb_define_method(rb_byte_buffer_class, "get_int32", rb_bson_byte_buffer_get_int32, 0);
110
+
111
+ /*
112
+ * call-seq:
113
+ * buffer.get_uint32(buffer) -> Fixnum
114
+ *
115
+ * Reads an unsigned 32 bit number from the byte buffer.
116
+ *
117
+ * @return [ Fixnum ] The unsigned 32 bits integer from the buffer
118
+ *
119
+ * @api private
120
+ */
121
+ rb_define_method(rb_byte_buffer_class, "get_uint32", rb_bson_byte_buffer_get_uint32, 0);
100
122
  rb_define_method(rb_byte_buffer_class, "get_int64", rb_bson_byte_buffer_get_int64, 0);
101
123
  rb_define_method(rb_byte_buffer_class, "get_string", rb_bson_byte_buffer_get_string, 0);
102
124
 
@@ -202,6 +224,21 @@ void Init_bson_native()
202
224
  */
203
225
  rb_define_method(rb_byte_buffer_class, "put_int32", rb_bson_byte_buffer_put_int32, 1);
204
226
 
227
+ /*
228
+ * call-seq:
229
+ * buffer.put_uint32(fixnum) -> ByteBuffer
230
+ *
231
+ * Writes an unsigned 32-bit integer value to the buffer.
232
+ *
233
+ * If the argument cannot be represented in 32 bits, raises RangeError.
234
+ *
235
+ * Returns the modified +self+.
236
+ *
237
+ * @api private
238
+ *
239
+ */
240
+ rb_define_method(rb_byte_buffer_class, "put_uint32", rb_bson_byte_buffer_put_uint32, 1);
241
+
205
242
  /*
206
243
  * call-seq:
207
244
  * buffer.put_int64(fixnum) -> ByteBuffer
@@ -239,7 +276,7 @@ void Init_bson_native()
239
276
 
240
277
  /*
241
278
  * call-seq:
242
- * buffer.put_hash(hash) -> ByteBuffer
279
+ * buffer.put_hash(hash, validating_keys) -> ByteBuffer
243
280
  *
244
281
  * Writes a Hash into the byte buffer.
245
282
  *
@@ -292,6 +329,10 @@ void Init_bson_native()
292
329
  *
293
330
  * Returns the contents of the buffer as a binary string.
294
331
  *
332
+ * If the buffer is used for reading, the returned contents is the data
333
+ * that was not yet read. If the buffer is used for writing, the returned
334
+ * contents is the complete data that has been written so far.
335
+ *
295
336
  * Note: this method copies the buffer's contents into a newly allocated
296
337
  * +String+ instance. It does not return a reference to the data stored in
297
338
  * the buffer itself.
@@ -21,6 +21,7 @@ static void pvt_raise_decode_error(volatile VALUE msg);
21
21
  static int32_t pvt_validate_length(byte_buffer_t *b);
22
22
  static uint8_t pvt_get_type_byte(byte_buffer_t *b);
23
23
  static VALUE pvt_get_int32(byte_buffer_t *b);
24
+ static VALUE pvt_get_uint32(byte_buffer_t *b);
24
25
  static VALUE pvt_get_int64(byte_buffer_t *b, int argc, VALUE *argv);
25
26
  static VALUE pvt_get_double(byte_buffer_t *b);
26
27
  static VALUE pvt_get_string(byte_buffer_t *b, const char *data_type);
@@ -41,12 +42,12 @@ void pvt_raise_decode_error(volatile VALUE msg) {
41
42
  int32_t pvt_validate_length(byte_buffer_t *b)
42
43
  {
43
44
  int32_t length;
44
-
45
+
45
46
  ENSURE_BSON_READ(b, 4);
46
47
  memcpy(&length, READ_PTR(b), 4);
47
48
  length = BSON_UINT32_TO_LE(length);
48
49
 
49
- /* minimum valid length is 4 (byte count) + 1 (terminating byte) */
50
+ /* minimum valid length is 4 (byte count) + 1 (terminating byte) */
50
51
  if(length >= 5){
51
52
  ENSURE_BSON_READ(b, length);
52
53
 
@@ -59,7 +60,7 @@ int32_t pvt_validate_length(byte_buffer_t *b)
59
60
  else{
60
61
  rb_raise(rb_eRangeError, "Buffer contained invalid length %d at %zu", length, b->read_position);
61
62
  }
62
-
63
+
63
64
  return length;
64
65
  }
65
66
 
@@ -175,13 +176,13 @@ VALUE pvt_get_string(byte_buffer_t *b, const char *data_type)
175
176
  }
176
177
  ENSURE_BSON_READ(b, 4 + length);
177
178
  str_ptr = READ_PTR(b) + 4;
178
- last_byte = *(READ_PTR(b) + 4 + length_le - 1);
179
+ last_byte = *(READ_PTR(b) + 4 + length - 1);
179
180
  if (last_byte != 0) {
180
181
  pvt_raise_decode_error(rb_sprintf("Last byte of the string is not null: 0x%x", (int) last_byte));
181
182
  }
182
183
  rb_bson_utf8_validate(str_ptr, length - 1, true, data_type);
183
184
  string = rb_enc_str_new(str_ptr, length - 1, rb_utf8_encoding());
184
- b->read_position += 4 + length_le;
185
+ b->read_position += 4 + length;
185
186
  return string;
186
187
  }
187
188
 
@@ -205,7 +206,7 @@ VALUE pvt_get_symbol(byte_buffer_t *b, VALUE rb_buffer, int argc, VALUE *argv)
205
206
  klass = rb_funcall(rb_bson_registry, rb_intern("get"), 1, INT2FIX(BSON_TYPE_SYMBOL));
206
207
  value = rb_funcall(klass, rb_intern("from_bson"), 1, rb_buffer);
207
208
  }
208
-
209
+
209
210
  RB_GC_GUARD(klass);
210
211
  return value;
211
212
  }
@@ -259,6 +260,28 @@ VALUE pvt_get_int32(byte_buffer_t *b)
259
260
  return INT2NUM(BSON_UINT32_FROM_LE(i32));
260
261
  }
261
262
 
263
+ /**
264
+ * Get an unsigned int32 from the buffer.
265
+ */
266
+ VALUE rb_bson_byte_buffer_get_uint32(VALUE self)
267
+ {
268
+ byte_buffer_t *b;
269
+
270
+ TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
271
+ return pvt_get_uint32(b);
272
+ }
273
+
274
+ VALUE pvt_get_uint32(byte_buffer_t *b)
275
+ {
276
+ uint32_t i32;
277
+
278
+ ENSURE_BSON_READ(b, 4);
279
+ memcpy(&i32, READ_PTR(b), 4);
280
+ b->read_position += 4;
281
+ return UINT2NUM(BSON_UINT32_FROM_LE(i32));
282
+ }
283
+
284
+
262
285
  /**
263
286
  * Get a int64 from the buffer.
264
287
  */
@@ -284,7 +307,7 @@ VALUE pvt_get_int64(byte_buffer_t *b, int argc, VALUE *argv)
284
307
  memcpy(&i64, READ_PTR(b), 8);
285
308
  b->read_position += 8;
286
309
  num = LL2NUM(BSON_UINT64_FROM_LE(i64));
287
-
310
+
288
311
  if (pvt_get_mode_option(argc, argv) == BSON_MODE_BSON) {
289
312
  VALUE klass = rb_funcall(rb_bson_registry,rb_intern("get"),1, INT2FIX(BSON_TYPE_INT64));
290
313
  VALUE value = rb_funcall(klass, rb_intern("new"), 1, num);
@@ -293,7 +316,7 @@ VALUE pvt_get_int64(byte_buffer_t *b, int argc, VALUE *argv)
293
316
  } else {
294
317
  return num;
295
318
  }
296
-
319
+
297
320
  RB_GC_GUARD(num);
298
321
  }
299
322
 
@@ -353,11 +376,11 @@ VALUE rb_bson_byte_buffer_get_hash(int argc, VALUE *argv, VALUE self){
353
376
  rb_hash_aset(doc, field, pvt_read_field(b, self, type, argc, argv));
354
377
  RB_GC_GUARD(field);
355
378
  }
356
-
379
+
357
380
  if (READ_PTR(b) - start_ptr != length) {
358
381
  pvt_raise_decode_error(rb_sprintf("Expected to read %d bytes for the hash but read %ld bytes", length, READ_PTR(b) - start_ptr));
359
382
  }
360
-
383
+
361
384
  return doc;
362
385
  }
363
386
 
@@ -379,10 +402,10 @@ VALUE rb_bson_byte_buffer_get_array(int argc, VALUE *argv, VALUE self){
379
402
  rb_ary_push(array, pvt_read_field(b, self, type, argc, argv));
380
403
  }
381
404
  RB_GC_GUARD(array);
382
-
405
+
383
406
  if (READ_PTR(b) - start_ptr != length) {
384
407
  pvt_raise_decode_error(rb_sprintf("Expected to read %d bytes for the hash but read %ld bytes", length, READ_PTR(b) - start_ptr));
385
408
  }
386
-
409
+
387
410
  return array;
388
411
  }
@@ -27,6 +27,7 @@ static void pvt_replace_int32(byte_buffer_t *b, int32_t position, int32_t newval
27
27
  static void pvt_put_field(byte_buffer_t *b, VALUE rb_buffer, VALUE val, VALUE validating_keys);
28
28
  static void pvt_put_byte(byte_buffer_t *b, const char byte);
29
29
  static void pvt_put_int32(byte_buffer_t *b, const int32_t i32);
30
+ static void pvt_put_uint32(byte_buffer_t *b, const uint32_t i32);
30
31
  static void pvt_put_int64(byte_buffer_t *b, const int64_t i);
31
32
  static void pvt_put_double(byte_buffer_t *b, double f);
32
33
  static void pvt_put_cstring(byte_buffer_t *b, const char *str, int32_t length, const char *data_type);
@@ -390,6 +391,37 @@ void pvt_put_int32(byte_buffer_t *b, const int32_t i)
390
391
  b->write_position += 4;
391
392
  }
392
393
 
394
+ /* The docstring is in init.c. */
395
+ VALUE rb_bson_byte_buffer_put_uint32(VALUE self, VALUE i)
396
+ {
397
+ byte_buffer_t *b;
398
+ int64_t temp;
399
+ uint32_t i32;
400
+
401
+ if (RB_TYPE_P(i, T_FLOAT)) {
402
+ rb_raise(rb_eArgError, "put_uint32: incorrect type: float, expected: integer");
403
+ }
404
+
405
+ temp = NUM2LL(i);
406
+ if (temp < 0 || temp > UINT32_MAX) {
407
+ rb_raise(rb_eRangeError, "Number %lld is out of range [0, 2^32)", (long long)temp);
408
+ }
409
+
410
+ i32 = NUM2UINT(i);
411
+
412
+ TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
413
+ pvt_put_uint32(b, i32);
414
+ return self;
415
+ }
416
+
417
+ void pvt_put_uint32(byte_buffer_t *b, const uint32_t i)
418
+ {
419
+ const uint32_t i32 = BSON_UINT32_TO_LE(i);
420
+ ENSURE_BSON_WRITE(b, 4);
421
+ memcpy(WRITE_PTR(b), &i32, 4);
422
+ b->write_position += 4;
423
+ }
424
+
393
425
  /* The docstring is in init.c. */
394
426
  VALUE rb_bson_byte_buffer_put_int64(VALUE self, VALUE i)
395
427
  {
@@ -35,7 +35,7 @@ module BSON
35
35
  #
36
36
  # @since 2.1.0
37
37
  def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
38
- to_time.to_bson(buffer)
38
+ gregorian.to_time.to_bson(buffer)
39
39
  end
40
40
  end
41
41
 
@@ -124,8 +124,8 @@ module BSON
124
124
  #
125
125
  # @since 2.0.0
126
126
  def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
127
- buffer.put_int32(increment)
128
- buffer.put_int32(seconds)
127
+ buffer.put_uint32(increment)
128
+ buffer.put_uint32(seconds)
129
129
  end
130
130
 
131
131
  # Deserialize timestamp from BSON.
@@ -140,8 +140,8 @@ module BSON
140
140
  #
141
141
  # @since 2.0.0
142
142
  def self.from_bson(buffer, **options)
143
- increment = buffer.get_int32
144
- seconds = buffer.get_int32
143
+ increment = buffer.get_uint32
144
+ seconds = buffer.get_uint32
145
145
  new(seconds, increment)
146
146
  end
147
147
 
@@ -13,5 +13,5 @@
13
13
  # limitations under the License.
14
14
 
15
15
  module BSON
16
- VERSION = "4.9.4".freeze
16
+ VERSION = "4.11.1".freeze
17
17
  end
@@ -66,7 +66,7 @@ describe BSON::ByteBuffer do
66
66
  describe '#get_double' do
67
67
 
68
68
  let(:buffer) do
69
- described_class.new("#{12.5.to_bson.to_s}")
69
+ described_class.new(12.5.to_bson.to_s)
70
70
  end
71
71
 
72
72
  let!(:double) do
@@ -85,7 +85,7 @@ describe BSON::ByteBuffer do
85
85
  describe '#get_int32' do
86
86
 
87
87
  let(:buffer) do
88
- described_class.new("#{12.to_bson.to_s}")
88
+ described_class.new(12.to_bson.to_s)
89
89
  end
90
90
 
91
91
  let!(:int32) do
@@ -101,10 +101,66 @@ describe BSON::ByteBuffer do
101
101
  end
102
102
  end
103
103
 
104
+ describe '#get_uint32' do
105
+ context 'when using 2^32-1' do
106
+ let(:buffer) do
107
+ described_class.new(4294967295.to_bson.to_s)
108
+ end
109
+
110
+ let!(:int32) do
111
+ buffer.get_uint32
112
+ end
113
+
114
+ it 'gets the uint32 from the buffer' do
115
+ expect(int32).to eq(4294967295)
116
+ end
117
+
118
+ it 'increments the position by 4' do
119
+ expect(buffer.read_position).to eq(4)
120
+ end
121
+ end
122
+
123
+ context 'when using 2^32-2' do
124
+ let(:buffer) do
125
+ described_class.new(4294967294.to_bson.to_s)
126
+ end
127
+
128
+ let!(:int32) do
129
+ buffer.get_uint32
130
+ end
131
+
132
+ it 'gets the uint32 from the buffer' do
133
+ expect(int32).to eq(4294967294)
134
+ end
135
+
136
+ it 'increments the position by 4' do
137
+ expect(buffer.read_position).to eq(4)
138
+ end
139
+ end
140
+
141
+ context 'when using 0' do
142
+ let(:buffer) do
143
+ described_class.new(0.to_bson.to_s)
144
+ end
145
+
146
+ let!(:int32) do
147
+ buffer.get_uint32
148
+ end
149
+
150
+ it 'gets the uint32 from the buffer' do
151
+ expect(int32).to eq(0)
152
+ end
153
+
154
+ it 'increments the position by 4' do
155
+ expect(buffer.read_position).to eq(4)
156
+ end
157
+ end
158
+ end
159
+
104
160
  describe '#get_int64' do
105
161
 
106
162
  let(:buffer) do
107
- described_class.new("#{(Integer::MAX_64BIT - 1).to_bson.to_s}")
163
+ described_class.new((Integer::MAX_64BIT - 1).to_bson.to_s)
108
164
  end
109
165
 
110
166
  let!(:int64) do
@@ -31,12 +31,14 @@ describe BSON::ByteBuffer do
31
31
  described_class.new
32
32
  end
33
33
 
34
- before do
35
- buffer.put_int32(5)
36
- end
37
-
38
- it 'returns the length of the buffer' do
39
- expect(buffer.length).to eq(4)
34
+ context '#put_int32' do
35
+ before do
36
+ buffer.put_int32(5)
37
+ end
38
+
39
+ it 'returns the length of the buffer' do
40
+ expect(buffer.length).to eq(4)
41
+ end
40
42
  end
41
43
  end
42
44
 
@@ -50,6 +52,50 @@ describe BSON::ByteBuffer do
50
52
  expect(buffer.length).to eq(2)
51
53
  end
52
54
  end
55
+
56
+ context 'after the byte buffer was read from' do
57
+
58
+ let(:buffer) do
59
+ described_class.new({}.to_bson.to_s)
60
+ end
61
+
62
+ it 'returns the number of bytes remaining in the buffer' do
63
+ expect(buffer.length).to eq(5)
64
+ buffer.get_int32
65
+ expect(buffer.length).to eq(1)
66
+ end
67
+ end
68
+
69
+ context 'after the byte buffer was converted to string' do
70
+
71
+ shared_examples 'returns the total buffer length' do
72
+ it 'returns the total buffer length' do
73
+ expect(buffer.length).to eq(5)
74
+ buffer.to_s.length.should == 5
75
+ expect(buffer.length).to eq(5)
76
+ end
77
+ end
78
+
79
+ context 'read buffer' do
80
+
81
+ let(:buffer) do
82
+ described_class.new({}.to_bson.to_s)
83
+ end
84
+
85
+ include_examples 'returns the total buffer length'
86
+ end
87
+
88
+ context 'write buffer' do
89
+
90
+ let(:buffer) do
91
+ described_class.new.tap do |buffer|
92
+ buffer.put_bytes('hello')
93
+ end
94
+ end
95
+
96
+ include_examples 'returns the total buffer length'
97
+ end
98
+ end
53
99
  end
54
100
 
55
101
  describe '#rewind!' do
@@ -104,4 +150,81 @@ describe BSON::ByteBuffer do
104
150
  expect(buffer.write_position).to eq(1)
105
151
  end
106
152
  end
153
+
154
+ describe 'write followed by read' do
155
+ let(:buffer) do
156
+ described_class.new
157
+ end
158
+
159
+ context 'one cycle' do
160
+ it 'returns the written data' do
161
+ buffer.put_cstring('hello')
162
+ buffer.get_cstring.should == 'hello'
163
+ end
164
+ end
165
+
166
+ context 'two cycles' do
167
+ it 'returns the written data' do
168
+ buffer.put_cstring('hello')
169
+ buffer.get_cstring.should == 'hello'
170
+
171
+ buffer.put_cstring('world')
172
+ buffer.get_cstring.should == 'world'
173
+ end
174
+ end
175
+
176
+ context 'mixed cycles' do
177
+ it 'returns the written data' do
178
+ if BSON::Environment.jruby?
179
+ pending 'RUBY-2334'
180
+ end
181
+
182
+ buffer.put_int32(1)
183
+ buffer.put_int32(2)
184
+
185
+ buffer.get_int32.should == 1
186
+
187
+ buffer.put_int32(3)
188
+
189
+ buffer.get_int32.should == 2
190
+ buffer.get_int32.should == 3
191
+ end
192
+ end
193
+ end
194
+
195
+ describe '#to_s' do
196
+ context 'read buffer' do
197
+ let(:buffer) do
198
+ described_class.new("\x18\x00\x00\x00*\x00\x00\x00")
199
+ end
200
+
201
+ it 'returns the data' do
202
+ buffer.to_s.should == "\x18\x00\x00\x00*\x00\x00\x00"
203
+ end
204
+
205
+ it 'returns the remaining buffer contents after a read' do
206
+ buffer.to_s.should == "\x18\x00\x00\x00*\x00\x00\x00"
207
+ buffer.get_int32.should == 24
208
+ buffer.to_s.should == "*\x00\x00\x00"
209
+ end
210
+ end
211
+
212
+ context 'write buffer' do
213
+ let(:buffer) do
214
+ described_class.new.tap do |buffer|
215
+ buffer.put_int32(24)
216
+ end
217
+ end
218
+
219
+ it 'returns the data' do
220
+ buffer.to_s.should == "\x18\x00\x00\x00".force_encoding('binary')
221
+ end
222
+
223
+ it 'returns the complete buffer contents after a write' do
224
+ buffer.to_s.should == "\x18\x00\x00\x00".force_encoding('binary')
225
+ buffer.put_int32(42)
226
+ buffer.to_s.should == "\x18\x00\x00\x00*\x00\x00\x00".force_encoding('binary')
227
+ end
228
+ end
229
+ end
107
230
  end
@@ -585,6 +585,102 @@ describe BSON::ByteBuffer do
585
585
  end
586
586
  end
587
587
 
588
+ describe '#put_uint32' do
589
+ context 'when argument is a float' do
590
+ it 'raises an Argument Error' do
591
+ expect{ buffer.put_uint32(4.934) }.to raise_error(ArgumentError, "put_uint32: incorrect type: float, expected: integer")
592
+ end
593
+ end
594
+
595
+ context 'when number is in range' do
596
+ let(:modified) do
597
+ buffer.put_uint32(5)
598
+ end
599
+
600
+ it 'returns gets the correct number from the buffer' do
601
+ expect(modified.get_uint32).to eq(5)
602
+ end
603
+
604
+ it 'returns the length of the buffer' do
605
+ expect(modified.length).to eq(4)
606
+ end
607
+ end
608
+
609
+ context 'when number is 0' do
610
+ let(:modified) do
611
+ buffer.put_uint32(0)
612
+ end
613
+
614
+ it 'returns gets the correct number from the buffer' do
615
+ expect(modified.get_uint32).to eq(0)
616
+ end
617
+
618
+ it 'returns the length of the buffer' do
619
+ expect(modified.length).to eq(4)
620
+ end
621
+ end
622
+
623
+ context 'when number doesn\'t fit in signed int32' do
624
+ let(:modified) do
625
+ buffer.put_uint32(4294967295)
626
+ end
627
+
628
+ let(:expected) do
629
+ [ 4294967295 ].pack(BSON::Int32::PACK)
630
+ end
631
+
632
+ it 'appends the int32 to the byte buffer' do
633
+ expect(modified.to_s).to eq(expected)
634
+ end
635
+
636
+ it 'get returns correct number' do
637
+ expect(modified.get_uint32).to eq(4294967295)
638
+ end
639
+
640
+ it 'returns the length of the buffer' do
641
+ expect(modified.length).to eq(4)
642
+ end
643
+ end
644
+
645
+ context 'when number is 2^31' do
646
+ let(:modified) do
647
+ buffer.put_uint32(2147483648)
648
+ end
649
+
650
+ it 'returns gets the correct number from the buffer' do
651
+ expect(modified.get_uint32).to eq(2147483648)
652
+ end
653
+
654
+ it 'returns the length of the buffer' do
655
+ expect(modified.length).to eq(4)
656
+ end
657
+ end
658
+
659
+ context 'when number is 2^31-1' do
660
+ let(:modified) do
661
+ buffer.put_uint32(2147483647)
662
+ end
663
+
664
+ it 'returns gets the correct number from the buffer' do
665
+ expect(modified.get_uint32).to eq(2147483647)
666
+ end
667
+
668
+ it 'returns the length of the buffer' do
669
+ expect(modified.length).to eq(4)
670
+ end
671
+ end
672
+
673
+ context 'when number is not in range' do
674
+ it 'raises error on out of top range' do
675
+ expect{ buffer.put_uint32(4294967296) }.to raise_error(RangeError, "Number 4294967296 is out of range [0, 2^32)")
676
+ end
677
+
678
+ it 'raises error on out of bottom range' do
679
+ expect{ buffer.put_uint32(-1) }.to raise_error(RangeError, "Number -1 is out of range [0, 2^32)")
680
+ end
681
+ end
682
+ end
683
+
588
684
  describe '#put_int64' do
589
685
 
590
686
  context 'when the integer is 64 bit' do
@@ -35,5 +35,58 @@ describe DateTime do
35
35
 
36
36
  it_behaves_like "a serializable bson element"
37
37
  end
38
+
39
+ context "when the dates don't both use Gregorian" do
40
+
41
+ let(:shakespeare_datetime) do
42
+ DateTime.iso8601('1616-04-23', Date::ENGLAND)
43
+ end
44
+
45
+ let(:gregorian_datetime) do
46
+ DateTime.iso8601('1616-04-23', Date::GREGORIAN)
47
+ end
48
+
49
+ context "when putting to bson" do
50
+
51
+ let(:shakespeare) do
52
+ { a: shakespeare_datetime }.to_bson
53
+ end
54
+
55
+ let(:gregorian) do
56
+ { a: gregorian_datetime }.to_bson
57
+ end
58
+
59
+ it "does not equal each other" do
60
+ expect(shakespeare.to_s).to_not eq(gregorian.to_s)
61
+ end
62
+
63
+ it "the english date is 10 days later" do
64
+ expect(shakespeare.to_s).to eq({ a: DateTime.iso8601('1616-05-03', Date::GREGORIAN) }.to_bson.to_s)
65
+ end
66
+ end
67
+
68
+ context "when putting and receiving from bson" do
69
+
70
+ let(:shakespeare) do
71
+ Hash.from_bson(BSON::ByteBuffer.new({ a: shakespeare_datetime }.to_bson.to_s))
72
+ end
73
+
74
+ let(:gregorian) do
75
+ Hash.from_bson(BSON::ByteBuffer.new({ a: gregorian_datetime }.to_bson.to_s))
76
+ end
77
+
78
+ it "does not equal each other" do
79
+ expect(shakespeare).to_not eq(gregorian)
80
+ end
81
+
82
+ it "the english date is 10 days later" do
83
+ expect(shakespeare[:a]).to eq(DateTime.iso8601('1616-05-03', Date::GREGORIAN).to_time)
84
+ end
85
+
86
+ it "the gregorian date is the same" do
87
+ expect(gregorian[:a]).to eq(DateTime.iso8601('1616-04-23', Date::GREGORIAN).to_time)
88
+ end
89
+ end
90
+ end
38
91
  end
39
92
  end
@@ -244,6 +244,16 @@ describe Hash do
244
244
  /(Hash value for key 'foo'|Value) does not define its BSON serialized type:.*HashSpecUnserializableClass/)
245
245
  end
246
246
  end
247
+
248
+ context 'when reading from a byte buffer that was previously written to' do
249
+ let(:buffer) do
250
+ {foo: 42}.to_bson
251
+ end
252
+
253
+ it 'returns the original hash' do
254
+ expect(Hash.from_bson(buffer)).to eq('foo' => 42)
255
+ end
256
+ end
247
257
  end
248
258
 
249
259
  describe '#to_bson' do
@@ -310,4 +320,50 @@ describe Hash do
310
320
  end
311
321
  end
312
322
  end
323
+
324
+ describe '#from_bson' do
325
+ context 'when bson document has duplicate keys' do
326
+ let(:buf) do
327
+ buf = BSON::ByteBuffer.new
328
+ buf.put_int32(37)
329
+ buf.put_byte("\x02")
330
+ buf.put_cstring('foo')
331
+ buf.put_string('bar')
332
+ buf.put_byte("\x02")
333
+ buf.put_cstring('foo')
334
+ buf.put_string('overwrite')
335
+ buf.put_byte("\x00")
336
+
337
+ BSON::ByteBuffer.new(buf.to_s)
338
+ end
339
+
340
+ let(:doc) { Hash.from_bson(buf) }
341
+
342
+ it 'overwrites first value with second value' do
343
+ doc.should == {'foo' => 'overwrite'}
344
+ end
345
+ end
346
+
347
+ context 'when bson document has string and symbol keys of the same name' do
348
+ let(:buf) do
349
+ buf = BSON::ByteBuffer.new
350
+ buf.put_int32(31)
351
+ buf.put_byte("\x02")
352
+ buf.put_cstring('foo')
353
+ buf.put_string('bar')
354
+ buf.put_byte("\x0e")
355
+ buf.put_cstring('foo')
356
+ buf.put_string('bar')
357
+ buf.put_byte("\x00")
358
+
359
+ BSON::ByteBuffer.new(buf.to_s)
360
+ end
361
+
362
+ let(:doc) { Hash.from_bson(buf) }
363
+
364
+ it 'overwrites first value with second value' do
365
+ doc.should == {'foo' => :bar}
366
+ end
367
+ end
368
+ end
313
369
  end
@@ -13,6 +13,16 @@
13
13
  "canonical_bson": "100000001161002A00000015CD5B0700",
14
14
  "canonical_extjson": "{\"a\" : {\"$timestamp\" : {\"t\" : 123456789, \"i\" : 42} } }",
15
15
  "degenerate_extjson": "{\"a\" : {\"$timestamp\" : {\"i\" : 42, \"t\" : 123456789} } }"
16
+ },
17
+ {
18
+ "description": "Timestamp with high-order bit set on both seconds and increment",
19
+ "canonical_bson": "10000000116100FFFFFFFFFFFFFFFF00",
20
+ "canonical_extjson": "{\"a\" : {\"$timestamp\" : {\"t\" : 4294967295, \"i\" : 4294967295} } }"
21
+ },
22
+ {
23
+ "description": "Timestamp with high-order bit set on both seconds and increment (not UINT32_MAX)",
24
+ "canonical_bson": "1000000011610000286BEE00286BEE00",
25
+ "canonical_extjson": "{\"a\" : {\"$timestamp\" : {\"t\" : 4000000000, \"i\" : 4000000000} } }"
16
26
  }
17
27
  ],
18
28
  "decodeErrors": [
@@ -65,11 +65,11 @@
65
65
  "parseErrors": [
66
66
  {
67
67
  "description" : "Bad $regularExpression (extra field)",
68
- "string" : "{\"a\" : \"$regularExpression\": {\"pattern\": \"abc\", \"options\": \"\", \"unrelated\": true}}}"
68
+ "string" : "{\"a\" : {\"$regularExpression\": {\"pattern\": \"abc\", \"options\": \"\", \"unrelated\": true}}}"
69
69
  },
70
70
  {
71
71
  "description" : "Bad $regularExpression (missing options field)",
72
- "string" : "{\"a\" : \"$regularExpression\": {\"pattern\": \"abc\"}}}"
72
+ "string" : "{\"a\" : {\"$regularExpression\": {\"pattern\": \"abc\"}}}"
73
73
  },
74
74
  {
75
75
  "description": "Bad $regularExpression (pattern is number, not string)",
@@ -81,7 +81,7 @@
81
81
  },
82
82
  {
83
83
  "description" : "Bad $regularExpression (missing pattern field)",
84
- "string" : "{\"a\" : \"$regularExpression\": {\"options\":\"ix\"}}}"
84
+ "string" : "{\"a\" : {\"$regularExpression\": {\"options\":\"ix\"}}}"
85
85
  },
86
86
  {
87
87
  "description": "Bad $oid (number, not string)",
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.9.4
4
+ version: 4.11.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Brock
@@ -33,7 +33,7 @@ cert_chain:
33
33
  gpvfPNWMwyBDlHaNS3GfO6cRRxBOvEG05GUCsvtTY4Bpe8yjE64wg1ymb47LMOnv
34
34
  Qb1lGORmf/opg45mluKUYl7pQNZHD0d3
35
35
  -----END CERTIFICATE-----
36
- date: 2020-06-29 00:00:00.000000000 Z
36
+ date: 2020-11-04 00:00:00.000000000 Z
37
37
  dependencies: []
38
38
  description: A fully featured BSON specification implementation in Ruby
39
39
  email:
metadata.gz.sig CHANGED
Binary file