Skip to content

Commit

Permalink
test(condition): Add Fuzzing
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-kline committed Oct 1, 2024
1 parent bcc8108 commit 6fed949
Show file tree
Hide file tree
Showing 31 changed files with 1,101 additions and 0 deletions.
27 changes: 27 additions & 0 deletions condition/format_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,30 @@ func BenchmarkFormatJSONByte(b *testing.B) {
)
}
}

func FuzzTestFormatJSON(f *testing.F) {
testcases := [][]byte{
[]byte(`{"hello":"world"}`),
[]byte(`["a","b","c"]`),
[]byte(`{hello:"world"}`),
[]byte(`a`),
}

for _, tc := range testcases {
f.Add(tc)
}

f.Fuzz(func(t *testing.T, data []byte) {
ctx := context.TODO()
message := message.New().SetData(data)
insp, err := newFormatJSON(ctx, config.Config{})
if err != nil {
return
}

_, err = insp.Condition(ctx, message)
if err != nil {
return
}
})
}
27 changes: 27 additions & 0 deletions condition/format_mime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,30 @@ func BenchmarkFormatMIME(b *testing.B) {
)
}
}

func FuzzTestFormatMIME(f *testing.F) {
testcases := [][]byte{
[]byte(`{"hello":"world"}`),
[]byte(`["a","b","c"]`),
[]byte(`{hello:"world"}`),
[]byte(`a`),
}

for _, tc := range testcases {
f.Add(tc)
}

f.Fuzz(func(t *testing.T, data []byte) {
ctx := context.TODO()
message := message.New().SetData(data)
insp, err := newFormatMIME(ctx, config.Config{})
if err != nil {
return
}

_, err = insp.Condition(ctx, message)
if err != nil {
return
}
})
}
40 changes: 40 additions & 0 deletions condition/meta_all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,43 @@ func TestAllCondition(t *testing.T) {
})
}
}

func FuzzTestMetaAll(f *testing.F) {
testcases := [][]byte{
[]byte(`{"z":"a"}`),
[]byte(`["a","a","b"]`),
[]byte(`["a","a","a"]`),
}

for _, tc := range testcases {
f.Add(tc)
}

f.Fuzz(func(t *testing.T, data []byte) {
ctx := context.TODO()
message := message.New().SetData(data)
insp, err := newMetaAll(ctx, config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "@this",
},
"conditions": []config.Config{
{
Type: "string_contains",
Settings: map[string]interface{}{
"value": "a",
},
},
},
},
})
if err != nil {
return
}

_, err = insp.Condition(ctx, message)
if err != nil {
return
}
})
}
40 changes: 40 additions & 0 deletions condition/meta_any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,43 @@ func TestAnyCondition(t *testing.T) {
})
}
}

func FuzzTestMetaAny(f *testing.F) {
testcases := [][]byte{
[]byte(`{"z":"a"}`),
[]byte(`["a","b","c"]`),
[]byte(`["a","a","a"]`),
}

for _, tc := range testcases {
f.Add(tc)
}

f.Fuzz(func(t *testing.T, data []byte) {
ctx := context.TODO()
message := message.New().SetData(data)
insp, err := newMetaAny(ctx, config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "@this",
},
"conditions": []config.Config{
{
Type: "string_contains",
Settings: map[string]interface{}{
"value": "a",
},
},
},
},
})
if err != nil {
return
}

_, err = insp.Condition(ctx, message)
if err != nil {
return
}
})
}
40 changes: 40 additions & 0 deletions condition/meta_none_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,43 @@ func TestNoneCondition(t *testing.T) {
})
}
}

func FuzzTestMetaNone(f *testing.F) {
testcases := [][]byte{
[]byte(`{"z":"a"}`),
[]byte(`["b","b","b"]`),
[]byte(`["a","b","c"]`),
}

for _, tc := range testcases {
f.Add(tc)
}

f.Fuzz(func(t *testing.T, data []byte) {
ctx := context.TODO()
message := message.New().SetData(data)
insp, err := newMetaNone(ctx, config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "@this",
},
"conditions": []config.Config{
{
Type: "string_contains",
Settings: map[string]interface{}{
"value": "a",
},
},
},
},
})
if err != nil {
return
}

_, err = insp.Condition(ctx, message)
if err != nil {
return
}
})
}
27 changes: 27 additions & 0 deletions condition/network_ip_global_unicast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,30 @@ func BenchmarkNetworkIPGlobalUnicastByte(b *testing.B) {
)
}
}

