bindata 2.3.3 → 2.3.4
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.
- checksums.yaml +4 -4
- data/ChangeLog.rdoc +4 -0
- data/Rakefile +2 -2
- data/examples/gzip.rb +24 -24
- data/examples/ip_address.rb +3 -4
- data/examples/list.rb +20 -20
- data/examples/nbt.rb +14 -14
- data/examples/tcp_ip.rb +12 -14
- data/lib/bindata/alignment.rb +2 -2
- data/lib/bindata/array.rb +22 -23
- data/lib/bindata/base.rb +13 -12
- data/lib/bindata/base_primitive.rb +20 -17
- data/lib/bindata/bits.rb +4 -4
- data/lib/bindata/buffer.rb +5 -5
- data/lib/bindata/choice.rb +11 -13
- data/lib/bindata/count_bytes_remaining.rb +1 -2
- data/lib/bindata/delayed_io.rb +11 -11
- data/lib/bindata/dsl.rb +57 -64
- data/lib/bindata/float.rb +16 -13
- data/lib/bindata/int.rb +6 -6
- data/lib/bindata/io.rb +16 -16
- data/lib/bindata/lazy.rb +3 -3
- data/lib/bindata/name.rb +2 -2
- data/lib/bindata/offset.rb +3 -3
- data/lib/bindata/params.rb +13 -15
- data/lib/bindata/primitive.rb +3 -3
- data/lib/bindata/registry.rb +12 -12
- data/lib/bindata/rest.rb +1 -2
- data/lib/bindata/sanitize.rb +17 -18
- data/lib/bindata/skip.rb +7 -8
- data/lib/bindata/string.rb +6 -6
- data/lib/bindata/stringz.rb +3 -3
- data/lib/bindata/struct.rb +16 -16
- data/lib/bindata/trace.rb +9 -8
- data/lib/bindata/version.rb +1 -1
- data/lib/bindata/virtual.rb +3 -3
- data/lib/bindata/warnings.rb +6 -2
- data/test/alignment_test.rb +4 -4
- data/test/array_test.rb +29 -29
- data/test/base_primitive_test.rb +17 -17
- data/test/base_test.rb +6 -6
- data/test/bits_test.rb +1 -1
- data/test/buffer_test.rb +19 -19
- data/test/choice_test.rb +20 -20
- data/test/count_bytes_remaining_test.rb +1 -1
- data/test/delayed_io_test.rb +26 -26
- data/test/lazy_test.rb +16 -16
- data/test/offset_test.rb +8 -8
- data/test/params_test.rb +11 -11
- data/test/primitive_test.rb +11 -11
- data/test/record_test.rb +50 -50
- data/test/registry_test.rb +26 -26
- data/test/rest_test.rb +1 -1
- data/test/skip_test.rb +27 -27
- data/test/string_test.rb +24 -24
- data/test/stringz_test.rb +1 -1
- data/test/struct_test.rb +76 -75
- data/test/system_test.rb +51 -51
- data/test/virtual_test.rb +3 -3
- metadata +2 -2
data/test/registry_test.rb
CHANGED
@@ -59,10 +59,10 @@ describe BinData::Registry, "with numerics" do
|
|
59
59
|
let(:r) { BinData::RegisteredClasses }
|
60
60
|
|
61
61
|
it "lookup integers with endian" do
|
62
|
-
r.lookup("int24", {:
|
63
|
-
r.lookup("int24", {:
|
64
|
-
r.lookup("uint24", {:
|
65
|
-
r.lookup("uint24", {:
|
62
|
+
r.lookup("int24", {endian: :big}).to_s.must_equal "BinData::Int24be"
|
63
|
+
r.lookup("int24", {endian: :little}).to_s.must_equal "BinData::Int24le"
|
64
|
+
r.lookup("uint24", {endian: :big}).to_s.must_equal "BinData::Uint24be"
|
65
|
+
r.lookup("uint24", {endian: :little}).to_s.must_equal "BinData::Uint24le"
|
66
66
|
end
|
67
67
|
|
68
68
|
it "does not lookup integers without endian" do
|
@@ -76,18 +76,18 @@ describe BinData::Registry, "with numerics" do
|
|
76
76
|
r.lookup("int3")
|
77
77
|
}.must_raise BinData::UnRegisteredTypeError
|
78
78
|
lambda {
|
79
|
-
r.lookup("int3", {:
|
79
|
+
r.lookup("int3", {endian: :big})
|
80
80
|
}.must_raise BinData::UnRegisteredTypeError
|
81
81
|
lambda {
|
82
|
-
r.lookup("int3", {:
|
82
|
+
r.lookup("int3", {endian: :little})
|
83
83
|
}.must_raise BinData::UnRegisteredTypeError
|
84
84
|
end
|
85
85
|
|
86
86
|
it "lookup floats with endian" do
|
87
|
-
r.lookup("float", {:
|
88
|
-
r.lookup("float", {:
|
89
|
-
r.lookup("double", {:
|
90
|
-
r.lookup("double", {:
|
87
|
+
r.lookup("float", {endian: :big}).to_s.must_equal "BinData::FloatBe"
|
88
|
+
r.lookup("float", {endian: :little}).to_s.must_equal "BinData::FloatLe"
|
89
|
+
r.lookup("double", {endian: :big}).to_s.must_equal "BinData::DoubleBe"
|
90
|
+
r.lookup("double", {endian: :little}).to_s.must_equal "BinData::DoubleLe"
|
91
91
|
end
|
92
92
|
|
93
93
|
it "lookup bits" do
|
@@ -97,17 +97,17 @@ describe BinData::Registry, "with numerics" do
|
|
97
97
|
end
|
98
98
|
|
99
99
|
it "lookup bits by ignoring endian" do
|
100
|
-
r.lookup("bit2", {:
|
101
|
-
r.lookup("bit3le", {:
|
102
|
-
r.lookup("bit2", {:
|
103
|
-
r.lookup("bit3le", {:
|
100
|
+
r.lookup("bit2", {endian: :big}).to_s.must_equal "BinData::Bit2"
|
101
|
+
r.lookup("bit3le", {endian: :big}).to_s.must_equal "BinData::Bit3le"
|
102
|
+
r.lookup("bit2", {endian: :little}).to_s.must_equal "BinData::Bit2"
|
103
|
+
r.lookup("bit3le", {endian: :little}).to_s.must_equal "BinData::Bit3le"
|
104
104
|
end
|
105
105
|
|
106
106
|
it "lookup signed bits by ignoring endian" do
|
107
|
-
r.lookup("sbit2", {:
|
108
|
-
r.lookup("sbit3le", {:
|
109
|
-
r.lookup("sbit2", {:
|
110
|
-
r.lookup("sbit3le", {:
|
107
|
+
r.lookup("sbit2", {endian: :big}).to_s.must_equal "BinData::Sbit2"
|
108
|
+
r.lookup("sbit3le", {endian: :big}).to_s.must_equal "BinData::Sbit3le"
|
109
|
+
r.lookup("sbit2", {endian: :little}).to_s.must_equal "BinData::Sbit2"
|
110
|
+
r.lookup("sbit3le", {endian: :little}).to_s.must_equal "BinData::Sbit3le"
|
111
111
|
end
|
112
112
|
end
|
113
113
|
|
@@ -120,16 +120,16 @@ describe BinData::Registry, "with endian specific types" do
|
|
120
120
|
end
|
121
121
|
|
122
122
|
it "lookup little endian types" do
|
123
|
-
r.lookup('a', {:
|
123
|
+
r.lookup('a', {endian: :little}).must_equal A
|
124
124
|
end
|
125
125
|
|
126
126
|
it "lookup big endian types" do
|
127
|
-
r.lookup('b', {:
|
127
|
+
r.lookup('b', {endian: :big}).must_equal B
|
128
128
|
end
|
129
129
|
|
130
130
|
it "does not lookup types with non existent endian" do
|
131
131
|
lambda {
|
132
|
-
r.lookup('a', {:
|
132
|
+
r.lookup('a', {endian: :big})
|
133
133
|
}.must_raise BinData::UnRegisteredTypeError
|
134
134
|
end
|
135
135
|
|
@@ -137,7 +137,7 @@ describe BinData::Registry, "with endian specific types" do
|
|
137
137
|
r.register('c', C)
|
138
138
|
r.register('c_le', D)
|
139
139
|
|
140
|
-
r.lookup('c', {:
|
140
|
+
r.lookup('c', {endian: :little}).must_equal C
|
141
141
|
end
|
142
142
|
end
|
143
143
|
|
@@ -150,18 +150,18 @@ describe BinData::Registry, "with search_prefix" do
|
|
150
150
|
end
|
151
151
|
|
152
152
|
it "lookup single search_prefix" do
|
153
|
-
r.lookup('f', {:
|
153
|
+
r.lookup('f', {search_prefix: :a}).must_equal A
|
154
154
|
end
|
155
155
|
|
156
156
|
it "lookup single search_prefix with endian" do
|
157
|
-
r.lookup('f', {:
|
157
|
+
r.lookup('f', {search_prefix: :a, endian: :little}).must_equal A
|
158
158
|
end
|
159
159
|
|
160
160
|
it "lookup multiple search_prefix" do
|
161
|
-
r.lookup('f', {:
|
161
|
+
r.lookup('f', {search_prefix: [:x, :a]}).must_equal A
|
162
162
|
end
|
163
163
|
|
164
164
|
it "lookup first match in search_prefix" do
|
165
|
-
r.lookup('f', {:
|
165
|
+
r.lookup('f', {search_prefix: [:a, :b]}).must_equal A
|
166
166
|
end
|
167
167
|
end
|
data/test/rest_test.rb
CHANGED
data/test/skip_test.rb
CHANGED
@@ -12,7 +12,7 @@ describe BinData::Skip, "when instantiating" do
|
|
12
12
|
end
|
13
13
|
|
14
14
|
describe BinData::Skip, "with :length" do
|
15
|
-
let(:obj) { BinData::Skip.new(:
|
15
|
+
let(:obj) { BinData::Skip.new(length: 5) }
|
16
16
|
let(:io) { StringIO.new("abcdefghij") }
|
17
17
|
|
18
18
|
it "initial state" do
|
@@ -37,28 +37,28 @@ describe BinData::Skip, "with :length" do
|
|
37
37
|
end
|
38
38
|
|
39
39
|
describe BinData::Skip, "with :to_abs_offset" do
|
40
|
-
BinData::Struct.new(:
|
40
|
+
BinData::Struct.new(fields: [ [:skip, :f, { to_abs_offset: 5 } ] ])
|
41
41
|
|
42
|
-
let(:skip_obj) { [:skip, :f, { :
|
42
|
+
let(:skip_obj) { [:skip, :f, { to_abs_offset: 5 } ] }
|
43
43
|
let(:io) { StringIO.new("abcdefghij") }
|
44
44
|
|
45
45
|
it "reads skipping forward" do
|
46
46
|
fields = [ skip_obj ]
|
47
|
-
obj = BinData::Struct.new(:
|
47
|
+
obj = BinData::Struct.new(fields: fields)
|
48
48
|
obj.read(io)
|
49
49
|
io.pos.must_equal 5
|
50
50
|
end
|
51
51
|
|
52
52
|
it "reads skipping in place" do
|
53
|
-
fields = [ [:string, :a, { :
|
54
|
-
obj = BinData::Struct.new(:
|
53
|
+
fields = [ [:string, :a, { read_length: 5 }], skip_obj ]
|
54
|
+
obj = BinData::Struct.new(fields: fields)
|
55
55
|
obj.read(io)
|
56
56
|
io.pos.must_equal 5
|
57
57
|
end
|
58
58
|
|
59
59
|
it "does not read skipping backwards" do
|
60
|
-
fields = [ [:string, :a, { :
|
61
|
-
obj = BinData::Struct.new(:
|
60
|
+
fields = [ [:string, :a, { read_length: 10 }], skip_obj ]
|
61
|
+
obj = BinData::Struct.new(fields: fields)
|
62
62
|
|
63
63
|
lambda {
|
64
64
|
obj.read(io)
|
@@ -67,19 +67,19 @@ describe BinData::Skip, "with :to_abs_offset" do
|
|
67
67
|
|
68
68
|
it "writes skipping forward" do
|
69
69
|
fields = [ skip_obj ]
|
70
|
-
obj = BinData::Struct.new(:
|
70
|
+
obj = BinData::Struct.new(fields: fields)
|
71
71
|
obj.to_binary_s.must_equal "\000\000\000\000\000"
|
72
72
|
end
|
73
73
|
|
74
74
|
it "reads skipping in place" do
|
75
|
-
fields = [ [:string, :a, { :
|
76
|
-
obj = BinData::Struct.new(:
|
75
|
+
fields = [ [:string, :a, { value: "abcde" }], skip_obj ]
|
76
|
+
obj = BinData::Struct.new(fields: fields)
|
77
77
|
obj.to_binary_s.must_equal "abcde"
|
78
78
|
end
|
79
79
|
|
80
80
|
it "does not write skipping backwards" do
|
81
|
-
fields = [ [:string, :a, { :
|
82
|
-
obj = BinData::Struct.new(:
|
81
|
+
fields = [ [:string, :a, { value: "abcdefghij" }], skip_obj ]
|
82
|
+
obj = BinData::Struct.new(fields: fields)
|
83
83
|
lambda {
|
84
84
|
obj.to_binary_s
|
85
85
|
}.must_raise BinData::ValidityError
|
@@ -90,34 +90,34 @@ describe BinData::Skip, "with :until_valid" do
|
|
90
90
|
let(:io) { StringIO.new("abcdefghij") }
|
91
91
|
|
92
92
|
it "skips to valid match" do
|
93
|
-
skip_obj = [:string, { :
|
94
|
-
fields = [ [:skip, :s, { :
|
95
|
-
obj = BinData::Struct.new(:
|
93
|
+
skip_obj = [:string, { read_length: 1, assert: "f" }]
|
94
|
+
fields = [ [:skip, :s, { until_valid: skip_obj }] ]
|
95
|
+
obj = BinData::Struct.new(fields: fields)
|
96
96
|
obj.read(io)
|
97
97
|
io.pos.must_equal 5
|
98
98
|
end
|
99
99
|
|
100
100
|
it "doesn't skip when validator doesn't assert" do
|
101
|
-
skip_obj = [:string, { :
|
102
|
-
fields = [ [:skip, :s, { :
|
103
|
-
obj = BinData::Struct.new(:
|
101
|
+
skip_obj = [:string, { read_length: 1 }]
|
102
|
+
fields = [ [:skip, :s, { until_valid: skip_obj }] ]
|
103
|
+
obj = BinData::Struct.new(fields: fields)
|
104
104
|
obj.read(io)
|
105
105
|
io.pos.must_equal 0
|
106
106
|
end
|
107
107
|
|
108
108
|
it "raises EOFError when no match" do
|
109
|
-
skip_obj = [:string, { :
|
110
|
-
fields = [ [:skip, :s, { :
|
111
|
-
obj = BinData::Struct.new(:
|
109
|
+
skip_obj = [:string, { read_length: 1, assert: "X" }]
|
110
|
+
fields = [ [:skip, :s, { until_valid: skip_obj }] ]
|
111
|
+
obj = BinData::Struct.new(fields: fields)
|
112
112
|
lambda {
|
113
113
|
obj.read(io)
|
114
114
|
}.must_raise EOFError
|
115
115
|
end
|
116
116
|
|
117
117
|
it "raises IOError when validator reads beyond stream" do
|
118
|
-
skip_obj = [:string, { :
|
119
|
-
fields = [ [:skip, :s, { :
|
120
|
-
obj = BinData::Struct.new(:
|
118
|
+
skip_obj = [:string, { read_length: 30 }]
|
119
|
+
fields = [ [:skip, :s, { until_valid: skip_obj }] ]
|
120
|
+
obj = BinData::Struct.new(fields: fields)
|
121
121
|
lambda {
|
122
122
|
obj.read(io)
|
123
123
|
}.must_raise IOError
|
@@ -125,9 +125,9 @@ describe BinData::Skip, "with :until_valid" do
|
|
125
125
|
|
126
126
|
class DSLSkip < BinData::Record
|
127
127
|
skip :s do
|
128
|
-
string :
|
128
|
+
string read_length: 1, assert: "f"
|
129
129
|
end
|
130
|
-
string :a, :
|
130
|
+
string :a, read_length: 1
|
131
131
|
end
|
132
132
|
|
133
133
|
it "uses block form" do
|
data/test/string_test.rb
CHANGED
@@ -4,24 +4,24 @@ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
|
|
4
4
|
|
5
5
|
describe BinData::String, "with mutually exclusive parameters" do
|
6
6
|
it ":value and :initial_value" do
|
7
|
-
params = {:
|
7
|
+
params = {value: "", initial_value: ""}
|
8
8
|
lambda { BinData::String.new(params) }.must_raise ArgumentError
|
9
9
|
end
|
10
10
|
|
11
11
|
it ":length and :read_length" do
|
12
|
-
params = {:
|
12
|
+
params = {length: 5, read_length: 5}
|
13
13
|
lambda { BinData::String.new(params) }.must_raise ArgumentError
|
14
14
|
end
|
15
15
|
|
16
16
|
it ":value and :length" do
|
17
|
-
params = {:
|
17
|
+
params = {value: "", length: 5}
|
18
18
|
lambda { BinData::String.new(params) }.must_raise ArgumentError
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
22
|
describe BinData::String, "when assigning" do
|
23
|
-
let(:small) { BinData::String.new(:
|
24
|
-
let(:large) { BinData::String.new(:
|
23
|
+
let(:small) { BinData::String.new(length: 3, pad_byte: "A") }
|
24
|
+
let(:large) { BinData::String.new(length: 5, pad_byte: "B") }
|
25
25
|
|
26
26
|
it "copies data from small to large" do
|
27
27
|
large.assign(small)
|
@@ -47,7 +47,7 @@ describe BinData::String do
|
|
47
47
|
end
|
48
48
|
|
49
49
|
describe BinData::String, "with :read_length" do
|
50
|
-
let(:obj) { BinData::String.new(:
|
50
|
+
let(:obj) { BinData::String.new(read_length: 5) }
|
51
51
|
|
52
52
|
specify { obj.num_bytes.must_equal 0 }
|
53
53
|
specify { obj.value.must_equal "" }
|
@@ -68,7 +68,7 @@ describe BinData::String, "with :read_length" do
|
|
68
68
|
end
|
69
69
|
|
70
70
|
describe BinData::String, "with :length" do
|
71
|
-
let(:obj) { BinData::String.new(:
|
71
|
+
let(:obj) { BinData::String.new(length: 5) }
|
72
72
|
|
73
73
|
specify { obj.num_bytes.must_equal 5 }
|
74
74
|
specify { obj.value.must_equal "\0\0\0\0\0" }
|
@@ -100,7 +100,7 @@ describe BinData::String, "with :length" do
|
|
100
100
|
end
|
101
101
|
|
102
102
|
describe BinData::String, "with :read_length and :initial_value" do
|
103
|
-
let(:obj) { BinData::String.new(:
|
103
|
+
let(:obj) { BinData::String.new(read_length: 5, initial_value: "abcdefghij") }
|
104
104
|
|
105
105
|
specify { obj.num_bytes.must_equal 10 }
|
106
106
|
specify { obj.value.must_equal "abcdefghij" }
|
@@ -119,7 +119,7 @@ describe BinData::String, "with :read_length and :initial_value" do
|
|
119
119
|
end
|
120
120
|
|
121
121
|
describe BinData::String, "with :read_length and :value" do
|
122
|
-
let(:obj) { BinData::String.new(:
|
122
|
+
let(:obj) { BinData::String.new(read_length: 5, value: "abcdefghij") }
|
123
123
|
|
124
124
|
specify { obj.num_bytes.must_equal 10 }
|
125
125
|
specify { obj.value.must_equal "abcdefghij" }
|
@@ -149,7 +149,7 @@ describe BinData::String, "with :read_length and :value" do
|
|
149
149
|
end
|
150
150
|
|
151
151
|
describe BinData::String, "with :length and :initial_value" do
|
152
|
-
let(:obj) { BinData::String.new(:
|
152
|
+
let(:obj) { BinData::String.new(length: 5, initial_value: "abcdefghij") }
|
153
153
|
|
154
154
|
specify { obj.num_bytes.must_equal 5 }
|
155
155
|
specify { obj.value.must_equal "abcde" }
|
@@ -165,27 +165,27 @@ end
|
|
165
165
|
|
166
166
|
describe BinData::String, "with :pad_byte" do
|
167
167
|
it "accepts a numeric value for :pad_byte" do
|
168
|
-
str = BinData::String.new(:
|
168
|
+
str = BinData::String.new(length: 5, pad_byte: 6)
|
169
169
|
str.assign("abc")
|
170
170
|
str.must_equal "abc\x06\x06"
|
171
171
|
end
|
172
172
|
|
173
173
|
it "accepts a character for :pad_byte" do
|
174
|
-
str = BinData::String.new(:
|
174
|
+
str = BinData::String.new(length: 5, pad_byte: "R")
|
175
175
|
str.assign("abc")
|
176
176
|
str.must_equal "abcRR"
|
177
177
|
end
|
178
178
|
|
179
179
|
it "does not accept a string for :pad_byte" do
|
180
|
-
params = {:
|
180
|
+
params = {length: 5, pad_byte: "RR"}
|
181
181
|
lambda { BinData::String.new(params) }.must_raise ArgumentError
|
182
182
|
end
|
183
183
|
end
|
184
184
|
|
185
185
|
describe BinData::String, "with :trim_padding" do
|
186
186
|
it "set false is the default" do
|
187
|
-
str1 = BinData::String.new(:
|
188
|
-
str2 = BinData::String.new(:
|
187
|
+
str1 = BinData::String.new(length: 5)
|
188
|
+
str2 = BinData::String.new(length: 5, trim_padding: false)
|
189
189
|
str1.assign("abc")
|
190
190
|
str2.assign("abc")
|
191
191
|
str1.must_equal "abc\0\0"
|
@@ -193,7 +193,7 @@ describe BinData::String, "with :trim_padding" do
|
|
193
193
|
end
|
194
194
|
|
195
195
|
describe "trim padding set" do
|
196
|
-
let(:obj) { BinData::String.new(:
|
196
|
+
let(:obj) { BinData::String.new(pad_byte: 'R', trim_padding: true) }
|
197
197
|
|
198
198
|
it "trims the value" do
|
199
199
|
obj.assign("abcRR")
|
@@ -219,8 +219,8 @@ end
|
|
219
219
|
|
220
220
|
describe BinData::String, "with :pad_front" do
|
221
221
|
it "set false is the default" do
|
222
|
-
str1 = BinData::String.new(:
|
223
|
-
str2 = BinData::String.new(:
|
222
|
+
str1 = BinData::String.new(length: 5)
|
223
|
+
str2 = BinData::String.new(length: 5, pad_front: false)
|
224
224
|
str1.assign("abc")
|
225
225
|
str2.assign("abc")
|
226
226
|
str1.must_equal "abc\0\0"
|
@@ -228,19 +228,19 @@ describe BinData::String, "with :pad_front" do
|
|
228
228
|
end
|
229
229
|
|
230
230
|
it "pads to the front" do
|
231
|
-
str = BinData::String.new(:
|
231
|
+
str = BinData::String.new(length: 5, pad_byte: 'R', pad_front: true)
|
232
232
|
str.assign("abc")
|
233
233
|
str.must_equal "RRabc"
|
234
234
|
end
|
235
235
|
|
236
236
|
it "can alternatively be accesses by :pad_left" do
|
237
|
-
str = BinData::String.new(:
|
237
|
+
str = BinData::String.new(length: 5, pad_byte: 'R', pad_left: true)
|
238
238
|
str.assign("abc")
|
239
239
|
str.must_equal "RRabc"
|
240
240
|
end
|
241
241
|
|
242
242
|
describe "and :trim_padding" do
|
243
|
-
let(:obj) { BinData::String.new(:
|
243
|
+
let(:obj) { BinData::String.new(length: 5, pad_byte: 'R', pad_front: true, trim_padding: true) }
|
244
244
|
|
245
245
|
it "assigns" do
|
246
246
|
obj.assign("abc")
|
@@ -276,7 +276,7 @@ describe BinData::String, "with Ruby 1.9 encodings" do
|
|
276
276
|
end
|
277
277
|
|
278
278
|
it "stores read values as binary" do
|
279
|
-
obj = UTF8String.new(:
|
279
|
+
obj = UTF8String.new(read_length: binary_str.bytesize)
|
280
280
|
obj.read(binary_str)
|
281
281
|
|
282
282
|
obj.to_binary_s.must_equal_binary binary_str
|
@@ -297,14 +297,14 @@ end
|
|
297
297
|
|
298
298
|
describe BinData::String, "warnings" do
|
299
299
|
it "warns if has :asserted_value but no :length" do
|
300
|
-
obj = BinData::String.new(:
|
300
|
+
obj = BinData::String.new(asserted_value: "ABC")
|
301
301
|
obj.must_warn "obj does not have a :read_length parameter - returning empty string" do
|
302
302
|
lambda { obj.read("abcde") }.must_raise BinData::ValidityError
|
303
303
|
end
|
304
304
|
end
|
305
305
|
|
306
306
|
it "warns if has :value but no :read_length" do
|
307
|
-
obj = BinData::String.new(:
|
307
|
+
obj = BinData::String.new(value: "ABC")
|
308
308
|
obj.must_warn "obj does not have a :read_length parameter - returning empty string" do
|
309
309
|
obj.read("abcde")
|
310
310
|
end
|
data/test/stringz_test.rb
CHANGED
@@ -74,7 +74,7 @@ describe BinData::Stringz, "when setting the value" do
|
|
74
74
|
end
|
75
75
|
|
76
76
|
describe BinData::Stringz, "with max_length" do
|
77
|
-
let(:obj) { BinData::Stringz.new(:
|
77
|
+
let(:obj) { BinData::Stringz.new(max_length: 5) }
|
78
78
|
|
79
79
|
it "reads less than max_length" do
|
80
80
|
io = StringIO.new("abc\0xyz")
|
data/test/struct_test.rb
CHANGED
@@ -4,14 +4,14 @@ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
|
|
4
4
|
|
5
5
|
describe BinData::Struct, "when initializing" do
|
6
6
|
it "fails on non registered types" do
|
7
|
-
params = {:
|
7
|
+
params = {fields: [[:non_registered_type, :a]]}
|
8
8
|
lambda {
|
9
9
|
BinData::Struct.new(params)
|
10
10
|
}.must_raise BinData::UnRegisteredTypeError
|
11
11
|
end
|
12
12
|
|
13
13
|
it "fails on duplicate names" do
|
14
|
-
params = {:
|
14
|
+
params = {fields: [[:int8, :a], [:int8, :b], [:int8, :a]]}
|
15
15
|
lambda {
|
16
16
|
BinData::Struct.new(params)
|
17
17
|
}.must_raise NameError
|
@@ -19,21 +19,21 @@ describe BinData::Struct, "when initializing" do
|
|
19
19
|
|
20
20
|
it "fails on reserved names" do
|
21
21
|
# note that #invert is from Hash.instance_methods
|
22
|
-
params = {:
|
22
|
+
params = {fields: [[:int8, :a], [:int8, :invert]]}
|
23
23
|
lambda {
|
24
24
|
BinData::Struct.new(params)
|
25
25
|
}.must_raise NameError
|
26
26
|
end
|
27
27
|
|
28
28
|
it "fails when field name shadows an existing method" do
|
29
|
-
params = {:
|
29
|
+
params = {fields: [[:int8, :object_id]]}
|
30
30
|
lambda {
|
31
31
|
BinData::Struct.new(params)
|
32
32
|
}.must_raise NameError
|
33
33
|
end
|
34
34
|
|
35
35
|
it "fails on unknown endian" do
|
36
|
-
params = {:
|
36
|
+
params = {endian: 'bad value', fields: []}
|
37
37
|
lambda {
|
38
38
|
BinData::Struct.new(params)
|
39
39
|
}.must_raise ArgumentError
|
@@ -42,10 +42,10 @@ end
|
|
42
42
|
|
43
43
|
describe BinData::Struct, "with anonymous fields" do
|
44
44
|
let(:obj) {
|
45
|
-
params = { :
|
46
|
-
[:int8, :a, {:
|
45
|
+
params = { fields: [
|
46
|
+
[:int8, :a, {initial_value: 5}],
|
47
47
|
[:int8, nil],
|
48
|
-
[:int8, '', {:
|
48
|
+
[:int8, '', {value: :a}]
|
49
49
|
] }
|
50
50
|
BinData::Struct.new(params)
|
51
51
|
}
|
@@ -56,7 +56,7 @@ describe BinData::Struct, "with anonymous fields" do
|
|
56
56
|
|
57
57
|
it "does not include anonymous fields in snapshot" do
|
58
58
|
obj.a = 5
|
59
|
-
obj.snapshot.must_equal({:
|
59
|
+
obj.snapshot.must_equal({a: 5})
|
60
60
|
end
|
61
61
|
|
62
62
|
it "writes anonymous fields" do
|
@@ -68,12 +68,12 @@ end
|
|
68
68
|
|
69
69
|
describe BinData::Struct, "with hidden fields" do
|
70
70
|
let(:obj) {
|
71
|
-
params = { :
|
72
|
-
:
|
71
|
+
params = { hide: [:b, :c],
|
72
|
+
fields: [
|
73
73
|
[:int8, :a],
|
74
|
-
[:int8, 'b', {:
|
74
|
+
[:int8, 'b', {initial_value: 5}],
|
75
75
|
[:int8, :c],
|
76
|
-
[:int8, :d, {:
|
76
|
+
[:int8, :d, {value: :b}]] }
|
77
77
|
BinData::Struct.new(params)
|
78
78
|
}
|
79
79
|
|
@@ -95,7 +95,7 @@ describe BinData::Struct, "with hidden fields" do
|
|
95
95
|
|
96
96
|
it "does not include hidden fields in snapshot" do
|
97
97
|
obj.b = 7
|
98
|
-
obj.snapshot.must_equal({:
|
98
|
+
obj.snapshot.must_equal({a: 0, d: 7})
|
99
99
|
end
|
100
100
|
|
101
101
|
it "detects hidden fields with has_key?" do
|
@@ -104,8 +104,8 @@ describe BinData::Struct, "with hidden fields" do
|
|
104
104
|
end
|
105
105
|
|
106
106
|
describe BinData::Struct, "with multiple fields" do
|
107
|
-
let(:params) { { :
|
108
|
-
let(:obj) { BinData::Struct.new({:
|
107
|
+
let(:params) { { fields: [ [:int8, :a], [:int8, :b] ] } }
|
108
|
+
let(:obj) { BinData::Struct.new({a: 1, b: 2}, params) }
|
109
109
|
|
110
110
|
specify { obj.field_names.must_equal [:a, :b] }
|
111
111
|
specify { obj.to_binary_s.must_equal_binary "\x01\x02" }
|
@@ -165,17 +165,17 @@ describe BinData::Struct, "with multiple fields" do
|
|
165
165
|
assert snap.respond_to?(:a)
|
166
166
|
snap.a.must_equal 1
|
167
167
|
snap.b.must_equal 2
|
168
|
-
snap.must_equal({ :
|
168
|
+
snap.must_equal({ a: 1, b: 2 })
|
169
169
|
end
|
170
170
|
|
171
171
|
it "assigns from partial hash" do
|
172
|
-
obj.assign(:
|
172
|
+
obj.assign(a: 3)
|
173
173
|
obj.a.must_equal 3
|
174
174
|
obj.b.must_equal 0
|
175
175
|
end
|
176
176
|
|
177
177
|
it "assigns from hash" do
|
178
|
-
obj.assign(:
|
178
|
+
obj.assign(a: 3, b: 4)
|
179
179
|
obj.a.must_equal 3
|
180
180
|
obj.b.must_equal 4
|
181
181
|
end
|
@@ -230,16 +230,16 @@ end
|
|
230
230
|
|
231
231
|
describe BinData::Struct, "with nested structs" do
|
232
232
|
let(:obj) {
|
233
|
-
inner1 = [ [:int8, :w, {:
|
234
|
-
[:int8, :x, {:
|
233
|
+
inner1 = [ [:int8, :w, {initial_value: 3}],
|
234
|
+
[:int8, :x, {value: :the_val}] ]
|
235
235
|
|
236
|
-
inner2 = [ [:int8, :y, {:
|
236
|
+
inner2 = [ [:int8, :y, {value: -> { parent.b.w }}],
|
237
237
|
[:int8, :z] ]
|
238
238
|
|
239
|
-
params = { :
|
240
|
-
[:int8, :a, {:
|
241
|
-
[:struct, :b, {:
|
242
|
-
[:struct, :c, {:
|
239
|
+
params = { fields: [
|
240
|
+
[:int8, :a, {initial_value: 6}],
|
241
|
+
[:struct, :b, {fields: inner1, the_val: :a}],
|
242
|
+
[:struct, :c, {fields: inner2}]] }
|
243
243
|
BinData::Struct.new(params)
|
244
244
|
}
|
245
245
|
|
@@ -269,22 +269,23 @@ end
|
|
269
269
|
|
270
270
|
describe BinData::Struct, "with an endian defined" do
|
271
271
|
let(:obj) {
|
272
|
-
BinData::Struct.new(:
|
273
|
-
:
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
272
|
+
BinData::Struct.new(endian: :little,
|
273
|
+
fields: [
|
274
|
+
[:uint16, :a],
|
275
|
+
[:float, :b],
|
276
|
+
[:array, :c,
|
277
|
+
{type: :int8, initial_length: 2}],
|
278
|
+
[:choice, :d,
|
279
|
+
{choices: [[:uint16], [:uint32]],
|
280
|
+
selection: 1}],
|
281
|
+
[:struct, :e,
|
282
|
+
{fields: [[:uint16, :f],
|
283
|
+
[:uint32be, :g]]}],
|
284
|
+
[:struct, :h,
|
285
|
+
{fields: [
|
286
|
+
[:struct, :i,
|
287
|
+
{fields: [[:uint16, :j]]}]]}]
|
288
|
+
])
|
288
289
|
}
|
289
290
|
|
290
291
|
it "uses correct endian" do
|
@@ -305,8 +306,8 @@ end
|
|
305
306
|
|
306
307
|
describe BinData::Struct, "with bit fields" do
|
307
308
|
let(:obj) {
|
308
|
-
params = { :
|
309
|
-
BinData::Struct.new({:
|
309
|
+
params = { fields: [ [:bit1le, :a], [:bit2le, :b], [:uint8, :c], [:bit1le, :d] ] }
|
310
|
+
BinData::Struct.new({a: 1, b: 2, c: 3, d: 1}, params)
|
310
311
|
}
|
311
312
|
|
312
313
|
specify { obj.num_bytes.must_equal 3 }
|
@@ -331,12 +332,12 @@ end
|
|
331
332
|
|
332
333
|
describe BinData::Struct, "with nested endian" do
|
333
334
|
it "uses correct endian" do
|
334
|
-
nested_params = { :
|
335
|
-
:
|
336
|
-
params = { :
|
337
|
-
:
|
338
|
-
|
339
|
-
|
335
|
+
nested_params = { endian: :little,
|
336
|
+
fields: [[:int16, :b], [:int16, :c]] }
|
337
|
+
params = { endian: :big,
|
338
|
+
fields: [[:int16, :a],
|
339
|
+
[:struct, :s, nested_params],
|
340
|
+
[:int16, :d]] }
|
340
341
|
obj = BinData::Struct.new(params)
|
341
342
|
obj.read("\x00\x01\x02\x00\x03\x00\x00\x04")
|
342
343
|
|
@@ -352,57 +353,57 @@ describe BinData::Struct, "with a search_prefix" do
|
|
352
353
|
class BShort < BinData::Uint8; end
|
353
354
|
|
354
355
|
it "searches symbol prefixes" do
|
355
|
-
obj = BinData::Struct.new(:
|
356
|
-
:
|
356
|
+
obj = BinData::Struct.new(search_prefix: :a,
|
357
|
+
fields: [ [:short, :f] ])
|
357
358
|
obj.f.class.name.must_equal "AShort"
|
358
359
|
end
|
359
360
|
|
360
361
|
it "searches string prefixes" do
|
361
|
-
obj = BinData::Struct.new(:
|
362
|
-
:
|
362
|
+
obj = BinData::Struct.new(search_prefix: "a",
|
363
|
+
fields: [ [:short, :f] ])
|
363
364
|
obj.f.class.name.must_equal "AShort"
|
364
365
|
end
|
365
366
|
|
366
367
|
it "searches string prefixes with optional underscore" do
|
367
|
-
obj = BinData::Struct.new(:
|
368
|
-
:
|
368
|
+
obj = BinData::Struct.new(search_prefix: "a_",
|
369
|
+
fields: [ [:short, :f] ])
|
369
370
|
obj.f.class.name.must_equal "AShort"
|
370
371
|
end
|
371
372
|
|
372
373
|
it "searches multiple prefixes" do
|
373
|
-
obj = BinData::Struct.new(:
|
374
|
-
:
|
374
|
+
obj = BinData::Struct.new(search_prefix: [:x, :a],
|
375
|
+
fields: [ [:short, :f] ])
|
375
376
|
obj.f.class.name.must_equal "AShort"
|
376
377
|
end
|
377
378
|
|
378
379
|
it "uses parent search_prefix" do
|
379
|
-
nested_params = { :
|
380
|
-
obj = BinData::Struct.new(:
|
381
|
-
:
|
380
|
+
nested_params = { fields: [[:short, :f]] }
|
381
|
+
obj = BinData::Struct.new(search_prefix: :a,
|
382
|
+
fields: [[:struct, :s, nested_params]])
|
382
383
|
obj.s.f.class.name.must_equal "AShort"
|
383
384
|
end
|
384
385
|
|
385
386
|
it "searches parent search_prefix" do
|
386
|
-
nested_params = { :
|
387
|
-
obj = BinData::Struct.new(:
|
388
|
-
:
|
387
|
+
nested_params = { search_prefix: :x, fields: [[:short, :f]] }
|
388
|
+
obj = BinData::Struct.new(search_prefix: :a,
|
389
|
+
fields: [[:struct, :s, nested_params]])
|
389
390
|
obj.s.f.class.name.must_equal "AShort"
|
390
391
|
end
|
391
392
|
|
392
393
|
it "prioritises nested search_prefix" do
|
393
|
-
nested_params = { :
|
394
|
-
obj = BinData::Struct.new(:
|
395
|
-
:
|
394
|
+
nested_params = { search_prefix: :a, fields: [[:short, :f]] }
|
395
|
+
obj = BinData::Struct.new(search_prefix: :b,
|
396
|
+
fields: [[:struct, :s, nested_params]])
|
396
397
|
obj.s.f.class.name.must_equal "AShort"
|
397
398
|
end
|
398
399
|
end
|
399
400
|
|
400
401
|
describe BinData::Struct, "with byte_align" do
|
401
402
|
let(:obj) {
|
402
|
-
params = { :
|
403
|
-
|
404
|
-
|
405
|
-
|
403
|
+
params = { fields: [[:int8, :a],
|
404
|
+
[:int8, :b, byte_align: 5],
|
405
|
+
[:bit2, :c],
|
406
|
+
[:int8, :d, byte_align: 3]] }
|
406
407
|
BinData::Struct.new(params)
|
407
408
|
}
|
408
409
|
|
@@ -412,11 +413,11 @@ describe BinData::Struct, "with byte_align" do
|
|
412
413
|
|
413
414
|
it "reads" do
|
414
415
|
obj.read("\x01\x00\x00\x00\x00\x02\xc0\x00\x00\x04")
|
415
|
-
obj.snapshot.must_equal({ :
|
416
|
+
obj.snapshot.must_equal({ a: 1, b: 2, c: 3, d: 4 })
|
416
417
|
end
|
417
418
|
|
418
419
|
it "writes" do
|
419
|
-
obj.assign(:
|
420
|
+
obj.assign(a: 1, b: 2, c: 3, d: 4)
|
420
421
|
obj.to_binary_s.must_equal_binary "\x01\x00\x00\x00\x00\x02\xc0\x00\x00\x04"
|
421
422
|
end
|
422
423
|
|
@@ -430,9 +431,9 @@ end
|
|
430
431
|
|
431
432
|
describe BinData::Struct, "with dynamically named types" do
|
432
433
|
it "instantiates" do
|
433
|
-
_ = BinData::Struct.new(:
|
434
|
+
_ = BinData::Struct.new(name: :my_struct, fields: [[:int8, :a, {initial_value: 3}]])
|
434
435
|
|
435
|
-
obj = BinData::Struct.new(:
|
436
|
+
obj = BinData::Struct.new(fields: [[:my_struct, :v]])
|
436
437
|
|
437
438
|
obj.v.a.must_equal 3
|
438
439
|
end
|