Merge branch 'JonasAlfredsson-space_fix'
diff --git a/client.go b/client.go
index 40a4bd2..693e4e6 100644
--- a/client.go
+++ b/client.go
@@ -185,7 +185,7 @@
go func() {
defer wg.Done()
- err := a.Session.Run(fmt.Sprintf("%s -qt %s", a.RemoteBinary, remotePath))
+ err := a.Session.Run(fmt.Sprintf("%s -qt %q", a.RemoteBinary, remotePath))
if err != nil {
errCh <- err
return
@@ -244,7 +244,7 @@
}
defer in.Close()
- err = a.Session.Start(fmt.Sprintf("%s -f %s", a.RemoteBinary, remotePath))
+ err = a.Session.Start(fmt.Sprintf("%s -f %q", a.RemoteBinary, remotePath))
if err != nil {
errCh <- err
return
diff --git a/tests/basic_test.go b/tests/basic_test.go
index 18fce6a..93c1895 100644
--- a/tests/basic_test.go
+++ b/tests/basic_test.go
@@ -1,109 +1,108 @@
package tests
import (
- scp "github.com/bramvdbogaerde/go-scp"
- "github.com/bramvdbogaerde/go-scp/auth"
- "golang.org/x/crypto/ssh"
"io/ioutil"
"os"
"strings"
"testing"
+
+ scp "github.com/bramvdbogaerde/go-scp"
+ "github.com/bramvdbogaerde/go-scp/auth"
+ "golang.org/x/crypto/ssh"
)
-// This test, tests the basic functionality of the library: copying files
-// it assumes that a docker container is running an SSH server at port
-// 2244 using password authentication.
-//
-// It also assumes that the directory /results is writable within that container
-// and is mapped to the tmp/ directory within this directory.
-func TestCopy(t *testing.T) {
- // Use SSH key authentication from the auth package
- // we ignore the host key in this example, please change this if you use this library
+func establishConnection(t *testing.T) scp.Client {
+ // Use SSH key authentication from the auth package.
+ // During testing we ignore the host key, don't to that when you use this.
clientConfig, _ := auth.PasswordKey("bram", "test", ssh.InsecureIgnoreHostKey())
- // For other authentication methods see ssh.ClientConfig and ssh.AuthMethod
-
- // Create a new SCP client
+ // Create a new SCP client.
client := scp.NewClient("127.0.0.1:2244", &clientConfig)
- // Connect to the remote server
+ // Connect to the remote server.
err := client.Connect()
if err != nil {
- t.Errorf("Couldn't establish a connection to the remote server %s", err)
- return
+ t.Fatalf("Couldn't establish a connection to the remote server: %s", err)
}
+ return client
+}
- // Open a file
- f, _ := os.Open("./input.txt")
-
- // Close client connection after the file has been copied
+// TestCopy tests the basic functionality of copying a file to the remote
+// destination.
+//
+// It assumes that a Docker container is running an SSH server at port 2244
+// that is using password authentication. It also assumes that the directory
+// /data is writable within that container and is mapped to ./tmp/ within the
+// directory the test is run from.
+func TestCopy(t *testing.T) {
+ client := establishConnection(t)
defer client.Close()
- // Close the file after it has been copied
+ // Open a file we can transfer to the remote container.
+ f, _ := os.Open("./data/upload_file.txt")
defer f.Close()
- // Finaly, copy the file over
- // Usage: CopyFile(fileReader, remotePath, permission)
+ // Create a file name with exotic characters and spaces in them.
+ // If this test works for this, simpler files should not be a problem.
+ filename := "Exöt1ç uploaded file.txt"
- err = client.CopyFile(f, "/data/output.txt", "0655")
-
+ // Finaly, copy the file over.
+ // Usage: CopyFile(fileReader, remotePath, permission).
+ err := client.CopyFile(f, "/data/"+filename, "0777")
if err != nil {
- t.Errorf("Error while copying file %s", err)
+ t.Errorf("Error while copying file: %s", err)
}
- content, err := ioutil.ReadFile("./tmp/output.txt")
+ // Read what the receiver have written to disk.
+ content, err := ioutil.ReadFile("./tmp/" + filename)
if err != nil {
- t.Errorf("Test has failed, file could not be opened")
+ t.Errorf("Result file could not be read: %s", err)
}
text := string(content)
expected := "It Works\n"
if strings.Compare(text, expected) != 0 {
- t.Errorf("Got different text than expected, expected \"%s\" got, \"%s\"", expected, text)
+ t.Errorf("Got different text than expected, expected %q got, %q", expected, text)
}
}
-// This test assumes that a Docker container is running that has the SCP binary available
-// and exposes an SSH server on port 2244 using password authentication.
+// TestDownloadFile tests the basic functionality of copying a file from the
+// remote destination.
//
-// The test checks whether it can retrieve a file from the remote and checks the file against the expected file
+// It assumes that a Docker container is running an SSH server at port 2244
+// that is using password authentication. It also assumes that the directory
+// /data is writable within that container and is mapped to ./tmp/ within the
+// directory the test is run from.
func TestDownloadFile(t *testing.T) {
- // Use SSH key authentication from the auth package
- // we ignore the host key in this example, please change this if you use this library
- clientConfig, _ := auth.PasswordKey("bram", "test", ssh.InsecureIgnoreHostKey())
+ client := establishConnection(t)
+ defer client.Close()
- // For other authentication methods see ssh.ClientConfig and ssh.AuthMethod
+ // Open a file we can transfer to the remote container.
+ f, _ := os.Open("./data/input.txt")
+ defer f.Close()
- // Create a new SCP client
- client := scp.NewClient("127.0.0.1:2244", &clientConfig)
-
- // Connect to the remote server
- err := client.Connect()
- if err != nil {
- t.Errorf("Couldn't establish a connection to the remote server %s", err)
- return
- }
-
- f, err := os.OpenFile("./tmp/output.txt", os.O_RDWR|os.O_CREATE, 0755)
+ // Create a local file to write to.
+ f, err := os.OpenFile("./tmp/output.txt", os.O_RDWR|os.O_CREATE, 0777)
if err != nil {
t.Errorf("Couldn't open the output file")
}
-
- // Close client connection after the file has been copied
- defer client.Close()
-
- // Close the file after it has been copied
defer f.Close()
- err = client.CopyFromRemote(f, "/input/test_download.txt")
+ // Use a file name with exotic characters and spaces in them.
+ // If this test works for this, simpler files should not be a problem.
+ err = client.CopyFromRemote(f, "/input/Exöt1ç download file.txt.txt")
if err != nil {
t.Errorf("Copy failed from remote")
}
content, err := ioutil.ReadFile("./tmp/output.txt")
+ if err != nil {
+ t.Errorf("Result file could not be read: %s", err)
+ }
+
text := string(content)
expected := "It works for download!\n"
if strings.Compare(text, expected) != 0 {
- t.Errorf("Got different text than expected, expected \"%s\" got, \"%s\"", expected, text)
+ t.Errorf("Got different text than expected, expected %q got, %q", expected, text)
}
}
diff --git a/tests/data/test_download.txt "b/tests/data/Ex\303\266t1\303\247 download file.txt.txt"
similarity index 100%
rename from tests/data/test_download.txt
rename to "tests/data/Ex\303\266t1\303\247 download file.txt.txt"
diff --git a/tests/input.txt b/tests/data/upload_file.txt
similarity index 100%
rename from tests/input.txt
rename to tests/data/upload_file.txt
diff --git a/tests/run_all.sh b/tests/run_all.sh
index fa0593a..0219fa3 100755
--- a/tests/run_all.sh
+++ b/tests/run_all.sh
@@ -4,7 +4,7 @@
echo "Running from $(pwd)"
-echo "Starting docker containers"
+echo "Starting docker containers"
docker run -d \
--name go-scp-test \
diff --git a/tests/tmp/.gitkeep b/tests/tmp/.gitkeep
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/tmp/.gitkeep