mirror of https://github.com/k3s-io/k3s
108 lines
2.7 KiB
Go
108 lines
2.7 KiB
Go
/*
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package util
|
|
|
|
import (
|
|
"testing"
|
|
|
|
flag "github.com/spf13/pflag"
|
|
)
|
|
|
|
func TestIP(t *testing.T) {
|
|
testCases := []struct {
|
|
input string
|
|
success bool
|
|
expected string
|
|
}{
|
|
{"0.0.0.0", true, "0.0.0.0"},
|
|
{" 0.0.0.0 ", true, "0.0.0.0"},
|
|
{"1.2.3.4", true, "1.2.3.4"},
|
|
{"127.0.0.1", true, "127.0.0.1"},
|
|
{"255.255.255.255", true, "255.255.255.255"},
|
|
{"", false, ""},
|
|
{"0", false, ""},
|
|
{"localhost", false, ""},
|
|
{"0.0.0", false, ""},
|
|
{"0.0.0.", false, ""},
|
|
{"0.0.0.0.", false, ""},
|
|
{"0.0.0.256", false, ""},
|
|
{"0 . 0 . 0 . 0", false, ""},
|
|
}
|
|
|
|
for i := range testCases {
|
|
tc := &testCases[i]
|
|
var f flag.Value = &IP{}
|
|
err := f.Set(tc.input)
|
|
if err != nil && tc.success == true {
|
|
t.Errorf("expected success, got %q", err)
|
|
continue
|
|
} else if err == nil && tc.success == false {
|
|
t.Errorf("expected failure")
|
|
continue
|
|
} else if tc.success {
|
|
if f.String() != tc.expected {
|
|
t.Errorf("expected %q, got %q", tc.expected, f.String())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestIPNet(t *testing.T) {
|
|
testCases := []struct {
|
|
input string
|
|
success bool
|
|
expected string
|
|
}{
|
|
{"0.0.0.0/0", true, "0.0.0.0/0"},
|
|
{" 0.0.0.0/0 ", true, "0.0.0.0/0"},
|
|
{"1.2.3.4/8", true, "1.0.0.0/8"},
|
|
{"127.0.0.1/16", true, "127.0.0.0/16"},
|
|
{"255.255.255.255/19", true, "255.255.224.0/19"},
|
|
{"255.255.255.255/32", true, "255.255.255.255/32"},
|
|
{"", false, ""},
|
|
{"/0", false, ""},
|
|
{"0", false, ""},
|
|
{"0/0", false, ""},
|
|
{"localhost/0", false, ""},
|
|
{"0.0.0/4", false, ""},
|
|
{"0.0.0./8", false, ""},
|
|
{"0.0.0.0./12", false, ""},
|
|
{"0.0.0.256/16", false, ""},
|
|
{"0.0.0.0 /20", false, ""},
|
|
{"0.0.0.0/ 24", false, ""},
|
|
{"0 . 0 . 0 . 0 / 28", false, ""},
|
|
{"0.0.0.0/33", false, ""},
|
|
}
|
|
|
|
for i := range testCases {
|
|
tc := &testCases[i]
|
|
var f flag.Value = &IPNet{}
|
|
err := f.Set(tc.input)
|
|
if err != nil && tc.success == true {
|
|
t.Errorf("expected success, got %q", err)
|
|
continue
|
|
} else if err == nil && tc.success == false {
|
|
t.Errorf("expected failure")
|
|
continue
|
|
} else if tc.success {
|
|
if f.String() != tc.expected {
|
|
t.Errorf("expected %q, got %q", tc.expected, f.String())
|
|
}
|
|
}
|
|
}
|
|
}
|