Skip to content

Commit 94f77cc

Browse files
author
Léonard Dallot
committed
Add controls over DBus capacity regarding call parameters
1 parent 09600d2 commit 94f77cc

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

internal/apis/call.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ func CallOnObject(path dbus.ObjectPath, callName string, args ...any) error {
2727
return err
2828
}
2929

30+
if err = checkDbusCompatibilityWitArgs(conn, args); err != nil {
31+
return err
32+
}
33+
3034
obj := conn.Object(ObjectName, path)
3135
call := obj.Call(callName, 0, args...)
3236
return call.Err
@@ -38,7 +42,13 @@ func call(callName string, args ...any) (*dbus.Call, error) {
3842
return nil, err
3943
}
4044

45+
if err = checkDbusCompatibilityWitArgs(conn, args); err != nil {
46+
return nil, err
47+
}
48+
4149
obj := conn.Object(ObjectName, ObjectPath)
50+
4251
call := obj.Call(callName, 0, args...)
52+
4353
return call, call.Err
4454
}

internal/apis/checks.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package apis
2+
3+
import (
4+
"errors"
5+
6+
"github.com/godbus/dbus/v5"
7+
)
8+
9+
func checkDbusCompatibilityWitArgs(conn *dbus.Conn, args ...any) error {
10+
// Sending file through DBus must be tested before proceeding
11+
// See https://pkg.go.dev/github.com/godbus/dbus?utm_source=godoc#hdr-Unix_FD_passing
12+
if anyOf[any](args, isFileDescriptor) && !conn.SupportsUnixFDs() {
13+
return errors.New("DBus connection does not support passing unix file descriptors")
14+
}
15+
return nil
16+
}
17+
18+
func isFileDescriptor(v any) bool {
19+
_, ok := v.(dbus.UnixFD)
20+
return ok
21+
}
22+
23+
func anyOf[T any](values []T, assertion func(T) bool) bool {
24+
for _, v := range values {
25+
if assertion(v) {
26+
return true
27+
}
28+
}
29+
return false
30+
}

internal/apis/checks_test.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package apis
2+
3+
import (
4+
"testing"
5+
6+
"github.com/godbus/dbus/v5"
7+
)
8+
9+
func isNegative(v int) bool {
10+
return v < 0
11+
}
12+
13+
func TestAnyOfIsNegativeTrue(t *testing.T) {
14+
testValues := []int{5, 7, -4, 6, 8}
15+
expected := true
16+
result := anyOf[int](testValues, isNegative)
17+
if expected != result {
18+
t.Errorf("Expecting %v, got %v", expected, result)
19+
}
20+
}
21+
22+
func TestAnyOfIsNegativeFalse(t *testing.T) {
23+
testValues := []int{5, 7, 4, 6, 8}
24+
expected := false
25+
result := anyOf[int](testValues, isNegative)
26+
if expected != result {
27+
t.Errorf("Expecting %v, got %v", expected, result)
28+
}
29+
}
30+
31+
func TestAnyOfIsNegativeEmpty(t *testing.T) {
32+
testValues := []int{}
33+
expected := false
34+
result := anyOf[int](testValues, isNegative)
35+
if expected != result {
36+
t.Errorf("Expecting %v, got %v", expected, result)
37+
}
38+
}
39+
40+
func TestAnyOfIsNegativeFirst(t *testing.T) {
41+
testValues := []int{-5, 7, 4, 6, 8}
42+
expected := true
43+
result := anyOf[int](testValues, isNegative)
44+
if expected != result {
45+
t.Errorf("Expecting %v, got %v", expected, result)
46+
}
47+
}
48+
49+
func TestAnyOfIsNegativeLast(t *testing.T) {
50+
testValues := []int{5, 7, 4, 6, -8}
51+
expected := true
52+
result := anyOf[int](testValues, isNegative)
53+
if expected != result {
54+
t.Errorf("Expecting %v, got %v", expected, result)
55+
}
56+
}
57+
58+
func TestIsFDTrue(t *testing.T) {
59+
var tested dbus.UnixFD
60+
expected := true
61+
result := isFileDescriptor(tested)
62+
63+
if expected != result {
64+
t.Errorf("Expecting %v, got %v", expected, result)
65+
}
66+
}
67+
68+
func TestIsFDInt(t *testing.T) {
69+
var tested int
70+
expected := false
71+
result := isFileDescriptor(tested)
72+
73+
if expected != result {
74+
t.Errorf("Expecting %v, got %v", expected, result)
75+
}
76+
}
77+
78+
func TestIsFDInterface(t *testing.T) {
79+
var tested interface{}
80+
expected := false
81+
result := isFileDescriptor(tested)
82+
83+
if expected != result {
84+
t.Errorf("Expecting %v, got %v", expected, result)
85+
}
86+
}
87+
88+
func TestAnyOfIsFDTrue(t *testing.T) {
89+
var fd dbus.UnixFD
90+
var i interface{}
91+
92+
testValues := []any{5, fd, i}
93+
expected := true
94+
result := anyOf[any](testValues, isFileDescriptor)
95+
if expected != result {
96+
t.Errorf("Expecting %v, got %v", expected, result)
97+
}
98+
}
99+
100+
func TestAnyOfIsFDFalse(t *testing.T) {
101+
var i interface{}
102+
103+
testValues := []any{5, -7, i}
104+
expected := false
105+
result := anyOf[any](testValues, isFileDescriptor)
106+
if expected != result {
107+
t.Errorf("Expecting %v, got %v", expected, result)
108+
}
109+
}

0 commit comments

Comments
 (0)