forked from helm-unittest/helm-unittest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request helm-unittest#521 from gofogo/issue-499
- Loading branch information
Showing
13 changed files
with
503 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package common | ||
|
||
import ( | ||
"bytes" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
const ( | ||
bsCode byte = byte('\\') | ||
metaCharactersNeedsEscapePattern = `.*[.+*?()|[\]{}^$].*` | ||
) | ||
|
||
var metaRegex = regexp.MustCompile(metaCharactersNeedsEscapePattern) | ||
|
||
type YmlEscapeHandlers struct{} | ||
|
||
// Escape function is required, as yaml library no longer maintained | ||
// yaml unmaintained library issue https://github.com/go-yaml/yaml/pull/862 | ||
func (y *YmlEscapeHandlers) Escape(content string) []byte { | ||
if !strings.Contains(content, `\`) && !metaRegex.MatchString(content) { | ||
return nil | ||
} | ||
return escapeBackslashes([]byte(content)) | ||
} | ||
|
||
// escapeBackslashes escapes backslashes in the given byte slice. | ||
// It ensures that an even number of backslashes are present by doubling any single backslash found. | ||
func escapeBackslashes(content []byte) []byte { | ||
var result bytes.Buffer | ||
i := 0 | ||
for i < len(content) { | ||
if content[i] != bsCode { | ||
result.WriteByte(content[i]) | ||
i++ | ||
continue | ||
} | ||
|
||
count := 1 | ||
for i+count < len(content) && content[i+count] == bsCode { | ||
count++ | ||
} | ||
|
||
times := count | ||
if count%2 == 1 { | ||
times++ | ||
} | ||
|
||
for j := 0; j < times; j++ { | ||
result.WriteByte(bsCode) | ||
} | ||
|
||
i += count | ||
} | ||
return result.Bytes() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package common_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
. "github.com/helm-unittest/helm-unittest/internal/common" | ||
) | ||
|
||
const ( | ||
bsCode byte = byte('\\') | ||
) | ||
|
||
func TestEscape_InputAsRunes_EscapeBackslash_Code92(t *testing.T) { | ||
y := &YmlEscapeHandlers{} | ||
cases := []struct { | ||
name string | ||
content []byte | ||
expected []byte | ||
}{ | ||
{ | ||
name: "single and double backslashes", | ||
// paradox \(root\\) | ||
content: []byte{10, 9, 112, 97, 114, 97, 100, 111, 120, 32, bsCode, 40, 114, 111, 111, 116, bsCode, bsCode, 41, 10, 9}, | ||
expected: []byte{10, 9, 112, 97, 114, 97, 100, 111, 120, 32, bsCode, bsCode, 40, 114, 111, 111, 116, bsCode, bsCode, 41, 10, 9}, | ||
}, | ||
{ | ||
name: "single and triple backslashes with special characters", | ||
// `runAsUser` is set to `0` \(root\\\ | ||
content: []byte{96, 114, 117, 110, 65, 115, 85, 115, 101, 114, 96, 32, 105, 115, 32, 115, 101, 116, 32, 116, 111, 32, 96, 48, 96, 32, bsCode, 40, 114, 111, 111, 116, bsCode, bsCode, bsCode}, | ||
// `runAsUser` is set to `0` \\(root\\\\ | ||
expected: []byte{96, 114, 117, 110, 65, 115, 85, 115, 101, 114, 96, 32, 105, 115, 32, 115, 101, 116, 32, 116, 111, 32, 96, 48, 96, 32, bsCode, bsCode, 40, 114, 111, 111, 116, bsCode, bsCode, bsCode, bsCode}, | ||
}, | ||
{ | ||
name: "single and triple backslashes with special characters", | ||
// `runAsUser` is set to `0` \(root\\) | ||
content: []byte{96, 114, 117, 110, 65, 115, 85, 115, 101, 114, 96, 32, 105, 115, 32, 115, 101, 116, 32, 116, 111, 32, 96, 48, 96, 32, bsCode, 40, 114, 111, 111, 116, bsCode, bsCode, bsCode}, | ||
// `runAsUser` is set to `0` \\(root\\\\ | ||
expected: []byte{96, 114, 117, 110, 65, 115, 85, 115, 101, 114, 96, 32, 105, 115, 32, 115, 101, 116, 32, 116, 111, 32, 96, 48, 96, 32, bsCode, bsCode, 40, 114, 111, 111, 116, bsCode, bsCode, bsCode, bsCode}, | ||
}, | ||
{ | ||
name: "special characters and backslashes", | ||
// `run` is set \\0\\\ \(root\\\)\\\\ | ||
content: []byte{96, 114, 117, 110, 96, 32, 105, 115, 32, 115, 101, 116, 32, bsCode, 48, bsCode, bsCode, bsCode, 32, bsCode, 40, 114, 111, 111, 116, bsCode, bsCode, bsCode, 41, bsCode, bsCode, bsCode, bsCode}, | ||
// `run` is set \\0\\\\ \\(root\\\\)\\\\ | ||
expected: []byte{96, 114, 117, 110, 96, 32, 105, 115, 32, 115, 101, 116, 32, bsCode, bsCode, 48, bsCode, bsCode, bsCode, bsCode, 32, bsCode, bsCode, 40, 114, 111, 111, 116, bsCode, bsCode, bsCode, bsCode, 41, bsCode, bsCode, bsCode, bsCode}, | ||
}, | ||
{ | ||
name: "empty", | ||
content: []byte{}, | ||
expected: nil, | ||
}, | ||
{ | ||
name: "Mixed Backslashes", | ||
content: []byte("hello\\world\\\\"), | ||
expected: []byte("hello\\\\world\\\\"), | ||
}, | ||
{ | ||
name: "no backslashes", | ||
content: []byte("abrakadabra"), | ||
expected: nil, | ||
}, | ||
{ | ||
name: "contains meta characters but no slashes", | ||
content: []byte("some text with [value] needs escaping"), | ||
expected: []byte("some text with [value] needs escaping"), | ||
}, | ||
{ | ||
name: "backslashes at the end", | ||
content: []byte("hello world\\"), | ||
expected: []byte("hello world\\\\"), | ||
}, | ||
{ | ||
name: "backslashes in the middle", | ||
content: []byte("hello\\world\\"), | ||
expected: []byte("hello\\\\world\\\\"), | ||
}, | ||
{ | ||
name: "even number of backslashes", | ||
content: []byte("hello" + `\\\` + "world"), | ||
expected: []byte("hello" + `\\\\` + "world"), | ||
}, | ||
} | ||
|
||
for _, tt := range cases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
actual := y.Escape(string(tt.content)) | ||
|
||
assert.Equal(t, string(tt.expected), string(actual)) | ||
assert.Equal(t, tt.expected, actual) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.