1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2026-10-28 15:02:28 +00:00
Files
go-chasquid-smtp/internal/protoio/protoio_test.go
T
Alberto Bertogli 8b19760f4e 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.
2026-10-03 10:34:35 +01:00

136 lines
3.3 KiB
Go

package protoio
import (
"os"
"testing"
"blitiri.com.ar/go/chasquid/internal/protoio/testpb"
)
func TestBin(t *testing.T) {
fname := t.ArtifactDir() + "/f"
pb := &testpb.M{Content: "hola"}
if err := WriteMessage(fname, pb, 0600); err != nil {
t.Error(err)
}
pb2 := &testpb.M{}
if err := ReadMessage(fname, pb2); err != nil {
t.Error(err)
}
if pb.Content != pb2.Content {
t.Errorf("content mismatch, got %q, expected %q", pb2.Content, pb.Content)
}
}
func TestText(t *testing.T) {
fname := t.ArtifactDir() + "/f"
pb := &testpb.M{Content: "hola"}
if err := WriteTextMessage(fname, pb, 0600); err != nil {
t.Error(err)
}
pb2 := &testpb.M{}
if err := ReadTextMessage(fname, pb2); err != nil {
t.Error(err)
}
if pb.Content != pb2.Content {
t.Errorf("content mismatch, got %q, expected %q", pb2.Content, pb.Content)
}
}
func TestStore(t *testing.T) {
dir := t.ArtifactDir()
st, err := NewStore(dir + "/store")
if err != nil {
t.Fatalf("failed to create store: %v", err)
}
if ids, err := st.ListIDs(); len(ids) != 0 || err != nil {
t.Errorf("expected no ids, got %v - %v", ids, err)
}
pb := &testpb.M{Content: "hola"}
if err := st.Put("f", pb); err != nil {
t.Error(err)
}
pb2 := &testpb.M{}
if ok, err := st.Get("f", pb2); err != nil || !ok {
t.Errorf("Get(f): %v - %v", ok, err)
}
if pb.Content != pb2.Content {
t.Errorf("content mismatch, got %q, expected %q", pb2.Content, pb.Content)
}
if ok, err := st.Get("notexists", pb2); err != nil || ok {
t.Errorf("Get(notexists): %v - %v", ok, err)
}
// Add an extraneous file, which ListIDs should ignore.
mustCreate(t, dir+"/store/"+"somefile")
// Add a file that is not properly query-escaped, and should be ignored.
mustCreate(t, dir+"/store/"+"s:somefile%N")
if ids, err := st.ListIDs(); len(ids) != 1 || ids[0] != "f" || err != nil {
t.Errorf("expected [f], got %v - %v", ids, err)
}
}
func mustCreate(t *testing.T, fname string) {
t.Helper()
f, err := os.Create(fname)
if f != nil {
f.Close()
}
if err != nil {
t.Fatalf("failed to create file %q: %v", fname, err)
}
}
func TestFileErrors(t *testing.T) {
pb := &testpb.M{Content: "hola"}
if err := WriteMessage("/proc/doesnotexist", pb, 0600); err == nil {
t.Errorf("write to /proc/doesnotexist worked, expected error")
}
if err := WriteTextMessage("/proc/doesnotexist", pb, 0600); err == nil {
t.Errorf("text write to /proc/doesnotexist worked, expected error")
}
if err := ReadMessage("/doesnotexist", pb); err == nil {
t.Errorf("read from /doesnotexist worked, expected error")
}
if err := ReadTextMessage("/doesnotexist", pb); err == nil {
t.Errorf("text read from /doesnotexist worked, expected error")
}
s := &Store{dir: "/doesnotexist"}
if ids, err := s.ListIDs(); !(ids == nil && err != nil) {
t.Errorf("list /doesnotexist worked (%v, %v), expected error", ids, err)
}
}
func TestMarshalErrors(t *testing.T) {
dir := t.ArtifactDir()
// The marshaller enforces that strings are well-formed utf8. So to create
// a marshalling error, we use a non-utf8 string.
pb := &testpb.M{Content: "\xc3\x28"}
if err := WriteMessage(dir+"/f", pb, 0600); err == nil {
t.Errorf("write worked, expected error")
}
if err := WriteTextMessage(dir+"/ft", pb, 0600); err == nil {
t.Errorf("text write worked, expected error")
}
}