-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Bug reading password from a buffer when reader returns EOF (#11796)
(cherry picked from commit e44a4a9)
- Loading branch information
1 parent
0962437
commit dd43fe3
Showing
2 changed files
with
73 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package input | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type fakeReader struct { | ||
fnc func(p []byte) (int, error) | ||
} | ||
|
||
func (f fakeReader) Read(p []byte) (int, error) { | ||
return f.fnc(p) | ||
} | ||
|
||
var _ io.Reader = fakeReader{} | ||
|
||
func TestReadLineFromBuf(t *testing.T) { | ||
var fr fakeReader | ||
|
||
t.Run("it correctly returns the password when reader returns EOF", func(t *testing.T) { | ||
fr.fnc = func(p []byte) (int, error) { | ||
return copy(p, []byte("hello")), io.EOF | ||
} | ||
buf := bufio.NewReader(fr) | ||
|
||
pass, err := readLineFromBuf(buf) | ||
require.NoError(t, err) | ||
require.Equal(t, "hello", pass) | ||
}) | ||
|
||
t.Run("it returns EOF if reader has been exhausted", func(t *testing.T) { | ||
fr.fnc = func(p []byte) (int, error) { | ||
return 0, io.EOF | ||
} | ||
buf := bufio.NewReader(fr) | ||
|
||
_, err := readLineFromBuf(buf) | ||
require.ErrorIs(t, err, io.EOF) | ||
}) | ||
|
||
t.Run("it returns the error if it's not EOF regardles if it read something or not", func(t *testing.T) { | ||
expectedErr := errors.New("oh no") | ||
fr.fnc = func(p []byte) (int, error) { | ||
return copy(p, []byte("hello")), expectedErr | ||
} | ||
buf := bufio.NewReader(fr) | ||
|
||
_, err := readLineFromBuf(buf) | ||
require.ErrorIs(t, err, expectedErr) | ||
}) | ||
|
||
} |