Files
realy/dns/nip05_additional_test.go

208 lines
5.3 KiB
Go

package dns
import (
"bytes"
"testing"
)
func TestIsValidIdentifier(t *testing.T) {
tests := []struct {
name string
input string
expected bool
}{
{"Valid with name", "user@example.com", true},
{"Valid domain only", "example.com", true},
{"Valid with subdomain", "user@sub.example.com", true},
{"Valid with plus", "user+tag@example.com", true},
{"Valid with underscore", "user_name@example.com", true},
{"Invalid no domain", "user@", false},
{"Invalid special chars", "user!@example.com", false},
{"Invalid format", "not-an-email", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := IsValidIdentifier(tt.input)
if result != tt.expected {
t.Errorf("IsValidIdentifier(%q) = %v, want %v", tt.input, result, tt.expected)
}
})
}
}
func TestNormalizeIdentifier(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"Already normalized", "example.com", "example.com"},
{"With underscore prefix", "_@example.com", "example.com"},
{"With name", "user@example.com", "user@example.com"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := NormalizeIdentifier(tt.input)
if result != tt.expected {
t.Errorf("NormalizeIdentifier(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
func TestStringSliceToByteSlice(t *testing.T) {
tests := []struct {
name string
input []string
expected [][]byte
}{
{
"Empty slice",
[]string{},
[][]byte{},
},
{
"Single item",
[]string{"test"},
[][]byte{[]byte("test")},
},
{
"Multiple items",
[]string{"test1", "test2", "test3"},
[][]byte{[]byte("test1"), []byte("test2"), []byte("test3")},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := StringSliceToByteSlice(tt.input)
if len(result) != len(tt.expected) {
t.Fatalf("StringSliceToByteSlice(%v) returned slice of length %d, want %d",
tt.input, len(result), len(tt.expected))
}
for i, v := range result {
if !bytes.Equal(v, tt.expected[i]) {
t.Errorf("StringSliceToByteSlice(%v)[%d] = %v, want %v",
tt.input, i, v, tt.expected[i])
}
}
})
}
}
func TestParseIdentifierEdgeCases(t *testing.T) {
tests := []struct {
name string
input string
expectedName string
expectedDomain string
expectError bool
}{
{"Empty string", "", "", "", true},
{"Just @", "@", "", "", true},
{"Multiple @", "user@domain@example.com", "", "", true},
{"Invalid domain", "user@invalid", "", "", true},
{"Valid with hyphen", "user-name@example.com", "user-name", "example.com", false},
{"Valid with numbers", "user123@example123.com", "user123", "example123.com", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
name, domain, err := ParseIdentifier(tt.input)
if tt.expectError && err == nil {
t.Errorf("ParseIdentifier(%q) expected error, got nil", tt.input)
return
}
if !tt.expectError && err != nil {
t.Errorf("ParseIdentifier(%q) unexpected error: %v", tt.input, err)
return
}
if !tt.expectError {
if name != tt.expectedName {
t.Errorf("ParseIdentifier(%q) name = %q, want %q", tt.input, name, tt.expectedName)
}
if domain != tt.expectedDomain {
t.Errorf("ParseIdentifier(%q) domain = %q, want %q", tt.input, domain, tt.expectedDomain)
}
}
})
}
}
func TestNewWellKnownResponse(t *testing.T) {
resp := NewWellKnownResponse()
if resp == nil {
t.Fatal("NewWellKnownResponse() returned nil")
}
if resp.Names == nil {
t.Error("NewWellKnownResponse() Names map is nil")
}
if resp.Relays == nil {
t.Error("NewWellKnownResponse() Relays map is nil")
}
if resp.NIP46 == nil {
t.Error("NewWellKnownResponse() NIP46 map is nil")
}
// Test that we can add entries to the maps
resp.Names["test"] = "pubkey"
resp.Relays["pubkey"] = []string{"relay1", "relay2"}
resp.NIP46["pubkey"] = []string{"nip46url"}
if resp.Names["test"] != "pubkey" {
t.Error("Failed to add entry to Names map")
}
if len(resp.Relays["pubkey"]) != 2 {
t.Error("Failed to add entry to Relays map")
}
if len(resp.NIP46["pubkey"]) != 1 {
t.Error("Failed to add entry to NIP46 map")
}
}
func TestRegexPattern(t *testing.T) {
// Test the Nip05Regex pattern directly
tests := []struct {
name string
input string
expected bool
}{
{"Valid email format", "user@example.com", true},
{"Valid domain only", "example.com", true},
{"Valid subdomain", "user@sub.example.com", true},
{"Valid with underscore", "user_name@example.com", true},
{"Valid with hyphen", "user-name@example.com", true},
{"Valid with plus", "user+tag@example.com", true},
{"Valid with numbers", "user123@example123.com", true},
{"Invalid no TLD", "user@localhost", false},
{"Valid IP address", "user@127.0.0.1", true},
{"Invalid with port", "user@example.com:8080", false},
{"Invalid special chars", "user!@example.com", false},
{"Invalid multiple @", "user@domain@example.com", false},
{"Empty string", "", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := Nip05Regex.MatchString(tt.input)
if result != tt.expected {
t.Errorf("Nip05Regex.MatchString(%q) = %v, want %v", tt.input, result, tt.expected)
}
})
}
}