2015-07-12 11:33:30 +03:00
|
|
|
package opts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIpOptString(t *testing.T) {
|
|
|
|
addresses := []string{"", "0.0.0.0"}
|
|
|
|
var ip net.IP
|
|
|
|
|
|
|
|
for _, address := range addresses {
|
2015-07-28 21:18:04 +03:00
|
|
|
stringAddress := NewIPOpt(&ip, address).String()
|
2015-07-12 11:33:30 +03:00
|
|
|
if stringAddress != address {
|
|
|
|
t.Fatalf("IpOpt string should be `%s`, not `%s`", address, stringAddress)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewIpOptInvalidDefaultVal(t *testing.T) {
|
|
|
|
ip := net.IPv4(127, 0, 0, 1)
|
|
|
|
defaultVal := "Not an ip"
|
|
|
|
|
2015-07-28 21:18:04 +03:00
|
|
|
ipOpt := NewIPOpt(&ip, defaultVal)
|
2015-07-12 11:33:30 +03:00
|
|
|
|
|
|
|
expected := "127.0.0.1"
|
|
|
|
if ipOpt.String() != expected {
|
|
|
|
t.Fatalf("Expected [%v], got [%v]", expected, ipOpt.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewIpOptValidDefaultVal(t *testing.T) {
|
|
|
|
ip := net.IPv4(127, 0, 0, 1)
|
|
|
|
defaultVal := "192.168.1.1"
|
|
|
|
|
2015-07-28 21:18:04 +03:00
|
|
|
ipOpt := NewIPOpt(&ip, defaultVal)
|
2015-07-12 11:33:30 +03:00
|
|
|
|
|
|
|
expected := "192.168.1.1"
|
|
|
|
if ipOpt.String() != expected {
|
|
|
|
t.Fatalf("Expected [%v], got [%v]", expected, ipOpt.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIpOptSetInvalidVal(t *testing.T) {
|
|
|
|
ip := net.IPv4(127, 0, 0, 1)
|
2015-07-28 21:18:04 +03:00
|
|
|
ipOpt := &IPOpt{IP: &ip}
|
2015-07-12 11:33:30 +03:00
|
|
|
|
2015-07-21 19:15:36 +03:00
|
|
|
invalidIP := "invalid ip"
|
2015-07-12 11:33:30 +03:00
|
|
|
expectedError := "invalid ip is not an ip address"
|
2015-07-21 19:15:36 +03:00
|
|
|
err := ipOpt.Set(invalidIP)
|
2015-07-12 11:33:30 +03:00
|
|
|
if err == nil || err.Error() != expectedError {
|
|
|
|
t.Fatalf("Expected an Error with [%v], got [%v]", expectedError, err.Error())
|
|
|
|
}
|
|
|
|
}
|