File: struct_test.rb

package info (click to toggle)
ruby-bindata 2.4.14-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 600 kB
  • sloc: ruby: 8,566; makefile: 4
file content (440 lines) | stat: -rwxr-xr-x 11,682 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#!/usr/bin/env ruby

require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))

describe BinData::Struct, "when initializing" do
  it "fails on non registered types" do
    params = {fields: [[:non_registered_type, :a]]}
    _ {
      BinData::Struct.new(params)
    }.must_raise BinData::UnRegisteredTypeError
  end

  it "fails on duplicate names" do
    params = {fields: [[:int8, :a], [:int8, :b], [:int8, :a]]}
    _ {
      BinData::Struct.new(params)
    }.must_raise NameError
  end

  it "fails on reserved names" do
    # note that #invert is from Hash.instance_methods
    params = {fields: [[:int8, :a], [:int8, :invert]]}
    _ {
      BinData::Struct.new(params)
    }.must_raise NameError
  end

  it "fails when field name shadows an existing method" do
    params = {fields: [[:int8, :object_id]]}
    _ {
      BinData::Struct.new(params)
    }.must_raise NameError
  end

  it "fails on unknown endian" do
    params = {endian: 'bad value', fields: []}
    _ {
      BinData::Struct.new(params)
    }.must_raise ArgumentError
  end
end

describe BinData::Struct, "with anonymous fields" do
  let(:obj) {
    params = { fields: [
                            [:int8, :a, {initial_value: 5}],
                            [:int8, nil],
                            [:int8, '', {value: :a}]
                          ] }
    BinData::Struct.new(params)
  }

  it "only shows non anonymous fields" do
    _(obj.field_names).must_equal [:a]
  end

  it "does not include anonymous fields in snapshot" do
    obj.a = 5
    _(obj.snapshot).must_equal({a: 5})
  end

  it "writes anonymous fields" do
    obj.read("\001\002\003")
    obj.a.clear
    _(obj.to_binary_s).must_equal_binary "\005\002\005"
  end
end

describe BinData::Struct, "with hidden fields" do
  let(:obj) {
    params = { hide: [:b, :c],
               fields: [
                   [:int8, :a],
                   [:int8, 'b', {initial_value: 5}],
                   [:int8, :c],
                   [:int8, :d, {value: :b}]] }
    BinData::Struct.new(params)
  }

  it "only shows fields that aren't hidden" do
    _(obj.field_names).must_equal [:a, :d]
  end

  it "shows all fields when requested" do
    _(obj.field_names(true)).must_equal [:a, :b, :c, :d]
  end

  it "accesses hidden fields directly" do
    _(obj.b).must_equal 5
    obj.c = 15
    _(obj.c).must_equal 15

    _(obj).must_respond_to :b=
  end

  it "does not include hidden fields in snapshot" do
    obj.b = 7
    _(obj.snapshot).must_equal({a: 0, d: 7})
  end

  it "detects hidden fields with has_key?" do
    assert obj.has_key?("b")
  end
end

