2022-09-24 13:04:14 +00:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-09-24 13:04:14 +00:00
|
|
|
|
|
|
|
//go:build !windows
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 19:42:06 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-09-24 13:04:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestApplyUmask(t *testing.T) {
|
|
|
|
f, err := os.CreateTemp(t.TempDir(), "test-filemode-")
|
2024-07-30 19:42:06 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-24 13:04:14 +00:00
|
|
|
|
|
|
|
err = os.Chmod(f.Name(), 0o777)
|
2024-07-30 19:42:06 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-24 13:04:14 +00:00
|
|
|
st, err := os.Stat(f.Name())
|
2024-07-30 19:42:06 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-24 13:04:14 +00:00
|
|
|
assert.EqualValues(t, 0o777, st.Mode().Perm()&0o777)
|
|
|
|
|
|
|
|
oldDefaultUmask := defaultUmask
|
|
|
|
defaultUmask = 0o037
|
|
|
|
defer func() {
|
|
|
|
defaultUmask = oldDefaultUmask
|
|
|
|
}()
|
|
|
|
err = ApplyUmask(f.Name(), os.ModePerm)
|
2024-07-30 19:42:06 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-24 13:04:14 +00:00
|
|
|
st, err = os.Stat(f.Name())
|
2024-07-30 19:42:06 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-24 13:04:14 +00:00
|
|
|
assert.EqualValues(t, 0o740, st.Mode().Perm()&0o777)
|
|
|
|
}
|