k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/validation/validate/validator_test.go (about) 1 // Copyright 2015 go-swagger maintainers 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 validate 16 17 import ( 18 "math" 19 "reflect" 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 "k8s.io/kube-openapi/pkg/validation/spec" 24 ) 25 26 func TestNumberValidator_EdgeCases(t *testing.T) { 27 // Apply 28 var min = float64(math.MinInt32 - 1) 29 var max = float64(math.MaxInt32 + 1) 30 31 v := numberValidator{ 32 Path: "path", 33 In: "in", 34 //Default: 35 //MultipleOf: 36 Maximum: &max, // *float64 37 ExclusiveMaximum: false, 38 Minimum: &min, // *float64 39 ExclusiveMinimum: false, 40 // Allows for more accurate behavior regarding integers 41 Type: "integer", 42 Format: "int32", 43 } 44 45 // numberValidator applies to: Parameter,Schema,Items,Header 46 47 sources := []interface{}{ 48 new(spec.Schema), 49 } 50 51 testNumberApply(t, &v, sources) 52 53 assert.False(t, v.Applies(float64(32), reflect.Float64)) 54 55 // Now for different scenarios on Minimum, Maximum 56 // - The Maximum value does not respect the Type|Format specification 57 // - Value is checked as float64 with Maximum as float64 and fails 58 res := v.Validate(int64(math.MaxInt32 + 2)) 59 assert.True(t, res.HasErrors()) 60 // - The Minimum value does not respect the Type|Format specification 61 // - Value is checked as float64 with Maximum as float64 and fails 62 res = v.Validate(int64(math.MinInt32 - 2)) 63 assert.True(t, res.HasErrors()) 64 } 65 66 func testNumberApply(t *testing.T, v *numberValidator, sources []interface{}) { 67 for _, source := range sources { 68 // numberValidator does not applies to: 69 assert.False(t, v.Applies(source, reflect.String)) 70 assert.False(t, v.Applies(source, reflect.Struct)) 71 // numberValidator applies to: 72 assert.True(t, v.Applies(source, reflect.Int)) 73 assert.True(t, v.Applies(source, reflect.Int8)) 74 assert.True(t, v.Applies(source, reflect.Uint16)) 75 assert.True(t, v.Applies(source, reflect.Uint32)) 76 assert.True(t, v.Applies(source, reflect.Uint64)) 77 assert.True(t, v.Applies(source, reflect.Uint)) 78 assert.True(t, v.Applies(source, reflect.Uint8)) 79 assert.True(t, v.Applies(source, reflect.Uint16)) 80 assert.True(t, v.Applies(source, reflect.Uint32)) 81 assert.True(t, v.Applies(source, reflect.Uint64)) 82 assert.True(t, v.Applies(source, reflect.Float32)) 83 assert.True(t, v.Applies(source, reflect.Float64)) 84 } 85 } 86 87 func TestStringValidator_EdgeCases(t *testing.T) { 88 // Apply 89 90 v := stringValidator{} 91 92 // stringValidator applies to: Parameter,Schema,Items,Header 93 94 sources := []interface{}{ 95 new(spec.Schema), 96 } 97 98 testStringApply(t, &v, sources) 99 100 assert.False(t, v.Applies("A string", reflect.String)) 101 102 } 103 104 func testStringApply(t *testing.T, v *stringValidator, sources []interface{}) { 105 for _, source := range sources { 106 // numberValidator does not applies to: 107 assert.False(t, v.Applies(source, reflect.Struct)) 108 assert.False(t, v.Applies(source, reflect.Int)) 109 // numberValidator applies to: 110 assert.True(t, v.Applies(source, reflect.String)) 111 } 112 } 113 114 func TestBasicCommonValidator_EdgeCases(t *testing.T) { 115 // Apply 116 117 v := basicCommonValidator{} 118 119 // basicCommonValidator applies to: Parameter,Schema,Header 120 121 sources := []interface{}{ 122 new(spec.Schema), 123 } 124 125 testCommonApply(t, &v, sources) 126 127 assert.False(t, v.Applies("A string", reflect.String)) 128 129 } 130 131 func testCommonApply(t *testing.T, v *basicCommonValidator, sources []interface{}) { 132 for _, source := range sources { 133 assert.True(t, v.Applies(source, reflect.String)) 134 } 135 }