2015-12-30 10:16:22 +03:00
|
|
|
package afero
|
|
|
|
|
|
|
|
import (
|
2016-01-12 08:34:15 +03:00
|
|
|
"os"
|
2016-02-15 15:34:47 +03:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2016-01-11 23:30:26 +03:00
|
|
|
"testing"
|
2017-11-29 21:44:12 +03:00
|
|
|
"fmt"
|
2015-12-30 10:16:22 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBasePath(t *testing.T) {
|
2016-01-11 23:30:26 +03:00
|
|
|
baseFs := &MemMapFs{}
|
|
|
|
baseFs.MkdirAll("/base/path/tmp", 0777)
|
2016-01-12 05:41:03 +03:00
|
|
|
bp := NewBasePathFs(baseFs, "/base/path")
|
2015-12-30 10:16:22 +03:00
|
|
|
|
2016-01-11 23:30:26 +03:00
|
|
|
if _, err := bp.Create("/tmp/foo"); err != nil {
|
|
|
|
t.Errorf("Failed to set real path")
|
|
|
|
}
|
2015-12-30 10:16:22 +03:00
|
|
|
|
2016-01-11 23:30:26 +03:00
|
|
|
if fh, err := bp.Create("../tmp/bar"); err == nil {
|
|
|
|
t.Errorf("succeeded in creating %s ...", fh.Name())
|
|
|
|
}
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
2016-01-12 08:34:15 +03:00
|
|
|
|
|
|
|
func TestBasePathRoot(t *testing.T) {
|
|
|
|
baseFs := &MemMapFs{}
|
|
|
|
baseFs.MkdirAll("/base/path/foo/baz", 0777)
|
|
|
|
baseFs.MkdirAll("/base/path/boo/", 0777)
|
|
|
|
bp := NewBasePathFs(baseFs, "/base/path")
|
|
|
|
|
|
|
|
rd, err := ReadDir(bp, string(os.PathSeparator))
|
|
|
|
|
|
|
|
if len(rd) != 2 {
|
|
|
|
t.Errorf("base path doesn't respect root")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2016-02-15 15:34:47 +03:00
|
|
|
|
|
|
|
func TestRealPath(t *testing.T) {
|
|
|
|
fs := NewOsFs()
|
|
|
|
baseDir, err := TempDir(fs, "", "base")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("error creating tempDir", err)
|
|
|
|
}
|
|
|
|
defer fs.RemoveAll(baseDir)
|
|
|
|
anotherDir, err := TempDir(fs, "", "another")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("error creating tempDir", err)
|
|
|
|
}
|
|
|
|
defer fs.RemoveAll(anotherDir)
|
|
|
|
|
|
|
|
bp := NewBasePathFs(fs, baseDir).(*BasePathFs)
|
|
|
|
|
|
|
|
subDir := filepath.Join(baseDir, "s1")
|
|
|
|
|
|
|
|
realPath, err := bp.RealPath("/s1")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Got error %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if realPath != subDir {
|
|
|
|
t.Errorf("Expected \n%s got \n%s", subDir, realPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
_, err = bp.RealPath(anotherDir)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// on *nix we have no way of just looking at the path and tell that anotherDir
|
|
|
|
// is not inside the base file system.
|
|
|
|
// The user will receive an os.ErrNotExist later.
|
|
|
|
surrealPath, err := bp.RealPath(anotherDir)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Got error %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
excpected := filepath.Join(baseDir, anotherDir)
|
|
|
|
|
|
|
|
if surrealPath != excpected {
|
|
|
|
t.Errorf("Expected \n%s got \n%s", excpected, surrealPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-04-18 20:29:19 +03:00
|
|
|
|
|
|
|
func TestNestedBasePaths(t *testing.T) {
|
|
|
|
type dirSpec struct {
|
|
|
|
Dir1, Dir2, Dir3 string
|
|
|
|
}
|
|
|
|
dirSpecs := []dirSpec{
|
|
|
|
dirSpec{Dir1: "/", Dir2: "/", Dir3: "/"},
|
|
|
|
dirSpec{Dir1: "/", Dir2: "/path2", Dir3: "/"},
|
|
|
|
dirSpec{Dir1: "/path1/dir", Dir2: "/path2/dir/", Dir3: "/path3/dir"},
|
2016-04-20 04:23:58 +03:00
|
|
|
dirSpec{Dir1: "C:/path1", Dir2: "path2/dir", Dir3: "/path3/dir/"},
|
2016-04-18 20:29:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, ds := range dirSpecs {
|
|
|
|
memFs := NewMemMapFs()
|
|
|
|
level1Fs := NewBasePathFs(memFs, ds.Dir1)
|
|
|
|
level2Fs := NewBasePathFs(level1Fs, ds.Dir2)
|
|
|
|
level3Fs := NewBasePathFs(level2Fs, ds.Dir3)
|
|
|
|
|
|
|
|
type spec struct {
|
2016-04-20 15:12:23 +03:00
|
|
|
BaseFs Fs
|
|
|
|
FileName string
|
2016-04-18 20:29:19 +03:00
|
|
|
}
|
|
|
|
specs := []spec{
|
2016-04-20 15:12:23 +03:00
|
|
|
spec{BaseFs: level3Fs, FileName: "f.txt"},
|
|
|
|
spec{BaseFs: level2Fs, FileName: "f.txt"},
|
|
|
|
spec{BaseFs: level1Fs, FileName: "f.txt"},
|
2016-04-18 20:29:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range specs {
|
2016-04-20 04:23:58 +03:00
|
|
|
if err := s.BaseFs.MkdirAll(s.FileName, 0755); err != nil {
|
2016-04-18 20:29:19 +03:00
|
|
|
t.Errorf("Got error %s", err.Error())
|
2016-04-20 04:23:58 +03:00
|
|
|
}
|
|
|
|
if _, err := s.BaseFs.Stat(s.FileName); err != nil {
|
|
|
|
t.Errorf("Got error %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.BaseFs == level3Fs {
|
|
|
|
pathToExist := filepath.Join(ds.Dir3, s.FileName)
|
|
|
|
if _, err := level2Fs.Stat(pathToExist); err != nil {
|
|
|
|
t.Errorf("Got error %s (path %s)", err.Error(), pathToExist)
|
|
|
|
}
|
|
|
|
} else if s.BaseFs == level2Fs {
|
|
|
|
pathToExist := filepath.Join(ds.Dir2, ds.Dir3, s.FileName)
|
|
|
|
if _, err := level1Fs.Stat(pathToExist); err != nil {
|
|
|
|
t.Errorf("Got error %s (path %s)", err.Error(), pathToExist)
|
|
|
|
}
|
2016-04-18 20:29:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-29 21:44:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
func TestBasePathOpenFile(t *testing.T) {
|
|
|
|
baseFs := &MemMapFs{}
|
|
|
|
baseFs.MkdirAll("/base/path/tmp", 0777)
|
|
|
|
bp := NewBasePathFs(baseFs, "/base/path")
|
|
|
|
f, err := bp.OpenFile("/tmp/file.txt", os.O_CREATE, 0600)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to open file: %v", err)
|
|
|
|
}
|
2017-11-29 22:01:03 +03:00
|
|
|
if filepath.Dir(f.Name()) != filepath.Clean("/tmp") {
|
2017-11-29 21:44:12 +03:00
|
|
|
t.Fatalf("realpath leaked: %s", f.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasePathCreate(t *testing.T) {
|
|
|
|
baseFs := &MemMapFs{}
|
|
|
|
baseFs.MkdirAll("/base/path/tmp", 0777)
|
|
|
|
bp := NewBasePathFs(baseFs, "/base/path")
|
|
|
|
f, err := bp.Create("/tmp/file.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create file: %v", err)
|
|
|
|
}
|
2017-11-29 22:01:03 +03:00
|
|
|
if filepath.Dir(f.Name()) != filepath.Clean("/tmp") {
|
2017-11-29 21:44:12 +03:00
|
|
|
t.Fatalf("realpath leaked: %s", f.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasePathTempFile(t *testing.T) {
|
|
|
|
baseFs := &MemMapFs{}
|
|
|
|
baseFs.MkdirAll("/base/path/tmp", 0777)
|
|
|
|
bp := NewBasePathFs(baseFs, "/base/path")
|
|
|
|
|
|
|
|
tDir, err := TempDir(bp, "/tmp", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to TempDir: %v", err)
|
|
|
|
}
|
|
|
|
fmt.Println(tDir)
|
2017-11-29 22:01:03 +03:00
|
|
|
if filepath.Dir(tDir) != filepath.Clean("/tmp") {
|
2017-11-29 21:44:12 +03:00
|
|
|
t.Fatalf("Tempdir realpath leaked: %s", tDir)
|
|
|
|
}
|
|
|
|
tempFile, err := TempFile(bp, tDir, "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to TempFile: %v", err)
|
|
|
|
}
|
|
|
|
defer tempFile.Close()
|
|
|
|
if expected, actual := tDir, filepath.Dir(tempFile.Name()); expected != actual {
|
|
|
|
t.Fatalf("TempFile realpath leaked: expected %s, got %s", expected, actual)
|
|
|
|
}
|
|
|
|
}
|