Implements copying files from host machine to qemu
This commit is contained in:
		| @@ -13,6 +13,7 @@ import ( | ||||
| 	"os" | ||||
| 	"os/exec" | ||||
| 	"runtime" | ||||
| 	"strings" | ||||
| 	"syscall" | ||||
| 	"time" | ||||
|  | ||||
| @@ -262,3 +263,19 @@ func (q *QemuSystem) Command(user, cmd string) (output string, err error) { | ||||
| 	output = string(bytesOutput) | ||||
| 	return | ||||
| } | ||||
|  | ||||
| // CopyFile is copy file from local machine to remote through ssh/scp | ||||
| func (q *QemuSystem) CopyFile(user, localPath, remotePath string) (err error) { | ||||
| 	addrPort := strings.Split(q.sshAddrPort, ":") | ||||
| 	addr := addrPort[0] | ||||
| 	port := addrPort[1] | ||||
|  | ||||
| 	cmd := exec.Command("scp", "-P", port, "-o", "StrictHostKeyChecking=no", | ||||
| 		localPath, user+"@"+addr+":"+remotePath) | ||||
| 	output, err := cmd.CombinedOutput() | ||||
| 	if err != nil { | ||||
| 		return errors.New(string(output)) | ||||
| 	} | ||||
|  | ||||
| 	return | ||||
| } | ||||
|   | ||||
| @@ -5,6 +5,9 @@ | ||||
| package qemukernel | ||||
|  | ||||
| import ( | ||||
| 	"crypto/sha512" | ||||
| 	"fmt" | ||||
| 	"io/ioutil" | ||||
| 	"net" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| @@ -137,3 +140,35 @@ func TestQemuSystemCommand(t *testing.T) { | ||||
| 		t.Fatal("User have rights for /etc/shadow. WAT?!") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestQemuSystemCopyFile(t *testing.T) { | ||||
| 	qemu, err := startTestQemu() | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	defer qemu.Stop() | ||||
|  | ||||
| 	localPath := "/bin/sh" | ||||
|  | ||||
| 	content, err := ioutil.ReadFile(localPath) | ||||
| 	if err != nil { | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	sha_local := fmt.Sprintf("%x", sha512.Sum512(content)) | ||||
|  | ||||
| 	err = qemu.CopyFile("user", localPath, "/tmp/test") | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
|  | ||||
| 	sha_remote, err := qemu.Command("user", "sha512sum /tmp/test") | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	sha_remote = strings.Split(sha_remote, " ")[0] | ||||
|  | ||||
| 	if sha_local != sha_remote { | ||||
| 		t.Fatal(fmt.Sprintf("Broken file (%s instead of %s)", sha_remote, sha_local)) | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user