agent_unix_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2014 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 darwin dragonfly freebsd linux netbsd openbsd
  5. package test
  6. import (
  7. "bytes"
  8. "testing"
  9. "golang.org/x/crypto/ssh"
  10. "golang.org/x/crypto/ssh/agent"
  11. )
  12. func TestAgentForward(t *testing.T) {
  13. server := newServer(t)
  14. defer server.Shutdown()
  15. conn := server.Dial(clientConfig())
  16. defer conn.Close()
  17. keyring := agent.NewKeyring()
  18. if err := keyring.Add(agent.AddedKey{PrivateKey: testPrivateKeys["dsa"]}); err != nil {
  19. t.Fatalf("Error adding key: %s", err)
  20. }
  21. if err := keyring.Add(agent.AddedKey{
  22. PrivateKey: testPrivateKeys["dsa"],
  23. ConfirmBeforeUse: true,
  24. LifetimeSecs: 3600,
  25. }); err != nil {
  26. t.Fatalf("Error adding key with constraints: %s", err)
  27. }
  28. pub := testPublicKeys["dsa"]
  29. sess, err := conn.NewSession()
  30. if err != nil {
  31. t.Fatalf("NewSession: %v", err)
  32. }
  33. if err := agent.RequestAgentForwarding(sess); err != nil {
  34. t.Fatalf("RequestAgentForwarding: %v", err)
  35. }
  36. if err := agent.ForwardToAgent(conn, keyring); err != nil {
  37. t.Fatalf("SetupForwardKeyring: %v", err)
  38. }
  39. out, err := sess.CombinedOutput("ssh-add -L")
  40. if err != nil {
  41. t.Fatalf("running ssh-add: %v, out %s", err, out)
  42. }
  43. key, _, _, _, err := ssh.ParseAuthorizedKey(out)
  44. if err != nil {
  45. t.Fatalf("ParseAuthorizedKey(%q): %v", out, err)
  46. }
  47. if !bytes.Equal(key.Marshal(), pub.Marshal()) {
  48. t.Fatalf("got key %s, want %s", ssh.MarshalAuthorizedKey(key), ssh.MarshalAuthorizedKey(pub))
  49. }
  50. }