2016-06-06 23:57:42 +02:00
|
|
|
package test_helpers
|
2015-11-12 21:02:44 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
2015-11-14 17:16:17 +01:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2016-02-07 13:28:55 +01:00
|
|
|
"path/filepath"
|
2015-12-13 20:10:52 +01:00
|
|
|
"syscall"
|
2015-11-12 21:02:44 +01:00
|
|
|
"testing"
|
2015-11-27 23:34:55 +01:00
|
|
|
|
2016-02-06 19:20:54 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/nametransform"
|
2015-11-12 21:02:44 +01:00
|
|
|
)
|
|
|
|
|
2015-11-15 13:38:19 +01:00
|
|
|
// Note: the code assumes that all have a trailing slash
|
2016-06-06 23:57:42 +02:00
|
|
|
const TmpDir = "/tmp/gocryptfs_main_test/"
|
|
|
|
const DefaultPlainDir = TmpDir + "plain/"
|
|
|
|
const DefaultCipherDir = TmpDir + "cipher/"
|
2015-11-12 21:02:44 +01:00
|
|
|
|
2016-06-06 23:57:42 +02:00
|
|
|
const GocryptfsBinary = "../../gocryptfs"
|
2015-11-15 13:38:19 +01:00
|
|
|
|
2016-06-06 23:57:42 +02:00
|
|
|
// ResetTmpDir - delete old tmp dir, create new one, write gocryptfs.diriv
|
|
|
|
func ResetTmpDir(plaintextNames bool) {
|
2015-11-12 21:02:44 +01:00
|
|
|
|
2016-04-10 19:43:37 +02:00
|
|
|
// Try to unmount everything
|
2016-06-06 23:57:42 +02:00
|
|
|
entries, err := ioutil.ReadDir(TmpDir)
|
2016-04-10 19:43:37 +02:00
|
|
|
if err == nil {
|
|
|
|
for _, e := range entries {
|
2016-06-06 23:57:42 +02:00
|
|
|
fu := exec.Command("fusermount", "-z", "-u", filepath.Join(TmpDir, e.Name()))
|
2016-04-10 19:43:37 +02:00
|
|
|
fu.Run()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-06 23:57:42 +02:00
|
|
|
err = os.RemoveAll(TmpDir)
|
2015-11-15 15:05:15 +01:00
|
|
|
if err != nil {
|
2016-01-24 13:07:18 +01:00
|
|
|
fmt.Println("resetTmpDir: RemoveAll:" + err.Error())
|
2015-11-15 15:05:15 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2015-11-12 21:02:44 +01:00
|
|
|
|
2016-06-06 23:57:42 +02:00
|
|
|
err = os.MkdirAll(DefaultPlainDir, 0777)
|
2015-11-12 21:02:44 +01:00
|
|
|
if err != nil {
|
2015-11-15 15:05:15 +01:00
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
2015-11-12 21:02:44 +01:00
|
|
|
}
|
|
|
|
|
2016-06-06 23:57:42 +02:00
|
|
|
err = os.MkdirAll(DefaultCipherDir, 0777)
|
2015-11-12 21:02:44 +01:00
|
|
|
if err != nil {
|
2015-11-15 15:05:15 +01:00
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
2015-11-12 21:02:44 +01:00
|
|
|
}
|
2016-02-07 13:28:55 +01:00
|
|
|
if !plaintextNames {
|
2016-06-06 23:57:42 +02:00
|
|
|
err = nametransform.WriteDirIV(DefaultCipherDir)
|
2016-02-07 13:28:55 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2015-11-27 21:50:11 +01:00
|
|
|
}
|
2015-11-12 21:02:44 +01:00
|
|
|
}
|
|
|
|
|
2016-06-06 23:57:42 +02:00
|
|
|
// Mount CIPHERDIR "c" on PLAINDIR "p"
|
|
|
|
func Mount(c string, p string, extraArgs ...string) error {
|
2015-11-12 21:02:44 +01:00
|
|
|
var args []string
|
|
|
|
args = append(args, extraArgs...)
|
2016-02-07 13:28:55 +01:00
|
|
|
args = append(args, "-nosyslog", "-q", "-wpanic")
|
|
|
|
//args = append(args, "-fusedebug")
|
|
|
|
//args = append(args, "-d")
|
2015-11-15 15:05:15 +01:00
|
|
|
args = append(args, c)
|
|
|
|
args = append(args, p)
|
2016-06-06 23:57:42 +02:00
|
|
|
cmd := exec.Command(GocryptfsBinary, args...)
|
2016-02-06 19:20:54 +01:00
|
|
|
cmd.Stderr = os.Stderr
|
2016-06-06 23:57:42 +02:00
|
|
|
if testing.Verbose() {
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
}
|
2016-06-06 22:30:39 +02:00
|
|
|
return cmd.Run()
|
|
|
|
}
|
|
|
|
|
2016-06-06 23:57:42 +02:00
|
|
|
// MountOrExit calls mount() and exits on failure.
|
|
|
|
func MountOrExit(c string, p string, extraArgs ...string) {
|
|
|
|
err := Mount(c, p, extraArgs...)
|
2015-11-12 21:02:44 +01:00
|
|
|
if err != nil {
|
2016-06-06 22:30:39 +02:00
|
|
|
fmt.Printf("mount failed: %v", err)
|
2015-11-12 21:02:44 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-06 23:57:42 +02:00
|
|
|
// MountOrFatal calls mount() and calls t.Fatal() on failure.
|
|
|
|
func MountOrFatal(t *testing.T, c string, p string, extraArgs ...string) {
|
|
|
|
err := Mount(c, p, extraArgs...)
|
2016-06-06 22:30:39 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(fmt.Errorf("mount failed: %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-06 23:57:42 +02:00
|
|
|
// Unmount PLAINDIR "p"
|
|
|
|
func Unmount(p string) error {
|
2015-11-15 15:05:15 +01:00
|
|
|
fu := exec.Command("fusermount", "-u", "-z", p)
|
2015-11-12 21:02:44 +01:00
|
|
|
fu.Stdout = os.Stdout
|
|
|
|
fu.Stderr = os.Stderr
|
2015-11-15 15:05:15 +01:00
|
|
|
err := fu.Run()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
return err
|
2015-11-12 21:02:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return md5 string for file "filename"
|
2016-06-06 23:57:42 +02:00
|
|
|
func Md5fn(filename string) string {
|
2015-11-12 21:02:44 +01:00
|
|
|
buf, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ReadFile: %v\n", err)
|
|
|
|
return ""
|
|
|
|
}
|
2016-06-06 23:57:42 +02:00
|
|
|
return Md5hex(buf)
|
2015-11-12 21:02:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return md5 string for "buf"
|
2016-06-06 23:57:42 +02:00
|
|
|
func Md5hex(buf []byte) string {
|
2015-11-12 21:02:44 +01:00
|
|
|
rawHash := md5.Sum(buf)
|
|
|
|
hash := hex.EncodeToString(rawHash[:])
|
|
|
|
return hash
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the file size equals "want". This checks:
|
|
|
|
// 1) Size reported by Stat()
|
|
|
|
// 2) Number of bytes returned when reading the whole file
|
2016-06-06 23:57:42 +02:00
|
|
|
func VerifySize(t *testing.T, path string, want int) {
|
2015-11-12 21:02:44 +01:00
|
|
|
buf, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ReadFile failed: %v", err)
|
|
|
|
} else if len(buf) != want {
|
|
|
|
t.Errorf("wrong read size: got=%d want=%d", len(buf), want)
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Stat failed: %v", err)
|
|
|
|
} else if fi.Size() != int64(want) {
|
|
|
|
t.Errorf("wrong stat file size, got=%d want=%d", fi.Size(), want)
|
|
|
|
}
|
|
|
|
}
|
2015-12-10 20:02:18 +01:00
|
|
|
|
|
|
|
// Create and delete a directory
|
2016-06-06 23:57:42 +02:00
|
|
|
func TestMkdirRmdir(t *testing.T, plainDir string) {
|
2015-12-10 20:02:18 +01:00
|
|
|
dir := plainDir + "dir1"
|
|
|
|
err := os.Mkdir(dir, 0777)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = syscall.Rmdir(dir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-12-11 19:54:53 +01:00
|
|
|
|
|
|
|
// Removing a non-empty dir should fail with ENOTEMPTY
|
|
|
|
if os.Mkdir(dir, 0777) != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f, err := os.Create(dir + "/file")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
err = syscall.Rmdir(dir)
|
|
|
|
errno := err.(syscall.Errno)
|
|
|
|
if errno != syscall.ENOTEMPTY {
|
|
|
|
t.Errorf("Should have gotten ENOTEMPTY, go %v", errno)
|
|
|
|
}
|
2015-12-13 20:10:52 +01:00
|
|
|
if syscall.Unlink(dir+"/file") != nil {
|
2015-12-11 19:54:53 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if syscall.Rmdir(dir) != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should also be able to remove a directory we do not have permissions to
|
|
|
|
// read or write
|
|
|
|
err = os.Mkdir(dir, 0000)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = syscall.Rmdir(dir)
|
|
|
|
if err != nil {
|
2016-04-10 19:43:37 +02:00
|
|
|
// Make sure the directory can cleaned up by the next test run
|
|
|
|
os.Chmod(dir, 0700)
|
2015-12-11 19:54:53 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-12-10 20:02:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create and rename a file
|
2016-06-06 23:57:42 +02:00
|
|
|
func TestRename(t *testing.T, plainDir string) {
|
2015-12-13 20:10:52 +01:00
|
|
|
file1 := plainDir + "rename1"
|
|
|
|
file2 := plainDir + "rename2"
|
2015-12-10 20:02:18 +01:00
|
|
|
err := ioutil.WriteFile(file1, []byte("content"), 0777)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = syscall.Rename(file1, file2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
syscall.Unlink(file2)
|
|
|
|
}
|
2016-02-07 10:55:13 +01:00
|
|
|
|
|
|
|
// verifyExistence - check in 3 ways that "path" exists:
|
|
|
|
// stat, open, readdir
|
2016-06-06 23:57:42 +02:00
|
|
|
func VerifyExistence(path string) bool {
|
2016-02-07 10:55:13 +01:00
|
|
|
|
|
|
|
// Check that file can be stated
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
//t.Log(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that file can be opened
|
|
|
|
fd, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
//t.Log(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
|
|
|
|
// Check that file shows up in directory listing
|
|
|
|
dir := filepath.Dir(path)
|
|
|
|
name := filepath.Base(path)
|
|
|
|
fi, err := ioutil.ReadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
//t.Log(err)
|
|
|
|
return false
|
|
|
|
}
|
2016-02-07 13:28:55 +01:00
|
|
|
for _, i := range fi {
|
2016-02-07 10:55:13 +01:00
|
|
|
if i.Name() == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|