hx_cbor 2021.8.20

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +30 -0
  3. data/.travis.yml +24 -0
  4. data/ChangeLog +106 -0
  5. data/Gemfile +11 -0
  6. data/README.rdoc +191 -0
  7. data/Rakefile +97 -0
  8. data/doclib/cbor.rb +80 -0
  9. data/doclib/cbor/buffer.rb +193 -0
  10. data/doclib/cbor/core_ext.rb +133 -0
  11. data/doclib/cbor/error.rb +14 -0
  12. data/doclib/cbor/packer.rb +133 -0
  13. data/doclib/cbor/simple.rb +15 -0
  14. data/doclib/cbor/tagged.rb +16 -0
  15. data/doclib/cbor/unpacker.rb +138 -0
  16. data/ext/cbor/3424.i.rb +29 -0
  17. data/ext/cbor/buffer.c +693 -0
  18. data/ext/cbor/buffer.h +484 -0
  19. data/ext/cbor/buffer_class.c +516 -0
  20. data/ext/cbor/buffer_class.h +41 -0
  21. data/ext/cbor/cbor.h +69 -0
  22. data/ext/cbor/compat.h +147 -0
  23. data/ext/cbor/core_ext.c +201 -0
  24. data/ext/cbor/core_ext.h +35 -0
  25. data/ext/cbor/example.rb +10 -0
  26. data/ext/cbor/extconf.rb +29 -0
  27. data/ext/cbor/install.sh +1 -0
  28. data/ext/cbor/packer.c +169 -0
  29. data/ext/cbor/packer.h +362 -0
  30. data/ext/cbor/packer_class.c +304 -0
  31. data/ext/cbor/packer_class.h +39 -0
  32. data/ext/cbor/rbinit.c +51 -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 +784 -0
  40. data/ext/cbor/unpacker.h +135 -0
  41. data/ext/cbor/unpacker_class.c +439 -0
  42. data/ext/cbor/unpacker_class.h +39 -0
  43. data/hx_cbor.gemspec +25 -0
  44. data/lib/cbor.rb +6 -0
  45. data/lib/cbor/version.rb +3 -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 +540 -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 +198 -0
