mirror of
https://blitiri.com.ar/repos/chasquid
synced 2026-10-29 15:02:30 +00:00
8b19760f4e
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.
72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
|
|
"blitiri.com.ar/go/chasquid/internal/testlib"
|
|
)
|
|
|
|
func mustMkdir(t *testing.T, path string) string {
|
|
t.Helper()
|
|
err := os.Mkdir(path, 0700)
|
|
if err != nil {
|
|
t.Fatalf("failed to create directory: %v", err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
func TestFindCertKey(t *testing.T) {
|
|
tmpDir := t.ArtifactDir()
|
|
|
|
cases := []struct {
|
|
name string
|
|
files []string
|
|
want string
|
|
}{
|
|
{"certbot", []string{"privkey.pem"}, "privkey.pem"},
|
|
{"lego", []string{"key.pem"}, "key.pem"},
|
|
{"both", []string{"privkey.pem", "key.pem"}, "privkey.pem"},
|
|
}
|
|
for _, c := range cases {
|
|
dir := mustMkdir(t, filepath.Join(tmpDir, c.name))
|
|
for _, f := range c.files {
|
|
testlib.Rewrite(t, filepath.Join(dir, f), "")
|
|
}
|
|
|
|
got, err := findCertKey(dir)
|
|
if err != nil {
|
|
t.Errorf("%s: unexpected error: %v", c.name, err)
|
|
}
|
|
if want := filepath.Join(dir, c.want); got != want {
|
|
t.Errorf("%s: got %q, expected %q", c.name, got, want)
|
|
}
|
|
}
|
|
|
|
// No key files: the error should mention the directory.
|
|
dir := mustMkdir(t, filepath.Join(tmpDir, "empty"))
|
|
got, err := findCertKey(dir)
|
|
if err == nil || !strings.Contains(err.Error(), dir) {
|
|
t.Errorf("empty: got %q, %v, expected 'no private key' error",
|
|
got, err)
|
|
}
|
|
|
|
// privkey.pem is there but can't be stat'ed (a symlink loop here, which
|
|
// also works when running as root). We expect that error to be returned,
|
|
// instead of silently falling back to key.pem.
|
|
dir = mustMkdir(t, filepath.Join(tmpDir, "loop"))
|
|
err = os.Symlink("privkey.pem", filepath.Join(dir, "privkey.pem"))
|
|
if err != nil {
|
|
t.Fatalf("failed to create symlink: %v", err)
|
|
}
|
|
testlib.Rewrite(t, filepath.Join(dir, "key.pem"), "")
|
|
got, err = findCertKey(dir)
|
|
if !errors.Is(err, syscall.ELOOP) {
|
|
t.Errorf("loop: got %q, %v, expected ELOOP error", got, err)
|
|
}
|
|
}
|