diff options
author | Emiliano Ciavatta | 2020-04-21 11:54:46 +0000 |
---|---|---|
committer | Emiliano Ciavatta | 2020-04-21 11:54:46 +0000 |
commit | 26c992c0b529ad52910ee9ed8f8b13dc9f3b9904 (patch) | |
tree | 53656080a2aaf046524fd3f75cfb2ed8d232d956 | |
parent | 516712f670803979c65fd3db73dd2de9d7175139 (diff) |
Add application_context tests
-rw-r--r-- | application_context.go | 12 | ||||
-rw-r--r-- | application_context_test.go | 51 | ||||
-rw-r--r-- | application_router.go | 2 | ||||
-rw-r--r-- | rules_manager.go | 13 |
4 files changed, 57 insertions, 21 deletions
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() |