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
|
package main
import (
"bytes"
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestSetupApplication(t *testing.T) {
toolkit := NewRouterTestToolkit(t, false)
settings := make(map[string]interface{})
assert.Equal(t, http.StatusServiceUnavailable, toolkit.MakeRequest("GET", "/api/rules", nil).Code)
assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("POST", "/setup", settings).Code)
settings["config"] = Config{ServerAddress: "1.2.3.4", FlagRegex: "FLAG{test}", AuthRequired: true}
assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("POST", "/setup", settings).Code)
settings["accounts"] = gin.Accounts{"username": "password"}
assert.Equal(t, http.StatusAccepted, toolkit.MakeRequest("POST", "/setup", settings).Code)
assert.Equal(t, http.StatusNotFound, toolkit.MakeRequest("POST", "/setup", settings).Code)
toolkit.wrapper.Destroy(t)
}
func TestAuthRequired(t *testing.T) {
toolkit := NewRouterTestToolkit(t, true)
assert.Equal(t, http.StatusOK, toolkit.MakeRequest("GET", "/api/rules", nil).Code)
config := toolkit.appContext.Config
config.AuthRequired = true
toolkit.appContext.SetConfig(config)
toolkit.appContext.SetAccounts(gin.Accounts{"username": "password"})
assert.Equal(t, http.StatusUnauthorized, toolkit.MakeRequest("GET", "/api/rules", nil).Code)
toolkit.wrapper.Destroy(t)
}
func TestRulesApi(t *testing.T) {
toolkit := NewRouterTestToolkit(t, true)
// AddRule
assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("POST", "/api/rules", Rule{}).Code)
assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("POST", "/api/rules",
Rule{Name: "testRule"}).Code)
assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("POST", "/api/rules",
Rule{Name: "testRule", Color: "invalidColor"}).Code)
w := toolkit.MakeRequest("POST", "/api/rules", Rule{Name: "testRule", Color: "#fff"})
var testRuleID struct{ ID string }
assert.Equal(t, http.StatusOK, w.Code)
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &testRuleID))
assert.Equal(t, http.StatusUnprocessableEntity, toolkit.MakeRequest("POST", "/api/rules",
Rule{Name: "testRule", Color: "#fff"}).Code) // same name
// UpdateRule
assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("PUT", "/api/rules/invalidID",
Rule{Name: "invalidRule", Color: "#000"}).Code)
assert.Equal(t, http.StatusNotFound, toolkit.MakeRequest("PUT", "/api/rules/000000000000000000000000",
Rule{Name: "invalidRule", Color: "#000"}).Code)
assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("PUT", "/api/rules/"+testRuleID.ID, Rule{}).Code)
assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("PUT", "/api/rules/"+testRuleID.ID,
Rule{Name: "invalidRule", Color: "invalidColor"}).Code)
w = toolkit.MakeRequest("POST", "/api/rules", Rule{Name: "testRule2", Color: "#eee"})
var testRule2ID struct{ ID string }
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &testRule2ID))
assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("PUT", "/api/rules/"+testRule2ID.ID,
Rule{Name: "testRule", Color: "#fff"}).Code) // duplicate
w = toolkit.MakeRequest("PUT", "/api/rules/"+testRuleID.ID, Rule{Name: "newRule1", Color: "#ddd"})
var testRule Rule
assert.Equal(t, http.StatusOK, w.Code)
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &testRule))
assert.Equal(t, "newRule1", testRule.Name)
assert.Equal(t, "#ddd", testRule.Color)
// GetRule
assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("GET", "/api/rules/invalidID", nil).Code)
assert.Equal(t, http.StatusNotFound, toolkit.MakeRequest("GET", "/api/rules/000000000000000000000000", nil).Code)
w = toolkit.MakeRequest("GET", "/api/rules/"+testRuleID.ID, nil)
assert.Equal(t, http.StatusOK, w.Code)
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &testRule))
assert.Equal(t, testRuleID.ID, testRule.ID.Hex())
assert.Equal(t, "newRule1", testRule.Name)
assert.Equal(t, "#ddd", testRule.Color)
// GetRules
w = toolkit.MakeRequest("GET", "/api/rules", nil)
var rules []Rule
assert.Equal(t, http.StatusOK, w.Code)
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &rules))
assert.Len(t, rules, 3)
toolkit.wrapper.Destroy(t)
}
func TestPcapImporterApi(t *testing.T) {
toolkit := NewRouterTestToolkit(t, true)
// Import pcap
assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("POST", "/api/pcap/file", nil).Code)
assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("POST", "/api/pcap/file",
gin.H{"file": "invalidPath"}).Code)
w := toolkit.MakeRequest("POST", "/api/pcap/file", gin.H{"file": "test_data/ping_pong_10000.pcap"})
var sessionID struct{ Session string }
assert.Equal(t, http.StatusAccepted, w.Code)
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &sessionID))
assert.Equal(t, "369ef4b6abb6214b4ee2e0c81ecb93c49e275c26c85e30493b37727d408cf280", sessionID.Session)
assert.Equal(t, http.StatusUnprocessableEntity, toolkit.MakeRequest("POST", "/api/pcap/file",
gin.H{"file": "test_data/ping_pong_10000.pcap"}).Code) // duplicate
// Get sessions
var sessions []ImportingSession
w = toolkit.MakeRequest("GET", "/api/pcap/sessions", nil)
assert.Equal(t, http.StatusOK, w.Code)
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &sessions))
assert.Len(t, sessions, 1)
assert.Equal(t, sessionID.Session, sessions[0].ID)
// Get session
var session ImportingSession
w = toolkit.MakeRequest("GET", "/api/pcap/sessions/"+sessionID.Session, nil)
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &session))
assert.Equal(t, sessionID.Session, session.ID)
// Cancel session
assert.Equal(t, http.StatusNotFound, toolkit.MakeRequest("DELETE", "/api/pcap/sessions/invalidSession",
nil).Code)
assert.Equal(t, http.StatusAccepted, toolkit.MakeRequest("DELETE", "/api/pcap/sessions/"+sessionID.Session,
nil).Code)
time.Sleep(1*time.Second) // wait for termination
toolkit.wrapper.Destroy(t)
}
type RouterTestToolkit struct {
appContext *ApplicationContext
wrapper *TestStorageWrapper
router *gin.Engine
t *testing.T
}
func NewRouterTestToolkit(t *testing.T, withSetup bool) *RouterTestToolkit {
wrapper := NewTestStorageWrapper(t)
wrapper.AddCollection(Settings)
appContext, err := CreateApplicationContext(wrapper.Storage, "test")
require.NoError(t, err)
gin.SetMode(gin.ReleaseMode)
notificationController := NewNotificationController(appContext)
go notificationController.Run()
router := CreateApplicationRouter(appContext, notificationController)
toolkit := RouterTestToolkit{
appContext: appContext,
wrapper: wrapper,
router: router,
t: t,
}
if withSetup {
settings := gin.H{
"config": Config{ServerAddress: "1.2.3.4", FlagRegex: "FLAG{test}", AuthRequired: false},
"accounts": gin.Accounts{},
}
toolkit.MakeRequest("POST", "/setup", settings)
}
return &toolkit
}
func (rtt *RouterTestToolkit) MakeRequest(method string, url string, body interface{}) *httptest.ResponseRecorder {
var r io.Reader
if body != nil {
buf, err := json.Marshal(body)
require.NoError(rtt.t, err)
r = bytes.NewBuffer(buf)
}
w := httptest.NewRecorder()
req, err := http.NewRequest(method, url, r)
require.NoError(rtt.t, err)
rtt.router.ServeHTTP(w, req)
return w
}
|