Skip to content

Commit

Permalink
Merge pull request #115 from free5gc/fix/empty-sd
Browse files Browse the repository at this point in the history
fix: allow empty sd value in snssai
  • Loading branch information
ianchen0119 authored Aug 14, 2024
2 parents db0b9a7 + c36495a commit 772cdc8
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 12 deletions.
28 changes: 16 additions & 12 deletions pkg/factory/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (c *Configuration) validate() (bool, error) {
}

for _, snssaiInfo := range c.SNssaiInfo {
if result, err := snssaiInfo.validate(); err != nil {
if result, err := snssaiInfo.Validate(); err != nil {
return result, err
}
}
Expand Down Expand Up @@ -166,17 +166,19 @@ type SnssaiInfoItem struct {
DnnInfos []*SnssaiDnnInfoItem `yaml:"dnnInfos" valid:"required"`
}

func (s *SnssaiInfoItem) validate() (bool, error) {
func (s *SnssaiInfoItem) Validate() (bool, error) {
if snssai := s.SNssai; snssai != nil {
if result := (snssai.Sst >= 0 && snssai.Sst <= 255); !result {
err := errors.New("Invalid sNssai.Sst: " + strconv.Itoa(int(snssai.Sst)) + ", should be in range 0~255.")
return false, err
}

if result := govalidator.StringMatches(snssai.Sd, "^[0-9A-Fa-f]{6}$"); !result {
err := errors.New("Invalid sNssai.Sd: " + snssai.Sd +
", should be 3 bytes hex string and in range 000000~FFFFFF.")
return false, err
if snssai.Sd != "" {
if result := govalidator.StringMatches(snssai.Sd, "^[0-9A-Fa-f]{6}$"); !result {
err := errors.New("Invalid sNssai.Sd: " + snssai.Sd +
", should be 3 bytes hex string and in range 000000~FFFFFF.")
return false, err
}
}
}

Expand Down Expand Up @@ -489,7 +491,7 @@ func (u *UPNode) validate() (bool, error) {
})

for _, snssaiInfo := range u.SNssaiInfos {
if result, err := snssaiInfo.validate(); err != nil {
if result, err := snssaiInfo.Validate(); err != nil {
return result, err
}
}
Expand Down Expand Up @@ -547,17 +549,19 @@ type SnssaiUpfInfoItem struct {
DnnUpfInfoList []*DnnUpfInfoItem `json:"dnnUpfInfoList" yaml:"dnnUpfInfoList" valid:"required"`
}

func (s *SnssaiUpfInfoItem) validate() (bool, error) {
func (s *SnssaiUpfInfoItem) Validate() (bool, error) {
if s.SNssai != nil {
if result := (s.SNssai.Sst >= 0 && s.SNssai.Sst <= 255); !result {
err := errors.New("Invalid sNssai.Sst: " + strconv.Itoa(int(s.SNssai.Sst)) + ", should be in range 0~255.")
return false, err
}

if result := govalidator.StringMatches(s.SNssai.Sd, "^[0-9A-Fa-f]{6}$"); !result {
err := errors.New("Invalid sNssai.Sd: " + s.SNssai.Sd +
", should be 3 bytes hex string and in range 000000~FFFFFF.")
return false, err
if s.SNssai.Sd != "" {
if result := govalidator.StringMatches(s.SNssai.Sd, "^[0-9A-Fa-f]{6}$"); !result {
err := errors.New("Invalid sNssai.Sd: " + s.SNssai.Sd +
", should be 3 bytes hex string and in range 000000~FFFFFF.")
return false, err
}
}
}

Expand Down
106 changes: 106 additions & 0 deletions pkg/factory/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package factory_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/free5gc/openapi/models"
"github.com/free5gc/smf/pkg/factory"
)

func TestSnssaiInfoItem(t *testing.T) {
testcase := []struct {
Name string
Snssai *models.Snssai
DnnInfos []*factory.SnssaiDnnInfoItem
}{
{
Name: "Default",
Snssai: &models.Snssai{
Sst: int32(1),
Sd: "010203",
},
DnnInfos: []*factory.SnssaiDnnInfoItem{
{
Dnn: "internet",
DNS: &factory.DNS{
IPv4Addr: "8.8.8.8",
},
},
},
},
{
Name: "Empty SD",
Snssai: &models.Snssai{
Sst: int32(1),
},
DnnInfos: []*factory.SnssaiDnnInfoItem{
{
Dnn: "internet2",
DNS: &factory.DNS{
IPv4Addr: "1.1.1.1",
},
},
},
},
}

for _, tc := range testcase {
t.Run(tc.Name, func(t *testing.T) {
snssaiInfoItem := factory.SnssaiInfoItem{
SNssai: tc.Snssai,
DnnInfos: tc.DnnInfos,
}

ok, err := snssaiInfoItem.Validate()
require.True(t, ok)
require.Nil(t, err)
})
}
}

func TestSnssaiUpfInfoItem(t *testing.T) {
testcase := []struct {
Name string
Snssai *models.Snssai
DnnInfos []*factory.DnnUpfInfoItem
}{
{
Name: "Default",
Snssai: &models.Snssai{
Sst: int32(1),
Sd: "010203",
},
DnnInfos: []*factory.DnnUpfInfoItem{
{
Dnn: "internet",
},
},
},
{
Name: "Empty SD",
Snssai: &models.Snssai{
Sst: int32(1),
},
DnnInfos: []*factory.DnnUpfInfoItem{
{
Dnn: "internet2",
},
},
},
}

for _, tc := range testcase {
t.Run(tc.Name, func(t *testing.T) {
snssaiInfoItem := factory.SnssaiUpfInfoItem{
SNssai: tc.Snssai,
DnnUpfInfoList: tc.DnnInfos,
}

ok, err := snssaiInfoItem.Validate()
require.True(t, ok)
require.Nil(t, err)
})
}
}

0 comments on commit 772cdc8

Please sign in to comment.