2014-05-13 13:34:30 -04:00
|
|
|
package symlink
|
2013-12-11 19:31:02 -05:00
|
|
|
|
|
|
|
import (
|
2014-04-11 17:42:53 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2013-12-11 19:31:02 -05:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
func abs(t *testing.T, p string) string {
|
2013-12-11 19:31:02 -05:00
|
|
|
o, err := filepath.Abs(p)
|
|
|
|
if err != nil {
|
2013-12-11 20:12:53 -05:00
|
|
|
t.Fatal(err)
|
2013-12-11 19:31:02 -05:00
|
|
|
}
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFollowSymLinkNormal(t *testing.T) {
|
|
|
|
link := "testdata/fs/a/d/c/data"
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
rewrite, err := FollowSymlinkInScope(link, "testdata")
|
2013-12-11 19:31:02 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
if expected := abs(t, "testdata/b/c/data"); expected != rewrite {
|
2013-12-11 19:31:02 -05:00
|
|
|
t.Fatalf("Expected %s got %s", expected, rewrite)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 17:52:36 -04:00
|
|
|
func TestFollowSymLinkRelativePath(t *testing.T) {
|
|
|
|
link := "testdata/fs/i"
|
|
|
|
|
|
|
|
rewrite, err := FollowSymlinkInScope(link, "testdata")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected := abs(t, "testdata/fs/a"); expected != rewrite {
|
|
|
|
t.Fatalf("Expected %s got %s", expected, rewrite)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-11 17:42:53 -04:00
|
|
|
func TestFollowSymLinkUnderLinkedDir(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "docker-fs-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Mkdir(filepath.Join(dir, "realdir"), 0700)
|
|
|
|
os.Symlink("realdir", filepath.Join(dir, "linkdir"))
|
|
|
|
|
|
|
|
linkDir := filepath.Join(dir, "linkdir", "foo")
|
|
|
|
dirUnderLinkDir := filepath.Join(dir, "linkdir", "foo", "bar")
|
|
|
|
os.MkdirAll(dirUnderLinkDir, 0700)
|
|
|
|
|
|
|
|
rewrite, err := FollowSymlinkInScope(dirUnderLinkDir, linkDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rewrite != dirUnderLinkDir {
|
|
|
|
t.Fatalf("Expected %s got %s", dirUnderLinkDir, rewrite)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 19:31:02 -05:00
|
|
|
func TestFollowSymLinkRandomString(t *testing.T) {
|
2013-12-11 20:12:53 -05:00
|
|
|
if _, err := FollowSymlinkInScope("toto", "testdata"); err == nil {
|
|
|
|
t.Fatal("Random string should fail but didn't")
|
2013-12-11 19:31:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFollowSymLinkLastLink(t *testing.T) {
|
|
|
|
link := "testdata/fs/a/d"
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
rewrite, err := FollowSymlinkInScope(link, "testdata")
|
2013-12-11 19:31:02 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
if expected := abs(t, "testdata/b"); expected != rewrite {
|
2013-12-11 19:31:02 -05:00
|
|
|
t.Fatalf("Expected %s got %s", expected, rewrite)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFollowSymLinkRelativeLink(t *testing.T) {
|
|
|
|
link := "testdata/fs/a/e/c/data"
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
rewrite, err := FollowSymlinkInScope(link, "testdata")
|
2013-12-11 19:31:02 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
if expected := abs(t, "testdata/fs/b/c/data"); expected != rewrite {
|
2013-12-11 19:31:02 -05:00
|
|
|
t.Fatalf("Expected %s got %s", expected, rewrite)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFollowSymLinkRelativeLinkScope(t *testing.T) {
|
|
|
|
link := "testdata/fs/a/f"
|
2013-12-11 20:12:53 -05:00
|
|
|
|
|
|
|
rewrite, err := FollowSymlinkInScope(link, "testdata")
|
2013-12-11 19:31:02 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
if expected := abs(t, "testdata/test"); expected != rewrite {
|
|
|
|
t.Fatalf("Expected %s got %s", expected, rewrite)
|
|
|
|
}
|
|
|
|
|
|
|
|
link = "testdata/fs/b/h"
|
2013-12-11 19:31:02 -05:00
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
rewrite, err = FollowSymlinkInScope(link, "testdata")
|
2013-12-11 19:31:02 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
if expected := abs(t, "testdata/root"); expected != rewrite {
|
2013-12-11 19:31:02 -05:00
|
|
|
t.Fatalf("Expected %s got %s", expected, rewrite)
|
|
|
|
}
|
|
|
|
}
|