cbor 0.5.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/.travis.yml +5 -0
  4. data/ChangeLog +87 -0
  5. data/README.rdoc +180 -0
  6. data/Rakefile +94 -0
  7. data/cbor.gemspec +26 -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/buffer.c +693 -0
  17. data/ext/cbor/buffer.h +469 -0
  18. data/ext/cbor/buffer_class.c +516 -0
  19. data/ext/cbor/buffer_class.h +41 -0
  20. data/ext/cbor/cbor.h +69 -0
  21. data/ext/cbor/compat.h +136 -0
  22. data/ext/cbor/core_ext.c +181 -0
  23. data/ext/cbor/core_ext.h +35 -0
  24. data/ext/cbor/extconf.rb +25 -0
  25. data/ext/cbor/packer.c +169 -0
  26. data/ext/cbor/packer.h +337 -0
  27. data/ext/cbor/packer_class.c +304 -0
  28. data/ext/cbor/packer_class.h +39 -0
  29. data/ext/cbor/rbinit.c +51 -0
  30. data/ext/cbor/renamer.h +56 -0
  31. data/ext/cbor/rmem.c +103 -0
  32. data/ext/cbor/rmem.h +118 -0
  33. data/ext/cbor/sysdep.h +135 -0
  34. data/ext/cbor/sysdep_endian.h +59 -0
  35. data/ext/cbor/sysdep_types.h +55 -0
  36. data/ext/cbor/unpacker.c +735 -0
  37. data/ext/cbor/unpacker.h +133 -0
  38. data/ext/cbor/unpacker_class.c +417 -0
  39. data/ext/cbor/unpacker_class.h +39 -0
  40. data/lib/cbor.rb +9 -0
  41. data/lib/cbor/version.rb +3 -0
  42. data/spec/buffer_io_spec.rb +260 -0
  43. data/spec/buffer_spec.rb +576 -0
  44. data/spec/cases.cbor +0 -0
  45. data/spec/cases.cbor_stream +0 -0
  46. data/spec/cases.json +1 -0
  47. data/spec/cases.msg +0 -0
  48. data/spec/cases_compact.msg +0 -0
  49. data/spec/cases_spec.rb +39 -0
  50. data/spec/format_spec.rb +445 -0
  51. data/spec/packer_spec.rb +127 -0
  52. data/spec/random_compat.rb +24 -0
  53. data/spec/spec_helper.rb +45 -0
  54. data/spec/unpacker_spec.rb +238 -0
  55. metadata +196 -0
