-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathfile_unix_test.go
74 lines (56 loc) · 1.91 KB
/
file_unix_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
//go:build !windows
// +build !windows
package utils_test
import (
"fmt"
"os"
"path/filepath"
"time"
gc "gopkg.in/check.v1"
"github.com/juju/errors"
"github.com/juju/utils/v4"
)
type unixFileSuite struct {
}
var _ = gc.Suite(&unixFileSuite{})
func (s *unixFileSuite) TestEnsureBaseDir(c *gc.C) {
c.Assert(utils.EnsureBaseDir(`/a`, `/b/c`), gc.Equals, `/a/b/c`)
c.Assert(utils.EnsureBaseDir(`/`, `/b/c`), gc.Equals, `/b/c`)
c.Assert(utils.EnsureBaseDir(``, `/b/c`), gc.Equals, `/b/c`)
}
func (s *unixFileSuite) TestFileOwner(c *gc.C) {
username, err := utils.LocalUsername()
c.Assert(err, gc.IsNil)
path := filepath.Join(os.TempDir(), fmt.Sprintf("file-%d", time.Now().UnixNano()))
_, err = os.Create(path)
c.Assert(err, gc.IsNil)
ok, err := utils.IsFileOwner(path, username)
c.Assert(err, gc.IsNil)
c.Assert(ok, gc.Equals, true)
}
func (s *unixFileSuite) TestFileOwnerUsingRoot(c *gc.C) {
path := filepath.Join(os.TempDir(), fmt.Sprintf("file-%d", time.Now().UnixNano()))
_, err := os.Create(path)
c.Assert(err, gc.IsNil)
ok, err := utils.IsFileOwner(path, "root")
c.Assert(err, gc.IsNil)
c.Assert(ok, gc.Equals, false)
}
func (s *unixFileSuite) TestFileOwnerWithInvalidPath(c *gc.C) {
username, err := utils.LocalUsername()
c.Assert(err, gc.IsNil)
path := filepath.Join(os.TempDir(), "file-bad")
ok, err := utils.IsFileOwner(path, username)
c.Assert(errors.Cause(err), gc.ErrorMatches, "stat .*: no such file or directory")
c.Assert(ok, gc.Equals, false)
}
func (s *unixFileSuite) TestFileOwnerWithInvalidUsername(c *gc.C) {
path := filepath.Join(os.TempDir(), fmt.Sprintf("file-%d", time.Now().UnixNano()))
_, err := os.Create(path)
c.Assert(err, gc.IsNil)
ok, err := utils.IsFileOwner(path, "invalid")
c.Assert(errors.Cause(err), gc.ErrorMatches, "user: unknown user invalid")
c.Assert(ok, gc.Equals, false)
}