Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"io"
"net"
"os"
"os/exec"
"reflect"
"runtime"
"strings"
Expand Down Expand Up @@ -6129,7 +6130,7 @@ func TestMsg_WriteTo(t *testing.T) {
t.Fatalf("failed to initialize S/MIME signing: %s", err)
}
buffer := bytes.NewBuffer(nil)
if _, err := message.WriteTo(buffer); err != nil {
if _, err = message.WriteTo(buffer); err != nil {
t.Fatalf("failed to write message to buffer: %s", err)
}
fileContentType := "text/plain; charset=utf-8"
Expand Down Expand Up @@ -6767,6 +6768,35 @@ func TestMsg_WriteToTempFile(t *testing.T) {
t.Errorf("expected message buffer to contain Testmail, got: %s", got)
}
})
t.Run("WriteToTempFile S/MIME signed simple text message, verified with OpenSSL", func(t *testing.T) {
openSSL := getOpenSSLPath()
if openSSL == "" {
t.Skip("OpenSSL not found - skipping test")
}
message := testMessage(t)
keypair, err := getDummyKeyPairTLS()
if err != nil {
t.Fatalf("failed to load dummy key material: %s", err)
}
if err = message.SignWithTLSCertificate(keypair); err != nil {
t.Fatalf("failed to initialize S/MIME signing: %s", err)
}
msgFile, err := message.WriteToTempFile()
if err != nil {
t.Fatalf("failed to write message to buffer: %s", err)
}
t.Cleanup(func() {
if err := os.RemoveAll(msgFile); err != nil {
t.Errorf("failed to remove temp file: %s", err)
}
})
openSSLExec := exec.Command(openSSL, "smime", "-verify", "-noverify", "-in", msgFile)
out, err := openSSLExec.CombinedOutput()
if err != nil {
t.Errorf("S/MIME signing failed, expected OpenSSL to verify the message but got error: %s "+
"// exec output: %s", err, out)
}
})
}

func TestMsg_hasAlt(t *testing.T) {
Expand Down
16 changes: 16 additions & 0 deletions smime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"crypto/tls"
"crypto/x509"
"errors"
"os"
"strings"
"testing"
)
Expand Down Expand Up @@ -183,6 +184,21 @@ func TestGetLeafCertificate(t *testing.T) {
})
}

func getOpenSSLPath() string {
paths := []string{"/bin/openssl", "/usr/bin/openssl", "/usr/local/bin/openssl"}
openSSL := ""
for _, path := range paths {
if info, err := os.Stat(path); err == nil {
if info.IsDir() || info.Mode()&0o111 == 0 {
continue
}
openSSL = path
break
}
}
return openSSL
}

// getDummyRSACryptoMaterial loads a certificate (RSA), the associated private key and certificate (RSA) is loaded
// from local disk for testing purposes
func getDummyRSACryptoMaterial() (crypto.PrivateKey, *x509.Certificate, *x509.Certificate, error) {
Expand Down
Loading