|
| 1 | +//go:build !windows |
| 2 | +// +build !windows |
| 3 | + |
| 4 | +/* |
| 5 | +Copyright 2020 The Kubernetes Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +*/ |
| 19 | + |
| 20 | +package nfs |
| 21 | + |
| 22 | +import ( |
| 23 | + "os" |
| 24 | + "syscall" |
| 25 | + "testing" |
| 26 | +) |
| 27 | + |
| 28 | +func TestChmodIfPermissionMismatchSpecialBits(t *testing.T) { |
| 29 | + tests := []struct { |
| 30 | + desc string |
| 31 | + initialMode uint32 |
| 32 | + requestedMode os.FileMode |
| 33 | + expectChmod bool |
| 34 | + }{ |
| 35 | + { |
| 36 | + desc: "setgid bit 02770", |
| 37 | + initialMode: 0770, |
| 38 | + requestedMode: 02770, |
| 39 | + expectChmod: true, |
| 40 | + }, |
| 41 | + { |
| 42 | + desc: "sticky bit 01777", |
| 43 | + initialMode: 0777, |
| 44 | + requestedMode: 01777, |
| 45 | + expectChmod: true, |
| 46 | + }, |
| 47 | + { |
| 48 | + desc: "setgid already set 02770 -> 02770 no change", |
| 49 | + initialMode: 02770, |
| 50 | + requestedMode: 02770, |
| 51 | + expectChmod: false, |
| 52 | + }, |
| 53 | + { |
| 54 | + desc: "setuid bit 04755", |
| 55 | + initialMode: 0755, |
| 56 | + requestedMode: 04755, |
| 57 | + expectChmod: true, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + for _, test := range tests { |
| 62 | + t.Run(test.desc, func(t *testing.T) { |
| 63 | + dir := t.TempDir() |
| 64 | + targetPath := dir + "/testdir" |
| 65 | + if err := os.Mkdir(targetPath, 0777); err != nil { |
| 66 | + t.Fatalf("failed to create test dir: %v", err) |
| 67 | + } |
| 68 | + // Set initial permissions using syscall to support special bits |
| 69 | + if err := syscall.Chmod(targetPath, test.initialMode); err != nil { |
| 70 | + t.Fatalf("failed to set initial mode: %v", err) |
| 71 | + } |
| 72 | + |
| 73 | + if err := chmodIfPermissionMismatch(targetPath, test.requestedMode); err != nil { |
| 74 | + t.Fatalf("chmodIfPermissionMismatch failed: %v", err) |
| 75 | + } |
| 76 | + |
| 77 | + // Verify the final permissions |
| 78 | + info, err := os.Lstat(targetPath) |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("failed to stat: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + // Get raw mode bits via syscall for accurate comparison |
| 84 | + stat := info.Sys().(*syscall.Stat_t) |
| 85 | + actualMode := uint32(stat.Mode) & 07777 |
| 86 | + expectedMode := uint32(test.requestedMode) |
| 87 | + if actualMode != expectedMode { |
| 88 | + t.Errorf("expected mode 0%o, got 0%o", expectedMode, actualMode) |
| 89 | + } |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
0 commit comments