From c0e856f2adb697953d39e64b6e7d5ec4a27aa3e8 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sun, 24 Mar 2024 21:34:06 +0100 Subject: [PATCH 1/3] 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. --- b64linebreaker_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/b64linebreaker_test.go b/b64linebreaker_test.go index af58e514..be5a372f 100644 --- a/b64linebreaker_test.go +++ b/b64linebreaker_test.go @@ -8,6 +8,7 @@ import ( "bufio" "bytes" "encoding/base64" + "errors" "fmt" "io" "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 func removeNewLines(data []byte) []byte { result := make([]byte, len(data)) @@ -463,6 +504,16 @@ func (e errorWriter) Close() error { 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) { f.Add([]byte("abc")) f.Add([]byte("def")) From 4d6bca0f6557ffe34a8fe441b49527dc234ff1a8 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sun, 24 Mar 2024 22:23:36 +0100 Subject: [PATCH 2/3] Refactor test cases and introduce mock writers in b64linebreaker_test.go The commit modifies existing test cases in the TestBase64LineBreaker_WriteAndClose function where it introduces mock writers to better simulate I/O operations. The introduced mock writers are 'mockWriterExcess' and 'mockWriterNewline' which respectively simulate scenarios of data exceeding body length and inclusion of newline. --- b64linebreaker_test.go | 71 ++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 23 deletions(-) diff --git a/b64linebreaker_test.go b/b64linebreaker_test.go index be5a372f..b5288fc4 100644 --- a/b64linebreaker_test.go +++ b/b64linebreaker_test.go @@ -384,6 +384,11 @@ LjI4MiIgc3R5bGU9ImZpbGw6I2ZmYjI1YztzdHJva2U6IzAwMDtzdHJva2Utd2lkdGg6NC45NXB4 OyIvPjwvZz48L3N2Zz4= ` +var ( + mockErr = errors.New("mock write error") + mockNewlineErr = errors.New("mock newline error") +) + // TestBase64LineBreaker tests the Write and Close methods of the Base64LineBreaker func TestBase64LineBreaker(t *testing.T) { l, err := os.Open("assets/gopher2.svg") @@ -440,39 +445,40 @@ func TestBase64LineBreakerFailures(t *testing.T) { func TestBase64LineBreaker_WriteAndClose(t *testing.T) { tests := []struct { - name string - data []byte - expectedWrite string + name string + data []byte + writer io.Writer }{ { - name: "Write data within MaxBodyLength", - data: []byte("testdata"), - expectedWrite: "testdata", + name: "Write data within MaxBodyLength", + data: []byte("testdata"), + writer: &mockWriterExcess{writeError: mockErr}, }, { - name: "Write data exceeds MaxBodyLength", - data: []byte("verylongtestdata"), - expectedWrite: "verylongtest", + name: "Write data exceeds MaxBodyLength", + data: []byte("verylongtestdataverylongtestdataverylongtestdata" + + "verylongtestdataverylongtestdataverylongtestdata"), + writer: &mockWriterExcess{writeError: mockErr}, + }, + { + name: "Write data exceeds MaxBodyLength with newline", + data: []byte("verylongtestdataverylongtestdataverylongtestdata" + + "verylongtestdataverylongtestdataverylongtestdata"), + writer: &mockWriterNewline{writeError: mockErr}, }, } - 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 + blr := &Base64LineBreaker{out: tt.writer} _, err := blr.Write(tt.data) - if err != nil && !errors.Is(err, mockErr) { + if err != nil && !errors.Is(err, mockErr) && !errors.Is(err, mockNewlineErr) { t.Errorf("Unexpected error while writing: %v", err) - return } err = blr.Close() - if err != nil && !errors.Is(err, mockErr) { + if err != nil && !errors.Is(err, mockErr) && !errors.Is(err, mockNewlineErr) { t.Errorf("Unexpected error while closing: %v", err) - return } }) } @@ -504,14 +510,33 @@ func (e errorWriter) Close() error { return fmt.Errorf("supposed to always fail") } -// MockWriter is a mock implementation of io.Writer used for testing. -type mockWriter struct { +type mockWriterExcess struct { + writeError error +} +type mockWriterNewline 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 (w *mockWriterExcess) Write(p []byte) (n int, err error) { + switch len(p) { + case 0: + return 0, nil + case 2: + return 2, nil + default: + return len(p), mockErr + } +} + +func (w *mockWriterNewline) Write(p []byte) (n int, err error) { + switch len(p) { + case 0: + return 0, nil + case 2: + return 2, mockNewlineErr + default: + return len(p), nil + } } func FuzzBase64LineBreaker_Write(f *testing.F) { From 86e648a695ecd6e1ba65d2469d7fa06d4a4f171f Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sun, 24 Mar 2024 22:26:15 +0100 Subject: [PATCH 3/3] Rename error variables in b64linebreaker_test.go In b64linebreaker_test.go, the names of error variables "mockErr" and "mockNewlineErr" have been changed to "errMockDefault" and "errMockNewline" to better clarify their roles. All instances in test cases where these error variables were used have been updated correspondingly. --- b64linebreaker_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/b64linebreaker_test.go b/b64linebreaker_test.go index b5288fc4..9340d8de 100644 --- a/b64linebreaker_test.go +++ b/b64linebreaker_test.go @@ -385,8 +385,8 @@ OyIvPjwvZz48L3N2Zz4= ` var ( - mockErr = errors.New("mock write error") - mockNewlineErr = errors.New("mock newline error") + errMockDefault = errors.New("mock write error") + errMockNewline = errors.New("mock newline error") ) // TestBase64LineBreaker tests the Write and Close methods of the Base64LineBreaker @@ -452,19 +452,19 @@ func TestBase64LineBreaker_WriteAndClose(t *testing.T) { { name: "Write data within MaxBodyLength", data: []byte("testdata"), - writer: &mockWriterExcess{writeError: mockErr}, + writer: &mockWriterExcess{writeError: errMockDefault}, }, { name: "Write data exceeds MaxBodyLength", data: []byte("verylongtestdataverylongtestdataverylongtestdata" + "verylongtestdataverylongtestdataverylongtestdata"), - writer: &mockWriterExcess{writeError: mockErr}, + writer: &mockWriterExcess{writeError: errMockDefault}, }, { name: "Write data exceeds MaxBodyLength with newline", data: []byte("verylongtestdataverylongtestdataverylongtestdata" + "verylongtestdataverylongtestdataverylongtestdata"), - writer: &mockWriterNewline{writeError: mockErr}, + writer: &mockWriterNewline{writeError: errMockDefault}, }, } @@ -473,11 +473,11 @@ func TestBase64LineBreaker_WriteAndClose(t *testing.T) { blr := &Base64LineBreaker{out: tt.writer} _, err := blr.Write(tt.data) - if err != nil && !errors.Is(err, mockErr) && !errors.Is(err, mockNewlineErr) { + if err != nil && !errors.Is(err, errMockDefault) && !errors.Is(err, errMockNewline) { t.Errorf("Unexpected error while writing: %v", err) } err = blr.Close() - if err != nil && !errors.Is(err, mockErr) && !errors.Is(err, mockNewlineErr) { + if err != nil && !errors.Is(err, errMockDefault) && !errors.Is(err, errMockNewline) { t.Errorf("Unexpected error while closing: %v", err) } }) @@ -524,7 +524,7 @@ func (w *mockWriterExcess) Write(p []byte) (n int, err error) { case 2: return 2, nil default: - return len(p), mockErr + return len(p), errMockDefault } } @@ -533,7 +533,7 @@ func (w *mockWriterNewline) Write(p []byte) (n int, err error) { case 0: return 0, nil case 2: - return 2, mockNewlineErr + return 2, errMockNewline default: return len(p), nil }