storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/xl-storage_unix_test.go (about)

     1  //go:build linux || darwin || dragonfly || freebsd || netbsd || openbsd
     2  // +build linux darwin dragonfly freebsd netbsd openbsd
     3  
     4  /*
     5   * MinIO Cloud Storage, (C) 2016-2020 MinIO, Inc.
     6   *
     7   * Licensed under the Apache License, Version 2.0 (the "License");
     8   * you may not use this file except in compliance with the License.
     9   * You may obtain a copy of the License at
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing, software
    14   * distributed under the License is distributed on an "AS IS" BASIS,
    15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   * See the License for the specific language governing permissions and
    17   * limitations under the License.
    18   */
    19  
    20  package cmd
    21  
    22  import (
    23  	"context"
    24  	"io/ioutil"
    25  	"os"
    26  	"path"
    27  	"syscall"
    28  	"testing"
    29  )
    30  
    31  // Based on `man getumask` a vaporware GNU extension to glibc.
    32  // returns file mode creation mask.
    33  func getUmask() int {
    34  	mask := syscall.Umask(0)
    35  	syscall.Umask(mask)
    36  	return mask
    37  }
    38  
    39  // Tests if the directory and file creations happen with proper umask.
    40  func TestIsValidUmaskVol(t *testing.T) {
    41  	tmpPath, err := ioutil.TempDir(globalTestTmpDir, "minio-")
    42  	if err != nil {
    43  		t.Fatalf("Initializing temporary directory failed with %s.", err)
    44  	}
    45  	testCases := []struct {
    46  		volName       string
    47  		expectedUmask int
    48  	}{
    49  		{"is-this-valid", getUmask()},
    50  	}
    51  	testCase := testCases[0]
    52  
    53  	// Initialize a new xlStorage layer.
    54  	disk, err := newLocalXLStorage(tmpPath)
    55  	if err != nil {
    56  		t.Fatalf("Initializing xlStorage failed with %s.", err)
    57  	}
    58  
    59  	// Attempt to create a volume to verify the permissions later.
    60  	// MakeVol creates 0777.
    61  	if err = disk.MakeVol(context.Background(), testCase.volName); err != nil {
    62  		t.Fatalf("Creating a volume failed with %s expected to pass.", err)
    63  	}
    64  	defer os.RemoveAll(tmpPath)
    65  
    66  	// Stat to get permissions bits.
    67  	st, err := os.Stat(path.Join(tmpPath, testCase.volName))
    68  	if err != nil {
    69  		t.Fatalf("Stat failed with %s expected to pass.", err)
    70  	}
    71  
    72  	// Get umask of the bits stored.
    73  	currentUmask := 0777 - uint32(st.Mode().Perm())
    74  
    75  	// Verify if umask is correct.
    76  	if int(currentUmask) != testCase.expectedUmask {
    77  		t.Fatalf("Umask check failed expected %d, got %d", testCase.expectedUmask, currentUmask)
    78  	}
    79  }
    80  
    81  // Tests if the file creations happen with proper umask.
    82  func TestIsValidUmaskFile(t *testing.T) {
    83  	tmpPath, err := ioutil.TempDir(globalTestTmpDir, "minio-")
    84  	if err != nil {
    85  		t.Fatalf("Initializing temporary directory failed with %s.", err)
    86  	}
    87  	testCases := []struct {
    88  		volName       string
    89  		expectedUmask int
    90  	}{
    91  		{"is-this-valid", getUmask()},
    92  	}
    93  	testCase := testCases[0]
    94  
    95  	// Initialize a new xlStorage layer.
    96  	disk, err := newLocalXLStorage(tmpPath)
    97  	if err != nil {
    98  		t.Fatalf("Initializing xlStorage failed with %s.", err)
    99  	}
   100  
   101  	// Attempt to create a volume to verify the permissions later.
   102  	// MakeVol creates directory with 0777 perms.
   103  	if err = disk.MakeVol(context.Background(), testCase.volName); err != nil {
   104  		t.Fatalf("Creating a volume failed with %s expected to pass.", err)
   105  	}
   106  
   107  	defer os.RemoveAll(tmpPath)
   108  
   109  	// Attempt to create a file to verify the permissions later.
   110  	// AppendFile creates file with 0666 perms.
   111  	if err = disk.AppendFile(context.Background(), testCase.volName, pathJoin("hello-world.txt", xlStorageFormatFile), []byte("Hello World")); err != nil {
   112  		t.Fatalf("Create a file `test` failed with %s expected to pass.", err)
   113  	}
   114  
   115  	// CheckFile - stat the file.
   116  	if err := disk.CheckFile(context.Background(), testCase.volName, "hello-world.txt"); err != nil {
   117  		t.Fatalf("Stat failed with %s expected to pass.", err)
   118  	}
   119  }