Gitfed
bastien-mrq/gitfed / cmd / gitfed-install / netutil_test.go
package main

import (
	"context"
	"testing"
)

func TestCheckDNSResolvedAndMatches(t *testing.T) {
	ctx := context.Background()
	out := checkDNS(ctx, "localhost", "127.0.0.1")
	if !out.Resolved {
		t.Fatalf("expected localhost to resolve, err=%v", out.Err)
	}
	if !out.Matches {
		t.Errorf("expected 127.0.0.1 to be among localhost's addresses: %v", out.Addresses)
	}
}

func TestCheckDNSMismatch(t *testing.T) {
	ctx := context.Background()
	out := checkDNS(ctx, "localhost", "203.0.113.99") // TEST-NET-3, never a real answer
	if !out.Resolved {
		t.Fatalf("expected localhost to resolve, err=%v", out.Err)
	}
	if out.Matches {
		t.Errorf("203.0.113.99 should never be among localhost's addresses: %v", out.Addresses)
	}
}

func TestCheckDNSUnresolvable(t *testing.T) {
	ctx := context.Background()
	out := checkDNS(ctx, "this-domain-should-not-exist.invalid", "1.2.3.4")
	if out.Resolved {
		t.Errorf("expected an .invalid domain to fail resolution, got addresses: %v", out.Addresses)
	}
	if out.Err == nil {
		t.Error("expected a non-nil Err for an unresolvable domain")
	}
}