mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-20 06:38:45 +05:00
fix *3gpp_uli to accept raw bytes instead of hex
This commit is contained in:
committed by
Dan Christian Bogos
parent
6a5351b915
commit
69b940039b
@@ -22,6 +22,7 @@ package general_tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -34,7 +35,7 @@ import (
|
||||
)
|
||||
|
||||
func TestDiamULI(t *testing.T) {
|
||||
t.Skip("configuration reference for *3gpp_uli request_fields; does not verify anything")
|
||||
// t.Skip("configuration reference for *3gpp_uli request_fields; does not verify anything")
|
||||
switch *utils.DBType {
|
||||
case utils.MetaInternal:
|
||||
case utils.MetaMySQL, utils.MetaMongo, utils.MetaPostgres:
|
||||
@@ -141,7 +142,11 @@ func TestDiamULI(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
userLocInfoRaw := "8245f750000145f75000000101"
|
||||
// Binary ULI from Wireshark capture: TAI+ECGI, MCC=547, MNC=05, TAC=1, ECI=257
|
||||
uliBytes, err := hex.DecodeString("8245f750000145f75000000101")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ccr := diam.NewRequest(diam.CreditControl, 4, nil)
|
||||
ccr.NewAVP(avp.ServiceInformation, avp.Mbit, 10415,
|
||||
&diam.GroupedAVP{
|
||||
@@ -149,7 +154,7 @@ func TestDiamULI(t *testing.T) {
|
||||
diam.NewAVP(avp.PSInformation, avp.Mbit, 10415,
|
||||
&diam.GroupedAVP{
|
||||
AVP: []*diam.AVP{
|
||||
diam.NewAVP(avp.TGPPUserLocationInfo, avp.Mbit, 10415, datatype.OctetString(userLocInfoRaw)),
|
||||
diam.NewAVP(avp.TGPPUserLocationInfo, avp.Mbit, 10415, datatype.OctetString(uliBytes)),
|
||||
},
|
||||
},
|
||||
),
|
||||
|
||||
@@ -876,18 +876,14 @@ func NewULIConverter(params string) (*ULIConverter, error) {
|
||||
}
|
||||
|
||||
// Convert implements DataConverter interface.
|
||||
// Input must be raw bytes (as delivered by Diameter OctetString AVPs).
|
||||
func (c *ULIConverter) Convert(in any) (any, error) {
|
||||
raw := IfaceAsString(in)
|
||||
if raw == "" {
|
||||
return nil, errors.New("empty ULI input")
|
||||
}
|
||||
|
||||
data, err := hex.DecodeString(strings.TrimPrefix(raw, "0x"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid ULI hex: %w", err)
|
||||
}
|
||||
|
||||
uli, err := DecodeULI(data)
|
||||
uli, err := DecodeULI([]byte(raw))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -262,49 +262,49 @@ func TestULIConverter(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params string
|
||||
input string
|
||||
hex string // raw ULI bytes in hex, decoded to binary before Convert
|
||||
expected any
|
||||
}{
|
||||
{
|
||||
name: "Extract TAI.MCC",
|
||||
params: "*3gpp_uli:TAI.MCC",
|
||||
input: "8245f750000145f75000000101",
|
||||
hex: "8245f750000145f75000000101",
|
||||
expected: "547",
|
||||
},
|
||||
{
|
||||
name: "Extract TAI.MNC",
|
||||
params: "*3gpp_uli:TAI.MNC",
|
||||
input: "8245f750000145f75000000101",
|
||||
hex: "8245f750000145f75000000101",
|
||||
expected: "05",
|
||||
},
|
||||
{
|
||||
name: "Extract TAI.TAC",
|
||||
params: "*3gpp_uli:TAI.TAC",
|
||||
input: "8245f750000145f75000000101",
|
||||
hex: "8245f750000145f75000000101",
|
||||
expected: uint16(1),
|
||||
},
|
||||
{
|
||||
name: "Extract ECGI.ECI",
|
||||
params: "*3gpp_uli:ECGI.ECI",
|
||||
input: "8245f750000145f75000000101",
|
||||
hex: "8245f750000145f75000000101",
|
||||
expected: uint32(257),
|
||||
},
|
||||
{
|
||||
name: "Extract TAI5GS.MCC from 5GS TAI",
|
||||
params: "*3gpp_uli:TAI5GS.MCC",
|
||||
input: "88130062123456",
|
||||
hex: "88130062123456",
|
||||
expected: "310",
|
||||
},
|
||||
{
|
||||
name: "Extract TAI5GS.TAC from 5GS TAI",
|
||||
params: "*3gpp_uli:TAI5GS.TAC",
|
||||
input: "88130062123456",
|
||||
hex: "88130062123456",
|
||||
expected: uint32(0x123456),
|
||||
},
|
||||
{
|
||||
name: "Extract NCGI.NCI from NCGI",
|
||||
params: "*3gpp_uli:NCGI.NCI",
|
||||
input: "871300620123456789",
|
||||
hex: "871300620123456789",
|
||||
expected: uint64(0x123456789),
|
||||
},
|
||||
}
|
||||
@@ -315,7 +315,11 @@ func TestULIConverter(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("NewULIConverter failed: %v", err)
|
||||
}
|
||||
got, err := conv.Convert(tt.input)
|
||||
raw, err := hex.DecodeString(tt.hex)
|
||||
if err != nil {
|
||||
t.Fatalf("invalid test hex: %v", err)
|
||||
}
|
||||
got, err := conv.Convert(string(raw))
|
||||
if err != nil {
|
||||
t.Fatalf("Convert failed: %v", err)
|
||||
}
|
||||
@@ -369,26 +373,16 @@ func TestDecodeULI_UnsupportedType(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestULIConverter_0xPrefix(t *testing.T) {
|
||||
conv, err := NewULIConverter("*3gpp_uli:TAI.MCC")
|
||||
if err != nil {
|
||||
t.Fatalf("NewULIConverter failed: %v", err)
|
||||
}
|
||||
got, err := conv.Convert("0x8245f750000145f75000000101")
|
||||
if err != nil {
|
||||
t.Fatalf("Convert failed: %v", err)
|
||||
}
|
||||
if got != "547" {
|
||||
t.Errorf("got %q, want %q", got, "547")
|
||||
}
|
||||
}
|
||||
|
||||
func TestULIConverter_EmptyPath(t *testing.T) {
|
||||
conv, err := NewULIConverter("*3gpp_uli")
|
||||
if err != nil {
|
||||
t.Fatalf("NewULIConverter failed: %v", err)
|
||||
}
|
||||
got, err := conv.Convert("8013006204d2")
|
||||
raw, err := hex.DecodeString("8013006204d2")
|
||||
if err != nil {
|
||||
t.Fatalf("invalid test hex: %v", err)
|
||||
}
|
||||
got, err := conv.Convert(string(raw))
|
||||
if err != nil {
|
||||
t.Fatalf("Convert failed: %v", err)
|
||||
}
|
||||
@@ -415,7 +409,8 @@ func TestULIConverter_Errors(t *testing.T) {
|
||||
input any
|
||||
}{
|
||||
{"empty string", ""},
|
||||
{"invalid hex", "zzzz"},
|
||||
{"unsupported ULI type", string([]byte{0x03, 0x62, 0xF2, 0x10, 0x12, 0x34, 0x56, 0x78})},
|
||||
{"truncated data", string([]byte{0x82, 0x45})},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
||||
Reference in New Issue
Block a user