data/spec/cases.cbor ADDED
Binary file
Binary file
data/spec/cases.json ADDED
@@ -0,0 +1 @@
1
+ [false,true,null,0,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,127,127,255,65535,4294967295,-32,-32,-128,-32768,-2147483648,0.0,-0.0,1.0,-1.0,"a","a","a","","","",[0],[0],[0],[],[],[],{},{},{},{"a":97},{"a":97},{"a":97},[[]],[["a"]]]
data/spec/cases.msg ADDED
Binary file
Binary file
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+ require 'json'
3
+
4
+ describe MessagePack do
5
+ here = File.dirname(__FILE__)
6
+ CASES = File.read("#{here}/cases.cbor_stream")
7
+ CASES_JSON = File.read("#{here}/cases.json")
8
+ CASES_INDEFINITE = File.read("#{here}/cases.cbor_stream") # TODO
9
+
10
+ it 'compare with json' do
11
+ ms = []
12
+ MessagePack::Unpacker.new.feed_each(CASES) {|m|
13
+ ms << m
14
+ }
15
+
16
+ js = JSON.load(CASES_JSON)
17
+
18
+ ms.zip(js) {|m,j|
19
+ m.should == j
20
+ }
21
+ end
22
+
23
+ it 'compare with compat' do
24
+ ms = []
25
+ MessagePack::Unpacker.new.feed_each(CASES) {|m|
26
+ ms << m
27
+ }
28
+
29
+ cs = []
30
+ MessagePack::Unpacker.new.feed_each(CASES_INDEFINITE) {|c|
31
+ cs << c
32
+ }
33
+
34
+ ms.zip(cs) {|m,c|
35
+ m.should == c
36
+ }
37
+ end
38
+ end
39
+
@@ -0,0 +1,540 @@
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
+ it "(_ a b)" do
338
+ check_decode "\x7f\xff", "".encode_as_utf8
339
+ check_decode "\x5f\xff", "".b
340
+ check_decode "\x7faa\xff", "a".encode_as_utf8
341
+ check_decode "\x5fAa\xff", "a".b
342
+ check_decode "\x7faabbb\xff", "abb".encode_as_utf8
343
+ check_decode "\x5fAaBbb\xff", "abb".b
344
+ if ''.respond_to? :encode
345
+ lambda {
346
+ check_decode "\x7faaBbb\xff", "abb".encode_as_utf8
347
+ }.should raise_error(MessagePack::MalformedFormatError)
348
+ lambda {
349
+ check_decode "\x7fAa\xff", "a".encode_as_utf8
350
+ }.should raise_error(MessagePack::MalformedFormatError)
351
+ lambda {
352
+ check_decode "\x5fAabbb\xff", "abb".b
353
+ }.should raise_error(MessagePack::MalformedFormatError)
354
+ lambda {
355
+ check_decode "\x5faa\xff", "a".b
356
+ }.should raise_error(MessagePack::MalformedFormatError)
357
+ end
358
+ end
359
+
360
+ it "(_ not-a-string)" do
361
+ lambda {
362
+ check_decode "\x5f\x00\xff", "".b
363
+ }.should raise_error(MessagePack::MalformedFormatError)
364
+ end
365
+
366
+ it "bare break" do
367
+ lambda {
368
+ check_decode "\xff", "".b
369
+ }.should raise_error(MessagePack::MalformedFormatError)
370
+ lambda {
371
+ check_decode "\x82\xff\x00\x00", "".b
372
+ }.should raise_error(MessagePack::MalformedFormatError)
373
+ lambda {
374
+ check_decode "\x82\x9f\xff\xff", "".b
375
+ }.should raise_error(MessagePack::MalformedFormatError)
376
+ end
377
+
378
+ it "Tagged" do
379
+ expect { check 10, CBOR::Tagged.new("foo", 2) }.to raise_error(TypeError)
380
+ check 2, CBOR::Tagged.new(10, 2)
381
+ check 4, CBOR::Tagged.new(0xBEEF, 2)
382
+ check 6, CBOR::Tagged.new(0xDEADBEEF, 2)
383
+ check 10, CBOR::Tagged.new(0xDEADBEEFDEADBEEF, 2)
384
+ expect { check 10, CBOR::Tagged.new(0x1DEADBEEFDEADBEEF, 2) }.to raise_error(RangeError)
385
+ end
386
+
387
+ it "Time" do
388
+ check_decode "\xc1\x19\x12\x67", Time.at(4711)
389
+ check 6, Time.at(Time.now.to_i)
390
+ end
391
+
392
+ it "URI" do
393
+ # check_decode "\xd8\x20\x78\x13http://www.ietf.org", CBOR::Tagged.new(32, "http://www.ietf.org")
394
+ require 'uri'
395
+ check_decode "\xd8\x20\x78\x13http://www.ietf.org", URI.parse("http://www.ietf.org")
396
+ # This doesn't work yet if 'uri' is not required before 'cbor':
397
+ # check 6, URI.parse("http://www.ietf.org")
398
+ end
399
+
400
+ it "regexp" do
401
+ check_decode "\xd8\x23\x63foo", /foo/
402
+ check 14, /(?-mix:foo)/
403
+ check 6, /foo/
404
+ end
405
+
406
+ it "Unknown simple 0" do
407
+ check_decode "\xf3", CBOR::Simple.new(19)
408
+ check 1, CBOR::Simple.new(0)
409
+ end
410
+
411
+ it "Unknown tag 0" do
412
+ check_decode "\xc0\x00", CBOR::Tagged.new(0, 0)
413
+ check 11, CBOR::Tagged.new(4711, "Frotzel")
414
+ end
415
+
416
+ it "Keys as Symbols" do # Experimental!
417
+ CBOR.decode(CBOR.encode({:a => 1}), :keys_as_symbols).should == {:a => 1}
418
+ CBOR.decode(CBOR.encode({:a => 1})).should == {"a" => 1}
419
+ expect { CBOR.decode("\x00", :foobar) }.to raise_error(ArgumentError)
420
+ end
421
+
422
+ it 'CBOR.decode symbolize_keys' do
423
+ symbolized_hash = {:a => 'b', :c => 'd'}
424
+ CBOR.decode(CBOR.encode(symbolized_hash), :symbolize_keys => true).should == symbolized_hash
425
+ end
426
+
427
+ it 'Unpacker#read symbolize_keys' do
428
+ unpacker = Unpacker.new(:symbolize_keys => true)
429
+ symbolized_hash = {:a => 'b', :c => 'd'}
430
+ unpacker.feed(CBOR.encode(symbolized_hash)).read.should == symbolized_hash
431
+ end
432
+
433
+ it 'handle outrageous sizes 1' do
434
+ expect { CBOR.decode("\xa1") }.to raise_error(EOFError)
435
+ expect { CBOR.decode("\xba\xff\xff\xff\xff") }.to raise_error(EOFError)
436
+ expect { CBOR.decode("\xbb\xff\xff\xff\xff\xff\xff\xff\xff") }.to raise_error(EOFError)
437
+ expect { CBOR.decode("\xbb\x01\x01\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
438
+ expect { CBOR.decode("\xbb\x00\x00\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
439
+ expect { CBOR.decode("\x81") }.to raise_error(EOFError)
440
+ expect { CBOR.decode("\x9a\xff\xff\xff\xff") }.to raise_error(EOFError)
441
+ end
442
+ it 'handle outrageous sizes 2' do
443
+ expect { CBOR.decode("\x9b\xff\xff\xff\xff\xff\xff\xff\xff") }.to raise_error(EOFError)
444
+ end
445
+ it 'handle outrageous sizes 3' do
446
+ expect { CBOR.decode("\x9b\x01\x01\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
447
+ end
448
+ it 'handle outrageous sizes 4' do
449
+ expect { CBOR.decode("\x9b\x00\x00\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
450
+ expect { CBOR.decode("\x61") }.to raise_error(EOFError)
451
+ expect { CBOR.decode("\x7a\xff\xff\xff\xff") }.to raise_error(EOFError)
452
+ end
453
+ it 'handle outrageous sizes 5' do
454
+ expect { CBOR.decode("\x7b\xff\xff\xff\xff\xff\xff\xff\xff") }.to raise_error(EOFError)
455
+ end
456
+ it 'handle outrageous sizes 6' do
457
+ expect { CBOR.decode("\x7b\x01\x01\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
458
+ end
459
+ it 'handle outrageous sizes 7' do
460
+ expect { CBOR.decode("\x7b\x00\x00\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
461
+ expect { CBOR.decode("\x41") }.to raise_error(EOFError)
462
+ expect { CBOR.decode("\x5a\xff\xff\xff\xff") }.to raise_error(EOFError)
463
+ end
464
+ it 'handle outrageous sizes 8' do
465
+ expect { CBOR.decode("\x5b\xff\xff\xff\xff\xff\xff\xff\xff") }.to raise_error(EOFError)
466
+ end
467
+ it 'handle outrageous sizes 9' do
468
+ expect { CBOR.decode("\x5b\x01\x01\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
469
+ end
470
+ it 'handle outrageous sizes 10' do
471
+ expect { CBOR.decode("\x5b\x00\x00\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
472
+ end
473
+
474
+
475
+ ## FIXME
476
+ # it "{0=>0, 1=>1, ..., 14=>14}" do
477
+ # a = (0..14).to_a;
478
+ # 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"
479
+ # end
480
+ #
481
+ # it "{0=>0, 1=>1, ..., 15=>15}" do
482
+ # a = (0..15).to_a;
483
+ # 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"
484
+ # end
485
+
486
+ ## FIXME
487
+ # it "fixmap" do
488
+ # check_map 1, 0
489
+ # check_map 1, (1<<4)-1
490
+ # end
491
+ #
492
+ # it "map 16" do
493
+ # check_map 3, (1<<4)
494
+ # check_map 3, (1<<16)-1
495
+ # end
496
+ #
497
+ # it "map 32" do
498
+ # check_map 5, (1<<16)
499
+ # #check_map 5, (1<<32)-1 # memory error
500
+ # end
501
+
502
+ def check(len, obj)
503
+ raw = obj.to_cbor.to_s
504
+ raw.length.should == len
505
+ obj2 = MessagePack.unpack(raw)
506
+ obj2.should == obj
507
+ if obj.respond_to? :encoding
508
+ obj2.encoding.should == obj.encoding
509
+ end
510
+ end
511
+
512
+ def check_raw(overhead, num)
513
+ check num+overhead, " "*num
514
+ end
515
+
516
+ def check_array(overhead, num)
517
+ check num+overhead, Array.new(num)
518
+ end
519
+
520
+ def check_bn(bn, tag)
521
+ bnb = bignum_to_bytes(bn < 0 ? ~bn : bn)
522
+ bnbs = bnb.size
523
+ match bn, "#{tag.chr}#{(bnbs + 0x40).chr}#{bnb}"
524
+ end
525
+
526
+ def match(obj, buf)
527
+ raw = obj.to_cbor.to_s
528
+ raw.should == buf
529
+ end
530
+
531
+ def check_decode(c, obj)
532
+ obj2 = MessagePack.unpack(c)
533
+ obj2.should == obj
534
+ if obj.respond_to? :encoding
535
+ obj2.encoding.should == obj.encoding
536
+ end
537
+ end
538
+
539
+ end
540
+