Skip to content

Commit

Permalink
test: Adding fuzz testing for common util
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Chaudhary <[email protected]>
  • Loading branch information
ispeakc0de committed Mar 8, 2024
1 parent 5f0d882 commit 05e4858
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
2 changes: 2 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 Down Expand Up @@ -59,6 +60,7 @@ require (
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
115 changes: 114 additions & 1 deletion pkg/utils/common/common_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package common

import (
"github.com/stretchr/testify/assert"
"regexp"
"strconv"
"strings"
"testing"

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

func FuzzRandomInterval(f *testing.F) {
Expand Down Expand Up @@ -38,3 +42,112 @@ func FuzzRandomInterval(f *testing.F) {
}
})
}

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)
})
}

0 comments on commit 05e4858

Please sign in to comment.