terminal_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build aix darwin dragonfly freebsd linux,!appengine netbsd openbsd windows plan9 solaris
  5. package terminal
  6. import (
  7. "bytes"
  8. "io"
  9. "os"
  10. "runtime"
  11. "testing"
  12. )
  13. type MockTerminal struct {
  14. toSend []byte
  15. bytesPerRead int
  16. received []byte
  17. }
  18. func (c *MockTerminal) Read(data []byte) (n int, err error) {
  19. n = len(data)
  20. if n == 0 {
  21. return
  22. }
  23. if n > len(c.toSend) {
  24. n = len(c.toSend)
  25. }
  26. if n == 0 {
  27. return 0, io.EOF
  28. }
  29. if c.bytesPerRead > 0 && n > c.bytesPerRead {
  30. n = c.bytesPerRead
  31. }
  32. copy(data, c.toSend[:n])
  33. c.toSend = c.toSend[n:]
  34. return
  35. }
  36. func (c *MockTerminal) Write(data []byte) (n int, err error) {
  37. c.received = append(c.received, data...)
  38. return len(data), nil
  39. }
  40. func TestClose(t *testing.T) {
  41. c := &MockTerminal{}
  42. ss := NewTerminal(c, "> ")
  43. line, err := ss.ReadLine()
  44. if line != "" {
  45. t.Errorf("Expected empty line but got: %s", line)
  46. }
  47. if err != io.EOF {
  48. t.Errorf("Error should have been EOF but got: %s", err)
  49. }
  50. }
  51. var keyPressTests = []struct {
  52. in string
  53. line string
  54. err error
  55. throwAwayLines int
  56. }{
  57. {
  58. err: io.EOF,
  59. },
  60. {
  61. in: "\r",
  62. line: "",
  63. },
  64. {
  65. in: "foo\r",
  66. line: "foo",
  67. },
  68. {
  69. in: "a\x1b[Cb\r", // right
  70. line: "ab",
  71. },
  72. {
  73. in: "a\x1b[Db\r", // left
  74. line: "ba",
  75. },
  76. {
  77. in: "a\177b\r", // backspace
  78. line: "b",
  79. },
  80. {
  81. in: "\x1b[A\r", // up
  82. },
  83. {
  84. in: "\x1b[B\r", // down
  85. },
  86. {
  87. in: "\016\r", // ^P
  88. },
  89. {
  90. in: "\014\r", // ^N
  91. },
  92. {
  93. in: "line\x1b[A\x1b[B\r", // up then down
  94. line: "line",
  95. },
  96. {
  97. in: "line1\rline2\x1b[A\r", // recall previous line.
  98. line: "line1",
  99. throwAwayLines: 1,
  100. },
  101. {
  102. // recall two previous lines and append.
  103. in: "line1\rline2\rline3\x1b[A\x1b[Axxx\r",
  104. line: "line1xxx",
  105. throwAwayLines: 2,
  106. },
  107. {
  108. // Ctrl-A to move to beginning of line followed by ^K to kill
  109. // line.
  110. in: "a b \001\013\r",
  111. line: "",
  112. },
  113. {
  114. // Ctrl-A to move to beginning of line, Ctrl-E to move to end,
  115. // finally ^K to kill nothing.
  116. in: "a b \001\005\013\r",
  117. line: "a b ",
  118. },
  119. {
  120. in: "\027\r",
  121. line: "",
  122. },
  123. {
  124. in: "a\027\r",
  125. line: "",
  126. },
  127. {
  128. in: "a \027\r",
  129. line: "",
  130. },
  131. {
  132. in: "a b\027\r",
  133. line: "a ",
  134. },
  135. {
  136. in: "a b \027\r",
  137. line: "a ",
  138. },
  139. {
  140. in: "one two thr\x1b[D\027\r",
  141. line: "one two r",
  142. },
  143. {
  144. in: "\013\r",
  145. line: "",
  146. },
  147. {
  148. in: "a\013\r",
  149. line: "a",
  150. },
  151. {
  152. in: "ab\x1b[D\013\r",
  153. line: "a",
  154. },
  155. {
  156. in: "Ξεσκεπάζω\r",
  157. line: "Ξεσκεπάζω",
  158. },
  159. {
  160. in: "£\r\x1b[A\177\r", // non-ASCII char, enter, up, backspace.
  161. line: "",
  162. throwAwayLines: 1,
  163. },
  164. {
  165. in: "£\r££\x1b[A\x1b[B\177\r", // non-ASCII char, enter, 2x non-ASCII, up, down, backspace, enter.
  166. line: "£",
  167. throwAwayLines: 1,
  168. },
  169. {
  170. // Ctrl-D at the end of the line should be ignored.
  171. in: "a\004\r",
  172. line: "a",
  173. },
  174. {
  175. // a, b, left, Ctrl-D should erase the b.
  176. in: "ab\x1b[D\004\r",
  177. line: "a",
  178. },
  179. {
  180. // a, b, c, d, left, left, ^U should erase to the beginning of
  181. // the line.
  182. in: "abcd\x1b[D\x1b[D\025\r",
  183. line: "cd",
  184. },
  185. {
  186. // Bracketed paste mode: control sequences should be returned
  187. // verbatim in paste mode.
  188. in: "abc\x1b[200~de\177f\x1b[201~\177\r",
  189. line: "abcde\177",
  190. },
  191. {
  192. // Enter in bracketed paste mode should still work.
  193. in: "abc\x1b[200~d\refg\x1b[201~h\r",
  194. line: "efgh",
  195. throwAwayLines: 1,
  196. },
  197. {
  198. // Lines consisting entirely of pasted data should be indicated as such.
  199. in: "\x1b[200~a\r",
  200. line: "a",
  201. err: ErrPasteIndicator,
  202. },
  203. }
  204. func TestKeyPresses(t *testing.T) {
  205. for i, test := range keyPressTests {
  206. for j := 1; j < len(test.in); j++ {
  207. c := &MockTerminal{
  208. toSend: []byte(test.in),
  209. bytesPerRead: j,
  210. }
  211. ss := NewTerminal(c, "> ")
  212. for k := 0; k < test.throwAwayLines; k++ {
  213. _, err := ss.ReadLine()
  214. if err != nil {
  215. t.Errorf("Throwaway line %d from test %d resulted in error: %s", k, i, err)
  216. }
  217. }
  218. line, err := ss.ReadLine()
  219. if line != test.line {
  220. t.Errorf("Line resulting from test %d (%d bytes per read) was '%s', expected '%s'", i, j, line, test.line)
  221. break
  222. }
  223. if err != test.err {
  224. t.Errorf("Error resulting from test %d (%d bytes per read) was '%v', expected '%v'", i, j, err, test.err)
  225. break
  226. }
  227. }
  228. }
  229. }
  230. func TestPasswordNotSaved(t *testing.T) {
  231. c := &MockTerminal{
  232. toSend: []byte("password\r\x1b[A\r"),
  233. bytesPerRead: 1,
  234. }
  235. ss := NewTerminal(c, "> ")
  236. pw, _ := ss.ReadPassword("> ")
  237. if pw != "password" {
  238. t.Fatalf("failed to read password, got %s", pw)
  239. }
  240. line, _ := ss.ReadLine()
  241. if len(line) > 0 {
  242. t.Fatalf("password was saved in history")
  243. }
  244. }
  245. var setSizeTests = []struct {
  246. width, height int
  247. }{
  248. {40, 13},
  249. {80, 24},
  250. {132, 43},
  251. }
  252. func TestTerminalSetSize(t *testing.T) {
  253. for _, setSize := range setSizeTests {
  254. c := &MockTerminal{
  255. toSend: []byte("password\r\x1b[A\r"),
  256. bytesPerRead: 1,
  257. }
  258. ss := NewTerminal(c, "> ")
  259. ss.SetSize(setSize.width, setSize.height)
  260. pw, _ := ss.ReadPassword("Password: ")
  261. if pw != "password" {
  262. t.Fatalf("failed to read password, got %s", pw)
  263. }
  264. if string(c.received) != "Password: \r\n" {
  265. t.Errorf("failed to set the temporary prompt expected %q, got %q", "Password: ", c.received)
  266. }
  267. }
  268. }
  269. func TestReadPasswordLineEnd(t *testing.T) {
  270. var tests = []struct {
  271. input string
  272. want string
  273. }{
  274. {"\n", ""},
  275. {"\r\n", ""},
  276. {"test\r\n", "test"},
  277. {"testtesttesttes\n", "testtesttesttes"},
  278. {"testtesttesttes\r\n", "testtesttesttes"},
  279. {"testtesttesttesttest\n", "testtesttesttesttest"},
  280. {"testtesttesttesttest\r\n", "testtesttesttesttest"},
  281. }
  282. for _, test := range tests {
  283. buf := new(bytes.Buffer)
  284. if _, err := buf.WriteString(test.input); err != nil {
  285. t.Fatal(err)
  286. }
  287. have, err := readPasswordLine(buf)
  288. if err != nil {
  289. t.Errorf("readPasswordLine(%q) failed: %v", test.input, err)
  290. continue
  291. }
  292. if string(have) != test.want {
  293. t.Errorf("readPasswordLine(%q) returns %q, but %q is expected", test.input, string(have), test.want)
  294. continue
  295. }
  296. if _, err = buf.WriteString(test.input); err != nil {
  297. t.Fatal(err)
  298. }
  299. have, err = readPasswordLine(buf)
  300. if err != nil {
  301. t.Errorf("readPasswordLine(%q) failed: %v", test.input, err)
  302. continue
  303. }
  304. if string(have) != test.want {
  305. t.Errorf("readPasswordLine(%q) returns %q, but %q is expected", test.input, string(have), test.want)
  306. continue
  307. }
  308. }
  309. }
  310. func TestMakeRawState(t *testing.T) {
  311. fd := int(os.Stdout.Fd())
  312. if !IsTerminal(fd) {
  313. t.Skip("stdout is not a terminal; skipping test")
  314. }
  315. st, err := GetState(fd)
  316. if err != nil {
  317. t.Fatalf("failed to get terminal state from GetState: %s", err)
  318. }
  319. if runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") {
  320. t.Skip("MakeRaw not allowed on iOS; skipping test")
  321. }
  322. defer Restore(fd, st)
  323. raw, err := MakeRaw(fd)
  324. if err != nil {
  325. t.Fatalf("failed to get terminal state from MakeRaw: %s", err)
  326. }
  327. if *st != *raw {
  328. t.Errorf("states do not match; was %v, expected %v", raw, st)
  329. }
  330. }
  331. func TestOutputNewlines(t *testing.T) {
  332. // \n should be changed to \r\n in terminal output.
  333. buf := new(bytes.Buffer)
  334. term := NewTerminal(buf, ">")
  335. term.Write([]byte("1\n2\n"))
  336. output := string(buf.Bytes())
  337. const expected = "1\r\n2\r\n"
  338. if output != expected {
  339. t.Errorf("incorrect output: was %q, expected %q", output, expected)
  340. }
  341. }