Skip to content

Commit

Permalink
fix: IMEI/IMEISV validation, test cases and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
williamlin0518 committed Jan 8, 2025
1 parent db19072 commit 361a2aa
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 55 deletions.
80 changes: 40 additions & 40 deletions nasConvert/MobileIdentity5GS.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
// 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
}
30 changes: 15 additions & 15 deletions nasConvert/MobileIdentity5GS_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 361a2aa

Please sign in to comment.