gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/namespace/parser_test.go (about) 1 // Copyright 2020 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package namespace 6 7 import ( 8 "io" 9 "os" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestParse(t *testing.T) { 17 type args struct { 18 r io.Reader 19 } 20 _open := func(name string) func(t *testing.T) args { 21 return func(t *testing.T) args { 22 r, _ := os.Open(name) 23 return args{r} 24 } 25 } 26 27 tests := []struct { 28 name string 29 args func(t *testing.T) args 30 31 want1 File 32 wantErr bool 33 inspectErr func(err error, t *testing.T) 34 }{ 35 { 36 name: "namespace", 37 args: _open("testdata/namespace"), 38 want1: File{ 39 cmd{ 40 syscall: MOUNT, 41 flag: REPL | AFTER | CACHE, 42 args: []string{"#s/boot", "/root", "$rootspec"}, 43 }, 44 cmd{ 45 syscall: BIND, 46 flag: REPL | AFTER, 47 args: []string{"$rootdir", "/"}, 48 }, 49 cmd{ 50 syscall: BIND, 51 flag: REPL | CREATE, 52 args: []string{"$rootdir/mnt", "/mnt"}, 53 }, 54 // kernel devices 55 cmd{ 56 syscall: BIND, 57 args: []string{"#c", "/dev"}, 58 }, 59 cmd{ 60 syscall: BIND, 61 args: []string{"#d", "/fd"}, 62 }, 63 cmd{ 64 syscall: BIND, 65 flag: REPL | CREATE, 66 args: []string{"#e", "/env"}, 67 }, 68 cmd{ 69 syscall: BIND, 70 args: []string{"#p", "/proc"}, 71 }, 72 cmd{ 73 syscall: BIND, 74 flag: REPL | CREATE, 75 args: []string{"#s", "/srv"}, 76 }, 77 cmd{ 78 syscall: BIND, 79 flag: REPL | AFTER, 80 args: []string{"#¤", "/dev"}, 81 }, 82 cmd{ 83 syscall: BIND, 84 flag: REPL | AFTER, 85 args: []string{"#S", "/dev"}, 86 }, 87 cmd{ 88 syscall: BIND, 89 flag: REPL | BEFORE, 90 args: []string{"#k", "/dev"}, 91 }, 92 cmd{ 93 syscall: BIND, 94 flag: REPL | AFTER, 95 args: []string{"#κ", "/dev"}, 96 }, 97 cmd{ 98 syscall: BIND, 99 flag: REPL | AFTER, 100 args: []string{"#u", "/dev"}, 101 }, 102 cmd{ 103 syscall: BIND, 104 flag: REPL | BEFORE, 105 args: []string{"#P", "/dev"}, 106 }, 107 // mount points 108 cmd{ 109 syscall: MOUNT, 110 flag: REPL | AFTER, 111 args: []string{"/srv/slashn", "/n"}, 112 }, 113 //authentication 114 cmd{ 115 syscall: MOUNT, 116 flag: REPL | AFTER, 117 args: []string{"/srv/factotum", "/mnt"}, 118 }, 119 // standard bin 120 cmd{ 121 syscall: BIND, 122 flag: REPL, 123 args: []string{"/$cputype/bin", "/bin"}, 124 }, 125 cmd{ 126 syscall: BIND, 127 flag: REPL | AFTER, 128 args: []string{"/rc/bin", "/bin"}, 129 }, 130 // internal networks 131 cmd{ 132 syscall: BIND, 133 flag: REPL | AFTER, 134 args: []string{"#l", "/net"}, 135 }, 136 cmd{ 137 syscall: BIND, 138 flag: REPL | AFTER, 139 args: []string{"#I", "/net"}, 140 }, 141 cmd{ 142 syscall: MOUNT, 143 flag: REPL | AFTER, 144 args: []string{"/srv/cs", "/net"}, 145 }, 146 cmd{ 147 syscall: MOUNT, 148 flag: REPL | AFTER, 149 args: []string{"/srv/dns", "/net"}, 150 }, 151 cmd{ 152 syscall: MOUNT, 153 flag: REPL | AFTER, 154 args: []string{"/srv/net", "/net"}, 155 }, 156 cmd{ 157 syscall: MOUNT, 158 flag: REPL | BEFORE, 159 args: []string{"/srv/ssh", "/net"}, 160 }, 161 // usbd, mainly disks 162 cmd{ 163 syscall: MOUNT, 164 args: []string{"/srv/usb", "/n/usb"}, 165 }, 166 cmd{ 167 syscall: MOUNT, 168 flag: REPL | AFTER, 169 args: []string{"/srv/usb", "/dev"}, 170 }, 171 cmd{ 172 syscall: BIND, 173 flag: REPL | CREATE, 174 args: []string{"/usr/$user/tmp", "/tmp"}, 175 }, 176 cmd{ 177 syscall: CHDIR, 178 args: []string{"/usr/$user"}, 179 }, 180 cmd{ 181 syscall: INCLUDE, 182 args: []string{"/lib/namespace.local"}, 183 }, 184 cmd{ 185 syscall: INCLUDE, 186 args: []string{"/lib/namespace.$sysname"}, 187 }, 188 cmd{ 189 syscall: INCLUDE, 190 args: []string{"/cfg/$sysname/namespace"}, 191 }, 192 }, 193 }, 194 } 195 196 for _, tt := range tests { 197 t.Run(tt.name, func(t *testing.T) { 198 tArgs := tt.args(t) 199 200 got1, err := Parse(tArgs.r) 201 202 assert.Equal(t, tt.want1, got1) 203 204 if tt.wantErr { 205 require.Error(t, err) 206 if tt.inspectErr != nil { 207 tt.inspectErr(err, t) 208 } 209 } 210 }) 211 } 212 } 213 214 func print(err error, t *testing.T) { t.Log(err) } 215 216 func Test_parseLine(t *testing.T) { 217 type args struct { 218 line string 219 } 220 tests := []struct { 221 name string 222 args func(t *testing.T) args 223 224 want1 cmd 225 wantErr bool 226 inspectErr func(err error, t *testing.T) 227 }{ 228 { 229 name: "mount", 230 args: func(*testing.T) args { return args{"mount -aC #s/boot /root $rootspec"} }, 231 want1: cmd{ 232 syscall: MOUNT, 233 flag: REPL | AFTER | CACHE, 234 args: []string{"#s/boot", "/root", "$rootspec"}, 235 }, 236 wantErr: false, 237 inspectErr: print, 238 }, 239 { 240 name: "mount", 241 args: func(*testing.T) args { return args{"mount -aC #s/boot /root"} }, 242 want1: cmd{ 243 syscall: MOUNT, 244 flag: REPL | AFTER | CACHE, 245 args: []string{"#s/boot", "/root"}, 246 }, 247 wantErr: false, 248 inspectErr: print, 249 }, 250 { 251 name: "bind", 252 args: func(*testing.T) args { return args{"bind -a '#r' /dev"} }, 253 want1: cmd{ 254 syscall: BIND, 255 args: []string{"'#r'", "/dev"}, 256 flag: REPL | AFTER, 257 }, 258 wantErr: false, 259 }, 260 { 261 name: "include", 262 args: func(*testing.T) args { return args{". /cfg/$sysname/namespace"} }, 263 want1: cmd{ 264 syscall: INCLUDE, 265 args: []string{"/cfg/$sysname/namespace"}, 266 flag: REPL, 267 }, 268 wantErr: false, 269 }, 270 { 271 name: "clear", 272 args: func(*testing.T) args { return args{"clear"} }, 273 want1: cmd{ 274 syscall: RFORK, 275 args: []string{}, 276 flag: REPL, 277 }, 278 wantErr: false, 279 }, 280 { 281 name: "umount", 282 args: func(*testing.T) args { return args{"unmount /dev"} }, 283 want1: cmd{ 284 syscall: UNMOUNT, 285 args: []string{"/dev"}, 286 flag: REPL, 287 }, 288 wantErr: false, 289 }, 290 { 291 name: "cd", 292 args: func(*testing.T) args { return args{"cd /cfg"} }, 293 want1: cmd{ 294 syscall: CHDIR, 295 args: []string{"/cfg"}, 296 flag: REPL, 297 }, 298 wantErr: false, 299 }, 300 { 301 name: "import", 302 args: func(*testing.T) args { return args{"import -a $host /srv /srv"} }, 303 want1: cmd{ 304 syscall: IMPORT, 305 args: []string{"$host", "/srv", "/srv"}, 306 flag: AFTER, 307 }, 308 wantErr: false, 309 }, 310 } 311 312 for _, tt := range tests { 313 t.Run(tt.name, func(t *testing.T) { 314 tArgs := tt.args(t) 315 316 got1, err := ParseLine(tArgs.line) 317 318 assert.Equal(t, tt.want1, got1) 319 320 if tt.wantErr { 321 require.Error(t, err) 322 if tt.inspectErr != nil { 323 tt.inspectErr(err, t) 324 } 325 } 326 }) 327 } 328 }