github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/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  // Copyright (c) 2015-2021 MinIO, Inc.
     5  //
     6  // This file is part of MinIO Object Storage stack
     7  //
     8  // This program is free software: you can redistribute it and/or modify
     9  // it under the terms of the GNU Affero General Public License as published by
    10  // the Free Software Foundation, either version 3 of the License, or
    11  // (at your option) any later version.
    12  //
    13  // This program is distributed in the hope that it will be useful
    14  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  // GNU Affero General Public License for more details.
    17  //
    18  // You should have received a copy of the GNU Affero General Public License
    19  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    20  
    21  package cmd
    22  
    23  import (
    24  	"context"
    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 := t.TempDir()
    42  	testCases := []struct {
    43  		volName       string
    44  		expectedUmask int
    45  	}{
    46  		{"is-this-valid", getUmask()},
    47  	}
    48  	testCase := testCases[0]
    49  
    50  	// Initialize a new xlStorage layer.
    51  	disk, err := newLocalXLStorage(tmpPath)
    52  	if err != nil {
    53  		t.Fatalf("Initializing xlStorage failed with %s.", err)
    54  	}
    55  
    56  	// Attempt to create a volume to verify the permissions later.
    57  	// MakeVol creates 0777.
    58  	if err = disk.MakeVol(context.Background(), testCase.volName); err != nil {
    59  		t.Fatalf("Creating a volume failed with %s expected to pass.", err)
    60  	}
    61  
    62  	// Stat to get permissions bits.
    63  	st, err := os.Stat(path.Join(tmpPath, testCase.volName))
    64  	if err != nil {
    65  		t.Fatalf("Stat failed with %s expected to pass.", err)
    66  	}
    67  
    68  	// Get umask of the bits stored.
    69  	currentUmask := 0o777 - uint32(st.Mode().Perm())
    70  
    71  	// Verify if umask is correct.
    72  	if int(currentUmask) != testCase.expectedUmask {
    73  		t.Fatalf("Umask check failed expected %d, got %d", testCase.expectedUmask, currentUmask)
    74  	}
    75  }
    76  
    77  // Tests if the file creations happen with proper umask.
    78  func TestIsValidUmaskFile(t *testing.T) {
    79  	tmpPath := t.TempDir()
    80  	testCases := []struct {
    81  		volName       string
    82  		expectedUmask int
    83  	}{
    84  		{"is-this-valid", getUmask()},
    85  	}
    86  	testCase := testCases[0]
    87  
    88  	// Initialize a new xlStorage layer.
    89  	disk, err := newLocalXLStorage(tmpPath)
    90  	if err != nil {
    91  		t.Fatalf("Initializing xlStorage failed with %s.", err)
    92  	}
    93  
    94  	// Attempt to create a volume to verify the permissions later.
    95  	// MakeVol creates directory with 0777 perms.
    96  	if err = disk.MakeVol(context.Background(), testCase.volName); err != nil {
    97  		t.Fatalf("Creating a volume failed with %s expected to pass.", err)
    98  	}
    99  
   100  	// Attempt to create a file to verify the permissions later.
   101  	// AppendFile creates file with 0666 perms.
   102  	if err = disk.AppendFile(context.Background(), testCase.volName, pathJoin("hello-world.txt", xlStorageFormatFile), []byte("Hello World")); err != nil {
   103  		t.Fatalf("Create a file `test` failed with %s expected to pass.", err)
   104  	}
   105  
   106  	// CheckFile - stat the file.
   107  	if _, err := disk.StatInfoFile(context.Background(), testCase.volName, "hello-world.txt/"+xlStorageFormatFile, false); err != nil {
   108  		t.Fatalf("Stat failed with %s expected to pass.", err)
   109  	}
   110  }