github.com/rentongzhang/docker@v1.8.2-rc1/pkg/archive/archive_windows_test.go (about)

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