github.com/miqui/docker@v1.9.1/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  	folder, err := ioutil.TempDir("", "docker-archive-test")
    14  	if err != nil {
    15  		t.Fatal(err)
    16  	}
    17  	defer os.RemoveAll(folder)
    18  	dest := "c:dest"
    19  	srcFolder := filepath.Join(folder, "src")
    20  	src := filepath.Join(folder, "src", "src")
    21  	err = os.MkdirAll(srcFolder, 0740)
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	ioutil.WriteFile(src, []byte("content"), 0777)
    26  	err = CopyWithTar(src, dest)
    27  	if err == nil {
    28  		t.Fatalf("archiver.CopyWithTar should throw an error on invalid dest.")
    29  	}
    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  
    52  func TestCanonicalTarName(t *testing.T) {
    53  	cases := []struct {
    54  		in       string
    55  		isDir    bool
    56  		expected string
    57  	}{
    58  		{"foo", false, "foo"},
    59  		{"foo", true, "foo/"},
    60  		{`foo\bar`, false, "foo/bar"},
    61  		{`foo\bar`, true, "foo/bar/"},
    62  	}
    63  	for _, v := range cases {
    64  		if out, err := canonicalTarName(v.in, v.isDir); err != nil {
    65  			t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err)
    66  		} else if out != v.expected {
    67  			t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, out)
    68  		}
    69  	}
    70  }
    71  
    72  func TestChmodTarEntry(t *testing.T) {
    73  	cases := []struct {
    74  		in, expected os.FileMode
    75  	}{
    76  		{0000, 0111},
    77  		{0777, 0755},
    78  		{0644, 0755},
    79  		{0755, 0755},
    80  		{0444, 0555},
    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  }