diff --git a/config/config.go b/config/config.go index 7374a58..5c62942 100644 --- a/config/config.go +++ b/config/config.go @@ -23,11 +23,11 @@ func IsNet(host string) (bool, string) { // E.g., "[fe80::1]:80". i := strings.LastIndex(host, "]") if i < 0 { - log.Fatal("Unmatched [ in destination config") + log.Println("Unmatched [ in destination config") return false, "" } if !validPort(host[i+1:]) { - log.Fatal("No valid port specified") + log.Println("No valid port specified") return false, "" } // trim brackets @@ -36,21 +36,27 @@ func IsNet(host string) (bool, string) { } } else { // Ipv4 or DNS name - ss := strings.Split(string(host), ":") + ss := strings.Split(host, ":") if len(ss) > 1 { if !validPort(":" + ss[1]) { - log.Fatal("No valid port specified") + log.Println("No valid port specified") return false, "" } + // if not nil, its a valid IP adress if net.ParseIP(ss[0]) != nil { return true, host } - if validDNS.MatchString(ss[0]) { + // if "localhost", its valid + if strings.Compare("localhost", ss[0]) == 0 { + return true, host + } + // check against the regex + if validDNS.MatchString(ss[0]) { return true, host } else { - log.Fatal(fmt.Sprintf("DNS/IP: %s, Server Port: %s\n", ss[0], ss[1])) - return false, "" - } + log.Println(fmt.Sprintf("DNS/IP: %s, Server Port: %s", ss[0], ss[1])) + return false, "" + } } } return false, host @@ -93,7 +99,7 @@ func ReadConfigFile(folder string, fileName string) []byte { log.Fatal(err) } - // trim \r and \n if present - r := bytes.TrimSuffix(data[:count], []byte("\n")) - return bytes.TrimSuffix(r, []byte("\r")) + // trim \r and \n if present + r := bytes.TrimSuffix(data[:count], []byte("\n")) + return bytes.TrimSuffix(r, []byte("\r")) } diff --git a/d4-golang-utils_test.go b/d4-golang-utils_test.go new file mode 100644 index 0000000..69bb960 --- /dev/null +++ b/d4-golang-utils_test.go @@ -0,0 +1,38 @@ +package main + +import ( + "testing" + + config "github.com/D4-project/d4-golang-utils/config" +) + +var testCases = []struct { + name string + str string + expected bool +}{ + {"Well-formed IPv4 with port", "127.0.0.1:4443", true}, + {"Well-formed IPv4 without port", "127.0.0.1", false}, + {"Malformed IPv4 with port", "127..0.1:4443", false}, + {"Malformed IPv4 without port", "127..0.1", false}, + {"Well-formed IPv6 with port - 2", "[::1]:4443", true}, + {"Well-formed IPv6 without port", "[fe80::1%25en0]", false}, + {"Malformed IPv6 with port", "[::::1]:4443", false}, + {"Malformed IPv6 without port", "[::::::::1]", false}, + {"Malformed IPv6 : missing square brackets", "::::::::1:4443", false}, + {"Well-formed DNS name with port", "toto.circl.lu:4443", true}, + {"Well-formed DNS name without port", "toto.circl.lu", false}, + {"Malformed DNS name with port", ".:4443", false}, + {"Localhost with port", "localhost:4443", true}, +} + +func TestIsNet(t *testing.T) { + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + b, _ := config.IsNet(tc.str) + if b != tc.expected { + t.Fail() + } + }) + } +}