1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
package mtree
import (
"fmt"
"os"
"testing"
"time"
)
func TestKeyValRoundtrip(t *testing.T) {
kv := KeyVal("xattr.security.selinux=dW5jb25maW5lZF91Om9iamVjdF9yOnVzZXJfaG9tZV90OnMwAA==")
expected := "xattr.security.selinux"
got := string(kv.Keyword())
if got != expected {
t.Errorf("expected %q; got %q", expected, got)
}
expected = "xattr"
got = string(kv.Keyword().Prefix())
if got != expected {
t.Errorf("expected %q; got %q", expected, got)
}
expected = "security.selinux"
got = kv.Keyword().Suffix()
if got != expected {
t.Errorf("expected %q; got %q", expected, got)
}
expected = "dW5jb25maW5lZF91Om9iamVjdF9yOnVzZXJfaG9tZV90OnMwAA=="
got = kv.Value()
if got != expected {
t.Errorf("expected %q; got %q", expected, got)
}
expected = "xattr.security.selinux=farts"
got = string(kv.NewValue("farts"))
if got != expected {
t.Errorf("expected %q; got %q", expected, got)
}
kv1 := KeyVal(got)
kv2 := kv.NewValue("farts")
if !kv2.Equal(kv1) {
t.Errorf("expected equality of %q and %q", kv1, kv2)
}
}
type fakeFileInfo struct {
mtime time.Time
}
func (ffi fakeFileInfo) Name() string {
// noop
return ""
}
func (ffi fakeFileInfo) Size() int64 {
// noop
return -1
}
func (ffi fakeFileInfo) Mode() os.FileMode {
// noop
return 0
}
func (ffi fakeFileInfo) ModTime() time.Time {
return ffi.mtime
}
func (ffi fakeFileInfo) IsDir() bool {
return ffi.Mode().IsDir()
}
func (ffi fakeFileInfo) Sys() interface{} {
// noop
return nil
}
func TestKeywordsTimeNano(t *testing.T) {
// We have to make sure that timeKeywordFunc always returns the correct
// formatting with regards to the nanotime.
for _, test := range []struct {
sec, nsec int64
}{
{1234, 123456789},
{5555, 987654321},
{1337, 100000000},
{8888, 999999999},
{144123582122, 1},
{857125628319, 0},
} {
mtime := time.Unix(test.sec, test.nsec)
expected := KeyVal(fmt.Sprintf("time=%d.%9.9d", test.sec, test.nsec))
got, err := timeKeywordFunc("", fakeFileInfo{
mtime: mtime,
}, nil)
if err != nil {
t.Errorf("unexpected error while parsing '%q': %q", mtime, err)
}
if len(got) != 1 {
t.Errorf("expected 1 KeyVal, but got %d", len(got))
}
if expected != got[0] {
t.Errorf("keyword didn't match, expected '%s' got '%s'", expected, got[0])
}
}
}
func TestKeywordsTimeTar(t *testing.T) {
// tartimeKeywordFunc always has nsec = 0.
for _, test := range []struct {
sec, nsec int64
}{
{1234, 123456789},
{5555, 987654321},
{1337, 100000000},
{8888, 999999999},
{144123582122, 1},
{857125628319, 0},
} {
mtime := time.Unix(test.sec, test.nsec)
expected := KeyVal(fmt.Sprintf("tar_time=%d.%9.9d", test.sec, 0))
got, err := tartimeKeywordFunc("", fakeFileInfo{
mtime: mtime,
}, nil)
if err != nil {
t.Errorf("unexpected error while parsing '%q': %q", mtime, err)
}
if len(got) != 1 {
t.Errorf("expected 1 KeyVal, but got %d", len(got))
}
if expected != got[0] {
t.Errorf("keyword didn't match, expected '%s' got '%s'", expected, got[0])
}
}
}
func TestKeywordSynonym(t *testing.T) {
checklist := []struct {
give string
expect Keyword
}{
{give: "time", expect: "time"},
{give: "md5", expect: "md5digest"},
{give: "md5digest", expect: "md5digest"},
{give: "rmd160", expect: "ripemd160digest"},
{give: "rmd160digest", expect: "ripemd160digest"},
{give: "ripemd160digest", expect: "ripemd160digest"},
{give: "sha1", expect: "sha1digest"},
{give: "sha1digest", expect: "sha1digest"},
{give: "sha256", expect: "sha256digest"},
{give: "sha256digest", expect: "sha256digest"},
{give: "sha384", expect: "sha384digest"},
{give: "sha384digest", expect: "sha384digest"},
{give: "sha512", expect: "sha512digest"},
{give: "sha512digest", expect: "sha512digest"},
{give: "xattr", expect: "xattr"},
{give: "xattrs", expect: "xattr"},
}
for i, check := range checklist {
got := KeywordSynonym(check.give)
if got != check.expect {
t.Errorf("%d: expected %q; got %q", i, check.expect, got)
}
}
}
|