File: umask_test.go

package info (click to toggle)
golang-github-containers-common 0.64.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,932 kB
  • sloc: makefile: 132; sh: 111
file content (121 lines) | stat: -rw-r--r-- 2,491 bytes parent folder | download | duplicates (3)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package umask_test

import (
	"os"
	"path/filepath"
	"syscall"
	"testing"

	"github.com/containers/common/pkg/umask"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestMkdirAllIgnoreUmask(t *testing.T) {
	t.Parallel()
	const testMode = os.FileMode(0o744)

	for _, tc := range []struct {
		name    string
		prepare func() string
		assert  func(string, error)
	}{
		{
			name: "success",
			prepare: func() string {
				dir := t.TempDir()
				return filepath.Join(dir, "foo", "bar")
			},
			assert: func(dir string, err error) {
				assert.NoError(t, err)

				// Assert $TMPDIR/foo/bar
				assert.DirExists(t, dir)
				info, err := os.Stat(dir)
				assert.NoError(t, err)
				assert.Equal(t, testMode, info.Mode().Perm())

				// Assert $TMPDIR/foo
				dir = filepath.Dir(dir)
				assert.DirExists(t, dir)
				info, err = os.Stat(dir)
				assert.NoError(t, err)
				assert.Equal(t, testMode, info.Mode().Perm())
			},
		},
		{
			name:    "success no dir to create",
			prepare: os.TempDir,
			assert: func(_ string, err error) {
				assert.NoError(t, err)
			},
		},
	} {
		prepare := tc.prepare
		assert := tc.assert

		t.Run(tc.name, func(t *testing.T) {
			old := syscall.Umask(0o077)
			defer syscall.Umask(old)

			t.Parallel()
			dir := prepare()

			err := umask.MkdirAllIgnoreUmask(dir, testMode)
			assert(dir, err)
		})
	}
}

func TestWriteFileIgnoreUmask(t *testing.T) {
	t.Parallel()
	const testMode = os.FileMode(0o744)

	for _, tc := range []struct {
		name    string
		prepare func() string
		assert  func(string, error)
	}{
		{
			name: "success",
			prepare: func() string {
				dir := t.TempDir()
				return filepath.Join(dir, "test")
			},
			assert: func(path string, err error) {
				assert.NoError(t, err)

				// Assert $TMPDIR/test
				assert.FileExists(t, path)
				info, err := os.Stat(path)
				assert.NoError(t, err)
				assert.Equal(t, testMode, info.Mode().Perm())
			},
		},
		{
			name: "failure path does not exist",
			prepare: func() string {
				path := t.TempDir()
				require.NoError(t, os.RemoveAll(path))
				return filepath.Join(path, "foo")
			},
			assert: func(_ string, err error) {
				assert.Error(t, err)
			},
		},
	} {
		prepare := tc.prepare
		assert := tc.assert

		t.Run(tc.name, func(t *testing.T) {
			old := syscall.Umask(0o077)
			defer syscall.Umask(old)

			t.Parallel()
			path := prepare()

			err := umask.WriteFileIgnoreUmask(path, []byte("test"), testMode)
			assert(path, err)
		})
	}
}