forked from godbus/dbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransport_unix_test.go
157 lines (140 loc) · 3.56 KB
/
transport_unix_test.go
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
package dbus
import (
"os"
"testing"
)
const testString = `This is a test!
This text should be read from the file that is created by this test.`
type unixFDTest struct {
t *testing.T
}
func (t unixFDTest) Testfd(fd UnixFD) (string, *Error) {
var b [4096]byte
file := os.NewFile(uintptr(fd), "testfile")
defer file.Close()
n, err := file.Read(b[:])
if err != nil {
return "", &Error{"com.github.guelfey.test.Error", nil}
}
return string(b[:n]), nil
}
func (t unixFDTest) Testvariant(v Variant) (string, *Error) {
var b [4096]byte
fd := v.Value().(UnixFD)
file := os.NewFile(uintptr(fd), "testfile")
defer file.Close()
n, err := file.Read(b[:])
if err != nil {
return "", &Error{"com.github.guelfey.test.Error", nil}
}
return string(b[:n]), nil
}
type unixfdContainer struct {
Fd UnixFD
}
func (t unixFDTest) Teststruct(s unixfdContainer) (string, *Error) {
var b [4096]byte
file := os.NewFile(uintptr(s.Fd), "testfile")
defer file.Close()
n, err := file.Read(b[:])
if err != nil {
return "", &Error{"com.github.guelfey.test.Error", nil}
}
return string(b[:n]), nil
}
func (t unixFDTest) Testvariantstruct(vs Variant) (string, *Error) {
var b [4096]byte
s := vs.Value().([]interface{})
u := s[0].(UnixFD)
file := os.NewFile(uintptr(u), "testfile")
defer file.Close()
n, err := file.Read(b[:])
if err != nil {
return "", &Error{"com.github.guelfey.test.Error", nil}
}
return string(b[:n]), nil
}
type variantContainer struct {
V Variant
}
func (t unixFDTest) Teststructvariant(sv variantContainer) (string, *Error) {
var b [4096]byte
fd := sv.V.Value().(UnixFD)
file := os.NewFile(uintptr(fd), "testfile")
defer file.Close()
n, err := file.Read(b[:])
if err != nil {
return "", &Error{"com.github.guelfey.test.Error", nil}
}
return string(b[:n]), nil
}
func TestUnixFDs(t *testing.T) {
conn, err := ConnectSessionBus()
if err != nil {
t.Fatal(err)
}
defer conn.Close()
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
defer w.Close()
name := conn.Names()[0]
test := unixFDTest{t}
err = conn.Export(test, "/com/github/guelfey/test", "com.github.guelfey.test")
if err != nil {
t.Fatal(err)
}
var s string
obj := conn.Object(name, "/com/github/guelfey/test")
if _, err := w.Write([]byte(testString)); err != nil {
t.Fatal(err)
}
err = obj.Call("com.github.guelfey.test.Testfd", 0, UnixFD(r.Fd())).Store(&s)
if err != nil {
t.Fatal(err)
}
if s != testString {
t.Fatal("got", s, "wanted", testString)
}
if _, err := w.Write([]byte(testString)); err != nil {
t.Fatal(err)
}
err = obj.Call("com.github.guelfey.test.Testvariant", 0, MakeVariant(UnixFD(r.Fd()))).Store(&s)
if err != nil {
t.Fatal(err)
}
if s != testString {
t.Fatal("got", s, "wanted", testString)
}
if _, err := w.Write([]byte(testString)); err != nil {
t.Fatal(err)
}
err = obj.Call("com.github.guelfey.test.Teststruct", 0, unixfdContainer{UnixFD(r.Fd())}).Store(&s)
if err != nil {
t.Fatal(err)
}
if s != testString {
t.Fatal("got", s, "wanted", testString)
}
if _, err := w.Write([]byte(testString)); err != nil {
t.Fatal(err)
}
err = obj.Call("com.github.guelfey.test.Testvariantstruct", 0, MakeVariant(unixfdContainer{UnixFD(r.Fd())})).Store(&s)
if err != nil {
t.Fatal(err)
}
if s != testString {
t.Fatal("got", s, "wanted", testString)
}
if _, err := w.Write([]byte(testString)); err != nil {
t.Fatal(err)
}
err = obj.Call("com.github.guelfey.test.Teststructvariant", 0, variantContainer{MakeVariant(UnixFD(r.Fd()))}).Store(&s)
if err != nil {
t.Fatal(err)
}
if s != testString {
t.Fatal("got", s, "wanted", testString)
}
}