archive_windows_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // +build windows
  2. package archive
  3. import (
  4. "os"
  5. "testing"
  6. )
  7. func TestCanonicalTarNameForPath(t *testing.T) {
  8. cases := []struct {
  9. in, expected string
  10. shouldFail bool
  11. }{
  12. {"foo", "foo", false},
  13. {"foo/bar", "___", true}, // unix-styled windows path must fail
  14. {`foo\bar`, "foo/bar", false},
  15. {`foo\bar`, "foo/bar/", false},
  16. }
  17. for _, v := range cases {
  18. if out, err := CanonicalTarNameForPath(v.in); err != nil && !v.shouldFail {
  19. t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err)
  20. } else if v.shouldFail && err == nil {
  21. t.Fatalf("canonical path call should have pailed with error. in=%s out=%s", v.in, out)
  22. } else if !v.shouldFail && out != v.expected {
  23. t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, out)
  24. }
  25. }
  26. }
  27. func TestCanonicalTarName(t *testing.T) {
  28. cases := []struct {
  29. in string
  30. isDir bool
  31. expected string
  32. }{
  33. {"foo", false, "foo"},
  34. {"foo", true, "foo/"},
  35. {`foo\bar`, false, "foo/bar"},
  36. {`foo\bar`, true, "foo/bar/"},
  37. }
  38. for _, v := range cases {
  39. if out, err := canonicalTarName(v.in, v.isDir); err != nil {
  40. t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err)
  41. } else if out != v.expected {
  42. t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, out)
  43. }
  44. }
  45. }
  46. func TestChmodTarEntry(t *testing.T) {
  47. cases := []struct {
  48. in, expected os.FileMode
  49. }{
  50. {0000, 0100},
  51. {0777, 0711},
  52. {0644, 0700},
  53. {0755, 0711},
  54. {0444, 0500},
  55. }
  56. for _, v := range cases {
  57. if out := chmodTarEntry(v.in); out != v.expected {
  58. t.Fatalf("wrong chmod. expected:%v got:%v", v.expected, out)
  59. }
  60. }
  61. }