github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/strutil/pathiter_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2018 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package strutil_test 21 22 import ( 23 . "gopkg.in/check.v1" 24 25 "github.com/snapcore/snapd/strutil" 26 ) 27 28 type pathIterSuite struct{} 29 30 var _ = Suite(&pathIterSuite{}) 31 32 func (s *pathIterSuite) TestPathIteratorEmpty(c *C) { 33 iter, err := strutil.NewPathIterator("") 34 c.Assert(err, ErrorMatches, `cannot iterate over unclean path ""`) 35 c.Assert(iter, IsNil) 36 } 37 38 func (s *pathIterSuite) TestPathIteratorFilename(c *C) { 39 iter, err := strutil.NewPathIterator("foo") 40 c.Assert(err, IsNil) 41 c.Assert(iter.Path(), Equals, "foo") 42 c.Assert(iter.Depth(), Equals, 0) 43 44 c.Assert(iter.Next(), Equals, true) 45 c.Assert(iter.CurrentBase(), Equals, "") 46 c.Assert(iter.CurrentPath(), Equals, "foo") 47 c.Assert(iter.CurrentName(), Equals, "foo") 48 c.Assert(iter.CurrentCleanName(), Equals, "foo") 49 c.Assert(iter.Depth(), Equals, 1) 50 51 c.Assert(iter.Next(), Equals, false) 52 c.Assert(iter.Depth(), Equals, 1) 53 } 54 55 func (s *pathIterSuite) TestPathIteratorRelative(c *C) { 56 iter, err := strutil.NewPathIterator("foo/bar") 57 c.Assert(err, IsNil) 58 c.Assert(iter.Path(), Equals, "foo/bar") 59 c.Assert(iter.Depth(), Equals, 0) 60 61 c.Assert(iter.Next(), Equals, true) 62 c.Assert(iter.CurrentBase(), Equals, "") 63 c.Assert(iter.CurrentPath(), Equals, "foo/") 64 c.Assert(iter.CurrentName(), Equals, "foo/") 65 c.Assert(iter.CurrentCleanName(), Equals, "foo") 66 c.Assert(iter.Depth(), Equals, 1) 67 68 c.Assert(iter.Next(), Equals, true) 69 c.Assert(iter.CurrentBase(), Equals, "foo") 70 c.Assert(iter.CurrentPath(), Equals, "foo/bar") 71 c.Assert(iter.CurrentName(), Equals, "bar") 72 c.Assert(iter.CurrentCleanName(), Equals, "bar") 73 c.Assert(iter.Depth(), Equals, 2) 74 75 c.Assert(iter.Next(), Equals, false) 76 c.Assert(iter.Depth(), Equals, 2) 77 } 78 79 func (s *pathIterSuite) TestPathIteratorAbsoluteAlmostClean(c *C) { 80 iter, err := strutil.NewPathIterator("/foo/bar/") 81 c.Assert(err, IsNil) 82 c.Assert(iter.Path(), Equals, "/foo/bar/") 83 c.Assert(iter.Depth(), Equals, 0) 84 85 c.Assert(iter.Next(), Equals, true) 86 c.Assert(iter.CurrentBase(), Equals, "") 87 c.Assert(iter.CurrentPath(), Equals, "/") 88 c.Assert(iter.CurrentName(), Equals, "/") 89 c.Assert(iter.CurrentCleanName(), Equals, "") 90 c.Assert(iter.Depth(), Equals, 1) 91 92 c.Assert(iter.Next(), Equals, true) 93 c.Assert(iter.CurrentBase(), Equals, "/") 94 c.Assert(iter.CurrentPath(), Equals, "/foo/") 95 c.Assert(iter.CurrentName(), Equals, "foo/") 96 c.Assert(iter.CurrentCleanName(), Equals, "foo") 97 c.Assert(iter.Depth(), Equals, 2) 98 99 c.Assert(iter.Next(), Equals, true) 100 c.Assert(iter.CurrentBase(), Equals, "/foo") 101 c.Assert(iter.CurrentPath(), Equals, "/foo/bar/") 102 c.Assert(iter.CurrentName(), Equals, "bar/") 103 c.Assert(iter.CurrentCleanName(), Equals, "bar") 104 c.Assert(iter.Depth(), Equals, 3) 105 106 c.Assert(iter.Next(), Equals, false) 107 c.Assert(iter.Depth(), Equals, 3) 108 } 109 110 func (s *pathIterSuite) TestPathIteratorAbsoluteClean(c *C) { 111 iter, err := strutil.NewPathIterator("/foo/bar") 112 c.Assert(err, IsNil) 113 c.Assert(iter.Path(), Equals, "/foo/bar") 114 c.Assert(iter.Depth(), Equals, 0) 115 116 c.Assert(iter.Next(), Equals, true) 117 c.Assert(iter.CurrentBase(), Equals, "") 118 c.Assert(iter.CurrentPath(), Equals, "/") 119 c.Assert(iter.CurrentName(), Equals, "/") 120 c.Assert(iter.CurrentCleanName(), Equals, "") 121 c.Assert(iter.Depth(), Equals, 1) 122 123 c.Assert(iter.Next(), Equals, true) 124 c.Assert(iter.CurrentBase(), Equals, "/") 125 c.Assert(iter.CurrentPath(), Equals, "/foo/") 126 c.Assert(iter.CurrentName(), Equals, "foo/") 127 c.Assert(iter.CurrentCleanName(), Equals, "foo") 128 c.Assert(iter.Depth(), Equals, 2) 129 130 c.Assert(iter.Next(), Equals, true) 131 c.Assert(iter.CurrentBase(), Equals, "/foo") 132 c.Assert(iter.CurrentPath(), Equals, "/foo/bar") 133 c.Assert(iter.CurrentName(), Equals, "bar") 134 c.Assert(iter.CurrentCleanName(), Equals, "bar") 135 c.Assert(iter.Depth(), Equals, 3) 136 137 c.Assert(iter.Next(), Equals, false) 138 c.Assert(iter.Depth(), Equals, 3) 139 } 140 141 func (s *pathIterSuite) TestPathIteratorAbsoluteCleanDepth4(c *C) { 142 iter, err := strutil.NewPathIterator("/foo/bar/baz") 143 c.Assert(err, IsNil) 144 c.Assert(iter.Path(), Equals, "/foo/bar/baz") 145 c.Assert(iter.Depth(), Equals, 0) 146 147 c.Assert(iter.Next(), Equals, true) 148 c.Assert(iter.CurrentBase(), Equals, "") 149 c.Assert(iter.CurrentPath(), Equals, "/") 150 c.Assert(iter.CurrentName(), Equals, "/") 151 c.Assert(iter.CurrentCleanName(), Equals, "") 152 c.Assert(iter.Depth(), Equals, 1) 153 154 c.Assert(iter.Next(), Equals, true) 155 c.Assert(iter.CurrentBase(), Equals, "/") 156 c.Assert(iter.CurrentPath(), Equals, "/foo/") 157 c.Assert(iter.CurrentName(), Equals, "foo/") 158 c.Assert(iter.CurrentCleanName(), Equals, "foo") 159 c.Assert(iter.Depth(), Equals, 2) 160 161 c.Assert(iter.Next(), Equals, true) 162 c.Assert(iter.CurrentBase(), Equals, "/foo") 163 c.Assert(iter.CurrentPath(), Equals, "/foo/bar/") 164 c.Assert(iter.CurrentName(), Equals, "bar/") 165 c.Assert(iter.CurrentCleanName(), Equals, "bar") 166 c.Assert(iter.Depth(), Equals, 3) 167 168 c.Assert(iter.Next(), Equals, true) 169 c.Assert(iter.CurrentBase(), Equals, "/foo/bar") 170 c.Assert(iter.CurrentPath(), Equals, "/foo/bar/baz") 171 c.Assert(iter.CurrentName(), Equals, "baz") 172 c.Assert(iter.CurrentCleanName(), Equals, "baz") 173 c.Assert(iter.Depth(), Equals, 4) 174 175 c.Assert(iter.Next(), Equals, false) 176 c.Assert(iter.Depth(), Equals, 4) 177 } 178 179 func (s *pathIterSuite) TestPathIteratorRootDir(c *C) { 180 iter, err := strutil.NewPathIterator("/") 181 c.Assert(err, IsNil) 182 c.Assert(iter.Path(), Equals, "/") 183 c.Assert(iter.Depth(), Equals, 0) 184 185 c.Assert(iter.Next(), Equals, true) 186 c.Assert(iter.CurrentBase(), Equals, "") 187 c.Assert(iter.CurrentPath(), Equals, "/") 188 c.Assert(iter.CurrentName(), Equals, "/") 189 c.Assert(iter.CurrentCleanName(), Equals, "") 190 c.Assert(iter.Depth(), Equals, 1) 191 192 c.Assert(iter.Next(), Equals, false) 193 c.Assert(iter.Depth(), Equals, 1) 194 } 195 196 func (s *pathIterSuite) TestPathIteratorUncleanPath(c *C) { 197 iter, err := strutil.NewPathIterator("///some/../junk") 198 c.Assert(err, ErrorMatches, `cannot iterate over unclean path ".*"`) 199 c.Assert(iter, IsNil) 200 } 201 202 func (s *pathIterSuite) TestPathIteratorUnicode(c *C) { 203 iter, err := strutil.NewPathIterator("/zażółć/gęślą/jaźń") 204 c.Assert(err, IsNil) 205 c.Assert(iter.Path(), Equals, "/zażółć/gęślą/jaźń") 206 c.Assert(iter.Depth(), Equals, 0) 207 208 c.Assert(iter.Next(), Equals, true) 209 c.Assert(iter.CurrentBase(), Equals, "") 210 c.Assert(iter.CurrentPath(), Equals, "/") 211 c.Assert(iter.CurrentName(), Equals, "/") 212 c.Assert(iter.CurrentCleanName(), Equals, "") 213 c.Assert(iter.Depth(), Equals, 1) 214 215 c.Assert(iter.Next(), Equals, true) 216 c.Assert(iter.CurrentBase(), Equals, "/") 217 c.Assert(iter.CurrentPath(), Equals, "/zażółć/") 218 c.Assert(iter.CurrentName(), Equals, "zażółć/") 219 c.Assert(iter.CurrentCleanName(), Equals, "zażółć") 220 c.Assert(iter.Depth(), Equals, 2) 221 222 c.Assert(iter.Next(), Equals, true) 223 c.Assert(iter.CurrentBase(), Equals, "/zażółć") 224 c.Assert(iter.CurrentPath(), Equals, "/zażółć/gęślą/") 225 c.Assert(iter.CurrentName(), Equals, "gęślą/") 226 c.Assert(iter.CurrentCleanName(), Equals, "gęślą") 227 c.Assert(iter.Depth(), Equals, 3) 228 229 c.Assert(iter.Next(), Equals, true) 230 c.Assert(iter.CurrentBase(), Equals, "/zażółć/gęślą") 231 c.Assert(iter.CurrentPath(), Equals, "/zażółć/gęślą/jaźń") 232 c.Assert(iter.CurrentName(), Equals, "jaźń") 233 c.Assert(iter.CurrentCleanName(), Equals, "jaźń") 234 c.Assert(iter.Depth(), Equals, 4) 235 236 c.Assert(iter.Next(), Equals, false) 237 c.Assert(iter.Depth(), Equals, 4) 238 } 239 240 func (s *pathIterSuite) TestPathIteratorRewind(c *C) { 241 iter, err := strutil.NewPathIterator("/foo/bar") 242 c.Assert(err, IsNil) 243 for i := 0; i < 2; i++ { 244 c.Assert(iter.Next(), Equals, true) 245 c.Assert(iter.Depth(), Equals, 1) 246 c.Assert(iter.CurrentPath(), Equals, "/") 247 c.Assert(iter.Next(), Equals, true) 248 c.Assert(iter.Depth(), Equals, 2) 249 c.Assert(iter.CurrentPath(), Equals, "/foo/") 250 iter.Rewind() 251 } 252 } 253 254 func (s *pathIterSuite) TestPathIteratorExample(c *C) { 255 iter, err := strutil.NewPathIterator("/some/path/there") 256 c.Assert(err, IsNil) 257 for iter.Next() { 258 _ = iter.CurrentBase() 259 _ = iter.CurrentPath() 260 _ = iter.CurrentName() 261 _ = iter.CurrentCleanName() 262 _ = iter.Depth() 263 } 264 }