knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/testing/fuzzer/fuzzer.go (about) 1 /* 2 Copyright 2020 The Knative Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package fuzzer 18 19 import ( 20 "math/rand" 21 "net/url" 22 23 corev1 "k8s.io/api/core/v1" 24 "k8s.io/apimachinery/pkg/api/apitesting/fuzzer" 25 "k8s.io/apimachinery/pkg/runtime/serializer" 26 "knative.dev/pkg/apis" 27 "sigs.k8s.io/randfill" 28 ) 29 30 // Funcs includes fuzzing funcs for knative.dev/serving types 31 // 32 // For other examples see 33 // https://github.com/kubernetes/apimachinery/blob/master/pkg/apis/meta/fuzzer/fuzzer.go 34 var Funcs = fuzzer.MergeFuzzerFuncs( 35 func(codecs serializer.CodecFactory) []any { 36 return []any{ 37 func(u *apis.URL, c randfill.Continue) { 38 u.Scheme = randStringAtoZ(c.Rand) 39 u.Host = randStringAtoZ(c.Rand) 40 u.User = url.UserPassword( 41 randStringAtoZ(c.Rand), // username 42 randStringAtoZ(c.Rand), // password 43 ) 44 u.RawPath = url.PathEscape(c.String(0)) 45 u.RawQuery = url.QueryEscape(c.String(0)) 46 }, 47 } 48 }, 49 ) 50 51 // FuzzConditions fuzzes the values for the conditions. It doesn't add 52 // any new condition types 53 // 54 // Consumers should initialize their conditions prior to fuzzing them. 55 // For example: 56 // 57 // func(s *SomeStatus, c fuzz.Continue) { 58 // c.FuzzNoCustom(s) // fuzz the status object 59 // 60 // // Clear the random fuzzed condition 61 // s.Status.SetConditions(nil) 62 // 63 // // Fuzz the known conditions except their type value 64 // s.InitializeConditions() 65 // fuzz.Conditions(&s.Status, c) 66 // } 67 func FuzzConditions(accessor apis.ConditionsAccessor, c randfill.Continue) { 68 conds := accessor.GetConditions() 69 for i, cond := range conds { 70 // Leave condition.Type untouched 71 cond.Status = corev1.ConditionStatus(c.String(0)) 72 cond.Severity = apis.ConditionSeverity(c.String(0)) 73 cond.Message = c.String(0) 74 cond.Reason = c.String(0) 75 c.FillNoCustom(&cond.LastTransitionTime) 76 conds[i] = cond 77 } 78 accessor.SetConditions(conds) 79 } 80 81 // taken from gofuzz internals for RandString 82 type charRange struct { 83 first, last rune 84 } 85 86 func (c *charRange) choose(r *rand.Rand) rune { 87 count := int64(c.last - c.first + 1) 88 ch := c.first + rune(r.Int63n(count)) 89 90 return ch 91 } 92 93 // not fully exhaustive 94 func randStringAtoZ(r *rand.Rand) string { 95 hostCharRange := charRange{'a', 'z'} 96 97 n := r.Intn(20) 98 runes := make([]rune, n) 99 for i := range runes { 100 runes[i] = hostCharRange.choose(r) 101 } 102 return string(runes) 103 }