istio.io/istio@v0.0.0-20240520182934-d79c90f27776/tests/fuzz/v1alpha3_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  	"errors"
    19  	"testing"
    20  
    21  	fuzz "github.com/AdaLogics/go-fuzz-headers"
    22  
    23  	"istio.io/istio/pilot/pkg/model"
    24  	"istio.io/istio/pilot/pkg/networking/core"
    25  	"istio.io/istio/tests/fuzz/utils"
    26  )
    27  
    28  func init() {
    29  	testing.Init()
    30  }
    31  
    32  func ValidateTestOptions(to core.TestOptions) error {
    33  	for _, csc := range to.ConfigStoreCaches {
    34  		if csc == nil {
    35  			return errors.New("a ConfigStoreController was nil")
    36  		}
    37  	}
    38  	for _, sr := range to.ServiceRegistries {
    39  		if sr == nil {
    40  			return errors.New("a ServiceRegistry was nil")
    41  		}
    42  	}
    43  	return nil
    44  }
    45  
    46  func FuzzValidateClusters(data []byte) int {
    47  	proxy := model.Proxy{}
    48  	f := fuzz.NewConsumer(data)
    49  	to := core.TestOptions{}
    50  	err := f.GenerateStruct(&to)
    51  	if err != nil {
    52  		return 0
    53  	}
    54  	err = ValidateTestOptions(to)
    55  	if err != nil {
    56  		return 0
    57  	}
    58  	err = f.GenerateStruct(&proxy)
    59  	if err != nil || !proxy.FuzzValidate() {
    60  		return 0
    61  	}
    62  	cg := core.NewConfigGenTest(utils.NopTester{}, to)
    63  	p := cg.SetupProxy(&proxy)
    64  	_ = cg.Clusters(p)
    65  	_ = cg.Routes(p)
    66  	return 1
    67  }