archive_windows_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. folder, err := ioutil.TempDir("", "docker-archive-test")
  11. if err != nil {
  12. t.Fatal(err)
  13. }
  14. defer os.RemoveAll(folder)
  15. dest := "c:dest"
  16. srcFolder := filepath.Join(folder, "src")
  17. src := filepath.Join(folder, "src", "src")
  18. err = os.MkdirAll(srcFolder, 0740)
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. ioutil.WriteFile(src, []byte("content"), 0777)
  23. err = CopyWithTar(src, dest)
  24. if err == nil {
  25. t.Fatalf("archiver.CopyWithTar should throw an error on invalid dest.")
  26. }
  27. }
  28. func TestCanonicalTarNameForPath(t *testing.T) {
  29. cases := []struct {
  30. in, expected string
  31. shouldFail bool
  32. }{
  33. {"foo", "foo", false},
  34. {"foo/bar", "___", true}, // unix-styled windows path must fail
  35. {`foo\bar`, "foo/bar", false},
  36. }
  37. for _, v := range cases {
  38. if out, err := CanonicalTarNameForPath(v.in); err != nil && !v.shouldFail {
  39. t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err)
  40. } else if v.shouldFail && err == nil {
  41. t.Fatalf("canonical path call should have failed with error. in=%s out=%s", v.in, out)
  42. } else if !v.shouldFail && out != v.expected {
  43. t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, out)
  44. }
  45. }
  46. }
  47. func TestCanonicalTarName(t *testing.T) {
  48. cases := []struct {
  49. in string
  50. isDir bool
  51. expected string
  52. }{
  53. {"foo", false, "foo"},
  54. {"foo", true, "foo/"},
  55. {`foo\bar`, false, "foo/bar"},
  56. {`foo\bar`, true, "foo/bar/"},
  57. }
  58. for _, v := range cases {
  59. if out, err := canonicalTarName(v.in, v.isDir); err != nil {
  60. t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err)
  61. } else if out != v.expected {
  62. t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, out)
  63. }
  64. }
  65. }
  66. func TestChmodTarEntry(t *testing.T) {
  67. cases := []struct {
  68. in, expected os.FileMode
  69. }{
  70. {0000, 0111},
  71. {0777, 0755},
  72. {0644, 0755},
  73. {0755, 0755},
  74. {0444, 0555},
  75. }
  76. for _, v := range cases {
  77. if out := chmodTarEntry(v.in); out != v.expected {
  78. t.Fatalf("wrong chmod. expected:%v got:%v", v.expected, out)
  79. }
  80. }
  81. }