File: packet_unmarshaller_spec.rb

package info (click to toggle)
ruby-dbus 0.25.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 776 kB
  • sloc: ruby: 6,584; xml: 225; sh: 38; makefile: 8
file content (248 lines) | stat: -rwxr-xr-x 7,162 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
#!/usr/bin/env rspec
# frozen_string_literal: true

require_relative "spec_helper"
require "dbus"
require "ostruct"
require "yaml"

data_dir = File.expand_path("data", __dir__)
marshall_yaml_s = File.read("#{data_dir}/marshall.yaml")
marshall_yaml = YAML.safe_load(marshall_yaml_s)

# Helper to access PacketUnmarshaller internals.
# Add it to its public API?
# @param p_u [PacketUnmarshaller]
# @return [String] the binary string with unconsumed data
def remaining_buffer(p_u)
  raw_msg = p_u.instance_variable_get(:@raw_msg)
  raw_msg.remaining_bytes
end

RSpec.shared_examples "parses good data" do |cases|
  describe "parses all the instances of good test data" do
    cases.each_with_index do |(buffer, endianness, expected), i|
      it "parses plain data ##{i}" do
        buffer = String.new(buffer, encoding: Encoding::BINARY)
        subject = described_class.new(buffer, endianness)

        results = subject.unmarshall(signature, mode: :plain)
        # unmarshall works on multiple signatures but we use one
        expect(results).to be_an(Array)
        expect(results.size).to eq(1)
        result = results.first

        expect(result).to eq(expected)

        expect(remaining_buffer(subject)).to be_empty
      end

      it "parses exact data ##{i}" do
        buffer = String.new(buffer, encoding: Encoding::BINARY)
        subject = described_class.new(buffer, endianness)

        results = subject.unmarshall(signature, mode: :exact)
        # unmarshall works on multiple signatures but we use one
        expect(results).to be_an(Array)
        expect(results.size).to eq(1)
        result = results.first

        expect(result).to be_a(DBus::Data::Base)
        expect(result.value).to eq(expected)

        expect(remaining_buffer(subject)).to be_empty
      end
    end
  end
end

# this is necessary because we do an early switch on the signature
RSpec.shared_examples "reports empty data" do
  it "reports empty data" do
    [:big, :little].each do |endianness|
      subject = described_class.new("", endianness)
      expect { subject.unmarshall(signature) }.to raise_error(DBus::IncompleteBufferException)
    end
  end
end

describe DBus::PacketUnmarshaller do
  context "marshall.yaml" do
    marshall_yaml.each do |test|
      t = OpenStruct.new(test)
      signature = t.sig
      buffer = buffer_from_yaml(t.buf)
      endianness = t.end.to_sym

      # successful parse
      if !t.val.nil?
        expected = t.val

        it "parses a '#{signature}' to get #{t.val.inspect} (plain)" do
          subject = described_class.new(buffer, endianness)
          results = subject.unmarshall(signature, mode: :plain)
          # unmarshall works on multiple signatures but we use one
          expect(results).to be_an(Array)
          expect(results.size).to eq(1)
          result = results.first

          expect(result).to eq(expected)
          expect(remaining_buffer(subject)).to be_empty
        end

        it "parses a '#{t.sig}' to get #{t.val.inspect} (exact)" do
          subject = described_class.new(buffer, endianness)
          results = subject.unmarshall(signature, mode: :exact)
          # unmarshall works on multiple signatures but we use one
          expect(results).to be_an(Array)
          expect(results.size).to eq(1)
          result = results.first

          expect(result).to be_a(DBus::Data::Base)
          expect(result.value).to eq(expected)

          expect(remaining_buffer(subject)).to be_empty
        end
      elsif t.exc
        next if t.unmarshall == false

        exc_class = DBus.const_get(t.exc)
        msg_re = Regexp.new(Regexp.escape(t.msg))

        # TODO: InvalidPacketException is never rescued.
        # The other end is sending invalid data. Can we do better than crashing?
        # When we can test with peer connections, try it out.
        it "parses a '#{signature}' to report a #{t.exc}" do
          subject = described_class.new(buffer, endianness)
          expect { subject.unmarshall(signature, mode: :plain) }.to raise_error(exc_class, msg_re)

          subject = described_class.new(buffer, endianness)
          expect { subject.unmarshall(signature, mode: :exact) }.to raise_error(exc_class, msg_re)
        end
      end
    end
  end

  context "BYTEs" do
    let(:signature) { "y" }
    include_examples "reports empty data"
  end

  context "BOOLEANs" do
    let(:signature) { "b" }
    include_examples "reports empty data"
  end

  context "INT16s" do
    let(:signature) { "n" }
    include_examples "reports empty data"
  end

  context "UINT16s" do
    let(:signature) { "q" }
    include_examples "reports empty data"
  end

  context "INT32s" do
    let(:signature) { "i" }
    include_examples "reports empty data"
  end

  context "UINT32s" do
    let(:signature) { "u" }
    include_examples "reports empty data"
  end

  context "UNIX_FDs" do
    let(:signature) { "h" }
    include_examples "reports empty data"
  end

  context "INT64s" do
    let(:signature) { "x" }
    include_examples "reports empty data"
  end

  context "UINT64s" do
    let(:signature) { "t" }
    include_examples "reports empty data"
  end

  context "DOUBLEs" do
    let(:signature) { "d" }
    # See https://en.wikipedia.org/wiki/Double-precision_floating-point_format
    # for binary representations
    # TODO: figure out IEEE754 comparisons
    good = [
      # But == cant distinguish -0.0
      ["\x00\x00\x00\x00\x00\x00\x00\x80", :little, -0.0],
      # But NaN == NaN is false!
      # ["\xff\xff\xff\xff\xff\xff\xff\xff", :little, Float::NAN],
      ["\x80\x00\x00\x00\x00\x00\x00\x00", :big, -0.0]
      # ["\xff\xff\xff\xff\xff\xff\xff\xff", :big, Float::NAN]
    ]
    include_examples "parses good data", good
    include_examples "reports empty data"
  end

  context "STRINGs" do
    let(:signature) { "s" }
    include_examples "reports empty data"
  end

  context "OBJECT_PATHs" do
    let(:signature) { "o" }
    include_examples "reports empty data"
  end

  context "SIGNATUREs" do
    let(:signature) { "g" }
    include_examples "reports empty data"
  end

  context "ARRAYs" do
    context "of BYTEs" do
      let(:signature) { "ay" }
      include_examples "reports empty data"
    end

    context "of UINT64s" do
      let(:signature) { "at" }
      include_examples "reports empty data"
    end

    context "of STRUCT of 2 UINT16s" do
      let(:signature) { "a(qq)" }
      include_examples "reports empty data"
    end

    context "of DICT_ENTRIES" do
      let(:signature) { "a{yq}" }
      include_examples "reports empty data"
    end
  end

  context "STRUCTs" do
    # TODO: this is invalid but does not raise
    context "(generic 'r' struct)" do
      let(:signature) { "r" }
    end

    context "of two shorts" do
      let(:signature) { "(qq)" }
      include_examples "reports empty data"
    end
  end

  # makes sense here? or in array? remember invalid sigs are rejected elsewhere
  context "DICT_ENTRYs" do
    context "(generic 'e' dict_entry)" do
      let(:signature) { "e" }
    end
  end

  context "VARIANTs" do
    let(:signature) { "v" }
    include_examples "reports empty data"
  end
end