62 lines
2.0 KiB
Go
62 lines
2.0 KiB
Go
package discovery
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/mdns"
|
|
)
|
|
|
|
func service(ip string) *mdns.ServiceEntry {
|
|
return &mdns.ServiceEntry{AddrV4: net.ParseIP(ip), Host: "server.local.", Port: 8083,
|
|
InfoFields: []string{"name=Linux desktop", "path=/connect", "tls=0", "auth=password", "v=release"}}
|
|
}
|
|
|
|
func TestBrowseCollectsDeduplicatedValidAdvertisements(t *testing.T) {
|
|
values, err := browse(context.Background(), func(ctx context.Context, p *mdns.QueryParam) error {
|
|
if p.Service != ServiceType || p.Timeout.Seconds() > 4 {
|
|
t.Fatal("unbounded or wrong service query")
|
|
}
|
|
for _, entry := range []*mdns.ServiceEntry{service("192.168.1.3"), service("192.168.1.2"), service("192.168.1.3"), service("127.0.0.1"), nil} {
|
|
p.Entries <- entry
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil || len(values) != 2 {
|
|
t.Fatalf("values=%+v err=%v", values, err)
|
|
}
|
|
if values[0].Endpoint != "ws://192.168.1.2:8083/connect" || values[0].Name != "Linux desktop" || values[0].Auth != "password" {
|
|
t.Fatalf("wrong record: %+v", values[0])
|
|
}
|
|
}
|
|
|
|
func TestBrowseHonorsCancellation(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
_, err := browse(ctx, func(ctx context.Context, p *mdns.QueryParam) error { <-ctx.Done(); return ctx.Err() })
|
|
if err == nil {
|
|
t.Fatal("canceled browse must fail")
|
|
}
|
|
}
|
|
|
|
func TestDiscoveryRejectsMalformedAndUnsafeAdvertisements(t *testing.T) {
|
|
for _, ip := range []string{"0.0.0.0", "127.0.0.1", "224.0.0.1", "169.254.3.1", "::", "::1", "fe80::1"} {
|
|
if _, ok := discovered(service(ip)); ok {
|
|
t.Fatalf("accepted address %s", ip)
|
|
}
|
|
}
|
|
for _, fields := range [][]string{{"path=//evil/connect", "auth=password", "tls=0"}, {"path=/connect", "auth=unknown", "tls=0"}, {"path=/connect", "auth=password", "tls=garbage"}} {
|
|
item := service("192.168.1.2")
|
|
item.InfoFields = fields
|
|
if _, ok := discovered(item); ok {
|
|
t.Fatalf("accepted invalid TXT %+v", fields)
|
|
}
|
|
}
|
|
item := service("192.168.1.2")
|
|
item.Port = 65536
|
|
if _, ok := discovered(item); ok {
|
|
t.Fatal("accepted invalid port")
|
|
}
|
|
}
|