...

Source file src/k8s.io/apimachinery/pkg/api/apitesting/fuzzer/valuefuzz.go

Documentation: k8s.io/apimachinery/pkg/api/apitesting/fuzzer

     1  /*
     2  Copyright 2017 The Kubernetes 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  	"reflect"
    21  )
    22  
    23  // ValueFuzz recursively changes all basic type values in an object. Any kind of references will not
    24  // be touch, i.e. the addresses of slices, maps, pointers will stay unchanged.
    25  func ValueFuzz(obj interface{}) {
    26  	valueFuzz(reflect.ValueOf(obj))
    27  }
    28  
    29  func valueFuzz(obj reflect.Value) {
    30  	switch obj.Kind() {
    31  	case reflect.Array:
    32  		for i := 0; i < obj.Len(); i++ {
    33  			valueFuzz(obj.Index(i))
    34  		}
    35  	case reflect.Slice:
    36  		if obj.IsNil() {
    37  			// TODO: set non-nil value
    38  		} else {
    39  			for i := 0; i < obj.Len(); i++ {
    40  				valueFuzz(obj.Index(i))
    41  			}
    42  		}
    43  	case reflect.Interface, reflect.Pointer:
    44  		if obj.IsNil() {
    45  			// TODO: set non-nil value
    46  		} else {
    47  			valueFuzz(obj.Elem())
    48  		}
    49  	case reflect.Struct:
    50  		for i, n := 0, obj.NumField(); i < n; i++ {
    51  			valueFuzz(obj.Field(i))
    52  		}
    53  	case reflect.Map:
    54  		if obj.IsNil() {
    55  			// TODO: set non-nil value
    56  		} else {
    57  			for _, k := range obj.MapKeys() {
    58  				// map values are not addressable. We need a copy.
    59  				v := obj.MapIndex(k)
    60  				copy := reflect.New(v.Type())
    61  				copy.Elem().Set(v)
    62  				valueFuzz(copy.Elem())
    63  				obj.SetMapIndex(k, copy.Elem())
    64  			}
    65  			// TODO: set some new value
    66  		}
    67  	case reflect.Func: // ignore, we don't have function types in our API
    68  	default:
    69  		if !obj.CanSet() {
    70  			return
    71  		}
    72  		switch obj.Kind() {
    73  		case reflect.String:
    74  			obj.SetString(obj.String() + "x")
    75  		case reflect.Bool:
    76  			obj.SetBool(!obj.Bool())
    77  		case reflect.Float32, reflect.Float64:
    78  			obj.SetFloat(obj.Float()*2.0 + 1.0)
    79  		case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
    80  			obj.SetInt(obj.Int() + 1)
    81  		case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
    82  			obj.SetUint(obj.Uint() + 1)
    83  		default:
    84  		}
    85  	}
    86  }
    87  

View as plain text