...
1
2
3
4
5
6
7 package test
8
9 import (
10 "bytes"
11 "testing"
12
13 "golang.org/x/crypto/ssh"
14 "golang.org/x/crypto/ssh/agent"
15 )
16
17 func TestAgentForward(t *testing.T) {
18 server := newServer(t)
19 conn := server.Dial(clientConfig())
20 defer conn.Close()
21
22 keyring := agent.NewKeyring()
23 if err := keyring.Add(agent.AddedKey{PrivateKey: testPrivateKeys["dsa"]}); err != nil {
24 t.Fatalf("Error adding key: %s", err)
25 }
26 if err := keyring.Add(agent.AddedKey{
27 PrivateKey: testPrivateKeys["dsa"],
28 ConfirmBeforeUse: true,
29 LifetimeSecs: 3600,
30 }); err != nil {
31 t.Fatalf("Error adding key with constraints: %s", err)
32 }
33 pub := testPublicKeys["dsa"]
34
35 sess, err := conn.NewSession()
36 if err != nil {
37 skipIfIssue64959(t, err)
38 t.Fatalf("NewSession: %v", err)
39 }
40 if err := agent.RequestAgentForwarding(sess); err != nil {
41 t.Fatalf("RequestAgentForwarding: %v", err)
42 }
43
44 if err := agent.ForwardToAgent(conn, keyring); err != nil {
45 t.Fatalf("SetupForwardKeyring: %v", err)
46 }
47 out, err := sess.CombinedOutput("ssh-add -L")
48 if err != nil {
49 t.Fatalf("running ssh-add: %v, out %s", err, out)
50 }
51 key, _, _, _, err := ssh.ParseAuthorizedKey(out)
52 if err != nil {
53 t.Fatalf("ParseAuthorizedKey(%q): %v", out, err)
54 }
55
56 if !bytes.Equal(key.Marshal(), pub.Marshal()) {
57 t.Fatalf("got key %s, want %s", ssh.MarshalAuthorizedKey(key), ssh.MarshalAuthorizedKey(pub))
58 }
59 }
60
View as plain text