2023-01-15 16:14:19 +01:00
|
|
|
// SPDX-FileCopyrightText: 2022-2023 The go-mail Authors
|
2022-06-17 15:05:54 +02:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2024-10-27 20:46:16 +01:00
|
|
|
//go:build linux || freebsd
|
|
|
|
// +build linux freebsd
|
2022-03-18 23:10:53 +01:00
|
|
|
|
|
|
|
package mail
|
|
|
|
|
2022-03-18 23:23:59 +01:00
|
|
|
import (
|
2024-10-27 15:54:39 +01:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2022-09-08 15:08:17 +02:00
|
|
|
"os"
|
2022-03-18 23:23:59 +01:00
|
|
|
"testing"
|
|
|
|
)
|
2022-03-18 23:10:53 +01:00
|
|
|
|
2024-10-27 15:54:39 +01:00
|
|
|
func TestMsg_AttachFile_unixOnly(t *testing.T) {
|
|
|
|
t.Run("AttachFile with fileFromFS fails on open", func(t *testing.T) {
|
2024-10-27 20:59:24 +01:00
|
|
|
if os.Getenv("PERFORM_UNIX_OPEN_WRITE_TESTS") != "true" {
|
|
|
|
t.Skipf("PERFORM_UNIX_OPEN_WRITE_TESTS variable is not set. Skipping unix open/write tests")
|
|
|
|
}
|
|
|
|
|
2024-10-28 15:42:20 +01:00
|
|
|
tempFile, err := os.CreateTemp("", "attachfile-open-write-test.*.txt")
|
2024-10-27 20:59:24 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create temp file: %s", err)
|
|
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
|
|
if err := os.Remove(tempFile.Name()); err != nil {
|
|
|
|
t.Errorf("failed to remove temp file: %s", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if err = os.Chmod(tempFile.Name(), 0o000); err != nil {
|
|
|
|
t.Fatalf("failed to chmod temp file: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-10-27 15:54:39 +01:00
|
|
|
message := NewMsg()
|
|
|
|
if message == nil {
|
|
|
|
t.Fatal("message is nil")
|
|
|
|
}
|
2024-10-27 20:59:24 +01:00
|
|
|
message.AttachFile(tempFile.Name())
|
2024-10-27 15:54:39 +01:00
|
|
|
attachments := message.GetAttachments()
|
|
|
|
if len(attachments) != 1 {
|
|
|
|
t.Fatalf("failed to get attachments, expected 1, got: %d", len(attachments))
|
|
|
|
}
|
|
|
|
messageBuf := bytes.NewBuffer(nil)
|
2024-10-27 20:59:24 +01:00
|
|
|
_, err = attachments[0].Writer(messageBuf)
|
2024-10-27 15:54:39 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Error("writer func expected to fail, but didn't")
|
|
|
|
}
|
|
|
|
if !errors.Is(err, os.ErrPermission) {
|
|
|
|
t.Errorf("expected error to be %s, got: %s", os.ErrPermission, err)
|
|
|
|
}
|
|
|
|
})
|
2024-10-28 09:34:05 +01:00
|
|
|
}
|
|
|
|
|
2024-10-28 12:01:31 +01:00
|
|
|
func TestMsg_EmbedFile_unixOnly(t *testing.T) {
|
|
|
|
t.Run("EmbedFile with fileFromFS fails on open", func(t *testing.T) {
|
|
|
|
if os.Getenv("PERFORM_UNIX_OPEN_WRITE_TESTS") != "true" {
|
|
|
|
t.Skipf("PERFORM_UNIX_OPEN_WRITE_TESTS variable is not set. Skipping unix open/write tests")
|
|
|
|
}
|
|
|
|
|
2024-10-28 15:42:20 +01:00
|
|
|
tempFile, err := os.CreateTemp("", "embedfile-open-write-test.*.txt")
|
2024-10-28 12:01:31 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create temp file: %s", err)
|
|
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
|
|
if err := os.Remove(tempFile.Name()); err != nil {
|
|
|
|
t.Errorf("failed to remove temp file: %s", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if err = os.Chmod(tempFile.Name(), 0o000); err != nil {
|
|
|
|
t.Fatalf("failed to chmod temp file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
message := NewMsg()
|
|
|
|
if message == nil {
|
|
|
|
t.Fatal("message is nil")
|
|
|
|
}
|
|
|
|
message.EmbedFile(tempFile.Name())
|
|
|
|
embeds := message.GetEmbeds()
|
|
|
|
if len(embeds) != 1 {
|
|
|
|
t.Fatalf("failed to get embeds, expected 1, got: %d", len(embeds))
|
|
|
|
}
|
|
|
|
messageBuf := bytes.NewBuffer(nil)
|
|
|
|
_, err = embeds[0].Writer(messageBuf)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("writer func expected to fail, but didn't")
|
|
|
|
}
|
|
|
|
if !errors.Is(err, os.ErrPermission) {
|
|
|
|
t.Errorf("expected error to be %s, got: %s", os.ErrPermission, err)
|
|
|
|
}
|
|
|
|
})
|
2024-10-28 12:17:20 +01:00
|
|
|
}
|
|
|
|
|
2024-10-28 17:14:06 +01:00
|
|
|
func TestMsg_WriteToFile_unixOnly(t *testing.T) {
|
|
|
|
t.Run("WriteToFile fails on create", func(t *testing.T) {
|
|
|
|
tempfile, err := os.CreateTemp("", "testmail-create.*.eml")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create temp file: %s", err)
|
|
|
|
}
|
|
|
|
if err = os.Chmod(tempfile.Name(), 0o000); err != nil {
|
|
|
|
t.Fatalf("failed to chmod temp file: %s", err)
|
|
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
|
|
if err = tempfile.Close(); err != nil {
|
|
|
|
t.Fatalf("failed to close temp file: %s", err)
|
|
|
|
}
|
|
|
|
if err = os.Remove(tempfile.Name()); err != nil {
|
|
|
|
t.Fatalf("failed to remove temp file: %s", err)
|
2022-03-18 23:10:53 +01:00
|
|
|
}
|
|
|
|
})
|
2024-10-28 17:14:06 +01:00
|
|
|
message := testMessage(t)
|
|
|
|
if err = message.WriteToFile(tempfile.Name()); err == nil {
|
|
|
|
t.Errorf("expected error, got nil")
|
|
|
|
}
|
|
|
|
})
|
2022-09-08 15:08:17 +02:00
|
|
|
}
|
2024-09-20 19:45:49 +02:00
|
|
|
|
|
|
|
func TestMsg_WriteToTempFileFailed(t *testing.T) {
|
|
|
|
m := NewMsg()
|
|
|
|
_ = m.From("Toni Tester <tester@example.com>")
|
|
|
|
_ = m.To("Ellenor Tester <ellinor@example.com>")
|
|
|
|
m.SetBodyString(TypeTextPlain, "This is a test")
|
|
|
|
|
2024-09-20 21:42:10 +02:00
|
|
|
curTmpDir := os.Getenv("TMPDIR")
|
|
|
|
defer func() {
|
|
|
|
if err := os.Setenv("TMPDIR", curTmpDir); err != nil {
|
|
|
|
t.Errorf("failed to set TMPDIR environment variable: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-09-20 19:45:49 +02:00
|
|
|
if err := os.Setenv("TMPDIR", "/invalid/directory/that/does/not/exist"); err != nil {
|
|
|
|
t.Errorf("failed to set TMPDIR environment variable: %s", err)
|
|
|
|
}
|
|
|
|
_, err := m.WriteToTempFile()
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("WriteToTempFile() did not fail as expected")
|
|
|
|
}
|
|
|
|
}
|