gopkg.in/essentialkaos/ek.v3@v3.5.1/path/path_test.go (about)

     1  package path
     2  
     3  // ////////////////////////////////////////////////////////////////////////////////// //
     4  //                                                                                    //
     5  //                     Copyright (c) 2009-2016 Essential Kaos                         //
     6  //      Essential Kaos Open Source License <http://essentialkaos.com/ekol?en>         //
     7  //                                                                                    //
     8  // ////////////////////////////////////////////////////////////////////////////////// //
     9  
    10  import (
    11  	"pkg.re/essentialkaos/ek.v3/env"
    12  	"testing"
    13  
    14  	. "pkg.re/check.v1"
    15  )
    16  
    17  // ////////////////////////////////////////////////////////////////////////////////// //
    18  
    19  func Test(t *testing.T) { TestingT(t) }
    20  
    21  type PathUtilSuite struct{}
    22  
    23  // ////////////////////////////////////////////////////////////////////////////////// //
    24  
    25  var _ = Suite(&PathUtilSuite{})
    26  
    27  // ////////////////////////////////////////////////////////////////////////////////// //
    28  
    29  func (s *PathUtilSuite) TestBase(c *C) {
    30  	c.Assert(Base("/some/test/path"), Equals, "path")
    31  	c.Assert(Clean("//some/test//path"), Equals, "/some/test/path")
    32  	c.Assert(Dir("/some/test/path"), Equals, "/some/test")
    33  	c.Assert(Ext("/some/test/path/file.jpg"), Equals, ".jpg")
    34  	c.Assert(IsAbs("/some/test/path"), Equals, true)
    35  	c.Assert(Join("/", "some", "test", "path"), Equals, "/some/test/path")
    36  
    37  	match, err := Match("/some/test/*", "/some/test/path")
    38  
    39  	c.Assert(match, Equals, true)
    40  	c.Assert(err, IsNil)
    41  
    42  	d, f := Split("/some/test/path/file.jpg")
    43  
    44  	c.Assert(d, Equals, "/some/test/path/")
    45  	c.Assert(f, Equals, "file.jpg")
    46  }
    47  
    48  func (s *PathUtilSuite) TestEvalHome(c *C) {
    49  	homeDir := env.Get()["HOME"]
    50  
    51  	c.Assert(Clean("~/path"), Equals, homeDir+"/path")
    52  	c.Assert(Clean("/path"), Equals, "/path")
    53  }
    54  
    55  func (s *PathUtilSuite) TestSafe(c *C) {
    56  	c.Assert(IsSafe("/home/user/test.jpg"), Equals, true)
    57  	c.Assert(IsSafe("/home/user"), Equals, true)
    58  	c.Assert(IsSafe("/opt/software-1234"), Equals, true)
    59  	c.Assert(IsSafe("/srv/my-supper-service"), Equals, true)
    60  
    61  	c.Assert(IsSafe(""), Equals, false)
    62  	c.Assert(IsSafe("/"), Equals, false)
    63  	c.Assert(IsSafe("/dev/tty3"), Equals, false)
    64  	c.Assert(IsSafe("/etc/file.conf"), Equals, false)
    65  	c.Assert(IsSafe("/lib/some-lib"), Equals, false)
    66  	c.Assert(IsSafe("/lib64/some-lib"), Equals, false)
    67  	c.Assert(IsSafe("/lost+found"), Equals, false)
    68  	c.Assert(IsSafe("/proc/19313"), Equals, false)
    69  	c.Assert(IsSafe("/root"), Equals, false)
    70  	c.Assert(IsSafe("/sbin/useradd"), Equals, false)
    71  	c.Assert(IsSafe("/bin/useradd"), Equals, false)
    72  	c.Assert(IsSafe("/selinux"), Equals, false)
    73  	c.Assert(IsSafe("/sys/kernel"), Equals, false)
    74  	c.Assert(IsSafe("/usr/bin/du"), Equals, false)
    75  	c.Assert(IsSafe("/usr/sbin/chroot"), Equals, false)
    76  	c.Assert(IsSafe("/usr/lib/some-lib"), Equals, false)
    77  	c.Assert(IsSafe("/usr/lib64/some-lib"), Equals, false)
    78  	c.Assert(IsSafe("/usr/libexec/gcc"), Equals, false)
    79  	c.Assert(IsSafe("/usr/include/xlocale.h"), Equals, false)
    80  	c.Assert(IsSafe("/var/cache/yum"), Equals, false)
    81  	c.Assert(IsSafe("/var/db/yum"), Equals, false)
    82  	c.Assert(IsSafe("/var/lib/pgsql"), Equals, false)
    83  }
    84  
    85  func (s *PathUtilSuite) TestDotfile(c *C) {
    86  	c.Assert(IsDotfile(""), Equals, false)
    87  	c.Assert(IsDotfile("/some/dir/abcd"), Equals, false)
    88  	c.Assert(IsDotfile("/some/dir/"), Equals, false)
    89  	c.Assert(IsDotfile("/"), Equals, false)
    90  	c.Assert(IsDotfile("/////"), Equals, false)
    91  	c.Assert(IsDotfile("   /    "), Equals, false)
    92  
    93  	c.Assert(IsDotfile(".dotfile"), Equals, true)
    94  	c.Assert(IsDotfile("/.dotfile"), Equals, true)
    95  	c.Assert(IsDotfile("/some/dir/.abcd"), Equals, true)
    96  }