-
Notifications
You must be signed in to change notification settings - Fork 129
Adding unit test cases for init.go and convert.go of AMF #177
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,167 @@ | ||||||
| package util | ||||||
|
|
||||||
| import ( | ||||||
| "testing" | ||||||
|
|
||||||
| "github.com/stretchr/testify/assert" | ||||||
| "github.com/stretchr/testify/require" | ||||||
| "github.com/free5gc/openapi/models" | ||||||
| ) | ||||||
|
|
||||||
| func TestSnssaiHexToModels(t *testing.T) { | ||||||
| t.Run("valid hex string with SST and SD", func(t *testing.T) { | ||||||
| hexStr := "01112233" // SST = 0x01 (1), SD = "112233" | ||||||
| expected := &models.Snssai{ | ||||||
| Sst: 1, | ||||||
| Sd: "112233", | ||||||
| } | ||||||
|
|
||||||
| snssai, err := SnssaiHexToModels(hexStr) | ||||||
| require.NoError(t, err) | ||||||
| assert.Equal(t, expected, snssai) | ||||||
| }) | ||||||
|
|
||||||
| t.Run("invalid hex string for SST", func(t *testing.T) { | ||||||
| hexStr := "ZZ112233" // invalid SST hex | ||||||
|
|
||||||
| snssai, err := SnssaiHexToModels(hexStr) | ||||||
| assert.Nil(t, snssai) | ||||||
| assert.Error(t, err) | ||||||
| }) | ||||||
| } | ||||||
| func TestSnssaiModelsToHex(t *testing.T) { | ||||||
| t.Run("valid Snssai with Sst and Sd", func(t *testing.T) { | ||||||
| snssai := models.Snssai{ | ||||||
| Sst: 1, | ||||||
| Sd: "112233", | ||||||
| } | ||||||
| expected := "01112233" // 01 (hex of 1) + Sd | ||||||
|
|
||||||
| hexStr := SnssaiModelsToHex(snssai) | ||||||
| assert.Equal(t, expected, hexStr) | ||||||
| }) | ||||||
|
|
||||||
| t.Run("Sst with double digit value", func(t *testing.T) { | ||||||
| snssai := models.Snssai{ | ||||||
| Sst: 26, | ||||||
| Sd: "abcdef", | ||||||
| } | ||||||
| expected := "1aabcdef" // 1a is hex of 26 | ||||||
|
|
||||||
| hexStr := SnssaiModelsToHex(snssai) | ||||||
| assert.Equal(t, expected, hexStr) | ||||||
| }) | ||||||
|
|
||||||
| t.Run("empty Sd field", func(t *testing.T) { | ||||||
| snssai := models.Snssai{ | ||||||
| Sst: 15, | ||||||
| Sd: "", | ||||||
| } | ||||||
| expected := "0f" // just the hex of 15 | ||||||
|
|
||||||
| hexStr := SnssaiModelsToHex(snssai) | ||||||
| assert.Equal(t, expected, hexStr) | ||||||
| }) | ||||||
| } | ||||||
| func TestSeperateAmfId(t *testing.T) { | ||||||
|
||||||
| t.Run("valid AMF ID", func(t *testing.T) { | ||||||
| amfid := "12a1b2" // regionId = "12", rest = a1b2 | ||||||
|
|
||||||
| regionId, setId, ptrId, err := SeperateAmfId(amfid) | ||||||
|
|
||||||
| assert.NoError(t, err) | ||||||
| assert.Equal(t, "12", regionId) | ||||||
| assert.Equal(t, "286", setId) // derived from bits | ||||||
| assert.Equal(t, "32", ptrId) // derived from bits | ||||||
| }) | ||||||
|
|
||||||
| t.Run("invalid AMF ID length", func(t *testing.T) { | ||||||
| amfid := "1234" | ||||||
|
|
||||||
| regionId, setId, ptrId, err := SeperateAmfId(amfid) | ||||||
|
|
||||||
| assert.Error(t, err) | ||||||
| assert.Empty(t, regionId) | ||||||
| assert.Empty(t, setId) | ||||||
| assert.Empty(t, ptrId) | ||||||
| }) | ||||||
|
|
||||||
| t.Run("invalid AMF ID hex characters", func(t *testing.T) { | ||||||
| amfid := "12zzzz" // invalid hex in last 4 characters | ||||||
|
|
||||||
| regionId, setId, ptrId, err := SeperateAmfId(amfid) | ||||||
|
|
||||||
| assert.Error(t, err) | ||||||
| assert.Equal(t, "12", regionId) | ||||||
| assert.Empty(t, setId) | ||||||
| assert.Empty(t, ptrId) | ||||||
| }) | ||||||
| } | ||||||
|
|
||||||
| func TestPlmnIdStringToModels(t *testing.T) { | ||||||
| t.Run("valid 5-digit PLMN ID", func(t *testing.T) { | ||||||
| plmnStr := "28393" // MCC: 310, MNC: 15 | ||||||
|
|
||||||
| expected := models.PlmnId{ | ||||||
| Mcc: "283", | ||||||
| Mnc: "93", | ||||||
| } | ||||||
|
|
||||||
| result := PlmnIdStringToModels(plmnStr) | ||||||
|
|
||||||
| assert.Equal(t, expected, result) | ||||||
| }) | ||||||
|
|
||||||
| t.Run("valid 6-digit PLMN ID", func(t *testing.T) { | ||||||
| plmnStr := "460011" // MCC: 460, MNC: 011 | ||||||
|
|
||||||
| expected := models.PlmnId{ | ||||||
| Mcc: "460", | ||||||
| Mnc: "011", | ||||||
| } | ||||||
|
|
||||||
| result := PlmnIdStringToModels(plmnStr) | ||||||
|
|
||||||
| assert.Equal(t, expected, result) | ||||||
| }) | ||||||
|
|
||||||
| t.Run("invalid PLMN ID (too short)", func(t *testing.T) { | ||||||
| plmnStr := "12" // invalid | ||||||
|
|
||||||
| defer func() { | ||||||
| if r := recover(); r == nil { | ||||||
| t.Error("Expected panic due to short plmnId, but function did not panic") | ||||||
| } | ||||||
| }() | ||||||
|
|
||||||
| _ = PlmnIdStringToModels(plmnStr) // should panic on plmnId[:3] | ||||||
| }) | ||||||
| } | ||||||
| func TestTACConfigToModels(t *testing.T) { | ||||||
| t.Run("valid TAC integer string", func(t *testing.T) { | ||||||
| input := "12345" | ||||||
| expected := "003039" // 12345 in hex is 0x3039 | ||||||
|
|
||||||
| result := TACConfigToModels(input) | ||||||
|
|
||||||
| assert.Equal(t, expected, result) | ||||||
| }) | ||||||
|
|
||||||
| t.Run("maximum 3-byte TAC value", func(t *testing.T) { | ||||||
| input := "16777215" // 0xFFFFFF | ||||||
| expected := "ffffff" | ||||||
|
|
||||||
| result := TACConfigToModels(input) | ||||||
|
|
||||||
| assert.Equal(t, expected, result) | ||||||
| }) | ||||||
|
|
||||||
| t.Run("invalid TAC string (non-numeric)", func(t *testing.T) { | ||||||
| input := "abc" | ||||||
|
|
||||||
| result := TACConfigToModels(input) | ||||||
|
|
||||||
| // When ParseUint fails, it logs and returns empty hex string | ||||||
|
||||||
| // When ParseUint fails, it logs and returns empty hex string | |
| // When ParseUint fails, it logs the error and returns "000000" (since tmp is 0 if err != nil) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,224 @@ | ||||||||||||||||||||||||||||
| package service | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||||
| "testing" | ||||||||||||||||||||||||||||
| "time" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| amf_context "github.com/free5gc/amf/internal/context" | ||||||||||||||||||||||||||||
| "github.com/free5gc/amf/internal/sbi" | ||||||||||||||||||||||||||||
| "github.com/free5gc/amf/internal/sbi/consumer" | ||||||||||||||||||||||||||||
| "github.com/free5gc/amf/internal/sbi/processor" | ||||||||||||||||||||||||||||
| "github.com/free5gc/amf/pkg/factory" | ||||||||||||||||||||||||||||
| "github.com/stretchr/testify/assert" | ||||||||||||||||||||||||||||
| "github.com/stretchr/testify/mock" | ||||||||||||||||||||||||||||
| "github.com/free5gc/openapi/models" | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
| "github.com/free5gc/openapi/models" | |
| "github.com/free5gc/openapi/models" |
Copilot
AI
Dec 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assertion is incorrect. The test calls app.SetLogEnable(true) on line 33 but never sets it to false before the assertion on line 36. The test should verify that SetLogEnable(true) worked first, or this should be testing the false value that was just set.
| app.SetLogEnable(true) | |
| app.SetLogEnable(true) | |
| assert.True(t, app.cfg.GetLogEnable()) |
Copilot
AI
Dec 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The SetLogFilePath method does not exist in the AmfApp struct or the app.App interface. This test will fail to compile. The app.App interface only defines SetLogEnable, SetLogLevel, and SetReportCaller for logging configuration.
| func TestSetLogFilePath(t *testing.T) { | |
| config := &factory.Config{} | |
| app := &AmfApp{cfg: config} | |
| logFilePath := "/tmp/test_amf_log.log" | |
| f, err := os.Create(logFilePath) | |
| assert.NoError(t, err) | |
| defer f.Close() | |
| defer os.Remove(logFilePath) | |
| app.SetLogFilePath(f) | |
| assert.Equal(t, logFilePath, f.Name()) | |
| } |
Copilot
AI
Dec 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The AMF global variable is being set in NewApp (line 91 of init.go), but this test doesn't verify that behavior. Consider adding an assertion to verify that the global AMF variable is properly set after NewApp is called.
Copilot
AI
Dec 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace on empty line. This should be removed to maintain code cleanliness.
Copilot
AI
Dec 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment is misleading. The Consumer method doesn't "wrap the mock inside a dummy real consumer" - it actually creates and returns a real Consumer instance. This could be confusing for future maintainers.
| // Wrap the mock inside a dummy real consumer | |
| // Creates and returns a real consumer.Consumer instance for use in tests |
Copilot
AI
Dec 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is misleading. The comment says "In package consumer" but this code is in the service package's test file. Additionally, DummySCTPListener is defined but never used in any test.
| // In package consumer | |
| type DummySCTPListener struct{} | |
| func (d *DummySCTPListener) Close() error { | |
| // Pretend to close without doing anything | |
| return nil | |
| } |
Copilot
AI
Dec 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace on empty line. This should be removed to maintain code cleanliness.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace on line. Remove the extra space after 'util' to maintain code cleanliness.