@marcel91200 / caddy Mirror · read-only / commits / 0bc27e5f
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 10:45 UTC

logging: fix file mode configuration parsing (#6383)

Commit 101d3e7 introduced file mode setting, but was missing a JSON Marshaller so that CaddyFile can be converted to JSON safely.

Ririsoft committed Jun 8, 2024 at 19:34 UTC 0bc27e5fb1252716f82c2b5af56189e8b46ead3c
2 files changed +44
modules/logging/filewriter.go
+5
@@ -59,6 +59,11 @@ func (m *fileMode) UnmarshalJSON(b []byte) error {
59 return err
60 }
61
62 +// MarshalJSON satisfies json.Marshaler.
63 +func (m *fileMode) MarshalJSON() ([]byte, error) {
64 + return []byte(fmt.Sprintf("\"%04o\"", *m)), nil
65 +}
66 +
67 // parseFileMode parses a file mode string,
68 // adding support for `chmod` unix command like
69 // 1 to 4 digital octal values.
modules/logging/filewriter_test.go
+39
@@ -306,3 +306,42 @@ func TestFileModeJSON(t *testing.T) {
306 })
307 }
308 }
309 +
310 +func TestFileModeToJSON(t *testing.T) {
311 + tests := []struct {
312 + name string
313 + mode fileMode
314 + want string
315 + wantErr bool
316 + }{
317 + {
318 + name: "none zero",
319 + mode: 0644,
320 + want: `"0644"`,
321 + wantErr: false,
322 + },
323 + {
324 + name: "zero mode",
325 + mode: 0,
326 + want: `"0000"`,
327 + wantErr: false,
328 + },
329 + }
330 +
331 + for _, tt := range tests {
332 + t.Run(tt.name, func(t *testing.T) {
333 + var b []byte
334 + var err error
335 +
336 + if b, err = json.Marshal(&tt.mode); (err != nil) != tt.wantErr {
337 + t.Fatalf("MarshalJSON() error = %v, want %v", err, tt.wantErr)
338 + }
339 +
340 + got := string(b[:])
341 +
342 + if got != tt.want {
343 + t.Errorf("got mode %v, want %v", got, tt.want)
344 + }
345 + })
346 + }
347 +}