Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 01:00 UTC
| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //go:build windows |
| 16 | |
| 17 | package logging |
| 18 | |
| 19 | import ( |
| 20 | "os" |
| 21 | "path" |
| 22 | "testing" |
| 23 | ) |
| 24 | |
| 25 | // Windows relies on ACLs instead of unix permissions model. |
| 26 | // Go allows to open files with a particular mode but it is limited to read or write. |
| 27 | // See https://cs.opensource.google/go/go/+/refs/tags/go1.22.3:src/syscall/syscall_windows.go;l=708. |
| 28 | // This is pretty restrictive and has little interest for log files and thus we just test that log files are |
| 29 | // opened with R/W permissions by default on Windows too. |
| 30 | func TestFileCreationMode(t *testing.T) { |
| 31 | dir, err := os.MkdirTemp("", "caddytest") |
| 32 | if err != nil { |
| 33 | t.Fatalf("failed to create tempdir: %v", err) |
| 34 | } |
| 35 | defer os.RemoveAll(dir) |
| 36 | |
| 37 | fw := &FileWriter{ |
| 38 | Filename: path.Join(dir, "test.log"), |
| 39 | } |
| 40 | |
| 41 | logger, err := fw.OpenWriter() |
| 42 | if err != nil { |
| 43 | t.Fatalf("failed to create file: %v", err) |
| 44 | } |
| 45 | defer logger.Close() |
| 46 | |
| 47 | st, err := os.Stat(fw.Filename) |
| 48 | if err != nil { |
| 49 | t.Fatalf("failed to check file permissions: %v", err) |
| 50 | } |
| 51 | |
| 52 | if st.Mode().Perm()&0o600 != 0o600 { |
| 53 | t.Fatalf("file mode is %v, want rw for user", st.Mode().Perm()) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func TestDirMode_Windows_CreateSucceeds(t *testing.T) { |
| 58 | dir, err := os.MkdirTemp("", "caddytest") |
| 59 | if err != nil { |
| 60 | t.Fatalf("failed to create tempdir: %v", err) |
| 61 | } |
| 62 | defer os.RemoveAll(dir) |
| 63 | |
| 64 | tests := []struct { |
| 65 | name string |
| 66 | dirMode string |
| 67 | }{ |
| 68 | {"inherit", "inherit"}, |
| 69 | {"from_file", "from_file"}, |
| 70 | {"octal", "0755"}, |
| 71 | {"default", ""}, |
| 72 | } |
| 73 | |
| 74 | for _, tt := range tests { |
| 75 | t.Run(tt.name, func(t *testing.T) { |
| 76 | subdir := path.Join(dir, "logs-"+tt.name) |
| 77 | fw := &FileWriter{ |
| 78 | Filename: path.Join(subdir, "test.log"), |
| 79 | DirMode: tt.dirMode, |
| 80 | Mode: 0o600, |
| 81 | } |
| 82 | w, err := fw.OpenWriter() |
| 83 | if err != nil { |
| 84 | t.Fatalf("failed to open writer: %v", err) |
| 85 | } |
| 86 | defer w.Close() |
| 87 | |
| 88 | if _, err := os.Stat(fw.Filename); err != nil { |
| 89 | t.Fatalf("expected file to exist: %v", err) |
| 90 | } |
| 91 | }) |
| 92 | } |
| 93 | } |