Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IE helper methods that return values in string #60

Merged
merged 6 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion ie/flow-information.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion ie/forwarding-policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
121 changes: 121 additions & 0 deletions ie/ie_string_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
20 changes: 19 additions & 1 deletion ie/node-id.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]}
}
Expand Down
2 changes: 1 addition & 1 deletion ie/smf-set-id.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions ie/ue-ip-address-pool-identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}