forked from osquery/osquery-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
197 lines (174 loc) · 5.24 KB
/
server_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
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
package osquery
import (
"context"
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"sync"
"syscall"
"testing"
"time"
"github.com/apache/thrift/lib/go/thrift"
"github.com/kolide/osquery-go/gen/osquery"
"github.com/kolide/osquery-go/plugin/logger"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Verify that an error in server.Start will return an error instead of deadlock.
func TestNoDeadlockOnError(t *testing.T) {
registry := make(map[string](map[string]OsqueryPlugin))
for reg, _ := range validRegistryNames {
registry[reg] = make(map[string]OsqueryPlugin)
}
mut := sync.Mutex{}
mock := &MockExtensionManager{
RegisterExtensionFunc: func(info *osquery.InternalExtensionInfo, registry osquery.ExtensionRegistry) (*osquery.ExtensionStatus, error) {
mut.Lock()
defer mut.Unlock()
return nil, errors.New("boom!")
},
PingFunc: func() (*osquery.ExtensionStatus, error) {
return &osquery.ExtensionStatus{}, nil
},
}
server := &ExtensionManagerServer{
serverClient: mock,
registry: registry,
}
log := func(ctx context.Context, typ logger.LogType, logText string) error {
fmt.Printf("%s: %s\n", typ, logText)
return nil
}
server.RegisterPlugin(logger.NewPlugin("testLogger", log))
err := server.Run()
assert.Error(t, err)
mut.Lock()
defer mut.Unlock()
assert.True(t, mock.RegisterExtensionFuncInvoked)
}
// Ensure that the extension server will shutdown and return if the osquery
// instance it is talking to stops responding to pings.
func TestShutdownWhenPingFails(t *testing.T) {
registry := make(map[string](map[string]OsqueryPlugin))
for reg, _ := range validRegistryNames {
registry[reg] = make(map[string]OsqueryPlugin)
}
mock := &MockExtensionManager{
RegisterExtensionFunc: func(info *osquery.InternalExtensionInfo, registry osquery.ExtensionRegistry) (*osquery.ExtensionStatus, error) {
return &osquery.ExtensionStatus{}, nil
},
PingFunc: func() (*osquery.ExtensionStatus, error) {
// As if the socket was closed
return nil, syscall.EPIPE
},
}
server := &ExtensionManagerServer{
serverClient: mock,
registry: registry,
}
err := server.Run()
assert.Error(t, err)
assert.Contains(t, err.Error(), "broken pipe")
}
// How many parallel tests to run (because sync issues do not occur on every
// run, this maximizes our chances of seeing any issue by quickly executing
// many runs of the test).
const parallelTestShutdownDeadlock = 20
func TestShutdownDeadlock(t *testing.T) {
for i := 0; i < parallelTestShutdownDeadlock; i++ {
t.Run("", func(t *testing.T) {
t.Parallel()
testShutdownDeadlock(t)
})
}
}
func testShutdownDeadlock(t *testing.T) {
tempPath, err := ioutil.TempFile("", "")
require.Nil(t, err)
defer os.Remove(tempPath.Name())
retUUID := osquery.ExtensionRouteUUID(0)
mock := &MockExtensionManager{
RegisterExtensionFunc: func(info *osquery.InternalExtensionInfo, registry osquery.ExtensionRegistry) (*osquery.ExtensionStatus, error) {
return &osquery.ExtensionStatus{Code: 0, UUID: retUUID}, nil
},
}
server := ExtensionManagerServer{serverClient: mock, sockPath: tempPath.Name()}
wait := sync.WaitGroup{}
wait.Add(1)
go func() {
err := server.Start()
require.Nil(t, err)
wait.Done()
}()
// Wait for server to be set up
server.waitStarted()
// Create a raw client to access the shutdown method that is not
// usually exposed.
listenPath := fmt.Sprintf("%s.%d", tempPath.Name(), retUUID)
addr, err := net.ResolveUnixAddr("unix", listenPath)
require.Nil(t, err)
timeout := 500 * time.Millisecond
trans := thrift.NewTSocketFromAddrTimeout(addr, timeout)
err = trans.Open()
require.Nil(t, err)
client := osquery.NewExtensionManagerClientFactory(trans,
thrift.NewTBinaryProtocolFactoryDefault())
// Simultaneously call shutdown through a request from the client and
// directly on the server object.
wait.Add(1)
go func() {
defer wait.Done()
client.Shutdown(context.Background())
}()
wait.Add(1)
go func() {
defer wait.Done()
err = server.Shutdown(context.Background())
require.Nil(t, err)
}()
// Track whether shutdown completed
completed := make(chan struct{})
go func() {
wait.Wait()
close(completed)
}()
// either indicate successful shutdown, or fatal the test because it
// hung
select {
case <-completed:
// Success. Do nothing.
case <-time.After(5 * time.Second):
t.Fatal("hung on shutdown")
}
}
func TestShutdownBasic(t *testing.T) {
tempPath, err := ioutil.TempFile("", "")
require.Nil(t, err)
defer os.Remove(tempPath.Name())
retUUID := osquery.ExtensionRouteUUID(0)
mock := &MockExtensionManager{
RegisterExtensionFunc: func(info *osquery.InternalExtensionInfo, registry osquery.ExtensionRegistry) (*osquery.ExtensionStatus, error) {
return &osquery.ExtensionStatus{Code: 0, UUID: retUUID}, nil
},
}
server := ExtensionManagerServer{serverClient: mock, sockPath: tempPath.Name()}
completed := make(chan struct{})
go func() {
err := server.Start()
require.NoError(t, err)
close(completed)
}()
server.waitStarted()
err = server.Shutdown(context.Background())
require.NoError(t, err)
// Either indicate successful shutdown, or fatal the test because it
// hung
select {
case <-completed:
// Success. Do nothing.
case <-time.After(5 * time.Second):
t.Fatal("hung on shutdown")
}
}