network/pkg/rqlite/cluster_discovery_test.go
anonpenguin23 a9844a1451 feat: add unit tests for gateway authentication and RQLite utilities
- Introduced comprehensive unit tests for the authentication service in the gateway, covering JWT generation, Base58 decoding, and signature verification for Ethereum and Solana.
- Added tests for RQLite cluster discovery functions, including host replacement logic and public IP validation.
- Implemented tests for RQLite utility functions, focusing on exponential backoff and data directory path resolution.
- Enhanced serverless engine tests to validate timeout handling and memory limits for WASM functions.
2025-12-31 12:26:31 +02:00

98 lines
2.4 KiB
Go

package rqlite
import (
"testing"
"github.com/DeBrosOfficial/network/pkg/discovery"
)
func TestShouldReplaceHost(t *testing.T) {
tests := []struct {
host string
expected bool
}{
{"", true},
{"localhost", true},
{"127.0.0.1", true},
{"::1", true},
{"0.0.0.0", true},
{"1.1.1.1", false},
{"8.8.8.8", false},
{"example.com", false},
}
for _, tt := range tests {
if got := shouldReplaceHost(tt.host); got != tt.expected {
t.Errorf("shouldReplaceHost(%s) = %v; want %v", tt.host, got, tt.expected)
}
}
}
func TestIsPublicIP(t *testing.T) {
tests := []struct {
ip string
expected bool
}{
{"127.0.0.1", false},
{"192.168.1.1", false},
{"10.0.0.1", false},
{"172.16.0.1", false},
{"1.1.1.1", true},
{"8.8.8.8", true},
{"2001:4860:4860::8888", true},
}
for _, tt := range tests {
if got := isPublicIP(tt.ip); got != tt.expected {
t.Errorf("isPublicIP(%s) = %v; want %v", tt.ip, got, tt.expected)
}
}
}
func TestReplaceAddressHost(t *testing.T) {
tests := []struct {
address string
newHost string
expected string
replaced bool
}{
{"localhost:4001", "1.1.1.1", "1.1.1.1:4001", true},
{"127.0.0.1:4001", "1.1.1.1", "1.1.1.1:4001", true},
{"8.8.8.8:4001", "1.1.1.1", "8.8.8.8:4001", false}, // Don't replace public IP
{"invalid", "1.1.1.1", "invalid", false},
}
for _, tt := range tests {
got, replaced := replaceAddressHost(tt.address, tt.newHost)
if got != tt.expected || replaced != tt.replaced {
t.Errorf("replaceAddressHost(%s, %s) = %s, %v; want %s, %v", tt.address, tt.newHost, got, replaced, tt.expected, tt.replaced)
}
}
}
func TestRewriteAdvertisedAddresses(t *testing.T) {
meta := &discovery.RQLiteNodeMetadata{
NodeID: "localhost:4001",
RaftAddress: "localhost:4001",
HTTPAddress: "localhost:4002",
}
changed, originalNodeID := rewriteAdvertisedAddresses(meta, "1.1.1.1", true)
if !changed {
t.Error("expected changed to be true")
}
if originalNodeID != "localhost:4001" {
t.Errorf("expected originalNodeID localhost:4001, got %s", originalNodeID)
}
if meta.RaftAddress != "1.1.1.1:4001" {
t.Errorf("expected RaftAddress 1.1.1.1:4001, got %s", meta.RaftAddress)
}
if meta.HTTPAddress != "1.1.1.1:4002" {
t.Errorf("expected HTTPAddress 1.1.1.1:4002, got %s", meta.HTTPAddress)
}
if meta.NodeID != "1.1.1.1:4001" {
t.Errorf("expected NodeID 1.1.1.1:4001, got %s", meta.NodeID)
}
}