func FuzzTestNetworkIPGlobalUnicast(f *testing.F) {
testcases := [][]byte{
[]byte(`{"ip":"192.168.1.1"}`),
[]byte(`{"ip":"2001:0db8:85a3:0000:0000:8a2e:0370:7334"}`),
[]byte(`{"ip":"255.255.255.255"}`),
[]byte(`{"ip":"::1"}`),
}

for _, tc := range testcases {
f.Add(tc)
}

f.Fuzz(func(t *testing.T, data []byte) {
ctx := context.TODO()
message := message.New().SetData(data)
insp, err := newNetworkIPGlobalUnicast(ctx, config.Config{})
if err != nil {
return
}

_, err = insp.Condition(ctx, message)
if err != nil {
return
}
})
}
27 changes: 27 additions & 0 deletions condition/network_ip_link_local_multicast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,30 @@ func BenchmarkNetworkIPLinkLocalMulticastByte(b *testing.B) {
)
}
}

func FuzzTestNetworkIPLinkLocalMulticast(f *testing.F) {
testcases := [][]byte{
[]byte("224.0.0.12"),
[]byte("224.0.0.1"),
[]byte("239.255.255.255"),
[]byte("192.168.1.1"),
}

for _, tc := range testcases {
f.Add(tc)
}

f.Fuzz(func(t *testing.T, data []byte) {
ctx := context.TODO()
message := message.New().SetData(data)
insp, err := newNetworkIPLinkLocalMulticast(ctx, config.Config{})
if err != nil {
return
}

_, err = insp.Condition(ctx, message)
if err != nil {
return
}
})
}
27 changes: 27 additions & 0 deletions condition/network_ip_link_local_unicast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,30 @@ func BenchmarkNetworkIPLinkLocalUnicastByte(b *testing.B) {
)
}
}

func FuzzTestNetworkIPLinkLocalUnicast(f *testing.F) {
testcases := [][]byte{
[]byte("169.254.255.255"),
[]byte("169.254.0.1"),
[]byte("192.168.1.1"),
[]byte("10.0.0.1"),
}

for _, tc := range testcases {
f.Add(tc)
}

f.Fuzz(func(t *testing.T, data []byte) {
ctx := context.TODO()
message := message.New().SetData(data)
insp, err := newNetworkIPLinkLocalUnicast(ctx, config.Config{})
if err != nil {
return
}

_, err = insp.Condition(ctx, message)
if err != nil {
return
}
})
}
27 changes: 27 additions & 0 deletions condition/network_ip_loopback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,30 @@ func BenchmarkNetworkIPLoopbackByte(b *testing.B) {
)
}
}

func FuzzTestNetworkIPLoopback(f *testing.F) {
testcases := [][]byte{
[]byte("127.0.0.1"),
[]byte("8.8.8.8"),
[]byte("::1"),
[]byte("192.168.1.1"),
}

for _, tc := range testcases {
f.Add(tc)
}

f.Fuzz(func(t *testing.T, data []byte) {
ctx := context.TODO()
message := message.New().SetData(data)
insp, err := newNetworkIPLoopback(ctx, config.Config{})
if err != nil {
return
}

_, err = insp.Condition(ctx, message)
if err != nil {
return
}
})
}
27 changes: 27 additions & 0 deletions condition/network_ip_multicast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,30 @@ func BenchmarkNetworkIPMulticastByte(b *testing.B) {
)
}
}

func FuzzTestNetworkIPMulticast(f *testing.F) {
testcases := [][]byte{
[]byte("224.0.0.12"),
[]byte("239.255.255.255"),
[]byte("192.168.1.1"),
[]byte("10.0.0.1"),
}

for _, tc := range testcases {
f.Add(tc)
}

f.Fuzz(func(t *testing.T, data []byte) {
ctx := context.TODO()
message := message.New().SetData(data)
insp, err := newNetworkIPMulticast(ctx, config.Config{})
if err != nil {
return
}

_, err = insp.Condition(ctx, message)
if err != nil {
return
}
})
}
33 changes: 33 additions & 0 deletions condition/network_ip_private_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,36 @@ func BenchmarkNetworkIPPrivateByte(b *testing.B) {
)
}
}

func FuzzTestNetworkIPPrivate(f *testing.F) {
testcases := [][]byte{
[]byte("8.8.8.8"),
[]byte(`{"ip_address":"192.168.1.2"}`),
[]byte("10.0.0.1"),
[]byte("172.16.0.1"),
}

for _, tc := range testcases {
f.Add(tc)
}

f.Fuzz(func(t *testing.T, data []byte) {
ctx := context.TODO()
message := message.New().SetData(data)
insp, err := newNetworkIPPrivate(ctx, config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "ip_address",
},
},
})
if err != nil {
return
}

_, err = insp.Condition(ctx, message)
if err != nil {
return
}
})
}
Loading

0 comments on commit 6fed949

Please sign in to comment.