Skip to content

Commit d5872be

Browse files
authored
test: add fuzzers for egctl.translateGatewayAPIToIR (#5430)
* test: add fuzz testing for GatewayAPI to IR translation Signed-off-by: sudipto baral <[email protected]> * test: Generate boolean parameter with go-fuzz-header Signed-off-by: sudipto baral <[email protected]> --------- Signed-off-by: sudipto baral <[email protected]>
1 parent 36060cb commit d5872be

File tree

5 files changed

+59
-3
lines changed

5 files changed

+59
-3
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.24.0
55
require (
66
fortio.org/fortio v1.68.0
77
fortio.org/log v1.17.1
8+
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24
89
github.com/Masterminds/semver/v3 v3.3.1
910
github.com/andybalholm/brotli v1.1.1
1011
github.com/avast/retry-go v3.0.0+incompatible
@@ -107,7 +108,6 @@ require (
107108
fortio.org/version v1.0.4 // indirect
108109
github.com/4meepo/tagalign v1.4.1 // indirect
109110
github.com/Abirdcfly/dupword v0.1.3 // indirect
110-
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 // indirect
111111
github.com/Antonboom/errname v1.0.0 // indirect
112112
github.com/Antonboom/nilnil v1.0.1 // indirect
113113
github.com/Antonboom/testifylint v1.5.2 // indirect

internal/cmd/egctl/translate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func translate(w io.Writer, inFile, inType string, outTypes []string, output, re
246246
result.Xds = res
247247
}
248248
if outType == irType {
249-
res, err := translateGatewayAPIToIR(resources)
249+
res, err := TranslateGatewayAPIToIR(resources)
250250
if err != nil {
251251
return err
252252
}
@@ -265,7 +265,7 @@ func translate(w io.Writer, inFile, inType string, outTypes []string, output, re
265265
return fmt.Errorf("unable to find translate from input type %s to output type %s", inType, outTypes)
266266
}
267267

268-
func translateGatewayAPIToIR(resources *resource.Resources) (*gatewayapi.TranslateResult, error) {
268+
func TranslateGatewayAPIToIR(resources *resource.Resources) (*gatewayapi.TranslateResult, error) {
269269
if resources.GatewayClass == nil {
270270
return nil, fmt.Errorf("the GatewayClass resource is required")
271271
}

test/fuzz/ir_fuzz_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright Envoy Gateway Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
// The full text of the Apache license is available in the LICENSE file at
4+
// the root of the repo.
5+
6+
package fuzz
7+
8+
import (
9+
"testing"
10+
11+
fuzz "github.com/AdaLogics/go-fuzz-headers"
12+
"sigs.k8s.io/yaml"
13+
14+
"github.com/envoyproxy/gateway/internal/cmd/egctl"
15+
"github.com/envoyproxy/gateway/internal/gatewayapi/resource"
16+
)
17+
18+
func FuzzGatewayAPIToIR(f *testing.F) {
19+
f.Fuzz(func(t *testing.T, data []byte) {
20+
fc := fuzz.NewConsumer(data)
21+
resources := &resource.Resources{}
22+
if err := fc.GenerateStruct(resources); err != nil {
23+
return
24+
}
25+
addMissingResources, err := fc.GetBool()
26+
if err != nil {
27+
return
28+
}
29+
30+
// Populate default values
31+
yamlBytes, err := yaml.Marshal(resources)
32+
if err != nil {
33+
return
34+
}
35+
rs, err := resource.LoadResourcesFromYAMLBytes(yamlBytes, addMissingResources)
36+
if err != nil {
37+
return
38+
}
39+
40+
_, _ = egctl.TranslateGatewayAPIToIR(rs)
41+
})
42+
}

tools/make/env.mk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ IMAGE_NAME ?= gateway-dev
1515
IMAGE ?= ${REGISTRY}/${IMAGE_NAME}
1616
# Tag is the tag to use for build and push image targets.
1717
TAG ?= $(REV)
18+
19+
# Fuzzing variables
20+
21+
# FUZZ_TIME is the time to run the fuzzer for
22+
FUZZ_TIME ?= 5s

tools/make/golang.mk

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ go.build: $(addprefix go.build., $(addprefix $(PLATFORM)., $(BINS)))
3939
.PHONY: go.build.multiarch
4040
go.build.multiarch: $(foreach p,$(PLATFORMS),$(addprefix go.build., $(addprefix $(p)., $(BINS))))
4141

42+
.PHONY: go.test.fuzz
43+
go.test.fuzz: ## Run all fuzzers in the test/fuzz folder one by one
44+
@$(LOG_TARGET)
45+
@for dir in $$(go list -f '{{.Dir}}' ./test/fuzz/...); do \
46+
for test in $$(go test -list=Fuzz.* $$dir | grep ^Fuzz); do \
47+
echo "go test -fuzz=$$test -fuzztime=$(FUZZ_TIME)"; \
48+
(cd $$dir && go test -fuzz=$$test -fuzztime=$(FUZZ_TIME)) || exit 1; \
49+
done; \
50+
done
4251

4352
.PHONY: go.test.unit
4453
go.test.unit: ## Run go unit tests

0 commit comments

Comments
 (0)