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