Skip to content

Commit 37cf263

Browse files
authored
Merge pull request kubernetes#119619 from skitt/intstr-parse-parseint
Limit intstr.Parse() to 32-bit integer parsing
2 parents 4168923 + cc1670d commit 37cf263

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

staging/src/k8s.io/apimachinery/pkg/util/intstr/intstr_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,63 @@ func TestGetValueFromIntOrPercentNil(t *testing.T) {
264264
t.Errorf("expected error got none")
265265
}
266266
}
267+
268+
func TestParse(t *testing.T) {
269+
tests := []struct {
270+
input string
271+
output IntOrString
272+
}{
273+
{
274+
input: "0",
275+
output: IntOrString{Type: Int, IntVal: 0},
276+
},
277+
{
278+
input: "2147483647", // math.MaxInt32
279+
output: IntOrString{Type: Int, IntVal: 2147483647},
280+
},
281+
{
282+
input: "-2147483648", // math.MinInt32
283+
output: IntOrString{Type: Int, IntVal: -2147483648},
284+
},
285+
{
286+
input: "2147483648", // math.MaxInt32+1
287+
output: IntOrString{Type: String, StrVal: "2147483648"},
288+
},
289+
{
290+
input: "-2147483649", // math.MinInt32-1
291+
output: IntOrString{Type: String, StrVal: "-2147483649"},
292+
},
293+
{
294+
input: "9223372036854775807", // math.MaxInt64
295+
output: IntOrString{Type: String, StrVal: "9223372036854775807"},
296+
},
297+
{
298+
input: "-9223372036854775808", // math.MinInt64
299+
output: IntOrString{Type: String, StrVal: "-9223372036854775808"},
300+
},
301+
{
302+
input: "9223372036854775808", // math.MaxInt64+1
303+
output: IntOrString{Type: String, StrVal: "9223372036854775808"},
304+
},
305+
{
306+
input: "-9223372036854775809", // math.MinInt64-1
307+
output: IntOrString{Type: String, StrVal: "-9223372036854775809"},
308+
},
309+
}
310+
311+
for i, test := range tests {
312+
t.Logf("test case %d", i)
313+
value := Parse(test.input)
314+
if test.output.Type != value.Type {
315+
t.Errorf("expected type %d (%v), but got %d (%v)", test.output.Type, test.output, value.Type, value)
316+
continue
317+
}
318+
if value.Type == Int && test.output.IntVal != value.IntVal {
319+
t.Errorf("expected int value %d (%v), but got %d (%v)", test.output.IntVal, test.output, value.IntVal, value)
320+
continue
321+
}
322+
if value.Type == String && test.output.StrVal != value.StrVal {
323+
t.Errorf("expected string value %q (%v), but got %q (%v)", test.output.StrVal, test.output, value.StrVal, value)
324+
}
325+
}
326+
}

0 commit comments

Comments
 (0)