-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
172 lines (155 loc) · 4.04 KB
/
context_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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package dbus
import (
"context"
"os"
"testing"
)
func TestContextHeader(t *testing.T) {
var conn *Conn
tests := []struct {
name string
hdr header
wantEmitter Interface
wantSender Peer
wantDestination Peer
}{
{
name: "call",
hdr: header{
Type: msgTypeCall,
Version: 1,
Serial: 1234,
Path: "/foo/bar",
Interface: "org.test.Foo",
Member: "Bar",
Destination: "org.test.Peer",
},
wantEmitter: Interface{},
wantSender: Peer{},
wantDestination: conn.Peer("org.test.Peer"),
},
{
name: "return",
hdr: header{
Type: msgTypeReturn,
Version: 1,
Serial: 1234,
Sender: ":1.234",
Destination: ":2.345",
ReplySerial: 2,
},
wantEmitter: Interface{},
wantSender: conn.Peer(":1.234"),
wantDestination: conn.Peer(":2.345"),
},
{
name: "error",
hdr: header{
Type: msgTypeError,
Version: 1,
Serial: 1234,
Sender: ":1.234",
Destination: ":2.345",
ReplySerial: 2,
ErrName: "org.Test.Error",
},
wantEmitter: Interface{},
wantSender: conn.Peer(":1.234"),
wantDestination: conn.Peer(":2.345"),
},
{
name: "signal",
hdr: header{
Type: msgTypeSignal,
Version: 1,
Serial: 1234,
Sender: ":1.234",
Path: "/foo/bar",
Interface: "org.test.Interface",
Member: "Signal",
},
wantEmitter: conn.Peer(":1.234").Object("/foo/bar").Interface("org.test.Interface"),
wantSender: conn.Peer(":1.234"),
wantDestination: Peer{},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ctx := withContextHeader(context.Background(), conn, &tc.hdr)
gotEmitter, ok := ContextEmitter(ctx)
wantOK := tc.wantEmitter.Name() != ""
t.Logf("ContextEmitter() = %s, %v", gotEmitter, ok)
if ok {
if got, want := gotEmitter.String(), tc.wantEmitter.String(); got != want {
t.Errorf("wrong emitter, got %q want %q", got, want)
}
} else if wantOK {
t.Errorf("emitter not found in call context, want %q", tc.wantEmitter)
}
gotSender, ok := ContextSender(ctx)
wantOK = tc.wantSender.Name() != ""
t.Logf("ContextSender() = %s, %v", gotSender, ok)
if ok {
if got, want := gotSender.String(), tc.wantSender.String(); got != want {
t.Errorf("wrong sender, got %q want %q", got, want)
}
} else if wantOK {
t.Errorf("sender not found in call context, want %q", tc.wantSender)
}
gotDestination, ok := ContextDestination(ctx)
wantOK = tc.wantDestination.Name() != ""
t.Logf("ContextDestination() = %s, %v", gotDestination, ok)
if ok {
if got, want := gotDestination.String(), tc.wantDestination.String(); got != want {
t.Errorf("wrong destination, got %q want %q", got, want)
}
} else if wantOK {
t.Errorf("destination not found in call context, want %q", tc.wantDestination)
}
})
}
}
func TestContextFile(t *testing.T) {
var want []*os.File
for range 2 {
f, err := os.CreateTemp(t.TempDir(), "contextfile")
if err != nil {
t.Fatal(err)
}
defer f.Close()
want = append(want, f)
}
ctx := withContextFiles(context.Background(), &want)
for i := range 2 {
got := contextFile(ctx, uint32(i))
if got == nil {
t.Fatal("file not found in context")
}
if got != want[i] {
t.Fatalf("wrong file received, got %p, want file %d from %v", got, i, want)
}
}
got := contextFile(ctx, 2)
if got != nil {
t.Fatalf("got unexpected file %p from %v", got, want)
}
add, err := os.CreateTemp(t.TempDir(), "contextfile")
if err != nil {
t.Fatal(err)
}
defer add.Close()
gotIdx, err := contextPutFile(ctx, add)
if err != nil {
t.Fatalf("failed to put file: %v", err)
}
if want := uint32(2); gotIdx != want {
t.Fatalf("unexpected file index after put, got %d want %d", gotIdx, want)
}
got = contextFile(ctx, 2)
if got == nil {
t.Fatal("newly added file not found in context")
}
if got != add {
t.Fatalf("wrong file received, got %p, want %p", got, add)
}
}