aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmiliano Ciavatta2020-04-21 20:17:13 +0000
committerEmiliano Ciavatta2020-04-21 20:17:13 +0000
commit111bef5ea1c3eb28329715cb0598a6d7363853e2 (patch)
treeb1eb673b7367ec8ff0bef5ef5410900f446c9be0
parentf8dd01e9cd59ff7c0920eab3dd65c02a15de059e (diff)
Add application_router tests
-rw-r--r--application_router.go4
-rw-r--r--application_router_test.go73
-rw-r--r--pcap_importer_test.go4
-rw-r--r--rules_manager.go4
4 files changed, 79 insertions, 6 deletions
diff --git a/application_router.go b/application_router.go
index bdc8007..5ee3d62 100644
--- a/application_router.go
+++ b/application_router.go
@@ -86,10 +86,10 @@ func CreateApplicationRouter(applicationContext *ApplicationContext) *gin.Engine
return
}
- updated, err := applicationContext.RulesManager.UpdateRule(c, id, rule)
+ isPresent, err := applicationContext.RulesManager.UpdateRule(c, id, rule)
if err != nil {
badRequest(c, err)
- } else if !updated {
+ } else if !isPresent {
notFound(c, UnorderedDocument{"id": id})
} else {
success(c, rule)
diff --git a/application_router_test.go b/application_router_test.go
index ece38f1..8c94127 100644
--- a/application_router_test.go
+++ b/application_router_test.go
@@ -40,6 +40,79 @@ func TestAuthRequired(t *testing.T) {
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)
+
+ assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("POST", "/api/pcap/file", nil).Code)
+ assert.Equal(t, http.StatusUnprocessableEntity, toolkit.MakeRequest("POST", "/api/pcap/file",
+ gin.H{"path": "invalidPath"}).Code)
+ w := toolkit.MakeRequest("POST", "/api/pcap/file", gin.H{"path": "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{"path": "test_data/ping_pong_10000.pcap"}).Code) // duplicate
+
+ toolkit.wrapper.Destroy(t)
+}
+
type RouterTestToolkit struct {
appContext *ApplicationContext
wrapper *TestStorageWrapper
diff --git a/pcap_importer_test.go b/pcap_importer_test.go
index 8d03e4e..cafa7a0 100644
--- a/pcap_importer_test.go
+++ b/pcap_importer_test.go
@@ -53,7 +53,7 @@ func TestCancelImportSession(t *testing.T) {
session := waitSessionCompletion(t, pcapImporter, sessionID)
assert.Zero(t, session.CompletedAt)
- assert.Equal(t, 1270696, session.Size)
+ assert.Equal(t, int64(1270696), session.Size)
assert.Equal(t, 0, session.ProcessedPackets)
assert.Equal(t, 0, session.InvalidPackets)
assert.Equal(t, map[uint16]flowCount{}, session.PacketsPerService)
@@ -72,7 +72,7 @@ func TestImportNoTcpPackets(t *testing.T) {
require.NoError(t, err)
session := waitSessionCompletion(t, pcapImporter, sessionID)
- assert.Equal(t, 228024, session.Size)
+ assert.Equal(t, int64(228024), session.Size)
assert.Equal(t, 2000, session.ProcessedPackets)
assert.Equal(t, 2000, session.InvalidPackets)
assert.Equal(t, map[uint16]flowCount{}, session.PacketsPerService)
diff --git a/rules_manager.go b/rules_manager.go
index 2b721a2..c256354 100644
--- a/rules_manager.go
+++ b/rules_manager.go
@@ -176,8 +176,8 @@ func (rm *rulesManagerImpl) UpdateRule(context context.Context, id RowID, rule R
newRule.Color = rule.Color
delete(rm.rulesByName, newRule.Name)
- rm.rulesByName[rule.Name] = newRule
- rm.rules[rule.ID] = newRule
+ rm.rulesByName[newRule.Name] = newRule
+ rm.rules[id] = newRule
rm.mutex.Unlock()
}