github.com/system-transparency/u-root@v6.0.1-0.20190919065413-ed07a650de4c+incompatible/cmds/core/chmod/chmod_test.go (about)

     1  // Copyright 2016-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 main
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"io/ioutil"
    11  	"os"
    12  	"os/exec"
    13  	"path/filepath"
    14  	"testing"
    15  
    16  	"github.com/u-root/u-root/pkg/testutil"
    17  )
    18  
    19  func run(c *exec.Cmd) (string, string, error) {
    20  	var o, e bytes.Buffer
    21  	c.Stdout, c.Stderr = &o, &e
    22  	err := c.Run()
    23  	return o.String(), e.String(), err
    24  }
    25  
    26  func TestChmodSimple(t *testing.T) {
    27  	// Temporary directories.
    28  	tempDir, err := ioutil.TempDir("", "TestChmodSimple")
    29  	if err != nil {
    30  		t.Fatalf("cannot create temporary directory: %v", err)
    31  	}
    32  	defer os.RemoveAll(tempDir)
    33  
    34  	f, err := ioutil.TempFile(tempDir, "BLAH1")
    35  	if err != nil {
    36  		t.Fatalf("cannot create temporary file: %v", err)
    37  	}
    38  	defer f.Close()
    39  
    40  	for _, perm := range []os.FileMode{0777, 0644} {
    41  		// Set permissions using chmod.
    42  		c := testutil.Command(t, fmt.Sprintf("%0o", perm), f.Name())
    43  		c.Run()
    44  
    45  		// Check that it worked.
    46  		info, err := os.Stat(f.Name())
    47  		if err != nil {
    48  			t.Fatalf("stat(%q) failed: %v", f.Name(), err)
    49  		}
    50  		if got := info.Mode().Perm(); got != perm {
    51  			t.Errorf("Wrong file permissions on %q: got %0o, want %0o", f.Name(), got, perm)
    52  		}
    53  	}
    54  }
    55  
    56  func checkPath(t *testing.T, path string, want os.FileMode) {
    57  	info, err := os.Stat(path)
    58  	if err != nil {
    59  		t.Fatalf("stat(%q) failed: %v", path, err)
    60  	}
    61  	if got := info.Mode().Perm(); got != want {
    62  		t.Fatalf("Wrong file permissions on file %q: got %0o, want %0o",
    63  			path, got, want)
    64  	}
    65  }
    66  func TestChmodRecursive(t *testing.T) {
    67  	// Temporary directories.
    68  	tempDir, err := ioutil.TempDir("", "TestChmodRecursive")
    69  	if err != nil {
    70  		t.Fatalf("cannot create temporary directory: %v", err)
    71  	}
    72  	defer os.RemoveAll(tempDir)
    73  
    74  	var targetFiles []string
    75  	var targetDirectories []string
    76  	for _, dir := range []string{"L1_A", "L1_B", "L1_C",
    77  		filepath.Join("L1_A", "L2_A"),
    78  		filepath.Join("L1_A", "L2_B"),
    79  		filepath.Join("L1_A", "L2_C"),
    80  		filepath.Join("L1_B", "L2_A"),
    81  		filepath.Join("L1_B", "L2_B"),
    82  		filepath.Join("L1_B", "L2_C"),
    83  		filepath.Join("L1_C", "L2_A"),
    84  		filepath.Join("L1_C", "L2_B"),
    85  		filepath.Join("L1_C", "L2_C"),
    86  	} {
    87  		dir = filepath.Join(tempDir, dir)
    88  		err := os.Mkdir(dir, os.FileMode(0700))
    89  		if err != nil {
    90  			t.Fatalf("cannot create test directory: %v", err)
    91  		}
    92  		targetDirectories = append(targetDirectories, dir)
    93  		targetFile, err := os.Create(filepath.Join(dir, "X"))
    94  		if err != nil {
    95  			t.Fatalf("cannot create temporary file: %v", err)
    96  		}
    97  		targetFiles = append(targetFiles, targetFile.Name())
    98  
    99  	}
   100  
   101  	// Build chmod binary.
   102  	for _, perm := range []os.FileMode{0707, 0770} {
   103  		// Set target file permissions using chmod.
   104  		c := testutil.Command(t,
   105  			"-R",
   106  			fmt.Sprintf("%0o", perm),
   107  			tempDir)
   108  		c.Run()
   109  
   110  		// Check that it worked.
   111  		for _, dir := range targetDirectories {
   112  			checkPath(t, dir, perm)
   113  		}
   114  	}
   115  }
   116  
   117  func TestChmodReference(t *testing.T) {
   118  	// Temporary directories.
   119  	tempDir, err := ioutil.TempDir("", "TestChmodReference")
   120  	if err != nil {
   121  		t.Fatalf("cannot create temporary directory: %v", err)
   122  	}
   123  	defer os.RemoveAll(tempDir)
   124  
   125  	sourceFile, err := ioutil.TempFile(tempDir, "BLAH1")
   126  	if err != nil {
   127  		t.Fatalf("cannot create temporary file: %v", err)
   128  	}
   129  	defer sourceFile.Close()
   130  
   131  	targetFile, err := ioutil.TempFile(tempDir, "BLAH2")
   132  	if err != nil {
   133  		t.Fatalf("cannot create temporary file: %v", err)
   134  	}
   135  	defer targetFile.Close()
   136  
   137  	for _, perm := range []os.FileMode{0777, 0644} {
   138  		os.Chmod(sourceFile.Name(), perm)
   139  
   140  		// Set target file permissions using chmod.
   141  		c := testutil.Command(t,
   142  			"--reference",
   143  			sourceFile.Name(),
   144  			targetFile.Name())
   145  		c.Run()
   146  
   147  		// Check that it worked.
   148  		info, err := os.Stat(targetFile.Name())
   149  		if err != nil {
   150  			t.Fatalf("stat(%q) failed: %v", targetFile.Name(), err)
   151  		}
   152  		if got := info.Mode().Perm(); got != perm {
   153  			t.Fatalf("Wrong file permissions on file %q: got %0o, want %0o",
   154  				targetFile.Name(), got, perm)
   155  		}
   156  	}
   157  }
   158  
   159  func TestInvocationErrors(t *testing.T) {
   160  	tempDir, err := ioutil.TempDir("", "TestInvocationErrors")
   161  	if err != nil {
   162  		t.Fatalf("cannot create temporary directory: %v", err)
   163  	}
   164  	defer os.RemoveAll(tempDir)
   165  
   166  	f, err := ioutil.TempFile(tempDir, "BLAH1")
   167  	if err != nil {
   168  		t.Fatalf("cannot create temporary file: %v", err)
   169  	}
   170  	defer f.Close()
   171  
   172  	for _, v := range []struct {
   173  		args     []string
   174  		want     string
   175  		skipTo   int
   176  		skipFrom int
   177  	}{
   178  
   179  		{
   180  			args:     []string{f.Name()},
   181  			want:     "Usage",
   182  			skipTo:   0,
   183  			skipFrom: len("Usage"),
   184  		},
   185  		{
   186  			args:     []string{""},
   187  			want:     "Usage",
   188  			skipTo:   0,
   189  			skipFrom: len("Usage"),
   190  		},
   191  		{
   192  			args:     []string{"01777", f.Name()},
   193  			want:     "Invalid octal value 1777. Value should be less than or equal to 0777.\n",
   194  			skipTo:   20,
   195  			skipFrom: -1,
   196  		},
   197  		{
   198  			args:     []string{"0abas", f.Name()},
   199  			want:     "Unable to decode mode \"0abas\". Please use an octal value: strconv.ParseUint: parsing \"0abas\": invalid syntax\n",
   200  			skipTo:   20,
   201  			skipFrom: -1,
   202  		},
   203  		{
   204  			args:     []string{"0777", "blah1234"},
   205  			want:     "chmod blah1234: no such file or directory\n",
   206  			skipTo:   20,
   207  			skipFrom: -1,
   208  		},
   209  	} {
   210  		cmd := testutil.Command(t, v.args...)
   211  		_, stderr, err := run(cmd)
   212  		if v.skipFrom == -1 {
   213  			v.skipFrom = len(stderr)
   214  		}
   215  		// Ignore the date and time because we're using Log.Fatalf
   216  		if got := stderr[v.skipTo:v.skipFrom]; got != v.want {
   217  			t.Errorf("Chmod for %q failed: got %q, want %q", v.args, got, v.want)
   218  		}
   219  		if err == nil {
   220  			t.Errorf("Chmod for %q failed: got nil want err", v.args)
   221  		}
   222  	}
   223  }
   224  
   225  func TestMain(m *testing.M) {
   226  	testutil.Run(m, main)
   227  }