sftps: Fix test with latest API of the sftp package

This commit is contained in:
Jerry Jacobs 2018-03-09 11:31:10 +01:00
parent bbf41cb36d
commit 81cbd6395a
1 changed files with 17 additions and 24 deletions

View File

@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package afero package sftpfs
import ( import (
"testing" "testing"
@ -65,6 +65,7 @@ func SftpConnect(user, password, host string) (*SftpFsContext, error) {
Auth: []ssh.AuthMethod{ Auth: []ssh.AuthMethod{
ssh.Password(password), ssh.Password(password),
}, },
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
} }
sshc, err := ssh.Dial("tcp", host, sshcfg) sshc, err := ssh.Dial("tcp", host, sshcfg)
@ -97,7 +98,6 @@ func RunSftpServer(rootpath string) {
var ( var (
readOnly bool readOnly bool
debugLevelStr string debugLevelStr string
debugLevel int
debugStderr bool debugStderr bool
rootDir string rootDir string
) )
@ -109,10 +109,6 @@ func RunSftpServer(rootpath string) {
flag.Parse() flag.Parse()
debugStream := ioutil.Discard debugStream := ioutil.Discard
if debugStderr {
debugStream = os.Stderr
debugLevel = 1
}
// 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.
@ -146,7 +142,6 @@ func RunSftpServer(rootpath string) {
if err != nil { if err != nil {
log.Fatal("failed to listen for connection", err) log.Fatal("failed to listen for connection", err)
} }
fmt.Printf("Listening on %v\n", listener.Addr())
nConn, err := listener.Accept() nConn, err := listener.Accept()
if err != nil { if err != nil {
@ -155,11 +150,11 @@ func RunSftpServer(rootpath string) {
// 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 := ssh.NewServerConn(nConn, config) conn, chans, reqs, err := ssh.NewServerConn(nConn, config)
if err != nil { if err != nil {
log.Fatal("failed to handshake", err) log.Fatal("failed to handshake", err)
} }
fmt.Fprintf(debugStream, "SSH server established\n") defer conn.Close()
// The incoming Request channel must be serviced. // The incoming Request channel must be serviced.
go ssh.DiscardRequests(reqs) go ssh.DiscardRequests(reqs)
@ -200,13 +195,12 @@ func RunSftpServer(rootpath string) {
} }
}(requests) }(requests)
server, err := sftp.NewServer(channel, channel, debugStream, debugLevel, readOnly, rootpath) server, err := sftp.NewServer(channel, sftp.WithDebug(debugStream))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err := server.Serve(); err != nil { _ = server.Serve()
log.Fatal("sftp server completed with error:", err) return
}
} }
} }
@ -253,25 +247,23 @@ func TestSftpCreate(t *testing.T) {
} }
defer ctx.Disconnect() defer ctx.Disconnect()
var AppFs Fs = SftpFs{ var fs = New(ctx.sftpc)
SftpClient: ctx.sftpc,
}
AppFs.MkdirAll("test/dir1/dir2/dir3", os.FileMode(0777)) fs.MkdirAll("test/dir1/dir2/dir3", os.FileMode(0777))
AppFs.Mkdir("test/foo", os.FileMode(0000)) fs.Mkdir("test/foo", os.FileMode(0000))
AppFs.Chmod("test/foo", os.FileMode(0700)) fs.Chmod("test/foo", os.FileMode(0700))
AppFs.Mkdir("test/bar", os.FileMode(0777)) fs.Mkdir("test/bar", os.FileMode(0777))
file, err := AppFs.Create("file1") file, err := fs.Create("file1")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
defer file.Close() defer file.Close()
file.Write([]byte("hello\t")) file.Write([]byte("hello "))
file.WriteString("world!\n") file.WriteString("world!\n")
f1, err := AppFs.Open("file1") f1, err := fs.Open("file1")
if err != nil { if err != nil {
log.Fatalf("open: %v", err) log.Fatalf("open: %v", err)
} }
@ -279,8 +271,9 @@ func TestSftpCreate(t *testing.T) {
b := make([]byte, 100) b := make([]byte, 100)
_, err = f1.Read(b) _, _ = f1.Read(b)
fmt.Println(string(b)) fmt.Println(string(b))
fmt.Println("done")
// TODO check here if "hello\tworld\n" is in buffer b // TODO check here if "hello\tworld\n" is in buffer b
} }