describe BinData::Struct, "with multiple fields" do
  let(:params) { { fields: [ [:int8, :a], [:int8, :b] ] } }
  let(:obj) { BinData::Struct.new({a: 1, b: 2}, params) }

  specify { _(obj.field_names).must_equal [:a, :b] }
  specify { _(obj.to_binary_s).must_equal_binary "\x01\x02" }

  it "returns num_bytes" do
    _(obj.a.num_bytes).must_equal 1
    _(obj.b.num_bytes).must_equal 1
    _(obj.num_bytes).must_equal   2
  end

  it "identifies accepted parameters" do
    _(BinData::Struct.accepted_parameters.all).must_include :fields
    _(BinData::Struct.accepted_parameters.all).must_include :hide
    _(BinData::Struct.accepted_parameters.all).must_include :endian
  end

  it "clears" do
    obj.a = 6
    obj.clear
    assert obj.clear?
  end

  it "clears individual elements" do
    obj.a = 6
    obj.b = 7
    obj.a.clear
    assert obj.a.clear?
    refute obj.b.clear?
  end

  it "reads elements dynamically" do
    _(obj[:a]).must_equal 1
  end

  it "handles not existing elements" do
    _(obj[:does_not_exist]).must_be_nil
  end

  it "writes elements dynamically" do
    obj[:a] = 2
    _(obj.a).must_equal 2
  end

  it "implements has_key?" do
    assert obj.has_key?("a")
  end

  it "reads ordered" do
    obj.read("\x03\x04")

    _(obj.a).must_equal 3
    _(obj.b).must_equal 4
  end

  it "returns a snapshot" do
    snap = obj.snapshot
    assert snap.respond_to?(:a)
    _(snap.a).must_equal 1
    _(snap.b).must_equal 2
    _(snap).must_equal({ a: 1, b: 2 })
  end

  it "assigns from partial hash" do
    obj.assign(a: 3)
    _(obj.a).must_equal 3
    _(obj.b).must_equal 0
  end

  it "assigns from hash" do
    obj.assign(a: 3, b: 4)
    _(obj.a).must_equal 3
    _(obj.b).must_equal 4
  end

  it "assigns from nil" do
    obj.assign(nil)
    assert obj.clear?
  end

  it "assigns from Struct" do
    src = BinData::Struct.new(params)
    src.a = 3
    src.b = 4

    obj.assign(src)
    _(obj.a).must_equal 3
    _(obj.b).must_equal 4
  end

  it "assigns from snapshot" do
    src = BinData::Struct.new(params)
    src.a = 3
    src.b = 4

    obj.assign(src.snapshot)
    _(obj.a).must_equal 3
    _(obj.b).must_equal 4
  end

  it "fails on unknown method call" do
    _ { obj.does_not_exist }.must_raise NoMethodError
  end

  describe "#snapshot" do
    it "has ordered #keys" do
      _(obj.snapshot.keys).must_equal [:a, :b]
    end

    it "has ordered #each" do
      keys = []
      obj.snapshot.each { |el| keys << el[0] }
      _(keys).must_equal [:a, :b]
    end

    it "has ordered #each_pair" do
      keys = []
      obj.snapshot.each_pair { |k, v| keys << k }
      _(keys).must_equal [:a, :b]
    end
  end
end

describe BinData::Struct, "with nested structs" do
  let(:obj) {
    inner1 = [ [:int8, :w, {initial_value: 3}],
               [:int8, :x, {value: :the_val}] ]

    inner2 = [ [:int8, :y, {value: -> { parent.b.w }}],
               [:int8, :z] ]

    params = { fields: [
                 [:int8, :a, {initial_value: 6}],
                 [:struct, :b, {fields: inner1, the_val: :a}],
                 [:struct, :c, {fields: inner2}]] }
    BinData::Struct.new(params)
  }

  specify { _(obj.field_names).must_equal [:a, :b, :c] }

  it "returns num_bytes" do
    _(obj.b.num_bytes).must_equal 2
    _(obj.c.num_bytes).must_equal 2
    _(obj.num_bytes).must_equal 5
  end

  it "accesses nested fields" do
    _(obj.a).must_equal   6
    _(obj.b.w).must_equal 3
    _(obj.b.x).must_equal 6
    _(obj.c.y).must_equal 3
    _(obj.c.z).must_equal 0
  end

  it "returns correct abs_offset" do
    _(obj.b.abs_offset).must_equal 1
    _(obj.b.w.abs_offset).must_equal 1
    _(obj.c.abs_offset).must_equal 3
    _(obj.c.z.abs_offset).must_equal 4
  end
end

describe BinData::Struct, "with an endian defined" do
  let(:obj) {
    BinData::Struct.new(endian: :little,
                        fields: [
                                  [:uint16, :a],
                                  [:float, :b],
                                  [:array, :c,
                                    {type: :int8, initial_length: 2}],
                                  [:choice, :d,
                                    {choices: [[:uint16], [:uint32]],
                                     selection: 1}],
                                  [:struct, :e,
                                    {fields: [[:uint16, :f],
                                                 [:uint32be, :g]]}],
                                  [:struct, :h,
                                    {fields: [
                                      [:struct, :i,
                                        {fields: [[:uint16, :j]]}]]}]
                                ])
  }

  it "uses correct endian" do
    obj.a = 1
    obj.b = 2.0
    obj.c[0] = 3
    obj.c[1] = 4
    obj.d = 5
    obj.e.f = 6
    obj.e.g = 7
    obj.h.i.j = 8

    expected = [1, 2.0, 3, 4, 5, 6, 7, 8].pack('veCCVvNv')

    _(obj.to_binary_s).must_equal_binary expected
  end
end

