forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsender_test.go
210 lines (166 loc) · 6.01 KB
/
sender_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
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
Velociraptor - Hunting Evil
Copyright (C) 2019 Velocidex Innovations.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package http_comms
import (
"bytes"
"context"
"io/ioutil"
"net/http"
"os"
"sync"
"testing"
"time"
errors "github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"www.velocidex.com/golang/velociraptor/config"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
"www.velocidex.com/golang/velociraptor/crypto"
crypto_proto "www.velocidex.com/golang/velociraptor/crypto/proto"
"www.velocidex.com/golang/velociraptor/executor"
"www.velocidex.com/golang/velociraptor/logging"
"www.velocidex.com/golang/velociraptor/utils"
)
type MockHTTPConnector struct {
wg *sync.WaitGroup
mu sync.Mutex
received []string
connected bool
t *testing.T
}
func (self *MockHTTPConnector) GetCurrentUrl(handler string) string { return "http://URL/" + handler }
func (self *MockHTTPConnector) Post(handler string, data []byte, urgent bool) (*http.Response, error) {
self.mu.Lock()
defer self.mu.Unlock()
// Emulate an error if we are not connected.
if !self.connected {
return nil, errors.New("Unavailable")
}
defer self.wg.Done()
manager := crypto.NullCryptoManager{}
message_info, err := manager.Decrypt(data)
require.NoError(self.t, err)
message_info.IterateJobs(context.Background(),
func(ctx context.Context, item *crypto_proto.GrrMessage) {
self.received = append(self.received, item.Name)
})
return &http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("")),
StatusCode: 200,
}, nil
}
func (self *MockHTTPConnector) ReKeyNextServer() {}
func (self *MockHTTPConnector) ServerName() string { return "VelociraptorServer" }
// Try to send the message immediately. If we get through we increase the wg.
func CanSendToExecutor(
wg *sync.WaitGroup,
exec *executor.ClientExecutor,
msg *crypto_proto.GrrMessage) bool {
select {
case exec.Outbound <- msg:
// Add to the wg a task - this will be subtracted when
// the final post is made.
wg.Add(1)
return true
case <-time.After(500 * time.Millisecond):
return false
}
}
func testRingBuffer(
rb IRingBuffer,
config_obj *config_proto.Config,
message string,
t *testing.T) {
t.Parallel()
manager := &crypto.NullCryptoManager{}
exe := &executor.ClientExecutor{
Inbound: make(chan *crypto_proto.GrrMessage),
Outbound: make(chan *crypto_proto.GrrMessage),
}
logger := logging.GetLogger(config_obj, &logging.ClientComponent)
// Set global timeout on the test.
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
wg := &sync.WaitGroup{}
connector := &MockHTTPConnector{wg: wg, t: t}
// The connector is not connected initially.
connector.connected = false
sender, err := NewSender(
config_obj, connector, manager, exe, rb, nil, /* enroller */
logger, "Sender", "control", nil, &utils.RealClock{})
assert.NoError(t, err)
sender.Start(ctx)
// This message results in a 14 byte message enqueued. The
// RingBuffer is allowed to go over the size slightly but will
// prevent more messages from being generated. This ensures
// that all messages will be written to disk in case of a
// crash.
msg := &crypto_proto.GrrMessage{
Name: message,
}
// The first message will be enqueued.
assert.Equal(t, CanSendToExecutor(wg, exe, msg), true)
// These messages can not be sent since there is no room in
// the buffer.
assert.Equal(t, CanSendToExecutor(wg, exe, msg), false)
// Nothing is received yet since the connector is
// disconnected.
assert.Equal(t, len(connector.received), 0)
// The ring buffer is holding 14 bytes since none were
// successfully sent yet.
assert.Equal(t, sender.ring_buffer.AvailableBytes(), uint64(14))
// Turn the connector on - now sending will be successful. We
// need to wait for the communicator to retry sending.
connector.mu.Lock()
connector.connected = true
connector.mu.Unlock()
// Wait until the messages are delivered.
wg.Wait()
// There is one message received
assert.Equal(t, len(connector.received), 1)
// Wait for the messages to be committed in the ring buffer.
time.Sleep(500 * time.Millisecond)
// The ring buffer is now truncated to 0.
assert.Equal(t, sender.ring_buffer.AvailableBytes(), uint64(0))
// We can send more messages.
assert.Equal(t, CanSendToExecutor(wg, exe, msg), true)
}
func TestSender(t *testing.T) {
config_obj := config.GetDefaultConfig()
config_obj.Client.MaxPoll = 1
config_obj.Client.MaxPollStd = 1
// Make the ring buffer 10 bytes - this is enough for one
// message but no more.
rb := NewRingBuffer(config_obj, 10)
testRingBuffer(rb, config_obj, "0123456789", t)
}
func TestSenderWithFileBuffer(t *testing.T) {
config_obj := config.GetDefaultConfig()
tmpfile, err := ioutil.TempFile("", "test")
require.NoError(t, err)
defer os.Remove(tmpfile.Name())
tmpfile.Close()
// Make the ring buffer 10 bytes - this is enough for one
// message but no more.
config_obj.Client.LocalBuffer.DiskSize = 10
config_obj.Client.LocalBuffer.FilenameLinux = tmpfile.Name()
config_obj.Client.LocalBuffer.FilenameWindows = tmpfile.Name()
config_obj.Client.LocalBuffer.FilenameDarwin = tmpfile.Name()
config_obj.Client.MaxPoll = 1
config_obj.Client.MaxPollStd = 1
logger := logging.GetLogger(config_obj, &logging.ClientComponent)
rb, err := NewFileBasedRingBuffer(config_obj, logger)
require.NoError(t, err)
testRingBuffer(rb, config_obj, "0123456789", t)
}