gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/fspath/fspath_test.go (about) 1 // Copyright 2019 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 fspath 16 17 import ( 18 "slices" 19 "strings" 20 "testing" 21 ) 22 23 func TestParseIteratorPartialPathnames(t *testing.T) { 24 path := Parse("/foo//bar///baz////") 25 // Parse strips leading slashes, and records their presence as 26 // Path.Absolute. 27 if !path.Absolute { 28 t.Errorf("Path.Absolute: got false, wanted true") 29 } 30 // Parse strips trailing slashes, and records their presence as Path.Dir. 31 if !path.Dir { 32 t.Errorf("Path.Dir: got false, wanted true") 33 } 34 // The first Iterator.partialPathname is the input pathname, with leading 35 // and trailing slashes stripped. 36 it := path.Begin 37 if want := "foo//bar///baz"; it.partialPathname != want { 38 t.Errorf("first Iterator.partialPathname: got %q, wanted %q", it.partialPathname, want) 39 } 40 // Successive Iterator.partialPathnames remove the leading path component 41 // and following slashes, until we run out of path components and get a 42 // terminal Iterator. 43 it = it.Next() 44 if want := "bar///baz"; it.partialPathname != want { 45 t.Errorf("second Iterator.partialPathname: got %q, wanted %q", it.partialPathname, want) 46 } 47 it = it.Next() 48 if want := "baz"; it.partialPathname != want { 49 t.Errorf("third Iterator.partialPathname: got %q, wanted %q", it.partialPathname, want) 50 } 51 it = it.Next() 52 if want := ""; it.partialPathname != want { 53 t.Errorf("fourth Iterator.partialPathname: got %q, wanted %q", it.partialPathname, want) 54 } 55 if it.Ok() { 56 t.Errorf("fourth Iterator.Ok(): got true, wanted false") 57 } 58 } 59 60 func TestParse(t *testing.T) { 61 type testCase struct { 62 pathname string 63 relpath []string 64 abs bool 65 dir bool 66 } 67 tests := []testCase{ 68 { 69 pathname: "", 70 relpath: []string{}, 71 abs: false, 72 dir: false, 73 }, 74 { 75 pathname: "/", 76 relpath: []string{}, 77 abs: true, 78 dir: true, 79 }, 80 { 81 pathname: "//", 82 relpath: []string{}, 83 abs: true, 84 dir: true, 85 }, 86 } 87 for _, sep := range []string{"/", "//"} { 88 for _, abs := range []bool{false, true} { 89 for _, dir := range []bool{false, true} { 90 for _, pcs := range [][]string{ 91 // single path component 92 {"foo"}, 93 // multiple path components, including non-UTF-8 94 {".", "foo", "..", "\xe6", "bar"}, 95 } { 96 prefix := "" 97 if abs { 98 prefix = sep 99 } 100 suffix := "" 101 if dir { 102 suffix = sep 103 } 104 tests = append(tests, testCase{ 105 pathname: prefix + strings.Join(pcs, sep) + suffix, 106 relpath: pcs, 107 abs: abs, 108 dir: dir, 109 }) 110 } 111 } 112 } 113 } 114 115 for _, test := range tests { 116 t.Run(test.pathname, func(t *testing.T) { 117 p := Parse(test.pathname) 118 t.Logf("pathname %q => path %q", test.pathname, p) 119 if p.Absolute != test.abs { 120 t.Errorf("path absoluteness: got %v, wanted %v", p.Absolute, test.abs) 121 } 122 if p.Dir != test.dir { 123 t.Errorf("path must resolve to a directory: got %v, wanted %v", p.Dir, test.dir) 124 } 125 pcs := []string{} 126 for pit := p.Begin; pit.Ok(); pit = pit.Next() { 127 pcs = append(pcs, pit.String()) 128 } 129 if !slices.Equal(pcs, test.relpath) { 130 t.Errorf("relative path: got %v, wanted %v", pcs, test.relpath) 131 } 132 }) 133 } 134 }