mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-09 23:42:55 +01:00
#42: Better test coverage for b64linebreaker.go. Also we return errors now
This commit is contained in:
parent
b888d6058f
commit
ffc086e75f
2 changed files with 49 additions and 5 deletions
|
@ -4,7 +4,12 @@
|
||||||
|
|
||||||
package mail
|
package mail
|
||||||
|
|
||||||
import "io"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
const ErrNoOutWriter = "no io.Writer set for Base64LineBreaker"
|
||||||
|
|
||||||
// Base64LineBreaker is a io.WriteCloser that writes Base64 encoded data streams
|
// Base64LineBreaker is a io.WriteCloser that writes Base64 encoded data streams
|
||||||
// with line breaks at a given line length
|
// with line breaks at a given line length
|
||||||
|
@ -19,6 +24,9 @@ var nl = []byte(SingleNewLine)
|
||||||
// Write writes the data stream and inserts a SingleNewLine when the maximum
|
// Write writes the data stream and inserts a SingleNewLine when the maximum
|
||||||
// line length is reached
|
// line length is reached
|
||||||
func (l *Base64LineBreaker) Write(b []byte) (n int, err error) {
|
func (l *Base64LineBreaker) Write(b []byte) (n int, err error) {
|
||||||
|
if l.out == nil {
|
||||||
|
return 0, fmt.Errorf(ErrNoOutWriter)
|
||||||
|
}
|
||||||
if l.used+len(b) < MaxBodyLength {
|
if l.used+len(b) < MaxBodyLength {
|
||||||
copy(l.line[l.used:], b)
|
copy(l.line[l.used:], b)
|
||||||
l.used += len(b)
|
l.used += len(b)
|
||||||
|
@ -27,19 +35,19 @@ func (l *Base64LineBreaker) Write(b []byte) (n int, err error) {
|
||||||
|
|
||||||
n, err = l.out.Write(l.line[0:l.used])
|
n, err = l.out.Write(l.line[0:l.used])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return 0, err
|
||||||
}
|
}
|
||||||
excess := MaxBodyLength - l.used
|
excess := MaxBodyLength - l.used
|
||||||
l.used = 0
|
l.used = 0
|
||||||
|
|
||||||
n, err = l.out.Write(b[0:excess])
|
n, err = l.out.Write(b[0:excess])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
n, err = l.out.Write(nl)
|
n, err = l.out.Write(nl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return l.Write(b[excess:])
|
return l.Write(b[excess:])
|
||||||
|
@ -51,7 +59,7 @@ func (l *Base64LineBreaker) Close() (err error) {
|
||||||
if l.used > 0 {
|
if l.used > 0 {
|
||||||
_, err = l.out.Write(l.line[0:l.used])
|
_, err = l.out.Write(l.line[0:l.used])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
_, err = l.out.Write(nl)
|
_, err = l.out.Write(nl)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ package mail
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -410,6 +411,31 @@ func TestBase64LineBreaker(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestBase64LineBreakerFailures tests the cases in which the Base64LineBreaker would fail
|
||||||
|
func TestBase64LineBreakerFailures(t *testing.T) {
|
||||||
|
stt := []byte("short")
|
||||||
|
ltt := []byte(logoB64)
|
||||||
|
|
||||||
|
// No output writer defined
|
||||||
|
lb := Base64LineBreaker{}
|
||||||
|
if _, err := lb.Write(stt); err == nil {
|
||||||
|
t.Errorf("writing to Base64LineBreaker with no output io.Writer was supposed to failed, but didn't")
|
||||||
|
}
|
||||||
|
if err := lb.Close(); err != nil {
|
||||||
|
t.Errorf("failed to close Base64LineBreaker: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Closed output writer
|
||||||
|
wbuf := errorWriter{}
|
||||||
|
fb := Base64LineBreaker{out: wbuf}
|
||||||
|
if _, err := fb.Write(ltt); err == nil {
|
||||||
|
t.Errorf("writing to Base64LineBreaker with errorWriter was supposed to failed, but didn't")
|
||||||
|
}
|
||||||
|
if err := fb.Close(); err != nil {
|
||||||
|
t.Errorf("failed to close Base64LineBreaker: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// removeNewLines removes any newline characters from the given data
|
// removeNewLines removes any newline characters from the given data
|
||||||
func removeNewLines(data []byte) []byte {
|
func removeNewLines(data []byte) []byte {
|
||||||
result := make([]byte, len(data))
|
result := make([]byte, len(data))
|
||||||
|
@ -425,3 +451,13 @@ func removeNewLines(data []byte) []byte {
|
||||||
|
|
||||||
return result[0:n]
|
return result[0:n]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type errorWriter struct{}
|
||||||
|
|
||||||
|
func (e errorWriter) Write([]byte) (int, error) {
|
||||||
|
return 0, fmt.Errorf("supposed to always fail")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e errorWriter) Close() error {
|
||||||
|
return fmt.Errorf("supposed to always fail")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue