github.com/avfs/avfs@v0.33.1-0.20240303173310-c6ba67c33eb7/vfs/memfs/memfs_internal_test.go (about) 1 // 2 // Copyright 2020 The AVFS authors 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 //go:build !avfs_race 18 19 package memfs 20 21 import ( 22 "io/fs" 23 "testing" 24 25 "github.com/avfs/avfs" 26 ) 27 28 var ( 29 // Tests that dirNode struct implements node interface. 30 _ node = &dirNode{} 31 32 // Tests that fileNode struct implements node interface. 33 _ node = &fileNode{} 34 35 // Tests that symlinkNode struct implements node interface. 36 _ node = &symlinkNode{} 37 38 // Tests that MemInfo struct implements fs.FileInfo interface. 39 _ fs.FileInfo = &MemInfo{} 40 ) 41 42 func TestSearchNode(t *testing.T) { 43 vfs := New() 44 rn := vfs.rootNode 45 46 // Directories 47 da := vfs.createDir(rn, "a", avfs.DefaultDirPerm) 48 db := vfs.createDir(rn, "b", avfs.DefaultDirPerm) 49 dc := vfs.createDir(rn, "c", avfs.DefaultDirPerm) 50 da1 := vfs.createDir(da, "a1", avfs.DefaultDirPerm) 51 da2 := vfs.createDir(da, "a2", avfs.DefaultDirPerm) 52 db1 := vfs.createDir(db, "b1", avfs.DefaultDirPerm) 53 db1a := vfs.createDir(db1, "b1A", avfs.DefaultDirPerm) 54 db1b := vfs.createDir(db1, "b1B", avfs.DefaultDirPerm) 55 56 // Files 57 f1 := vfs.createFile(rn, "file1", avfs.DefaultFilePerm) 58 fa1 := vfs.createFile(da, "afile1", avfs.DefaultFilePerm) 59 fa2 := vfs.createFile(da, "afile2", avfs.DefaultFilePerm) 60 fa3 := vfs.createFile(da, "afile3", avfs.DefaultFilePerm) 61 62 // Symlinks 63 vfs.createSymlink(rn, "lroot", avfs.FromUnixPath(vfs, "/")) 64 vfs.createSymlink(rn, "la", avfs.FromUnixPath(vfs, "/a")) 65 vfs.createSymlink(db1b, "lb1", avfs.FromUnixPath(vfs, "/b/b1")) 66 vfs.createSymlink(dc, "lafile3", avfs.FromUnixPath(vfs, "../a/afile3")) 67 lloop1 := vfs.createSymlink(rn, "loop1", avfs.FromUnixPath(vfs, "/loop2")) 68 vfs.createSymlink(rn, "loop2", avfs.FromUnixPath(vfs, "/loop1")) 69 70 cases := []struct { 71 path string 72 parent *dirNode 73 child node 74 first, rest string 75 err error 76 }{ 77 // Existing directories 78 {path: "/", parent: rn, child: rn, first: "", rest: "", err: vfs.err.FileExists}, 79 {path: "/a", parent: rn, child: da, first: "a", rest: "", err: vfs.err.FileExists}, 80 {path: "/b", parent: rn, child: db, first: "b", rest: "", err: vfs.err.FileExists}, 81 {path: "/c", parent: rn, child: dc, first: "c", rest: "", err: vfs.err.FileExists}, 82 {path: "/a/a1", parent: da, child: da1, first: "a1", rest: "", err: vfs.err.FileExists}, 83 {path: "/a/a2", parent: da, child: da2, first: "a2", rest: "", err: vfs.err.FileExists}, 84 {path: "/b/b1", parent: db, child: db1, first: "b1", rest: "", err: vfs.err.FileExists}, 85 {path: "/b/b1/b1A", parent: db1, child: db1a, first: "b1A", rest: "", err: vfs.err.FileExists}, 86 {path: "/b/b1/b1B", parent: db1, child: db1b, first: "b1B", rest: "", err: vfs.err.FileExists}, 87 88 // Existing files 89 {path: "/file1", parent: rn, child: f1, first: "file1", rest: "", err: vfs.err.FileExists}, 90 {path: "/a/afile1", parent: da, child: fa1, first: "afile1", rest: "", err: vfs.err.FileExists}, 91 {path: "/a/afile2", parent: da, child: fa2, first: "afile2", rest: "", err: vfs.err.FileExists}, 92 {path: "/a/afile3", parent: da, child: fa3, first: "afile3", rest: "", err: vfs.err.FileExists}, 93 94 // Non existing 95 {path: "/z", parent: rn, first: "z", rest: "", err: vfs.err.NoSuchFile}, 96 {path: "/a/az", parent: da, first: "az", rest: "", err: vfs.err.NoSuchFile}, 97 {path: "/b/b1/b1z", parent: db1, first: "b1z", err: vfs.err.NoSuchFile}, 98 {path: "/b/b1/b1A/b1Az", parent: db1a, first: "b1Az", err: vfs.err.NoSuchFile}, 99 {path: "/b/b1/b1A/b1Az/not/exist", parent: db1a, first: "b1Az", rest: "/not/exist", err: vfs.err.NoSuchDir}, 100 {path: "/a/afile1/not/a/dir", parent: da, child: fa1, first: "afile1", rest: "/not/a/dir", err: vfs.err.NotADirectory}, 101 102 // Symlinks 103 {path: "/lroot", parent: rn, child: rn, first: "", rest: "", err: vfs.err.FileExists}, 104 {path: "/lroot/a", parent: rn, child: da, first: "a", rest: "", err: vfs.err.FileExists}, 105 {path: "/la/a1", parent: da, child: da1, first: "a1", rest: "", err: vfs.err.FileExists}, 106 {path: "/b/b1/b1B/lb1/b1A", parent: db1, child: db1a, first: "b1A", rest: "", err: vfs.err.FileExists}, 107 {path: "/c/lafile3", parent: da, child: fa3, first: "afile3", rest: "", err: vfs.err.FileExists}, 108 {path: "/loop1", parent: rn, child: lloop1, first: "loop1", rest: "", err: vfs.err.TooManySymlinks}, 109 } 110 111 for _, c := range cases { 112 path := avfs.FromUnixPath(vfs, c.path) 113 wantRest := vfs.FromSlash(c.rest) 114 115 parent, child, pi, err := vfs.searchNode(path, slmEval) 116 first := pi.Part() 117 rest := pi.Right() 118 119 if c.err != err { 120 t.Errorf("%s : want error to be %v, got %v", path, c.err, err) 121 } 122 123 if c.parent != parent { 124 t.Errorf("%s : want parent to be %v, got %v", path, c.parent, parent) 125 } 126 127 if c.child != child { 128 t.Errorf("%s : want child to be %v, got %v", path, c.child, child) 129 } 130 131 if c.first != first { 132 t.Errorf("%s : want first to be %s, got %s", path, c.first, first) 133 } 134 135 if wantRest != rest { 136 t.Errorf("%s : want rest to be %s, got %s", path, wantRest, rest) 137 } 138 } 139 }