Files
realy/keys/keys_test.go

198 lines
3.9 KiB
Go

package keys
import (
"testing"
)
func TestIsValid32ByteHex(t *testing.T) {
tests := []struct {
name string
input string
expected bool
}{
{
"Valid lowercase hex 32 bytes",
"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d",
true,
},
{
"Valid lowercase hex 32 bytes with different value",
"f9dd6a762506260b38a2d3e5b464213c2e47fa3877429fe9ee60e071a31a07d7",
true,
},
{
"Invalid uppercase hex",
"3BF0C63FCB93463407AF97A5E5EE64FA883D107EF9E558472C4EB9AAAEFA459D",
false,
},
{
"Invalid mixed case hex",
"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459D",
false,
},
{
"Invalid too short",
"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa45",
false,
},
{
"Invalid too long",
"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d00",
false,
},
{
"Invalid non-hex characters",
"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459g",
false,
},
{
"Empty string",
"",
false,
},
{
"Invalid special characters",
"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa45!@",
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := IsValid32ByteHex(tt.input)
if result != tt.expected {
t.Errorf("IsValid32ByteHex(%q) = %v, want %v", tt.input, result, tt.expected)
}
})
}
}
func TestIsValid32ByteHexWithBytes(t *testing.T) {
tests := []struct {
name string
input []byte
expected bool
}{
{
"Valid lowercase hex bytes",
[]byte("3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"),
true,
},
{
"Invalid uppercase hex bytes",
[]byte("3BF0C63FCB93463407AF97A5E5EE64FA883D107EF9E558472C4EB9AAAEFA459D"),
false,
},
{
"Empty bytes",
[]byte(""),
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := IsValid32ByteHex(tt.input)
if result != tt.expected {
t.Errorf("IsValid32ByteHex(%q) = %v, want %v", string(tt.input), result, tt.expected)
}
})
}
}
func TestIsValidPublicKey(t *testing.T) {
tests := []struct {
name string
input string
expected bool
}{
{
"Valid public key",
"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d",
true,
},
{
"Another valid public key",
"f9dd6a762506260b38a2d3e5b464213c2e47fa3877429fe9ee60e071a31a07d7",
true,
},
{
"Invalid too short",
"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa45",
false,
},
{
"Invalid too long",
"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d00",
false,
},
{
"Invalid non-hex",
"not-a-hex-string",
false,
},
{
"Empty string",
"",
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := IsValidPublicKey(tt.input)
if result != tt.expected {
t.Errorf("IsValidPublicKey(%q) = %v, want %v", tt.input, result, tt.expected)
}
})
}
}
func TestHexPubkeyToBytes(t *testing.T) {
tests := []struct {
name string
input string
expectError bool
expectedLen int
}{
{
"Valid hex string",
"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d",
false,
32,
},
{
"Invalid hex string",
"not-hex",
true,
0,
},
{
"Empty string",
"",
true,
0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := HexPubkeyToBytes(tt.input)
if tt.expectError && err == nil {
t.Errorf("HexPubkeyToBytes(%q) expected error, got nil", tt.input)
return
}
if !tt.expectError && err != nil {
t.Errorf("HexPubkeyToBytes(%q) unexpected error: %v", tt.input, err)
return
}
if !tt.expectError && len(result) != tt.expectedLen {
t.Errorf("HexPubkeyToBytes(%q) returned %d bytes, want %d", tt.input, len(result), tt.expectedLen)
}
})
}
}