describe BinData::Struct, "with bit fields" do
  let(:obj) {
    params = { fields: [ [:bit1le, :a], [:bit2le, :b], [:uint8, :c], [:bit1le, :d] ] }
    BinData::Struct.new({a: 1, b: 2, c: 3, d: 1}, params)
  }

  specify { _(obj.num_bytes).must_equal 3 }
  specify { _(obj.to_binary_s).must_equal_binary [0b0000_0101, 3, 1].pack("C*") }

  it "reads" do
    str = [0b0000_0110, 5, 0].pack("C*")
    obj.read(str)
    _(obj.a).must_equal 0
    _(obj.b).must_equal 3
    _(obj.c).must_equal 5
    _(obj.d).must_equal 0
  end

  it "has correct offsets" do
    _(obj.a.rel_offset).must_equal 0
    _(obj.b.rel_offset).must_equal 0
    _(obj.c.rel_offset).must_equal 1
    _(obj.d.rel_offset).must_equal 2
  end
end

describe BinData::Struct, "with nested endian" do
  it "uses correct endian" do
    nested_params = { endian: :little,
                      fields: [[:int16, :b], [:int16, :c]] }
    params = { endian: :big, 
               fields: [[:int16, :a],
                        [:struct, :s, nested_params],
                        [:int16, :d]] }
    obj = BinData::Struct.new(params)
    obj.read("\x00\x01\x02\x00\x03\x00\x00\x04")

    _(obj.a).must_equal   1
    _(obj.s.b).must_equal 2
    _(obj.s.c).must_equal 3
    _(obj.d).must_equal   4
  end
end

describe BinData::Struct, "with a search_prefix" do
  class AShort < BinData::Uint8; end
  class BShort < BinData::Uint8; end

  it "searches symbol prefixes" do
    obj = BinData::Struct.new(search_prefix: :a,
                              fields: [ [:short, :f] ])
    _(obj.f.class.name).must_equal "AShort"
  end

  it "searches string prefixes" do
    obj = BinData::Struct.new(search_prefix: "a",
                              fields: [ [:short, :f] ])
    _(obj.f.class.name).must_equal "AShort"
  end

  it "searches string prefixes with optional underscore" do
    obj = BinData::Struct.new(search_prefix: "a_",
                              fields: [ [:short, :f] ])
    _(obj.f.class.name).must_equal "AShort"
  end

  it "searches multiple prefixes" do
    obj = BinData::Struct.new(search_prefix: [:x, :a],
                              fields: [ [:short, :f] ])
    _(obj.f.class.name).must_equal "AShort"
  end

  it "uses parent search_prefix" do
    nested_params = { fields: [[:short, :f]] }
    obj = BinData::Struct.new(search_prefix: :a,
                              fields: [[:struct, :s, nested_params]])
    _(obj.s.f.class.name).must_equal "AShort"
  end

  it "searches parent search_prefix" do
    nested_params = { search_prefix: :x, fields: [[:short, :f]] }
    obj = BinData::Struct.new(search_prefix: :a,
                              fields: [[:struct, :s, nested_params]])
    _(obj.s.f.class.name).must_equal "AShort"
  end

  it "prioritises nested search_prefix" do
    nested_params = { search_prefix: :a, fields: [[:short, :f]] }
    obj = BinData::Struct.new(search_prefix: :b,
                              fields: [[:struct, :s, nested_params]])
    _(obj.s.f.class.name).must_equal "AShort"
  end
end

describe BinData::Struct, "with byte_align" do
  let(:obj) {
    params = { fields: [[:int8, :a],
                        [:int8, :b, byte_align: 5],
                        [:bit2, :c],
                        [:int8, :d, byte_align: 3]] }
    BinData::Struct.new(params)
  }

  it "has #num_bytes" do
    _(obj.num_bytes).must_equal 10
  end

  it "reads" do
    obj.read("\x01\x00\x00\x00\x00\x02\xc0\x00\x00\x04")
    _(obj.snapshot).must_equal({ a: 1, b: 2, c: 3, d: 4 })
  end

  it "writes" do
    obj.assign(a: 1, b: 2, c: 3, d: 4)
    _(obj.to_binary_s).must_equal_binary "\x01\x00\x00\x00\x00\x02\xc0\x00\x00\x04"
  end

  it "has correct offsets" do
    _(obj.a.rel_offset).must_equal 0
    _(obj.b.rel_offset).must_equal 5
    _(obj.c.rel_offset).must_equal 6
    _(obj.d.rel_offset).must_equal 9
  end
end

describe BinData::Struct, "with dynamically named types" do
  it "instantiates" do
    _ = BinData::Struct.new(name: :my_struct, fields: [[:int8, :a, {initial_value: 3}]])

    obj = BinData::Struct.new(fields: [[:my_struct, :v]])

    _(obj.v.a).must_equal 3
  end
end