Skip to content

Commit

Permalink
relies on external schema
Browse files Browse the repository at this point in the history
  • Loading branch information
M4tteoP committed Nov 11, 2023
1 parent 94fb167 commit 3a4cb51
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 40 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21

require (
github.com/Masterminds/sprig v2.22.0+incompatible
github.com/coreruleset/ftw-tests-schema v1.0.0
github.com/coreruleset/ftw-tests-schema v1.0.1-0.20231111145830-87a7096e32e1
github.com/go-logr/zerologr v1.2.3
github.com/goccy/go-yaml v1.9.2
github.com/google/uuid v1.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ github.com/antchfx/htmlquery v1.3.0/go.mod h1:zKPDVTMhfOmcwxheXUsx4rKJy8KEY/PU6e
github.com/antchfx/xpath v1.2.3 h1:CCZWOzv5bAqjVv0offZ2LVgVYFbeldKQVuLNbViZdes=
github.com/antchfx/xpath v1.2.3/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/coreruleset/ftw-tests-schema v1.0.0 h1:qvBx9hXoaqZGEDQ3E2cAIZowtmrV8afBZ5Yv4oFL7S4=
github.com/coreruleset/ftw-tests-schema v1.0.0/go.mod h1:eGixZc1HY61s6ze0f3TExkILnvmXwix4GS10iUXbDmU=
github.com/coreruleset/ftw-tests-schema v1.0.1-0.20231111145830-87a7096e32e1 h1:7qEBrJOMPvIp+Vnl5ASi1egZy+gQlLNaKM4TZfuBIgU=
github.com/coreruleset/ftw-tests-schema v1.0.1-0.20231111145830-87a7096e32e1/go.mod h1:eGixZc1HY61s6ze0f3TExkILnvmXwix4GS10iUXbDmU=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
22 changes: 11 additions & 11 deletions runner/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
var errBadTestInput = errors.New("ftw/run: bad test input: choose between data, encoded_request, or raw_request")

// Run runs your tests with the specified Config.
func Run(cfg *config.FTWConfiguration, tests []types.FTWTest, c RunnerConfig, out *output.Output) (*TestRunContext, error) {
func Run(cfg *config.FTWConfiguration, tests []test.FTWTest, c RunnerConfig, out *output.Output) (*TestRunContext, error) {
out.Println("%s", out.Message("** Running go-ftw!"))

logLines, err := waflog.NewFTWLogLines(cfg)
Expand Down Expand Up @@ -73,7 +73,7 @@ func Run(cfg *config.FTWConfiguration, tests []types.FTWTest, c RunnerConfig, ou
// RunTest runs an individual test.
// runContext contains information for the current test run
// ftwTest is the test you want to run
func RunTest(runContext *TestRunContext, ftwTest types.FTWTest) error {
func RunTest(runContext *TestRunContext, ftwTest test.FTWTest) error {
changed := true

for _, testCase := range ftwTest.Tests {
Expand All @@ -100,7 +100,7 @@ func RunTest(runContext *TestRunContext, ftwTest types.FTWTest) error {
if err != nil {
return err
}
if err := RunStage(runContext, ftwCheck, testCase, stage.Stage); err != nil {
if err := RunStage(runContext, ftwCheck, testCase, stage.SD); err != nil {
return err
}
}
Expand All @@ -114,13 +114,13 @@ func RunTest(runContext *TestRunContext, ftwTest types.FTWTest) error {
// ftwCheck is the current check utility
// testCase is the test case the stage belongs to
// stage is the stage you want to run
func RunStage(runContext *TestRunContext, ftwCheck *check.FTWCheck, testCase types.Test, stage types.Stage) error {
func RunStage(runContext *TestRunContext, ftwCheck *check.FTWCheck, testCase types.Test, stage types.StageData) error {
stageStartTime := time.Now()
stageID := uuid.NewString()
// Apply global overrides initially
testInput := stage.SD.Input
testInput := (test.Input)(stage.Input)
test.ApplyInputOverrides(&runContext.Config.TestOverride.Overrides, &testInput)
expectedOutput := stage.SD.Output
expectedOutput := stage.Output
expectErr := false
if expectedOutput.ExpectError != nil {
expectErr = *expectedOutput.ExpectError
Expand Down Expand Up @@ -181,7 +181,7 @@ func RunStage(runContext *TestRunContext, ftwCheck *check.FTWCheck, testCase typ
}

// Set expected test output in check
ftwCheck.SetExpectTestOutput(&expectedOutput)
ftwCheck.SetExpectTestOutput((*test.Output)(&expectedOutput))

// now get the test result based on output
testResult := checkResult(ftwCheck, response, responseErr)
Expand Down Expand Up @@ -240,9 +240,9 @@ func markAndFlush(runContext *TestRunContext, dest *ftwhttp.Destination, stageID
return nil, fmt.Errorf("can't find log marker. Am I reading the correct log? Log file: %s", runContext.Config.LogFile)
}

func needToSkipTest(include *regexp.Regexp, exclude *regexp.Regexp, title string, enabled bool) bool {
func needToSkipTest(include *regexp.Regexp, exclude *regexp.Regexp, title string, enabled *bool) bool {
// skip disabled tests
if !enabled {
if enabled != nil && !*enabled {
return true
}

Expand Down Expand Up @@ -274,7 +274,7 @@ func needToSkipTest(include *regexp.Regexp, exclude *regexp.Regexp, title string
return result
}

func checkTestSanity(testInput types.Input) bool {
func checkTestSanity(testInput test.Input) bool {
return (utils.IsNotEmpty(testInput.Data) && testInput.EncodedRequest != "") ||
(utils.IsNotEmpty(testInput.Data) && testInput.RAWRequest != "") ||
(testInput.EncodedRequest != "" && testInput.RAWRequest != "")
Expand Down Expand Up @@ -357,7 +357,7 @@ func checkResult(c *check.FTWCheck, response *ftwhttp.Response, responseError er
return Success
}

func getRequestFromTest(testInput types.Input) *ftwhttp.Request {
func getRequestFromTest(testInput test.Input) *ftwhttp.Request {
var req *ftwhttp.Request
// get raw request, if anything
raw, err := testInput.GetRawRequest()
Expand Down
8 changes: 4 additions & 4 deletions runner/run_input_override_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (s *inputOverrideTestSuite) TestSetHostFromDestAddr() {

s.NotNil(testInput.Headers, "Header map must exist after overriding `dest_addr`")

hostHeader := testInput.Headers.Get("Host")
hostHeader := testInput.GetHeaders().Get("Host")
s.NotEqual("", hostHeader, "Host header must be set after overriding `dest_addr`")
s.Equal(overrideHost, hostHeader, "Host header must be identical to `dest_addr` after overrding `dest_addr`")
}
Expand All @@ -191,7 +191,7 @@ func (s *inputOverrideTestSuite) TestSetHostFromHostHeaderOverride() {

test.ApplyInputOverrides(&s.cfg.TestOverride.Overrides, &testInput)

hostHeader := testInput.Headers.Get("Host")
hostHeader := testInput.GetHeaders().Get("Host")
s.NotEqual("", hostHeader, "Host header must be set after overriding the `Host` header")
if hostHeader == overrideHostHeader {
s.Equal(overrideHostHeader, hostHeader, "Host header override must take precence over OverrideEmptyHostHeader")
Expand All @@ -213,7 +213,7 @@ func (s *inputOverrideTestSuite) TestSetHeaderOverridingExistingOne() {

test.ApplyInputOverrides(&s.cfg.TestOverride.Overrides, &testInput)

overriddenHeader := testInput.Headers.Get("unique_id")
overriddenHeader := testInput.GetHeaders().Get("unique_id")
s.NotEqual("", overriddenHeader, "unique_id header must be set after overriding it")
s.Equal(overrideHeaderValue, overriddenHeader, "Host header must be identical to overridden `Host` header.")
}
Expand All @@ -231,7 +231,7 @@ func (s *inputOverrideTestSuite) TestApplyInputOverrides() {

test.ApplyInputOverrides(&s.cfg.TestOverride.Overrides, &testInput)

overriddenHeader := testInput.Headers.Get("unique_id")
overriddenHeader := testInput.GetHeaders().Get("unique_id")
s.NotEqual("", overriddenHeader, "unique_id header must be set after overriding it")
s.Equal(overrideHeaderValue, overriddenHeader, "Host header must be identical to overridden `Host` header.")
}
Expand Down
22 changes: 22 additions & 0 deletions test/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ package test
import (
"encoding/base64"

schema "github.com/coreruleset/ftw-tests-schema/types"
"github.com/coreruleset/go-ftw/ftwhttp"
"github.com/coreruleset/go-ftw/utils"
)

type Input schema.Input
type Output schema.Output
type FTWTest schema.FTWTest

// GetMethod returns the proper semantic when the field is empty
func (i *Input) GetMethod() string {
if i.Method == nil {
Expand Down Expand Up @@ -57,6 +63,14 @@ func (i *Input) GetPort() int {
return *i.Port
}

// GetHeaders returns the headers wrapped in a ftwhttp.Header
func (i *Input) GetHeaders() ftwhttp.Header {
if i.Headers == nil {
return ftwhttp.Header{}
}
return ftwhttp.Header(i.Headers)
}

// GetRawRequest returns the proper raw data, and error if there was none
func (i *Input) GetRawRequest() ([]byte, error) {
if utils.IsNotEmpty(i.EncodedRequest) {
Expand All @@ -68,3 +82,11 @@ func (i *Input) GetRawRequest() ([]byte, error) {
}
return nil, nil
}

// GetAutocompleteHeaders returns the proper raw data, and error if there was none
func (i *Input) GetAutocompleteHeaders() bool {
if i.AutocompleteHeaders == nil {
return true
}
return *i.AutocompleteHeaders
}
28 changes: 15 additions & 13 deletions test/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Overrides struct {
}

// ApplyInputOverride will check if config had global overrides and write that into the test.
func ApplyInputOverrides(overrides *Overrides, input *types.Input) {
func ApplyInputOverrides(overrides *Overrides, input *Input) {
applySimpleOverrides(overrides, input)
applyDestAddrOverride(overrides, input)
applyHeadersOverride(overrides, input)
Expand All @@ -37,19 +37,21 @@ func ApplyInputOverrides(overrides *Overrides, input *types.Input) {
}
}

func applyDestAddrOverride(overrides *Overrides, input *types.Input) {
func applyDestAddrOverride(overrides *Overrides, input *Input) {
if overrides.DestAddr != nil {
input.DestAddr = overrides.DestAddr
if input.Headers == nil {
input.Headers = ftwhttp.Header{}
}
if overrides.OverrideEmptyHostHeader != nil && *overrides.OverrideEmptyHostHeader && input.Headers.Get("Host") == "" {
input.Headers.Set("Host", *overrides.DestAddr)
if overrides.OverrideEmptyHostHeader != nil &&
*overrides.OverrideEmptyHostHeader &&
input.GetHeaders().Get("Host") == "" {
input.GetHeaders().Set("Host", *overrides.DestAddr)
}
}
}

func applySimpleOverrides(overrides *Overrides, input *types.Input) {
func applySimpleOverrides(overrides *Overrides, input *Input) {
if overrides.Port != nil {
input.Port = overrides.Port
}
Expand Down Expand Up @@ -87,38 +89,38 @@ func applySimpleOverrides(overrides *Overrides, input *types.Input) {
}
}

func applyHeadersOverride(overrides *Overrides, input *types.Input) {
func applyHeadersOverride(overrides *Overrides, input *Input) {
if overrides.Headers != nil {
if input.Headers == nil {
input.Headers = ftwhttp.Header{}
}
for k, v := range overrides.Headers {
input.Headers.Set(k, v)
input.GetHeaders().Set(k, v)
}
}
}

func postLoadTestFTWTest(ftwTest *types.FTWTest) {
func postLoadTestFTWTest(ftwTest *FTWTest) {
for _, test := range ftwTest.Tests {
postLoadTest(&test)
}
}

func postLoadTest(test *types.Test) {
for index := range test.Stages {
postLoadStage(&test.Stages[index].Stage)
postLoadStage(&test.Stages[index].SD)
}
}

func postLoadStage(stage *types.Stage) {
postLoadInput(&stage.Input)
func postLoadStage(stage *types.StageData) {
postLoadInput((*Input)(&stage.Input))
}

func postLoadInput(input *types.Input) {
func postLoadInput(input *Input) {
postProcessAutocompleteHeaders(input.AutocompleteHeaders, input.StopMagic, input)
}

func postProcessAutocompleteHeaders(autocompleteHeaders *bool, stopMagic *bool, input *types.Input) {
func postProcessAutocompleteHeaders(autocompleteHeaders *bool, stopMagic *bool, input *Input) {
autocompleteHeadersMissing := autocompleteHeaders == nil
stopMagicMissing := stopMagic == nil
// default value
Expand Down
18 changes: 9 additions & 9 deletions test/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (s *typesTestSuite) TestAutocompleteHeadersDefault_StopMagicDefault() {
test, err := GetTestFromYaml([]byte(autocompleteHeadersDefaultYaml))
s.NoError(err, "Parsing YAML shouldn't fail")

input := test.Tests[0].Stages[0].Stage.Input
input := test.Tests[0].Stages[0].SD.Input
s.True(*input.AutocompleteHeaders)
s.False(*input.StopMagic)
}
Expand All @@ -186,15 +186,15 @@ func (s *typesTestSuite) TestAutocompleteHeadersDefault_StopMagicTrue() {
test, err := GetTestFromYaml([]byte(autocompleteHeadersDefaultYaml))
s.NoError(err, "Parsing YAML shouldn't fail")

input := test.Tests[1].Stages[0].Stage.Input
input := test.Tests[1].Stages[0].SD.Input
s.False(*input.AutocompleteHeaders)
s.True(*input.StopMagic)
}
func (s *typesTestSuite) TestAutocompleteHeadersDefault_StopMagicFalse() {
test, err := GetTestFromYaml([]byte(autocompleteHeadersDefaultYaml))
s.NoError(err, "Parsing YAML shouldn't fail")

input := test.Tests[2].Stages[0].Stage.Input
input := test.Tests[2].Stages[0].SD.Input
s.True(*input.AutocompleteHeaders)
s.False(*input.StopMagic)
}
Expand All @@ -203,7 +203,7 @@ func (s *typesTestSuite) TestAutocompleteHeadersFalse_StopMagicDefault() {
test, err := GetTestFromYaml([]byte(autocompleteHeadersFalseYaml))
s.NoError(err, "Parsing YAML shouldn't fail")

input := test.Tests[0].Stages[0].Stage.Input
input := test.Tests[0].Stages[0].SD.Input
s.False(*input.AutocompleteHeaders)
s.True(*input.StopMagic)
}
Expand All @@ -212,7 +212,7 @@ func (s *typesTestSuite) TestAutocompleteHeadersFalse_StopMagicTrue() {
test, err := GetTestFromYaml([]byte(autocompleteHeadersFalseYaml))
s.NoError(err, "Parsing YAML shouldn't fail")

input := test.Tests[1].Stages[0].Stage.Input
input := test.Tests[1].Stages[0].SD.Input
s.False(*input.AutocompleteHeaders)
s.True(*input.StopMagic)
}
Expand All @@ -221,7 +221,7 @@ func (s *typesTestSuite) TestAutocompleteHeadersFalse_StopMagicFalse() {
test, err := GetTestFromYaml([]byte(autocompleteHeadersFalseYaml))
s.NoError(err, "Parsing YAML shouldn't fail")

input := test.Tests[2].Stages[0].Stage.Input
input := test.Tests[2].Stages[0].SD.Input
s.False(*input.AutocompleteHeaders)
s.True(*input.StopMagic)
}
Expand All @@ -230,7 +230,7 @@ func (s *typesTestSuite) TestAutocompleteHeadersTrue_StopMagicDefault() {
test, err := GetTestFromYaml([]byte(autocompleteHeadersTrueYaml))
s.NoError(err, "Parsing YAML shouldn't fail")

input := test.Tests[0].Stages[0].Stage.Input
input := test.Tests[0].Stages[0].SD.Input
s.True(*input.AutocompleteHeaders)
s.False(*input.StopMagic)
}
Expand All @@ -239,7 +239,7 @@ func (s *typesTestSuite) TestAutocompleteHeadersTrue_StopMagicTrue() {
test, err := GetTestFromYaml([]byte(autocompleteHeadersTrueYaml))
s.NoError(err, "Parsing YAML shouldn't fail")

input := test.Tests[1].Stages[0].Stage.Input
input := test.Tests[1].Stages[0].SD.Input
s.True(*input.AutocompleteHeaders)
s.False(*input.StopMagic)
}
Expand All @@ -248,7 +248,7 @@ func (s *typesTestSuite) TestAutocompleteHeadersTrue_StopMagicFalse() {
test, err := GetTestFromYaml([]byte(autocompleteHeadersTrueYaml))
s.NoError(err, "Parsing YAML shouldn't fail")

input := test.Tests[2].Stages[0].Stage.Input
input := test.Tests[2].Stages[0].SD.Input
s.True(*input.AutocompleteHeaders)
s.False(*input.StopMagic)
}

0 comments on commit 3a4cb51

Please sign in to comment.