diff --git a/internal/notification/const.go b/internal/notification/const.go index aee69c680..3534ee359 100644 --- a/internal/notification/const.go +++ b/internal/notification/const.go @@ -40,6 +40,10 @@ const ( fmtSMTPDialError = "error dialing the SMTP server: %w" ) -var ( - rfc2822DoubleNewLine = []byte("\r\n\r\n") +const ( + rfc2822NewLine = "\r\n" +) + +var ( + rfc2822DoubleNewLine = []byte(rfc2822NewLine + rfc2822NewLine) ) diff --git a/internal/notification/smtp_util_test.go b/internal/notification/smtp_util_test.go new file mode 100644 index 000000000..62c33fd09 --- /dev/null +++ b/internal/notification/smtp_util_test.go @@ -0,0 +1,97 @@ +package notification + +import ( + "bytes" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/authelia/authelia/v4/internal/utils" +) + +func TestNewMIMECharacteristics(t *testing.T) { + testCases := []struct { + name string + expected MIMECharacteristics + have []byte + }{ + { + "ShouldDetectMessageCharacteristics7Bit", + MIMECharacteristics{}, + createMIMEBytes(false, true, 5, 150), + }, + { + "ShouldDetectMessageCharacteristicsLongLine", + MIMECharacteristics{LongLines: true}, + createMIMEBytes(false, true, 3, 1200), + }, + { + "ShouldDetectMessageCharacteristicsLF", + MIMECharacteristics{LineFeeds: true}, + createMIMEBytes(false, false, 5, 150), + }, + { + "ShouldDetectMessageCharacteristics8Bit", + MIMECharacteristics{Characters8BIT: true}, + createMIMEBytes(true, true, 3, 150), + }, + { + "ShouldDetectMessageCharacteristicsLongLineAndLF", + MIMECharacteristics{true, true, false}, + createMIMEBytes(false, false, 3, 1200), + }, + { + "ShouldDetectMessageCharacteristicsLongLineAnd8Bit", + MIMECharacteristics{true, false, true}, + createMIMEBytes(true, true, 3, 1200), + }, + { + "ShouldDetectMessageCharacteristics8BitAndLF", + MIMECharacteristics{false, true, true}, + createMIMEBytes(true, false, 3, 150), + }, + { + "ShouldDetectMessageCharacteristicsAll", + MIMECharacteristics{true, true, true}, + createMIMEBytes(true, false, 3, 1200), + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + actual := NewMIMECharacteristics(tc.have) + + assert.Equal(t, tc.expected, actual) + }) + } +} + +func createMIMEBytes(include8bit, crlf bool, lines, length int) []byte { + buf := &bytes.Buffer{} + + for i := 0; i < lines; i++ { + for j := 0; j < length/100; j++ { + switch { + case include8bit: + buf.Write(utils.RandomBytes(50, utils.AlphaNumericCharacters, false)) + buf.Write([]byte("£")) + buf.Write(utils.RandomBytes(49, utils.AlphaNumericCharacters, false)) + default: + buf.Write(utils.RandomBytes(100, utils.AlphaNumericCharacters, false)) + } + } + + if n := length % 100; n != 0 { + buf.Write(utils.RandomBytes(n, utils.AlphaNumericCharacters, false)) + } + + switch { + case crlf: + buf.Write([]byte(rfc2822NewLine)) + default: + buf.Write([]byte("\n")) + } + } + + return buf.Bytes() +}