forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 5
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 (back…
…port cosmos#11796) (cosmos#11810)
- Loading branch information
1 parent
a614734
commit ac7faf8
Showing
3 changed files
with
74 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
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) | ||
}) | ||
|
||
} |