2014-02-12 06:46:55 +04:00
|
|
|
package opts
|
2013-12-03 02:33:33 +04:00
|
|
|
|
|
|
|
import (
|
2015-02-09 17:59:05 +03:00
|
|
|
"strings"
|
2013-12-03 02:33:33 +04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-07-10 01:47:55 +04:00
|
|
|
func TestValidateIPAddress(t *testing.T) {
|
|
|
|
if ret, err := ValidateIPAddress(`1.2.3.4`); err != nil || ret == "" {
|
|
|
|
t.Fatalf("ValidateIPAddress(`1.2.3.4`) got %s %s", ret, err)
|
2013-12-03 02:33:33 +04:00
|
|
|
}
|
|
|
|
|
2014-07-10 01:47:55 +04:00
|
|
|
if ret, err := ValidateIPAddress(`127.0.0.1`); err != nil || ret == "" {
|
|
|
|
t.Fatalf("ValidateIPAddress(`127.0.0.1`) got %s %s", ret, err)
|
2013-12-03 02:33:33 +04:00
|
|
|
}
|
|
|
|
|
2014-07-10 01:47:55 +04:00
|
|
|
if ret, err := ValidateIPAddress(`::1`); err != nil || ret == "" {
|
|
|
|
t.Fatalf("ValidateIPAddress(`::1`) got %s %s", ret, err)
|
2013-12-03 02:33:33 +04:00
|
|
|
}
|
|
|
|
|
2014-07-10 01:47:55 +04:00
|
|
|
if ret, err := ValidateIPAddress(`127`); err == nil || ret != "" {
|
|
|
|
t.Fatalf("ValidateIPAddress(`127`) got %s %s", ret, err)
|
2014-06-13 16:02:12 +04:00
|
|
|
}
|
|
|
|
|
2014-07-10 01:47:55 +04:00
|
|
|
if ret, err := ValidateIPAddress(`random invalid string`); err == nil || ret != "" {
|
|
|
|
t.Fatalf("ValidateIPAddress(`random invalid string`) got %s %s", ret, err)
|
2013-12-03 02:33:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-02-07 20:48:14 +04:00
|
|
|
|
2014-08-10 05:13:44 +04:00
|
|
|
func TestListOpts(t *testing.T) {
|
|
|
|
o := NewListOpts(nil)
|
|
|
|
o.Set("foo")
|
2014-10-07 05:54:52 +04:00
|
|
|
if o.String() != "[foo]" {
|
|
|
|
t.Errorf("%s != [foo]", o.String())
|
|
|
|
}
|
|
|
|
o.Set("bar")
|
|
|
|
if o.Len() != 2 {
|
|
|
|
t.Errorf("%d != 2", o.Len())
|
|
|
|
}
|
|
|
|
if !o.Get("bar") {
|
|
|
|
t.Error("o.Get(\"bar\") == false")
|
|
|
|
}
|
|
|
|
if o.Get("baz") {
|
|
|
|
t.Error("o.Get(\"baz\") == true")
|
|
|
|
}
|
|
|
|
o.Delete("foo")
|
|
|
|
if o.String() != "[bar]" {
|
|
|
|
t.Errorf("%s != [bar]", o.String())
|
|
|
|
}
|
2014-08-10 05:13:44 +04:00
|
|
|
}
|
|
|
|
|
2014-06-26 15:03:23 +04:00
|
|
|
func TestValidateDnsSearch(t *testing.T) {
|
2014-02-07 20:48:14 +04:00
|
|
|
valid := []string{
|
2014-06-26 15:03:23 +04:00
|
|
|
`.`,
|
2014-02-07 20:48:14 +04:00
|
|
|
`a`,
|
|
|
|
`a.`,
|
|
|
|
`1.foo`,
|
|
|
|
`17.foo`,
|
|
|
|
`foo.bar`,
|
|
|
|
`foo.bar.baz`,
|
|
|
|
`foo.bar.`,
|
|
|
|
`foo.bar.baz`,
|
|
|
|
`foo1.bar2`,
|
|
|
|
`foo1.bar2.baz`,
|
|
|
|
`1foo.2bar.`,
|
|
|
|
`1foo.2bar.baz`,
|
|
|
|
`foo-1.bar-2`,
|
|
|
|
`foo-1.bar-2.baz`,
|
|
|
|
`foo-1.bar-2.`,
|
|
|
|
`foo-1.bar-2.baz`,
|
|
|
|
`1-foo.2-bar`,
|
|
|
|
`1-foo.2-bar.baz`,
|
|
|
|
`1-foo.2-bar.`,
|
|
|
|
`1-foo.2-bar.baz`,
|
|
|
|
}
|
|
|
|
|
|
|
|
invalid := []string{
|
|
|
|
``,
|
2014-06-26 15:03:23 +04:00
|
|
|
` `,
|
|
|
|
` `,
|
2014-02-07 20:48:14 +04:00
|
|
|
`17`,
|
|
|
|
`17.`,
|
|
|
|
`.17`,
|
|
|
|
`17-.`,
|
|
|
|
`17-.foo`,
|
|
|
|
`.foo`,
|
|
|
|
`foo-.bar`,
|
|
|
|
`-foo.bar`,
|
|
|
|
`foo.bar-`,
|
|
|
|
`foo.bar-.baz`,
|
|
|
|
`foo.-bar`,
|
|
|
|
`foo.-bar.baz`,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, domain := range valid {
|
2014-06-26 15:03:23 +04:00
|
|
|
if ret, err := ValidateDnsSearch(domain); err != nil || ret == "" {
|
|
|
|
t.Fatalf("ValidateDnsSearch(`"+domain+"`) got %s %s", ret, err)
|
2014-02-07 20:48:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, domain := range invalid {
|
2014-06-26 15:03:23 +04:00
|
|
|
if ret, err := ValidateDnsSearch(domain); err == nil || ret != "" {
|
|
|
|
t.Fatalf("ValidateDnsSearch(`"+domain+"`) got %s %s", ret, err)
|
2014-02-07 20:48:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-04 18:20:28 +03:00
|
|
|
|
|
|
|
func TestValidateExtraHosts(t *testing.T) {
|
|
|
|
valid := []string{
|
|
|
|
`myhost:192.168.0.1`,
|
|
|
|
`thathost:10.0.2.1`,
|
|
|
|
`anipv6host:2003:ab34:e::1`,
|
|
|
|
`ipv6local:::1`,
|
|
|
|
}
|
|
|
|
|
2015-02-09 17:59:05 +03:00
|
|
|
invalid := map[string]string{
|
|
|
|
`myhost:192.notanipaddress.1`: `invalid IP`,
|
|
|
|
`thathost-nosemicolon10.0.0.1`: `bad format`,
|
|
|
|
`anipv6host:::::1`: `invalid IP`,
|
|
|
|
`ipv6local:::0::`: `invalid IP`,
|
2015-02-04 18:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, extrahost := range valid {
|
|
|
|
if _, err := ValidateExtraHost(extrahost); err != nil {
|
|
|
|
t.Fatalf("ValidateExtraHost(`"+extrahost+"`) should succeed: error %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-09 17:59:05 +03:00
|
|
|
for extraHost, expectedError := range invalid {
|
|
|
|
if _, err := ValidateExtraHost(extraHost); err == nil {
|
|
|
|
t.Fatalf("ValidateExtraHost(`%q`) should have failed validation", extraHost)
|
|
|
|
} else {
|
|
|
|
if !strings.Contains(err.Error(), expectedError) {
|
|
|
|
t.Fatalf("ValidateExtraHost(`%q`) error should contain %q", extraHost, expectedError)
|
|
|
|
}
|
2015-02-04 18:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|