Skip to content

Commit

Permalink
Initialize genHeader in RequestMDNTo method
Browse files Browse the repository at this point in the history
Add checks to initialize `genHeader` if it is nil in the RequestMDNTo method to prevent potential nil map assignment errors. The newly implemented tests showed that this method was never actually working and the old test was inefficient to identify this.
  • Loading branch information
wneessen committed Oct 26, 2024
1 parent 9e51dba commit 4f97cd8
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
7 changes: 4 additions & 3 deletions msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,9 @@ func (m *Msg) IsDelivered() bool {
// References:
// - https://datatracker.ietf.org/doc/html/rfc8098
func (m *Msg) RequestMDNTo(rcpts ...string) error {
if m.genHeader == nil {
m.genHeader = make(map[Header][]string)
}
var addresses []string
for _, addrVal := range rcpts {
address, err := mail.ParseAddress(addrVal)
Expand All @@ -1195,9 +1198,7 @@ func (m *Msg) RequestMDNTo(rcpts ...string) error {
}
addresses = append(addresses, address.String())
}
if _, ok := m.genHeader[HeaderDispositionNotificationTo]; ok {
m.genHeader[HeaderDispositionNotificationTo] = addresses
}
m.genHeader[HeaderDispositionNotificationTo] = addresses
return nil
}

Expand Down
70 changes: 70 additions & 0 deletions msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1915,6 +1915,76 @@ func TestMsg_IsDelivered(t *testing.T) {
})
}

func TestMsg_RequestMDNTo(t *testing.T) {
t.Run("RequestMDNTo with valid address", func(t *testing.T) {
message := NewMsg()
if message == nil {
t.Fatal("message is nil")
}
if err := message.RequestMDNTo("[email protected]"); err != nil {
t.Fatalf("failed to set RequestMDNTo: %s", err)
}
checkGenHeader(t, message, HeaderDispositionNotificationTo, "RequestMDNTo", 0, 1, "<[email protected]>")
})
t.Run("RequestMDNTo with valid address and nil-genHeader", func(t *testing.T) {
message := NewMsg()
if message == nil {
t.Fatal("message is nil")
}
message.genHeader = nil
if err := message.RequestMDNTo("[email protected]"); err != nil {
t.Fatalf("failed to set RequestMDNTo: %s", err)
}
checkGenHeader(t, message, HeaderDispositionNotificationTo, "RequestMDNTo", 0, 1, "<[email protected]>")
})
t.Run("RequestMDNTo with multiple valid addresses", func(t *testing.T) {
message := NewMsg()
if message == nil {
t.Fatal("message is nil")
}
if err := message.RequestMDNTo("[email protected]", "[email protected]"); err != nil {
t.Fatalf("failed to set RequestMDNTo: %s", err)
}
checkGenHeader(t, message, HeaderDispositionNotificationTo, "RequestMDNTo", 0, 2, "<[email protected]>")
checkGenHeader(t, message, HeaderDispositionNotificationTo, "RequestMDNTo", 1, 2, "<[email protected]>")
})
t.Run("RequestMDNTo with invalid address", func(t *testing.T) {
message := NewMsg()
if message == nil {
t.Fatal("message is nil")
}
if err := message.RequestMDNTo("invalid"); err == nil {
t.Fatalf("RequestMDNTo should fail with invalid address")
}
})
t.Run("RequestMDNTo with empty string should fail", func(t *testing.T) {
message := NewMsg()
if message == nil {
t.Fatal("message is nil")
}
if err := message.RequestMDNTo(""); err == nil {
t.Fatalf("RequestMDNTo should fail with invalid address")
}
})
t.Run("RequestMDNTo with different RFC5322 addresses", func(t *testing.T) {
message := NewMsg()
if message == nil {
t.Fatal("message is nil")
}
for _, tt := range rfc5322Test {
t.Run(tt.value, func(t *testing.T) {
err := message.RequestMDNTo(tt.value)
if err != nil && tt.valid {
t.Errorf("RequestMDNTo on address %s should succeed, but failed with: %s", tt.value, err)
}
if err == nil && !tt.valid {
t.Errorf("RequestMDNTo on address %s should fail, but succeeded", tt.value)
}
})
}
})
}

// checkAddrHeader verifies the correctness of an AddrHeader in a Msg based on the provided criteria.
// It checks whether the AddrHeader contains the correct address, name, and number of fields.
func checkAddrHeader(t *testing.T, message *Msg, header AddrHeader, fn string, field, wantFields int,
Expand Down

0 comments on commit 4f97cd8

Please sign in to comment.