Skip to content

Commit

Permalink
test: Adding fuzz testing for common util (litmuschaos#688)
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Chaudhary <[email protected]>
  • Loading branch information
S-ayanide authored and ispeakc0de committed Mar 8, 2024
1 parent eef3b40 commit d72c7f0
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 5 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/litmuschaos/litmus-go
go 1.18

require (
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24
github.com/Azure/azure-sdk-for-go v56.1.0+incompatible
github.com/Azure/go-autorest/autorest v0.11.18
github.com/Azure/go-autorest/autorest/azure/auth v0.5.7
Expand All @@ -14,6 +15,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
Expand Down Expand Up @@ -56,7 +58,9 @@ 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
github.com/stretchr/objx v0.2.0 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/net v0.19.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 h1:bvDV9vkmnHYOMsOr4WLk+Vo07yKIzd94sVoIqshQ4bU=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8=
github.com/Azure/azure-sdk-for-go v56.1.0+incompatible h1:Ofcecdw3F1ZqnpDEZcLzH9Hq0P4Y5Si8+EioXJSamJs=
github.com/Azure/azure-sdk-for-go v56.1.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8=
Expand Down Expand Up @@ -480,6 +482,7 @@ github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5q
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
18 changes: 13 additions & 5 deletions pkg/utils/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os/exec"
"os/signal"
"reflect"
"regexp"
"strconv"
"strings"
"syscall"
Expand All @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand All @@ -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, "-")
Expand All @@ -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))
Expand Down
119 changes: 119 additions & 0 deletions pkg/utils/common/common_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package common

import (
"strings"
"testing"

fuzz "github.com/AdaLogics/go-fuzz-headers"
"github.com/litmuschaos/litmus-go/pkg/types"
"github.com/stretchr/testify/require"
)

func FuzzGetContainerNames(f *testing.F) {

f.Fuzz(func(t *testing.T, data []byte) {
fuzzConsumer := fuzz.NewConsumer(data)
targetStruct := &struct {
chaosDetails types.ChaosDetails
}{}
err := fuzzConsumer.GenerateStruct(targetStruct)
if err != nil {
return
}
names := GetContainerNames(&targetStruct.chaosDetails)
require.Equal(t, len(names), len(targetStruct.chaosDetails.SideCar)+1)
})
}

func FuzzGetSidecarVolumes(f *testing.F) {

f.Fuzz(func(t *testing.T, data []byte) {
fuzzConsumer := fuzz.NewConsumer(data)
targetStruct := &struct {
chaosDetails types.ChaosDetails
}{}
err := fuzzConsumer.GenerateStruct(targetStruct)
if err != nil {
return
}
volumes := GetSidecarVolumes(&targetStruct.chaosDetails)
var volCounts = 0
for _, s := range targetStruct.chaosDetails.SideCar {
volCounts += len(s.Secrets)
}
require.Equal(t, len(volumes), len(volumes))
})
}

func FuzzBuildSidecar(f *testing.F) {

f.Fuzz(func(t *testing.T, data []byte) {
fuzzConsumer := fuzz.NewConsumer(data)
targetStruct := &struct {
chaosDetails types.ChaosDetails
}{}
err := fuzzConsumer.GenerateStruct(targetStruct)
if err != nil {
return
}
containers := BuildSidecar(&targetStruct.chaosDetails)
require.Equal(t, len(containers), len(targetStruct.chaosDetails.SideCar))
})
}

func FuzzContains(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
fuzzConsumer := fuzz.NewConsumer(data)
targetStruct := &struct {
val string
slice []string
}{}
err := fuzzConsumer.GenerateStruct(targetStruct)
if err != nil {
return
}
contains := Contains(targetStruct.val, targetStruct.slice)
for _, s := range targetStruct.slice {
if s == targetStruct.val {
require.True(t, contains)
return
}
}
require.False(t, contains)
})
}

func FuzzSubStringExistsInSlice(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
fuzzConsumer := fuzz.NewConsumer(data)
targetStruct := &struct {
val string
slice []string
}{}
err := fuzzConsumer.GenerateStruct(targetStruct)
if err != nil {
return
}
contains := SubStringExistsInSlice(targetStruct.val, targetStruct.slice)
for _, s := range targetStruct.slice {
if strings.Contains(s, targetStruct.val) {
require.True(t, contains)
return
}
}
require.False(t, contains)
})
}

func FuzzGetRandomSequence(f *testing.F) {
f.Add("random")

f.Fuzz(func(t *testing.T, sequence string) {
val := GetRandomSequence(sequence)
if strings.ToLower(sequence) == "random" {
require.Contains(t, []string{"serial", "parallel"}, val)
return
}
require.Equal(t, sequence, val)
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("2778")

0 comments on commit d72c7f0

Please sign in to comment.