terminal_test.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. var renderTests = []struct {
  231. in string
  232. received string
  233. err error
  234. }{
  235. {
  236. // Cursor move after keyHome (left 4) then enter (right 4, newline)
  237. in: "abcd\x1b[H\r",
  238. received: "> abcd\x1b[4D\x1b[4C\r\n",
  239. },
  240. {
  241. // Write, home, prepend, enter. Prepends rewrites the line.
  242. in: "cdef\x1b[Hab\r",
  243. received: "> cdef" + // Initial input
  244. "\x1b[4Da" + // Move cursor back, insert first char
  245. "cdef" + // Copy over original string
  246. "\x1b[4Dbcdef" + // Repeat for second char with copy
  247. "\x1b[4D" + // Put cursor back in position to insert again
  248. "\x1b[4C\r\n", // Put cursor at the end of the line and newline.
  249. },
  250. }
  251. func TestRender(t *testing.T) {
  252. for i, test := range renderTests {
  253. for j := 1; j < len(test.in); j++ {
  254. c := &MockTerminal{
  255. toSend: []byte(test.in),
  256. bytesPerRead: j,
  257. }
  258. ss := NewTerminal(c, "> ")
  259. _, err := ss.ReadLine()
  260. if err != test.err {
  261. t.Errorf("Error resulting from test %d (%d bytes per read) was '%v', expected '%v'", i, j, err, test.err)
  262. break
  263. }
  264. if test.received != string(c.received) {
  265. t.Errorf("Results rendered from test %d (%d bytes per read) was '%s', expected '%s'", i, j, c.received, test.received)
  266. break
  267. }
  268. }
  269. }
  270. }
  271. func TestPasswordNotSaved(t *testing.T) {
  272. c := &MockTerminal{
  273. toSend: []byte("password\r\x1b[A\r"),
  274. bytesPerRead: 1,
  275. }
  276. ss := NewTerminal(c, "> ")
  277. pw, _ := ss.ReadPassword("> ")
  278. if pw != "password" {
  279. t.Fatalf("failed to read password, got %s", pw)
  280. }
  281. line, _ := ss.ReadLine()
  282. if len(line) > 0 {
  283. t.Fatalf("password was saved in history")
  284. }
  285. }
  286. var setSizeTests = []struct {
  287. width, height int
  288. }{
  289. {40, 13},
  290. {80, 24},
  291. {132, 43},
  292. }
  293. func TestTerminalSetSize(t *testing.T) {
  294. for _, setSize := range setSizeTests {
  295. c := &MockTerminal{
  296. toSend: []byte("password\r\x1b[A\r"),
  297. bytesPerRead: 1,
  298. }
  299. ss := NewTerminal(c, "> ")
  300. ss.SetSize(setSize.width, setSize.height)
  301. pw, _ := ss.ReadPassword("Password: ")
  302. if pw != "password" {
  303. t.Fatalf("failed to read password, got %s", pw)
  304. }
  305. if string(c.received) != "Password: \r\n" {
  306. t.Errorf("failed to set the temporary prompt expected %q, got %q", "Password: ", c.received)
  307. }
  308. }
  309. }
  310. func TestReadPasswordLineEnd(t *testing.T) {
  311. var tests = []struct {
  312. input string
  313. want string
  314. }{
  315. {"\n", ""},
  316. {"\r\n", ""},
  317. {"test\r\n", "test"},
  318. {"testtesttesttes\n", "testtesttesttes"},
  319. {"testtesttesttes\r\n", "testtesttesttes"},
  320. {"testtesttesttesttest\n", "testtesttesttesttest"},
  321. {"testtesttesttesttest\r\n", "testtesttesttesttest"},
  322. }
  323. for _, test := range tests {
  324. buf := new(bytes.Buffer)
  325. if _, err := buf.WriteString(test.input); err != nil {
  326. t.Fatal(err)
  327. }
  328. have, err := readPasswordLine(buf)
  329. if err != nil {
  330. t.Errorf("readPasswordLine(%q) failed: %v", test.input, err)
  331. continue
  332. }
  333. if string(have) != test.want {
  334. t.Errorf("readPasswordLine(%q) returns %q, but %q is expected", test.input, string(have), test.want)
  335. continue
  336. }
  337. if _, err = buf.WriteString(test.input); err != nil {
  338. t.Fatal(err)
  339. }
  340. have, err = readPasswordLine(buf)
  341. if err != nil {
  342. t.Errorf("readPasswordLine(%q) failed: %v", test.input, err)
  343. continue
  344. }
  345. if string(have) != test.want {
  346. t.Errorf("readPasswordLine(%q) returns %q, but %q is expected", test.input, string(have), test.want)
  347. continue
  348. }
  349. }
  350. }
  351. func TestMakeRawState(t *testing.T) {
  352. fd := int(os.Stdout.Fd())
  353. if !IsTerminal(fd) {
  354. t.Skip("stdout is not a terminal; skipping test")
  355. }
  356. st, err := GetState(fd)
  357. if err != nil {
  358. t.Fatalf("failed to get terminal state from GetState: %s", err)
  359. }
  360. if runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") {
  361. t.Skip("MakeRaw not allowed on iOS; skipping test")
  362. }
  363. defer Restore(fd, st)
  364. raw, err := MakeRaw(fd)
  365. if err != nil {
  366. t.Fatalf("failed to get terminal state from MakeRaw: %s", err)
  367. }
  368. if *st != *raw {
  369. t.Errorf("states do not match; was %v, expected %v", raw, st)
  370. }
  371. }
  372. func TestOutputNewlines(t *testing.T) {
  373. // \n should be changed to \r\n in terminal output.
  374. buf := new(bytes.Buffer)
  375. term := NewTerminal(buf, ">")
  376. term.Write([]byte("1\n2\n"))
  377. output := string(buf.Bytes())
  378. const expected = "1\r\n2\r\n"
  379. if output != expected {
  380. t.Errorf("incorrect output: was %q, expected %q", output, expected)
  381. }
  382. }