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>
This commit is contained in:
Dave Cheney
2015-02-02 06:42:08 +00:00
parent bfc286917c
commit 7db43667c7
2 changed files with 27 additions and 25 deletions
+25 -24
View File
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ssh
package ssh_test
import (
"bytes"
@@ -12,14 +12,15 @@ import (
"net"
"net/http"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/terminal"
)
func ExampleNewServerConn() {
// An SSH server is represented by a ServerConfig, which holds
// 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
// a production setting.
if c.User() == "testuser" && string(pass) == "tiger" {
@@ -34,7 +35,7 @@ func ExampleNewServerConn() {
panic("Failed to load private key")
}
private, err := ParsePrivateKey(privateBytes)
private, err := ssh.ParsePrivateKey(privateBytes)
if err != nil {
panic("Failed to parse private key")
}
@@ -54,12 +55,12 @@ func ExampleNewServerConn() {
// Before use, a handshake must be performed on the incoming
// net.Conn.
_, chans, reqs, err := NewServerConn(nConn, config)
_, chans, reqs, err := ssh.NewServerConn(nConn, config)
if err != nil {
panic("failed to handshake")
}
// The incoming Request channel must be serviced.
go DiscardRequests(reqs)
go ssh.DiscardRequests(reqs)
// Service the incoming Channel channel.
for newChannel := range chans {
@@ -68,7 +69,7 @@ func ExampleNewServerConn() {
// "session" and ServerShell may be used to present a simple
// terminal interface.
if newChannel.ChannelType() != "session" {
newChannel.Reject(UnknownChannelType, "unknown channel type")
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
continue
}
channel, requests, err := newChannel.Accept()
@@ -79,7 +80,7 @@ func ExampleNewServerConn() {
// Sessions have out-of-band requests such as "shell",
// "pty-req" and "env". Here we handle only the
// "shell" request.
go func(in <-chan *Request) {
go func(in <-chan *ssh.Request) {
for req := range in {
ok := false
switch req.Type {
@@ -117,13 +118,13 @@ func ExampleDial() {
//
// To authenticate with the remote server you must pass at least one
// implementation of AuthMethod via the Auth field in ClientConfig.
config := &ClientConfig{
config := &ssh.ClientConfig{
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 {
panic("Failed to dial: " + err.Error())
}
@@ -147,14 +148,14 @@ func ExampleDial() {
}
func ExampleClient_Listen() {
config := &ClientConfig{
config := &ssh.ClientConfig{
User: "username",
Auth: []AuthMethod{
Password("password"),
Auth: []ssh.AuthMethod{
ssh.Password("password"),
},
}
// Dial your ssh server.
conn, err := Dial("tcp", "localhost:22", config)
conn, err := ssh.Dial("tcp", "localhost:22", config)
if err != nil {
log.Fatalf("unable to connect: %s", err)
}
@@ -175,14 +176,14 @@ func ExampleClient_Listen() {
func ExampleSession_RequestPty() {
// Create client config
config := &ClientConfig{
config := &ssh.ClientConfig{
User: "username",
Auth: []AuthMethod{
Password("password"),
Auth: []ssh.AuthMethod{
ssh.Password("password"),
},
}
// Connect to ssh server
conn, err := Dial("tcp", "localhost:22", config)
conn, err := ssh.Dial("tcp", "localhost:22", config)
if err != nil {
log.Fatalf("unable to connect: %s", err)
}
@@ -194,10 +195,10 @@ func ExampleSession_RequestPty() {
}
defer session.Close()
// 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
if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
+2 -1
View File
@@ -11,10 +11,11 @@ package test
import (
"bytes"
"errors"
"golang.org/x/crypto/ssh"
"io"
"strings"
"testing"
"golang.org/x/crypto/ssh"
)
func TestRunCommandSuccess(t *testing.T) {