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

test: add testcase for ParseFlowDesc and NewFlowDesc #50

Merged
merged 3 commits into from
May 11, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions internal/forwarder/flowdesc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,23 @@ func TestParseFlowDesc(t *testing.T) {
},
},
},
{
name: "any to assign",
s: "permit out ip from any to assigned",
fd: FlowDesc{
Action: "permit",
Dir: "out",
Proto: 0xff,
Src: &net.IPNet{
IP: net.IPv6zero,
Mask: net.CIDRMask(0, 128),
},
Dst: &net.IPNet{
IP: net.IPv6zero,
Mask: net.CIDRMask(0, 128),
},
},
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
Expand Down
189 changes: 189 additions & 0 deletions internal/forwarder/gtp5g_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import (
"testing"
"time"

"github.com/khirono/go-nl"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/wmnsk/go-pfcp/ie"

"github.com/free5gc/go-gtp5gnl"
"github.com/free5gc/go-upf/internal/report"
"github.com/free5gc/go-upf/pkg/factory"
)
Expand Down Expand Up @@ -278,3 +281,189 @@ func TestGtp5g_CreateRules(t *testing.T) {
g.log.Infof("Receive final reports from URR(%d)", rs[0].URRID)
})
}

func TestNewFlowDesc(t *testing.T) {
if testing.Short() {
t.Skip("skipping testing in short mode")
}

var wg sync.WaitGroup
g, err := OpenGtp5g(&wg, ":"+strconv.Itoa(factory.UpfGtpDefaultPort), 1400)
if err != nil {
t.Fatal(err)
}
defer func() {
g.Close()
wg.Wait()
}()

cases := []struct {
name string
s string
swapSrcDst bool
attrs nl.AttrList
err error
}{
{
name: "permit out any to assigned",
s: "permit out ip from any to assigned",
swapSrcDst: false,
attrs: nl.AttrList{
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_ACTION,
Value: nl.AttrU8(gtp5gnl.SDF_FILTER_PERMIT),
},
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_DIRECTION,
Value: nl.AttrU8(gtp5gnl.SDF_FILTER_OUT),
},
},
err: nil,
},
{
name: "network addr (UL)",
s: "permit out ip from 10.20.30.40/24 to 50.60.70.80/16",
swapSrcDst: false,
attrs: nl.AttrList{
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_ACTION,
Value: nl.AttrU8(gtp5gnl.SDF_FILTER_PERMIT),
},
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_DIRECTION,
Value: nl.AttrU8(gtp5gnl.SDF_FILTER_OUT),
},
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_SRC_IPV4,
Value: nl.AttrBytes(net.IPv4(10, 20, 30, 0).To4()),
},
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_DEST_IPV4,
Value: nl.AttrBytes(net.IPv4(50, 60, 0, 0).To4()),
},
},
err: nil,
},
{
name: "network addr (DL)",
s: "permit out ip from 10.20.30.40/24 to 50.60.70.80/16",
swapSrcDst: true,
attrs: nl.AttrList{
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_ACTION,
Value: nl.AttrU8(gtp5gnl.SDF_FILTER_PERMIT),
},
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_DIRECTION,
Value: nl.AttrU8(gtp5gnl.SDF_FILTER_OUT),
},
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_SRC_IPV4,
Value: nl.AttrBytes(net.IPv4(50, 60, 0, 0).To4()),
},
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_DEST_IPV4,
Value: nl.AttrBytes(net.IPv4(10, 20, 30, 0).To4()),
},
},
err: nil,
},
{
name: "source port (DL)",
s: "permit out ip from 10.20.30.40/24 345,789-792,1023-1026 to 50.60.70.80/16 456-458,1088,1089",
swapSrcDst: false,
attrs: nl.AttrList{
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_ACTION,
Value: nl.AttrU8(gtp5gnl.SDF_FILTER_PERMIT),
},
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_DIRECTION,
Value: nl.AttrU8(gtp5gnl.SDF_FILTER_OUT),
},
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_SRC_IPV4,
Value: nl.AttrBytes(net.IPv4(10, 20, 30, 0).To4()),
},
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_DEST_IPV4,
Value: nl.AttrBytes(net.IPv4(50, 60, 0, 0).To4()),
},
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_SRC_PORT,
Value: nl.AttrBytes(convertSlice([][]uint16{
{345},
{789, 792},
{1023, 1026},
})),
},
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_DEST_PORT,
Value: nl.AttrBytes(convertSlice([][]uint16{
{456, 458},
{1088},
{1089},
})),
},
},
err: nil,
},
{
name: "source port (UL)",
s: "permit out ip from 10.20.30.40/24 345,789-792,1023-1026 to 50.60.70.80/16 456-458,1088,1089",
swapSrcDst: true,
attrs: nl.AttrList{
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_ACTION,
Value: nl.AttrU8(gtp5gnl.SDF_FILTER_PERMIT),
},
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_DIRECTION,
Value: nl.AttrU8(gtp5gnl.SDF_FILTER_OUT),
},
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_SRC_IPV4,
Value: nl.AttrBytes(net.IPv4(50, 60, 0, 0).To4()),
},
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_DEST_IPV4,
Value: nl.AttrBytes(net.IPv4(10, 20, 30, 0).To4()),
},
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_SRC_PORT,
Value: nl.AttrBytes(convertSlice([][]uint16{
{456, 458},
{1088},
{1089},
})),
},
nl.Attr{
Type: gtp5gnl.FLOW_DESCRIPTION_DEST_PORT,
Value: nl.AttrBytes(convertSlice([][]uint16{
{345},
{789, 792},
{1023, 1026},
})),
},
},
err: nil,
},
}

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
attrs, err := g.newFlowDesc(tt.s, tt.swapSrcDst)
if tt.err == nil {
if err != nil {
t.Fatal(err)
}
assert.Subset(t, attrs, tt.attrs)
} else if err != tt.err {
t.Errorf("wantErr %v; but got %v", tt.err, err)
}
})
}
}

// TODO
// Test on newSdfFilter()
Comment on lines +468 to +469
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I leave this to TODO since it's more complicated.
The others are ready for review.
@ianchen0119

Loading