8000 Reformat as testify by fzipi · Pull Request #162 · coreruleset/go-ftw · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Reformat as testify #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = false
insert_final_newline = true

[*.yaml]
indent_size = 2
10 changes: 6 additions & 4 deletions check/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ type FTWCheck struct {
}

// NewCheck creates a new FTWCheck, allowing to inject the configuration
func NewCheck(c *config.FTWConfiguration) *FTWCheck {
//TODO: check error
ll, _ := waflog.NewFTWLogLines(c)
func NewCheck(c *config.FTWConfiguration) (*FTWCheck, error) {
ll, err := waflog.NewFTWLogLines(c)
if err != nil {
return nil, err
}
check := &FTWCheck{
log: ll,
cfg: c,
expected: &test.Output{},
}

return check
return check, nil
}

// SetExpectTestOutput sets the combined expected output from this test
Expand Down
100 changes: 68 additions & 32 deletions check/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,57 @@ import (
"sort"
"testing"

"github.com/stretchr/testify/assert"
"github.com/coreruleset/go-ftw/utils"

"github.com/stretchr/testify/suite"

"github.com/coreruleset/go-ftw/config"
"github.com/coreruleset/go-ftw/test"
)

var yamlApacheConfig = `---
logfile: 'tests/logs/modsec2-apache/apache2/error.log'
`

var yamlNginxConfig = `---
var configMap = map[string]string{
"TestNewCheck": `---
logfile: 'tests/logs/modsec3-nginx/nginx/error.log'
testoverride:
ignore:
'942200-1': 'Ignore Me'
`
`, "TestForced": `---
testoverride:
ignore:
'942200-1': 'Ignore Me'
forcepass:
'1245': 'Forced Pass'
forcefail:
'6789': 'Forced Fail'
`, "TestCloudMode": `---
mode: "cloud"`,
}

var yamlCloudConfig = `---
mode: "cloud"
`
type checkBaseTestSuite struct {
suite.Suite
cfg *config.FTWConfiguration
}

