ruby-dbus 0.18.0.beta1 → 0.18.0.beta4

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,14 @@
4
4
  require_relative "spec_helper"
5
5
  require "dbus"
6
6
 
7
+ # FIXME: factor out DBus::TestFixtures::Value in spec_helper
8
+ require "ostruct"
9
+ require "yaml"
10
+
11
+ data_dir = File.expand_path("data", __dir__)
12
+ marshall_yaml_s = File.read("#{data_dir}/marshall.yaml")
13
+ marshall_yaml = YAML.safe_load(marshall_yaml_s)
14
+
7
15
  describe "PropertyTest" do
8
16
  before(:each) do
9
17
  @session_bus = DBus::ASessionBus.new
@@ -52,7 +60,7 @@ describe "PropertyTest" do
52
60
 
53
61
  it "tests get all" do
54
62
  all = @iface.all_properties
55
- expect(all.keys.sort).to eq(["MyStruct", "ReadMe", "ReadOrWriteMe"])
63
+ expect(all.keys.sort).to eq(["MyArray", "MyDict", "MyStruct", "MyVariant", "ReadMe", "ReadOrWriteMe"])
56
64
  end
57
65
 
58
66
  it "tests get all on a V1 object" do
@@ -60,7 +68,7 @@ describe "PropertyTest" do
60
68
  iface = obj["org.ruby.SampleInterface"]
61
69
 
62
70
  all = iface.all_properties
63
- expect(all.keys.sort).to eq(["MyStruct", "ReadMe", "ReadOrWriteMe"])
71
+ expect(all.keys.sort).to eq(["MyArray", "MyDict", "MyStruct", "MyVariant", "ReadMe", "ReadOrWriteMe"])
64
72
  end
65
73
 
66
74
  it "tests unknown property reading" do
@@ -122,5 +130,75 @@ describe "PropertyTest" do
122
130
  struct = @iface["MyStruct"]
123
131
  expect(struct).to be_frozen
124
132
  end
133
+
134
+ it "Get returns the correctly typed value (check with dbus-send)" do
135
+ # As big as the DBus::Data branch is,
136
+ # it still does not handle the :exact mode on the client/proxy side.
137
+ # So we resort to parsing dbus-send output.
138
+ cmd = "dbus-send --print-reply " \
139
+ "--dest=org.ruby.service " \
140
+ "/org/ruby/MyInstance " \
141
+ "org.freedesktop.DBus.Properties.Get " \
142
+ "string:org.ruby.SampleInterface " \
143
+ "string:MyStruct"
144
+ reply = `#{cmd}`
145
+ expect(reply).to match(/variant\s+struct {\s+string "three"\s+string "strings"\s+string "in a struct"\s+}/)
146
+ end
147
+
148
+ it "GetAll returns the correctly typed value (check with dbus-send)" do
149
+ cmd = "dbus-send --print-reply " \
150
+ "--dest=org.ruby.service " \
151
+ "/org/ruby/MyInstance " \
152
+ "org.freedesktop.DBus.Properties.GetAll " \
153
+ "string:org.ruby.SampleInterface "
154
+ reply = `#{cmd}`
155
+ expect(reply).to match(/variant\s+struct {\s+string "three"\s+string "strings"\s+string "in a struct"\s+}/)
156
+ end
157
+ end
158
+
159
+ context "an array-typed property" do
160
+ it "gets read as an array" do
161
+ val = @iface["MyArray"]
162
+ expect(val).to eq([42, 43])
163
+ end
164
+ end
165
+
166
+ context "an dict-typed property" do
167
+ it "gets read as a hash" do
168
+ val = @iface["MyDict"]
169
+ expect(val).to eq({
170
+ "one" => 1,
171
+ "two" => "dva",
172
+ "three" => [3, 3, 3]
173
+ })
174
+ end
175
+ end
176
+
177
+ context "a variant-typed property" do
178
+ it "gets read at all" do
179
+ obj = @svc.object("/org/ruby/MyDerivedInstance")
180
+ iface = obj["org.ruby.SampleInterface"]
181
+ val = iface["MyVariant"]
182
+ expect(val).to eq([42, 43])
183
+ end
184
+ end
185
+
186
+ context "marshall.yaml round-trip via a VARIANT property" do
187
+ marshall_yaml.each do |test|
188
+ t = OpenStruct.new(test)
189
+ next if t.val.nil?
190
+
191
+ # Round trips do not work yet because the properties
192
+ # must present a plain Ruby value so the exact D-Bus type is lost.
193
+ # Round trips will work once users can declare accepting DBus::Data
194
+ # in properties and method arguments.
195
+ it "Sets #{t.sig.inspect}:#{t.val.inspect} and Gets something back" do
196
+ before = DBus::Data.make_typed(t.sig, t.val)
197
+ expect { @iface["MyVariant"] = before }.to_not raise_error
198
+ expect { _after = @iface["MyVariant"] }.to_not raise_error
199
+ # round-trip:
200
+ # expect(after).to eq(before.value)
201
+ end
202
+ end
125
203
  end
