From 26c992c0b529ad52910ee9ed8f8b13dc9f3b9904 Mon Sep 17 00:00:00 2001 From: Emiliano Ciavatta Date: Tue, 21 Apr 2020 13:54:46 +0200 Subject: Add application_context tests --- application_context.go | 12 +++++------ application_context_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++++ application_router.go | 2 -- rules_manager.go | 13 ------------ 4 files changed, 57 insertions(+), 21 deletions(-) create mode 100644 application_context_test.go diff --git a/application_context.go b/application_context.go index 77fae4e..50a1665 100644 --- a/application_context.go +++ b/application_context.go @@ -23,28 +23,28 @@ type ApplicationContext struct { func CreateApplicationContext(storage Storage) (*ApplicationContext, error) { var configWrapper struct { - config Config + Config Config } if err := storage.Find(Settings).Filter(OrderedDocument{{"_id", "config"}}). First(&configWrapper); err != nil { return nil, err } var accountsWrapper struct { - accounts gin.Accounts + Accounts gin.Accounts } if err := storage.Find(Settings).Filter(OrderedDocument{{"_id", "accounts"}}). First(&accountsWrapper); err != nil { return nil, err } - if accountsWrapper.accounts == nil { - accountsWrapper.accounts = make(gin.Accounts) + if accountsWrapper.Accounts == nil { + accountsWrapper.Accounts = make(gin.Accounts) } applicationContext := &ApplicationContext{ Storage: storage, - Config: configWrapper.config, - Accounts: accountsWrapper.accounts, + Config: configWrapper.Config, + Accounts: accountsWrapper.Accounts, } applicationContext.configure() diff --git a/application_context_test.go b/application_context_test.go new file mode 100644 index 0000000..2a94cf6 --- /dev/null +++ b/application_context_test.go @@ -0,0 +1,51 @@ +package main + +import ( + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestCreateApplicationContext(t *testing.T) { + wrapper := NewTestStorageWrapper(t) + wrapper.AddCollection(Settings) + + appContext, err := CreateApplicationContext(wrapper.Storage) + assert.NoError(t, err) + assert.False(t, appContext.IsConfigured) + assert.Zero(t, appContext.Config) + assert.Len(t, appContext.Accounts, 0) + assert.Nil(t, appContext.PcapImporter) + assert.Nil(t, appContext.RulesManager) + + config := Config{ + ServerIP: "10.10.10.10", + FlagRegex: "FLAG{test}", + AuthRequired: true, + } + accounts := gin.Accounts{ + "username": "password", + } + appContext.SetConfig(config) + appContext.SetAccounts(accounts) + assert.Equal(t, appContext.Config, config) + assert.Equal(t, appContext.Accounts, accounts) + assert.NotNil(t, appContext.PcapImporter) + assert.NotNil(t, appContext.RulesManager) + assert.True(t, appContext.IsConfigured) + + config.FlagRegex = "FLAG{test2}" + accounts["username"] = "password2" + appContext.SetConfig(config) + appContext.SetAccounts(accounts) + + checkAppContext, err := CreateApplicationContext(wrapper.Storage) + assert.NoError(t, err) + assert.True(t, checkAppContext.IsConfigured) + assert.Equal(t, checkAppContext.Config, config) + assert.Equal(t, checkAppContext.Accounts, accounts) + assert.NotNil(t, checkAppContext.PcapImporter) + assert.NotNil(t, checkAppContext.RulesManager) + + wrapper.Destroy(t) +} diff --git a/application_router.go b/application_router.go index 3ec74d5..d79fee8 100644 --- a/application_router.go +++ b/application_router.go @@ -3,7 +3,6 @@ package main import ( "github.com/gin-gonic/gin" "net/http" - log "github.com/sirupsen/logrus" ) func CreateApplicationRouter(applicationContext *ApplicationContext) *gin.Engine { @@ -97,7 +96,6 @@ func CreateApplicationRouter(applicationContext *ApplicationContext) *gin.Engine func SetupRequiredMiddleware(applicationContext *ApplicationContext) gin.HandlerFunc { return func(c *gin.Context) { - log.Error("aaaaaaaaaaaaaa") if !applicationContext.IsConfigured { c.AbortWithStatusJSON(http.StatusServiceUnavailable, gin.H{ "error": "setup required", diff --git a/rules_manager.go b/rules_manager.go index 015d8f7..2b721a2 100644 --- a/rules_manager.go +++ b/rules_manager.go @@ -194,19 +194,6 @@ func (rm *rulesManagerImpl) GetRules() []Rule { return rules } -func (rm *rulesManagerImpl) SetFlag(context context.Context, flagRegex string) error { - _, err := rm.AddRule(context, Rule{ - Name: "flag", - Color: "#ff0000", - Notes: "Mark connections where the flag is stolen", - Patterns: []Pattern{ - {Regex: flagRegex, Direction: DirectionToClient}, - }, - }) - - return err -} - func (rm *rulesManagerImpl) FillWithMatchedRules(connection *Connection, clientMatches map[uint][]PatternSlice, serverMatches map[uint][]PatternSlice) { rm.mutex.Lock() -- cgit v1.2.3-70-g09d2