1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
package tls
import (
"testing"
)
func TestSortAddressesByFamily(t *testing.T) {
tests := []struct {
name string
input []string
expected []string
}{
{
name: "IPv4 only",
input: []string{"192.168.1.1", "10.0.0.1"},
expected: []string{"192.168.1.1", "10.0.0.1"},
},
{
name: "IPv6 only",
input: []string{"2001:db8::1", "2001:db8::2"},
expected: []string{"2001:db8::1", "2001:db8::2"},
},
{
name: "Mixed - IPv4 first in input",
input: []string{"192.168.1.1", "2001:db8::1", "10.0.0.1", "2001:db8::2"},
expected: []string{"2001:db8::1", "2001:db8::2", "192.168.1.1", "10.0.0.1"},
},
{
name: "Mixed - IPv6 first in input",
input: []string{"2001:db8::1", "192.168.1.1", "2001:db8::2", "10.0.0.1"},
expected: []string{"2001:db8::1", "2001:db8::2", "192.168.1.1", "10.0.0.1"},
},
{
name: "Empty input",
input: []string{},
expected: []string{},
},
{
name: "Single IPv4",
input: []string{"192.168.1.1"},
expected: []string{"192.168.1.1"},
},
{
name: "Single IPv6",
input: []string{"2001:db8::1"},
expected: []string{"2001:db8::1"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := sortAddressesByFamily(tt.input)
if len(result) != len(tt.expected) {
t.Errorf("length mismatch: got %d, expected %d", len(result), len(tt.expected))
return
}
for i, addr := range result {
if addr != tt.expected[i] {
t.Errorf("address mismatch at index %d: got %s, expected %s", i, addr, tt.expected[i])
}
}
})
}
}
|