gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/lisafs/node_test.go (about) 1 // Copyright 2022 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package lisafs 16 17 import ( 18 "fmt" 19 "testing" 20 ) 21 22 func TestLookup(t *testing.T) { 23 for _, numChildren := range []int{ 24 numStaticChildren, // For static children. 25 2 * numStaticChildren, // For dynamic children. 26 } { 27 t.Run(fmt.Sprintf("%dChildren", numChildren), func(t *testing.T) { 28 var root Node 29 root.InitLocked("", nil) 30 root.childrenMu.Lock() 31 defer root.childrenMu.Unlock() 32 33 // truth is the source of truth. 34 truth := make(map[string]*Node) 35 36 // Fill node with children. 37 for i := 0; i < numChildren; i++ { 38 name := fmt.Sprintf("%d", i) 39 var child Node 40 child.InitLocked(name, &root) 41 truth[name] = &child 42 } 43 44 // Test that lookup finds child correctly. 45 for i := 0; i < numChildren; i++ { 46 name := fmt.Sprintf("%d", i) 47 if got, want := root.LookupChildLocked(name), truth[name]; got != want { 48 t.Errorf("incorrect child returned by root: want %p, got %p", want, got) 49 } 50 } 51 }) 52 } 53 } 54 55 func TestDelete(t *testing.T) { 56 for _, numChildren := range []int{ 57 numStaticChildren, // For static children. 58 2 * numStaticChildren, // For dynamic children. 59 } { 60 t.Run(fmt.Sprintf("%dChildren", numChildren), func(t *testing.T) { 61 var root Node 62 root.InitLocked("", nil) 63 root.childrenMu.Lock() 64 defer root.childrenMu.Unlock() 65 66 // truth is the source of truth. 67 truth := make(map[string]*Node) 68 69 // Fill node with children. 70 for i := 0; i < numChildren; i++ { 71 name := fmt.Sprintf("%d", i) 72 var child Node 73 child.InitLocked(name, &root) 74 truth[name] = &child 75 } 76 77 // Now remove them and check if correct node is removed. 78 for i := 0; i < numChildren; i++ { 79 name := fmt.Sprintf("%d", i) 80 if got, want := root.removeChildLocked(name), truth[name]; got != want { 81 t.Errorf("root deleted incorrect node: want %p, got %p", want, got) 82 } 83 } 84 }) 85 } 86 }