| 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 |
"encoding/json" |
| 19 |
"math" |
| 20 |
"reflect" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"github.com/stretchr/testify/assert" |
| 24 |
"github.com/stretchr/testify/require" |
| 25 |
|
| 26 |
"github.com/go-openapi/swag" |
| 27 |
"k8s.io/kube-openapi/pkg/validation/spec" |
| 28 |
"k8s.io/kube-openapi/pkg/validation/strfmt" |
| 29 |
) |
| 30 |
|
| 31 |
func TestSchemaValidator_Validate_Pattern(t *testing.T) { |
| 32 |
var schemaJSON = ` |
| 33 |
{ |
| 34 |
"properties": { |
| 35 |
"name": { |
| 36 |
"type": "string", |
| 37 |
"pattern": "^[A-Za-z]+$", |
| 38 |
"minLength": 1 |
| 39 |
}, |
| 40 |
"place": { |
| 41 |
"type": "string", |
| 42 |
"pattern": "^[A-Za-z]+$", |
| 43 |
"minLength": 1 |
| 44 |
} |
| 45 |
}, |
| 46 |
"required": [ |
| 47 |
"name" |
| 48 |
] |
| 49 |
}` |
| 50 |
|
| 51 |
schema := new(spec.Schema) |
| 52 |
require.NoError(t, json.Unmarshal([]byte(schemaJSON), schema)) |
| 53 |
|
| 54 |
var input map[string]interface{} |
| 55 |
var inputJSON = `{"name": "Ivan"}` |
| 56 |
|
| 57 |
require.NoError(t, json.Unmarshal([]byte(inputJSON), &input)) |
| 58 |
assert.NoError(t, AgainstSchema(schema, input, strfmt.Default)) |
| 59 |
|
| 60 |
input["place"] = json.Number("10") |
| 61 |
|
| 62 |
assert.Error(t, AgainstSchema(schema, input, strfmt.Default)) |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
func TestSchemaValidator_PatternProperties(t *testing.T) { |
| 67 |
var schemaJSON = ` |
| 68 |
{ |
| 69 |
"properties": { |
| 70 |
"name": { |
| 71 |
"type": "string", |
| 72 |
"pattern": "^[A-Za-z]+$", |
| 73 |
"minLength": 1 |
| 74 |
} |
| 75 |
}, |
| 76 |
"patternProperties": { |
| 77 |
"address-[0-9]+": { |
| 78 |
"type": "string", |
| 79 |
"pattern": "^[\\s|a-z]+$" |
| 80 |
} |
| 81 |
}, |
| 82 |
"required": [ |
| 83 |
"name" |
| 84 |
], |
| 85 |
"additionalProperties": false |
| 86 |
}` |
| 87 |
|
| 88 |
schema := new(spec.Schema) |
| 89 |
require.NoError(t, json.Unmarshal([]byte(schemaJSON), schema)) |
| 90 |
|
| 91 |
var input map[string]interface{} |
| 92 |
|
| 93 |
// ok |
| 94 |
var inputJSON = `{"name": "Ivan","address-1": "sesame street"}` |
| 95 |
require.NoError(t, json.Unmarshal([]byte(inputJSON), &input)) |
| 96 |
assert.NoError(t, AgainstSchema(schema, input, strfmt.Default)) |
| 97 |
|
| 98 |
// fail pattern regexp |
| 99 |
input["address-1"] = "1, Sesame Street" |
| 100 |
assert.Error(t, AgainstSchema(schema, input, strfmt.Default)) |
| 101 |
|
| 102 |
// fail patternProperties regexp |
| 103 |
inputJSON = `{"name": "Ivan","address-1": "sesame street","address-A": "address"}` |
| 104 |
require.NoError(t, json.Unmarshal([]byte(inputJSON), &input)) |
| 105 |
assert.Error(t, AgainstSchema(schema, input, strfmt.Default)) |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
func TestSchemaValidator_ReferencePanic(t *testing.T) { |
| 110 |
assert.PanicsWithValue(t, `schema references not supported: http://localhost:1234/integer.json`, schemaRefValidator) |
| 111 |
} |
| 112 |
|
| 113 |
func schemaRefValidator() { |
| 114 |
var schemaJSON = ` |
| 115 |
{ |
| 116 |
"$ref": "http://localhost:1234/integer.json" |
| 117 |
}` |
| 118 |
|
| 119 |
schema := new(spec.Schema) |
| 120 |
_ = json.Unmarshal([]byte(schemaJSON), schema) |
| 121 |
|
| 122 |
var input map[string]interface{} |
| 123 |
|
| 124 |
// ok |
| 125 |
var inputJSON = `{"name": "Ivan","address-1": "sesame street"}` |
| 126 |
_ = json.Unmarshal([]byte(inputJSON), &input) |
| 127 |
// panics |
| 128 |
_ = AgainstSchema(schema, input, strfmt.Default) |
| 129 |
} |
| 130 |
|
| 131 |
// Test edge cases in schemaValidator which are difficult |
| 132 |
// to simulate with specs |
| 133 |
func TestSchemaValidator_EdgeCases(t *testing.T) { |
| 134 |
var s *SchemaValidator |
| 135 |
|
| 136 |
res := s.Validate("123") |
| 137 |
assert.NotNil(t, res) |
| 138 |
assert.True(t, res.IsValid()) |
| 139 |
|
| 140 |
s = NewSchemaValidator(nil, nil, "", strfmt.Default) |
| 141 |
assert.Nil(t, s) |
| 142 |
|
| 143 |
v := "ABC" |
| 144 |
b := s.Applies(v, reflect.String) |
| 145 |
assert.False(t, b) |
| 146 |
|
| 147 |
sp := spec.Schema{} |
| 148 |
b = s.Applies(&sp, reflect.Struct) |
| 149 |
assert.True(t, b) |
| 150 |
|
| 151 |
spp := spec.Float64Property() |
| 152 |
|
| 153 |
s = NewSchemaValidator(spp, nil, "", strfmt.Default) |
| 154 |
|
| 155 |
s.SetPath("path") |
| 156 |
assert.Equal(t, "path", s.Path) |
| 157 |
|
| 158 |
r := s.Validate(nil) |
| 159 |
assert.NotNil(t, r) |
| 160 |
assert.False(t, r.IsValid()) |
| 161 |
|
| 162 |
// Validating json.Number data against number|float64 |
| 163 |
j := json.Number("123") |
| 164 |
r = s.Validate(j) |
| 165 |
assert.True(t, r.IsValid()) |
| 166 |
|
| 167 |
// Validating json.Number data against integer|int32 |
| 168 |
spp = spec.Int32Property() |
| 169 |
s = NewSchemaValidator(spp, nil, "", strfmt.Default) |
| 170 |
j = json.Number("123") |
| 171 |
r = s.Validate(j) |
| 172 |
assert.True(t, r.IsValid()) |
| 173 |
|
| 174 |
bignum := swag.FormatFloat64(math.MaxFloat64) |
| 175 |
j = json.Number(bignum) |
| 176 |
r = s.Validate(j) |
| 177 |
assert.False(t, r.IsValid()) |
| 178 |
|
| 179 |
// Validating incorrect json.Number data |
| 180 |
spp = spec.Float64Property() |
| 181 |
s = NewSchemaValidator(spp, nil, "", strfmt.Default) |
| 182 |
j = json.Number("AXF") |
| 183 |
r = s.Validate(j) |
| 184 |
assert.False(t, r.IsValid()) |
| 185 |
} |
| 186 |
|
| 187 |
func TestNumericFormatEnforcement(t *testing.T) { |
| 188 |
tests := []struct { |
| 189 |
name string |
| 190 |
schema *spec.Schema |
| 191 |
value interface{} |
| 192 |
expectSuccess bool |
| 193 |
}{ |
| 194 |
// Integer int32 tests |
| 195 |
{ |
| 196 |
name: "int32 valid value", |
| 197 |
schema: &spec.Schema{ |
| 198 |
SchemaProps: spec.SchemaProps{ |
| 199 |
Type: []string{"integer"}, |
| 200 |
Format: "int32", |
| 201 |
}, |
| 202 |
}, |
| 203 |
value: int64(123), |
| 204 |
expectSuccess: true, |
| 205 |
}, |
| 206 |
{ |
| 207 |
name: "int32 overflow value", |
| 208 |
schema: &spec.Schema{ |
| 209 |
SchemaProps: spec.SchemaProps{ |
| 210 |
Type: []string{"integer"}, |
| 211 |
Format: "int32", |
| 212 |
}, |
| 213 |
}, |
| 214 |
value: int64(2147483648), // MaxInt32 + 1 |
| 215 |
expectSuccess: false, |
| 216 |
}, |
| 217 |
{ |
| 218 |
name: "int32 boundary max value", |
| 219 |
schema: &spec.Schema{ |
| 220 |
SchemaProps: spec.SchemaProps{ |
| 221 |
Type: []string{"integer"}, |
| 222 |
Format: "int32", |
| 223 |
}, |
| 224 |
}, |
| 225 |
value: int64(2147483647), // MaxInt32 |
| 226 |
expectSuccess: true, |
| 227 |
}, |
| 228 |
{ |
| 229 |
name: "int32 boundary min value", |
| 230 |
schema: &spec.Schema{ |
| 231 |
SchemaProps: spec.SchemaProps{ |
| 232 |
Type: []string{"integer"}, |
| 233 |
Format: "int32", |
| 234 |
}, |
| 235 |
}, |
| 236 |
value: int64(-2147483648), // MinInt32 |
| 237 |
expectSuccess: true, |
| 238 |
}, |
| 239 |
{ |
| 240 |
name: "int32 underflow value", |
| 241 |
schema: &spec.Schema{ |
| 242 |
SchemaProps: spec.SchemaProps{ |
| 243 |
Type: []string{"integer"}, |
| 244 |
Format: "int32", |
| 245 |
}, |
| 246 |
}, |
| 247 |
value: int64(-2147483649), // MinInt32 - 1 |
| 248 |
expectSuccess: false, |
| 249 |
}, |
| 250 |
|
| 251 |
// Integer int64 tests |
| 252 |
{ |
| 253 |
name: "int64 valid value", |
| 254 |
schema: &spec.Schema{ |
| 255 |
SchemaProps: spec.SchemaProps{ |
| 256 |
Type: []string{"integer"}, |
| 257 |
Format: "int64", |
| 258 |
}, |
| 259 |
}, |
| 260 |
value: int64(9223372036854775807), // MaxInt64 |
| 261 |
expectSuccess: true, |
| 262 |
}, |
| 263 |
|
| 264 |
// Number float (float32) tests |
| 265 |
{ |
| 266 |
name: "float32 valid value", |
| 267 |
schema: &spec.Schema{ |
| 268 |
SchemaProps: spec.SchemaProps{ |
| 269 |
Type: []string{"number"}, |
| 270 |
Format: "float", |
| 271 |
}, |
| 272 |
}, |
| 273 |
value: float64(1.23), |
| 274 |
expectSuccess: true, |
| 275 |
}, |
| 276 |
{ |
| 277 |
name: "float32 overflow value", |
| 278 |
schema: &spec.Schema{ |
| 279 |
SchemaProps: spec.SchemaProps{ |
| 280 |
Type: []string{"number"}, |
| 281 |
Format: "float", |
| 282 |
}, |
| 283 |
}, |
| 284 |
value: float64(math.MaxFloat32) * 1.1, |
| 285 |
expectSuccess: false, |
| 286 |
}, |
| 287 |
|
| 288 |
// Number double (float64) tests |
| 289 |
{ |
| 290 |
name: "double valid value", |
| 291 |
schema: &spec.Schema{ |
| 292 |
SchemaProps: spec.SchemaProps{ |
| 293 |
Type: []string{"number"}, |
| 294 |
Format: "double", |
| 295 |
}, |
| 296 |
}, |
| 297 |
value: float64(1.23), |
| 298 |
expectSuccess: true, |
| 299 |
}, |
| 300 |
{ |
| 301 |
name: "double max value", |
| 302 |
schema: &spec.Schema{ |
| 303 |
SchemaProps: spec.SchemaProps{ |
| 304 |
Type: []string{"number"}, |
| 305 |
Format: "double", |
| 306 |
}, |
| 307 |
}, |
| 308 |
value: math.MaxFloat64, |
| 309 |
expectSuccess: true, |
| 310 |
}, |
| 311 |
|
| 312 |
// Mismatched format tests (format should be ignored) |
| 313 |
{ |
| 314 |
name: "number type with int32 format (format ignored)", |
| 315 |
schema: &spec.Schema{ |
| 316 |
SchemaProps: spec.SchemaProps{ |
| 317 |
Type: []string{"number"}, |
| 318 |
Format: "int32", |
| 319 |
}, |
| 320 |
}, |
| 321 |
value: float64(math.MaxInt32) + 1.0, // Should be valid as a number, ignoring int32 limit |
| 322 |
expectSuccess: true, |
| 323 |
}, |
| 324 |
{ |
| 325 |
name: "integer type with float format (format ignored)", |
| 326 |
schema: &spec.Schema{ |
| 327 |
SchemaProps: spec.SchemaProps{ |
| 328 |
Type: []string{"integer"}, |
| 329 |
Format: "float", |
| 330 |
}, |
| 331 |
}, |
| 332 |
value: int64(math.MaxInt32) * 2, // Should be valid as integer (int64), ignoring float limit/check |
| 333 |
expectSuccess: true, |
| 334 |
}, |
| 335 |
{ |
| 336 |
name: "multiple types with int32 format (format ignored)", |
| 337 |
schema: &spec.Schema{ |
| 338 |
SchemaProps: spec.SchemaProps{ |
| 339 |
Type: []string{"integer", "string"}, |
| 340 |
Format: "int32", |
| 341 |
}, |
| 342 |
}, |
| 343 |
value: int64(math.MaxInt32) + 1, // Should be valid as integer (int64), ignoring int32 limit due to ambiguity |
| 344 |
expectSuccess: true, |
| 345 |
}, |
| 346 |
} |
| 347 |
|
| 348 |
for _, tc := range tests { |
| 349 |
t.Run(tc.name, func(t *testing.T) { |
| 350 |
validator := NewSchemaValidator(tc.schema, nil, "", strfmt.Default) |
| 351 |
res := validator.Validate(tc.value) |
| 352 |
if tc.expectSuccess { |
| 353 |
assert.Empty(t, res.Errors, "Expected success for %s", tc.name) |
| 354 |
} else { |
| 355 |
assert.NotEmpty(t, res.Errors, "Expected error for %s", tc.name) |
| 356 |
} |
| 357 |
}) |
| 358 |
} |
| 359 |
} |