bindata 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of bindata might be problematic. Click here for more details.

@@ -139,7 +139,6 @@ module AllIntegers
139
139
 
140
140
  def create_mapping_of_class_to_nbits(endian, signed)
141
141
  base = signed ? "Int" : "Uint"
142
- signed_sym = signed ? :signed : :unsigned
143
142
  endian_str = (endian == :little) ? "le" : "be"
144
143
 
145
144
  result = {}
@@ -134,6 +134,55 @@ describe BinData::IO::Read, "#with_buffer" do
134
134
  end
135
135
  io.offset.must_equal(10)
136
136
  end
137
+
138
+ it "restricts large -ve seeks" do
139
+ io.readbytes(2)
140
+ io.with_buffer(10) do
141
+ io.seekbytes(-1)
142
+ io.offset.must_equal(2)
143
+ end
144
+ end
145
+ end
146
+
147
+ describe BinData::IO::Write, "writing to non seekable stream" do
148
+ before do
149
+ @rd, @wr = IO::pipe
150
+ @io = BinData::IO::Write.new(@wr)
151
+ end
152
+
153
+ after do
154
+ @rd.close
155
+ @wr.close
156
+ end
157
+
158
+ it "writes data" do
159
+ @io.writebytes("1234567890")
160
+ @rd.read(10).must_equal "1234567890"
161
+ end
162
+
163
+ it "has correct offset" do
164
+ @io.writebytes("1234567890")
165
+ @io.offset.must_equal 10
166
+ end
167
+
168
+ it "does not seek backwards" do
169
+ @io.writebytes("1234567890")
170
+ lambda {
171
+ @io.seekbytes(-5)
172
+ }.must_raise IOError
173
+ end
174
+
175
+ it "does not seek forwards" do
176
+ lambda {
177
+ @io.seekbytes(5)
178
+ }.must_raise IOError
179
+ end
180
+
181
+ it "#num_bytes_remaining raises IOError" do
182
+ lambda {
183
+ @io.num_bytes_remaining
184
+ }.must_raise IOError
185
+ end
137
186
  end
138
187
 
139
188
  describe BinData::IO::Write, "when writing" do
@@ -205,6 +254,21 @@ describe BinData::IO::Write, "#with_buffer" do
205
254
 
206
255
  stream.value.must_equal "abc\0\0de\0\0\0"
207
256
  end
257
+
258
+ it "restricts large seeks" do
259
+ io.with_buffer(10) do
260
+ io.seekbytes(15)
261
+ end
262
+ io.offset.must_equal(10)
263
+ end
264
+
265
+ it "restricts large -ve seeks" do
266
+ io.writebytes("12")
267
+ io.with_buffer(10) do
268
+ io.seekbytes(-1)
269
+ io.offset.must_equal(2)
270
+ end
271
+ end
208
272
  end
209
273
 
210
274
  describe BinData::IO::Read, "reading bits in big endian" do
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
+ require 'bindata/offset'
4
5
 
5
6
  describe BinData::Base, "offsets" do
6
7
  class ThreeByteReader < BinData::Base
@@ -91,6 +91,24 @@ describe BinData::Record, "when defining with errors" do
91
91
  end
92
92
  }.must_raise_on_line ArgumentError, 2, "unknown value for endian 'a bad value' in BadEndianRecord"
93
93
  end
94
+
95
+ it "fails when endian is after a field" do
96
+ lambda {
97
+ class BadEndianPosRecord < BinData::Record
98
+ string :a
99
+ endian :little
100
+ end
101
+ }.must_raise_on_line SyntaxError, 3, "endian must be called before defining fields in BadEndianPosRecord"
102
+ end
103
+
104
+ it "fails when search_prefix is after a field" do
105
+ lambda {
106
+ class BadSearchPrefixPosRecord < BinData::Record
107
+ string :a
108
+ search_prefix :pre
109
+ end
110
+ }.must_raise_on_line SyntaxError, 3, "search_prefix must be called before defining fields in BadSearchPrefixPosRecord"
111
+ end
94
112
  end
95
113
 
96
114
  describe BinData::Record, "with anonymous fields" do
@@ -532,7 +550,7 @@ describe BinData::Record, "defined recursively" do
532
550
  end
533
551
 
534
552
  it "can be created" do
535
- obj = RecursiveRecord.new
553
+ RecursiveRecord.new
536
554
  end
537
555
 
538
556
  it "reads" do
@@ -2,7 +2,16 @@
2
2
 
3
3
  require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
4
 
5
- describe BinData::Skip do
5
+ describe BinData::Skip, "when instantiating" do
6
+ describe "with no mandatory parameters supplied" do
7
+ it "raises an error" do
8
+ args = {}
9
+ lambda { BinData::Skip.new(args) }.must_raise ArgumentError
10
+ end
11
+ end
12
+ end
13
+
14
+ describe BinData::Skip, "with :length" do
6
15
  let(:obj) { BinData::Skip.new(:length => 5) }
7
16
  let(:io) { StringIO.new("abcdefghij") }
8
17
 
@@ -26,3 +35,53 @@ describe BinData::Skip do
26
35
  obj.to_binary_s.must_equal_binary "\000" * 5
27
36
  end
28
37
  end
