diff --git a/go.mod b/go.mod index 01e9b616e..7a93f9a43 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.8.1 github.com/spf13/cobra v1.1.1 + github.com/stretchr/testify v1.7.0 google.golang.org/api v0.48.0 gopkg.in/yaml.v2 v2.4.0 k8s.io/api v0.26.0 @@ -56,6 +57,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spf13/pflag v1.0.5 // indirect go.opencensus.io v0.23.0 // indirect golang.org/x/crypto v0.16.0 // indirect diff --git a/pkg/utils/common/common.go b/pkg/utils/common/common.go index 1b1f09d8d..8759627d2 100644 --- a/pkg/utils/common/common.go +++ b/pkg/utils/common/common.go @@ -10,6 +10,7 @@ import ( "os/exec" "os/signal" "reflect" + "regexp" "strconv" "strings" "syscall" @@ -29,13 +30,17 @@ type ENVDetails struct { ENV []apiv1.EnvVar } -//WaitForDuration waits for the given time duration (in seconds) +// WaitForDuration waits for the given time duration (in seconds) func WaitForDuration(duration int) { time.Sleep(time.Duration(duration) * time.Second) } // RandomInterval wait for the random interval lies between lower & upper bounds func RandomInterval(interval string) error { + re := regexp.MustCompile(`^\d+(-\d+)?$`) + if re.MatchString(interval) == false { + return cerrors.Error{ErrorCode: cerrors.ErrorTypeGeneric, Reason: "could not parse CHAOS_INTERVAL env, bad input"} + } intervals := strings.Split(interval, "-") var lowerBound, upperBound int switch len(intervals) { @@ -49,6 +54,9 @@ func RandomInterval(interval string) error { return cerrors.Error{ErrorCode: cerrors.ErrorTypeGeneric, Reason: "could not parse CHAOS_INTERVAL env, invalid format"} } rand.Seed(time.Now().UnixNano()) + if upperBound < 1 { + return cerrors.Error{ErrorCode: cerrors.ErrorTypeGeneric, Reason: "invalid CHAOS_INTERVAL env value, value below lower limit"} + } waitTime := lowerBound + rand.Intn(upperBound-lowerBound) log.Infof("[Wait]: Wait for the random chaos interval %vs", waitTime) WaitForDuration(waitTime) @@ -98,7 +106,7 @@ func AbortWatcherWithoutExit(expname string, clients clients.ClientSets, resultD } } -//FilterBasedOnPercentage return the slice of list based on the the provided percentage +// FilterBasedOnPercentage return the slice of list based on the the provided percentage func FilterBasedOnPercentage(percentage int, list []string) []string { var finalList []string @@ -175,7 +183,7 @@ func GetStatusMessage(defaultCheck bool, defaultMsg, probeStatus string) string return "Probes: " + probeStatus } -//GetRandomSequence will gives a random value for sequence +// GetRandomSequence will gives a random value for sequence func GetRandomSequence(sequence string) string { if strings.ToLower(sequence) == "random" { rand.Seed(time.Now().UnixNano()) @@ -186,7 +194,7 @@ func GetRandomSequence(sequence string) string { return sequence } -//ValidateRange validates the given range of numbers +// ValidateRange validates the given range of numbers func ValidateRange(a string) string { var lb, ub int intervals := strings.Split(a, "-") @@ -204,7 +212,7 @@ func ValidateRange(a string) string { } } -//getRandomValue gives a random value between two integers +// getRandomValue gives a random value between two integers func getRandomValue(a, b int) int { rand.Seed(time.Now().Unix()) return (a + rand.Intn(b-a+1)) diff --git a/pkg/utils/common/common_fuzz_test.go b/pkg/utils/common/common_fuzz_test.go new file mode 100644 index 000000000..e3a59dded --- /dev/null +++ b/pkg/utils/common/common_fuzz_test.go @@ -0,0 +1,40 @@ +package common + +import ( + "github.com/stretchr/testify/assert" + "regexp" + "strconv" + "strings" + "testing" +) + +func FuzzRandomInterval(f *testing.F) { + testCases := []struct { + interval string + }{ + { + interval: "13", + }, + } + + for _, tc := range testCases { + f.Add(tc.interval) + } + + f.Fuzz(func(t *testing.T, interval string) { + re := regexp.MustCompile(`^\d+(-\d+)?$`) + intervals := strings.Split(interval, "-") + err := RandomInterval(interval) + + if re.MatchString(interval) == false { + assert.Error(t, err, "{\"errorCode\":\"GENERIC_ERROR\",\"reason\":\"could not parse CHAOS_INTERVAL env, bad input\"}") + } + + num, _ := strconv.Atoi(intervals[0]) + if num < 1 && err != nil { + assert.Error(t, err, "{\"errorCode\":\"GENERIC_ERROR\",\"reason\":\"invalid CHAOS_INTERVAL env value, value below lower limit\"}") + } else if num > 1 && err != nil { + t.Errorf("Unexpected Error: %v", err) + } + }) +} diff --git a/pkg/utils/common/testdata/fuzz/FuzzRandomInterval/711bbee1d16a50e2 b/pkg/utils/common/testdata/fuzz/FuzzRandomInterval/711bbee1d16a50e2 new file mode 100644 index 000000000..1354784e5 --- /dev/null +++ b/pkg/utils/common/testdata/fuzz/FuzzRandomInterval/711bbee1d16a50e2 @@ -0,0 +1,2 @@ +go test fuzz v1 +string("2778")