Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions fn/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fn

import (
"os"
"syscall"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -35,6 +36,12 @@ func TestWriteFile(t *testing.T) {
err = os.Chmod(filename, 0444)
require.NoError(t, err, "couldn't change file to read-only")

// Root can bypass read-only file mode, so change the file owner.
if syscall.Geteuid() == 0 {
err = os.Chown(filename, 1000, 1000)
require.NoError(t, err, "couldn't chown the file")
}

// Write must fail and keep the file.
err = WriteFile(filename, []byte(data2), 0644)
require.Error(t, err, "shouldn't write a read-only file")
Expand Down Expand Up @@ -65,6 +72,12 @@ func TestWriteFileRemove(t *testing.T) {
err = os.Chmod(filename, 0444)
require.NoError(t, err, "couldn't change file to read-only")

// Root can bypass read-only file mode, so change the file owner.
if syscall.Geteuid() == 0 {
err = os.Chown(filename, 1000, 1000)
require.NoError(t, err, "couldn't chown the file")
}

// Write must fail and remove the file.
err = WriteFileRemove(filename, []byte(data2), 0644)
require.Error(t, err, "shouldn't write a read-only file")
Expand Down
Loading