...

Source file src/github.com/linkerd/linkerd2/controller/api/destination/destination_fuzzer.go

Documentation: github.com/linkerd/linkerd2/controller/api/destination

     1  package destination
     2  
     3  import (
     4  	"testing"
     5  
     6  	fuzz "github.com/AdaLogics/go-fuzz-headers"
     7  	pb "github.com/linkerd/linkerd2-proxy-api/go/destination"
     8  	"github.com/linkerd/linkerd2/controller/api/destination/watcher"
     9  	"github.com/linkerd/linkerd2/controller/api/util"
    10  	sp "github.com/linkerd/linkerd2/controller/gen/apis/serviceprofile/v1alpha2"
    11  	logging "github.com/sirupsen/logrus"
    12  )
    13  
    14  func init() {
    15  	testing.Init()
    16  }
    17  
    18  // FuzzAdd fuzzes the Add method of the destination server.
    19  func FuzzAdd(data []byte) int {
    20  	f := fuzz.NewConsumer(data)
    21  	set := watcher.AddressSet{}
    22  	err := f.GenerateStruct(&set)
    23  	if err != nil {
    24  		return 0
    25  	}
    26  	t := &testing.T{}
    27  	_, translator := makeEndpointTranslator(t)
    28  	translator.Start()
    29  	defer translator.Stop()
    30  	translator.Add(set)
    31  	translator.Remove(set)
    32  	return 1
    33  }
    34  
    35  // FuzzGet fuzzes the Get method of the destination server.
    36  func FuzzGet(data []byte) int {
    37  	f := fuzz.NewConsumer(data)
    38  	dest1 := &pb.GetDestination{}
    39  	err := f.GenerateStruct(dest1)
    40  	if err != nil {
    41  		return 0
    42  	}
    43  	dest2 := &pb.GetDestination{}
    44  	err = f.GenerateStruct(dest2)
    45  	if err != nil {
    46  		return 0
    47  	}
    48  	dest3 := &pb.GetDestination{}
    49  	err = f.GenerateStruct(dest3)
    50  	if err != nil {
    51  		return 0
    52  	}
    53  	t := &testing.T{}
    54  	server := makeServer(t)
    55  
    56  	stream := &bufferingGetStream{
    57  		updates:          make(chan *pb.Update, 50),
    58  		MockServerStream: util.NewMockServerStream(),
    59  	}
    60  	_ = server.Get(dest1, stream)
    61  	_ = server.Get(dest2, stream)
    62  	_ = server.Get(dest3, stream)
    63  	return 1
    64  }
    65  
    66  // FuzzGetProfile fuzzes the GetProfile method of the destination server.
    67  func FuzzGetProfile(data []byte) int {
    68  	f := fuzz.NewConsumer(data)
    69  	dest := &pb.GetDestination{}
    70  	err := f.GenerateStruct(dest)
    71  	if err != nil {
    72  		return 0
    73  	}
    74  	t := &testing.T{}
    75  	server := makeServer(t)
    76  	stream := &bufferingGetProfileStream{
    77  		updates:          []*pb.DestinationProfile{},
    78  		MockServerStream: util.NewMockServerStream(),
    79  	}
    80  	stream.Cancel()
    81  	_ = server.GetProfile(dest, stream)
    82  	return 1
    83  }
    84  
    85  // FuzzProfileTranslatorUpdate fuzzes the Update method of the profile translator.
    86  func FuzzProfileTranslatorUpdate(data []byte) int {
    87  	f := fuzz.NewConsumer(data)
    88  	profile := &sp.ServiceProfile{}
    89  	err := f.GenerateStruct(profile)
    90  	if err != nil {
    91  		return 0
    92  	}
    93  	t := &testing.T{}
    94  	mockGetProfileServer := &mockDestinationGetProfileServer{profilesReceived: make(chan *pb.DestinationProfile, 50)}
    95  
    96  	translator := newProfileTranslator(mockGetProfileServer, logging.WithField("test", t.Name()), "foo.bar.svc.cluster.local", 80, nil)
    97  	translator.Start()
    98  	defer translator.Stop()
    99  	translator.Update(profile)
   100  	return 1
   101  }
   102  

View as plain text