session_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. // Copyright 2012 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 !windows,!solaris,!js
  5. package test
  6. // Session functional tests.
  7. import (
  8. "bytes"
  9. "errors"
  10. "fmt"
  11. "io"
  12. "runtime"
  13. "strings"
  14. "testing"
  15. "golang.org/x/crypto/ssh"
  16. )
  17. func TestRunCommandSuccess(t *testing.T) {
  18. server := newServer(t)
  19. defer server.Shutdown()
  20. conn := server.Dial(clientConfig())
  21. defer conn.Close()
  22. session, err := conn.NewSession()
  23. if err != nil {
  24. t.Fatalf("session failed: %v", err)
  25. }
  26. defer session.Close()
  27. err = session.Run("true")
  28. if err != nil {
  29. t.Fatalf("session failed: %v", err)
  30. }
  31. }
  32. func TestHostKeyCheck(t *testing.T) {
  33. server := newServer(t)
  34. defer server.Shutdown()
  35. conf := clientConfig()
  36. hostDB := hostKeyDB()
  37. conf.HostKeyCallback = hostDB.Check
  38. // change the keys.
  39. hostDB.keys[ssh.KeyAlgoRSA][25]++
  40. hostDB.keys[ssh.KeyAlgoDSA][25]++
  41. hostDB.keys[ssh.KeyAlgoECDSA256][25]++
  42. conn, err := server.TryDial(conf)
  43. if err == nil {
  44. conn.Close()
  45. t.Fatalf("dial should have failed.")
  46. } else if !strings.Contains(err.Error(), "host key mismatch") {
  47. t.Fatalf("'host key mismatch' not found in %v", err)
  48. }
  49. }
  50. func TestRunCommandStdin(t *testing.T) {
  51. server := newServer(t)
  52. defer server.Shutdown()
  53. conn := server.Dial(clientConfig())
  54. defer conn.Close()
  55. session, err := conn.NewSession()
  56. if err != nil {
  57. t.Fatalf("session failed: %v", err)
  58. }
  59. defer session.Close()
  60. r, w := io.Pipe()
  61. defer r.Close()
  62. defer w.Close()
  63. session.Stdin = r
  64. err = session.Run("true")
  65. if err != nil {
  66. t.Fatalf("session failed: %v", err)
  67. }
  68. }
  69. func TestRunCommandStdinError(t *testing.T) {
  70. server := newServer(t)
  71. defer server.Shutdown()
  72. conn := server.Dial(clientConfig())
  73. defer conn.Close()
  74. session, err := conn.NewSession()
  75. if err != nil {
  76. t.Fatalf("session failed: %v", err)
  77. }
  78. defer session.Close()
  79. r, w := io.Pipe()
  80. defer r.Close()
  81. session.Stdin = r
  82. pipeErr := errors.New("closing write end of pipe")
  83. w.CloseWithError(pipeErr)
  84. err = session.Run("true")
  85. if err != pipeErr {
  86. t.Fatalf("expected %v, found %v", pipeErr, err)
  87. }
  88. }
  89. func TestRunCommandFailed(t *testing.T) {
  90. server := newServer(t)
  91. defer server.Shutdown()
  92. conn := server.Dial(clientConfig())
  93. defer conn.Close()
  94. session, err := conn.NewSession()
  95. if err != nil {
  96. t.Fatalf("session failed: %v", err)
  97. }
  98. defer session.Close()
  99. err = session.Run(`bash -c "kill -9 $$"`)
  100. if err == nil {
  101. t.Fatalf("session succeeded: %v", err)
  102. }
  103. }
  104. func TestRunCommandWeClosed(t *testing.T) {
  105. server := newServer(t)
  106. defer server.Shutdown()
  107. conn := server.Dial(clientConfig())
  108. defer conn.Close()
  109. session, err := conn.NewSession()
  110. if err != nil {
  111. t.Fatalf("session failed: %v", err)
  112. }
  113. err = session.Shell()
  114. if err != nil {
  115. t.Fatalf("shell failed: %v", err)
  116. }
  117. err = session.Close()
  118. if err != nil {
  119. t.Fatalf("shell failed: %v", err)
  120. }
  121. }
  122. func TestFuncLargeRead(t *testing.T) {
  123. server := newServer(t)
  124. defer server.Shutdown()
  125. conn := server.Dial(clientConfig())
  126. defer conn.Close()
  127. session, err := conn.NewSession()
  128. if err != nil {
  129. t.Fatalf("unable to create new session: %s", err)
  130. }
  131. stdout, err := session.StdoutPipe()
  132. if err != nil {
  133. t.Fatalf("unable to acquire stdout pipe: %s", err)
  134. }
  135. err = session.Start("dd if=/dev/urandom bs=2048 count=1024")
  136. if err != nil {
  137. t.Fatalf("unable to execute remote command: %s", err)
  138. }
  139. buf := new(bytes.Buffer)
  140. n, err := io.Copy(buf, stdout)
  141. if err != nil {
  142. t.Fatalf("error reading from remote stdout: %s", err)
  143. }
  144. if n != 2048*1024 {
  145. t.Fatalf("Expected %d bytes but read only %d from remote command", 2048, n)
  146. }
  147. }
  148. func TestKeyChange(t *testing.T) {
  149. server := newServer(t)
  150. defer server.Shutdown()
  151. conf := clientConfig()
  152. hostDB := hostKeyDB()
  153. conf.HostKeyCallback = hostDB.Check
  154. conf.RekeyThreshold = 1024
  155. conn := server.Dial(conf)
  156. defer conn.Close()
  157. for i := 0; i < 4; i++ {
  158. session, err := conn.NewSession()
  159. if err != nil {
  160. t.Fatalf("unable to create new session: %s", err)
  161. }
  162. stdout, err := session.StdoutPipe()
  163. if err != nil {
  164. t.Fatalf("unable to acquire stdout pipe: %s", err)
  165. }
  166. err = session.Start("dd if=/dev/urandom bs=1024 count=1")
  167. if err != nil {
  168. t.Fatalf("unable to execute remote command: %s", err)
  169. }
  170. buf := new(bytes.Buffer)
  171. n, err := io.Copy(buf, stdout)
  172. if err != nil {
  173. t.Fatalf("error reading from remote stdout: %s", err)
  174. }
  175. want := int64(1024)
  176. if n != want {
  177. t.Fatalf("Expected %d bytes but read only %d from remote command", want, n)
  178. }
  179. }
  180. if changes := hostDB.checkCount; changes < 4 {
  181. t.Errorf("got %d key changes, want 4", changes)
  182. }
  183. }
  184. func TestValidTerminalMode(t *testing.T) {
  185. if runtime.GOOS == "aix" {
  186. // On AIX, sshd cannot acquire /dev/pts/* if launched as
  187. // a non-root user.
  188. t.Skipf("skipping on %s", runtime.GOOS)
  189. }
  190. server := newServer(t)
  191. defer server.Shutdown()
  192. conn := server.Dial(clientConfig())
  193. defer conn.Close()
  194. session, err := conn.NewSession()
  195. if err != nil {
  196. t.Fatalf("session failed: %v", err)
  197. }
  198. defer session.Close()
  199. stdout, err := session.StdoutPipe()
  200. if err != nil {
  201. t.Fatalf("unable to acquire stdout pipe: %s", err)
  202. }
  203. stdin, err := session.StdinPipe()
  204. if err != nil {
  205. t.Fatalf("unable to acquire stdin pipe: %s", err)
  206. }
  207. tm := ssh.TerminalModes{ssh.ECHO: 0}
  208. if err = session.RequestPty("xterm", 80, 40, tm); err != nil {
  209. t.Fatalf("req-pty failed: %s", err)
  210. }
  211. err = session.Shell()
  212. if err != nil {
  213. t.Fatalf("session failed: %s", err)
  214. }
  215. stdin.Write([]byte("stty -a && exit\n"))
  216. var buf bytes.Buffer
  217. if _, err := io.Copy(&buf, stdout); err != nil {
  218. t.Fatalf("reading failed: %s", err)
  219. }
  220. if sttyOutput := buf.String(); !strings.Contains(sttyOutput, "-echo ") {
  221. t.Fatalf("terminal mode failure: expected -echo in stty output, got %s", sttyOutput)
  222. }
  223. }
  224. func TestWindowChange(t *testing.T) {
  225. if runtime.GOOS == "aix" {
  226. // On AIX, sshd cannot acquire /dev/pts/* if launched as
  227. // a non-root user.
  228. t.Skipf("skipping on %s", runtime.GOOS)
  229. }
  230. server := newServer(t)
  231. defer server.Shutdown()
  232. conn := server.Dial(clientConfig())
  233. defer conn.Close()
  234. session, err := conn.NewSession()
  235. if err != nil {
  236. t.Fatalf("session failed: %v", err)
  237. }
  238. defer session.Close()
  239. stdout, err := session.StdoutPipe()
  240. if err != nil {
  241. t.Fatalf("unable to acquire stdout pipe: %s", err)
  242. }
  243. stdin, err := session.StdinPipe()
  244. if err != nil {
  245. t.Fatalf("unable to acquire stdin pipe: %s", err)
  246. }
  247. tm := ssh.TerminalModes{ssh.ECHO: 0}
  248. if err = session.RequestPty("xterm", 80, 40, tm); err != nil {
  249. t.Fatalf("req-pty failed: %s", err)
  250. }
  251. if err := session.WindowChange(100, 100); err != nil {
  252. t.Fatalf("window-change failed: %s", err)
  253. }
  254. err = session.Shell()
  255. if err != nil {
  256. t.Fatalf("session failed: %s", err)
  257. }
  258. stdin.Write([]byte("stty size && exit\n"))
  259. var buf bytes.Buffer
  260. if _, err := io.Copy(&buf, stdout); err != nil {
  261. t.Fatalf("reading failed: %s", err)
  262. }
  263. if sttyOutput := buf.String(); !strings.Contains(sttyOutput, "100 100") {
  264. t.Fatalf("terminal WindowChange failure: expected \"100 100\" stty output, got %s", sttyOutput)
  265. }
  266. }
  267. func testOneCipher(t *testing.T, cipher string, cipherOrder []string) {
  268. server := newServer(t)
  269. defer server.Shutdown()
  270. conf := clientConfig()
  271. conf.Ciphers = []string{cipher}
  272. // Don't fail if sshd doesn't have the cipher.
  273. conf.Ciphers = append(conf.Ciphers, cipherOrder...)
  274. conn, err := server.TryDial(conf)
  275. if err != nil {
  276. t.Fatalf("TryDial: %v", err)
  277. }
  278. defer conn.Close()
  279. numBytes := 4096
  280. // Exercise sending data to the server
  281. if _, _, err := conn.Conn.SendRequest("drop-me", false, make([]byte, numBytes)); err != nil {
  282. t.Fatalf("SendRequest: %v", err)
  283. }
  284. // Exercise receiving data from the server
  285. session, err := conn.NewSession()
  286. if err != nil {
  287. t.Fatalf("NewSession: %v", err)
  288. }
  289. out, err := session.Output(fmt.Sprintf("dd if=/dev/zero bs=%d count=1", numBytes))
  290. if err != nil {
  291. t.Fatalf("Output: %v", err)
  292. }
  293. if len(out) != numBytes {
  294. t.Fatalf("got %d bytes, want %d bytes", len(out), numBytes)
  295. }
  296. }
  297. var deprecatedCiphers = []string{
  298. "aes128-cbc", "3des-cbc",
  299. "arcfour128", "arcfour256",
  300. }
  301. func TestCiphers(t *testing.T) {
  302. var config ssh.Config
  303. config.SetDefaults()
  304. cipherOrder := append(config.Ciphers, deprecatedCiphers...)
  305. for _, ciph := range cipherOrder {
  306. t.Run(ciph, func(t *testing.T) {
  307. testOneCipher(t, ciph, cipherOrder)
  308. })
  309. }
  310. }
  311. func TestMACs(t *testing.T) {
  312. var config ssh.Config
  313. config.SetDefaults()
  314. macOrder := config.MACs
  315. for _, mac := range macOrder {
  316. t.Run(mac, func(t *testing.T) {
  317. server := newServer(t)
  318. defer server.Shutdown()
  319. conf := clientConfig()
  320. conf.MACs = []string{mac}
  321. // Don't fail if sshd doesn't have the MAC.
  322. conf.MACs = append(conf.MACs, macOrder...)
  323. if conn, err := server.TryDial(conf); err == nil {
  324. conn.Close()
  325. } else {
  326. t.Fatalf("failed for MAC %q", mac)
  327. }
  328. })
  329. }
  330. }
  331. func TestKeyExchanges(t *testing.T) {
  332. var config ssh.Config
  333. config.SetDefaults()
  334. kexOrder := config.KeyExchanges
  335. // Based on the discussion in #17230, the key exchange algorithms
  336. // diffie-hellman-group-exchange-sha1 and diffie-hellman-group-exchange-sha256
  337. // are not included in the default list of supported kex so we have to add them
  338. // here manually.
  339. kexOrder = append(kexOrder, "diffie-hellman-group-exchange-sha1", "diffie-hellman-group-exchange-sha256")
  340. for _, kex := range kexOrder {
  341. t.Run(kex, func(t *testing.T) {
  342. server := newServer(t)
  343. defer server.Shutdown()
  344. conf := clientConfig()
  345. // Don't fail if sshd doesn't have the kex.
  346. conf.KeyExchanges = append([]string{kex}, kexOrder...)
  347. conn, err := server.TryDial(conf)
  348. if err == nil {
  349. conn.Close()
  350. } else {
  351. t.Errorf("failed for kex %q", kex)
  352. }
  353. })
  354. }
  355. }
  356. func TestClientAuthAlgorithms(t *testing.T) {
  357. for _, key := range []string{
  358. "rsa",
  359. "dsa",
  360. "ecdsa",
  361. "ed25519",
  362. } {
  363. t.Run(key, func(t *testing.T) {
  364. server := newServer(t)
  365. conf := clientConfig()
  366. conf.SetDefaults()
  367. conf.Auth = []ssh.AuthMethod{
  368. ssh.PublicKeys(testSigners[key]),
  369. }
  370. conn, err := server.TryDial(conf)
  371. if err == nil {
  372. conn.Close()
  373. } else {
  374. t.Errorf("failed for key %q", key)
  375. }
  376. server.Shutdown()
  377. })
  378. }
  379. }