-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmessage_processor_struct_test.go
91 lines (86 loc) · 2.91 KB
/
message_processor_struct_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
package toolbox
import (
"reflect"
"strings"
"testing"
"time"
)
func TestSubjectReportRequest_Lint(t *testing.T) {
// Lint a request with a text comment
req := SubjectReportRequest{
SubjectIP: strings.Repeat("I", 1000),
SubjectHostName: strings.Repeat("H", 1000),
SubjectPlatform: strings.Repeat("P", 1000),
SubjectComment: strings.Repeat("C", 10000),
CommandRequest: AppCommandRequest{Command: strings.Repeat("R", MaxCmdLength+100)},
CommandResponse: AppCommandResponse{Command: strings.Repeat("W", MaxCmdLength+100)},
}
req.Lint()
if req.SubjectIP != strings.Repeat("I", 64) {
t.Fatal(req.SubjectIP)
}
if req.SubjectHostName != strings.Repeat("H", 256) {
t.Fatal(req.SubjectHostName)
}
if req.SubjectPlatform != strings.Repeat("P", 128) {
t.Fatal(req.SubjectPlatform)
}
if req.SubjectComment != strings.Repeat("C", 4*1024) {
t.Fatal(req.SubjectComment)
}
if req.CommandRequest.Command != strings.Repeat("R", MaxCmdLength) {
t.Fatal(req.CommandRequest.Command)
}
if req.CommandResponse.Command != strings.Repeat("W", MaxCmdLength) {
t.Fatal(req.CommandResponse.Command)
}
// Lint a request with a JSON object as comment
req = SubjectReportRequest{
SubjectIP: strings.Repeat("I", 1000),
SubjectHostName: strings.Repeat("H", 1000),
SubjectPlatform: strings.Repeat("P", 1000),
SubjectComment: map[string]interface{}{"key": "value"},
CommandRequest: AppCommandRequest{Command: strings.Repeat("R", MaxCmdLength+100)},
CommandResponse: AppCommandResponse{Command: strings.Repeat("W", MaxCmdLength+100)},
}
req.Lint()
if !reflect.DeepEqual(req.SubjectComment, map[string]interface{}{"key": "value"}) {
t.Fatal(req.SubjectComment)
}
}
func TestSubjectReportRequest_SerialiseCompact(t *testing.T) {
req := SubjectReportRequest{
SubjectIP: "123.132.123.123",
SubjectHostName: "hzgl-dev-abc.example.com",
SubjectPlatform: "windows/amd64",
SubjectComment: "hello there\nsecond line",
CommandRequest: AppCommandRequest{
Command: "123456098765.s start-computer",
},
CommandResponse: AppCommandResponse{
Command: "123456098765.s stop-computer",
ReceivedAt: time.Unix(1234567890, 0),
Result: "stopped the computer all right\nsecond line",
RunDurationSec: 182,
},
}
serialised := req.SerialiseCompact()
t.Log(len(serialised))
t.Log(serialised)
// Deserialise the complete string
var deserialised SubjectReportRequest
if err := deserialised.DeserialiseFromCompact(serialised); err != nil {
t.Log(err)
}
if !reflect.DeepEqual(deserialised, req) {
t.Fatalf("\n%+v\n%+v\n", deserialised, req)
}
// Deserialise truncated string
var deserialised2 SubjectReportRequest
if err := deserialised2.DeserialiseFromCompact(serialised[:30]); err != ErrSubjectReportTruncated {
t.Fatal(err)
}
if deserialised2.SubjectHostName != "hzgl-dev-abc.example.com" || deserialised2.CommandRequest.Command != "12345" {
t.Fatalf("%+v", deserialised2)
}
}