Skip to content

Commit

Permalink
limit windows testing to R/W permissions for owner
Browse files Browse the repository at this point in the history
  • Loading branch information
ririsoft committed May 15, 2024
1 parent cc499d0 commit aa9c71f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 17 deletions.
12 changes: 8 additions & 4 deletions modules/logging/filewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !windows

package logging

import (
"encoding/json"
"os"
"path"
"syscall"
"testing"

"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
Expand Down Expand Up @@ -64,8 +67,8 @@ func TestFileCreationMode(t *testing.T) {
},
}

m := umask(0o000)
defer umask(m)
m := syscall.Umask(0o000)
defer syscall.Umask(m)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -96,8 +99,8 @@ func TestFileCreationMode(t *testing.T) {
}

func TestFileRotationPreserveMode(t *testing.T) {
m := umask(0o000)
defer umask(m)
m := syscall.Umask(0o000)
defer syscall.Umask(m)

dir, err := os.MkdirTemp("", "caddytest")
if err != nil {
Expand Down Expand Up @@ -132,6 +135,7 @@ func TestFileRotationPreserveMode(t *testing.T) {
}

if len(files) != 2 {
t.Log("got files: ", files)
t.Fatalf("got %v files want 2", len(files))
}

Expand Down
10 changes: 0 additions & 10 deletions modules/logging/filewriter_test_posix.go

This file was deleted.

55 changes: 52 additions & 3 deletions modules/logging/filewriter_test_windows.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,55 @@
// Copyright 2015 Matthew Holt and The Caddy Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build windows

package logging

//nolint:unused
func umask(_ int) (oldmask int) {
return 0000
import (
"os"
"path"
"testing"
)

// Windows relies on ACLs instead of unix permissions model.
// Go allows to open files with a particular mode put it is limited to read or write.
// See https://cs.opensource.google/go/go/+/refs/tags/go1.22.3:src/syscall/syscall_windows.go;l=708.
// This is pretty restrictive and has few interest for log files and thus we just test that log files are
// opened with R/W permissions by default on Windows too.
func TestFileCreationMode(t *testing.T) {
dir, err := os.MkdirTemp("", "caddytest")
if err != nil {
t.Fatalf("failed to create tempdir: %v", err)
}
defer os.RemoveAll(dir)

fw := &FileWriter{
Filename: path.Join(dir, "test.log"),
}

logger, err := fw.OpenWriter()
if err != nil {
t.Fatalf("failed to create file: %v", err)
}
defer logger.Close()

st, err := os.Stat(fw.Filename)
if err != nil {
t.Fatalf("failed to check file permissions: %v", err)
}

if st.Mode().Perm()&0o600 != 0o600 {
t.Errorf("file mode is %v, want rw for user", st.Mode().Perm())
}
}

0 comments on commit aa9c71f

Please sign in to comment.