(t *testing.T)
| 15 | ) |
| 16 | |
| 17 | func TestFileCreationModes(t *testing.T) { |
| 18 | tests := []struct { |
| 19 | name string |
| 20 | filePerm os.FileMode |
| 21 | dirPerm os.FileMode |
| 22 | umask int |
| 23 | replace bool |
| 24 | wantFilePerm os.FileMode |
| 25 | wantDirPerm os.FileMode |
| 26 | }{ |
| 27 | { |
| 28 | name: "separate file and directory modes", |
| 29 | filePerm: 0o600, |
| 30 | dirPerm: 0o700, |
| 31 | wantFilePerm: 0o600, |
| 32 | wantDirPerm: 0o700, |
| 33 | }, |
| 34 | { |
| 35 | name: "creation respects umask", |
| 36 | filePerm: 0o666, |
| 37 | dirPerm: 0o777, |
| 38 | umask: 0o027, |
| 39 | wantFilePerm: 0o640, |
| 40 | wantDirPerm: 0o750, |
| 41 | }, |
| 42 | { |
| 43 | name: "replacement preserves existing modes", |
| 44 | filePerm: 0o666, |
| 45 | dirPerm: 0o777, |
| 46 | replace: true, |
| 47 | wantFilePerm: 0o640, |
| 48 | wantDirPerm: 0o750, |
| 49 | }, |
| 50 | } |
| 51 | |
| 52 | for _, tt := range tests { |
| 53 | t.Run(tt.name, func(t *testing.T) { |
| 54 | // Umask is process-wide, so these tests must stay sequential. |
| 55 | oldUmask := syscall.Umask(tt.umask) |
| 56 | t.Cleanup(func() { syscall.Umask(oldUmask) }) |
| 57 | |
| 58 | for _, operation := range []string{"OpenFile", "WriteFile", "Root.Create", "Root.WriteFile", "Root.CopyFile"} { |
| 59 | t.Run(operation, func(t *testing.T) { |
| 60 | rootDir := t.TempDir() |
| 61 | name := filepath.Join("one", "two", "file.txt") |
| 62 | path := filepath.Join(rootDir, name) |
| 63 | if tt.replace { |
| 64 | require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o750)) |
| 65 | require.NoError(t, os.WriteFile(path, []byte("old"), 0o640)) |
| 66 | } |
| 67 | |
| 68 | root, err := safepaths.OpenRoot(rootDir) |
| 69 | require.NoError(t, err) |
| 70 | t.Cleanup(func() { require.NoError(t, root.Close()) }) |
| 71 | |
| 72 | var file *os.File |
| 73 | switch operation { |
| 74 | case "OpenFile": |
nothing calls this directly
no test coverage detected