Skip to content

Commit

Permalink
chore: add test for YAML unmarshalling (#396)
Browse files Browse the repository at this point in the history
The new tests specifically test unmarshalling of line feed and carriage
return escape sequences in double qouted strings, as that was an issue
with goccy/go-yaml.

Refs #359

Co-authored-by: Felipe Zipitría <[email protected]>
  • Loading branch information
theseion and fzipi authored Nov 18, 2024
1 parent 9414c73 commit 3e9d462
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions test/yaml_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2024 OWASP CRS Project
// SPDX-License-Identifier: Apache-2.0

package test

import (
"fmt"
"testing"

"github.com/stretchr/testify/suite"
"gopkg.in/yaml.v3"
)

type yamlTestSuite struct {
suite.Suite
}

func TestYamlTestSuite(t *testing.T) {
suite.Run(t, new(yamlTestSuite))
}

func (s *yamlTestSuite) TestUnmarshalling_LineFeed() {
s.testUnmarshalling(`\n`, "\n")
}

func (s *yamlTestSuite) TestUnmarshalling_CarriageReturn() {
s.testUnmarshalling(`\r`, "\r")

}

func (s *yamlTestSuite) testUnmarshalling(escapeSequence string, literal string) {
yamlTemplate := `---
tests:
- test_id: 1234
stages:
- input:
headers:
"%s": "%s"
`
keyTemplate := "Some%sHeader"
valueTemplate := "some%svalue"

key := fmt.Sprintf(keyTemplate, escapeSequence)
value := fmt.Sprintf(valueTemplate, escapeSequence)
expectedKey := fmt.Sprintf(keyTemplate, literal)
expectedValue := fmt.Sprintf(valueTemplate, literal)

yamlString := fmt.Sprintf(yamlTemplate, key, value)
test := &FTWTest{}

err := yaml.Unmarshal([]byte(yamlString), test)
s.Require().NoError(err)

headers := test.FTWTest.Tests[0].Stages[0].Input.Headers
s.Contains(headers, expectedKey)
s.Equal(expectedValue, headers[expectedKey])
}

0 comments on commit 3e9d462

Please sign in to comment.