diff --git a/ie/flow-information.go b/ie/flow-information.go index cad599e8..21c4ce0e 100644 --- a/ie/flow-information.go +++ b/ie/flow-information.go @@ -95,7 +95,7 @@ func (i *IE) FlowDescription() (string, error) { return "", io.ErrUnexpectedEOF } - return string(i.Payload[4:l]), nil + return string(i.Payload[3 : 3+l]), nil case ApplicationDetectionInformation: ies, err := i.ApplicationDetectionInformation() if err != nil { diff --git a/ie/forwarding-policy.go b/ie/forwarding-policy.go index c71ce157..45032054 100644 --- a/ie/forwarding-policy.go +++ b/ie/forwarding-policy.go @@ -88,5 +88,5 @@ func (i *IE) ForwardingPolicyIdentifier() (string, error) { return "", io.ErrUnexpectedEOF } - return string(v[1:idlen]), nil + return string(v[1 : idlen+1]), nil } diff --git a/ie/ie_string_test.go b/ie/ie_string_test.go new file mode 100644 index 00000000..3a5a5540 --- /dev/null +++ b/ie/ie_string_test.go @@ -0,0 +1,121 @@ +// Copyright 2019-2020 go-pfcp authors. All rights reserved. +// Use of this source code is governed by a MIT-style license that can be +// found in the LICENSE file. + +package ie_test + +import ( + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/wmnsk/go-pfcp/ie" +) + +func TestStringIEs(t *testing.T) { + cases := []struct { + description string + structured *ie.IE + decoded string + decoderFunc func(*ie.IE) (string, error) + }{ + { + description: "ActivatePredefinedRules", + structured: ie.NewActivatePredefinedRules("go-pfcp"), + decoded: "go-pfcp", + decoderFunc: func(i *ie.IE) (string, error) { return i.ActivatePredefinedRules() }, + }, { + description: "APNDNN", + structured: ie.NewAPNDNN("some.apn.example"), + decoded: "some.apn.example", + decoderFunc: func(i *ie.IE) (string, error) { return i.APNDNN() }, + }, { + description: "ApplicationID", + structured: ie.NewApplicationID("https://github.com/wmnsk/go-pfcp/"), + decoded: "https://github.com/wmnsk/go-pfcp/", + decoderFunc: func(i *ie.IE) (string, error) { return i.ApplicationID() }, + }, { + description: "ApplicationInstanceID", + structured: ie.NewApplicationInstanceID("go-pfcp"), + decoded: "go-pfcp", + decoderFunc: func(i *ie.IE) (string, error) { return i.ApplicationInstanceID() }, + }, { + description: "DataNetworkAccessIdentifier", + structured: ie.NewDataNetworkAccessIdentifier("go-pfcp"), + decoded: "go-pfcp", + decoderFunc: func(i *ie.IE) (string, error) { return i.DataNetworkAccessIdentifier() }, + }, { + description: "DeactivatePredefinedRules", + structured: ie.NewDeactivatePredefinedRules("go-pfcp"), + decoded: "go-pfcp", + decoderFunc: func(i *ie.IE) (string, error) { return i.DeactivatePredefinedRules() }, + }, { + description: "FlowInformation/FlowDescription", + structured: ie.NewFlowInformation(ie.FlowDirectionDownlink, "go-pfcp"), + decoded: "go-pfcp", + decoderFunc: func(i *ie.IE) (string, error) { return i.FlowDescription() }, + }, { + description: "ForwardingPolicyIdentifier", + structured: ie.NewForwardingPolicy("go-pfcp"), + decoded: "go-pfcp", + decoderFunc: func(i *ie.IE) (string, error) { return i.ForwardingPolicyIdentifier() }, + }, { + description: "FramedIPv6Route", + structured: ie.NewFramedIPv6Route("go-pfcp"), + decoded: "go-pfcp", + decoderFunc: func(i *ie.IE) (string, error) { return i.FramedIPv6Route() }, + }, { + description: "FramedRoute", + structured: ie.NewFramedRoute("go-pfcp"), + decoded: "go-pfcp", + decoderFunc: func(i *ie.IE) (string, error) { return i.FramedRoute() }, + }, { + description: "NetworkInstance", + structured: ie.NewNetworkInstance("go-pfcp"), + decoded: "go-pfcp", + decoderFunc: func(i *ie.IE) (string, error) { return i.NetworkInstance() }, + }, { + description: "NodeID/IPv4", + structured: ie.NewNodeID("127.0.0.1", "", ""), + decoded: "127.0.0.1", + decoderFunc: func(i *ie.IE) (string, error) { return i.NodeID() }, + }, { + description: "NodeID/IPv6", + structured: ie.NewNodeID("", "2001::1", ""), + decoded: "2001::1", + decoderFunc: func(i *ie.IE) (string, error) { return i.NodeID() }, + }, { + description: "NodeID/FQDN", + structured: ie.NewNodeID("", "", "go-pfcp.epc.3gppnetwork.org"), + decoded: "go-pfcp.epc.3gppnetwork.org", + decoderFunc: func(i *ie.IE) (string, error) { return i.NodeID() }, + }, { + description: "PortManagementInformationContainer", + structured: ie.NewPortManagementInformationContainer("go-pfcp"), + decoded: "go-pfcp", + decoderFunc: func(i *ie.IE) (string, error) { return i.PortManagementInformationContainer() }, + }, { + description: "SMFSetID", + structured: ie.NewSMFSetID("go-pfcp"), + decoded: "go-pfcp", + decoderFunc: func(i *ie.IE) (string, error) { return i.SMFSetIDString() }, + }, { + description: "UEIPAddressPoolIdentity", + structured: ie.NewUEIPAddressPoolIdentity("go-pfcp"), + decoded: "go-pfcp", + decoderFunc: func(i *ie.IE) (string, error) { return i.UEIPAddressPoolIdentityString() }, + }, + } + + for _, c := range cases { + t.Run(c.description, func(t *testing.T) { + got, err := c.decoderFunc(c.structured) + if err != nil { + t.Fatal(err) + } + + if diff := cmp.Diff(got, c.decoded); diff != "" { + t.Error(diff) + } + }) + } +} diff --git a/ie/node-id.go b/ie/node-id.go index 66245df8..b2026d17 100644 --- a/ie/node-id.go +++ b/ie/node-id.go @@ -67,7 +67,25 @@ func (i *IE) NodeID() (string, error) { case NodeIDIPv6Address: return net.IP(i.Payload[1:]).To16().String(), nil case NodeIDFQDN: - return string(i.Payload[1:]), nil + b := i.Payload[1:] + var ( + nodeID []string + offset int + ) + max := len(b) + for { + if offset >= max { + break + } + l := int(b[offset]) + if offset+l+1 > max { + break + } + nodeID = append(nodeID, string(b[offset+1:offset+l+1])) + offset += l + 1 + } + + return strings.Join(nodeID, "."), nil default: return "", &InvalidNodeIDError{ID: i.Payload[0]} } diff --git a/ie/smf-set-id.go b/ie/smf-set-id.go index 8dca4fdc..0a8e3e51 100644 --- a/ie/smf-set-id.go +++ b/ie/smf-set-id.go @@ -26,7 +26,7 @@ func (i *IE) SMFSetID() ([]byte, error) { return i.Payload, nil } -// SMFSetIDString returns SMFSetIDString in string if the type of IE matches. +// SMFSetIDString returns SMFSetID in string if the type of IE matches. func (i *IE) SMFSetIDString() (string, error) { v, err := i.SMFSetID() if err != nil { diff --git a/ie/ue-ip-address-pool-identity.go b/ie/ue-ip-address-pool-identity.go index 9bc6aa07..5c939947 100644 --- a/ie/ue-ip-address-pool-identity.go +++ b/ie/ue-ip-address-pool-identity.go @@ -53,7 +53,7 @@ func (i *IE) UEIPAddressPoolIdentity() ([]byte, error) { } } -// UEIPAddressPoolIdentityString returns UEIPAddressPoolIdentityString in string if the type of IE matches. +// UEIPAddressPoolIdentityString returns UEIPAddressPoolIdentity in string if the type of IE matches. func (i *IE) UEIPAddressPoolIdentityString() (string, error) { v, err := i.UEIPAddressPoolIdentity() if err != nil { @@ -65,5 +65,5 @@ func (i *IE) UEIPAddressPoolIdentityString() (string, error) { return "", io.ErrUnexpectedEOF } - return string(v[1:idlen]), nil + return string(v[1 : idlen+1]), nil }