istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/security/authz/builder/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 builder
    16  
    17  import (
    18  	"testing"
    19  
    20  	"istio.io/istio/pilot/pkg/model"
    21  	"istio.io/istio/pilot/pkg/security/trustdomain"
    22  	"istio.io/istio/pkg/fuzz"
    23  )
    24  
    25  func FuzzBuildHTTP(f *testing.F) {
    26  	fuzz.Fuzz(f, func(fg fuzz.Helper) {
    27  		bundle := fuzz.Struct[trustdomain.Bundle](fg)
    28  		push := fuzz.Struct[*model.PushContext](fg, validatePush)
    29  		node := fuzz.Struct[*model.Proxy](fg)
    30  		selectionOpts := model.PolicyMatcherForProxy(node)
    31  		policies := push.AuthzPolicies.ListAuthorizationPolicies(selectionOpts)
    32  		option := fuzz.Struct[Option](fg)
    33  		b := New(bundle, push, policies, option)
    34  		if b == nil {
    35  			fg.T().Skip()
    36  			return // To help linter
    37  		}
    38  		b.BuildHTTP()
    39  	})
    40  }
    41  
    42  func FuzzBuildTCP(f *testing.F) {
    43  	fuzz.Fuzz(f, func(fg fuzz.Helper) {
    44  		bundle := fuzz.Struct[trustdomain.Bundle](fg)
    45  		push := fuzz.Struct[*model.PushContext](fg, validatePush)
    46  		node := fuzz.Struct[*model.Proxy](fg)
    47  		selectionOpts := model.PolicyMatcherForProxy(node)
    48  		policies := push.AuthzPolicies.ListAuthorizationPolicies(selectionOpts)
    49  		option := fuzz.Struct[Option](fg)
    50  		b := New(bundle, push, policies, option)
    51  		if b == nil {
    52  			fg.T().Skip()
    53  			return // To help linter
    54  		}
    55  		b.BuildTCP()
    56  	})
    57  }
    58  
    59  func validatePush(in *model.PushContext) bool {
    60  	if in == nil {
    61  		return false
    62  	}
    63  	if in.AuthzPolicies == nil {
    64  		return false
    65  	}
    66  	return true
    67  }