From db19072d1a91d5780473c19612e6660e2287adb6 Mon Sep 17 00:00:00 2001 From: william Date: Wed, 8 Jan 2025 02:27:13 +0000 Subject: [PATCH 1/4] fix: IMEI/IMEISV validation and test cases --- nasConvert/MobileIdentity5GS.go | 48 +++++++++++++++++++++++ nasConvert/MobileIdentity5GS_test.go | 57 +++++++++++++++++----------- 2 files changed, 83 insertions(+), 22 deletions(-) diff --git a/nasConvert/MobileIdentity5GS.go b/nasConvert/MobileIdentity5GS.go index 2fbcc9f..ad131b0 100644 --- a/nasConvert/MobileIdentity5GS.go +++ b/nasConvert/MobileIdentity5GS.go @@ -7,6 +7,7 @@ import ( "math/bits" "strconv" "strings" + "unicode" "github.com/free5gc/nas/logger" "github.com/free5gc/nas/nasMessage" @@ -282,5 +283,52 @@ func PeiToStringWithError(buf []byte) (string, error) { digitStr = digitStr[:len(digitStr)-1] // remove the last digit } + if prefix == "imei-" { + // Validate IMEI before returning + if len(digitStr) != 15 { + return "", fmt.Errorf("invalid IMEI length: expected 15 digits, got %d", len(digitStr)) + } + valid, err := ValidateIMEI(digitStr) + if err != nil { + return "", fmt.Errorf("IMEI validation error: %w", err) + } + if !valid { + return "", fmt.Errorf("invalid IMEI checksum") + } + } else{ + if len(digitStr) != 16 { + return "", fmt.Errorf("invalid IMEISV length: expected 16 digits, got %d", len(digitStr)) + } + } + return prefix + digitStr, nil } + +func ValidateIMEI(imei string) (bool, error) { + // Remove any non-digit characters + cleanIMEI := strings.ReplaceAll(imei, "-", "") + cleanIMEI = strings.ReplaceAll(cleanIMEI, " ", "") + + // Check if all characters are digits + for _, char := range cleanIMEI { + if !unicode.IsDigit(char) { + return false, fmt.Errorf("IMEI contains non-digit character: %c", char) + } + } + + // Luhn algorithm validation + sum := 0 + for i := len(cleanIMEI) - 1; i >= 0; i-- { + digit := int(cleanIMEI[i] - '0') + + if (len(cleanIMEI)-i)%2 == 0 { + digit *= 2 + if digit > 9 { + digit = digit/10 + digit%10 + } + } + sum += digit + } + + return sum%10 == 0, nil +} \ No newline at end of file diff --git a/nasConvert/MobileIdentity5GS_test.go b/nasConvert/MobileIdentity5GS_test.go index 3987982..7f669b3 100644 --- a/nasConvert/MobileIdentity5GS_test.go +++ b/nasConvert/MobileIdentity5GS_test.go @@ -311,40 +311,53 @@ func TestPeiToStringWithError(t *testing.T) { wantErr bool }{ { - name: "PEI-IMEI-even", - args: args{ - buf: []byte{0x3, 0xf1}, - }, - want: "imei-01", - wantErr: false, - }, + name: "Complete-Valid-IMEI", + args: args{ + // Example encoding for a valid 15-digit IMEI + buf: []byte{ + 0x4b, 0x09, 0x51, 0x24, 0x30, 0x32, 0x57, 0x81, + }, + }, + want: "imei-490154203237518", + wantErr: false, + }, { - name: "PEI-IMEISV-odd", + name: "Complete-Ivalid-IMEI", + args: args{ + // Example encoding for a valid 15-digit IMEI + buf: []byte{ + 0x4b, 0x09, 0x51, 0x24, 0x30, 0x32, 0x57, 0x82, + }, + }, + wantErr: true, + }, + { + name: "Complete-Valid-IMEISV", args: args{ - buf: []byte{0xd, 0x21}, + buf: []byte{ + 0x90, 0x87, 0x65, 0x43, 0x21, 0x01, 0x23, 0x45, 0x60, + }, }, - want: "imeisv-012", + want: "imeisv-9785634121032540", wantErr: false, }, { - name: "PEI-nil", - wantErr: true, - }, - { - name: "PEI-IMEI-len1", + name: "IMEI-TooLong", args: args{ - buf: []byte{0xb}, + buf: []byte{ + 0x4b, 0x09, 0x51, 0x24, 0x30, 0x32, 0x57, 0x81, 0x20, + }, }, - want: "imei-0", - wantErr: false, + wantErr: true, }, { - name: "PEI-IMEI-len0", + name: "IMEI-TooShort", args: args{ - buf: []byte{0x3}, + buf: []byte{ + 0x4b, 0x09, 0x51, 0x24, 0x30, 0x32, + }, }, - want: "imei-", - wantErr: false, + wantErr: true, }, } for _, tt := range tests { From 361a2aaca8b97afd948af53724e3f9a51b100274 Mon Sep 17 00:00:00 2001 From: william Date: Wed, 8 Jan 2025 02:39:59 +0000 Subject: [PATCH 2/4] fix: IMEI/IMEISV validation, test cases and linting --- nasConvert/MobileIdentity5GS.go | 80 ++++++++++++++-------------- nasConvert/MobileIdentity5GS_test.go | 30 +++++------ 2 files changed, 55 insertions(+), 55 deletions(-) diff --git a/nasConvert/MobileIdentity5GS.go b/nasConvert/MobileIdentity5GS.go index ad131b0..038dba8 100644 --- a/nasConvert/MobileIdentity5GS.go +++ b/nasConvert/MobileIdentity5GS.go @@ -284,51 +284,51 @@ func PeiToStringWithError(buf []byte) (string, error) { } if prefix == "imei-" { - // Validate IMEI before returning + // Validate IMEI before returning if len(digitStr) != 15 { - return "", fmt.Errorf("invalid IMEI length: expected 15 digits, got %d", len(digitStr)) - } - valid, err := ValidateIMEI(digitStr) - if err != nil { - return "", fmt.Errorf("IMEI validation error: %w", err) - } - if !valid { - return "", fmt.Errorf("invalid IMEI checksum") - } - } else{ + return "", fmt.Errorf("invalid IMEI length: expected 15 digits, got %d", len(digitStr)) + } + valid, err := ValidateIMEI(digitStr) + if err != nil { + return "", fmt.Errorf("IMEI validation error: %w", err) + } + if !valid { + return "", fmt.Errorf("invalid IMEI checksum") + } + } else { if len(digitStr) != 16 { - return "", fmt.Errorf("invalid IMEISV length: expected 16 digits, got %d", len(digitStr)) - } + return "", fmt.Errorf("invalid IMEISV length: expected 16 digits, got %d", len(digitStr)) + } } return prefix + digitStr, nil } func ValidateIMEI(imei string) (bool, error) { - // Remove any non-digit characters - cleanIMEI := strings.ReplaceAll(imei, "-", "") - cleanIMEI = strings.ReplaceAll(cleanIMEI, " ", "") - - // Check if all characters are digits - for _, char := range cleanIMEI { - if !unicode.IsDigit(char) { - return false, fmt.Errorf("IMEI contains non-digit character: %c", char) - } - } - - // Luhn algorithm validation - sum := 0 - for i := len(cleanIMEI) - 1; i >= 0; i-- { - digit := int(cleanIMEI[i] - '0') - - if (len(cleanIMEI)-i)%2 == 0 { - digit *= 2 - if digit > 9 { - digit = digit/10 + digit%10 - } - } - sum += digit - } - - return sum%10 == 0, nil -} \ No newline at end of file + // Remove any non-digit characters + cleanIMEI := strings.ReplaceAll(imei, "-", "") + cleanIMEI = strings.ReplaceAll(cleanIMEI, " ", "") + + // Check if all characters are digits + for _, char := range cleanIMEI { + if !unicode.IsDigit(char) { + return false, fmt.Errorf("IMEI contains non-digit character: %c", char) + } + } + + // Luhn algorithm validation + sum := 0 + for i := len(cleanIMEI) - 1; i >= 0; i-- { + digit := int(cleanIMEI[i] - '0') + + if (len(cleanIMEI)-i)%2 == 0 { + digit *= 2 + if digit > 9 { + digit = digit/10 + digit%10 + } + } + sum += digit + } + + return sum%10 == 0, nil +} diff --git a/nasConvert/MobileIdentity5GS_test.go b/nasConvert/MobileIdentity5GS_test.go index 7f669b3..c4234a4 100644 --- a/nasConvert/MobileIdentity5GS_test.go +++ b/nasConvert/MobileIdentity5GS_test.go @@ -311,26 +311,26 @@ func TestPeiToStringWithError(t *testing.T) { wantErr bool }{ { - name: "Complete-Valid-IMEI", - args: args{ - // Example encoding for a valid 15-digit IMEI - buf: []byte{ + name: "Complete-Valid-IMEI", + args: args{ + // Example encoding for a valid 15-digit IMEI + buf: []byte{ 0x4b, 0x09, 0x51, 0x24, 0x30, 0x32, 0x57, 0x81, }, - }, - want: "imei-490154203237518", - wantErr: false, - }, + }, + want: "imei-490154203237518", + wantErr: false, + }, { - name: "Complete-Ivalid-IMEI", - args: args{ - // Example encoding for a valid 15-digit IMEI - buf: []byte{ + name: "Complete-Ivalid-IMEI", + args: args{ + // Example encoding for a valid 15-digit IMEI + buf: []byte{ 0x4b, 0x09, 0x51, 0x24, 0x30, 0x32, 0x57, 0x82, }, - }, - wantErr: true, - }, + }, + wantErr: true, + }, { name: "Complete-Valid-IMEISV", args: args{ From d66e0157085ea17777af7edcbe39a53aabb004b0 Mon Sep 17 00:00:00 2001 From: william Date: Mon, 13 Jan 2025 05:22:16 +0000 Subject: [PATCH 3/4] fix: IMEI/IMEISV validation, comment and function name --- nasConvert/MobileIdentity5GS.go | 2 +- nasConvert/MobileIdentity5GS_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/nasConvert/MobileIdentity5GS.go b/nasConvert/MobileIdentity5GS.go index 038dba8..34b8815 100644 --- a/nasConvert/MobileIdentity5GS.go +++ b/nasConvert/MobileIdentity5GS.go @@ -304,7 +304,7 @@ func PeiToStringWithError(buf []byte) (string, error) { return prefix + digitStr, nil } -func ValidateIMEI(imei string) (bool, error) { +func validateIMEI(imei string) (bool, error) { // Remove any non-digit characters cleanIMEI := strings.ReplaceAll(imei, "-", "") cleanIMEI = strings.ReplaceAll(cleanIMEI, " ", "") diff --git a/nasConvert/MobileIdentity5GS_test.go b/nasConvert/MobileIdentity5GS_test.go index c4234a4..393d6a6 100644 --- a/nasConvert/MobileIdentity5GS_test.go +++ b/nasConvert/MobileIdentity5GS_test.go @@ -324,7 +324,7 @@ func TestPeiToStringWithError(t *testing.T) { { name: "Complete-Ivalid-IMEI", args: args{ - // Example encoding for a valid 15-digit IMEI + // Not valid 15-digit IMEI: CD(Check Digit) not valid buf: []byte{ 0x4b, 0x09, 0x51, 0x24, 0x30, 0x32, 0x57, 0x82, }, From 01ef6ac07e353b0358f69420dec66d225fc6ba20 Mon Sep 17 00:00:00 2001 From: william Date: Mon, 13 Jan 2025 05:26:04 +0000 Subject: [PATCH 4/4] fix: IMEI/IMEISV validation, comment and function name to lowercase --- nasConvert/MobileIdentity5GS.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nasConvert/MobileIdentity5GS.go b/nasConvert/MobileIdentity5GS.go index 34b8815..51670b0 100644 --- a/nasConvert/MobileIdentity5GS.go +++ b/nasConvert/MobileIdentity5GS.go @@ -288,7 +288,7 @@ func PeiToStringWithError(buf []byte) (string, error) { if len(digitStr) != 15 { return "", fmt.Errorf("invalid IMEI length: expected 15 digits, got %d", len(digitStr)) } - valid, err := ValidateIMEI(digitStr) + valid, err := validateIMEI(digitStr) if err != nil { return "", fmt.Errorf("IMEI validation error: %w", err) }