doc_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. package pty
  2. import (
  3. "bytes"
  4. "io"
  5. "os"
  6. "testing"
  7. )
  8. // Will fill p from reader r
  9. func readBytes(r io.Reader, p []byte) error {
  10. bytesRead := 0
  11. for bytesRead < len(p) {
  12. n, err := r.Read(p[bytesRead:])
  13. if err != nil {
  14. return err
  15. }
  16. bytesRead = bytesRead + n
  17. }
  18. return nil
  19. }
  20. func TestOpen(t *testing.T) {
  21. t.Parallel()
  22. pty, tty, err := Open()
  23. if err != nil {
  24. t.Errorf("Unexpected error from Open: %s", err)
  25. }
  26. err = tty.Close()
  27. if err != nil {
  28. t.Errorf("Unexpected error from tty Close: %s", err)
  29. }
  30. err = pty.Close()
  31. if err != nil {
  32. t.Errorf("Unexpected error from pty Close: %s", err)
  33. }
  34. }
  35. func TestName(t *testing.T) {
  36. t.Parallel()
  37. pty, tty, err := Open()
  38. if err != nil {
  39. t.Errorf("Unexpected error from Open: %s", err)
  40. }
  41. // Check name isn't empty. There's variation on what exactly the OS calls these files.
  42. if pty.Name() == "" {
  43. t.Error("pty name was empty")
  44. }
  45. if tty.Name() == "" {
  46. t.Error("tty name was empty")
  47. }
  48. err = tty.Close()
  49. if err != nil {
  50. t.Errorf("Unexpected error from tty Close: %s", err)
  51. }
  52. err = pty.Close()
  53. if err != nil {
  54. t.Errorf("Unexpected error from pty Close: %s", err)
  55. }
  56. }
  57. // TestOpenByName ensures that the name associated with the tty is valid
  58. // and can be opened and used if passed by file name (rather than passing
  59. // the existing open file descriptor).
  60. func TestOpenByName(t *testing.T) {
  61. t.Parallel()
  62. pty, tty, err := Open()
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. defer pty.Close()
  67. defer tty.Close()
  68. ttyFile, err := os.OpenFile(tty.Name(), os.O_RDWR, 0600)
  69. if err != nil {
  70. t.Fatalf("Failed to open tty file: %v", err)
  71. }
  72. defer ttyFile.Close()
  73. // Ensure we can write to the newly opened tty file and read on the pty.
  74. text := []byte("ping")
  75. n, err := ttyFile.Write(text)
  76. if err != nil {
  77. t.Errorf("Unexpected error from Write: %s", err)
  78. }
  79. if n != len(text) {
  80. t.Errorf("Unexpected count returned from Write, got %d expected %d", n, len(text))
  81. }
  82. buffer := make([]byte, len(text))
  83. if err := readBytes(pty, buffer); err != nil {
  84. t.Errorf("Unexpected error from readBytes: %s", err)
  85. }
  86. if !bytes.Equal(text, buffer) {
  87. t.Errorf("Unexpected result returned from Read, got %v expected %v", buffer, text)
  88. }
  89. }
  90. func TestGetsize(t *testing.T) {
  91. t.Parallel()
  92. pty, tty, err := Open()
  93. if err != nil {
  94. t.Errorf("Unexpected error from Open: %s", err)
  95. }
  96. prows, pcols, err := Getsize(pty)
  97. if err != nil {
  98. t.Errorf("Unexpected error from Getsize: %s", err)
  99. }
  100. trows, tcols, err := Getsize(tty)
  101. if err != nil {
  102. t.Errorf("Unexpected error from Getsize: %s", err)
  103. }
  104. if prows != trows {
  105. t.Errorf("pty rows != tty rows: %d != %d", prows, trows)
  106. }
  107. if prows != trows {
  108. t.Errorf("pty cols != tty cols: %d != %d", pcols, tcols)
  109. }
  110. err = tty.Close()
  111. if err != nil {
  112. t.Errorf("Unexpected error from tty Close: %s", err)
  113. }
  114. err = pty.Close()
  115. if err != nil {
  116. t.Errorf("Unexpected error from pty Close: %s", err)
  117. }
  118. }
  119. func TestGetsizefull(t *testing.T) {
  120. t.Parallel()
  121. pty, tty, err := Open()
  122. if err != nil {
  123. t.Errorf("Unexpected error from Open: %s", err)
  124. }
  125. psize, err := GetsizeFull(pty)
  126. if err != nil {
  127. t.Errorf("Unexpected error from GetsizeFull: %s", err)
  128. }
  129. tsize, err := GetsizeFull(tty)
  130. if err != nil {
  131. t.Errorf("Unexpected error from GetsizeFull: %s", err)
  132. }
  133. if psize.X != tsize.X {
  134. t.Errorf("pty x != tty x: %d != %d", psize.X, tsize.X)
  135. }
  136. if psize.Y != tsize.Y {
  137. t.Errorf("pty y != tty y: %d != %d", psize.Y, tsize.Y)
  138. }
  139. if psize.Rows != tsize.Rows {
  140. t.Errorf("pty rows != tty rows: %d != %d", psize.Rows, tsize.Rows)
  141. }
  142. if psize.Cols != tsize.Cols {
  143. t.Errorf("pty cols != tty cols: %d != %d", psize.Cols, tsize.Cols)
  144. }
  145. err = tty.Close()
  146. if err != nil {
  147. t.Errorf("Unexpected error from tty Close: %s", err)
  148. }
  149. err = pty.Close()
  150. if err != nil {
  151. t.Errorf("Unexpected error from pty Close: %s", err)
  152. }
  153. }
  154. func TestSetsize(t *testing.T) {
  155. t.Parallel()
  156. pty, tty, err := Open()
  157. if err != nil {
  158. t.Errorf("Unexpected error from Open: %s", err)
  159. }
  160. psize, err := GetsizeFull(pty)
  161. if err != nil {
  162. t.Errorf("Unexpected error from GetsizeFull: %s", err)
  163. }
  164. psize.X = psize.X + 1
  165. psize.Y = psize.Y + 1
  166. psize.Rows = psize.Rows + 1
  167. psize.Cols = psize.Cols + 1
  168. err = Setsize(tty, psize)
  169. if err != nil {
  170. t.Errorf("Unexpected error from Setsize: %s", err)
  171. }
  172. tsize, err := GetsizeFull(tty)
  173. if err != nil {
  174. t.Errorf("Unexpected error from GetsizeFull: %s", err)
  175. }
  176. if psize.X != tsize.X {
  177. t.Errorf("pty x != tty x: %d != %d", psize.X, tsize.X)
  178. }
  179. if psize.Y != tsize.Y {
  180. t.Errorf("pty y != tty y: %d != %d", psize.Y, tsize.Y)
  181. }
  182. if psize.Rows != tsize.Rows {
  183. t.Errorf("pty rows != tty rows: %d != %d", psize.Rows, tsize.Rows)
  184. }
  185. if psize.Cols != tsize.Cols {
  186. t.Errorf("pty cols != tty cols: %d != %d", psize.Cols, tsize.Cols)
  187. }
  188. err = tty.Close()
  189. if err != nil {
  190. t.Errorf("Unexpected error from tty Close: %s", err)
  191. }
  192. err = pty.Close()
  193. if err != nil {
  194. t.Errorf("Unexpected error from pty Close: %s", err)
  195. }
  196. }
  197. func TestReadWriteText(t *testing.T) {
  198. t.Parallel()
  199. pty, tty, err := Open()
  200. if err != nil {
  201. t.Errorf("Unexpected error from Open: %s", err)
  202. }
  203. // Write to tty, read from pty
  204. text := []byte("ping")
  205. n, err := tty.Write(text)
  206. if err != nil {
  207. t.Errorf("Unexpected error from Write: %s", err)
  208. }
  209. if n != len(text) {
  210. t.Errorf("Unexpected count returned from Write, got %d expected %d", n, len(text))
  211. }
  212. buffer := make([]byte, 4)
  213. err = readBytes(pty, buffer)
  214. if err != nil {
  215. t.Errorf("Unexpected error from readBytes: %s", err)
  216. }
  217. if !bytes.Equal(text, buffer) {
  218. t.Errorf("Unexpected result returned from Read, got %v expected %v", buffer, text)
  219. }
  220. // Write to pty, read from tty.
  221. // We need to send a \n otherwise this will block in the terminal driver.
  222. text = []byte("pong\n")
  223. n, err = pty.Write(text)
  224. if err != nil {
  225. t.Errorf("Unexpected error from Write: %s", err)
  226. }
  227. if n != len(text) {
  228. t.Errorf("Unexpected count returned from Write, got %d expected %d", n, len(text))
  229. }
  230. buffer = make([]byte, 5)
  231. err = readBytes(tty, buffer)
  232. if err != nil {
  233. t.Errorf("Unexpected error from readBytes: %s", err)
  234. }
  235. expect := []byte("pong\n")
  236. if !bytes.Equal(expect, buffer) {
  237. t.Errorf("Unexpected result returned from Read, got %v expected %v", buffer, expect)
  238. }
  239. // Read the echo back from pty
  240. buffer = make([]byte, 5)
  241. err = readBytes(pty, buffer)
  242. if err != nil {
  243. t.Errorf("Unexpected error from readBytes: %s", err)
  244. }
  245. expect = []byte("pong\r")
  246. if !bytes.Equal(expect, buffer) {
  247. t.Errorf("Unexpected result returned from Read, got %v expected %v", buffer, expect)
  248. }
  249. err = tty.Close()
  250. if err != nil {
  251. t.Errorf("Unexpected error from tty Close: %s", err)
  252. }
  253. err = pty.Close()
  254. if err != nil {
  255. t.Errorf("Unexpected error from pty Close: %s", err)
  256. }
  257. }
  258. func TestReadWriteControls(t *testing.T) {
  259. t.Parallel()
  260. pty, tty, err := Open()
  261. if err != nil {
  262. t.Errorf("Unexpected error from Open: %s", err)
  263. }
  264. // Write the start of a line to pty
  265. text := []byte("pind")
  266. n, err := pty.Write(text)
  267. if err != nil {
  268. t.Errorf("Unexpected error from Write: %s", err)
  269. }
  270. if n != len(text) {
  271. t.Errorf("Unexpected count returned from Write, got %d expected %d", n, len(text))
  272. }
  273. // Backspace that last char
  274. n, err = pty.Write([]byte("\b"))
  275. if err != nil {
  276. t.Errorf("Unexpected error from Write: %s", err)
  277. }
  278. if n != 1 {
  279. t.Errorf("Unexpected count returned from Write, got %d expected %d", n, 1)
  280. }
  281. // Write the correct char and a CR
  282. n, err = pty.Write([]byte("g\n"))
  283. if err != nil {
  284. t.Errorf("Unexpected error from Write: %s", err)
  285. }
  286. if n != 2 {
  287. t.Errorf("Unexpected count returned from Write, got %d expected %d", n, 2)
  288. }
  289. // Read the line
  290. buffer := make([]byte, 7)
  291. err = readBytes(tty, buffer)
  292. if err != nil {
  293. t.Errorf("Unexpected error from readBytes: %s", err)
  294. }
  295. expect := []byte("pind\bg\n")
  296. if !bytes.Equal(expect, buffer) {
  297. t.Errorf("Unexpected result returned from Read, got %v expected %v", buffer, expect)
  298. }
  299. // Read the echo back from pty
  300. buffer = make([]byte, 7)
  301. err = readBytes(pty, buffer)
  302. if err != nil {
  303. t.Errorf("Unexpected error from readBytes: %s", err)
  304. }
  305. expect = []byte("pind^Hg")
  306. if !bytes.Equal(expect, buffer) {
  307. t.Errorf("Unexpected result returned from Read, got %v expected %v", buffer, expect)
  308. }
  309. err = tty.Close()
  310. if err != nil {
  311. t.Errorf("Unexpected error from tty Close: %s", err)
  312. }
  313. err = pty.Close()
  314. if err != nil {
  315. t.Errorf("Unexpected error from pty Close: %s", err)
  316. }
  317. }