@@ -0,0 +1,127 @@
1
+ # encoding: ascii-8bit
2
+ require 'spec_helper'
3
+
4
+ require 'stringio'
5
+ if defined?(Encoding)
6
+ Encoding.default_external = 'ASCII-8BIT'
7
+ end
8
+
9
+ describe Packer do
10
+ let :packer do
11
+ Packer.new
12
+ end
13
+
14
+ it 'initialize' do
15
+ Packer.new
16
+ Packer.new(nil)
17
+ Packer.new(StringIO.new)
18
+ Packer.new({})
19
+ Packer.new(StringIO.new, {})
20
+ end
21
+
22
+ #it 'Packer' do
23
+ # Packer(packer).object_id.should == packer.object_id
24
+ # Packer(nil).class.should == Packer
25
+ # Packer('').class.should == Packer
26
+ # Packer('initbuf').to_s.should == 'initbuf'
27
+ #end
28
+
29
+ it 'write' do
30
+ packer.write([])
31
+ packer.to_s.should == "\x80"
32
+ end
33
+
34
+ it 'write_nil' do
35
+ packer.write_nil
36
+ packer.to_s.should == "\xf6"
37
+ end
38
+
39
+ it 'write_array_header 0' do
40
+ packer.write_array_header(0)
41
+ packer.to_s.should == "\x80"
42
+ end
43
+
44
+ it 'write_array_header 1' do
45
+ packer.write_array_header(1)
46
+ packer.to_s.should == "\x81"
47
+ end
48
+
49
+ it 'write_map_header 0' do
50
+ packer.write_map_header(0)
51
+ packer.to_s.should == "\xa0"
52
+ end
53
+
54
+ it 'write_map_header 1' do
55
+ packer.write_map_header(1)
56
+ packer.to_s.should == "\xa1"
57
+ end
58
+
59
+ it 'flush' do
60
+ io = StringIO.new
61
+ pk = Packer.new(io)
62
+ pk.write_nil
63
+ pk.flush
64
+ pk.to_s.should == ''
65
+ io.string.should == "\xf6"
66
+ end
67
+
68
+ it 'buffer' do
69
+ o1 = packer.buffer.object_id
70
+ packer.buffer << 'frsyuki'
71
+ packer.buffer.to_s.should == 'frsyuki'
72
+ packer.buffer.object_id.should == o1
73
+ end
74
+
75
+ it 'to_cbor returns String' do
76
+ nil.to_cbor.class.should == String
77
+ true.to_cbor.class.should == String
78
+ false.to_cbor.class.should == String
79
+ 1.to_cbor.class.should == String
80
+ 1.0.to_cbor.class.should == String
81
+ "".to_cbor.class.should == String
82
+ Hash.new.to_cbor.class.should == String
83
+ Array.new.to_cbor.class.should == String
84
+ end
85
+
86
+ class CustomPack01
87
+ def to_cbor(pk=nil)
88
+ return MessagePack.pack(self, pk) unless pk.class == MessagePack::Packer
89
+ pk.write_array_header(2)
90
+ pk.write(1)
91
+ pk.write(2)
92
+ return pk
93
+ end
94
+ end
95
+
96
+ class CustomPack02
97
+ def to_cbor(pk=nil)
98
+ [1,2].to_cbor(pk)
99
+ end
100
+ end
101
+
102
+ it 'calls custom to_cbor method' do
103
+ MessagePack.pack(CustomPack01.new).should == [1,2].to_cbor
104
+ MessagePack.pack(CustomPack02.new).should == [1,2].to_cbor
105
+ CustomPack01.new.to_cbor.should == [1,2].to_cbor
106
+ CustomPack02.new.to_cbor.should == [1,2].to_cbor
107
+ end
108
+
109
+ it 'calls custom to_cbor method with io' do
110
+ s01 = StringIO.new
111
+ MessagePack.pack(CustomPack01.new, s01)
112
+ s01.string.should == [1,2].to_cbor
113
+
114
+ s02 = StringIO.new
115
+ MessagePack.pack(CustomPack02.new, s02)
116
+ s02.string.should == [1,2].to_cbor
117
+
118
+ s03 = StringIO.new
119
+ CustomPack01.new.to_cbor(s03)
120
+ s03.string.should == [1,2].to_cbor
121
+
122
+ s04 = StringIO.new
123
+ CustomPack02.new.to_cbor(s04)
124
+ s04.string.should == [1,2].to_cbor
125
+ end
126
+ end
127
+
@@ -0,0 +1,24 @@
1
+
2
+ unless defined? Random
3
+ class Random
4
+ def initialize(seed=Time.now.to_i)
5
+ Kernel.srand(seed)
6
+ @seed = seed
7
+ end
8
+
9
+ attr_reader :seed
10
+
11
+ def rand(arg)
12
+ Kernel.rand(arg)
13
+ end
14
+
15
+ def bytes(n)
16
+ array = []
17
+ n.times do
18
+ array << rand(256)
19
+ end
20
+ array.pack('C*')
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,45 @@
1
+ class String
2
+ if ''.respond_to? :encode
3
+ def encode_as_utf8
4
+ encode(Encoding::UTF_8)
5
+ end
6
+ unless String.instance_methods.include?(:b)
7
+ def b
8
+ dup.force_encoding(Encoding::BINARY)
9
+ end
10
+ end
11
+ else
12
+ def encode_as_utf8
13
+ self # MRI 1.8
14
+ end
15
+ def b
16
+ self
17
+ end
18
+ end
19
+ def hexbytes(sep = '')
20
+ bytes.map{|x| "%02x" % x}.join(sep)
21
+ end
22
+ end
23
+
24
+ if ENV['SIMPLE_COV']
25
+ require 'simplecov'
26
+ SimpleCov.start do
27
+ add_filter 'spec/'
28
+ add_filter 'pkg/'
29
+ add_filter 'vendor/'
30
+ end
31
+ end
32
+
33
+ if ENV['GC_STRESS']
34
+ puts "enable GC.stress"
35
+ GC.stress = true
36
+ end
37
+
38
+ require 'cbor'
39
+
40
+ MessagePack = CBOR # XXX
41
+
42
+ Packer = MessagePack::Packer
43
+ Unpacker = MessagePack::Unpacker
44
+ Buffer = MessagePack::Buffer
45
+
@@ -0,0 +1,238 @@
1
+ # encoding: ascii-8bit
2
+ require 'spec_helper'
3
+
4
+ describe Unpacker do
5
+ let :unpacker do
6
+ Unpacker.new
7
+ end
8
+
9
+ let :packer do
10
+ Packer.new
11
+ end
12
+
13
+ # TODO initialize
14
+
15
+ it 'read_array_header succeeds' do
16
+ unpacker.feed("\x81")
17
+ unpacker.read_array_header.should == 1
18
+ end
19
+
20
+ it 'read_array_header fails' do
21
+ unpacker.feed("\xa1")
22
+ lambda {
23
+ unpacker.read_array_header
24
+ }.should raise_error(MessagePack::TypeError)
25
+ end
26
+
27
+ it 'read_map_header succeeds' do
28
+ unpacker.feed("\xa1")
29
+ unpacker.read_map_header.should == 1
30
+ end
31
+
32
+ it 'read_map_header fails' do
33
+ unpacker.feed("\x81")
34
+ lambda {
35
+ unpacker.read_map_header
36
+ }.should raise_error(MessagePack::TypeError)
37
+ end
38
+
39
+ it 'skip_nil succeeds' do
40
+ unpacker.feed("\xf6")
41
+ unpacker.skip_nil.should == true
42
+ end
43
+
44
+ it 'skip_nil fails' do
45
+ unpacker.feed("\x80")
46
+ unpacker.skip_nil.should == false
47
+ end
48
+
49
+ it 'skip skips objects' do
50
+ packer.write(1)
51
+ packer.write(2)
52
+ packer.write(3)
53
+ packer.write(4)
54
+ packer.write(5)
55
+
56
+ unpacker = Unpacker.new(packer.buffer)
57
+
58
+ unpacker.read.should == 1
59
+ unpacker.skip
60
+ unpacker.read.should == 3
61
+ unpacker.skip
62
+ unpacker.read.should == 5
63
+ end
64
+
65
+ it 'read raises EOFError' do
66
+ lambda {
67
+ unpacker.read
68
+ }.should raise_error(EOFError)
69
+ end
70
+
71
+ it 'skip raises EOFError' do
72
+ lambda {
73
+ unpacker.skip
74
+ }.should raise_error(EOFError)
75
+ end
76
+
77
+ it 'skip_nil raises EOFError' do
78
+ lambda {
79
+ unpacker.skip_nil
80
+ }.should raise_error(EOFError)
81
+ end
82
+
83
+ let :sample_object do
84
+ [1024, {["a","b"]=>["c","d"]}, ["e","f"], "d", 70000, 4.12, 1.5, 1.5, 1.5]
85
+ end
86
+
87
+ it 'sample object OK' do
88
+ obj2 = MessagePack.unpack(sample_object.to_cbor)
89
+ obj2.should == sample_object
90
+ end
91
+
92
+ it 'feed and each continue internal state' do
93
+ raw = sample_object.to_cbor.to_s * 4
94
+ objects = []
95
+
96
+ raw.split(//).each do |b|
97
+ unpacker.feed(b)
98
+ unpacker.each {|c|
99
+ objects << c
100
+ }
101
+ end
102
+
103
+ objects.should == [sample_object] * 4
104
+ end
105
+
106
+ it 'feed_each continues internal state' do
107
+ raw = sample_object.to_cbor.to_s * 4
108
+ objects = []
109
+
110
+ raw.split(//).each do |b|
111
+ unpacker.feed_each(b) {|c|
112
+ objects << c
113
+ }
114
+ end
115
+
116
+ objects.should == [sample_object] * 4
117
+ end
118
+
119
+ it 'reset clears internal buffer' do
120
+ # 1-element array
121
+ unpacker.feed("\x91")
122
+ unpacker.reset
123
+ unpacker.feed("\x01")
124
+
125
+ unpacker.each.map {|x| x }.should == [1]
126
+ end
127
+
128
+ it 'reset clears internal state' do
129
+ # 1-element array
130
+ unpacker.feed("\x91")
131
+ unpacker.each.map {|x| x }.should == []
132
+
133
+ unpacker.reset
134
+
135
+ unpacker.feed("\x01")
136
+ unpacker.each.map {|x| x }.should == [1]
137
+ end
138
+
139
+ it 'buffer' do
140
+ o1 = unpacker.buffer.object_id
141
+ unpacker.buffer << 'frsyuki'
142
+ unpacker.buffer.to_s.should == 'frsyuki'
143
+ unpacker.buffer.object_id.should == o1
144
+ end
145
+
146
+ it 'frozen short strings' do
147
+ raw = sample_object.to_cbor.to_s.force_encoding('UTF-8')
148
+ lambda {
149
+ unpacker.feed_each(raw.freeze) { }
150
+ }.should_not raise_error
151
+ end
152
+
153
+ it 'frozen long strings' do
154
+ raw = (sample_object.to_cbor.to_s * 10240).force_encoding('UTF-8')
155
+ lambda {
156
+ unpacker.feed_each(raw.freeze) { }
157
+ }.should_not raise_error
158
+ end
159
+
160
+ it 'read raises level stack too deep error' do
161
+ 512.times { packer.write_array_header(1) }
162
+ packer.write(nil)
163
+
164
+ unpacker = Unpacker.new(packer.buffer)
165
+ lambda {
166
+ unpacker.read
167
+ }.should raise_error(MessagePack::StackError)
168
+ end
169
+
170
+ it 'skip raises level stack too deep error' do
171
+ 512.times { packer.write_array_header(1) }
172
+ packer.write(nil)
173
+
174
+ unpacker = Unpacker.new(packer.buffer)
175
+ lambda {
176
+ unpacker.skip
177
+ }.should raise_error(MessagePack::StackError)
178
+ end
179
+
180
+ it 'read raises invalid byte error' do
181
+ unpacker.feed("\xdf")
182
+ lambda {
183
+ unpacker.read
184
+ }.should raise_error(MessagePack::MalformedFormatError)
185
+ end
186
+
187
+ it 'skip raises invalid byte error' do
188
+ unpacker.feed("\xdf")
189
+ lambda {
190
+ unpacker.skip
191
+ }.should raise_error(MessagePack::MalformedFormatError)
192
+ end
193
+
194
+ it "gc mark" do
195
+ raw = sample_object.to_cbor.to_s * 4
196
+
197
+ n = 0
198
+ raw.split(//).each do |b|
199
+ GC.start
200
+ unpacker.feed_each(b) {|o|
201
+ GC.start
202
+ o.should == sample_object
203
+ n += 1
204
+ }
205
+ GC.start
206
+ end
207
+
208
+ n.should == 4
209
+ end
210
+
211
+ it "buffer" do
212
+ orig = "a"*32*1024*4
213
+ raw = orig.to_cbor.to_s
214
+
215
+ n = 655
216
+ times = raw.size / n
217
+ times += 1 unless raw.size % n == 0
218
+
219
+ off = 0
220
+ parsed = false
221
+
222
+ times.times do
223
+ parsed.should == false
224
+
225
+ seg = raw[off, n]
226
+ off += seg.length
227
+
228
+ unpacker.feed_each(seg) {|obj|
229
+ parsed.should == false
230
+ obj.should == orig
231
+ parsed = true
232
+ }
233
+ end
234
+
235
+ parsed.should == true
236
+ end
237
+ end
238
+
metadata ADDED
@@ -0,0 +1,196 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cbor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.6.2
5
+ platform: ruby
6
+ authors:
7
+ - Carsten Bormann, standing on the tall shoulders of Sadayuki Furuhashi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake-compiler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.8.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.8.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '2.11'
69
+ - !ruby/object:Gem::Dependency
70
+ name: json
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.7'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.7'
83
+ - !ruby/object:Gem::Dependency
84
+ name: yard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 0.8.2
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 0.8.2
97
+ description: CBOR is a library for the CBOR binary object representation format, based
98
+ on Sadayuki Furuhashi's MessagePack library.
99
+ email: cabo@tzi.org
100
+ executables: []
101
+ extensions:
102
+ - ext/cbor/extconf.rb
103
+ extra_rdoc_files: []
104
+ files:
105
+ - .gitignore
106
+ - .travis.yml
107
+ - ChangeLog
108
+ - Gemfile
109
+ - README.rdoc
110
+ - Rakefile
111
+ - cbor.gemspec
112
+ - doclib/cbor.rb
113
+ - doclib/cbor/buffer.rb
114
+ - doclib/cbor/core_ext.rb
115
+ - doclib/cbor/error.rb
116
+ - doclib/cbor/packer.rb
117
+ - doclib/cbor/simple.rb
118
+ - doclib/cbor/tagged.rb
119
+ - doclib/cbor/unpacker.rb
120
+ - ext/cbor/buffer.c
121
+ - ext/cbor/buffer.h
122
+ - ext/cbor/buffer_class.c
123
+ - ext/cbor/buffer_class.h
124
+ - ext/cbor/cbor.h
125
+ - ext/cbor/compat.h
126
+ - ext/cbor/core_ext.c
127
+ - ext/cbor/core_ext.h
128
+ - ext/cbor/extconf.rb
129
+ - ext/cbor/packer.c
130
+ - ext/cbor/packer.h
131
+ - ext/cbor/packer_class.c
132
+ - ext/cbor/packer_class.h
133
+ - ext/cbor/rbinit.c
134
+ - ext/cbor/renamer.h
135
+ - ext/cbor/rmem.c
136
+ - ext/cbor/rmem.h
137
+ - ext/cbor/sysdep.h
138
+ - ext/cbor/sysdep_endian.h
139
+ - ext/cbor/sysdep_types.h
140
+ - ext/cbor/unpacker.c
141
+ - ext/cbor/unpacker.h
142
+ - ext/cbor/unpacker_class.c
143
+ - ext/cbor/unpacker_class.h
144
+ - lib/cbor.rb
145
+ - lib/cbor/version.rb
146
+ - spec/buffer_io_spec.rb
147
+ - spec/buffer_spec.rb
148
+ - spec/cases.cbor
149
+ - spec/cases.cbor_stream
150
+ - spec/cases.json
151
+ - spec/cases.msg
152
+ - spec/cases_compact.msg
153
+ - spec/cases_spec.rb
154
+ - spec/format_spec.rb
155
+ - spec/packer_spec.rb
156
+ - spec/random_compat.rb
157
+ - spec/spec_helper.rb
158
+ - spec/unpacker_spec.rb
159
+ homepage:
160
+ licenses:
161
+ - Apache 2.0
162
+ metadata: {}
163
+ post_install_message:
164
+ rdoc_options: []
165
+ require_paths:
166
+ - lib
167
+ required_ruby_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - '>='
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ required_rubygems_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - '>='
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ requirements: []
178
+ rubyforge_project:
179
+ rubygems_version: 2.0.3
180
+ signing_key:
181
+ specification_version: 4
182
+ summary: CBOR, Concise Binary Object Representation.
183
+ test_files:
184
+ - spec/buffer_io_spec.rb
185
+ - spec/buffer_spec.rb
186
+ - spec/cases.cbor
187
+ - spec/cases.cbor_stream
188
+ - spec/cases.json
189
+ - spec/cases.msg
190
+ - spec/cases_compact.msg
191
+ - spec/cases_spec.rb
192
+ - spec/format_spec.rb
193
+ - spec/packer_spec.rb
194
+ - spec/random_compat.rb
195
+ - spec/spec_helper.rb
196
+ - spec/unpacker_spec.rb