github.com/akerouanton/docker@v1.11.0-rc3/pkg/archive/archive_windows_test.go (about)

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