mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-09 23:42:55 +01:00
Add new test and mockWriter to b64linebreaker_test.go
This update includes a new TestBase64LineBreaker_WriteAndClose function with cases for validating write and close operations in the b64linebreaker module. In addition, a mockWriter is introduced to simulate I/O operations for testing purposes.
This commit is contained in:
parent
5384690a97
commit
c0e856f2ad
1 changed files with 51 additions and 0 deletions
|
@ -8,6 +8,7 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
@ -437,6 +438,46 @@ func TestBase64LineBreakerFailures(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBase64LineBreaker_WriteAndClose(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
data []byte
|
||||||
|
expectedWrite string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Write data within MaxBodyLength",
|
||||||
|
data: []byte("testdata"),
|
||||||
|
expectedWrite: "testdata",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Write data exceeds MaxBodyLength",
|
||||||
|
data: []byte("verylongtestdata"),
|
||||||
|
expectedWrite: "verylongtest",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var mockErr = errors.New("mock write error")
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
blr := &Base64LineBreaker{out: &buf}
|
||||||
|
mw := &mockWriter{writeError: mockErr}
|
||||||
|
blr.out = mw
|
||||||
|
|
||||||
|
_, err := blr.Write(tt.data)
|
||||||
|
if err != nil && !errors.Is(err, mockErr) {
|
||||||
|
t.Errorf("Unexpected error while writing: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = blr.Close()
|
||||||
|
if err != nil && !errors.Is(err, mockErr) {
|
||||||
|
t.Errorf("Unexpected error while closing: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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))
|
||||||
|
@ -463,6 +504,16 @@ func (e errorWriter) Close() error {
|
||||||
return fmt.Errorf("supposed to always fail")
|
return fmt.Errorf("supposed to always fail")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MockWriter is a mock implementation of io.Writer used for testing.
|
||||||
|
type mockWriter struct {
|
||||||
|
writeError error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write writes the data into a buffer.
|
||||||
|
func (w *mockWriter) Write(p []byte) (n int, err error) {
|
||||||
|
return 0, w.writeError
|
||||||
|
}
|
||||||
|
|
||||||
func FuzzBase64LineBreaker_Write(f *testing.F) {
|
func FuzzBase64LineBreaker_Write(f *testing.F) {
|
||||||
f.Add([]byte("abc"))
|
f.Add([]byte("abc"))
|
||||||
f.Add([]byte("def"))
|
f.Add([]byte("def"))
|
||||||
|
|
Loading…
Reference in a new issue