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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package p9
import (
"os"
"testing"
)
func TestFileModeHelpers(t *testing.T) {
fns := map[FileMode]struct {
// name identifies the file mode.
name string
// function is the function that should return true given the
// right FileMode.
function func(m FileMode) bool
}{
ModeRegular: {
name: "regular",
function: FileMode.IsRegular,
},
ModeDirectory: {
name: "directory",
function: FileMode.IsDir,
},
ModeNamedPipe: {
name: "named pipe",
function: FileMode.IsNamedPipe,
},
ModeCharacterDevice: {
name: "character device",
function: FileMode.IsCharacterDevice,
},
ModeBlockDevice: {
name: "block device",
function: FileMode.IsBlockDevice,
},
ModeSymlink: {
name: "symlink",
function: FileMode.IsSymlink,
},
ModeSocket: {
name: "socket",
function: FileMode.IsSocket,
},
}
for mode, info := range fns {
// Make sure the mode doesn't identify as anything but itself.
for testMode, testfns := range fns {
if mode != testMode && testfns.function(mode) {
t.Errorf("Mode %s returned true when asked if it was mode %s", info.name, testfns.name)
}
}
// Make sure mode identifies as itself.
if !info.function(mode) {
t.Errorf("Mode %s returned false when asked if it was itself", info.name)
}
}
}
func TestFileModeToQID(t *testing.T) {
for _, test := range []struct {
// name identifies the test.
name string
// mode is the FileMode we start out with.
mode FileMode
// want is the corresponding QIDType we expect.
want QIDType
}{
{
name: "Directories are of type directory",
mode: ModeDirectory,
want: TypeDir,
},
{
name: "Sockets are append-only files",
mode: ModeSocket,
want: TypeAppendOnly,
},
{
name: "Named pipes are append-only files",
mode: ModeNamedPipe,
want: TypeAppendOnly,
},
{
name: "Character devices are append-only files",
mode: ModeCharacterDevice,
want: TypeAppendOnly,
},
{
name: "Symlinks are of type symlink",
mode: ModeSymlink,
want: TypeSymlink,
},
{
name: "Regular files are of type regular",
mode: ModeRegular,
want: TypeRegular,
},
{
name: "Block devices are regular files",
mode: ModeBlockDevice,
want: TypeRegular,
},
} {
if qidType := test.mode.QIDType(); qidType != test.want {
t.Errorf("ModeToQID test %s failed: got %o, wanted %o", test.name, qidType, test.want)
}
}
}
func TestP9ModeConverters(t *testing.T) {
for _, m := range []FileMode{
ModeRegular,
ModeDirectory,
ModeCharacterDevice,
ModeBlockDevice,
ModeSocket,
ModeSymlink,
ModeNamedPipe,
} {
if mb := ModeFromOS(m.OSMode()); mb != m {
t.Errorf("Converting %o to OS.FileMode gives %o and is converted back as %o", m, m.OSMode(), mb)
}
}
}
func TestOSModeConverters(t *testing.T) {
// Modes that can be converted back and forth.
for _, m := range []os.FileMode{
0, // Regular file.
os.ModeDir,
os.ModeCharDevice | os.ModeDevice,
os.ModeDevice,
os.ModeSocket,
os.ModeSymlink,
os.ModeNamedPipe,
} {
if mb := ModeFromOS(m).OSMode(); mb != m {
t.Errorf("Converting %o to p9.FileMode gives %o and is converted back as %o", m, ModeFromOS(m), mb)
}
}
// Modes that will be converted to a regular file since p9 cannot
// express these.
for _, m := range []os.FileMode{
os.ModeAppend,
os.ModeExclusive,
os.ModeTemporary,
} {
if p9Mode := ModeFromOS(m); p9Mode != ModeRegular {
t.Errorf("Converting %o to p9.FileMode should have given ModeRegular, but yielded %o", m, p9Mode)
}
}
}
func TestAttrMaskContains(t *testing.T) {
req := AttrMask{Mode: true, Size: true}
have := AttrMask{}
if have.Contains(req) {
t.Fatalf("AttrMask %v should not be a superset of %v", have, req)
}
have.Mode = true
if have.Contains(req) {
t.Fatalf("AttrMask %v should not be a superset of %v", have, req)
}
have.Size = true
have.MTime = true
if !have.Contains(req) {
t.Fatalf("AttrMask %v should be a superset of %v", have, req)
}
}
|