-
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add uppercase transformation (#935)
* add uppercase transformation * add uppercase.json for tests --------- Co-authored-by: Felipe Zipitría <3012076+fzipi@users.noreply.github.com>
- Loading branch information
Showing
4 changed files
with
123 additions
and
0 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,38 @@ | ||
[ | ||
{ | ||
"input" : "", | ||
"name" : "uppercase", | ||
"type" : "tfn", | ||
"ret" : 0, | ||
"output" : "" | ||
}, | ||
{ | ||
"output" : "TESTCASE", | ||
"ret" : 0, | ||
"name" : "uppercase", | ||
"input" : "testcase", | ||
"type" : "tfn" | ||
}, | ||
{ | ||
"ret" : 0, | ||
"input" : "test\u0000case", | ||
"type" : "tfn", | ||
"name" : "uppercase", | ||
"output" : "TEST\u0000CASE" | ||
}, | ||
{ | ||
"output" : "TESTCASE", | ||
"input" : "testcase", | ||
"name" : "uppercase", | ||
"type" : "tfn", | ||
"ret" : 1 | ||
}, | ||
{ | ||
"ret" : 1, | ||
"input" : "Test\u0000Case", | ||
"name" : "uppercase", | ||
"type" : "tfn", | ||
"output" : "TEST\u0000CASE" | ||
} | ||
] | ||
|
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
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 | ||
} |
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,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) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
} |