gitlab-cbor 1.0.0

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.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +25 -0
  3. data/.gitlab-ci.yml +44 -0
  4. data/.travis.yml +24 -0
  5. data/CHANGELOG.md +30 -0
  6. data/ChangeLog +106 -0
  7. data/Gemfile +11 -0
  8. data/README.rdoc +191 -0
  9. data/Rakefile +97 -0
  10. data/ci/test.rb +31 -0
  11. data/doclib/cbor/buffer.rb +193 -0
  12. data/doclib/cbor/core_ext.rb +133 -0
  13. data/doclib/cbor/error.rb +14 -0
  14. data/doclib/cbor/packer.rb +133 -0
  15. data/doclib/cbor/simple.rb +15 -0
  16. data/doclib/cbor/tagged.rb +16 -0
  17. data/doclib/cbor/unpacker.rb +138 -0
  18. data/doclib/cbor.rb +80 -0
  19. data/ext/cbor/buffer.c +693 -0
  20. data/ext/cbor/buffer.h +484 -0
  21. data/ext/cbor/buffer_class.c +561 -0
  22. data/ext/cbor/buffer_class.h +41 -0
  23. data/ext/cbor/cbor.h +69 -0
  24. data/ext/cbor/compat.h +147 -0
  25. data/ext/cbor/core_ext.c +199 -0
  26. data/ext/cbor/core_ext.h +35 -0
  27. data/ext/cbor/extconf.rb +29 -0
  28. data/ext/cbor/packer.c +169 -0
  29. data/ext/cbor/packer.h +370 -0
  30. data/ext/cbor/packer_class.c +345 -0
  31. data/ext/cbor/packer_class.h +40 -0
  32. data/ext/cbor/rbinit.c +57 -0
  33. data/ext/cbor/renamer.h +56 -0
  34. data/ext/cbor/rmem.c +103 -0
  35. data/ext/cbor/rmem.h +118 -0
  36. data/ext/cbor/sysdep.h +139 -0
  37. data/ext/cbor/sysdep_endian.h +59 -0
  38. data/ext/cbor/sysdep_types.h +55 -0
  39. data/ext/cbor/unpacker.c +787 -0
  40. data/ext/cbor/unpacker.h +135 -0
  41. data/ext/cbor/unpacker_class.c +483 -0
  42. data/ext/cbor/unpacker_class.h +39 -0
  43. data/gitlab-cbor.gemspec +27 -0
  44. data/lib/cbor/version.rb +3 -0
  45. data/lib/cbor.rb +6 -0
  46. data/spec/buffer_io_spec.rb +260 -0
  47. data/spec/buffer_spec.rb +576 -0
  48. data/spec/cases.cbor +0 -0
  49. data/spec/cases.cbor_stream +0 -0
  50. data/spec/cases.json +1 -0
  51. data/spec/cases.msg +0 -0
  52. data/spec/cases_compact.msg +0 -0
  53. data/spec/cases_spec.rb +39 -0
  54. data/spec/format_spec.rb +603 -0
  55. data/spec/packer_spec.rb +127 -0
  56. data/spec/random_compat.rb +24 -0
  57. data/spec/spec_helper.rb +68 -0
  58. data/spec/unpacker_spec.rb +260 -0
  59. metadata +189 -0
