istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/bootstrap/fuzz_test.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 bootstrap
    16  
    17  import (
    18  	"bytes"
    19  	"io"
    20  	"os"
    21  	"testing"
    22  
    23  	fuzz "github.com/AdaLogics/go-fuzz-headers"
    24  )
    25  
    26  func FuzzWriteTo(f *testing.F) {
    27  	f.Fuzz(func(t *testing.T, data []byte) {
    28  		ff := fuzz.NewConsumer(data)
    29  
    30  		// create config
    31  		cfg := Config{}
    32  
    33  		err := ff.GenerateStruct(&cfg)
    34  		if err != nil {
    35  			return
    36  		}
    37  
    38  		if cfg.Metadata == nil {
    39  			return
    40  		}
    41  		if cfg.Metadata.ProxyConfig == nil {
    42  			return
    43  		}
    44  
    45  		i := New(cfg)
    46  
    47  		// create template file
    48  		templateBytes, err := ff.GetBytes()
    49  		if err != nil {
    50  			return
    51  		}
    52  
    53  		tf, err := os.Create("templateFile")
    54  		if err != nil {
    55  			return
    56  		}
    57  		defer func() {
    58  			tf.Close()
    59  			os.Remove("templateFile")
    60  		}()
    61  		_, err = tf.Write(templateBytes)
    62  		if err != nil {
    63  			return
    64  		}
    65  
    66  		// call target
    67  		var buf bytes.Buffer
    68  		i.WriteTo("templateFile", io.Writer(&buf))
    69  	})
    70  }