Files
realy/addresstag/addresstag_test.go
2025-06-26 14:50:18 +01:00

87 lines
2.2 KiB
Go

package addresstag
import (
"bytes"
"testing"
)
func TestDecodeAddressTag(t *testing.T) {
tests := []struct {
name string
tagValue string
wantK uint16
wantPkb string
wantD string
wantOK bool
}{
{
name: "valid tag",
tagValue: "123:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef:data",
wantK: 123,
wantPkb: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
wantD: "data",
wantOK: true,
},
{
name: "invalid format - less than 3 parts",
tagValue: "123:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
wantOK: false,
},
{
name: "invalid format - invalid key",
tagValue: "abc:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef:data",
wantOK: false,
},
{
name: "invalid format - invalid hex",
tagValue: "123:invalid_hex:data",
wantOK: false,
},
{
name: "max uint16 value",
tagValue: "65535:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef:data",
wantK: 65535,
wantPkb: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
wantD: "data",
wantOK: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k, pkb, d := DecodeAddressTag([]byte(tt.tagValue))
// Check if the function returned valid values
gotOK := k != 0 && len(pkb) == 32 && len(d) > 0
if gotOK != tt.wantOK {
t.Errorf("DecodeAddressTag() success = %v, want %v", gotOK, tt.wantOK)
return
}
if !tt.wantOK {
// If we don't expect success, no need to check the values
return
}
if k != tt.wantK {
t.Errorf("DecodeAddressTag() k = %v, want %v", k, tt.wantK)
}
// Convert pkb to hex string for comparison
if len(pkb) > 0 {
var hexPkb string
for _, b := range pkb {
hexPkb += string("0123456789abcdef"[b>>4]) + string("0123456789abcdef"[b&0xF])
}
if hexPkb != tt.wantPkb {
t.Errorf("DecodeAddressTag() pkb = %v, want %v", hexPkb, tt.wantPkb)
}
}
if !bytes.Equal(d, []byte(tt.wantD)) {
t.Errorf("DecodeAddressTag() d = %v, want %v", string(d), tt.wantD)
}
})
}
}