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>
diff --git a/ssh/example_test.go b/ssh/example_test.go
index 22f42ec..dfd9dca 100644
--- a/ssh/example_test.go
+++ b/ssh/example_test.go
@@ -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 @@
"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 @@
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 @@
// 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 @@
// "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 @@
// 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 @@
//
// 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 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 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 @@
}
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 {