istio.io/istio@v0.0.0-20240520182934-d79c90f27776/tests/fuzz/bootstrap_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  // nolint: revive
    16  package fuzz
    17  
    18  import (
    19  	"os"
    20  	"time"
    21  
    22  	fuzz "github.com/AdaLogics/go-fuzz-headers"
    23  
    24  	"istio.io/istio/pilot/pkg/bootstrap"
    25  	"istio.io/istio/pkg/config/mesh"
    26  )
    27  
    28  func FuzzNewBootstrapServer(data []byte) int {
    29  	f := fuzz.NewConsumer(data)
    30  
    31  	// Create mesh config file
    32  	meshConfigFile, err := createRandomConfigFile(f)
    33  	if err != nil {
    34  		return 0
    35  	}
    36  	defer os.Remove(meshConfigFile)
    37  	_, err = os.Stat(meshConfigFile)
    38  	if err != nil {
    39  		return 0
    40  	}
    41  	// Validate mesh config file
    42  	meshConfigYaml, err := mesh.ReadMeshConfigData(meshConfigFile)
    43  	if err != nil {
    44  		return 0
    45  	}
    46  	_, err = mesh.ApplyMeshConfigDefaults(meshConfigYaml)
    47  	if err != nil {
    48  		return 0
    49  	}
    50  
    51  	// Create kube config file
    52  	kubeConfigFile, err := createRandomConfigFile(f)
    53  	if err != nil {
    54  		return 0
    55  	}
    56  	defer os.Remove(kubeConfigFile)
    57  
    58  	args := &bootstrap.PilotArgs{}
    59  	err = f.GenerateStruct(args)
    60  	if err != nil {
    61  		return 0
    62  	}
    63  
    64  	args.MeshConfigFile = meshConfigFile
    65  	args.RegistryOptions.KubeConfig = kubeConfigFile
    66  	args.ShutdownDuration = 1 * time.Millisecond
    67  
    68  	stop := make(chan struct{})
    69  	s, err := bootstrap.NewServer(args)
    70  	if err != nil {
    71  		return 0
    72  	}
    73  	err = s.Start(stop)
    74  	if err != nil {
    75  		return 0
    76  	}
    77  	defer func() {
    78  		close(stop)
    79  		s.WaitUntilCompletion()
    80  	}()
    81  	return 1
    82  }