github.com/glycerine/xcryptossh@v7.0.4+incompatible/test/agent_unix_test.go (about)

     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  
     5  // +build darwin dragonfly freebsd linux netbsd openbsd
     6  
     7  package test
     8  
     9  import (
    10  	"bytes"
    11  	"context"
    12  	"testing"
    13  
    14  	ssh "github.com/glycerine/xcryptossh"
    15  	"github.com/glycerine/xcryptossh/agent"
    16  )
    17  
    18  func TestAgentForward(t *testing.T) {
    19  	ctx, cancelctx := context.WithCancel(context.Background())
    20  	defer cancelctx()
    21  
    22  	halt := ssh.NewHalter()
    23  	defer halt.ReqStop.Close()
    24  
    25  	server := newServer(t)
    26  	defer server.Shutdown()
    27  	conn := server.Dial(ctx, clientConfig(halt))
    28  	defer conn.Close()
    29  
    30  	keyring := agent.NewKeyring()
    31  	if err := keyring.Add(agent.AddedKey{PrivateKey: testPrivateKeys["dsa"]}); err != nil {
    32  		t.Fatalf("Error adding key: %s", err)
    33  	}
    34  	if err := keyring.Add(agent.AddedKey{
    35  		PrivateKey:       testPrivateKeys["dsa"],
    36  		ConfirmBeforeUse: true,
    37  		LifetimeSecs:     3600,
    38  	}); err != nil {
    39  		t.Fatalf("Error adding key with constraints: %s", err)
    40  	}
    41  	pub := testPublicKeys["dsa"]
    42  
    43  	sess, err := conn.NewSession(ctx)
    44  	if err != nil {
    45  		t.Fatalf("NewSession: %v", err)
    46  	}
    47  	if err := agent.RequestAgentForwarding(sess); err != nil {
    48  		t.Fatalf("RequestAgentForwarding: %v", err)
    49  	}
    50  
    51  	if err := agent.ForwardToAgent(ctx, conn, keyring); err != nil {
    52  		t.Fatalf("SetupForwardKeyring: %v", err)
    53  	}
    54  	out, err := sess.CombinedOutput("ssh-add -L")
    55  	if err != nil {
    56  		t.Fatalf("running ssh-add: %v, out %s", err, out)
    57  	}
    58  	key, _, _, _, err := ssh.ParseAuthorizedKey(out)
    59  	if err != nil {
    60  		t.Fatalf("ParseAuthorizedKey(%q): %v", out, err)
    61  	}
    62  
    63  	if !bytes.Equal(key.Marshal(), pub.Marshal()) {
    64  		t.Fatalf("got key %s, want %s", ssh.MarshalAuthorizedKey(key), ssh.MarshalAuthorizedKey(pub))
    65  	}
    66  }