Merge pull request #36918 from Sh4d1/master

add verification to check if escapeKeys have elements (pkg/term/proxy.go)
This commit is contained in:
Sebastiaan van Stijn 2018-04-30 11:57:21 +02:00 committed by GitHub
commit d4e48af48d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 2 deletions

View file

@ -34,6 +34,10 @@ func NewEscapeProxy(r io.Reader, escapeKeys []byte) io.Reader {
func (r *escapeProxy) Read(buf []byte) (int, error) {
nr, err := r.r.Read(buf)
if len(r.escapeKeys) == 0 {
return nr, err
}
preserve := func() {
// this preserves the original key presses in the passed in buffer
nr += r.escapeKeyPos

View file

@ -10,8 +10,8 @@ import (
)
func TestEscapeProxyRead(t *testing.T) {
escapeKeys, _ := ToBytes("DEL")
keys, _ := ToBytes("a,b,c,+")
escapeKeys, _ := ToBytes("")
keys, _ := ToBytes("a")
reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
buf := make([]byte, len(keys))
nr, err := reader.Read(buf)
@ -19,6 +19,32 @@ func TestEscapeProxyRead(t *testing.T) {
assert.Equal(t, nr, len(keys), fmt.Sprintf("nr %d should be equal to the number of %d", nr, len(keys)))
assert.DeepEqual(t, keys, buf)
keys, _ = ToBytes("a,b,c")
reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
buf = make([]byte, len(keys))
nr, err = reader.Read(buf)
assert.NilError(t, err)
assert.Equal(t, nr, len(keys), fmt.Sprintf("nr %d should be equal to the number of %d", nr, len(keys)))
assert.DeepEqual(t, keys, buf)
keys, _ = ToBytes("")
reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
buf = make([]byte, len(keys))
nr, err = reader.Read(buf)
assert.Assert(t, is.ErrorContains(err, ""), "Should throw error when no keys are to read")
assert.Equal(t, nr, 0, "nr should be zero")
assert.Check(t, is.Len(keys, 0))
assert.Check(t, is.Len(buf, 0))
escapeKeys, _ = ToBytes("DEL")
keys, _ = ToBytes("a,b,c,+")
reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
buf = make([]byte, len(keys))
nr, err = reader.Read(buf)
assert.NilError(t, err)
assert.Equal(t, nr, len(keys), fmt.Sprintf("nr %d should be equal to the number of %d", nr, len(keys)))
assert.DeepEqual(t, keys, buf)
keys, _ = ToBytes("")
reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
buf = make([]byte, len(keys))