PageRenderTime 64ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/pkg/symlink/fs_test.go

https://github.com/dotcloud/docker
Go | 121 lines | 95 code | 26 blank | 0 comment | 34 complexity | 29dcb2cc46e2cae2d49171a23ef29731 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-2-Clause, CC-BY-4.0, 0BSD, CC-BY-SA-4.0, GPL-2.0, BSD-3-Clause, MIT
  1. package symlink
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. )
  8. func abs(t *testing.T, p string) string {
  9. o, err := filepath.Abs(p)
  10. if err != nil {
  11. t.Fatal(err)
  12. }
  13. return o
  14. }
  15. func TestFollowSymLinkNormal(t *testing.T) {
  16. link := "testdata/fs/a/d/c/data"
  17. rewrite, err := FollowSymlinkInScope(link, "testdata")
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. if expected := abs(t, "testdata/b/c/data"); expected != rewrite {
  22. t.Fatalf("Expected %s got %s", expected, rewrite)
  23. }
  24. }
  25. func TestFollowSymLinkRelativePath(t *testing.T) {
  26. link := "testdata/fs/i"
  27. rewrite, err := FollowSymlinkInScope(link, "testdata")
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. if expected := abs(t, "testdata/fs/a"); expected != rewrite {
  32. t.Fatalf("Expected %s got %s", expected, rewrite)
  33. }
  34. }
  35. func TestFollowSymLinkUnderLinkedDir(t *testing.T) {
  36. dir, err := ioutil.TempDir("", "docker-fs-test")
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. os.Mkdir(filepath.Join(dir, "realdir"), 0700)
  41. os.Symlink("realdir", filepath.Join(dir, "linkdir"))
  42. linkDir := filepath.Join(dir, "linkdir", "foo")
  43. dirUnderLinkDir := filepath.Join(dir, "linkdir", "foo", "bar")
  44. os.MkdirAll(dirUnderLinkDir, 0700)
  45. rewrite, err := FollowSymlinkInScope(dirUnderLinkDir, linkDir)
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. if rewrite != dirUnderLinkDir {
  50. t.Fatalf("Expected %s got %s", dirUnderLinkDir, rewrite)
  51. }
  52. }
  53. func TestFollowSymLinkRandomString(t *testing.T) {
  54. if _, err := FollowSymlinkInScope("toto", "testdata"); err == nil {
  55. t.Fatal("Random string should fail but didn't")
  56. }
  57. }
  58. func TestFollowSymLinkLastLink(t *testing.T) {
  59. link := "testdata/fs/a/d"
  60. rewrite, err := FollowSymlinkInScope(link, "testdata")
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. if expected := abs(t, "testdata/b"); expected != rewrite {
  65. t.Fatalf("Expected %s got %s", expected, rewrite)
  66. }
  67. }
  68. func TestFollowSymLinkRelativeLink(t *testing.T) {
  69. link := "testdata/fs/a/e/c/data"
  70. rewrite, err := FollowSymlinkInScope(link, "testdata")
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. if expected := abs(t, "testdata/fs/b/c/data"); expected != rewrite {
  75. t.Fatalf("Expected %s got %s", expected, rewrite)
  76. }
  77. }
  78. func TestFollowSymLinkRelativeLinkScope(t *testing.T) {
  79. link := "testdata/fs/a/f"
  80. rewrite, err := FollowSymlinkInScope(link, "testdata")
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. if expected := abs(t, "testdata/test"); expected != rewrite {
  85. t.Fatalf("Expected %s got %s", expected, rewrite)
  86. }
  87. link = "testdata/fs/b/h"
  88. rewrite, err = FollowSymlinkInScope(link, "testdata")
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. if expected := abs(t, "testdata/root"); expected != rewrite {
  93. t.Fatalf("Expected %s got %s", expected, rewrite)
  94. }
  95. }