istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/networking/util/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 util 16 17 import ( 18 "encoding/json" 19 "testing" 20 21 networking "istio.io/api/networking/v1alpha3" 22 "istio.io/istio/pkg/fuzz" 23 "istio.io/istio/pkg/test/util/assert" 24 ) 25 26 func FuzzShallowCopyTrafficPolicy(f *testing.F) { 27 fuzz.Fuzz(f, func(fg fuzz.Helper) { 28 r := fuzz.Struct[*networking.TrafficPolicy](fg) 29 copied := ShallowCopyTrafficPolicy(r) 30 assert.Equal(fg.T(), r, copied) 31 }) 32 } 33 34 func FuzzShallowCopyPortTrafficPolicy(f *testing.F) { 35 fuzz.Fuzz(f, func(fg fuzz.Helper) { 36 r := fuzz.Struct[*networking.TrafficPolicy_PortTrafficPolicy](fg) 37 38 // The port does not need to be copied. 39 r.Port = nil 40 41 // The type has changed and cannot be compared directly. 42 // Convert to []byte for comparison. 43 copied := shadowCopyPortTrafficPolicy(r) 44 rjs, _ := json.Marshal(r) 45 cjs, _ := json.Marshal(copied) 46 assert.Equal(fg.T(), rjs, cjs) 47 }) 48 } 49 50 func FuzzMergeTrafficPolicy(f *testing.F) { 51 fuzz.Fuzz(f, func(fg fuzz.Helper) { 52 copyFrom := fuzz.Struct[*networking.TrafficPolicy](fg) 53 54 empty1 := &networking.TrafficPolicy{} 55 copied := mergeTrafficPolicy(empty1, copyFrom, true) 56 assert.Equal(fg.T(), copyFrom, copied) 57 58 empty2 := &networking.TrafficPolicy{} 59 copied = mergeTrafficPolicy(empty2, copied, true) 60 assert.Equal(fg.T(), copyFrom, copied) 61 }) 62 }