Skip to content

Commit

Permalink
Added fuzzer in Litmusctl (litmuschaos#210)
Browse files Browse the repository at this point in the history
Signed-off-by: Saranya-jena <[email protected]>
  • Loading branch information
Saranya-jena authored Mar 14, 2024
1 parent 95fad15 commit 49ebc8b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ func PrintInYamlFormat(inf interface{}) {
}

func GenerateRandomString(n int) (string, error) {
if n <= 0 {
return "", fmt.Errorf("length should not be negative")
}
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"
ret := make([]byte, n)
for i := 0; i < n; i++ {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
int(-82)
41 changes: 41 additions & 0 deletions pkg/utils/utils_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package utils

import (
"strings"
"testing"
)

func isValidString(s string) bool {
// Define the set of valid characters
validChars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"

// Iterate over each character in the string
for _, char := range s {
// Check if the character is not in the set of valid characters
if !strings.ContainsRune(validChars, char) {
return false
}
}
return true
}

func FuzzGenerateRandomString(f *testing.F) {
f.Add(10)
f.Fuzz(func(t *testing.T, n int) {
randomString, err := GenerateRandomString(n)
if err != nil && n > 0 {
t.Errorf("Error generating random string: %v", err)
}
// Perform checks on the generated string
// Check if the length matches the expected length
if n >= 0 && len(randomString) != n {
t.Errorf("Generated string length doesn't match expected length")
}

// Check if the string contains only valid characters
if !isValidString(randomString) {
t.Errorf("Generated string contains invalid characters")
}
})

}

0 comments on commit 49ebc8b

Please sign in to comment.