Rename and update tests for UNIX-specific functionality

Renamed `msg_nowin_test.go` to `msg_unix_test.go` to better reflect its purpose. Updated the file attachment test to use `/dev/mem` for Unix environments instead of a temporary file with restricted permissions, ensuring compatibility with continuous integration environments.
This commit is contained in:
Winni Neessen 2024-10-27 20:46:16 +01:00
parent e74adb8b90
commit 6a43cf4aaf
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -2,45 +2,34 @@
//
// SPDX-License-Identifier: MIT
//go:build !windows
// +build !windows
//go:build linux || freebsd
// +build linux freebsd
package mail
import (
"bytes"
"context"
"errors"
"os"
"testing"
"time"
)
func TestMsg_AttachFile_unixOnly(t *testing.T) {
t.Run("AttachFile with fileFromFS fails on open", func(t *testing.T) {
tempfile, err := os.CreateTemp("testdata/tmp", "attachfile-unable-to-open.*.txt")
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 to 0000: %s", err)
}
message := NewMsg()
if message == nil {
t.Fatal("message is nil")
}
message.AttachFile(tempfile.Name())
// The /dev/mem device should not be readable on normal UNIX systems. We choose this
// approach over os.Chmod(0000) on a temp file, since Github runners give full access
// to the file system
message.AttachFile("/dev/mem")
attachments := message.GetAttachments()
if len(attachments) != 1 {
t.Fatalf("failed to get attachments, expected 1, got: %d", len(attachments))
}
messageBuf := bytes.NewBuffer(nil)
_, err = attachments[0].Writer(messageBuf)
_, err := attachments[0].Writer(messageBuf)
if err == nil {
t.Error("writer func expected to fail, but didn't")
}
@ -128,6 +117,7 @@ func TestMsg_AttachReader_unixOnly(t *testing.T) {
})
}
/*
// TestMsg_WriteToSendmailWithContext tests the WriteToSendmailWithContext() method of the Msg
func TestMsg_WriteToSendmailWithContext(t *testing.T) {
if os.Getenv("TEST_SENDMAIL") != "true" {
@ -197,3 +187,4 @@ func TestMsg_WriteToTempFileFailed(t *testing.T) {
t.Errorf("WriteToTempFile() did not fail as expected")
}
}
*/