@@ -0,0 +1,603 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ def bignum_to_bytes(bn)
5
+ bn.to_s(16).chars.each_slice(2).map{|a| a.join.to_i(16).chr}.join
6
+ end
7
+ # => "\x98!sHq#\x98W\x91\x82sY\x87)Hu#E"
8
+ # 0x982173487123985791827359872948752345
9
+
10
+ unless defined?(Float::INFINITY)
11
+ class Float
12
+ INFINITY = 1.0/0.0
13
+ NAN = 0.0/0.0
14
+ end
15
+ end
16
+
17
+ describe MessagePack do
18
+ it "nil" do
19
+ check 1, nil
20
+ end
21
+
22
+ it "true" do
23
+ check 1, true
24
+ end
25
+
26
+ it "false" do
27
+ check 1, false
28
+ end
29
+
30
+ it "zero" do
31
+ check 1, 0
32
+ end
33
+
34
+ it "positive fixnum" do
35
+ check 1, 1
36
+ check 2, (1<<6)
37
+ check 2, (1<<7)-1
38
+ end
39
+
40
+ it "positive int 8" do
41
+ check 1, -1
42
+ check 2, (1<<7)
43
+ check 2, (1<<8)-1
44
+ end
45
+
46
+ it "positive int 16" do
47
+ check 3, (1<<8)
48
+ check 3, (1<<16)-1
49
+ check 3, 1024
50
+ check 4, [1024]
51
+ end
52
+
53
+ it "positive int 32" do
54
+ check 5, (1<<16)
55
+ check 5, (1<<32)-1
56
+ end
57
+
58
+ it "positive int 64" do
59
+ check 9, (1<<32)
60
+ check 9, (1<<64)-1
61
+ end
62
+
63
+ it "negative fixnum" do
64
+ check 1, -1
65
+ check 1, -24
66
+ check 2, -25
67
+ end
68
+
69
+ it "negative int 8" do
70
+ check 2, -((1<<5)+1)
71
+ check 2, -(1<<7)
72
+ end
73
+
74
+ it "negative int 16" do
75
+ check 2, -((1<<7)+1)
76
+ check 2, -256
77
+ check 3, -(1<<15)
78
+ end
79
+
80
+ it "negative int 32" do
81
+ check 3, -((1<<15)+1)
82
+ check 3, -(1<<16)
83
+ check 5, -(1<<31)
84
+ check 5, -(1<<32)
85
+ end
86
+
87
+ it "negative int 64" do
88
+ check 5, -((1<<31)+1)
89
+ check 9, -(1<<63)
90
+ check 9, -(1<<64)
91
+ end
92
+
93
+ it "half" do
94
+ check 3, 1.0
95
+ check 3, -1.0
96
+ check 3, -2.0
97
+ check 3, 65504.0
98
+ check 3, -65504.0
99
+ check 3, Math.ldexp(1, -14) # ≈ 6.10352 × 10−5 (minimum positive normal)
100
+ check 3, -Math.ldexp(1, -14) # ≈ 6.10352 × 10−5 (maximum negative normal)
101
+ check 3, Math.ldexp(1, -14) - Math.ldexp(1, -24) # ≈ 6.09756 × 10−5 (maximum subnormal)
102
+ check 3, Math.ldexp(1, -24) # ≈ 5.96046 × 10−8 (minimum positive subnormal)
103
+ check 3, -Math.ldexp(1, -24) # ≈ -5.96046 × 10−8 (maximum negative subnormal)
104
+ check 5, Math.ldexp(1, -14) - Math.ldexp(0.5, -24) # check loss of precision
105
+ check 5, Math.ldexp(1.5, -24) # loss of precision (subnormal)
106
+ check 3, Math.ldexp(1.5, -23)
107
+ check 5, Math.ldexp(1.75, -23)
108
+ check 3, Float::INFINITY
109
+ check 3, -Float::INFINITY
110
+ # check 3, Float::NAN # NAN is not equal to itself, to this never checks out...
111
+ raw = Float::NAN.to_cbor.to_s
112
+ raw.length.should == 3
113
+ MessagePack.unpack(raw).nan?.should == true
114
+ end
115
+
116
+ it "double" do
117
+ check 9, 0.1
118
+ check 9, -0.1
119
+ end
120
+
121
+ it "fixraw" do
122
+ check_raw 1, 0
123
+ check_raw 1, 23
124
+ end
125
+
126
+ it "raw 16" do
127
+ check_raw 2, (1<<5)
128
+ check_raw 3, (1<<16)-1
129
+ end
130
+
131
+ it "raw 32" do
132
+ check_raw 5, (1<<16)
133
+ #check_raw 5, (1<<32)-1 # memory error
134
+ end
135
+
136
+ it "fixarray" do
137
+ check_array 1, 0
138
+ check_array 1, (1<<4)-1
139
+ check_array 1, 23
140
+ end
141
+
142
+ it "array 16" do
143
+ check_array 1, (1<<4)
144
+ #check_array 3, (1<<16)-1
145
+ end
146
+
147
+ it "array 32" do
148
+ #check_array 5, (1<<16)
149
+ #check_array 5, (1<<32)-1 # memory error
150
+ end
151
+
152
+ it "nil" do
153
+ match nil, "\xf6".b
154
+ end
155
+
156
+ it "false" do
157
+ match false, "\xf4".b
158
+ end
159
+
160
+ it "true" do
161
+ match true, "\xf5".b
162
+ end
163
+
164
+ it "0" do
165
+ match 0, "\x00".b
166
+ end
167
+
168
+ it "127" do
169
+ match 127, "\x18\x7f".b
170
+ end
171
+
172
+ it "128" do
173
+ match 128, "\x18\x80".b
174
+ end
175
+
176
+ it "256" do
177
+ match 256, "\x19\x01\x00".b
178
+ end
179
+
180
+ it "-1" do
181
+ match -1, "\x20".b
182
+ end
183
+
184
+ it "-33" do
185
+ match -33, "\x38\x20".b
186
+ end
187
+
188
+ it "-129" do
189
+ match -129, "\x38\x80".b
190
+ end
191
+
192
+ it "-257" do
193
+ match -257, "\x39\x01\x00".b
194
+ end
195
+
196
+ it "{1=>1}" do
197
+ obj = {1=>1}
198
+ match obj, "\xA1\x01\x01".b
199
+ end
200
+
201
+ it "1.0" do
202
+ match 1.0, "\xF9\x3c\x00".b
203
+ end
204
+
205
+ it "NaN" do
206
+ match Float::NAN, "\xF9\x7e\x00".b
207
+ end
208
+
209
+ it "[]" do
210
+ match [], "\x80".b
211
+ end
212
+
213
+ it "[0, 1, ..., 14]" do
214
+ obj = (0..14).to_a
215
+ match obj, "\x8f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e".b
216
+ end
217
+
218
+ it "[0, 1, ..., 15]" do
219
+ obj = (0..15).to_a
220
+ match obj, "\x90\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f".b
221
+ end
222
+
223
+ it "[0, 1, ..., 22]" do
224
+ obj = (0..22).to_a
225
+ match obj, "\x97\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16".b
226
+ end
227
+
228
+ it "[0, 1, ..., 23]" do
229
+ obj = (0..23).to_a
230
+ match obj, "\x98\x18\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17".b
231
+ end
232
+
233
+ it "{}" do
234
+ obj = {}
235
+ match obj, "\xA0".b
236
+ end
237
+
238
+ it "very simple bignums" do
239
+ CBOR.decode("\xc2\x40").should == 0
240
+ CBOR.decode("\xc2\x41\x00").should == 0
241
+ CBOR.decode("\xc2\x41a").should == 97
242
+ CBOR.decode("\xc2\x42aa").should == 24929
243
+ CBOR.decode("\xc3\x40").should == ~0
244
+ CBOR.decode("\xc3\x41\x00").should == ~0
245
+ CBOR.decode("\xc3\x41a").should == ~97
246
+ CBOR.decode("\xc3\x42aa").should == ~24929
247
+ end
248
+
249
+ it "0x982173487123985791827359872948752345" do
250
+ check_bn(0x982173487123985791827359872948752345, 0xc2)
251
+ check_bn(-0x982173487123985791827359872948752345, 0xc3)
252
+ check_bn(0x9821734871239857918273598729487523, 0xc2)
253
+ check_bn(0x98217348712398579182735987294875, 0xc2)
254
+ check_bn(0x982173487123985791827359872948, 0xc2)
255
+ check_bn(0x9821734871239857918273598729, 0xc2)
256
+ check_bn(0x98217348712398579182735987, 0xc2)
257
+ check 20, 0x982173487123985791827359872948752345
258
+ check 20, -0x982173487123985791827359872948752345
259
+ check 19, 0x9821734871239857918273598729487523
260
+ check 18, 0x98217348712398579182735987294875
261
+ check 18, -0x98217348712398579182735987294875
262
+ check 17, 0x982173487123985791827359872948
263
+ check 16, 0x9821734871239857918273598729
264
+ check 15, 0x98217348712398579182735987
265
+ check 15, 0x9821734871239857918273598
266
+ check 16, [0x9821734871239857918273598]
267
+ check 39, 0x982173487123985791827359872948752345982173487123985791827359872948752345
268
+ check 3005, 256**3000+4711 # 3001 bignum, 3 string, 1 tag
269
+ end
270
+
271
+ it "fixnum/bignum switch" do
272
+ CBOR.encode(CBOR.decode("\xc2\x40")).should == "\x00".b
273
+ CBOR.encode(CBOR.decode("\xc2\x41\x00")).should == "\x00".b
274
+ CBOR.encode(CBOR.decode("\xc2\x41a")).should == "\x18a".b
275
+ CBOR.encode(CBOR.decode("\xc2\x42aa")).should == "\x19aa".b
276
+ CBOR.encode(CBOR.decode("\xc2\x43aaa")).should == "\x1A\x00aaa".b
277
+ CBOR.encode(CBOR.decode("\xc2\x44aaaa")).should == "\x1Aaaaa".b
278
+ CBOR.encode(CBOR.decode("\xc2\x45aaaaa")).should == "\e\x00\x00\x00aaaaa".b
279
+ CBOR.encode(CBOR.decode("\xc2\x46aaaaaa")).should == "\e\x00\x00aaaaaa".b
280
+ CBOR.encode(CBOR.decode("\xc2\x47aaaaaaa")).should == "\e\x00aaaaaaa".b
281
+ CBOR.encode(CBOR.decode("\xc2\x48aaaaaaaa")).should == "\eaaaaaaaa".b
282
+ CBOR.encode(CBOR.decode("\xc2\x49\x00aaaaaaaa")).should == "\eaaaaaaaa".b
283
+ CBOR.encode(CBOR.decode("\xc2\x49aaaaaaaaa")).should == "\xC2Iaaaaaaaaa".b
284
+ CBOR.encode(CBOR.decode("\xc2\x4a\x00aaaaaaaaa")).should == "\xC2Iaaaaaaaaa".b
285
+ CBOR.encode(CBOR.decode("\xc2\x4aaaaaaaaaaa")).should == "\xC2Jaaaaaaaaaa".b
286
+ CBOR.encode(CBOR.decode("\xc2\x4b\x00aaaaaaaaaa")).should == "\xC2Jaaaaaaaaaa".b
287
+ CBOR.encode(CBOR.decode("\xc2\x4baaaaaaaaaaa")).should == "\xC2Kaaaaaaaaaaa".b
288
+ CBOR.encode(CBOR.decode("\xc2\x4c\x00aaaaaaaaaaa")).should == "\xC2Kaaaaaaaaaaa".b
289
+ CBOR.encode(CBOR.decode("\xc2\x4caaaaaaaaaaaa")).should == "\xC2Laaaaaaaaaaaa".b
290
+ CBOR.encode(CBOR.decode("\xc2\x4d\x00aaaaaaaaaaaa")).should == "\xC2Laaaaaaaaaaaa".b
291
+ CBOR.encode(CBOR.decode("\xc2\x4daaaaaaaaaaaaa")).should == "\xC2Maaaaaaaaaaaaa".b
292
+ end
293
+
294
+ it "a-non-ascii" do
295
+ if ''.respond_to? :encode
296
+ match "abc".encode("UTF-32BE"), "cabc"
297
+ end
298
+ end
299
+
300
+ it "a" do
301
+ match "a".encode_as_utf8, "aa"
302
+ check 2, "a".encode_as_utf8
303
+ check_decode "aa", "a".encode_as_utf8
304
+ end
305
+
306
+ it "a.b" do
307
+ if ''.respond_to? :encode
308
+ match "a".b, "Aa"
309
+ end
310
+ check 2, "a".b
311
+ check_decode "Aa", "a".b
312
+ end
313
+
314
+ it "[_ ]" do
315
+ check_decode "\x9f\xff", []
316
+ end
317
+
318
+ it "[_ 1]" do
319
+ check_decode "\x9f\x01\xff", [1]
320
+ end
321
+
322
+ it "{_ }" do
323
+ check_decode "\xbf\xff", {}
324
+ end
325
+
326
+ it "{_ 1 => 2}" do
327
+ check_decode "\xbf\x01\x02\xff", {1 => 2}
328
+ end
329
+
330
+
331
+ it "{_ 1 => BREAK}" do
332
+ lambda {
333
+ check_decode "\xbf\x01\xff", {1 => 2}
334
+ }.should raise_error(MessagePack::MalformedFormatError)
335
+ end
336
+
337
+ # Regression tests for https://github.com/cabo/cbor-ruby/issues/20
338
+ # Nested indefinite-length containers should decode correctly.
339
+
340
+ it "{_ 'a' => [_ 1]}" do
341
+ # indefinite map containing an indefinite array
342
+ check_decode "\xbf\x61a\x9f\x01\xff\xff", {"a" => [1]}
343
+ end
344
+
345
+ it "{_ 'a' => [_ 1], 'b' => [2, 3]}" do
346
+ # indefinite map containing an indefinite and a definite array
347
+ check_decode "\xbf\x61a\x9f\x01\xff\x61b\x82\x02\x03\xff", {"a" => [1], "b" => [2, 3]}
348
+ end
349
+
350
+ it "{_ 'a' => {_ 'b' => 1}}" do
351
+ # indefinite map containing an indefinite map
352
+ check_decode "\xbf\x61a\xbf\x61b\x01\xff\xff", {"a" => {"b" => 1}}
353
+ end
354
+
355
+ it "{_ 'a' => {_ }}" do
356
+ # indefinite map containing an empty indefinite map
357
+ check_decode "\xbf\x61a\xbf\xff\xff", {"a" => {}}
358
+ end
359
+
360
+ it "{_ 'a' => {_ NO BREAK}" do
361
+ # indefinite map containing an empty indefinite map
362
+ lambda {
363
+ check_decode "\xbf\x61a\xbf\xff", {"a" => {}}
364
+ }.should raise_error(EOFError)
365
+ end
366
+
367
+ it "{_ 'a' => {} NO BREAK" do
368
+ # indefinite map containing an empty indefinite map
369
+ lambda {
370
+ check_decode "\xbf\x61a\xa0", {"a" => {}}
371
+ }.should raise_error(EOFError)
372
+ end
373
+
374
+ it "{_ 'a' => {_ 'b' => 1}, 'b' => {'c' => 2}}" do
375
+ # indefinite map containing an indefinite and a definite map
376
+ check_decode "\xbf\x61a\xbf\x61b\x01\xff\x61b\xa1\x61c\x02\xff", {"a" => {"b" => 1}, "b" => {"c" => 2}}
377
+ end
378
+
379
+ it "[_ {_ 'a' => 1}]" do
380
+ # indefinite array containing an indefinite map
381
+ check_decode "\x9f\xbf\x61a\x01\xff\xff", [{"a" => 1}]
382
+ end
383
+
384
+ it "{_ 'files' => [{_ 'name' => 'foo'}]}" do
385
+ # indefinite map containing a definite array of indefinite maps
386
+ # (the exact pattern produced by Jackson's CBOR serializer)
387
+ check_decode "\xbf\x65files\x81\xbf\x64name\x63foo\xff\xff",
388
+ {"files" => [{"name" => "foo"}]}
389
+ end
390
+
391
+ it "deeply nested indefinite containers" do
392
+ # {_ "a" => {_ "b" => [_ {_ "c" => 1}]}}
393
+ check_decode "\xbf\x61a\xbf\x61b\x9f\xbf\x61c\x01\xff\xff\xff\xff",
394
+ {"a" => {"b" => [{"c" => 1}]}}
395
+ end
396
+
397
+ it "(_ a b)" do
398
+ check_decode "\x7f\xff", "".encode_as_utf8
399
+ check_decode "\x5f\xff", "".b
400
+ check_decode "\x7faa\xff", "a".encode_as_utf8
401
+ check_decode "\x5fAa\xff", "a".b
402
+ check_decode "\x7faabbb\xff", "abb".encode_as_utf8
403
+ check_decode "\x5fAaBbb\xff", "abb".b
404
+ if ''.respond_to? :encode
405
+ lambda {
406
+ check_decode "\x7faaBbb\xff", "abb".encode_as_utf8
407
+ }.should raise_error(MessagePack::MalformedFormatError)
408
+ lambda {
409
+ check_decode "\x7fAa\xff", "a".encode_as_utf8
410
+ }.should raise_error(MessagePack::MalformedFormatError)
411
+ lambda {
412
+ check_decode "\x5fAabbb\xff", "abb".b
413
+ }.should raise_error(MessagePack::MalformedFormatError)
414
+ lambda {
415
+ check_decode "\x5faa\xff", "a".b
416
+ }.should raise_error(MessagePack::MalformedFormatError)
417
+ end
418
+ end
419
+
420
+ it "(_ not-a-string)" do
421
+ lambda {
422
+ check_decode "\x5f\x00\xff", "".b
423
+ }.should raise_error(MessagePack::MalformedFormatError)
424
+ end
425
+
426
+ it "bare break" do
427
+ lambda {
428
+ check_decode "\xff", "".b
429
+ }.should raise_error(MessagePack::MalformedFormatError)
430
+ lambda {
431
+ check_decode "\x82\xff\x00\x00", "".b
432
+ }.should raise_error(MessagePack::MalformedFormatError)
433
+ lambda {
434
+ check_decode "\x82\x9f\xff\xff", "".b
435
+ }.should raise_error(MessagePack::MalformedFormatError)
436
+ end
437
+
438
+ it "Tagged" do
439
+ expect { check 10, CBOR::Tagged.new("foo", 2) }.to raise_error(TypeError)
440
+ check 2, CBOR::Tagged.new(10, 2)
441
+ check 4, CBOR::Tagged.new(0xBEEF, 2)
442
+ check 6, CBOR::Tagged.new(0xDEADBEEF, 2)
443
+ check 10, CBOR::Tagged.new(0xDEADBEEFDEADBEEF, 2)
444
+ expect { check 10, CBOR::Tagged.new(0x1DEADBEEFDEADBEEF, 2) }.to raise_error(RangeError)
445
+ end
446
+
447
+ it "Time" do
448
+ check_decode "\xc1\x19\x12\x67", Time.at(4711)
449
+ check 6, Time.at(Time.now.to_i)
450
+ end
451
+
452
+ it "URI" do
453
+ require 'uri'
454
+ # Safe default: object-constructing tags are not reconstructed.
455
+ check_decode "\xd8\x20\x78\x13http://www.ietf.org", CBOR::Tagged.new(32, "http://www.ietf.org")
456
+ # Opt in to reconstruct the URI by listing its tag.
457
+ check_decode "\xd8\x20\x78\x13http://www.ietf.org", URI.parse("http://www.ietf.org"), reconstruct_from_tags: [CBOR::TAG_URI]
458
+ end
459
+
460
+ it "regexp" do
461
+ # Safe default: tag 35 is not compiled into a Regexp.
462
+ check_decode "\xd8\x23\x63foo", CBOR::Tagged.new(35, "foo")
463
+ # Opt in to reconstruct the Regexp by listing its tag.
464
+ check_decode "\xd8\x23\x63foo", /foo/, reconstruct_from_tags: [CBOR::TAG_REGEXP]
465
+ check 14, /(?-mix:foo)/, reconstruct_from_tags: [CBOR::TAG_REGEXP]
466
+ check 6, /foo/, reconstruct_from_tags: [CBOR::TAG_REGEXP]
467
+ end
468
+
469
+ it "Unknown simple 0" do
470
+ check_decode "\xf3", CBOR::Simple.new(19)
471
+ check 1, CBOR::Simple.new(0)
472
+ end
473
+
474
+ it "Unknown tag 0" do
475
+ check_decode "\xc0\x00", CBOR::Tagged.new(0, 0)
476
+ check 11, CBOR::Tagged.new(4711, "Frotzel")
477
+ end
478
+
479
+ it "Keys as Symbols" do # Experimental!
480
+ CBOR.decode(CBOR.encode({:a => 1}), :keys_as_symbols).should == {:a => 1}
481
+ CBOR.decode(CBOR.encode({:a => 1})).should == {"a" => 1}
482
+ expect { CBOR.decode("\x00", :foobar) }.to raise_error(ArgumentError)
483
+ end
484
+
485
+ it 'CBOR.decode symbolize_keys' do
486
+ symbolized_hash = {:a => 'b', :c => 'd'}
487
+ CBOR.decode(CBOR.encode(symbolized_hash), :symbolize_keys => true).should == symbolized_hash
488
+ end
489
+
490
+ it 'Unpacker#read symbolize_keys' do
491
+ unpacker = Unpacker.new(:symbolize_keys => true)
492
+ symbolized_hash = {:a => 'b', :c => 'd'}
493
+ unpacker.feed(CBOR.encode(symbolized_hash)).read.should == symbolized_hash
494
+ end
495
+
496
+ it 'handle outrageous sizes 1' do
497
+ expect { CBOR.decode("\xa1") }.to raise_error(EOFError)
498
+ expect { CBOR.decode("\xba\xff\xff\xff\xff") }.to raise_error(EOFError)
499
+ expect { CBOR.decode("\xbb\xff\xff\xff\xff\xff\xff\xff\xff") }.to raise_error(EOFError)
500
+ expect { CBOR.decode("\xbb\x01\x01\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
501
+ expect { CBOR.decode("\xbb\x00\x00\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
502
+ expect { CBOR.decode("\x81") }.to raise_error(EOFError)
503
+ expect { CBOR.decode("\x9a\xff\xff\xff\xff") }.to raise_error(EOFError)
504
+ end
505
+ it 'handle outrageous sizes 2' do
506
+ expect { CBOR.decode("\x9b\xff\xff\xff\xff\xff\xff\xff\xff") }.to raise_error(EOFError)
507
+ end
508
+ it 'handle outrageous sizes 3' do
509
+ expect { CBOR.decode("\x9b\x01\x01\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
510
+ end
511
+ it 'handle outrageous sizes 4' do
512
+ expect { CBOR.decode("\x9b\x00\x00\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
513
+ expect { CBOR.decode("\x61") }.to raise_error(EOFError)
514
+ expect { CBOR.decode("\x7a\xff\xff\xff\xff") }.to raise_error(EOFError)
515
+ end
516
+ it 'handle outrageous sizes 5' do
517
+ expect { CBOR.decode("\x7b\xff\xff\xff\xff\xff\xff\xff\xff") }.to raise_error(EOFError)
518
+ end
519
+ it 'handle outrageous sizes 6' do
520
+ expect { CBOR.decode("\x7b\x01\x01\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
521
+ end
522
+ it 'handle outrageous sizes 7' do
523
+ expect { CBOR.decode("\x7b\x00\x00\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
524
+ expect { CBOR.decode("\x41") }.to raise_error(EOFError)
525
+ expect { CBOR.decode("\x5a\xff\xff\xff\xff") }.to raise_error(EOFError)
526
+ end
527
+ it 'handle outrageous sizes 8' do
528
+ expect { CBOR.decode("\x5b\xff\xff\xff\xff\xff\xff\xff\xff") }.to raise_error(EOFError)
529
+ end
530
+ it 'handle outrageous sizes 9' do
531
+ expect { CBOR.decode("\x5b\x01\x01\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
532
+ end
533
+ it 'handle outrageous sizes 10' do
534
+ expect { CBOR.decode("\x5b\x00\x00\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
535
+ end
536
+
537
+
538
+ ## FIXME
539
+ # it "{0=>0, 1=>1, ..., 14=>14}" do
540
+ # a = (0..14).to_a;
541
+ # match Hash[*a.zip(a).flatten], "\x8f\x05\x05\x0b\x0b\x00\x00\x06\x06\x0c\x0c\x01\x01\x07\x07\x0d\x0d\x02\x02\x08\x08\x0e\x0e\x03\x03\x09\x09\x04\x04\x0a\x0a"
542
+ # end
543
+ #
544
+ # it "{0=>0, 1=>1, ..., 15=>15}" do
545
+ # a = (0..15).to_a;
546
+ # match Hash[*a.zip(a).flatten], "\xde\x00\x10\x05\x05\x0b\x0b\x00\x00\x06\x06\x0c\x0c\x01\x01\x07\x07\x0d\x0d\x02\x02\x08\x08\x0e\x0e\x03\x03\x09\x09\x0f\x0f\x04\x04\x0a\x0a"
547
+ # end
548
+
549
+ ## FIXME
550
+ # it "fixmap" do
551
+ # check_map 1, 0
552
+ # check_map 1, (1<<4)-1
553
+ # end
554
+ #
555
+ # it "map 16" do
556
+ # check_map 3, (1<<4)
557
+ # check_map 3, (1<<16)-1
558
+ # end
559
+ #
560
+ # it "map 32" do
561
+ # check_map 5, (1<<16)
562
+ # #check_map 5, (1<<32)-1 # memory error
563
+ # end
564
+
565
+ def check(len, obj, opts = nil)
566
+ raw = obj.to_cbor.to_s
567
+ raw.length.should == len
568
+ obj2 = opts ? MessagePack.unpack(raw, opts) : MessagePack.unpack(raw)
569
+ obj2.should == obj
570
+ if obj.respond_to? :encoding
571
+ obj2.encoding.should == obj.encoding
572
+ end
573
+ end
574
+
575
+ def check_raw(overhead, num)
576
+ check num+overhead, " "*num
577
+ end
578
+
579
+ def check_array(overhead, num)
580
+ check num+overhead, Array.new(num)
581
+ end
582
+
583
+ def check_bn(bn, tag)
584
+ bnb = bignum_to_bytes(bn < 0 ? ~bn : bn)
585
+ bnbs = bnb.size
586
+ match bn, "#{tag.chr}#{(bnbs + 0x40).chr}#{bnb}"
587
+ end
588
+
589
+ def match(obj, buf)
590
+ raw = obj.to_cbor.to_s
591
+ raw.should == buf
592
+ end
593
+
594
+ def check_decode(c, obj, opts = nil)
595
+ obj2 = opts ? MessagePack.unpack(c, opts) : MessagePack.unpack(c)
596
+ obj2.should == obj
597
+ if obj.respond_to? :encoding
598
+ obj2.encoding.should == obj.encoding
599
+ end
600
+ end
601
+
602
+ end
603
+