forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin_test.go
90 lines (77 loc) · 2.67 KB
/
admin_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
/*
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 actions_test
import (
"context"
"strings"
"testing"
"github.com/shirou/gopsutil/host"
assert "github.com/stretchr/testify/assert"
"www.velocidex.com/golang/velociraptor/actions"
actions_proto "www.velocidex.com/golang/velociraptor/actions/proto"
api_proto "www.velocidex.com/golang/velociraptor/api/proto"
"www.velocidex.com/golang/velociraptor/config"
crypto_proto "www.velocidex.com/golang/velociraptor/crypto/proto"
"www.velocidex.com/golang/velociraptor/responder"
)
func GetResponsesFromAction(
config_obj *api_proto.Config,
action actions.ClientAction,
ctx context.Context,
args *crypto_proto.GrrMessage) []*crypto_proto.GrrMessage {
c := make(chan *crypto_proto.GrrMessage)
result := []*crypto_proto.GrrMessage{}
go func() {
defer close(c)
action.Run(config_obj, ctx, args, c)
}()
for {
item, ok := <-c
if !ok {
return result
}
result = append(result, item)
}
}
func TestGetHostname(t *testing.T) {
config_obj := config.GetDefaultConfig()
ctx := context.Background()
get_hostname := actions.GetHostname{}
arg, err := responder.NewRequest(&crypto_proto.GrrMessage{})
if err != nil {
t.Fatal(err)
}
responses := GetResponsesFromAction(config_obj, &get_hostname, ctx, arg)
assert.Equal(t, len(responses), 2)
response := responder.ExtractGrrMessagePayload(
responses[0]).(*actions_proto.DataBlob)
info, _ := host.Info()
assert.Equal(t, info.Hostname, response.String_)
}
func TestGetPlatformInfo(t *testing.T) {
config_obj := config.GetDefaultConfig()
ctx := context.Background()
get_platform_info := actions.GetPlatformInfo{}
arg, err := responder.NewRequest(&crypto_proto.GrrMessage{})
if err != nil {
t.Fatal(err)
}
responses := GetResponsesFromAction(config_obj, &get_platform_info, ctx, arg)
assert.Equal(t, len(responses), 2)
response := responder.ExtractGrrMessagePayload(
responses[0]).(*actions_proto.Uname)
info, _ := host.Info()
assert.Equal(t, strings.Title(info.OS), response.System)
}