Endpoint parse to support also ipv6 format []:port 12/8912/1
authorJuha Hyttinen <juha.hyttinen@nokia.com>
Fri, 19 Aug 2022 10:22:45 +0000 (13:22 +0300)
committerJuha Hyttinen <juha.hyttinen@nokia.com>
Fri, 19 Aug 2022 10:25:23 +0000 (13:25 +0300)
Change-Id: Ie022a954e7fd9f3e853650041941045f108abe4e
Signed-off-by: Juha Hyttinen <juha.hyttinen@nokia.com>
pkg/xapp/alarm.go
pkg/xapp/rmrendpoint.go
pkg/xapp/rmrendpointlist_test.go

index 0e6f637..f6616a5 100755 (executable)
@@ -20,8 +20,9 @@
 package xapp
 
 import (
-       "gerrit.o-ran-sc.org/r/ric-plt/alarm-go.git/alarm"
        "os"
+
+       "gerrit.o-ran-sc.org/r/ric-plt/alarm-go.git/alarm"
 )
 
 type AlarmClient struct {
index cddcc4a..45f961d 100644 (file)
@@ -53,16 +53,18 @@ func (endpoint *RmrEndpoint) GetPort() uint16 {
 }
 
 func (endpoint *RmrEndpoint) Set(src string) bool {
-       elems := strings.Split(src, ":")
-       if len(elems) == 2 {
-               srcAddr := elems[0]
-               srcPort, err := strconv.ParseUint(elems[1], 10, 16)
+       if strings.Contains(src, ":") {
+               lind := strings.LastIndexByte(src, ':')
+               srcAddr := src[:lind]
+               srcPort, err := strconv.ParseUint(src[lind+1:], 10, 16)
                if err == nil {
                        endpoint.Addr = srcAddr
                        endpoint.Port = uint16(srcPort)
                        return true
                }
        }
+       endpoint.Addr = ""
+       endpoint.Port = 0
        return false
 }
 
index fe073e9..b85c1a6 100755 (executable)
 package xapp
 
 import (
+       "fmt"
        "testing"
 )
 
+func TestRmrEndpoint1(t *testing.T) {
+       addr := "127.0.0.1"
+       port := uint16(8080)
+       str := fmt.Sprintf("%s:%d", addr, port)
+       Logger.Info("CASE: TestRmrEndpoint1 %s", str)
+       ep := NewRmrEndpoint(str)
+       if ep == nil || ep.Addr != addr || ep.Port != port {
+               t.Errorf("NewRmrEndpoint: %s failed", str)
+       }
+}
+
+func TestRmrEndpoint2(t *testing.T) {
+       addr := "[2001:2003:fb69:ea00:c894:288b:4582:b5c/64]"
+       port := uint16(8080)
+       str := fmt.Sprintf("%s:%d", addr, port)
+       Logger.Info("CASE: TestRmrEndpoint2 %s", str)
+       ep := NewRmrEndpoint(str)
+       if ep == nil || ep.Addr != addr || ep.Port != port {
+               t.Errorf("NewRmrEndpoint: %s failed", str)
+       }
+}
+
+func TestRmrEndpoint3(t *testing.T) {
+       addr := "127.0.0.1"
+       str := fmt.Sprintf("%s:port", addr)
+       Logger.Info("CASE: TestRmrEndpoint3 %s", str)
+       ep := NewRmrEndpoint(str)
+       if ep != nil {
+               t.Errorf("NewRmrEndpoint: %s successful while should fail", str)
+       }
+}
+
+func TestRmrEndpoint4(t *testing.T) {
+       addr := "127.0.0.1"
+       str := fmt.Sprintf("%s:port", addr)
+       Logger.Info("CASE: TestRmrEndpoint4 %s", str)
+       ep := &RmrEndpoint{}
+       if ep.Set(str) == true {
+               t.Errorf("NewRmrEndpoint: Set %s successful while should fail", str)
+       }
+       if ep.Addr != "" || ep.Port != 0 {
+               t.Errorf("NewRmrEndpoint: Values %s successful while should fail", str)
+       }
+}
+
 func TestRmrEndpointList(t *testing.T) {
        Logger.Info("CASE: TestRmrEndpointList")