File: skip_test.rb

package info (click to toggle)
ruby-bindata 2.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 652 kB
  • sloc: ruby: 8,896; makefile: 4
file content (182 lines) | stat: -rwxr-xr-x 4,826 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
#!/usr/bin/env ruby

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

describe BinData::Skip, "when instantiating" do
  describe "with no mandatory parameters supplied" do
    it "raises an error" do
      args = {}
      _ { BinData::Skip.new(args) }.must_raise ArgumentError
    end
  end
end

describe BinData::Skip, "with :length" do
  let(:obj) { BinData::Skip.new(length: 5) }
  let(:io) { StringIO.new("abcdefghij") }

  it "initial state" do
    _(obj).must_equal ""
    _(obj.to_binary_s).must_equal_binary "\000" * 5
  end

  it "skips bytes" do
    obj.read(io)
    _(io.pos).must_equal 5
  end

  it "has expected binary representation after setting value" do
    obj.assign("123")
    _(obj.to_binary_s).must_equal_binary "\000" * 5
  end

  it "has expected binary representation after reading" do
    obj.read(io)
    _(obj.to_binary_s).must_equal_binary "\000" * 5
  end
end

describe BinData::Skip, "with :to_abs_offset" do
  BinData::Struct.new(fields: [ [:skip, :f, { to_abs_offset: 5 } ] ])

  let(:skip_obj) { [:skip, :f, { to_abs_offset: 5 } ] }
  let(:io) { StringIO.new("abcdefghij") }

  it "reads skipping forward" do
    fields = [ skip_obj ]
    obj = BinData::Struct.new(fields: fields)
    obj.read(io)
    _(io.pos).must_equal 5
  end

  it "reads skipping in place" do
    fields = [ [:string, :a, { read_length: 5 }], skip_obj ]
    obj = BinData::Struct.new(fields: fields)
    obj.read(io)
    _(io.pos).must_equal 5
  end

  it "does not read skipping backwards" do
    fields = [ [:string, :a, { read_length: 10 }], skip_obj ]
    obj = BinData::Struct.new(fields: fields)

    _ {
      obj.read(io)
    }.must_raise ArgumentError
  end

  it "writes skipping forward" do
    fields = [ skip_obj ]
    obj = BinData::Struct.new(fields: fields)
    _(obj.to_binary_s).must_equal "\000\000\000\000\000"
  end

  it "reads skipping in place" do
    fields = [ [:string, :a, { value: "abcde" }], skip_obj ]
    obj = BinData::Struct.new(fields: fields)
    _(obj.to_binary_s).must_equal "abcde"
  end

  it "does not write skipping backwards" do
    fields = [ [:string, :a, { value: "abcdefghij" }], skip_obj ]
    obj = BinData::Struct.new(fields: fields)
    _ {
      obj.to_binary_s
    }.must_raise ArgumentError
  end
end

describe BinData::Skip, "with :until_valid" do
  let(:io) { StringIO.new("abcdefghij") }

  it "doesn't skip when writing" do
    skip_obj = [:string, { read_length: 1, assert: "f" }]
    args = { until_valid: skip_obj }
    obj = BinData::Skip.new(args)
    _(obj.to_binary_s).must_equal ""
  end

  it "skips to valid match" do
    skip_obj = [:string, { read_length: 1, assert: "f" }]
    fields = [ [:skip, :s, { until_valid: skip_obj }] ]
    obj = BinData::Struct.new(fields: fields)
    obj.read(io)
    _(io.pos).must_equal 5
  end

  it "won't skip on unseekable stream" do
    rd, wr = IO::pipe
    unseekable_io = BinData::IO::Read.new(rd)
    wr.write io
    wr.close

    skip_obj = [:string, { read_length: 1, assert: "f" }]
    fields = [ [:skip, :s, { until_valid: skip_obj }] ]
    obj = BinData::Struct.new(fields: fields)
    _ {obj.read(unseekable_io)}.must_raise IOError
    rd.close
  end

  it "doesn't skip when validator doesn't assert" do
    skip_obj = [:string, { read_length: 1 }]
    fields = [ [:skip, :s, { until_valid: skip_obj }] ]
    obj = BinData::Struct.new(fields: fields)
    obj.read(io)
    _(io.pos).must_equal 0
  end

  it "raises IOError when no match" do
    skip_obj = [:string, { read_length: 1, assert: "X" }]
    fields = [ [:skip, :s, { until_valid: skip_obj }] ]
    obj = BinData::Struct.new(fields: fields)
    _ {
      obj.read(io)
    }.must_raise IOError
  end

  it "raises IOError when validator reads beyond stream" do
    skip_obj = [:string, { read_length: 30 }]
    fields = [ [:skip, :s, { until_valid: skip_obj }] ]
    obj = BinData::Struct.new(fields: fields)
    _ {
      obj.read(io)
    }.must_raise IOError
  end

  it "uses block form" do
    class DSLSkip < BinData::Record
      skip :s do
        string read_length: 1, assert: "f"
      end
      string :a, read_length: 1
    end

    obj = DSLSkip.read(io)
    _(obj.a).must_equal "f"
  end
end

describe BinData::Skip, "with :until_valid" do
  class SkipSearch < BinData::Record
    skip :s do
      uint8
      uint8 asserted_value: 1
      uint8 :a
      uint8 :b
      virtual assert: -> { a == b }
    end
    array :data, type: :uint8, initial_length: 4
  end

  let(:io) { BinData::IO.create_string_io("\x0f" * 10 + "\x00\x01\x02\x03\x00" + "\x02\x01\x03\x03" + "\x06") }

  it "finds valid match" do
    obj = SkipSearch.read(io)
    _(obj.data).must_equal [2, 1, 3, 3]
  end

  it "match is at expected offset" do
    obj = SkipSearch.read(io)
    _(obj.data.rel_offset).must_equal 15
  end
end