38
+
39
+ describe BinData::Skip, "with :to_abs_offset" do
40
+ BinData::Struct.new(:fields => [ [:skip, :f, { :to_abs_offset => 5 } ] ])
41
+
42
+ let(:skip_obj) { [:skip, :f, { :to_abs_offset => 5 } ] }
43
+ let(:io) { StringIO.new("abcdefghij") }
44
+
45
+ it "reads skipping forward" do
46
+ fields = [ skip_obj ]
47
+ obj = BinData::Struct.new(:fields => fields)
48
+ obj.read(io)
49
+ io.pos.must_equal 5
50
+ end
51
+
52
+ it "reads skipping in place" do
53
+ fields = [ [:string, :a, { :read_length => 5 }], skip_obj ]
54
+ obj = BinData::Struct.new(:fields => fields)
55
+ obj.read(io)
56
+ io.pos.must_equal 5
57
+ end
58
+
59
+ it "does not read skipping backwards" do
60
+ fields = [ [:string, :a, { :read_length => 10 }], skip_obj ]
61
+ obj = BinData::Struct.new(:fields => fields)
62
+
63
+ lambda {
64
+ obj.read(io)
65
+ }.must_raise BinData::ValidityError
66
+ end
67
+
68
+ it "writes skipping forward" do
69
+ fields = [ skip_obj ]
70
+ obj = BinData::Struct.new(:fields => fields)
71
+ obj.to_binary_s.must_equal "\000\000\000\000\000"
72
+ end
73
+
74
+ it "reads skipping in place" do
75
+ fields = [ [:string, :a, { :value => "abcde" }], skip_obj ]
76
+ obj = BinData::Struct.new(:fields => fields)
77
+ obj.to_binary_s.must_equal "abcde"
78
+ end
79
+
80
+ it "does not write skipping backwards" do
81
+ fields = [ [:string, :a, { :value => "abcdefghij" }], skip_obj ]
82
+ obj = BinData::Struct.new(:fields => fields)
83
+ lambda {
84
+ obj.to_binary_s
85
+ }.must_raise BinData::ValidityError
86
+ end
87
+ end
@@ -295,19 +295,6 @@ describe BinData::String, "with Ruby 1.9 encodings" do
295
295
  end
296
296
  end
297
297
 
298
- class Object
299
- def must_warn(msg, &block)
300
- result = ""
301
- callable = proc { |str|
302
- result = str
303
- }
304
- self.stub(:warn, callable) do
305
- block.call
306
- end
307
- result.must_equal msg
308
- end
309
- end
310
-
311
298
  describe BinData::String, "warnings" do
312
299
  it "warns if has :asserted_value but no :length" do
313
300
  obj = BinData::String.new(:asserted_value => "ABC")
@@ -81,6 +81,10 @@ describe BinData::Struct, "with hidden fields" do
81
81
  obj.field_names.must_equal [:a, :d]
82
82
  end
83
83
 
84
+ it "shows all fields when requested" do
85
+ obj.field_names(true).must_equal [:a, :b, :c, :d]
86
+ end
87
+
84
88
  it "accesses hidden fields directly" do
85
89
  obj.b.must_equal 5
86
90
  obj.c = 15
@@ -136,6 +140,10 @@ describe BinData::Struct, "with multiple fields" do
136
140
  obj[:a].must_equal 1
137
141
  end
138
142
 
143
+ it "handles not existing elements" do
144
+ obj[:does_not_exist].must_be_nil
145
+ end
146
+
139
147
  it "writes elements dynamically" do
140
148
  obj[:a] = 2
141
149
  obj.a.must_equal 2
@@ -154,6 +162,7 @@ describe BinData::Struct, "with multiple fields" do
154
162
 
155
163
  it "returns a snapshot" do
156
164
  snap = obj.snapshot
165
+ assert snap.respond_to?(:a)
157
166
  snap.a.must_equal 1
158
167
  snap.b.must_equal 2
159
168
  snap.must_equal({ :a => 1, :b => 2 })
@@ -421,7 +430,7 @@ end
421
430
 
422
431
  describe BinData::Struct, "with dynamically named types" do
423
432
  it "instantiates" do
424
- dyn = BinData::Struct.new(:name => :my_struct, :fields => [[:int8, :a, {:initial_value => 3}]])
433
+ _ = BinData::Struct.new(:name => :my_struct, :fields => [[:int8, :a, {:initial_value => 3}]])
425
434
 
426
435
  obj = BinData::Struct.new(:fields => [[:my_struct, :v]])
427
436
 
@@ -47,4 +47,15 @@ module Kernel
47
47
 
48
48
  (err_line - ref_line).must_equal line
49
49
  end
50
+
51
+ def must_warn(msg, &block)
52
+ result = ""
53
+ callable = proc { |str|
54
+ result = str
55
+ }
56
+ self.stub(:warn, callable) do
57
+ block.call
58
+ end
59
+ result.must_equal msg
60
+ end
50
61
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bindata
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dion Mendel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-30 00:00:00.000000000 Z
11
+ date: 2016-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -91,6 +91,7 @@ files:
91
91
  - lib/bindata/buffer.rb
92
92
  - lib/bindata/choice.rb
93
93
  - lib/bindata/count_bytes_remaining.rb
94
+ - lib/bindata/delayed_io.rb
94
95
  - lib/bindata/dsl.rb
95
96
  - lib/bindata/float.rb
96
97
  - lib/bindata/framework.rb
@@ -121,6 +122,7 @@ files:
121
122
  - test/buffer_test.rb
122
123
  - test/choice_test.rb
123
124
  - test/count_bytes_remaining_test.rb
125
+ - test/delayed_io_test.rb
124
126
  - test/float_test.rb
125
127
  - test/int_test.rb
126
128
  - test/io_test.rb