package main import "testing" func validTestFingerprint() string { certDER, _, _ := generateHubCert() return certFingerprint(certDER) } func TestInviteRoundtrip(t *testing.T) { fp := validTestFingerprint() want := inviteCode{Host: "100.101.102.103", Port: "47823", Fingerprint: fp, Token: "sekrit"} got, err := decodeInvite(encodeInvite(want)) if err != nil { t.Fatal(err) } if got != want { t.Fatalf("roundtrip mismatch: got %+v, want %+v", got, want) } } func TestDecodeInviteRejectsMalformed(t *testing.T) { fp := validTestFingerprint() cases := map[string]string{ "not base64 at all": "!!! not base64 !!!", "base64 but not json": "aGVsbG8gd29ybGQ", // "hello world" "missing token": encodeInvite(inviteCode{Host: "h", Port: "1", Fingerprint: fp}), "missing host": encodeInvite(inviteCode{Port: "1", Fingerprint: fp, Token: "t"}), "bad fingerprint": encodeInvite(inviteCode{Host: "h", Port: "1", Fingerprint: "not-a-fingerprint", Token: "t"}), } for name, code := range cases { if _, err := decodeInvite(code); err == nil { t.Errorf("%s: expected an error, got none", name) } } } func TestPickBestHost(t *testing.T) { cases := []struct { name string addrs []string want string }{ {"tailscale preferred over LAN", []string{"192.168.1.5", "100.101.102.103"}, "100.101.102.103"}, {"LAN only", []string{"192.168.1.5"}, "192.168.1.5"}, {"nothing usable", []string{"8.8.8.8"}, ""}, {"empty", nil, ""}, } for _, c := range cases { if got := pickBestHost(c.addrs); got != c.want { t.Errorf("%s: pickBestHost(%v) = %q, want %q", c.name, c.addrs, got, c.want) } } }