github.com/apptainer/singularity@v3.1.1+incompatible/internal/pkg/util/fs/helper_linux_test.go (about)

     1  // Copyright (c) 2018, Sylabs Inc. All rights reserved.
     2  // This software is licensed under a 3-clause BSD license. Please consult the
     3  // LICENSE.md file distributed with the sources of this project regarding your
     4  // rights to use or distribute this software.
     5  
     6  package fs
     7  
     8  import (
     9  	"io/ioutil"
    10  	"os"
    11  	"path/filepath"
    12  	"testing"
    13  
    14  	"github.com/sylabs/singularity/internal/pkg/test"
    15  )
    16  
    17  func TestIsFile(t *testing.T) {
    18  	test.DropPrivilege(t)
    19  	defer test.ResetPrivilege(t)
    20  
    21  	if IsFile("/etc/passwd") != true {
    22  		t.Errorf("IsFile returns false for file")
    23  	}
    24  }
    25  
    26  func TestIsDir(t *testing.T) {
    27  	test.DropPrivilege(t)
    28  	defer test.ResetPrivilege(t)
    29  
    30  	if IsDir("/etc") != true {
    31  		t.Errorf("IsDir returns false for directory")
    32  	}
    33  }
    34  
    35  func TestIsLink(t *testing.T) {
    36  	test.DropPrivilege(t)
    37  	defer test.ResetPrivilege(t)
    38  
    39  	if IsLink("/proc/mounts") != true {
    40  		t.Errorf("IsLink returns false for link")
    41  	}
    42  }
    43  
    44  func TestIsOwner(t *testing.T) {
    45  	test.DropPrivilege(t)
    46  	defer test.ResetPrivilege(t)
    47  
    48  	if IsOwner("/etc/passwd", 0) != true {
    49  		t.Errorf("IsOwner returns false for /etc/passwd owner")
    50  	}
    51  }
    52  
    53  func TestIsExec(t *testing.T) {
    54  	test.DropPrivilege(t)
    55  	defer test.ResetPrivilege(t)
    56  
    57  	if IsExec("/bin/ls") != true {
    58  		t.Errorf("IsExec returns false for /bin/ls execution bit")
    59  	}
    60  }
    61  
    62  func TestIsSuid(t *testing.T) {
    63  	test.DropPrivilege(t)
    64  	defer test.ResetPrivilege(t)
    65  
    66  	if IsSuid("/bin/su") != true {
    67  		t.Errorf("IsSuid returns false for /bin/su setuid bit")
    68  	}
    69  }
    70  
    71  func TestRootDir(t *testing.T) {
    72  	test.DropPrivilege(t)
    73  	defer test.ResetPrivilege(t)
    74  
    75  	var tests = []struct {
    76  		path     string
    77  		expected string
    78  	}{
    79  		{"/path/to/something", "/path"},
    80  		{"relative/path", "relative"},
    81  		{"/path/to/something/", "/path"},
    82  		{"relative/path/", "relative"},
    83  		{"/path", "/path"},
    84  		{"/path/", "/path"},
    85  		{"/path/../something", "/something"},
    86  		{"/", "/"},
    87  		{"./", "."},
    88  		{"/.././", "/"},
    89  		{"./path", "path"},
    90  		{"../path", ".."},
    91  		{"", "."},
    92  	}
    93  
    94  	for _, test := range tests {
    95  		if rootpath := RootDir(test.path); rootpath != test.expected {
    96  			t.Errorf("RootDir(%v) != \"%v\" (function returned %v)", test.path, test.expected, rootpath)
    97  		}
    98  	}
    99  }
   100  
   101  func TestMkdirAll(t *testing.T) {
   102  	test.DropPrivilege(t)
   103  	defer test.ResetPrivilege(t)
   104  
   105  	tmpdir, err := ioutil.TempDir("", "mkdir")
   106  	if err != nil {
   107  		t.Fatal(err)
   108  	}
   109  	defer os.RemoveAll(tmpdir)
   110  
   111  	if err := MkdirAll(filepath.Join(tmpdir, "test"), 0777); err != nil {
   112  		t.Error(err)
   113  	}
   114  	if err := MkdirAll(filepath.Join(tmpdir, "test/test"), 0000); err != nil {
   115  		t.Error(err)
   116  	}
   117  	if err := MkdirAll(filepath.Join(tmpdir, "test/test/test"), 0755); err == nil {
   118  		t.Errorf("should have failed with a permission denied")
   119  	}
   120  	fi, err := os.Stat(filepath.Join(tmpdir, "test"))
   121  	if err != nil {
   122  		t.Error(err)
   123  	}
   124  	if fi.Mode().Perm() != 0777 {
   125  		t.Errorf("bad mode applied on %s, got %v", filepath.Join(tmpdir, "test"), fi.Mode().Perm())
   126  	}
   127  }
   128  
   129  func TestMkdir(t *testing.T) {
   130  	test.DropPrivilege(t)
   131  	defer test.ResetPrivilege(t)
   132  
   133  	tmpdir, err := ioutil.TempDir("", "mkdir")
   134  	if err != nil {
   135  		t.Fatal(err)
   136  	}
   137  	defer os.RemoveAll(tmpdir)
   138  
   139  	test := filepath.Join(tmpdir, "test")
   140  	if err := Mkdir(test, 0777); err != nil {
   141  		t.Error(err)
   142  	}
   143  	fi, err := os.Stat(test)
   144  	if err != nil {
   145  		t.Error(err)
   146  	}
   147  	if fi.Mode().Perm() != 0777 {
   148  		t.Errorf("bad mode applied on %s, got %v", test, fi.Mode().Perm())
   149  	}
   150  }
   151  
   152  func TestEvalRelative(t *testing.T) {
   153  	test.DropPrivilege(t)
   154  	defer test.ResetPrivilege(t)
   155  
   156  	tmpdir, err := ioutil.TempDir("", "evalrelative")
   157  	if err != nil {
   158  		t.Fatal(err)
   159  	}
   160  	defer os.RemoveAll(tmpdir)
   161  
   162  	// test layout
   163  	// - /bin -> usr/bin
   164  	// - /sbin -> usr/sbin
   165  	// - /usr/bin
   166  	// - /usr/sbin
   167  	// - /bin/bin -> /bin
   168  	// - /bin/sbin -> ../sbin
   169  	// - /sbin/sbin2 -> ../../sbin
   170  
   171  	os.Symlink("usr/bin", filepath.Join(tmpdir, "bin"))
   172  	os.Symlink("usr/sbin", filepath.Join(tmpdir, "sbin"))
   173  
   174  	MkdirAll(filepath.Join(tmpdir, "usr", "bin"), 0755)
   175  	MkdirAll(filepath.Join(tmpdir, "usr", "sbin"), 0755)
   176  
   177  	os.Symlink("/bin", filepath.Join(tmpdir, "bin", "bin"))
   178  	os.Symlink("../sbin", filepath.Join(tmpdir, "bin", "sbin"))
   179  	os.Symlink("../../sbin", filepath.Join(tmpdir, "sbin", "sbin2"))
   180  
   181  	testPath := []struct {
   182  		path string
   183  		eval string
   184  	}{
   185  		{"/bin", "/usr/bin"},
   186  		{"/sbin", "/usr/sbin"},
   187  		{"/bin/bin", "/usr/bin"},
   188  		{"/bin/sbin", "/usr/sbin"},
   189  		{"/sbin/sbin2", "/usr/sbin"},
   190  		{"/bin/test", "/usr/bin/test"},
   191  		{"/sbin/test", "/usr/sbin/test"},
   192  		{"/usr/bin/test", "/usr/bin/test"},
   193  		{"/usr/sbin/test", "/usr/sbin/test"},
   194  		{"/bin/bin/test", "/usr/bin/test"},
   195  		{"/bin/sbin/test", "/usr/sbin/test"},
   196  		{"/sbin/sbin2/test", "/usr/sbin/test"},
   197  		{"/bin/bin/sbin/sbin2/test", "/usr/sbin/test"},
   198  		{"/fake/test", "/fake/test"},
   199  	}
   200  
   201  	for _, p := range testPath {
   202  		eval := EvalRelative(p.path, tmpdir)
   203  		if eval != p.eval {
   204  			t.Errorf("evaluated path %s expected path %s got %s", p.path, p.eval, eval)
   205  		}
   206  	}
   207  }
   208  
   209  func TestTouch(t *testing.T) {
   210  	test.DropPrivilege(t)
   211  	defer test.ResetPrivilege(t)
   212  
   213  	tmpdir, err := ioutil.TempDir("", "evalrelative")
   214  	if err != nil {
   215  		t.Fatal(err)
   216  	}
   217  	defer os.RemoveAll(tmpdir)
   218  
   219  	if err := Touch(tmpdir); err == nil {
   220  		t.Errorf("touch can't take a directory")
   221  	}
   222  
   223  	testing := filepath.Join(tmpdir, "testing")
   224  
   225  	if err := Touch(testing); err != nil {
   226  		t.Error(err)
   227  	}
   228  
   229  	if _, err := os.Stat(testing); os.IsNotExist(err) {
   230  		t.Errorf("creation of %s failed", testing)
   231  	}
   232  }