-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12edb27
commit 2368113
Showing
3 changed files
with
145 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,3 +154,26 @@ func TestMsgWriter_writeMsg_PGP(t *testing.T) { | |
t.Errorf("writeMsg failed. Expected PGP encoding header but didn't find it in message output") | ||
} | ||
} | ||
|
||
// TestMsgWriter_writeMsg_SMime tests the writeMsg method of the msgWriter with S/MIME types set | ||
func TestMsgWriter_writeMsg_SMime(t *testing.T) { | ||
keyPair, err := getDummyCertificate() | ||
if err != nil { | ||
t.Errorf("failed to load dummy certificate. Cause: %v", err) | ||
} | ||
m := NewMsg() | ||
if err := m.SignWithSMime(keyPair); err != nil { | ||
t.Errorf("set of certificate was not successful") | ||
} | ||
_ = m.From(`"Toni Tester" <[email protected]>`) | ||
_ = m.To(`"Toni Receiver" <[email protected]>`) | ||
m.Subject("This is a subject") | ||
m.SetBodyString(TypeTextPlain, "This is the body") | ||
buf := bytes.Buffer{} | ||
mw := &msgWriter{writer: &buf, charset: CharsetUTF8, encoder: mime.QEncoding} | ||
mw.writeMsg(m) | ||
ms := buf.String() | ||
if !strings.Contains(ms, `multipart/signed; protocol="application/pkcs7-signature"; micalg=sha256;`) { | ||
t.Errorf("writeMsg failed. Expected PGP encoding header but didn't find it in message output") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,19 @@ | ||
package mail | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"math/big" | ||
"time" | ||
"crypto/tls" | ||
) | ||
|
||
func getDummyPrivateKey() (*rsa.PrivateKey, error) { | ||
privateKey, err := rsa.GenerateKey(rand.Reader, 2048) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return privateKey, nil | ||
} | ||
|
||
func getDummyCertificate(privateKey *rsa.PrivateKey) (*x509.Certificate, error) { | ||
template := &x509.Certificate{ | ||
SerialNumber: big.NewInt(1234), | ||
Subject: pkix.Name{Organization: []string{"My Organization"}}, | ||
NotBefore: time.Now(), | ||
NotAfter: time.Now().AddDate(1, 0, 0), | ||
KeyUsage: x509.KeyUsageDigitalSignature, | ||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, | ||
} | ||
|
||
certDER, err := x509.CreateCertificate(rand.Reader, template, template, &privateKey.PublicKey, privateKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
const ( | ||
certFilePath = "dummy-cert.pem" | ||
keyFilePath = "dummy=key.pem" | ||
) | ||
|
||
cert, err := x509.ParseCertificate(certDER) | ||
func getDummyCertificate() (*tls.Certificate, error) { | ||
keyPair, err := tls.LoadX509KeyPair(certFilePath, keyFilePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return cert, nil | ||
return &keyPair, nil | ||
} |