server_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. package agent
  5. import (
  6. "testing"
  7. "github.com/gogits/gogs/modules/crypto/ssh"
  8. )
  9. func TestServer(t *testing.T) {
  10. c1, c2, err := netPipe()
  11. if err != nil {
  12. t.Fatalf("netPipe: %v", err)
  13. }
  14. defer c1.Close()
  15. defer c2.Close()
  16. client := NewClient(c1)
  17. go ServeAgent(NewKeyring(), c2)
  18. testAgentInterface(t, client, testPrivateKeys["rsa"], nil, 0)
  19. }
  20. func TestLockServer(t *testing.T) {
  21. testLockAgent(NewKeyring(), t)
  22. }
  23. func TestSetupForwardAgent(t *testing.T) {
  24. a, b, err := netPipe()
  25. if err != nil {
  26. t.Fatalf("netPipe: %v", err)
  27. }
  28. defer a.Close()
  29. defer b.Close()
  30. _, socket, cleanup := startAgent(t)
  31. defer cleanup()
  32. serverConf := ssh.ServerConfig{
  33. NoClientAuth: true,
  34. }
  35. serverConf.AddHostKey(testSigners["rsa"])
  36. incoming := make(chan *ssh.ServerConn, 1)
  37. go func() {
  38. conn, _, _, err := ssh.NewServerConn(a, &serverConf)
  39. if err != nil {
  40. t.Fatalf("Server: %v", err)
  41. }
  42. incoming <- conn
  43. }()
  44. conf := ssh.ClientConfig{}
  45. conn, chans, reqs, err := ssh.NewClientConn(b, "", &conf)
  46. if err != nil {
  47. t.Fatalf("NewClientConn: %v", err)
  48. }
  49. client := ssh.NewClient(conn, chans, reqs)
  50. if err := ForwardToRemote(client, socket); err != nil {
  51. t.Fatalf("SetupForwardAgent: %v", err)
  52. }
  53. server := <-incoming
  54. ch, reqs, err := server.OpenChannel(channelType, nil)
  55. if err != nil {
  56. t.Fatalf("OpenChannel(%q): %v", channelType, err)
  57. }
  58. go ssh.DiscardRequests(reqs)
  59. agentClient := NewClient(ch)
  60. testAgentInterface(t, agentClient, testPrivateKeys["rsa"], nil, 0)
  61. conn.Close()
  62. }