106 lines
3 KiB
Go
106 lines
3 KiB
Go
package contract
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
sdk "git.happydns.org/checker-sdk-go/checker"
|
|
)
|
|
|
|
func TestNewEntry_RoundTrip(t *testing.T) {
|
|
in := TLSEndpoint{
|
|
Host: "jabber.example.net",
|
|
Port: 5222,
|
|
SNI: "example.net",
|
|
STARTTLS: "xmpp-client",
|
|
RequireSTARTTLS: true,
|
|
}
|
|
e, err := NewEntry(in)
|
|
if err != nil {
|
|
t.Fatalf("NewEntry: %v", err)
|
|
}
|
|
if e.Type != Type {
|
|
t.Errorf("Type = %q, want %q", e.Type, Type)
|
|
}
|
|
if e.Ref == "" {
|
|
t.Error("Ref is empty")
|
|
}
|
|
|
|
out, err := ParseEntry(e)
|
|
if err != nil {
|
|
t.Fatalf("ParseEntry: %v", err)
|
|
}
|
|
if out != in {
|
|
t.Errorf("round-trip mismatch:\n got %+v\nwant %+v", out, in)
|
|
}
|
|
}
|
|
|
|
func TestRef_StableAcrossCalls(t *testing.T) {
|
|
ep1 := TLSEndpoint{Host: "a", Port: 443, STARTTLS: "smtp"}
|
|
ep2 := TLSEndpoint{Host: "a", Port: 443, STARTTLS: "smtp"}
|
|
if Ref(ep1) != Ref(ep2) {
|
|
t.Error("Ref is not deterministic")
|
|
}
|
|
}
|
|
|
|
func TestRef_SNIDefaultsToHost(t *testing.T) {
|
|
a := TLSEndpoint{Host: "example.net", Port: 443}
|
|
b := TLSEndpoint{Host: "example.net", Port: 443, SNI: "example.net"}
|
|
if Ref(a) != Ref(b) {
|
|
t.Errorf("Ref differs when SNI is omitted vs. explicit but equal: %s vs %s", Ref(a), Ref(b))
|
|
}
|
|
}
|
|
|
|
func TestRef_FieldsAffectOutput(t *testing.T) {
|
|
base := TLSEndpoint{Host: "example.net", Port: 443}
|
|
cases := []TLSEndpoint{
|
|
{Host: "other.net", Port: 443},
|
|
{Host: "example.net", Port: 8443},
|
|
{Host: "example.net", Port: 443, SNI: "alt.example.net"},
|
|
{Host: "example.net", Port: 443, STARTTLS: "smtp"},
|
|
{Host: "example.net", Port: 443, STARTTLS: "smtp", RequireSTARTTLS: true},
|
|
}
|
|
baseRef := Ref(base)
|
|
for i, c := range cases {
|
|
if Ref(c) == baseRef {
|
|
t.Errorf("case %d: expected Ref to change when a significant field changes, got %q", i, baseRef)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseEntry_WrongType(t *testing.T) {
|
|
e := sdk.DiscoveryEntry{Type: "something.else.v1", Ref: "x", Payload: json.RawMessage(`{}`)}
|
|
if _, err := ParseEntry(e); err == nil {
|
|
t.Error("expected error on wrong type, got nil")
|
|
}
|
|
}
|
|
|
|
func TestParseEntry_BadPayload(t *testing.T) {
|
|
e := sdk.DiscoveryEntry{Type: Type, Ref: "x", Payload: json.RawMessage(`not json`)}
|
|
if _, err := ParseEntry(e); err == nil {
|
|
t.Error("expected error on malformed payload, got nil")
|
|
}
|
|
}
|
|
|
|
func TestParseEntries_FiltersAndAccumulatesWarnings(t *testing.T) {
|
|
good, err := NewEntry(TLSEndpoint{Host: "a", Port: 443})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bad := sdk.DiscoveryEntry{Type: Type, Ref: "bad", Payload: json.RawMessage(`not json`)}
|
|
foreign := sdk.DiscoveryEntry{Type: "other.v1", Ref: "f", Payload: json.RawMessage(`{}`)}
|
|
|
|
entries, warnings := ParseEntries([]sdk.DiscoveryEntry{good, bad, foreign})
|
|
if len(entries) != 1 {
|
|
t.Errorf("entries = %d, want 1", len(entries))
|
|
}
|
|
if entries[0].Endpoint.Host != "a" {
|
|
t.Errorf("decoded host = %q, want %q", entries[0].Endpoint.Host, "a")
|
|
}
|
|
if entries[0].Ref != good.Ref {
|
|
t.Errorf("preserved ref = %q, want %q", entries[0].Ref, good.Ref)
|
|
}
|
|
if len(warnings) != 1 {
|
|
t.Errorf("warnings = %d, want 1 (one malformed payload of the correct type)", len(warnings))
|
|
}
|
|
}
|