126
204
  end
@@ -13,16 +13,30 @@ PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties"
13
13
  class Test < DBus::Object
14
14
  Point2D = Struct.new(:x, :y)
15
15
 
16
+ attr_writer :main_loop
17
+
16
18
  INTERFACE = "org.ruby.SampleInterface"
17
19
  def initialize(path)
18
20
  super path
19
21
  @read_me = "READ ME"
20
22
  @read_or_write_me = "READ OR WRITE ME"
21
23
  @my_struct = ["three", "strings", "in a struct"].freeze
24
+ @my_array = [42, 43]
25
+ @my_dict = {
26
+ "one" => 1,
27
+ "two" => "dva",
28
+ "three" => [3, 3, 3]
29
+ }
30
+ @my_variant = @my_array.dup
31
+ @main_loop = nil
22
32
  end
23
33
 
24
34
  # Create an interface aggregating all upcoming dbus_method defines.
25
35
  dbus_interface INTERFACE do
36
+ dbus_method :quit, "" do
37
+ @main_loop&.quit
38
+ end
39
+
26
40
  dbus_method :hello, "in name:s, in name2:s" do |name, name2|
27
41
  puts "hello(#{name}, #{name2})"
28
42
  end
@@ -93,7 +107,10 @@ class Test < DBus::Object
93
107
  end
94
108
  dbus_reader :explosive, "s"
95
109
 
96
- dbus_attr_reader :my_struct, "(sss)"
110
+ dbus_attr_accessor :my_struct, "(sss)"
111
+ dbus_attr_accessor :my_array, "aq"
112
+ dbus_attr_accessor :my_dict, "a{sv}"
113
+ dbus_attr_accessor :my_variant, "v"
97
114
  end
98
115
 
99
116
  # closing and reopening the same interface
@@ -193,6 +210,7 @@ end
193
210
  puts "listening, with ruby-#{RUBY_VERSION}"
194
211
  main = DBus::Main.new
195
212
  main << bus
213
+ myobj.main_loop = main
196
214
  begin
197
215
  main.run
198
216
  rescue SystemCallError
data/spec/spec_helper.rb CHANGED
@@ -15,6 +15,8 @@ if coverage
15
15
  SimpleCov.add_filter "_spec.rb"
16
16
  # do not cover the activesupport helpers
17
17
  SimpleCov.add_filter "/core_ext/"
18
+ # measure all if/else branches on a line
19
+ SimpleCov.enable_coverage :branch
18
20
 
19
21
  SimpleCov.start
20
22
 
@@ -118,3 +120,15 @@ def with_service_by_activation(&block)
118
120
 
119
121
  system "pkill -f #{exec}"
120
122
  end
123
+
124
+ # Make a binary string from readable YAML pieces; see data/marshall.yaml
125
+ def buffer_from_yaml(parts)
126
+ strings = parts.flatten.map do |part|
127
+ if part.is_a? Integer
128
+ part.chr
129
+ else
130
+ part
131
+ end
132
+ end
133
+ strings.join.force_encoding(Encoding::BINARY)
134
+ end
data/spec/type_spec.rb CHANGED
@@ -6,15 +6,76 @@ require "dbus"
6
6
 
7
7
  describe DBus do
8
8
  describe ".type" do
9
- ["i", "ai", "a(ii)", "aai"].each do |s|
10
- it "parses some type #{s}" do
11
- expect(DBus.type(s).to_s).to be_eql s
9
+ good = [
10
+ "i",
11
+ "ai",
12
+ "a(ii)",
13
+ "aai"
14
+ ]
15
+
16
+ context "valid single types" do
17
+ good.each do |s|
18
+ it "#{s.inspect} is parsed" do
19
+ expect(DBus.type(s).to_s).to eq(s)
20
+ end
21
+ end
22
+ end
23
+
24
+ bad = [
25
+ ["\x00", "Unknown type code"],
26
+ ["!", "Unknown type code"],
27
+
28
+ # ARRAY related
29
+ ["a", "Empty ARRAY"],
30
+ ["aa", "Empty ARRAY"],
31
+
32
+ # STRUCT related
33
+ ["r", "Abstract STRUCT"],
34
+ ["()", "Empty STRUCT"],
35
+ ["(ii", "STRUCT not closed"],
36
+ ["a{i)", "STRUCT unexpectedly closed"],
37
+
38
+ # TODO: deep nesting arrays, structs, combined
39
+
40
+ # DICT_ENTRY related
41
+ ["e", "Abstract DICT_ENTRY"],
42
+ ["a{}", "DICT_ENTRY must have 2 subtypes, found 0"],
43
+ ["a{s}", "DICT_ENTRY must have 2 subtypes, found 1"],
44
+ ["a{sss}", "DICT_ENTRY must have 2 subtypes, found 3"],
45
+ ["a{vs}", "DICT_ENTRY key must be basic (non-container)"],
46
+ ["{sv}", "DICT_ENTRY not an immediate child of an ARRAY"],
47
+ ["a({sv})", "DICT_ENTRY not an immediate child of an ARRAY"],
48
+ ["a{sv", "DICT_ENTRY not closed"],
49
+ ["}", "DICT_ENTRY unexpectedly closed"],
50
+
51
+ # Too long
52
+ ["(#{"y" * 254})", "longer than 255"],
53
+
54
+ # not Single Complete Types
55
+ ["", "expecting a Single Complete Type"],
56
+ ["ii", "more than a Single Complete Type"]
57
+ ]
58
+ context "invalid single types" do
59
+ bad.each.each do |s, msg|
60
+ it "#{s.inspect} raises an exception mentioning: #{msg}" do
61
+ rx = Regexp.new(Regexp.quote(msg))
62
+ expect { DBus.type(s) }.to raise_error(DBus::Type::SignatureException, rx)
63
+ end
12
64
  end
