istio.io/istio@v0.0.0-20240520182934-d79c90f27776/tests/fuzz/kube_crd_fuzzer.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package fuzz
    16  
    17  import (
    18  	fuzz "github.com/AdaLogics/go-fuzz-headers"
    19  
    20  	"istio.io/istio/pilot/pkg/config/kube/crd"
    21  	config2 "istio.io/istio/pkg/config"
    22  	"istio.io/istio/pkg/config/schema/collections"
    23  )
    24  
    25  // FuzzKubeCRD implements a fuzzer that targets
    26  // the kube CRD in two steps.
    27  // It first creates an object with a config
    28  // that has had pseudo-random values inserted.
    29  // When a valid object has been created, it
    30  // tries and convert that object. If this
    31  // conversion fails, it panics.
    32  func FuzzKubeCRD(data []byte) int {
    33  	f := fuzz.NewConsumer(data)
    34  	config := config2.Config{}
    35  	err := f.GenerateStruct(&config)
    36  	if err != nil {
    37  		return 0
    38  	}
    39  
    40  	// Create a valid obj:
    41  	obj, err := crd.ConvertConfig(config)
    42  	if err != nil {
    43  		return 0
    44  	}
    45  
    46  	// Convert the obj and report if it fails.
    47  	_, err = crd.ConvertObject(collections.VirtualService, obj, "cluster")
    48  	if err != nil {
    49  		panic(err)
    50  	}
    51  	return 1
    52  }