Skip to content

Commit

Permalink
add uppercase transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
blotus committed Dec 8, 2023
1 parent c0b5f7a commit 615c15b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/transformations/transformations.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func init() {
Register("replaceComments", replaceComments)
Register("replaceNulls", replaceNulls)
Register("sha1", sha1T)
Register("uppercase", upperCase)
Register("urlDecode", urlDecode)
Register("urlDecodeUni", urlDecodeUni)
Register("urlEncode", urlEncode)
Expand Down
15 changes: 15 additions & 0 deletions internal/transformations/uppercase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2022 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0

package transformations

import (
"strings"
)

func upperCase(data string) (string, bool, error) {
// TODO: Explicit implementation of ToUpper would allow optimizing away the byte by byte comparison for returning the changed boolean
// See https://github.com/corazawaf/coraza/pull/778#discussion_r1186963422
transformedData := strings.ToUpper(data)
return transformedData, data != transformedData, nil
}
69 changes: 69 additions & 0 deletions internal/transformations/uppercase_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2022 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0

package transformations

import "testing"

func TestUpperCase(t *testing.T) {
tests := []struct {
input string
want string
}{
{
input: "TestCase",
want: "TESTCASE",
},
{
input: "test\u0000case",
want: "TEST\u0000CASE",
},
{
input: "TESTCASE",
want: "TESTCASE",
},
{
input: "",
want: "",
},
{
input: "ThIs Is A tExT fOr TeStInG uPPerCAse FuNcTiOnAlItY.",
want: "THIS IS A TEXT FOR TESTING UPPERCASE FUNCTIONALITY.",
},
}

for _, tc := range tests {
tt := tc
t.Run(tt.input, func(t *testing.T) {
have, changed, err := upperCase(tt.input)
if err != nil {
t.Error(err)
}
if tt.input == tt.want && changed || tt.input != tt.want && !changed {
t.Errorf("input %q, have %q with changed %t", tt.input, have, changed)
}
if have != tt.want {
t.Errorf("have %q, want %q", have, tt.want)
}
})
}
}

func BenchmarkUppercase(b *testing.B) {
tests := []string{
"tesTcase",
"ThIs Is A tExT fOr TeStInG lOwErCaSe FuNcTiOnAlItY.ThIs Is A tExT fOr TeStInG lOwErCaSe FuNcTiOnAlItY. ThIs Is A tExT fOr TeStInG lOwErCaSe FuNcTiOnAlItY.ThIs Is A tExT fOr TeStInG lOwErCaSe FuNcTiOnAlItY.",
}
for i := 0; i < b.N; i++ {
for _, tt := range tests {
b.Run(tt, func(b *testing.B) {
for j := 0; j < b.N; j++ {
_, _, err := upperCase(tt)
if err != nil {
b.Error(err)
}
}
})
}
}
}

0 comments on commit 615c15b

Please sign in to comment.