istio.io/istio@v0.0.0-20240520182934-d79c90f27776/tests/fuzz/inject_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  	"bytes"
    19  
    20  	fuzz "github.com/AdaLogics/go-fuzz-headers"
    21  
    22  	"istio.io/istio/pkg/config/mesh"
    23  	"istio.io/istio/pkg/kube/inject"
    24  )
    25  
    26  func FuzzIntoResourceFile(data []byte) int {
    27  	f := fuzz.NewConsumer(data)
    28  	var sidecarTemplate map[string]string
    29  	err := f.FuzzMap(&sidecarTemplate)
    30  	if err != nil {
    31  		return 0
    32  	}
    33  	valuesConfig, err := f.GetString()
    34  	if err != nil {
    35  		return 0
    36  	}
    37  	meshYaml, err := f.GetString()
    38  	if err != nil {
    39  		return 0
    40  	}
    41  	mc, err := mesh.ApplyMeshConfigDefaults(meshYaml)
    42  	if err != nil {
    43  		return 0
    44  	}
    45  	inData, err := f.GetBytes()
    46  	if err != nil {
    47  		return 0
    48  	}
    49  	in := bytes.NewReader(inData)
    50  	var got bytes.Buffer
    51  	warn := func(s string) {}
    52  	revision, err := f.GetString()
    53  	if err != nil {
    54  		return 0
    55  	}
    56  	templs, err := inject.ParseTemplates(sidecarTemplate)
    57  	if err != nil {
    58  		return 0
    59  	}
    60  	vc, err := inject.NewValuesConfig(valuesConfig)
    61  	if err != nil {
    62  		return 0
    63  	}
    64  	_ = inject.IntoResourceFile(nil, templs, vc, revision, mc, in, &got, warn)
    65  	return 1
    66  }