From e9331e0b7c69b8d4d51e1528ceace53305ba48a0 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sun, 5 Nov 2023 19:48:43 +0100 Subject: [PATCH] Add time import and tests for invalid date in email Added `time` import in the eml_test.go and added two new test use-cases: `exampleMailPlainNoEncInvalidDate` and `exampleMailPlainNoEncNoDate`. The `exampleMailPlainNoEncInvalidDate` is used to check if the parser can correctly handle email with invalid date. Meanwhile, `exampleMailPlainNoEncNoDate` checks if the parser can correctly add the current date to an email that didn't specify a date. This will improve the parser's resilience and flexibility in handling various email scenarios. --- eml_test.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/eml_test.go b/eml_test.go index e94d07c..ca58e43 100644 --- a/eml_test.go +++ b/eml_test.go @@ -7,6 +7,7 @@ package mail import ( "strings" "testing" + "time" ) const ( @@ -28,6 +29,51 @@ This is a test mail. Please do not reply to this. Also this line is very long so should be wrapped. +Thank your for your business! +The go-mail team + +-- +This is a signature` + exampleMailPlainNoEncInvalidDate = `Date: Inv, 99 Nov 9999 99:99:00 +0000 +MIME-Version: 1.0 +Message-ID: <1305604950.683004066175.AAAAAAAAaaaaaaaaB@go-mail.dev> +Subject: Example mail // plain text without encoding +User-Agent: go-mail v0.4.0 // https://github.com/wneessen/go-mail +X-Mailer: go-mail v0.4.0 // https://github.com/wneessen/go-mail +From: "Toni Tester" +To: +Cc: +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +Dear Customer, + +This is a test mail. Please do not reply to this. Also this line is very long so it +should be wrapped. + + +Thank your for your business! +The go-mail team + +-- +This is a signature` + exampleMailPlainNoEncNoDate = `MIME-Version: 1.0 +Message-ID: <1305604950.683004066175.AAAAAAAAaaaaaaaaB@go-mail.dev> +Subject: Example mail // plain text without encoding +User-Agent: go-mail v0.4.0 // https://github.com/wneessen/go-mail +X-Mailer: go-mail v0.4.0 // https://github.com/wneessen/go-mail +From: "Toni Tester" +To: +Cc: +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +Dear Customer, + +This is a test mail. Please do not reply to this. Also this line is very long so it +should be wrapped. + + Thank your for your business! The go-mail team @@ -110,3 +156,24 @@ func TestEMLToMsgFromString(t *testing.T) { }) } } + +func TestEMLToMsgFromStringBrokenDate(t *testing.T) { + _, err := EMLToMsgFromString(exampleMailPlainNoEncInvalidDate) + if err == nil { + t.Error("EML with invalid date was supposed to fail, but didn't") + } + now := time.Now() + m, err := EMLToMsgFromString(exampleMailPlainNoEncNoDate) + if err != nil { + t.Errorf("EML with no date parsing failed: %s", err) + } + da := m.GetGenHeader(HeaderDate) + if len(da) < 1 { + t.Error("EML with no date expected current date, but got nothing") + return + } + d := da[0] + if d != now.Format(time.RFC1123Z) { + t.Errorf("EML with no date expected: %s, got: %s", now.Format(time.RFC1123Z), d) + } +}