diff --git a/pkg/utils/flock.go b/pkg/utils/flock.go index 942717b53..0a328ba72 100644 --- a/pkg/utils/flock.go +++ b/pkg/utils/flock.go @@ -18,7 +18,7 @@ type FLock struct { // TryFLock non-blockingly attempts to acquire a lock on the file. func TryFLock(filename string) (*FLock, error) { - fd, err := syscall.Open(filename, syscall.O_CREAT|syscall.O_RDONLY|syscall.O_CLOEXEC, 0o600) + fd, err := syscall.Open(filename, syscall.O_CREAT|syscall.O_RDONLY|syscall.O_CLOEXEC, syscall.S_IRUSR|syscall.S_IWUSR) if err != nil { return nil, err } diff --git a/pkg/utils/flock_test.go b/pkg/utils/flock_test.go new file mode 100644 index 000000000..4755eead9 --- /dev/null +++ b/pkg/utils/flock_test.go @@ -0,0 +1,93 @@ +//go:build !windows +// +build !windows + +package utils_test + +import ( + "os" + "path/filepath" + "testing" + + "github.com/ansible/receptor/pkg/utils" +) + +func TestTryFLock(t *testing.T) { + type args struct { + filename string + } + tests := []struct { + name string + args args + want *utils.FLock + wantErr bool + }{ + { + name: "Positive", + args: args{ + filename: filepath.Join(os.TempDir(), "good_flock_listener"), + }, + want: &utils.FLock{0}, + wantErr: false, + }, + { + name: "Negative", + args: args{ + filename: "", + }, + want: &utils.FLock{}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := utils.TryFLock(tt.args.filename) + if (err != nil) != tt.wantErr { + t.Errorf("%s: TryFLock(): error = %v, wantErr %v", tt.name, err, tt.wantErr) + + return + } + + if err == nil { + if got.Fd < 0 { + t.Errorf("%s: UnixSocketListen(): Invalid got Fd = %+v", tt.name, got) + } + } + }) + } +} + +func TestFLock_Unlock(t *testing.T) { + type fields struct { + Fd int + } + tests := []struct { + name string + fields fields + wantErr bool + }{ + { + name: "Positive", + fields: fields{ + Fd: 1, + }, + wantErr: false, + }, + { + name: "Negative", + fields: fields{ + Fd: -1, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + lock := &utils.FLock{ + Fd: tt.fields.Fd, + } + if err := lock.Unlock(); (err != nil) != tt.wantErr { + t.Errorf("%s: FLock.Unlock() error = %v, wantErr %v", tt.name, err, tt.wantErr) + } + }) + } +} diff --git a/pkg/utils/unixsock_test.go b/pkg/utils/unixsock_test.go index d5569fa89..0ffcdd770 100644 --- a/pkg/utils/unixsock_test.go +++ b/pkg/utils/unixsock_test.go @@ -32,7 +32,7 @@ func TestUnixSocketListen(t *testing.T) { { name: "Positive", args: args{ - filename: filepath.Join(os.TempDir(), "good_listener"), + filename: filepath.Join(os.TempDir(), "good_unixsock_listener"), permissions: 0x0400, }, wantErr: false, @@ -60,7 +60,7 @@ func TestUnixSocketListen(t *testing.T) { if err == nil { if got1.Fd < 0 { - t.Errorf("%s: UnixSocketListen(): Invalid got1 fd = %+v", tt.name, got1) + t.Errorf("%s: UnixSocketListen(): Invalid got1 Fd = %+v", tt.name, got1) } defer got1.Unlock()