13
65
  end
66
+ end
67
+
68
+ describe ".types" do
69
+ good = [
70
+ "",
71
+ "ii"
72
+ ]
14
73
 
15
- ["aa", "(ii", "ii)", "hrmp"].each do |s|
16
- it "raises exception for invalid type #{s}" do
17
- expect { DBus.type(s).to_s }.to raise_error DBus::Type::SignatureException
74
+ context "valid signatures" do
75
+ good.each do |s|
76
+ it "#{s.inspect} is parsed" do
77
+ expect(DBus.types(s).map(&:to_s).join).to eq(s)
78
+ end
18
79
  end
19
80
  end
20
81
  end
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env rspec
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "spec_helper"
5
+ require "dbus"
6
+
7
+ describe "Quit the service" do
8
+ it "Tells the service to quit and waits, to collate coverage data" do
9
+ session_bus = DBus::ASessionBus.new
10
+ @svc = session_bus.service("org.ruby.service")
11
+ @obj = @svc.object("/org/ruby/MyInstance")
12
+ @obj.default_iface = "org.ruby.SampleInterface"
13
+ @obj.quit
14
+ sleep 3
15
+ end
16
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-dbus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.0.beta1
4
+ version: 0.18.0.beta4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ruby DBus Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-24 00:00:00.000000000 Z
11
+ date: 2022-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rexml
@@ -131,6 +131,7 @@ files:
131
131
  - examples/no-introspect/tracker-test.rb
132
132
  - examples/rhythmbox/playpause.rb
133
133
  - examples/service/call_service.rb
134
+ - examples/service/complex-property.rb
134
135
  - examples/service/service_newapi.rb
135
136
  - examples/simple/call_introspect.rb
136
137
  - examples/simple/get_id.rb
@@ -144,6 +145,7 @@ files:
144
145
  - lib/dbus/bus_name.rb
145
146
  - lib/dbus/core_ext/class/attribute.rb
146
147
  - lib/dbus/core_ext/module/redefine_method.rb
148
+ - lib/dbus/data.rb
147
149
  - lib/dbus/error.rb
148
150
  - lib/dbus/introspect.rb
149
151
  - lib/dbus/logger.rb
@@ -156,6 +158,7 @@ files:
156
158
  - lib/dbus/proxy_object.rb
157
159
  - lib/dbus/proxy_object_factory.rb
158
160
  - lib/dbus/proxy_object_interface.rb
161
+ - lib/dbus/raw_message.rb
159
162
  - lib/dbus/type.rb
160
163
  - lib/dbus/xml.rb
161
164
  - ruby-dbus.gemspec
@@ -167,12 +170,16 @@ files:
167
170
  - spec/bus_spec.rb
168
171
  - spec/byte_array_spec.rb
169
172
  - spec/client_robustness_spec.rb
173
+ - spec/data/marshall.yaml
174
+ - spec/data_spec.rb
170
175
  - spec/err_msg_spec.rb
171
176
  - spec/introspect_xml_parser_spec.rb
172
177
  - spec/introspection_spec.rb
173
178
  - spec/main_loop_spec.rb
174
179
  - spec/node_spec.rb
175
180
  - spec/object_path_spec.rb
181
+ - spec/packet_marshaller_spec.rb
182
+ - spec/packet_unmarshaller_spec.rb
176
183
  - spec/property_spec.rb
177
184
  - spec/proxy_object_spec.rb
178
185
  - spec/server_robustness_spec.rb
@@ -190,6 +197,7 @@ files:
190
197
  - spec/type_spec.rb
191
198
  - spec/value_spec.rb
192
199
  - spec/variant_spec.rb
200
+ - spec/zzz_quit_spec.rb
193
201
  homepage: https://github.com/mvidner/ruby-dbus
194
202
  licenses:
195
203
  - LGPL-2.1