2018-02-05 16:05:59 -05:00
|
|
|
package term // import "github.com/docker/docker/pkg/term"
|
2017-06-05 07:57:01 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
|
|
|
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
2017-06-05 07:57:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEscapeProxyRead(t *testing.T) {
|
2018-04-26 11:19:01 -04:00
|
|
|
escapeKeys, _ := ToBytes("")
|
|
|
|
keys, _ := ToBytes("a")
|
2017-06-05 07:57:01 -04:00
|
|
|
reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
|
|
|
buf := make([]byte, len(keys))
|
|
|
|
nr, err := reader.Read(buf)
|
2018-03-13 15:28:34 -04:00
|
|
|
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)))
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.DeepEqual(t, keys, buf)
|
2017-06-05 07:57:01 -04:00
|
|
|
|
2018-04-26 11:19:01 -04:00
|
|
|
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)
|
|
|
|
|
2017-06-05 07:57:01 -04:00
|
|
|
keys, _ = ToBytes("")
|
|
|
|
reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
|
|
|
buf = make([]byte, len(keys))
|
|
|
|
nr, err = reader.Read(buf)
|
2018-03-13 15:28:34 -04:00
|
|
|
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))
|
2017-06-05 07:57:01 -04:00
|
|
|
|
|
|
|
escapeKeys, _ = ToBytes("ctrl-x,ctrl-@")
|
|
|
|
keys, _ = ToBytes("DEL")
|
|
|
|
reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
|
|
|
buf = make([]byte, len(keys))
|
|
|
|
nr, err = reader.Read(buf)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, nr, 1, fmt.Sprintf("nr %d should be equal to the number of 1", nr))
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.DeepEqual(t, keys, buf)
|
2017-06-05 07:57:01 -04:00
|
|
|
|
|
|
|
escapeKeys, _ = ToBytes("ctrl-c")
|
|
|
|
keys, _ = ToBytes("ctrl-c")
|
|
|
|
reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
|
|
|
buf = make([]byte, len(keys))
|
|
|
|
nr, err = reader.Read(buf)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Error(t, err, "read escape sequence")
|
|
|
|
assert.Equal(t, nr, 0, "nr should be equal to 0")
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.DeepEqual(t, keys, buf)
|
2017-06-05 07:57:01 -04:00
|
|
|
|
|
|
|
escapeKeys, _ = ToBytes("ctrl-c,ctrl-z")
|
|
|
|
keys, _ = ToBytes("ctrl-c,ctrl-z")
|
|
|
|
reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
|
|
|
buf = make([]byte, 1)
|
|
|
|
nr, err = reader.Read(buf)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, nr, 0, "nr should be equal to 0")
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.DeepEqual(t, keys[0:1], buf)
|
2017-06-05 07:57:01 -04:00
|
|
|
nr, err = reader.Read(buf)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Error(t, err, "read escape sequence")
|
|
|
|
assert.Equal(t, nr, 0, "nr should be equal to 0")
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.DeepEqual(t, keys[1:], buf)
|
2017-06-05 07:57:01 -04:00
|
|
|
|
|
|
|
escapeKeys, _ = ToBytes("ctrl-c,ctrl-z")
|
|
|
|
keys, _ = ToBytes("ctrl-c,DEL,+")
|
|
|
|
reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
|
|
|
buf = make([]byte, 1)
|
|
|
|
nr, err = reader.Read(buf)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, nr, 0, "nr should be equal to 0")
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.DeepEqual(t, keys[0:1], buf)
|
2017-06-05 07:57:01 -04:00
|
|
|
buf = make([]byte, len(keys))
|
|
|
|
nr, err = reader.Read(buf)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, nr, len(keys), fmt.Sprintf("nr should be equal to %d", len(keys)))
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.DeepEqual(t, keys, buf)
|
2017-06-05 07:57:01 -04:00
|
|
|
|
|
|
|
escapeKeys, _ = ToBytes("ctrl-c,ctrl-z")
|
|
|
|
keys, _ = ToBytes("ctrl-c,DEL")
|
|
|
|
reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
|
|
|
buf = make([]byte, 1)
|
|
|
|
nr, err = reader.Read(buf)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, nr, 0, "nr should be equal to 0")
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.DeepEqual(t, keys[0:1], buf)
|
2017-06-05 07:57:01 -04:00
|
|
|
buf = make([]byte, len(keys))
|
|
|
|
nr, err = reader.Read(buf)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, nr, len(keys), fmt.Sprintf("nr should be equal to %d", len(keys)))
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.DeepEqual(t, keys, buf)
|
2017-06-05 07:57:01 -04:00
|
|
|
}
|