|
1 | 1 | package common
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "github.com/stretchr/testify/assert" |
5 | 4 | "regexp"
|
6 | 5 | "strconv"
|
7 | 6 | "strings"
|
8 | 7 | "testing"
|
| 8 | + |
| 9 | + "github.com/litmuschaos/litmus-go/pkg/types" |
| 10 | + fuzz "github.com/AdaLogics/go-fuzz-headers" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
9 | 13 | )
|
10 | 14 |
|
11 | 15 | func FuzzRandomInterval(f *testing.F) {
|
@@ -38,3 +42,112 @@ func FuzzRandomInterval(f *testing.F) {
|
38 | 42 | }
|
39 | 43 | })
|
40 | 44 | }
|
| 45 | + |
| 46 | +func FuzzGetContainerNames(f *testing.F) { |
| 47 | + |
| 48 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 49 | + fuzzConsumer := fuzz.NewConsumer(data) |
| 50 | + targetStruct := &struct { |
| 51 | + chaosDetails types.ChaosDetails |
| 52 | + }{} |
| 53 | + err := fuzzConsumer.GenerateStruct(targetStruct) |
| 54 | + if err != nil { |
| 55 | + return |
| 56 | + } |
| 57 | + names := GetContainerNames(&targetStruct.chaosDetails) |
| 58 | + require.Equal(t, len(names), len(targetStruct.chaosDetails.SideCar)+1) |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +func FuzzGetSidecarVolumes(f *testing.F) { |
| 63 | + |
| 64 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 65 | + fuzzConsumer := fuzz.NewConsumer(data) |
| 66 | + targetStruct := &struct { |
| 67 | + chaosDetails types.ChaosDetails |
| 68 | + }{} |
| 69 | + err := fuzzConsumer.GenerateStruct(targetStruct) |
| 70 | + if err != nil { |
| 71 | + return |
| 72 | + } |
| 73 | + volumes := GetSidecarVolumes(&targetStruct.chaosDetails) |
| 74 | + var volCounts = 0 |
| 75 | + for _, s := range targetStruct.chaosDetails.SideCar { |
| 76 | + volCounts += len(s.Secrets) |
| 77 | + } |
| 78 | + require.Equal(t, len(volumes), len(volumes)) |
| 79 | + }) |
| 80 | +} |
| 81 | + |
| 82 | +func FuzzBuildSidecar(f *testing.F) { |
| 83 | + |
| 84 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 85 | + fuzzConsumer := fuzz.NewConsumer(data) |
| 86 | + targetStruct := &struct { |
| 87 | + chaosDetails types.ChaosDetails |
| 88 | + }{} |
| 89 | + err := fuzzConsumer.GenerateStruct(targetStruct) |
| 90 | + if err != nil { |
| 91 | + return |
| 92 | + } |
| 93 | + containers := BuildSidecar(&targetStruct.chaosDetails) |
| 94 | + require.Equal(t, len(containers), len(targetStruct.chaosDetails.SideCar)) |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +func FuzzContains(f *testing.F) { |
| 99 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 100 | + fuzzConsumer := fuzz.NewConsumer(data) |
| 101 | + targetStruct := &struct { |
| 102 | + val string |
| 103 | + slice []string |
| 104 | + }{} |
| 105 | + err := fuzzConsumer.GenerateStruct(targetStruct) |
| 106 | + if err != nil { |
| 107 | + return |
| 108 | + } |
| 109 | + contains := Contains(targetStruct.val, targetStruct.slice) |
| 110 | + for _, s := range targetStruct.slice { |
| 111 | + if s == targetStruct.val { |
| 112 | + require.True(t, contains) |
| 113 | + return |
| 114 | + } |
| 115 | + } |
| 116 | + require.False(t, contains) |
| 117 | + }) |
| 118 | +} |
| 119 | + |
| 120 | +func FuzzSubStringExistsInSlice(f *testing.F) { |
| 121 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 122 | + fuzzConsumer := fuzz.NewConsumer(data) |
| 123 | + targetStruct := &struct { |
| 124 | + val string |
| 125 | + slice []string |
| 126 | + }{} |
| 127 | + err := fuzzConsumer.GenerateStruct(targetStruct) |
| 128 | + if err != nil { |
| 129 | + return |
| 130 | + } |
| 131 | + contains := SubStringExistsInSlice(targetStruct.val, targetStruct.slice) |
| 132 | + for _, s := range targetStruct.slice { |
| 133 | + if strings.Contains(s, targetStruct.val) { |
| 134 | + require.True(t, contains) |
| 135 | + return |
| 136 | + } |
| 137 | + } |
| 138 | + require.False(t, contains) |
| 139 | + }) |
| 140 | +} |
| 141 | + |
| 142 | +func FuzzGetRandomSequence(f *testing.F) { |
| 143 | + f.Add("random") |
| 144 | + |
| 145 | + f.Fuzz(func(t *testing.T, sequence string) { |
| 146 | + val := GetRandomSequence(sequence) |
| 147 | + if strings.ToLower(sequence) == "random" { |
| 148 | + require.Contains(t, []string{"serial", "parallel"}, val) |
| 149 | + return |
| 150 | + } |
| 151 | + require.Equal(t, sequence, val) |
| 152 | + }) |
| 153 | +} |
0 commit comments