From 61c7743d553f24656b5fe210bd12531865b1cc07 Mon Sep 17 00:00:00 2001 From: ian60509 Date: Tue, 4 Jun 2024 06:19:53 +0000 Subject: [PATCH 1/9] test: build test --- internal/pfcp/message/build_test.go | 115 ++++++++++++++++++++++++++++ internal/pfcp/message/send_test.go | 1 + 2 files changed, 116 insertions(+) create mode 100644 internal/pfcp/message/build_test.go diff --git a/internal/pfcp/message/build_test.go b/internal/pfcp/message/build_test.go new file mode 100644 index 00000000..6896fec0 --- /dev/null +++ b/internal/pfcp/message/build_test.go @@ -0,0 +1,115 @@ +package message_test + +import ( + "net" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/free5gc/smf/internal/pfcp/message" + "github.com/free5gc/smf/internal/pfcp/udp" + "github.com/free5gc/pfcp/pfcpType" +) + +func TestBuildPfcpAssociationSetupRequest(t *testing.T) { + emptyReq, err := message.BuildPfcpAssociationSetupRequest() + if err != nil { + t.Errorf("TestBuildPfcpAssociationSetupRequest failed: %v", err) + } + + // BuildPfcpAssociationSetupRequest buila a empty template of pfcp.PFCPAssociationSetupRequest + assert.Equal(t, uint8(0), emptyReq.NodeID.NodeIdType) + assert.Equal(t, net.IP(nil), emptyReq.NodeID.IP) + assert.Equal(t, "", emptyReq.NodeID.FQDN) + + assert.Equal(t, + udp.ServerStartTime, + emptyReq.RecoveryTimeStamp.RecoveryTimeStamp) + assert.Nil(t, + emptyReq.UPFunctionFeatures) + assert.Equal(t, + pfcpType.CPFunctionFeatures{SupportedFeatures: 0}, + *emptyReq.CPFunctionFeatures) +} + +func TestBuildPfcpAssociationSetupResponse(t *testing.T) { + cause := pfcpType.Cause{CauseValue: pfcpType.CauseRequestAccepted} + rsp, err := message.BuildPfcpAssociationSetupResponse(cause) + + if err != nil { + t.Errorf("TestBuildPfcpAssociationSetupResponse failed: %v", err) + } + + assert.Equal(t, uint8(0), rsp.NodeID.NodeIdType) + assert.Equal(t, cause, *rsp.Cause) + + assert.Nil(t, + rsp.UPFunctionFeatures) + assert.Equal(t, + pfcpType.CPFunctionFeatures{SupportedFeatures: 0}, + *rsp.CPFunctionFeatures) +} + +func TestBuildPfcpAssociationReleaseRequest(t *testing.T) { + emptyReq, err := message.BuildPfcpAssociationReleaseRequest() + if err != nil { + t.Errorf("TestBuildPfcpAssociationReleaseRequest failed: %v", err) + } + + assert.Equal(t, uint8(0), emptyReq.NodeID.NodeIdType) +} + +func TestBuildPfcpAssociationReleaseResponse(t *testing.T) { + cause := pfcpType.Cause{CauseValue: pfcpType.CauseRequestAccepted} + rsp, err := message.BuildPfcpAssociationReleaseResponse(cause) + + if err != nil { + t.Errorf("TestBuildPfcpAssociationReleaseResponse failed: %v", err) + } + + assert.Equal(t, uint8(0), rsp.NodeID.NodeIdType) + assert.Equal(t, cause, *rsp.Cause) +} + +func TestBuildPfcpSessionEstablishmentRequest(t *testing.T) { + +} + +func TestBuildPfcpSessionEstablishmentResponse(t *testing.T) { + +} + +func TestBuildPfcpSessionModificationRequest(t *testing.T) { + +} + +func TestBuildPfcpSessionModificationResponse(t *testing.T) { + +} + + +func TestBuildPfcpSessionDeletionResponse(t *testing.T) { + _, err := message.BuildPfcpSessionDeletionResponse() + if err != nil { + t.Errorf("TestBuildPfcpSessionDeletionResponse failed: %v", err) + } + +} + +func TestBuildPfcpSessionReportResponse(t *testing.T) { + cause := pfcpType.Cause{CauseValue: pfcpType.CauseRequestAccepted} + rsp, err := message.BuildPfcpSessionReportResponse(cause) + if err != nil { + t.Errorf("TestBuildPfcpSessionReportResponse failed: %v", err) + } + assert.Equal(t, cause, *rsp.Cause) +} + +func TestBuildPfcpHeartbeatRequest(t *testing.T) { + rsq, err := message.BuildPfcpHeartbeatRequest() + if err != nil { + t.Errorf("TestBuildPfcpHeartbeatRequest failed: %v", err) + } + + assert.Equal(t, udp.ServerStartTime, rsq.RecoveryTimeStamp.RecoveryTimeStamp) +} diff --git a/internal/pfcp/message/send_test.go b/internal/pfcp/message/send_test.go index a114d0c3..9d263b9f 100644 --- a/internal/pfcp/message/send_test.go +++ b/internal/pfcp/message/send_test.go @@ -15,6 +15,7 @@ import ( ) func TestSendPfcpAssociationSetupRequest(t *testing.T) { + } func TestSendPfcpSessionEstablishmentResponse(t *testing.T) { From 44ed80c5541b85738f25f3fa44f793f08b30a4ea Mon Sep 17 00:00:00 2001 From: ming-hsien Date: Fri, 7 Jun 2024 17:02:22 +0000 Subject: [PATCH 2/9] feat: test file --- go.mod | 2 +- internal/pfcp/handler/handler_test.go | 39 ++++++- internal/pfcp/message/build_test.go | 140 +++++++++++++++++++++++--- 3 files changed, 164 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index 659c5c7f..4fd98007 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/free5gc/smf -go 1.21 +go 1.21.0 require ( github.com/antihax/optional v1.0.0 diff --git a/internal/pfcp/handler/handler_test.go b/internal/pfcp/handler/handler_test.go index 0410ee18..4ef9b8ab 100644 --- a/internal/pfcp/handler/handler_test.go +++ b/internal/pfcp/handler/handler_test.go @@ -1,11 +1,46 @@ package handler_test import ( + // "net" "testing" + + // "github.com/free5gc/pfcp" + // "github.com/free5gc/pfcp/pfcpType" + // "github.com/free5gc/pfcp/pfcpUdp" + // "github.com/free5gc/smf/internal/logger" + // "github.com/free5gc/smf/internal/pfcp/handler" + // "github.com/stretchr/testify/assert" ) func TestHandlePfcpAssociationSetupRequest(t *testing.T) { -} + // remoteAddr := &net.UDPAddr{ + // IP: net.ParseIP("192.168.1.1"), + // Port: 12345, + // } -func TestHandlePfcpAssociationReleaseRequest(t *testing.T) { + // testPfcpReq := &pfcp.Message{ + // Header: pfcp.Header{ + // Version: 1, + // MP: 0, + // S: 0, + // MessageType: pfcp.PFCP_ASSOCIATION_SETUP_REQUEST, + // MessageLength: 9, + // SEID: 0, + // SequenceNumber: 1, + // MessagePriority: 0, + // }, + // Body: pfcp.PFCPAssociationSetupRequest{ + // NodeID: &pfcpType.NodeID{ + // NodeIdType: 0, + // IP: net.ParseIP("192.168.1.1").To4(), + // }, + // }, + // } + // msg := pfcpUdp.NewMessage(remoteAddr, testPfcpReq) + // mockLogger := &logger.MockLogger{} + // handler.HandlePfcpAssociationSetupRequest(msg) + // assert.Equal(t, true, mockLogger.ErrorfInvoked) } + +// func TestHandlePfcpAssociationReleaseRequest(t *testing.T) { +// } diff --git a/internal/pfcp/message/build_test.go b/internal/pfcp/message/build_test.go index 6896fec0..344f2a2e 100644 --- a/internal/pfcp/message/build_test.go +++ b/internal/pfcp/message/build_test.go @@ -4,13 +4,90 @@ import ( "net" "testing" - "github.com/stretchr/testify/assert" - + "github.com/free5gc/openapi/models" + "github.com/free5gc/pfcp" + "github.com/free5gc/pfcp/pfcpType" + "github.com/free5gc/smf/internal/context" "github.com/free5gc/smf/internal/pfcp/message" "github.com/free5gc/smf/internal/pfcp/udp" - "github.com/free5gc/pfcp/pfcpType" + "github.com/free5gc/smf/pkg/factory" + "github.com/stretchr/testify/assert" ) +var userPlaneConfig = factory.UserPlaneInformation{ + UPNodes: map[string]*factory.UPNode{ + "GNodeB": { + Type: "AN", + }, + "UPF1": { + Type: "UPF", + NodeID: "10.4.0.11", + Addr: "10.4.0.11", + SNssaiInfos: []*factory.SnssaiUpfInfoItem{ + { + SNssai: &models.Snssai{ + Sst: 1, + Sd: "010203", + }, + DnnUpfInfoList: []*factory.DnnUpfInfoItem{ + { + Dnn: "internet", + DnaiList: []string{"mec"}, + }, + }, + }, + }, + InterfaceUpfInfoList: []*factory.InterfaceUpfInfoItem{ + { + InterfaceType: "N3", + Endpoints: []string{ + "10.3.0.11", + }, + NetworkInstances: []string{"internet"}, + }, + { + InterfaceType: "N9", + Endpoints: []string{ + "10.3.0.11", + }, + NetworkInstances: []string{"internet"}, + }, + }, + }, + }, + Links: []*factory.UPLink{ + { + A: "GNodeB", + B: "UPF1", + }, + { + A: "UPF1", + B: "UPF2", + }, + }, +} + +var testConfig = factory.Config{ + Info: &factory.Info{ + Version: "1.0.0", + Description: "SMF procdeure test configuration", + }, + Configuration: &factory.Configuration{ + Sbi: &factory.Sbi{ + Scheme: "http", + RegisterIPv4: "127.0.0.1", + BindingIPv4: "127.0.0.1", + Port: 8000, + }, + UserPlaneInformation: userPlaneConfig, + }, +} + +func initConfig() { + context.InitSmfContext(&testConfig) + factory.SmfConfig = &testConfig +} + func TestBuildPfcpAssociationSetupRequest(t *testing.T) { emptyReq, err := message.BuildPfcpAssociationSetupRequest() if err != nil { @@ -22,13 +99,13 @@ func TestBuildPfcpAssociationSetupRequest(t *testing.T) { assert.Equal(t, net.IP(nil), emptyReq.NodeID.IP) assert.Equal(t, "", emptyReq.NodeID.FQDN) - assert.Equal(t, - udp.ServerStartTime, + assert.Equal(t, + udp.ServerStartTime, emptyReq.RecoveryTimeStamp.RecoveryTimeStamp) assert.Nil(t, emptyReq.UPFunctionFeatures) - assert.Equal(t, - pfcpType.CPFunctionFeatures{SupportedFeatures: 0}, + assert.Equal(t, + pfcpType.CPFunctionFeatures{SupportedFeatures: 0}, *emptyReq.CPFunctionFeatures) } @@ -45,8 +122,8 @@ func TestBuildPfcpAssociationSetupResponse(t *testing.T) { assert.Nil(t, rsp.UPFunctionFeatures) - assert.Equal(t, - pfcpType.CPFunctionFeatures{SupportedFeatures: 0}, + assert.Equal(t, + pfcpType.CPFunctionFeatures{SupportedFeatures: 0}, *rsp.CPFunctionFeatures) } @@ -72,11 +149,42 @@ func TestBuildPfcpAssociationReleaseResponse(t *testing.T) { } func TestBuildPfcpSessionEstablishmentRequest(t *testing.T) { - + initConfig() + var NodeID = pfcpType.NodeID{ + NodeIdType: 0, + IP: net.ParseIP("10.4.0.11").To4(), + } + smctx := context.NewSMContext("imsi-208930000000001", 10) + pdrList := make([]*context.PDR, 0, 2) + farList := make([]*context.FAR, 0, 2) + qerList := make([]*context.QER, 0, 2) + urrList := make([]*context.URR, 0, 2) + barList := make([]*context.BAR, 0, 2) + + smctx.PFCPContext["10.4.0.11"] = &context.PFCPSessionContext{} + + req, err := message.BuildPfcpSessionEstablishmentRequest(NodeID, "10.4.0.11", smctx, pdrList, farList, barList, qerList, urrList) + if err != nil { + t.Errorf("TestBuildPfcpSessionEstablishmentRequest failed: %v", err) + } + + createPDR := make([]*pfcp.CreatePDR, 0) + assert.Equal(t, uint8(0), req.NodeID.NodeIdType) + assert.NotNil(t, req.CPFSEID) + assert.NotNil(t, req.PDNType) + assert.Equal(t, createPDR, req.CreatePDR) } +// hsien func TestBuildPfcpSessionEstablishmentResponse(t *testing.T) { - + rsp, err := message.BuildPfcpSessionEstablishmentResponse() + if err != nil { + t.Errorf("TestBuildPfcpSessionEstablishmentResponse failed: %v", err) + } + assert.Equal(t, uint8(0), rsp.NodeID.NodeIdType) + assert.Equal(t, pfcpType.CauseRequestAccepted, rsp.Cause.CauseValue) + assert.NotNil(t, rsp.UPFSEID) + assert.NotNil(t, rsp.CreatedPDR) } func TestBuildPfcpSessionModificationRequest(t *testing.T) { @@ -84,16 +192,20 @@ func TestBuildPfcpSessionModificationRequest(t *testing.T) { } func TestBuildPfcpSessionModificationResponse(t *testing.T) { - + rsp, err := message.BuildPfcpSessionEstablishmentResponse() + if err != nil { + t.Errorf("BuildPfcpSessionModificationResponse failed: %v", err) + } + assert.Equal(t, pfcpType.CauseRequestAccepted, rsp.Cause.CauseValue) + assert.NotNil(t, rsp.OffendingIE) + assert.NotNil(t, rsp.CreatedPDR) } - func TestBuildPfcpSessionDeletionResponse(t *testing.T) { _, err := message.BuildPfcpSessionDeletionResponse() if err != nil { t.Errorf("TestBuildPfcpSessionDeletionResponse failed: %v", err) } - } func TestBuildPfcpSessionReportResponse(t *testing.T) { From 1a3afb731e44ba367a0f22f5932a479485615a84 Mon Sep 17 00:00:00 2001 From: ming-hsien Date: Sat, 8 Jun 2024 10:16:41 +0000 Subject: [PATCH 3/9] feat: testing file (pfcp build) --- internal/pfcp/message/build_test.go | 167 +++++++++++++++------------- 1 file changed, 90 insertions(+), 77 deletions(-) diff --git a/internal/pfcp/message/build_test.go b/internal/pfcp/message/build_test.go index 344f2a2e..c49d71e4 100644 --- a/internal/pfcp/message/build_test.go +++ b/internal/pfcp/message/build_test.go @@ -4,8 +4,6 @@ import ( "net" "testing" - "github.com/free5gc/openapi/models" - "github.com/free5gc/pfcp" "github.com/free5gc/pfcp/pfcpType" "github.com/free5gc/smf/internal/context" "github.com/free5gc/smf/internal/pfcp/message" @@ -14,59 +12,6 @@ import ( "github.com/stretchr/testify/assert" ) -var userPlaneConfig = factory.UserPlaneInformation{ - UPNodes: map[string]*factory.UPNode{ - "GNodeB": { - Type: "AN", - }, - "UPF1": { - Type: "UPF", - NodeID: "10.4.0.11", - Addr: "10.4.0.11", - SNssaiInfos: []*factory.SnssaiUpfInfoItem{ - { - SNssai: &models.Snssai{ - Sst: 1, - Sd: "010203", - }, - DnnUpfInfoList: []*factory.DnnUpfInfoItem{ - { - Dnn: "internet", - DnaiList: []string{"mec"}, - }, - }, - }, - }, - InterfaceUpfInfoList: []*factory.InterfaceUpfInfoItem{ - { - InterfaceType: "N3", - Endpoints: []string{ - "10.3.0.11", - }, - NetworkInstances: []string{"internet"}, - }, - { - InterfaceType: "N9", - Endpoints: []string{ - "10.3.0.11", - }, - NetworkInstances: []string{"internet"}, - }, - }, - }, - }, - Links: []*factory.UPLink{ - { - A: "GNodeB", - B: "UPF1", - }, - { - A: "UPF1", - B: "UPF2", - }, - }, -} - var testConfig = factory.Config{ Info: &factory.Info{ Version: "1.0.0", @@ -79,13 +24,71 @@ var testConfig = factory.Config{ BindingIPv4: "127.0.0.1", Port: 8000, }, - UserPlaneInformation: userPlaneConfig, }, } -func initConfig() { +var testNodeID = pfcpType.NodeID{ + NodeIdType: 0, + IP: net.ParseIP("10.4.0.1").To4(), +} + +func initSmfContext() { context.InitSmfContext(&testConfig) - factory.SmfConfig = &testConfig +} + +func initRuleList() ([]*context.PDR, []*context.FAR, []*context.BAR, + []*context.QER, []*context.URR, +) { + var testPDR = &context.PDR{ + PDRID: uint16(1), + State: context.RULE_INITIAL, + OuterHeaderRemoval: &pfcpType.OuterHeaderRemoval{ + OuterHeaderRemovalDescription: (1), + }, + FAR: &context.FAR{}, + URR: []*context.URR{}, + QER: []*context.QER{}, + } + + var testFAR = &context.FAR{ + FARID: uint32(123), + // State Can be RULE_INITIAL or RULE_UPDATE or RULE_REMOVE + State: context.RULE_INITIAL, + ApplyAction: pfcpType.ApplyAction{ + Forw: true, + }, + ForwardingParameters: &context.ForwardingParameters{}, + BAR: &context.BAR{}, + } + + var testBAR = &context.BAR{ + BARID: uint8(124), + // State Can be RULE_INITIAL or RULE_UPDATE or RULE_REMOVE + State: context.RULE_INITIAL, + } + + var testQER = &context.QER{ + QERID: uint32(123), + // State Can be RULE_INITIAL or RULE_UPDATE or RULE_REMOVE + State: context.RULE_INITIAL, + } + + var testURR = &context.URR{ + URRID: uint32(123), + // State Can be RULE_INITIAL or RULE_UPDATE or RULE_REMOVE + State: context.RULE_INITIAL, + } + pdrList := make([]*context.PDR, 0) + farList := make([]*context.FAR, 0) + barList := make([]*context.BAR, 0) + qerList := make([]*context.QER, 0) + urrList := make([]*context.URR, 0) + pdrList = append(pdrList, testPDR) + farList = append(farList, testFAR) + barList = append(barList, testBAR) + qerList = append(qerList, testQER) + urrList = append(urrList, testURR) + return pdrList, farList, barList, qerList, urrList } func TestBuildPfcpAssociationSetupRequest(t *testing.T) { @@ -112,7 +115,6 @@ func TestBuildPfcpAssociationSetupRequest(t *testing.T) { func TestBuildPfcpAssociationSetupResponse(t *testing.T) { cause := pfcpType.Cause{CauseValue: pfcpType.CauseRequestAccepted} rsp, err := message.BuildPfcpAssociationSetupResponse(cause) - if err != nil { t.Errorf("TestBuildPfcpAssociationSetupResponse failed: %v", err) } @@ -139,7 +141,6 @@ func TestBuildPfcpAssociationReleaseRequest(t *testing.T) { func TestBuildPfcpAssociationReleaseResponse(t *testing.T) { cause := pfcpType.Cause{CauseValue: pfcpType.CauseRequestAccepted} rsp, err := message.BuildPfcpAssociationReleaseResponse(cause) - if err != nil { t.Errorf("TestBuildPfcpAssociationReleaseResponse failed: %v", err) } @@ -149,30 +150,24 @@ func TestBuildPfcpAssociationReleaseResponse(t *testing.T) { } func TestBuildPfcpSessionEstablishmentRequest(t *testing.T) { - initConfig() - var NodeID = pfcpType.NodeID{ - NodeIdType: 0, - IP: net.ParseIP("10.4.0.11").To4(), - } - smctx := context.NewSMContext("imsi-208930000000001", 10) - pdrList := make([]*context.PDR, 0, 2) - farList := make([]*context.FAR, 0, 2) - qerList := make([]*context.QER, 0, 2) - urrList := make([]*context.URR, 0, 2) - barList := make([]*context.BAR, 0, 2) - - smctx.PFCPContext["10.4.0.11"] = &context.PFCPSessionContext{} + initSmfContext() + var smctx = context.NewSMContext("imsi-208930000000001", 10) + pdrList, farList, barList, qerList, urrList := initRuleList() + smctx.PFCPContext["10.4.0.1"] = &context.PFCPSessionContext{} - req, err := message.BuildPfcpSessionEstablishmentRequest(NodeID, "10.4.0.11", smctx, pdrList, farList, barList, qerList, urrList) + req, err := message.BuildPfcpSessionEstablishmentRequest( + testNodeID, "10.4.0.1", smctx, pdrList, farList, barList, qerList, urrList) if err != nil { t.Errorf("TestBuildPfcpSessionEstablishmentRequest failed: %v", err) } - - createPDR := make([]*pfcp.CreatePDR, 0) assert.Equal(t, uint8(0), req.NodeID.NodeIdType) assert.NotNil(t, req.CPFSEID) assert.NotNil(t, req.PDNType) - assert.Equal(t, createPDR, req.CreatePDR) + assert.NotNil(t, req.CreatePDR) + assert.NotNil(t, req.CreateFAR) + assert.NotNil(t, req.CreateBAR) + assert.NotNil(t, req.CreateQER) + assert.NotNil(t, req.CreateURR) } // hsien @@ -188,7 +183,25 @@ func TestBuildPfcpSessionEstablishmentResponse(t *testing.T) { } func TestBuildPfcpSessionModificationRequest(t *testing.T) { + initSmfContext() + var smctx = context.NewSMContext("imsi-208930000000001", 10) + pdrList, farList, barList, qerList, urrList := initRuleList() + smctx.PFCPContext["10.4.0.1"] = &context.PFCPSessionContext{} + + req, err := message.BuildPfcpSessionModificationRequest( + testNodeID, "10.4.0.1", smctx, pdrList, farList, barList, qerList, urrList) + if err != nil { + t.Errorf("TestBuildPfcpSessionModificationRequest failed: %v", err) + } + assert.Equal(t, context.RULE_CREATE, pdrList[0].State) + + assert.NotNil(t, req.CPFSEID) + assert.NotNil(t, req.CreateBAR) + assert.NotNil(t, req.CreateFAR) + assert.NotNil(t, req.CreateBAR) + assert.NotNil(t, req.CreateQER) + assert.NotNil(t, req.CreateURR) } func TestBuildPfcpSessionModificationResponse(t *testing.T) { From 1b252cb4d0140fbaef03e095f73f130506247642 Mon Sep 17 00:00:00 2001 From: ming-hsien Date: Mon, 10 Jun 2024 10:18:10 +0000 Subject: [PATCH 4/9] feat: Add pfcp build test file --- internal/pfcp/handler/handler_test.go | 188 +++++++++++++++++++++----- 1 file changed, 154 insertions(+), 34 deletions(-) diff --git a/internal/pfcp/handler/handler_test.go b/internal/pfcp/handler/handler_test.go index 4ef9b8ab..333e7ef5 100644 --- a/internal/pfcp/handler/handler_test.go +++ b/internal/pfcp/handler/handler_test.go @@ -1,45 +1,165 @@ package handler_test import ( - // "net" + "bytes" + "fmt" + "io" + "net" + "regexp" "testing" - // "github.com/free5gc/pfcp" - // "github.com/free5gc/pfcp/pfcpType" - // "github.com/free5gc/pfcp/pfcpUdp" - // "github.com/free5gc/smf/internal/logger" - // "github.com/free5gc/smf/internal/pfcp/handler" - // "github.com/stretchr/testify/assert" + "github.com/free5gc/pfcp" + "github.com/sirupsen/logrus" + + "github.com/free5gc/pfcp/pfcpType" + "github.com/free5gc/pfcp/pfcpUdp" + + "github.com/free5gc/smf/internal/logger" + "github.com/free5gc/smf/internal/pfcp/handler" + . "github.com/smartystreets/goconvey/convey" ) +type LogCapture struct { + buffer bytes.Buffer +} + +func (lc *LogCapture) Write(p []byte) (n int, err error) { + return lc.buffer.Write(p) +} + +func (lc *LogCapture) String() string { + return lc.buffer.String() +} + +// func TestHandlePfcpHeartbeatRequest(t *testing.T) { +// } + +func TestHandlePfcpManagementRequest(t *testing.T) { + re := regexp.MustCompile(`(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{9}Z)(.*)`) + Convey("Test log message", t, func() { + remoteAddr := &net.UDPAddr{} + testPfcpReq := &pfcp.Message{} + msg := pfcpUdp.NewMessage(remoteAddr, testPfcpReq) + logCapture := &LogCapture{} + logger.Log.SetOutput(io.MultiWriter(logCapture, logrus.StandardLogger().Out)) + handler.HandlePfcpPfdManagementRequest(msg) + capturedLogs := re.FindStringSubmatch(logCapture.String()) + + logCaptureExp := &LogCapture{} + logger.Log.SetOutput(io.MultiWriter(logCaptureExp)) + logger.PfcpLog.Warnf("PFCP PFD Management Request handling is not implemented") + capturedLogsExp := re.FindStringSubmatch(logCaptureExp.String()) + So(capturedLogs[2], ShouldEqual, capturedLogsExp[2]) + }) +} + func TestHandlePfcpAssociationSetupRequest(t *testing.T) { - // remoteAddr := &net.UDPAddr{ - // IP: net.ParseIP("192.168.1.1"), - // Port: 12345, - // } - - // testPfcpReq := &pfcp.Message{ - // Header: pfcp.Header{ - // Version: 1, - // MP: 0, - // S: 0, - // MessageType: pfcp.PFCP_ASSOCIATION_SETUP_REQUEST, - // MessageLength: 9, - // SEID: 0, - // SequenceNumber: 1, - // MessagePriority: 0, - // }, - // Body: pfcp.PFCPAssociationSetupRequest{ - // NodeID: &pfcpType.NodeID{ - // NodeIdType: 0, - // IP: net.ParseIP("192.168.1.1").To4(), - // }, - // }, - // } - // msg := pfcpUdp.NewMessage(remoteAddr, testPfcpReq) - // mockLogger := &logger.MockLogger{} - // handler.HandlePfcpAssociationSetupRequest(msg) - // assert.Equal(t, true, mockLogger.ErrorfInvoked) + re := regexp.MustCompile(`(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{9}Z)(.*)`) + re2 := regexp.MustCompile(`(.*)\n(.*)`) + Convey("Test if NodeID is Nil", t, func() { + remoteAddr := &net.UDPAddr{ + IP: net.ParseIP("192.168.1.1"), + Port: 12345, + } + + testPfcpReq := &pfcp.Message{ + Header: pfcp.Header{ + Version: 1, + MP: 0, + S: 0, + MessageType: pfcp.PFCP_ASSOCIATION_SETUP_REQUEST, + MessageLength: 9, + SEID: 0, + SequenceNumber: 1, + MessagePriority: 0, + }, + Body: pfcp.PFCPAssociationSetupRequest{ + NodeID: nil, + }, + } + + logCapture := &LogCapture{} + logger.Log.SetOutput(io.MultiWriter(logCapture, logrus.StandardLogger().Out)) + + msg := pfcpUdp.NewMessage(remoteAddr, testPfcpReq) + handler.HandlePfcpAssociationSetupRequest(msg) + capturedLogs := re.FindStringSubmatch(logCapture.String()) + + logCaptureExp := &LogCapture{} + logger.Log.SetOutput(io.MultiWriter(logCaptureExp)) + logger.PfcpLog.Errorln("pfcp association needs NodeID") + logger.PfcpLog.Infof("Handle PFCP Association Setup Request with NodeID") + ExpLogs := re.FindStringSubmatch(logCaptureExp.String()) + fmt.Println(ExpLogs) + if len(capturedLogs) <= 2 || len(ExpLogs) <= 2 { + t.Errorf("The extracted log is not as expected.") + } + So(capturedLogs[2], ShouldEqual, ExpLogs[2]) + }) + Convey("Test if NodeID is NotNil, upf is Nil", t, func() { + remoteAddr := &net.UDPAddr{ + IP: net.ParseIP("192.168.1.1"), + Port: 12345, + } + + testPfcpReq := &pfcp.Message{ + Header: pfcp.Header{ + Version: 1, + MP: 0, + S: 0, + MessageType: pfcp.PFCP_ASSOCIATION_SETUP_REQUEST, + MessageLength: 9, + SEID: 0, + SequenceNumber: 1, + MessagePriority: 0, + }, + Body: pfcp.PFCPAssociationSetupRequest{ + NodeID: &pfcpType.NodeID{ + NodeIdType: pfcpType.NodeIdTypeIpv4Address, + IP: net.ParseIP("192.168.1.1").To4(), + }, + }, + } + + logCapture := &LogCapture{} + logger.Log.SetOutput(io.MultiWriter(logCapture, logrus.StandardLogger().Out)) + + msg := pfcpUdp.NewMessage(remoteAddr, testPfcpReq) + handler.HandlePfcpAssociationSetupRequest(msg) + capturedLogs := re.FindStringSubmatch(re2.FindStringSubmatch(logCapture.String())[1]) + + logCaptureExp := &LogCapture{} + logger.Log.SetOutput(io.MultiWriter(logCaptureExp)) + logger.PfcpLog.Infof("Handle PFCP Association Setup Request with NodeID[%s]", "192.168.1.1") + logger.PfcpLog.Errorf("can't find UPF[%s]", "192.168.1.1") + ExpLogs := re.FindStringSubmatch(re2.FindStringSubmatch(logCaptureExp.String())[1]) + if len(capturedLogs) <= 2 || len(ExpLogs) <= 2 { + t.Errorf("The extracted log is not as expected.") + } + So(capturedLogs[2], ShouldEqual, ExpLogs[2]) + capturedLogs = re.FindStringSubmatch(re2.FindStringSubmatch(logCapture.String())[2]) + ExpLogs = re.FindStringSubmatch(re2.FindStringSubmatch(logCaptureExp.String())[2]) + So(capturedLogs[2], ShouldEqual, ExpLogs[2]) + }) +} + +func TestHandlePfcpAssociationUpdateRequest(t *testing.T) { + re := regexp.MustCompile(`(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{9}Z)(.*)`) + Convey("Test logger message", t, func() { + remoteAddr := &net.UDPAddr{} + testPfcpReq := &pfcp.Message{} + msg := pfcpUdp.NewMessage(remoteAddr, testPfcpReq) + logCapture := &LogCapture{} + logger.Log.SetOutput(io.MultiWriter(logCapture, logrus.StandardLogger().Out)) + handler.HandlePfcpAssociationUpdateRequest(msg) + capturedLogs := re.FindStringSubmatch(logCapture.String()) + + logCaptureExp := &LogCapture{} + logger.Log.SetOutput(io.MultiWriter(logCaptureExp)) + logger.PfcpLog.Warnf("PFCP Association Update Request handling is not implemented") + capturedLogsExp := re.FindStringSubmatch(logCaptureExp.String()) + So(capturedLogs[2], ShouldEqual, capturedLogsExp[2]) + }) } // func TestHandlePfcpAssociationReleaseRequest(t *testing.T) { From 50b6807458d1cae3e49c30a67ada92e31c0228d4 Mon Sep 17 00:00:00 2001 From: ming-hsien Date: Wed, 12 Jun 2024 05:22:11 +0000 Subject: [PATCH 5/9] fix: build_test --- .gitignore | 3 ++ go.mod | 1 + go.sum | 2 + internal/pfcp/message/build_test.go | 65 ++++++++++++++++++++--------- 4 files changed, 52 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index c294e4dc..0fe62874 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,6 @@ cscope.* # Debug *.log *.pcap + +tttt_test.go +tttt.go \ No newline at end of file diff --git a/go.mod b/go.mod index 4fd98007..96b92951 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/free5gc/smf go 1.21.0 require ( + github.com/agiledragon/gomonkey/v2 v2.11.0 github.com/antihax/optional v1.0.0 github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d github.com/davecgh/go-spew v1.1.1 diff --git a/go.sum b/go.sum index b05b6d44..cdda9680 100644 --- a/go.sum +++ b/go.sum @@ -33,6 +33,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/agiledragon/gomonkey/v2 v2.11.0 h1:5oxSgA+tC1xuGsrIorR+sYiziYltmJyEZ9qA25b6l5U= +github.com/agiledragon/gomonkey/v2 v2.11.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY= github.com/antihax/optional v1.0.0 h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwcg= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/antonfisher/nested-logrus-formatter v1.3.1 h1:NFJIr+pzwv5QLHTPyKz9UMEoHck02Q9L0FP13b/xSbQ= diff --git a/internal/pfcp/message/build_test.go b/internal/pfcp/message/build_test.go index c49d71e4..faba23dd 100644 --- a/internal/pfcp/message/build_test.go +++ b/internal/pfcp/message/build_test.go @@ -24,11 +24,14 @@ var testConfig = factory.Config{ BindingIPv4: "127.0.0.1", Port: 8000, }, + PFCP: &factory.PFCP{ + NodeID: "10.4.0.1", + }, }, } -var testNodeID = pfcpType.NodeID{ - NodeIdType: 0, +var testNodeID = &pfcpType.NodeID{ + NodeIdType: pfcpType.NodeIdTypeIpv4Address, IP: net.ParseIP("10.4.0.1").To4(), } @@ -156,26 +159,45 @@ func TestBuildPfcpSessionEstablishmentRequest(t *testing.T) { smctx.PFCPContext["10.4.0.1"] = &context.PFCPSessionContext{} req, err := message.BuildPfcpSessionEstablishmentRequest( - testNodeID, "10.4.0.1", smctx, pdrList, farList, barList, qerList, urrList) + *testNodeID, "10.4.0.1", smctx, pdrList, farList, barList, qerList, urrList) if err != nil { t.Errorf("TestBuildPfcpSessionEstablishmentRequest failed: %v", err) } - assert.Equal(t, uint8(0), req.NodeID.NodeIdType) - assert.NotNil(t, req.CPFSEID) - assert.NotNil(t, req.PDNType) - assert.NotNil(t, req.CreatePDR) - assert.NotNil(t, req.CreateFAR) - assert.NotNil(t, req.CreateBAR) - assert.NotNil(t, req.CreateQER) - assert.NotNil(t, req.CreateURR) + // assert.Equal(t, uint8(0), req.NodeID.NodeIdType) + assert.Equal(t, testNodeID, req.NodeID) + assert.Equal(t, &pfcpType.PDNType{PdnType: pfcpType.PDNTypeIpv4}, req.PDNType) + assert.Equal(t, len(req.CreatePDR), 1) + assert.Equal(t, len(req.CreateFAR), 1) + assert.Equal(t, len(req.CreateBAR), 1) + assert.Equal(t, len(req.CreateQER), 1) + assert.Equal(t, len(req.CreateURR), 1) + assert.Equal(t, pdrList[0].State, context.RULE_CREATE) + assert.Equal(t, farList[0].State, context.RULE_CREATE) + assert.Equal(t, barList[0].State, context.RULE_CREATE) + assert.Equal(t, qerList[0].State, context.RULE_CREATE) + assert.Equal(t, urrList[0].State, context.RULE_CREATE) + + req2, err2 := message.BuildPfcpSessionEstablishmentRequest( + *testNodeID, "10.4.0.1", smctx, nil, nil, nil, nil, nil) + if err2 != nil { + t.Errorf("TestBuildPfcpSessionEstablishmentRequest failed: %v", err2) + } + assert.NotEqual(t, req2, req) + assert.Equal(t, len(req2.CreatePDR), 0) + assert.Equal(t, len(req2.CreateFAR), 0) + assert.Equal(t, len(req2.CreateBAR), 0) + assert.Equal(t, len(req2.CreateQER), 0) + assert.Equal(t, len(req2.CreateURR), 0) } // hsien func TestBuildPfcpSessionEstablishmentResponse(t *testing.T) { + initSmfContext() rsp, err := message.BuildPfcpSessionEstablishmentResponse() if err != nil { t.Errorf("TestBuildPfcpSessionEstablishmentResponse failed: %v", err) } + assert.Equal(t, rsp.NodeID, testNodeID) assert.Equal(t, uint8(0), rsp.NodeID.NodeIdType) assert.Equal(t, pfcpType.CauseRequestAccepted, rsp.Cause.CauseValue) assert.NotNil(t, rsp.UPFSEID) @@ -189,26 +211,31 @@ func TestBuildPfcpSessionModificationRequest(t *testing.T) { smctx.PFCPContext["10.4.0.1"] = &context.PFCPSessionContext{} req, err := message.BuildPfcpSessionModificationRequest( - testNodeID, "10.4.0.1", smctx, pdrList, farList, barList, qerList, urrList) + *testNodeID, "10.4.0.1", smctx, pdrList, farList, barList, qerList, urrList) if err != nil { t.Errorf("TestBuildPfcpSessionModificationRequest failed: %v", err) } assert.Equal(t, context.RULE_CREATE, pdrList[0].State) - - assert.NotNil(t, req.CPFSEID) - assert.NotNil(t, req.CreateBAR) - assert.NotNil(t, req.CreateFAR) - assert.NotNil(t, req.CreateBAR) - assert.NotNil(t, req.CreateQER) - assert.NotNil(t, req.CreateURR) + assert.Equal(t, context.RULE_CREATE, farList[0].State) + assert.Equal(t, context.RULE_INITIAL, barList[0].State) + assert.Equal(t, context.RULE_CREATE, qerList[0].State) + assert.Equal(t, context.RULE_CREATE, urrList[0].State) + + assert.Equal(t, len(req.CreatePDR), 1) + assert.Equal(t, len(req.CreateFAR), 1) + assert.Equal(t, len(req.CreateBAR), 1) + assert.Equal(t, len(req.CreateQER), 1) + assert.Equal(t, len(req.CreateURR), 1) } func TestBuildPfcpSessionModificationResponse(t *testing.T) { + initSmfContext() rsp, err := message.BuildPfcpSessionEstablishmentResponse() if err != nil { t.Errorf("BuildPfcpSessionModificationResponse failed: %v", err) } + assert.Equal(t, rsp.NodeID, testNodeID) assert.Equal(t, pfcpType.CauseRequestAccepted, rsp.Cause.CauseValue) assert.NotNil(t, rsp.OffendingIE) assert.NotNil(t, rsp.CreatedPDR) From fd718a941963ca6eb81bc41133217a42c2742299 Mon Sep 17 00:00:00 2001 From: chh Date: Wed, 12 Jun 2024 09:18:43 +0000 Subject: [PATCH 6/9] feat: udp send pfcp request test --- go.mod | 2 +- go.sum | 4 +-- internal/pfcp/udp/udp_test.go | 68 +++++++++++++++++++++++++++++++++-- 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 96b92951..df121ef1 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/free5gc/smf go 1.21.0 require ( - github.com/agiledragon/gomonkey/v2 v2.11.0 github.com/antihax/optional v1.0.0 github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d github.com/davecgh/go-spew v1.1.1 @@ -27,6 +26,7 @@ require ( ) require ( + github.com/agiledragon/gomonkey v2.0.2+incompatible github.com/antonfisher/nested-logrus-formatter v1.3.1 // indirect github.com/bytedance/sonic v1.9.1 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect diff --git a/go.sum b/go.sum index cdda9680..cf4b9d3e 100644 --- a/go.sum +++ b/go.sum @@ -33,8 +33,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= -github.com/agiledragon/gomonkey/v2 v2.11.0 h1:5oxSgA+tC1xuGsrIorR+sYiziYltmJyEZ9qA25b6l5U= -github.com/agiledragon/gomonkey/v2 v2.11.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY= +github.com/agiledragon/gomonkey v2.0.2+incompatible h1:eXKi9/piiC3cjJD1658mEE2o3NjkJ5vDLgYjCQu0Xlw= +github.com/agiledragon/gomonkey v2.0.2+incompatible/go.mod h1:2NGfXu1a80LLr2cmWXGBDaHEjb1idR6+FVlX5T3D9hw= github.com/antihax/optional v1.0.0 h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwcg= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/antonfisher/nested-logrus-formatter v1.3.1 h1:NFJIr+pzwv5QLHTPyKz9UMEoHck02Q9L0FP13b/xSbQ= diff --git a/internal/pfcp/udp/udp_test.go b/internal/pfcp/udp/udp_test.go index 07f084d5..4a971908 100644 --- a/internal/pfcp/udp/udp_test.go +++ b/internal/pfcp/udp/udp_test.go @@ -3,17 +3,20 @@ package udp_test import ( "context" "net" + "reflect" "testing" "time" - "github.com/stretchr/testify/require" - + "github.com/agiledragon/gomonkey" "github.com/free5gc/pfcp" "github.com/free5gc/pfcp/pfcpType" "github.com/free5gc/pfcp/pfcpUdp" smf_context "github.com/free5gc/smf/internal/context" smf_pfcp "github.com/free5gc/smf/internal/pfcp" "github.com/free5gc/smf/internal/pfcp/udp" + "github.com/free5gc/smf/pkg/factory" + . "github.com/smartystreets/goconvey/convey" + "github.com/stretchr/testify/require" ) const testPfcpClientPort = 12345 @@ -69,3 +72,64 @@ func TestRun(t *testing.T) { time.Sleep(300 * time.Millisecond) } + +var testConfig = factory.Config{ + Info: &factory.Info{ + Version: "1.0.0", + Description: "SMF procdeure test configuration", + }, + Configuration: &factory.Configuration{ + Sbi: &factory.Sbi{ + Scheme: "http", + RegisterIPv4: "127.0.0.1", + BindingIPv4: "127.0.0.1", + Port: 8000, + }, + }, +} + +var testNodeID = pfcpType.NodeID{ + NodeIdType: 0, + IP: net.ParseIP("127.0.0.3").To4(), +} + +func initSmfContext() { + context.InitSmfContext(&testConfig) +} + +func TestSendPfcpRequest(t *testing.T) { + //init smf context + initSmfContext() + context.GetSelf().CPNodeID = pfcpType.NodeID{ + NodeIdType: pfcpType.NodeIdTypeIpv4Address, + IP: net.ParseIP("127.0.0.1").To4(), + } + udp.Server = pfcpUdp.NewPfcpServer(net.ParseIP("127.0.0.1").To4().String()) + + //build message + pfcpMsg := &pfcp.PFCPAssociationSetupRequest{} + message := &pfcp.Message{ + Header: pfcp.Header{ + Version: pfcp.PfcpVersion, + MP: 0, + S: pfcp.SEID_NOT_PRESENT, + MessageType: pfcp.PFCP_ASSOCIATION_SETUP_REQUEST, + SequenceNumber: 1, + }, + Body: pfcpMsg, + } + addr := &net.UDPAddr{ + IP: testNodeID.ResolveNodeIdToIp(), + Port: pfcpUdp.PFCP_PORT, + } + + Convey("test SendPfcpRequest", t, func() { + patches := gomonkey.ApplyMethod(reflect.TypeOf(udp.Server), "WriteRequestTo", + func(_ *pfcpUdp.PfcpServer, _ *pfcp.Message, _ *net.UDPAddr) (*pfcpUdp.Message, error) { + return nil, nil + }) + defer patches.Reset() + _, err := udp.SendPfcpRequest(message, addr) + So(err, ShouldBeNil) + }) +} From 7f10cc031204a952e7ec1fc11760f5571e36795b Mon Sep 17 00:00:00 2001 From: chh Date: Tue, 29 Oct 2024 14:24:24 +0000 Subject: [PATCH 7/9] fix: linter error --- internal/pfcp/handler/handler_test.go | 5 ++--- internal/pfcp/message/build_test.go | 19 ++++++++++--------- internal/pfcp/message/send_test.go | 1 - internal/pfcp/udp/udp_test.go | 9 +++++---- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/internal/pfcp/handler/handler_test.go b/internal/pfcp/handler/handler_test.go index 333e7ef5..82b769bb 100644 --- a/internal/pfcp/handler/handler_test.go +++ b/internal/pfcp/handler/handler_test.go @@ -8,15 +8,14 @@ import ( "regexp" "testing" - "github.com/free5gc/pfcp" "github.com/sirupsen/logrus" + . "github.com/smartystreets/goconvey/convey" + "github.com/free5gc/pfcp" "github.com/free5gc/pfcp/pfcpType" "github.com/free5gc/pfcp/pfcpUdp" - "github.com/free5gc/smf/internal/logger" "github.com/free5gc/smf/internal/pfcp/handler" - . "github.com/smartystreets/goconvey/convey" ) type LogCapture struct { diff --git a/internal/pfcp/message/build_test.go b/internal/pfcp/message/build_test.go index faba23dd..834bcb16 100644 --- a/internal/pfcp/message/build_test.go +++ b/internal/pfcp/message/build_test.go @@ -4,12 +4,13 @@ import ( "net" "testing" + "github.com/stretchr/testify/assert" + "github.com/free5gc/pfcp/pfcpType" "github.com/free5gc/smf/internal/context" "github.com/free5gc/smf/internal/pfcp/message" "github.com/free5gc/smf/internal/pfcp/udp" "github.com/free5gc/smf/pkg/factory" - "github.com/stretchr/testify/assert" ) var testConfig = factory.Config{ @@ -42,7 +43,7 @@ func initSmfContext() { func initRuleList() ([]*context.PDR, []*context.FAR, []*context.BAR, []*context.QER, []*context.URR, ) { - var testPDR = &context.PDR{ + testPDR := &context.PDR{ PDRID: uint16(1), State: context.RULE_INITIAL, OuterHeaderRemoval: &pfcpType.OuterHeaderRemoval{ @@ -53,7 +54,7 @@ func initRuleList() ([]*context.PDR, []*context.FAR, []*context.BAR, QER: []*context.QER{}, } - var testFAR = &context.FAR{ + testFAR := &context.FAR{ FARID: uint32(123), // State Can be RULE_INITIAL or RULE_UPDATE or RULE_REMOVE State: context.RULE_INITIAL, @@ -64,19 +65,19 @@ func initRuleList() ([]*context.PDR, []*context.FAR, []*context.BAR, BAR: &context.BAR{}, } - var testBAR = &context.BAR{ + testBAR := &context.BAR{ BARID: uint8(124), // State Can be RULE_INITIAL or RULE_UPDATE or RULE_REMOVE State: context.RULE_INITIAL, } - var testQER = &context.QER{ + testQER := &context.QER{ QERID: uint32(123), // State Can be RULE_INITIAL or RULE_UPDATE or RULE_REMOVE State: context.RULE_INITIAL, } - var testURR = &context.URR{ + testURR := &context.URR{ URRID: uint32(123), // State Can be RULE_INITIAL or RULE_UPDATE or RULE_REMOVE State: context.RULE_INITIAL, @@ -154,7 +155,7 @@ func TestBuildPfcpAssociationReleaseResponse(t *testing.T) { func TestBuildPfcpSessionEstablishmentRequest(t *testing.T) { initSmfContext() - var smctx = context.NewSMContext("imsi-208930000000001", 10) + smctx := context.NewSMContext("imsi-208930000000001", 10) pdrList, farList, barList, qerList, urrList := initRuleList() smctx.PFCPContext["10.4.0.1"] = &context.PFCPSessionContext{} @@ -178,7 +179,7 @@ func TestBuildPfcpSessionEstablishmentRequest(t *testing.T) { assert.Equal(t, urrList[0].State, context.RULE_CREATE) req2, err2 := message.BuildPfcpSessionEstablishmentRequest( - *testNodeID, "10.4.0.1", smctx, nil, nil, nil, nil, nil) + *testNodeID, "10.4.0.1", smctx, nil, nil, nil, nil, nil) if err2 != nil { t.Errorf("TestBuildPfcpSessionEstablishmentRequest failed: %v", err2) } @@ -206,7 +207,7 @@ func TestBuildPfcpSessionEstablishmentResponse(t *testing.T) { func TestBuildPfcpSessionModificationRequest(t *testing.T) { initSmfContext() - var smctx = context.NewSMContext("imsi-208930000000001", 10) + smctx := context.NewSMContext("imsi-208930000000001", 10) pdrList, farList, barList, qerList, urrList := initRuleList() smctx.PFCPContext["10.4.0.1"] = &context.PFCPSessionContext{} diff --git a/internal/pfcp/message/send_test.go b/internal/pfcp/message/send_test.go index 9d263b9f..a114d0c3 100644 --- a/internal/pfcp/message/send_test.go +++ b/internal/pfcp/message/send_test.go @@ -15,7 +15,6 @@ import ( ) func TestSendPfcpAssociationSetupRequest(t *testing.T) { - } func TestSendPfcpSessionEstablishmentResponse(t *testing.T) { diff --git a/internal/pfcp/udp/udp_test.go b/internal/pfcp/udp/udp_test.go index 4a971908..a0b918a0 100644 --- a/internal/pfcp/udp/udp_test.go +++ b/internal/pfcp/udp/udp_test.go @@ -8,6 +8,9 @@ import ( "time" "github.com/agiledragon/gomonkey" + . "github.com/smartystreets/goconvey/convey" + "github.com/stretchr/testify/require" + "github.com/free5gc/pfcp" "github.com/free5gc/pfcp/pfcpType" "github.com/free5gc/pfcp/pfcpUdp" @@ -15,8 +18,6 @@ import ( smf_pfcp "github.com/free5gc/smf/internal/pfcp" "github.com/free5gc/smf/internal/pfcp/udp" "github.com/free5gc/smf/pkg/factory" - . "github.com/smartystreets/goconvey/convey" - "github.com/stretchr/testify/require" ) const testPfcpClientPort = 12345 @@ -98,7 +99,7 @@ func initSmfContext() { } func TestSendPfcpRequest(t *testing.T) { - //init smf context + // init smf context initSmfContext() context.GetSelf().CPNodeID = pfcpType.NodeID{ NodeIdType: pfcpType.NodeIdTypeIpv4Address, @@ -106,7 +107,7 @@ func TestSendPfcpRequest(t *testing.T) { } udp.Server = pfcpUdp.NewPfcpServer(net.ParseIP("127.0.0.1").To4().String()) - //build message + // build message pfcpMsg := &pfcp.PFCPAssociationSetupRequest{} message := &pfcp.Message{ Header: pfcp.Header{ From dbf8cc3d17a4eb9953d6825f83d80f2e26adfdc2 Mon Sep 17 00:00:00 2001 From: chh Date: Wed, 30 Oct 2024 03:51:02 +0000 Subject: [PATCH 8/9] fix: linter error --- go.mod | 1 - go.sum | 2 - internal/pfcp/udp/udp_test.go | 79 +++-------------------------------- 3 files changed, 5 insertions(+), 77 deletions(-) diff --git a/go.mod b/go.mod index df121ef1..4fd98007 100644 --- a/go.mod +++ b/go.mod @@ -26,7 +26,6 @@ require ( ) require ( - github.com/agiledragon/gomonkey v2.0.2+incompatible github.com/antonfisher/nested-logrus-formatter v1.3.1 // indirect github.com/bytedance/sonic v1.9.1 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect diff --git a/go.sum b/go.sum index cf4b9d3e..b05b6d44 100644 --- a/go.sum +++ b/go.sum @@ -33,8 +33,6 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= -github.com/agiledragon/gomonkey v2.0.2+incompatible h1:eXKi9/piiC3cjJD1658mEE2o3NjkJ5vDLgYjCQu0Xlw= -github.com/agiledragon/gomonkey v2.0.2+incompatible/go.mod h1:2NGfXu1a80LLr2cmWXGBDaHEjb1idR6+FVlX5T3D9hw= github.com/antihax/optional v1.0.0 h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwcg= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/antonfisher/nested-logrus-formatter v1.3.1 h1:NFJIr+pzwv5QLHTPyKz9UMEoHck02Q9L0FP13b/xSbQ= diff --git a/internal/pfcp/udp/udp_test.go b/internal/pfcp/udp/udp_test.go index a0b918a0..6a929bf4 100644 --- a/internal/pfcp/udp/udp_test.go +++ b/internal/pfcp/udp/udp_test.go @@ -1,23 +1,18 @@ package udp_test import ( - "context" "net" - "reflect" "testing" "time" - "github.com/agiledragon/gomonkey" - . "github.com/smartystreets/goconvey/convey" "github.com/stretchr/testify/require" "github.com/free5gc/pfcp" "github.com/free5gc/pfcp/pfcpType" "github.com/free5gc/pfcp/pfcpUdp" - smf_context "github.com/free5gc/smf/internal/context" + "github.com/free5gc/smf/internal/context" smf_pfcp "github.com/free5gc/smf/internal/pfcp" "github.com/free5gc/smf/internal/pfcp/udp" - "github.com/free5gc/smf/pkg/factory" ) const testPfcpClientPort = 12345 @@ -25,16 +20,13 @@ const testPfcpClientPort = 12345 func TestRun(t *testing.T) { // Set SMF Node ID - smfContext := smf_context.GetSelf() - - smfContext.CPNodeID = pfcpType.NodeID{ + context.GetSelf().CPNodeID = pfcpType.NodeID{ NodeIdType: pfcpType.NodeIdTypeIpv4Address, IP: net.ParseIP("127.0.0.1").To4(), } - smfContext.ExternalAddr = "127.0.0.1" - smfContext.ListenAddr = "127.0.0.1" + context.GetSelf().ExternalAddr = "127.0.0.1" + context.GetSelf().ListenAddr = "127.0.0.1" - smfContext.PfcpContext, smfContext.PfcpCancelFunc = context.WithCancel(context.Background()) udp.Run(smf_pfcp.Dispatch) testPfcpReq := pfcp.Message{ @@ -68,69 +60,8 @@ func TestRun(t *testing.T) { err := pfcpUdp.SendPfcpMessage(testPfcpReq, srcAddr, dstAddr) require.Nil(t, err) - err = udp.ClosePfcp() + err = udp.Server.Close() require.NoError(t, err) time.Sleep(300 * time.Millisecond) } - -var testConfig = factory.Config{ - Info: &factory.Info{ - Version: "1.0.0", - Description: "SMF procdeure test configuration", - }, - Configuration: &factory.Configuration{ - Sbi: &factory.Sbi{ - Scheme: "http", - RegisterIPv4: "127.0.0.1", - BindingIPv4: "127.0.0.1", - Port: 8000, - }, - }, -} - -var testNodeID = pfcpType.NodeID{ - NodeIdType: 0, - IP: net.ParseIP("127.0.0.3").To4(), -} - -func initSmfContext() { - context.InitSmfContext(&testConfig) -} - -func TestSendPfcpRequest(t *testing.T) { - // init smf context - initSmfContext() - context.GetSelf().CPNodeID = pfcpType.NodeID{ - NodeIdType: pfcpType.NodeIdTypeIpv4Address, - IP: net.ParseIP("127.0.0.1").To4(), - } - udp.Server = pfcpUdp.NewPfcpServer(net.ParseIP("127.0.0.1").To4().String()) - - // build message - pfcpMsg := &pfcp.PFCPAssociationSetupRequest{} - message := &pfcp.Message{ - Header: pfcp.Header{ - Version: pfcp.PfcpVersion, - MP: 0, - S: pfcp.SEID_NOT_PRESENT, - MessageType: pfcp.PFCP_ASSOCIATION_SETUP_REQUEST, - SequenceNumber: 1, - }, - Body: pfcpMsg, - } - addr := &net.UDPAddr{ - IP: testNodeID.ResolveNodeIdToIp(), - Port: pfcpUdp.PFCP_PORT, - } - - Convey("test SendPfcpRequest", t, func() { - patches := gomonkey.ApplyMethod(reflect.TypeOf(udp.Server), "WriteRequestTo", - func(_ *pfcpUdp.PfcpServer, _ *pfcp.Message, _ *net.UDPAddr) (*pfcpUdp.Message, error) { - return nil, nil - }) - defer patches.Reset() - _, err := udp.SendPfcpRequest(message, addr) - So(err, ShouldBeNil) - }) -} From f070ba816072c711a52f4d09e4397762243096d5 Mon Sep 17 00:00:00 2001 From: chh Date: Wed, 30 Oct 2024 05:33:47 +0000 Subject: [PATCH 9/9] style: remove unless code --- .gitignore | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 0fe62874..ad41b44a 100644 --- a/.gitignore +++ b/.gitignore @@ -26,7 +26,4 @@ cscope.* # Debug *.log -*.pcap - -tttt_test.go -tttt.go \ No newline at end of file +*.pcap \ No newline at end of file