Skip to content

Commit

Permalink
Add OpenSSL-based S/MIME verification test
Browse files Browse the repository at this point in the history
Introduce a function to locate the OpenSSL binary and use it in a new test case to verify S/MIME-signed messages. The test writes a signed message to a temporary file and uses OpenSSL to confirm its validity. This enhances test coverage for S/MIME signing functionality.
  • Loading branch information
wneessen committed Jan 23, 2025
1 parent 7e0e484 commit 97ba360
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
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

0 comments on commit 97ba360

Please sign in to comment.