bindata 0.10.0 → 0.11.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.
- data/ChangeLog +7 -1
- data/INSTALL +4 -0
- data/Rakefile +17 -0
- data/TODO +1 -22
- data/lib/bindata.rb +4 -2
- data/lib/bindata/array.rb +24 -81
- data/lib/bindata/base.rb +36 -25
- data/lib/bindata/base_primitive.rb +9 -0
- data/lib/bindata/choice.rb +26 -40
- data/lib/bindata/deprecated.rb +148 -0
- data/lib/bindata/lazy.rb +1 -3
- data/lib/bindata/params.rb +21 -92
- data/lib/bindata/primitive.rb +18 -42
- data/lib/bindata/record.rb +23 -54
- data/lib/bindata/registry.rb +33 -21
- data/lib/bindata/sanitize.rb +193 -47
- data/lib/bindata/string.rb +2 -14
- data/lib/bindata/struct.rb +36 -82
- data/lib/bindata/trace.rb +1 -2
- data/lib/bindata/wrapper.rb +139 -0
- data/setup.rb +1585 -0
- data/spec/array_spec.rb +7 -22
- data/spec/base_primitive_spec.rb +11 -2
- data/spec/base_spec.rb +24 -2
- data/spec/bits_spec.rb +1 -1
- data/spec/choice_spec.rb +2 -2
- data/spec/deprecated_spec.rb +96 -0
- data/spec/float_spec.rb +1 -1
- data/spec/int_spec.rb +1 -1
- data/spec/io_spec.rb +1 -1
- data/spec/lazy_spec.rb +1 -1
- data/spec/primitive_spec.rb +1 -10
- data/spec/record_spec.rb +8 -17
- data/spec/registry_spec.rb +41 -21
- data/spec/rest_spec.rb +1 -1
- data/spec/spec_common.rb +1 -1
- data/spec/string_spec.rb +1 -9
- data/spec/stringz_spec.rb +1 -1
- data/spec/struct_spec.rb +16 -11
- data/spec/system_spec.rb +18 -3
- data/spec/wrapper_spec.rb +84 -0
- data/tasks/pkg.rake +36 -0
- data/tasks/rdoc.rake +8 -0
- data/tasks/rspec.rake +17 -0
- metadata +43 -38
- data/spec/sanitize_spec.rb +0 -95
data/spec/array_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require File.expand_path(File.dirname(__FILE__)
|
4
|
-
require File.expand_path(File.dirname(__FILE__)
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
4
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "example"))
|
5
5
|
require 'bindata/array'
|
6
6
|
require 'bindata/int'
|
7
7
|
|
@@ -135,7 +135,7 @@ describe BinData::Array, "with several elements" do
|
|
135
135
|
end
|
136
136
|
|
137
137
|
it "should have correct num_bytes for individual elements" do
|
138
|
-
@data.num_bytes
|
138
|
+
@data[0].num_bytes.should == ExampleSingle.new.num_bytes
|
139
139
|
end
|
140
140
|
|
141
141
|
it "should be able to directly access elements" do
|
@@ -163,7 +163,7 @@ describe BinData::Array, "with several elements" do
|
|
163
163
|
|
164
164
|
it "should clear a single element" do
|
165
165
|
@data[1] = 8
|
166
|
-
@data.clear
|
166
|
+
@data[1].clear
|
167
167
|
@data[1].should == 2
|
168
168
|
end
|
169
169
|
|
@@ -173,14 +173,14 @@ describe BinData::Array, "with several elements" do
|
|
173
173
|
|
174
174
|
it "should be clear if all elements are clear" do
|
175
175
|
@data[1] = 8
|
176
|
-
@data.clear
|
176
|
+
@data[1].clear
|
177
177
|
@data.should be_clear
|
178
178
|
end
|
179
179
|
|
180
180
|
it "should test clear status of individual elements" do
|
181
181
|
@data[1] = 8
|
182
|
-
@data
|
183
|
-
@data
|
182
|
+
@data[0].should be_clear
|
183
|
+
@data[1].should_not be_clear
|
184
184
|
end
|
185
185
|
|
186
186
|
it "should symmetrically read and write" do
|
@@ -269,21 +269,6 @@ describe BinData::Array, "when accessing elements" do
|
|
269
269
|
@data.length.should == 5
|
270
270
|
end
|
271
271
|
|
272
|
-
it "should not extend on clear" do
|
273
|
-
@data.clear(9)
|
274
|
-
@data.length.should == 5
|
275
|
-
end
|
276
|
-
|
277
|
-
it "should not extend on clear?" do
|
278
|
-
@data.clear?(9).should be_true
|
279
|
-
@data.length.should == 5
|
280
|
-
end
|
281
|
-
|
282
|
-
it "should not extend on num_bytes" do
|
283
|
-
@data.num_bytes(9).should == 0
|
284
|
-
@data.length.should == 5
|
285
|
-
end
|
286
|
-
|
287
272
|
it "should raise error on bad input to []" do
|
288
273
|
lambda { @data["a"] }.should raise_error(TypeError)
|
289
274
|
lambda { @data[1, "a"] }.should raise_error(TypeError)
|
data/spec/base_primitive_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require File.expand_path(File.dirname(__FILE__)
|
4
|
-
require File.expand_path(File.dirname(__FILE__)
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
4
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "example"))
|
5
5
|
require 'bindata/base_primitive'
|
6
6
|
require 'bindata/io'
|
7
7
|
|
@@ -124,6 +124,15 @@ describe ExampleSingle do
|
|
124
124
|
@data.should == 5
|
125
125
|
5.should == @data
|
126
126
|
end
|
127
|
+
|
128
|
+
it "should work as hash keys" do
|
129
|
+
hash = {5 => 17}
|
130
|
+
|
131
|
+
obj = ExampleSingle.new
|
132
|
+
obj.value = 5
|
133
|
+
|
134
|
+
hash[obj].should == 17
|
135
|
+
end
|
127
136
|
end
|
128
137
|
|
129
138
|
describe BinData::BasePrimitive, "after initialisation" do
|
data/spec/base_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require File.expand_path(File.dirname(__FILE__)
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
4
4
|
require 'bindata/base'
|
5
5
|
|
6
6
|
class BaseStub < BinData::Base
|
@@ -56,6 +56,28 @@ describe BinData::Base, "when subclassing" do
|
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
+
describe BinData::Base, "with parameters" do
|
60
|
+
it "should raise error if parameter has nil value" do
|
61
|
+
lambda { BaseStub.new(:a => nil) }.should raise_error(ArgumentError)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should raise error if parameter name is invalid" do
|
65
|
+
lambda {
|
66
|
+
eval <<-END
|
67
|
+
class InvalidParameterNameBase < BinData::Base
|
68
|
+
optional_parameter :eval # i.e. Kernel#eval
|
69
|
+
end
|
70
|
+
END
|
71
|
+
}.should raise_error(NameError)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should convert keys to symbols" do
|
75
|
+
obj = BaseStub.new('a' => 3)
|
76
|
+
obj.should have_parameter(:a)
|
77
|
+
obj.get_parameter(:a).should == 3
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
59
81
|
describe BinData::Base, "with mandatory parameters" do
|
60
82
|
before(:all) do
|
61
83
|
eval <<-END
|
@@ -139,7 +161,7 @@ describe BinData::Base, "with multiple parameters" do
|
|
139
161
|
end
|
140
162
|
|
141
163
|
it "should identify internally accepted parameters" do
|
142
|
-
accepted = WithParamBase.
|
164
|
+
accepted = WithParamBase.accepted_parameters.all
|
143
165
|
accepted.should include(:p1)
|
144
166
|
accepted.should include(:p2)
|
145
167
|
accepted.should include(:p3)
|
data/spec/bits_spec.rb
CHANGED
data/spec/choice_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require File.expand_path(File.dirname(__FILE__)
|
4
|
-
require File.expand_path(File.dirname(__FILE__)
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
4
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "example"))
|
5
5
|
require 'bindata/choice'
|
6
6
|
|
7
7
|
class Chooser
|
@@ -0,0 +1,96 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
4
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "example"))
|
5
|
+
require 'bindata'
|
6
|
+
|
7
|
+
describe BinData::SingleValue, "when defining" do
|
8
|
+
it "should allow inheriting from deprecated SingleValue" do
|
9
|
+
lambda {
|
10
|
+
eval <<-END
|
11
|
+
class SubclassSingleValue < BinData::SingleValue
|
12
|
+
end
|
13
|
+
END
|
14
|
+
}.should_not raise_error
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe BinData::MultiValue, "when defining" do
|
19
|
+
it "should allow inheriting from deprecated MultiValue" do
|
20
|
+
lambda {
|
21
|
+
eval <<-END
|
22
|
+
class SubclassMultiValue < BinData::MultiValue
|
23
|
+
end
|
24
|
+
END
|
25
|
+
}.should_not raise_error
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe BinData::Array, "with several elements" do
|
30
|
+
before(:each) do
|
31
|
+
type = [:example_single, {:initial_value => lambda { index + 1 }}]
|
32
|
+
@data = BinData::Array.new(:type => type, :initial_length => 5)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should clear a single element" do
|
36
|
+
@data[1] = 8
|
37
|
+
@data.clear(1)
|
38
|
+
@data[1].should == 2
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should test clear status of individual elements" do
|
42
|
+
@data[1] = 8
|
43
|
+
@data.clear?(0).should be_true
|
44
|
+
@data.clear?(1).should be_false
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should have correct num_bytes for individual elements" do
|
48
|
+
@data.num_bytes(0).should == ExampleSingle.new.num_bytes
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should not extend on clear" do
|
52
|
+
@data.clear(9)
|
53
|
+
@data.length.should == 5
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should not extend on clear?" do
|
57
|
+
@data.clear?(9).should be_true
|
58
|
+
@data.length.should == 5
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should not extend on num_bytes" do
|
62
|
+
@data.num_bytes(9).should == 0
|
63
|
+
@data.length.should == 5
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe BinData::String, "with deprecated parameters" do
|
68
|
+
it "should substitude :trim_padding for :trim_value" do
|
69
|
+
obj = BinData::String.new(:trim_value => true)
|
70
|
+
obj.value = "abc\0"
|
71
|
+
obj.value.should == "abc"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe BinData::Struct, "with multiple fields" do
|
76
|
+
before(:each) do
|
77
|
+
@params = { :fields => [ [:int8, :a], [:int8, :b] ] }
|
78
|
+
@obj = BinData::Struct.new(@params)
|
79
|
+
@obj.a = 1
|
80
|
+
@obj.b = 2
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should return num_bytes" do
|
84
|
+
@obj.num_bytes(:a).should == 1
|
85
|
+
@obj.num_bytes(:b).should == 1
|
86
|
+
@obj.num_bytes.should == 2
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should clear individual elements" do
|
90
|
+
@obj.a = 6
|
91
|
+
@obj.b = 7
|
92
|
+
@obj.clear(:a)
|
93
|
+
@obj.should be_clear(:a)
|
94
|
+
@obj.should_not be_clear(:b)
|
95
|
+
end
|
96
|
+
end
|
data/spec/float_spec.rb
CHANGED
data/spec/int_spec.rb
CHANGED
data/spec/io_spec.rb
CHANGED
data/spec/lazy_spec.rb
CHANGED
data/spec/primitive_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require File.expand_path(File.dirname(__FILE__)
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
4
4
|
require 'bindata'
|
5
5
|
|
6
6
|
describe BinData::Primitive, "when subclassing" do
|
@@ -23,15 +23,6 @@ describe BinData::Primitive, "when subclassing" do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
describe BinData::Primitive, "when defining" do
|
26
|
-
it "should allow inheriting from deprecated SingleValue" do
|
27
|
-
lambda {
|
28
|
-
eval <<-END
|
29
|
-
class SubclassSingleValue < BinData::SingleValue
|
30
|
-
end
|
31
|
-
END
|
32
|
-
}.should_not raise_error
|
33
|
-
end
|
34
|
-
|
35
26
|
it "should fail on non registered types" do
|
36
27
|
lambda {
|
37
28
|
eval <<-END
|
data/spec/record_spec.rb
CHANGED
@@ -1,18 +1,9 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require File.expand_path(File.dirname(__FILE__)
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
4
4
|
require 'bindata'
|
5
5
|
|
6
6
|
describe BinData::Record, "when defining" do
|
7
|
-
it "should allow inheriting from deprecated MultiValue" do
|
8
|
-
lambda {
|
9
|
-
eval <<-END
|
10
|
-
class SubclassMultiValue < BinData::MultiValue
|
11
|
-
end
|
12
|
-
END
|
13
|
-
}.should_not raise_error
|
14
|
-
end
|
15
|
-
|
16
7
|
it "should fail on non registered types" do
|
17
8
|
lambda {
|
18
9
|
eval <<-END
|
@@ -119,14 +110,14 @@ describe BinData::Record, "with multiple fields" do
|
|
119
110
|
end
|
120
111
|
|
121
112
|
it "should return num_bytes" do
|
122
|
-
@obj.
|
123
|
-
@obj.
|
113
|
+
@obj.a.num_bytes.should == 1
|
114
|
+
@obj.b.num_bytes.should == 1
|
124
115
|
@obj.num_bytes.should == 2
|
125
116
|
end
|
126
117
|
|
127
118
|
it "should identify accepted parameters" do
|
128
|
-
BinData::Record.
|
129
|
-
BinData::Record.
|
119
|
+
BinData::Record.accepted_parameters.all.should include(:hide)
|
120
|
+
BinData::Record.accepted_parameters.all.should include(:endian)
|
130
121
|
end
|
131
122
|
|
132
123
|
it "should clear" do
|
@@ -138,9 +129,9 @@ describe BinData::Record, "with multiple fields" do
|
|
138
129
|
it "should clear individual elements" do
|
139
130
|
@obj.a = 6
|
140
131
|
@obj.b = 7
|
141
|
-
@obj.clear
|
142
|
-
@obj.should be_clear
|
143
|
-
@obj.should_not be_clear
|
132
|
+
@obj.a.clear
|
133
|
+
@obj.a.should be_clear
|
134
|
+
@obj.b.should_not be_clear
|
144
135
|
end
|
145
136
|
|
146
137
|
it "should write ordered" do
|
data/spec/registry_spec.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require File.expand_path(File.dirname(__FILE__)
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
4
|
+
require 'bindata/bits'
|
5
|
+
require 'bindata/int'
|
4
6
|
require 'bindata/registry'
|
5
7
|
|
6
8
|
describe BinData::Registry do
|
@@ -57,32 +59,50 @@ describe BinData::Registry do
|
|
57
59
|
end
|
58
60
|
|
59
61
|
it "should lookup integers with endian" do
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
62
|
+
@r.register("Int24be", A)
|
63
|
+
@r.register("Int24le", B)
|
64
|
+
@r.register("Uint24be", C)
|
65
|
+
@r.register("Uint24le", D)
|
66
|
+
|
67
|
+
@r.lookup("int24", :big).should == A
|
68
|
+
@r.lookup("int24", :little).should == B
|
69
|
+
@r.lookup("uint24", :big).should == C
|
70
|
+
@r.lookup("uint24", :little).should == D
|
69
71
|
end
|
70
72
|
|
71
73
|
it "should not lookup integers without endian" do
|
72
|
-
|
74
|
+
@r.register("Int24be", A)
|
73
75
|
|
74
|
-
|
76
|
+
@r.lookup("int24").should be_nil
|
75
77
|
end
|
76
78
|
|
77
79
|
it "should lookup floats with endian" do
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
80
|
+
@r.register("FloatBe", A)
|
81
|
+
@r.register("FloatLe", B)
|
82
|
+
@r.register("DoubleBe", C)
|
83
|
+
@r.register("DoubleLe", D)
|
84
|
+
|
85
|
+
@r.lookup("float", :big).should == A
|
86
|
+
@r.lookup("float", :little).should == B
|
87
|
+
@r.lookup("double", :big).should == C
|
88
|
+
@r.lookup("double", :little).should == D
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should automatically create classes for integers" do
|
92
|
+
BinData.const_defined?(:Uint40be).should be_false
|
93
|
+
@r.lookup("uint40be")
|
94
|
+
BinData.const_defined?(:Uint40be).should be_true
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should automatically create classes for big endian bits" do
|
98
|
+
BinData.const_defined?(:Bit801).should be_false
|
99
|
+
@r.lookup("bit801")
|
100
|
+
BinData.const_defined?(:Bit801).should be_true
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should automatically create classes for little endian bits" do
|
104
|
+
BinData.const_defined?(:Bit802le).should be_false
|
105
|
+
@r.lookup("bit802le")
|
106
|
+
BinData.const_defined?(:Bit802le).should be_true
|
87
107
|
end
|
88
108
|
end
|