1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2026-10-29 15:02:30 +00:00

test: Use modern testing features instead of custom helpers

Many of our tests rely on helpers that predate features which are now
available in the standard library.

This patch replaces them with the standard equivalents:

- MustTempDir/RemoveIfOk -> t.ArtifactDir() or t.TempDir() (depending on
  whether it is useful to keep the data on request).
- GetFreePort + waitForServer -> opening the listeners before starting
  the servers. To support this, localrpc.Server gains a Serve method. We
  may re-introduce these in the future if more use cases appear.
- WaitFor and real-time timing checks -> testing/synctest.
- context.Background() -> t.Context().
- Benchmark b.N loops -> b.Loop().
- reflect.DeepEqual -> cmp.Diff, and proto.Equal for protobufs.
This commit is contained in:
Alberto Bertogli
2026-10-03 10:34:35 +01:00
parent 8d763f450d
commit 8b19760f4e
30 changed files with 495 additions and 763 deletions
+5 -5
View File
@@ -10,7 +10,7 @@ import (
func TestTraceNoCtx(t *testing.T) {
// Call trace() on a context without a trace function, to check it doesn't
// panic.
ctx := context.Background()
ctx := t.Context()
trace(ctx, "test")
}
@@ -19,7 +19,7 @@ func TestTrace(t *testing.T) {
traceF := func(f string, a ...any) {
s = fmt.Sprintf(f, a...)
}
ctx := WithTraceFunc(context.Background(), traceF)
ctx := WithTraceFunc(t.Context(), traceF)
trace(ctx, "test %d", 1)
if s != "test 1" {
t.Errorf("trace function not called")
@@ -32,7 +32,7 @@ func TestLookupTXTNoCtx(t *testing.T) {
// We just check there is a reasonable error.
// We don't specifically check that it's NXDOMAIN because if we don't have
// internet access, the error may be different.
ctx := context.Background()
ctx := t.Context()
_, err := lookupTXT(ctx, "does.not.exist.example.com")
if _, ok := err.(*net.DNSError); !ok {
t.Fatalf("expected *net.DNSError, got %T", err)
@@ -45,7 +45,7 @@ func TestLookupTXT(t *testing.T) {
called = true
return nil, nil
}
ctx := WithLookupTXTFunc(context.Background(), lookupTXTF)
ctx := WithLookupTXTFunc(t.Context(), lookupTXTF)
lookupTXT(ctx, "example.com")
if !called {
t.Errorf("lookupTXT function not called")
@@ -54,7 +54,7 @@ func TestLookupTXT(t *testing.T) {
func TestMaxHeaders(t *testing.T) {
// First without an override, check we return the default.
ctx := context.Background()
ctx := t.Context()
if m := maxHeaders(ctx); m != 5 {
t.Errorf("expected 5, got %d", m)
}
+2 -2
View File
@@ -16,7 +16,7 @@ func TestLookupError(t *testing.T) {
errLookupF := func(ctx context.Context, name string) ([]string, error) {
return nil, testErr
}
ctx := WithLookupTXTFunc(context.Background(), errLookupF)
ctx := WithLookupTXTFunc(t.Context(), errLookupF)
pks, err := findPublicKeys(ctx, "example.com", "selector")
if pks != nil || err != testErr {
@@ -50,7 +50,7 @@ func testLookupTXT(ctx context.Context, name string) ([]string, error) {
}
func TestSkipBadRecords(t *testing.T) {
ctx := WithLookupTXTFunc(context.Background(), testLookupTXT)
ctx := WithLookupTXTFunc(t.Context(), testLookupTXT)
results["selector._domainkey.example.com"] = []string{
"not a tag",
"v=DKIM1; p=" + exampleRSAKeyB64,
+1 -1
View File
@@ -44,7 +44,7 @@ func TestFromPrivateFiles(t *testing.T) {
}
func testOne(t *testing.T, base string) {
ctx := context.Background()
ctx := t.Context()
ctx = WithTraceFunc(ctx, t.Logf)
ctx = loadDNS(t, ctx, base+".dns")
+4 -5
View File
@@ -1,7 +1,6 @@
package dkim
import (
"context"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
@@ -33,7 +32,7 @@ Joe.
`)
func TestSignRSA(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
ctx = WithTraceFunc(ctx, t.Logf)
// Generate a new key pair.
@@ -84,7 +83,7 @@ func TestSignRSA(t *testing.T) {
}
func TestSignEd25519(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
ctx = WithTraceFunc(ctx, t.Logf)
// Generate a new key pair.
@@ -141,7 +140,7 @@ func TestSignBadMessage(t *testing.T) {
Domain: "example.com",
Selector: "test",
}
_, err := s.Sign(context.Background(), "Bad message")
_, err := s.Sign(t.Context(), "Bad message")
if err == nil {
t.Errorf("Sign: wanted error; got nil")
}
@@ -159,7 +158,7 @@ func TestSignBadAlgorithm(t *testing.T) {
}
s.Signer = priv
_, err = s.Sign(context.Background(), basicMessage)
_, err = s.Sign(t.Context(), basicMessage)
if !errors.Is(err, errUnsupportedKeyType) {
t.Errorf("Sign: wanted unsupported key type; got %v", err)
}
+2 -2
View File
@@ -20,7 +20,7 @@ func makeLookupTXT(results map[string][]string) lookupTXTFunc {
}
func TestVerifyRF6376CExample(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
ctx = WithTraceFunc(ctx, t.Logf)
// Use the public key from the example in RFC 6376 appendix C.
@@ -88,7 +88,7 @@ Joe.
}
func TestVerifyRFC8463Example(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
ctx = WithTraceFunc(ctx, t.Logf)
// Use the public keys from the example in RFC 8463 appendix A.2.