-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol_message_test.go
199 lines (181 loc) · 5.57 KB
/
protocol_message_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
package arbor_test
import (
"strings"
"testing"
"time"
arbor "github.com/arborchat/arbor-go"
)
const (
testRoot = "root"
testID1 = "1"
testID2 = "2"
testID3 = "3"
testID4 = "4"
testUser = "testopheles"
testBadMessageType = 99
)
func getWelcome() *arbor.ProtocolMessage {
return &arbor.ProtocolMessage{
Type: arbor.WelcomeType,
Root: testRoot,
Recent: []string{testID1, testID2, testID3, testID4},
Major: 0,
Minor: 1,
}
}
func getNew() *arbor.ProtocolMessage {
return &arbor.ProtocolMessage{
Type: arbor.NewMessageType,
ChatMessage: &arbor.ChatMessage{
UUID: testID1,
Parent: testID2,
Username: testUser,
Timestamp: time.Now().Unix(),
Content: testContent,
},
}
}
func getQuery() *arbor.ProtocolMessage {
return &arbor.ProtocolMessage{
Type: arbor.QueryType,
ChatMessage: &arbor.ChatMessage{
UUID: testID1,
},
}
}
func getMeta() *arbor.ProtocolMessage {
return &arbor.ProtocolMessage{
Type: arbor.MetaType,
Meta: map[string]string{"key": "value"},
}
}
func getInvalid() *arbor.ProtocolMessage {
return &arbor.ProtocolMessage{Type: testBadMessageType}
}
func contains(input string, noneOf []string, eachOf []string) bool {
return !containsAnyOf(input, noneOf) && !containsEachOf(input, eachOf)
}
var (
// define fields that are only legal in specific message types for use in
// serialization tests
welcomeOnlyFields = []string{"Root", "Recent", "Major", "Minor"}
welcomeAllFields = append(welcomeOnlyFields, "Type")
newOnlyFields = []string{"Content", "Parent", "Username", "Timestamp"}
newAllFields = append(newOnlyFields, "Type", "UUID")
metaOnlyFields = []string{"Meta"}
metaAllFields = append(metaOnlyFields, "Type")
queryAllFields = []string{"Type", "UUID"}
)
// TestMarshalJSONWelcome ensures that the JSON serialization for WELCOME messages works
// and does not include message fields irrelevant for that message type.
func TestMarshalJSONWelcome(t *testing.T) {
strWelcome := marshalOrFail(t, getWelcome())
if strWelcome == "" {
t.Error("Received empty string")
}
if contains(strWelcome, append(newOnlyFields, metaOnlyFields...), welcomeAllFields) {
t.Error("Welcome message contained invalid fields", strWelcome)
}
}
// TestMarshalJSONNew ensures that the JSON serialization for NEW messages works
// and does not include message fields irrelevant for that message type.
func TestMarshalJSONNew(t *testing.T) {
strNewmessage := marshalOrFail(t, getNew())
if strNewmessage == "" {
t.Error("Received empty string")
}
if contains(strNewmessage, append(welcomeOnlyFields, metaOnlyFields...), newAllFields) {
t.Error("New message contained invalid fields", strNewmessage)
}
}
// TestMarshalJSONQuery ensures that the JSON serialization for QUERY messages works
// and does not include message fields irrelevant for that message type.
func TestMarshalJSONQuery(t *testing.T) {
strQuery := marshalOrFail(t, getQuery())
if strQuery == "" {
t.Error("Received empty string")
}
if contains(strQuery, append(append(welcomeOnlyFields, newAllFields...), metaOnlyFields...), queryAllFields) {
t.Error("Query message contained invalid fields", strQuery)
}
}
// TestMarshalJSONMeta ensures that the JSON serialization for META messages works
// and does not include message fields irrelevant for that message type.
func TestMarshalJSONMeta(t *testing.T) {
strMeta := marshalOrFail(t, getMeta())
if strMeta == "" {
t.Error("Received empty string")
}
if contains(strMeta, append(welcomeOnlyFields, newAllFields...), metaAllFields) {
t.Error("Query message contained invalid fields", strMeta)
}
}
// TestMarshalJSONInvalid ensures that the JSON serialization for invalid messages fails
func TestMarshalJSONInvalid(t *testing.T) {
badOutput, err := getInvalid().MarshalJSON()
if err == nil {
t.Error("Marshalling bad message type should be error")
}
if badOutput != nil {
t.Error("Marshalling bad message type should return nil slice, got", badOutput)
}
}
// marshals the message into JSON, failing the test if the marshal returns an error.
func marshalOrFail(t *testing.T, m *arbor.ProtocolMessage) string {
marhalled, err := m.MarshalJSON()
if err != nil {
t.Error(err)
}
return string(marhalled)
}
// TestString ensures that String() returns an non-empty string for valid messages and
// and empty string for invalid ones
func TestString(t *testing.T) {
msgFuncs := []func() *arbor.ProtocolMessage{getNew, getQuery, getWelcome}
for _, function := range msgFuncs {
m := function()
s := m.String()
if s == "" {
t.Error("String should not return empty string for valid message", m)
}
}
m := getInvalid()
s := m.String()
if s != "" {
t.Error("String should be empty for invalid message", m)
}
}
func containsAnyOf(input string, targets []string) bool {
for _, s := range targets {
if strings.Contains(input, s) {
return true
}
}
return false
}
func containsEachOf(input string, targets []string) bool {
count := 0
for _, s := range targets {
if strings.Contains(input, s) {
count++
}
}
return count == len(targets)
}
// TestRootIsValid ensures that our validation logic for messages takes the special properties
// of root messages (namely their lack of a parent id) into account.
func TestRootIsValid(t *testing.T) {
rootMsg := &arbor.ProtocolMessage{
Type: arbor.NewMessageType,
ChatMessage: &arbor.ChatMessage{
UUID: "Something",
Parent: "",
Username: "thing",
Content: "Stuff",
Timestamp: time.Now().Unix(),
},
}
if !rootMsg.IsValid() {
t.Error("Root message should be considered valid")
}
}