archive_windows_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // +build windows
  2. package archive
  3. import (
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. )
  9. func TestCopyFileWithInvalidDest(t *testing.T) {
  10. // TODO Windows: This is currently failing. Not sure what has
  11. // recently changed in CopyWithTar as used to pass. Further investigation
  12. // is required.
  13. t.Skip("Currently fails")
  14. folder, err := ioutil.TempDir("", "docker-archive-test")
  15. if err != nil {
  16. t.Fatal(err)
  17. }
  18. defer os.RemoveAll(folder)
  19. dest := "c:dest"
  20. srcFolder := filepath.Join(folder, "src")
  21. src := filepath.Join(folder, "src", "src")
  22. err = os.MkdirAll(srcFolder, 0740)
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. ioutil.WriteFile(src, []byte("content"), 0777)
  27. err = CopyWithTar(src, dest)
  28. if err == nil {
  29. t.Fatalf("archiver.CopyWithTar should throw an error on invalid dest.")
  30. }
  31. }
  32. func TestCanonicalTarNameForPath(t *testing.T) {
  33. cases := []struct {
  34. in, expected string
  35. shouldFail bool
  36. }{
  37. {"foo", "foo", false},
  38. {"foo/bar", "___", true}, // unix-styled windows path must fail
  39. {`foo\bar`, "foo/bar", false},
  40. }
  41. for _, v := range cases {
  42. if out, err := CanonicalTarNameForPath(v.in); err != nil && !v.shouldFail {
  43. t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err)
  44. } else if v.shouldFail && err == nil {
  45. t.Fatalf("canonical path call should have failed with error. in=%s out=%s", v.in, out)
  46. } else if !v.shouldFail && out != v.expected {
  47. t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, out)
  48. }
  49. }
  50. }
  51. func TestCanonicalTarName(t *testing.T) {
  52. cases := []struct {
  53. in string
  54. isDir bool
  55. expected string
  56. }{
  57. {"foo", false, "foo"},
  58. {"foo", true, "foo/"},
  59. {`foo\bar`, false, "foo/bar"},
  60. {`foo\bar`, true, "foo/bar/"},
  61. }
  62. for _, v := range cases {
  63. if out, err := canonicalTarName(v.in, v.isDir); err != nil {
  64. t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err)
  65. } else if out != v.expected {
  66. t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, out)
  67. }
  68. }
  69. }
  70. func TestChmodTarEntry(t *testing.T) {
  71. cases := []struct {
  72. in, expected os.FileMode
  73. }{
  74. {0000, 0111},
  75. {0777, 0755},
  76. {0644, 0755},
  77. {0755, 0755},
  78. {0444, 0555},
  79. {0755 | os.ModeDir, 0755 | os.ModeDir},
  80. {0755 | os.ModeSymlink, 0755 | os.ModeSymlink},
  81. }
  82. for _, v := range cases {
  83. if out := chmodTarEntry(v.in); out != v.expected {
  84. t.Fatalf("wrong chmod. expected:%v got:%v", v.expected, out)
  85. }
  86. }
  87. }