github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/mc_test.go (about)

     1  // Copyright (c) 2015-2022 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package cmd
    19  
    20  import (
    21  	"path/filepath"
    22  	"runtime"
    23  	"testing"
    24  	"time"
    25  
    26  	checkv1 "gopkg.in/check.v1"
    27  )
    28  
    29  func Test(t *testing.T) { checkv1.TestingT(t) }
    30  
    31  type TestSuite struct{}
    32  
    33  var _ = checkv1.Suite(&TestSuite{})
    34  
    35  func (s *TestSuite) SetUpSuite(_ *checkv1.C) {
    36  }
    37  
    38  func (s *TestSuite) TearDownSuite(_ *checkv1.C) {
    39  }
    40  
    41  func (s *TestSuite) TestValidPERMS(c *checkv1.C) {
    42  	perms := accessPerms("none")
    43  	c.Assert(perms.isValidAccessPERM(), checkv1.Equals, true)
    44  	c.Assert(string(perms), checkv1.Equals, "none")
    45  	perms = accessPerms("public")
    46  	c.Assert(perms.isValidAccessPERM(), checkv1.Equals, true)
    47  	c.Assert(string(perms), checkv1.Equals, "public")
    48  	perms = accessPerms("private")
    49  	c.Assert(perms.isValidAccessPERM(), checkv1.Equals, true)
    50  	c.Assert(string(perms), checkv1.Equals, "private")
    51  	perms = accessPerms("download")
    52  	c.Assert(perms.isValidAccessPERM(), checkv1.Equals, true)
    53  	c.Assert(string(perms), checkv1.Equals, "download")
    54  	perms = accessPerms("upload")
    55  	c.Assert(perms.isValidAccessPERM(), checkv1.Equals, true)
    56  	c.Assert(string(perms), checkv1.Equals, "upload")
    57  }
    58  
    59  func (s *TestSuite) TestInvalidPERMS(c *checkv1.C) {
    60  	perms := accessPerms("invalid")
    61  	c.Assert(perms.isValidAccessPERM(), checkv1.Equals, false)
    62  }
    63  
    64  func (s *TestSuite) TestGetMcConfigDir(c *checkv1.C) {
    65  	dir, err := getMcConfigDir()
    66  	c.Assert(err, checkv1.IsNil)
    67  	c.Assert(dir, checkv1.Not(checkv1.Equals), "")
    68  	c.Assert(mustGetMcConfigDir(), checkv1.Equals, dir)
    69  }
    70  
    71  func (s *TestSuite) TestGetMcConfigPath(c *checkv1.C) {
    72  	dir, err := getMcConfigPath()
    73  	c.Assert(err, checkv1.IsNil)
    74  	switch runtime.GOOS {
    75  	case "linux", "freebsd", "darwin", "solaris":
    76  		c.Assert(dir, checkv1.Equals, filepath.Join(mustGetMcConfigDir(), "config.json"))
    77  	case "windows":
    78  		c.Assert(dir, checkv1.Equals, filepath.Join(mustGetMcConfigDir(), "config.json"))
    79  	default:
    80  		c.Fail()
    81  	}
    82  	c.Assert(mustGetMcConfigPath(), checkv1.Equals, dir)
    83  }
    84  
    85  func (s *TestSuite) TestIsvalidAliasName(c *checkv1.C) {
    86  	c.Check(isValidAlias("helloWorld0"), checkv1.Equals, true)
    87  	c.Check(isValidAlias("hello_World0"), checkv1.Equals, true)
    88  	c.Check(isValidAlias("h0SFD2k24Fdsa"), checkv1.Equals, true)
    89  	c.Check(isValidAlias("fdslka-4"), checkv1.Equals, true)
    90  	c.Check(isValidAlias("fdslka-"), checkv1.Equals, true)
    91  	c.Check(isValidAlias("helloWorld$"), checkv1.Equals, false)
    92  	c.Check(isValidAlias("h0SFD2k2#Fdsa"), checkv1.Equals, false)
    93  	c.Check(isValidAlias("0dslka-4"), checkv1.Equals, false)
    94  	c.Check(isValidAlias("-fdslka"), checkv1.Equals, false)
    95  }
    96  
    97  func (s *TestSuite) TestHumanizedTime(c *checkv1.C) {
    98  	hTime := timeDurationToHumanizedDuration(time.Duration(10) * time.Second)
    99  	c.Assert(hTime.Minutes, checkv1.Equals, int64(0))
   100  	c.Assert(hTime.Hours, checkv1.Equals, int64(0))
   101  	c.Assert(hTime.Days, checkv1.Equals, int64(0))
   102  
   103  	hTime = timeDurationToHumanizedDuration(time.Duration(10) * time.Minute)
   104  	c.Assert(hTime.Hours, checkv1.Equals, int64(0))
   105  	c.Assert(hTime.Days, checkv1.Equals, int64(0))
   106  
   107  	hTime = timeDurationToHumanizedDuration(time.Duration(10) * time.Hour)
   108  	c.Assert(hTime.Days, checkv1.Equals, int64(0))
   109  
   110  	hTime = timeDurationToHumanizedDuration(time.Duration(24) * time.Hour)
   111  	c.Assert(hTime.Days, checkv1.Not(checkv1.Equals), int64(0))
   112  }