Skip to content

Commit

Permalink
chore: increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
d--j committed Jan 7, 2025
1 parent da1a1d4 commit c6747f8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
22 changes: 22 additions & 0 deletions internal/body/body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,28 @@ func TestBody(t *testing.T) {
_, _ = b.Write([]byte("test"))
t.Errorf("did not panic")
})
t.Run("error on Seek", func(t *testing.T) {
b := getBody(10, []byte("test"))
_, err := b.Seek(-1, io.SeekStart)
if err == nil {
t.Errorf("did not error on seek")
}
})
t.Run("error on switchToReading", func(t *testing.T) {
b := getBody(2, []byte("test"))
_ = b.file.Close()
_, err := b.Seek(0, io.SeekStart)
if err == nil {
t.Errorf("did not error on Seek")
}
b = getBody(2, []byte("test"))
_ = b.file.Close()
var buf [10]byte
_, err = b.Read(buf[:])
if err == nil {
t.Errorf("did not error on Read")
}
})
t.Run("temp file fail", func(t *testing.T) {
tmpdir := os.Getenv("TMPDIR")
tmp := os.Getenv("TMP")
Expand Down
26 changes: 25 additions & 1 deletion mailfilter/testtrx/trx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ import (
"reflect"
"testing"

"github.com/d--j/go-milter/internal/header"
"github.com/d--j/go-milter/mailfilter"
"github.com/d--j/go-milter/mailfilter/addr"
)

func TestTestTrx(t *testing.T) {
t.Parallel()
hdr, err := header.New([]byte("Subject: test\nX-H: 1\n\n"))
if err != nil {
t.Fatal(err)
}
trx := (&Trx{}).SetMTA(mailfilter.MTA{
Version: "Postfix 2.0.0",
FQDN: "mx.example.net",
Expand All @@ -33,9 +38,26 @@ func TestTestTrx(t *testing.T) {
SetQueueId("ABCD").
SetMailFrom(addr.NewMailFrom("root@localhost", "", "local", "", "")).
SetRcptTosList("root@localhost", "postmaster@example.com").
SetHeadersRaw([]byte("Subject: test\n\n")).
SetHeaders(hdr).
SetBodyBytes([]byte("test body"))

if trx.MailFrom() == nil || trx.MailFrom().Addr != "root@localhost" {
t.Errorf("MailFrom expected to be root@localhost, got %v", trx.MailFrom())
}
trx.HeadersEnforceOrder()
if trx.enforceHeaderOrder == true {
t.Errorf("HeadersEnforceOrder expected do nothing")
}
if trx.HasRcptTo("postmaster@example.net") {
t.Errorf("HasRcptTo postmaster@example.net expected false")
}
if len(trx.RcptTos()) != 2 {
t.Errorf("expected 2 RcptTos, got %v", len(trx.RcptTos()))
}
if trx.QueueId() != "ABCD" {
t.Errorf("QueueId expected ABCD, got %v", trx.QueueId())
}

m := trx.Modifications()
if m != nil || len(m) != 0 {
t.Fatalf("trx.Modification() got %v, want <nil>", m)
Expand All @@ -44,6 +66,7 @@ func TestTestTrx(t *testing.T) {
trx.ChangeMailFrom("", "A=B")
trx.DelRcptTo("root@localhost")
trx.AddRcptTo("postmaster@example.com", "A=B")
trx.AddRcptTo("postmaster@example.net", "")
trx.AddRcptTo("", "")
trx.Headers().Add("X-Add", "1")
trx.Headers().SetSubject("")
Expand All @@ -55,6 +78,7 @@ func TestTestTrx(t *testing.T) {
{Kind: DelRcptTo, Addr: "postmaster@example.com"},
{Kind: DelRcptTo, Addr: "root@localhost"},
{Kind: AddRcptTo, Addr: "postmaster@example.com", Args: "A=B"},
{Kind: AddRcptTo, Addr: "postmaster@example.net", Args: ""},
{Kind: AddRcptTo, Addr: "", Args: ""},
{Kind: ChangeHeader, Index: 1, Name: "Subject", Value: ""},
{Kind: InsertHeader, Index: 104, Name: "X-Add", Value: " 1"},
Expand Down

0 comments on commit c6747f8

Please sign in to comment.