func TestNewCheck(t *testing.T) {
cfg, err := config.NewConfigFromString(yamlNginxConfig)
assert.NoError(t, err)
func (s *checkBaseTestSuite) BeforeTest(_, name string) {
var err error
var logName string
s.cfg, err = config.NewConfigFromString(configMap[name])
s.NoError(err)
logName, err = utils.CreateTempFileWithContent(logText, "test-*.log")
s.NoError(err)
s.cfg.WithLogfile(logName)
}

c := NewCheck(cfg)
func TestCheckBaseTestSuite(t *testing.T) {
suite.Run(t, new(checkBaseTestSuite))
}

func (s *checkBaseTestSuite) TestNewCheck() {
c, err := NewCheck(s.cfg)
s.NoError(err)

for _, text := range c.cfg.TestOverride.Ignore {
assert.Equal(t, text, "Ignore Me", "Well, didn't match Ignore Me")
s.Equal(text, "Ignore Me", "Well, didn't match Ignore Me")
}

to := test.Output{
Expand All @@ -44,33 +66,37 @@ func TestNewCheck(t *testing.T) {
}
c.SetExpectTestOutput(&to)

assert.True(t, c.expected.ExpectError, "Problem setting expected output")
s.True(c.expected.ExpectError, "Problem setting expected output")

c.SetNoLogContains("nologcontains")

assert.Equal(t, c.expected.NoLogContains, "nologcontains", "Problem setting nologcontains")
s.Equal(c.expected.NoLogContains, "nologcontains", "Problem setting nologcontains")
}

func TestForced(t *testing.T) {
cfg, err := config.NewConfigFromString(yamlNginxConfig)
assert.NoError(t, err)
func (s *checkBaseTestSuite) TestForced() {
c, err := NewCheck(s.cfg)
s.NoError(err)

c := NewCheck(cfg)
s.True(c.ForcedIgnore("942200-1"), "Can't find ignored value")

assert.True(t, c.ForcedIgnore("942200-1"), "Can't find ignored value")
s.False(c.ForcedFail("1245"), "Value should not be found")

assert.False(t, c.ForcedFail("1245"), "Value should not be found")
s.False(c.ForcedPass("1234"), "Value should not be found")

assert.False(t, c.ForcedPass("1245"), "Value should not be found")
}
s.True(c.ForcedPass("1245"), "Value should be found")

s.True(c.ForcedFail("6789"), "Value should be found")

s.cfg.TestOverride.Ignore = make(map[*config.FTWRegexp]string)
s.Falsef(c.ForcedIgnore("anything"), "Should not find ignored value in empty map")

func TestCloudMode(t *testing.T) {
cfg, err := config.NewConfigFromString(yamlCloudConfig)
assert.NoError(t, err)
}

c := NewCheck(cfg)
func (s *checkBaseTestSuite) TestCloudMode() {
c, err := NewCheck(s.cfg)
s.NoError(err)

assert.True(t, c.CloudMode(), "couldn't detect cloud mode")
s.True(c.CloudMode(), "couldn't detect cloud mode")

status := []int{200, 301}
c.SetExpectStatus(status)
Expand All @@ -81,7 +107,7 @@ func TestCloudMode(t *testing.T) {
cloudStatus := c.expected.Status
sort.Ints(cloudStatus)
res := sort.SearchInts(cloudStatus, 403)
assert.Equalf(t, 2, res, "couldn't find expected 403 status in %#v -> %d", cloudStatus, res)
s.Equalf(2, res, "couldn't find expected 403 status in %#v -> %d", cloudStatus, res)

c.SetLogContains("")
c.SetNoLogContains("no log contains")
Expand All @@ -96,6 +122,16 @@ func TestCloudMode(t *testing.T) {
found = true
}
}
assert.True(t, found, "couldn't find expected 200 status")
s.True(found, "couldn't find expected 200 status")

}

func (s *checkBaseTestSuite) TestSetMarkers() {
c, err := NewCheck(s.cfg)
s.NoError(err)

c.SetStartMarker([]byte("TesTingStArtMarKer"))
c.SetEndMarker([]byte("TestIngEnDMarkeR"))
s.Equal([]byte("testingstartmarker"), c.log.StartMarker, "Couldn't set start marker")
s.Equal([]byte("testingendmarker"), c.log.EndMarker, "Couldn't set end marker")
}
39 changes: 27 additions & 12 deletions check/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
"github.com/coreruleset/go-ftw/utils"

"github.com/stretchr/testify/suite"

"github.com/coreruleset/go-ftw/config"
)
Expand All @@ -25,25 +27,38 @@ var expectedFailTests = []struct {
{errors.New("a"), false},
}

func TestAssertResponseErrorOK(t *testing.T) {
cfg, err := config.NewConfigFromString(yamlApacheConfig)
assert.NoError(t, err)
type checkErrorTestSuite struct {
suite.Suite
cfg *config.FTWConfiguration
}

func TestCheckErrorTestSuite(t *testing.T) {
suite.Run(t, new(checkErrorTestSuite))
}

c := NewCheck(cfg)
func (s *checkErrorTestSuite) SetupTest() {
var err error
s.cfg = config.NewDefaultConfig()

logName, err := utils.CreateTempFileWithContent(logText, "test-*.log")
s.NoError(err)
s.cfg.WithLogfile(logName)
}
func (s *checkErrorTestSuite) TestAssertResponseErrorOK() {
c, err := NewCheck(s.cfg)
s.NoError(err)
for _, e := range expectedOKTests {
c.SetExpectError(e.expected)
assert.Equal(t, e.expected, c.AssertExpectError(e.err))
s.Equal(e.expected, c.AssertExpectError(e.err))
}
}

func TestAssertResponseFail(t *testing.T) {
cfg, err := config.NewConfigFromString(yamlApacheConfig)
assert.NoError(t, err)

c := NewCheck(cfg)
func (s *checkErrorTestSuite) TestAssertResponseFail() {
c, err := NewCheck(s.cfg)
s.NoError(err)

for _, e := range expectedFailTests {
c.SetExpectError(e.expected)
assert.False(t, c.AssertExpectError(e.err))
s.False(c.AssertExpectError(e.err))
}
}
50 changes: 38 additions & 12 deletions check/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"

"github.com/coreruleset/go-ftw/config"
"github.com/coreruleset/go-ftw/utils"
Expand All @@ -16,24 +16,50 @@ var logText = `[Tue Jan 05 02:21:09.637165 2021] [:error] [pid 76:tid 1396834345
[Tue Jan 05 02:21:09.647668 2021] [:error] [pid 76:tid 139683434571520] [client 172.23.0.1:58998] [client 172.23.0.1] ModSecurity: Warning. Operator GE matched 5 at TX:inbound_anomaly_score. [file "/etc/modsecurity.d/owasp-crs/rules/RESPONSE-980-CORRELATION.conf"] [line "87"] [id "980130"] [msg "Inbound Anomaly Score Exceeded (Total Inbound Score: 5 - SQLI=0,XSS=0,RFI=0,LFI=0,RCE=0,PHPI=0,HTTP=0,SESS=0): individual paranoia level scores: 3, 2, 0, 0"] [ver "OWASP_CRS/3.3.0"] [tag "event-correlation"] [hostname "localhost"] [uri "/"] [unique_id "X-PNFSe1VwjCgYRI9FsbHgAAAIY"]
`

func TestAssertLogContainsOK(t *testing.T) {
cfg, err := config.NewConfigFromString(yamlApacheConfig)
assert.NoError(t, err)
type checkLogsTestSuite struct {
suite.Suite
cfg *config.FTWConfiguration
logName string
}

logName, _ := utils.CreateTempFileWithContent(logText, "test-*.log")
defer os.Remove(logName)
cfg.WithLogfile(logName)
func TestCheckLogsTestSuite(t *testing.T) {
suite.Run(t, new(checkLogsTestSuite))
}

c := NewCheck(cfg)
func (s *checkLogsTestSuite) SetupTest() {
var err error
s.cfg = config.NewDefaultConfig()

s.logName, err = utils.CreateTempFileWithContent(logText, "test-*.log")
s.NoError(err)
s.cfg.WithLogfile(s.logName)
}

func (s *checkLogsTestSuite) TearDownTest() {
err := os.Remove(s.logName)
s.NoError(err)
}
func (s *checkLogsTestSuite) TestAssertLogContainsOK() {
c, err := NewCheck(s.cfg)
s.NoError(err)

c.SetLogContains(`id "920300"`)
assert.True(t, c.AssertLogContains(), "did not find expected content 'id \"920300\"'")
s.True(c.AssertLogContains(), "did not find expected content 'id \"920300\"'&q 7507 uot;)

c.SetLogContains(`SOMETHING`)
assert.False(t, c.AssertLogContains(), "found something that is not there")
s.False(c.AssertLogContains(), "found something that is not there")
s.True(c.LogContainsRequired(), "if LogContains is not empty it should return true")

c.SetLogContains("")
s.False(c.AssertLogContains(), "empty LogContains should return false")

c.SetNoLogContains("SOMETHING")
assert.True(t, c.AssertNoLogContains(), "found something that is not there")
s.True(c.AssertNoLogContains(), "found something that is not there")

c.SetNoLogContains(`id "920300"`)
assert.False(t, c.AssertNoLogContains(), "did not find expected content")
s.False(c.AssertNoLogContains(), "did not find expected content")

c.SetNoLogContains("")
s.False(c.AssertNoLogContains(), "should return false when empty string is passed")
s.False(c.NoLogContainsRequired(), "if NoLogContains is an empty string is passed should return false")
}
Loading
0