2015-11-12 21:02:44 +01:00
|
|
|
package integration_tests
|
|
|
|
|
|
|
|
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
|
2015-11-12 21:02:44 +01:00
|
|
|
const tmpDir = "/tmp/gocryptfs_main_test/"
|
2015-11-15 15:05:15 +01:00
|
|
|
const defaultPlainDir = tmpDir + "plain/"
|
|
|
|
const defaultCipherDir = tmpDir + "cipher/"
|
2015-11-12 21:02:44 +01:00
|
|
|
|
2015-11-15 13:38:19 +01:00
|
|
|
const gocryptfsBinary = "../gocryptfs"
|
|
|
|
|
2015-12-11 23:27:38 +01:00
|
|
|
// resetTmpDir - delete old tmp dir, create new one, write gocryptfs.diriv
|
2016-02-07 13:28:55 +01:00
|
|
|
func resetTmpDir(plaintextNames bool) {
|
2015-11-12 21:02:44 +01:00
|
|
|
|
2016-04-10 19:43:37 +02:00
|
|
|
// Try to unmount everything
|
|
|
|
entries, err := ioutil.ReadDir(tmpDir)
|
|
|
|
if err == nil {
|
|
|
|
for _, e := range entries {
|
|
|
|
fu := exec.Command("fusermount", "-z", "-u", filepath.Join(tmpDir, e.Name()))
|
|
|
|
fu.Run()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-11-15 15:05:15 +01: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
|
|
|
}
|
|
|
|
|
2015-11-15 15:05:15 +01: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 {
|
|
|
|
err = nametransform.WriteDirIV(defaultCipherDir)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-11-15 15:05:15 +01:00
|
|
|
// mount CIPHERDIR "c" on PLAINDIR "p"
|
|
|
|
func mount(c string, p string, extraArgs ...string) {
|
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)
|
|
|
|
cmd := exec.Command(gocryptfsBinary, args...)
|
2016-02-06 19:20:54 +01:00
|
|
|
cmd.Stderr = os.Stderr
|
2015-11-15 13:38:19 +01:00
|
|
|
if testing.Verbose() {
|
2015-11-15 15:05:15 +01:00
|
|
|
cmd.Stdout = os.Stdout
|
2015-11-15 13:38:19 +01:00
|
|
|
}
|
2015-11-15 15:05:15 +01:00
|
|
|
err := cmd.Run()
|
2015-11-12 21:02:44 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-15 15:05:15 +01:00
|
|
|
// unmount PLAINDIR "p"
|
|
|
|
func unmount(p string) error {
|
|
|
|
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"
|
|
|
|
func md5fn(filename string) string {
|
|
|
|
buf, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ReadFile: %v\n", err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return md5hex(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return md5 string for "buf"
|
|
|
|
func md5hex(buf []byte) string {
|
|
|
|
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
|
|
|
|
func verifySize(t *testing.T, path string, want int) {
|
|
|
|
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
|
|
|
|
func testMkdirRmdir(t *testing.T, plainDir string) {
|
|
|
|
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
|
|
|
|
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
|
|
|
|
func verifyExistence(path string) bool {
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|