github.com/hugelgupf/u-root@v0.0.0-20191023214958-4807c632154c/pkg/find/find_test.go (about) 1 // Copyright 2015-2017 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 find 6 7 import ( 8 "io/ioutil" 9 "os" 10 "path/filepath" 11 "reflect" 12 "strings" 13 "syscall" 14 "testing" 15 ) 16 17 // TODO: I don't now where this subtesting stuff originated, I just copied it, 18 // but it's bad practice as you can not pick individual tests. 19 // Break this out into individual tests. 20 func TestSimple(t *testing.T) { 21 type tests struct { 22 name string 23 opts func(*Finder) error 24 names []string 25 } 26 27 var testCases = []tests{ 28 { 29 name: "basic find", 30 opts: func(_ *Finder) error { return nil }, 31 names: []string{ 32 "", 33 "/root", 34 "/root/ab", 35 "/root/ab/c", 36 "/root/ab/c/d", 37 "/root/ab/c/d/e", 38 "/root/ab/c/d/e/f", 39 "/root/ab/c/d/e/f/ghij", 40 "/root/ab/c/d/e/f/ghij/k", 41 "/root/ab/c/d/e/f/ghij/k/l", 42 "/root/ab/c/d/e/f/ghij/k/l/m", 43 "/root/ab/c/d/e/f/ghij/k/l/m/n", 44 "/root/ab/c/d/e/f/ghij/k/l/m/n/o", 45 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p", 46 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q", 47 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r", 48 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r/s", 49 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r/s/t", 50 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r/s/t/u", 51 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r/s/t/u/v", 52 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r/s/t/u/v/w", 53 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r/s/t/u/v/w/xyz", 54 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r/s/t/u/v/w/xyz/0777", 55 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r/s/t/u/v/w/xyz/file", 56 }, 57 }, 58 { 59 name: "just a dir", 60 opts: func(f *Finder) error { 61 f.Mode = os.ModeDir 62 f.ModeMask = os.ModeDir 63 return nil 64 }, 65 names: []string{ 66 "", 67 "/root", 68 "/root/ab", 69 "/root/ab/c", 70 "/root/ab/c/d", 71 "/root/ab/c/d/e", 72 "/root/ab/c/d/e/f", 73 "/root/ab/c/d/e/f/ghij", 74 "/root/ab/c/d/e/f/ghij/k", 75 "/root/ab/c/d/e/f/ghij/k/l", 76 "/root/ab/c/d/e/f/ghij/k/l/m", 77 "/root/ab/c/d/e/f/ghij/k/l/m/n", 78 "/root/ab/c/d/e/f/ghij/k/l/m/n/o", 79 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p", 80 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q", 81 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r", 82 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r/s", 83 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r/s/t", 84 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r/s/t/u", 85 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r/s/t/u/v", 86 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r/s/t/u/v/w", 87 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r/s/t/u/v/w/xyz", 88 }, 89 }, 90 { 91 name: "just a file", 92 opts: func(f *Finder) error { 93 f.Mode = 0 94 f.ModeMask = os.ModeType 95 return nil 96 }, 97 names: []string{ 98 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r/s/t/u/v/w/xyz/0777", 99 "/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r/s/t/u/v/w/xyz/file", 100 }, 101 }, 102 { 103 name: "file by mode", 104 opts: func(f *Finder) error { 105 f.Mode = 0444 106 f.ModeMask = os.ModePerm 107 return nil 108 }, 109 names: []string{"/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r/s/t/u/v/w/xyz/0777"}, 110 }, 111 { 112 name: "file by name", 113 opts: func(f *Finder) error { 114 f.Pattern = "*file" 115 return nil 116 }, 117 names: []string{"/root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r/s/t/u/v/w/xyz/file"}, 118 }, 119 } 120 d, err := ioutil.TempDir(os.TempDir(), "u-root.cmds.find") 121 if err != nil { 122 t.Fatal(err) 123 } 124 defer os.RemoveAll(d) 125 126 // Make sure files are actually created with the permissions we ask for. 127 syscall.Umask(0) 128 if err := os.MkdirAll(filepath.Join(d, "root/ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r/s/t/u/v/w/xyz"), 0775); err != nil { 129 t.Fatal(err) 130 } 131 if err := ioutil.WriteFile(filepath.Join(d, "root//ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r/s/t/u/v/w/xyz/file"), nil, 0664); err != nil { 132 t.Fatal(err) 133 } 134 if err := ioutil.WriteFile(filepath.Join(d, "root//ab/c/d/e/f/ghij/k/l/m/n/o/p/q/r/s/t/u/v/w/xyz/0777"), nil, 0444); err != nil { 135 t.Fatal(err) 136 } 137 138 for _, tc := range testCases { 139 t.Run(tc.name, func(t *testing.T) { 140 f, err := New(func(f *Finder) error { 141 f.Root = d 142 return nil 143 }, tc.opts) 144 if err != nil { 145 t.Fatal(err) 146 } 147 go f.Find() 148 149 var names []string 150 for o := range f.Names { 151 if o.Err != nil { 152 t.Errorf("%v: got %v, want nil", o.Name, o.Err) 153 } 154 names = append(names, strings.TrimPrefix(o.Name, d)) 155 } 156 157 if len(names) != len(tc.names) { 158 t.Errorf("Find output: got %d bytes, want %d bytes", len(names), len(tc.names)) 159 } 160 if !reflect.DeepEqual(names, tc.names) { 161 t.Errorf("Find output: got %v, want %v", names, tc.names) 162 } 163 }) 164 } 165 }