Unit tests.

This commit is contained in:
Matthew Hooker 2013-09-26 02:25:57 -07:00
parent 23a331fc00
commit e1dadfc57a
2 changed files with 43 additions and 1 deletions

View File

@ -6,7 +6,7 @@ import (
)
func copySingle(dest string, src string, copyCommand string) error {
cpCommand := fmt.Sprintf("sudo cp -fn %s %s", src, dest)
cpCommand := fmt.Sprintf("%s %s %s", copyCommand, src, dest)
localCmd := exec.Command("/bin/sh", "-c", cpCommand)
if err := localCmd.Run(); err != nil {
return err

View File

@ -0,0 +1,42 @@
package chroot
import (
"io/ioutil"
"log"
"os"
"testing"
)
func TestCopyFile(t *testing.T) {
first, err := ioutil.TempFile("", "copy_files_test")
if err != nil {
t.Fatalf("couldn't create temp file.")
}
defer os.Remove(first.Name())
newName := first.Name() + "-new"
payload := "copy_files_test.go payload"
if _, err = first.WriteString(payload); err != nil {
t.Fatalf("Couldn't write payload to first file.")
}
if err := copySingle(newName, first.Name(), "cp"); err != nil {
t.Fatalf("Couldn't copy file")
}
defer os.Remove(newName)
second, err := os.Open(newName)
if err != nil {
t.Fatalf("Couldn't open copied file.")
}
defer second.Close()
var copiedPayload = make([]byte, len(payload))
if _, err := second.Read(copiedPayload); err != nil {
t.Fatalf("Couldn't open copied file for reading.")
}
if string(copiedPayload) != payload {
t.Fatalf("payload not copied.")
}
}