瀏覽代碼

ssh: make godoc examples easier to reuse

Fixes golang/go#9747

Move the example tests to an external test package so that they
must explicitly reference the ssh package. The side effect is the
examples now become easier to copy and paste.

Change-Id: Ibbddea42bc5a41d11ffdef5144d9884ef3ef603f
Reviewed-on: https://go-review.googlesource.com/3710
Reviewed-by: Andrew Gerrand <adg@golang.org>
Dave Cheney 11 年之前
父節點
當前提交
7db43667c7
共有 2 個文件被更改,包括 27 次插入25 次删除
  1. 25 24
      ssh/example_test.go
  2. 2 1
      ssh/test/session_test.go

+ 25 - 24
ssh/example_test.go

@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 // license that can be found in the LICENSE file.
 
 
-package ssh
+package ssh_test
 
 
 import (
 import (
 	"bytes"
 	"bytes"
@@ -12,14 +12,15 @@ import (
 	"net"
 	"net"
 	"net/http"
 	"net/http"
 
 
+	"golang.org/x/crypto/ssh"
 	"golang.org/x/crypto/ssh/terminal"
 	"golang.org/x/crypto/ssh/terminal"
 )
 )
 
 
 func ExampleNewServerConn() {
 func ExampleNewServerConn() {
 	// An SSH server is represented by a ServerConfig, which holds
 	// An SSH server is represented by a ServerConfig, which holds
 	// certificate details and handles authentication of ServerConns.
 	// certificate details and handles authentication of ServerConns.
-	config := &ServerConfig{
-		PasswordCallback: func(c ConnMetadata, pass []byte) (*Permissions, error) {
+	config := &ssh.ServerConfig{
+		PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
 			// Should use constant-time compare (or better, salt+hash) in
 			// Should use constant-time compare (or better, salt+hash) in
 			// a production setting.
 			// a production setting.
 			if c.User() == "testuser" && string(pass) == "tiger" {
 			if c.User() == "testuser" && string(pass) == "tiger" {
@@ -34,7 +35,7 @@ func ExampleNewServerConn() {
 		panic("Failed to load private key")
 		panic("Failed to load private key")
 	}
 	}
 
 
-	private, err := ParsePrivateKey(privateBytes)
+	private, err := ssh.ParsePrivateKey(privateBytes)
 	if err != nil {
 	if err != nil {
 		panic("Failed to parse private key")
 		panic("Failed to parse private key")
 	}
 	}
@@ -54,12 +55,12 @@ func ExampleNewServerConn() {
 
 
 	// Before use, a handshake must be performed on the incoming
 	// Before use, a handshake must be performed on the incoming
 	// net.Conn.
 	// net.Conn.
-	_, chans, reqs, err := NewServerConn(nConn, config)
+	_, chans, reqs, err := ssh.NewServerConn(nConn, config)
 	if err != nil {
 	if err != nil {
 		panic("failed to handshake")
 		panic("failed to handshake")
 	}
 	}
 	// The incoming Request channel must be serviced.
 	// The incoming Request channel must be serviced.
-	go DiscardRequests(reqs)
+	go ssh.DiscardRequests(reqs)
 
 
 	// Service the incoming Channel channel.
 	// Service the incoming Channel channel.
 	for newChannel := range chans {
 	for newChannel := range chans {
@@ -68,7 +69,7 @@ func ExampleNewServerConn() {
 		// "session" and ServerShell may be used to present a simple
 		// "session" and ServerShell may be used to present a simple
 		// terminal interface.
 		// terminal interface.
 		if newChannel.ChannelType() != "session" {
 		if newChannel.ChannelType() != "session" {
-			newChannel.Reject(UnknownChannelType, "unknown channel type")
+			newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
 			continue
 			continue
 		}
 		}
 		channel, requests, err := newChannel.Accept()
 		channel, requests, err := newChannel.Accept()
@@ -79,7 +80,7 @@ func ExampleNewServerConn() {
 		// Sessions have out-of-band requests such as "shell",
 		// Sessions have out-of-band requests such as "shell",
 		// "pty-req" and "env".  Here we handle only the
 		// "pty-req" and "env".  Here we handle only the
 		// "shell" request.
 		// "shell" request.
-		go func(in <-chan *Request) {
+		go func(in <-chan *ssh.Request) {
 			for req := range in {
 			for req := range in {
 				ok := false
 				ok := false
 				switch req.Type {
 				switch req.Type {
@@ -117,13 +118,13 @@ func ExampleDial() {
 	//
 	//
 	// To authenticate with the remote server you must pass at least one
 	// To authenticate with the remote server you must pass at least one
 	// implementation of AuthMethod via the Auth field in ClientConfig.
 	// implementation of AuthMethod via the Auth field in ClientConfig.
-	config := &ClientConfig{
+	config := &ssh.ClientConfig{
 		User: "username",
 		User: "username",
-		Auth: []AuthMethod{
-			Password("yourpassword"),
+		Auth: []ssh.AuthMethod{
+			ssh.Password("yourpassword"),
 		},
 		},
 	}
 	}
-	client, err := Dial("tcp", "yourserver.com:22", config)
+	client, err := ssh.Dial("tcp", "yourserver.com:22", config)
 	if err != nil {
 	if err != nil {
 		panic("Failed to dial: " + err.Error())
 		panic("Failed to dial: " + err.Error())
 	}
 	}
@@ -147,14 +148,14 @@ func ExampleDial() {
 }
 }
 
 
 func ExampleClient_Listen() {
 func ExampleClient_Listen() {
-	config := &ClientConfig{
+	config := &ssh.ClientConfig{
 		User: "username",
 		User: "username",
-		Auth: []AuthMethod{
-			Password("password"),
+		Auth: []ssh.AuthMethod{
+			ssh.Password("password"),
 		},
 		},
 	}
 	}
 	// Dial your ssh server.
 	// Dial your ssh server.
-	conn, err := Dial("tcp", "localhost:22", config)
+	conn, err := ssh.Dial("tcp", "localhost:22", config)
 	if err != nil {
 	if err != nil {
 		log.Fatalf("unable to connect: %s", err)
 		log.Fatalf("unable to connect: %s", err)
 	}
 	}
@@ -175,14 +176,14 @@ func ExampleClient_Listen() {
 
 
 func ExampleSession_RequestPty() {
 func ExampleSession_RequestPty() {
 	// Create client config
 	// Create client config
-	config := &ClientConfig{
+	config := &ssh.ClientConfig{
 		User: "username",
 		User: "username",
-		Auth: []AuthMethod{
-			Password("password"),
+		Auth: []ssh.AuthMethod{
+			ssh.Password("password"),
 		},
 		},
 	}
 	}
 	// Connect to ssh server
 	// Connect to ssh server
-	conn, err := Dial("tcp", "localhost:22", config)
+	conn, err := ssh.Dial("tcp", "localhost:22", config)
 	if err != nil {
 	if err != nil {
 		log.Fatalf("unable to connect: %s", err)
 		log.Fatalf("unable to connect: %s", err)
 	}
 	}
@@ -194,10 +195,10 @@ func ExampleSession_RequestPty() {
 	}
 	}
 	defer session.Close()
 	defer session.Close()
 	// Set up terminal modes
 	// Set up terminal modes
-	modes := TerminalModes{
-		ECHO:          0,     // disable echoing
-		TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
-		TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
+	modes := ssh.TerminalModes{
+		ssh.ECHO:          0,     // disable echoing
+		ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
+		ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
 	}
 	}
 	// Request pseudo terminal
 	// Request pseudo terminal
 	if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
 	if err := session.RequestPty("xterm", 80, 40, modes); err != nil {

+ 2 - 1
ssh/test/session_test.go

@@ -11,10 +11,11 @@ package test
 import (
 import (
 	"bytes"
 	"bytes"
 	"errors"
 	"errors"
-	"golang.org/x/crypto/ssh"
 	"io"
 	"io"
 	"strings"
 	"strings"
 	"testing"
 	"testing"
+
+	"golang.org/x/crypto/ssh"
 )
 )
 
 
 func TestRunCommandSuccess(t *testing.T) {
 func TestRunCommandSuccess(t *testing.T) {