@@ -432,6 +432,7 @@ func TestConvertTagsToMap(t *testing.T) {
432432}
433433
434434func TestChmodIfPermissionMismatch (t * testing.T ) {
435+ skipIfTestingOnWindows (t )
435436 permissionMatchingPath , _ := getWorkDirPath ("permissionMatchingPath" )
436437 _ = makeDir (permissionMatchingPath , 0755 )
437438 defer os .RemoveAll (permissionMatchingPath )
@@ -440,11 +441,23 @@ func TestChmodIfPermissionMismatch(t *testing.T) {
440441 _ = makeDir (permissionMismatchPath , 0721 )
441442 defer os .RemoveAll (permissionMismatchPath )
442443
444+ permissionMatchGidMismatchPath , _ := getWorkDirPath ("permissionMatchGidMismatchPath" )
445+ _ = makeDir (permissionMatchGidMismatchPath , 0755 )
446+ _ = os .Chmod (permissionMatchGidMismatchPath , 0755 | os .ModeSetgid ) // Setgid bit is set
447+ defer os .RemoveAll (permissionMatchGidMismatchPath )
448+
449+ permissionMismatchGidMismatch , _ := getWorkDirPath ("permissionMismatchGidMismatch" )
450+ _ = makeDir (permissionMismatchGidMismatch , 0721 )
451+ _ = os .Chmod (permissionMismatchGidMismatch , 0721 | os .ModeSetgid ) // Setgid bit is set
452+ defer os .RemoveAll (permissionMismatchGidMismatch )
453+
443454 tests := []struct {
444- desc string
445- path string
446- mode os.FileMode
447- expectedError error
455+ desc string
456+ path string
457+ mode os.FileMode
458+ expectedPerms os.FileMode
459+ expectedGidBit bool
460+ expectedError error
448461 }{
449462 {
450463 desc : "Invalid path" ,
@@ -453,16 +466,44 @@ func TestChmodIfPermissionMismatch(t *testing.T) {
453466 expectedError : fmt .Errorf ("CreateFile invalid-path: The system cannot find the file specified" ),
454467 },
455468 {
456- desc : "permission matching path" ,
457- path : permissionMatchingPath ,
458- mode : 0755 ,
459- expectedError : nil ,
469+ desc : "permission matching path" ,
470+ path : permissionMatchingPath ,
471+ mode : 0755 ,
472+ expectedPerms : 0755 ,
473+ expectedGidBit : false ,
474+ expectedError : nil ,
460475 },
461476 {
462- desc : "permission mismatch path" ,
463- path : permissionMismatchPath ,
464- mode : 0755 ,
465- expectedError : nil ,
477+ desc : "permission mismatch path" ,
478+ path : permissionMismatchPath ,
479+ mode : 0755 ,
480+ expectedPerms : 0755 ,
481+ expectedGidBit : false ,
482+ expectedError : nil ,
483+ },
484+ {
485+ desc : "only match the permission mode bits" ,
486+ path : permissionMatchGidMismatchPath ,
487+ mode : 0755 ,
488+ expectedPerms : 0755 ,
489+ expectedGidBit : true ,
490+ expectedError : nil ,
491+ },
492+ {
493+ desc : "only change the permission mode bits when gid is set" ,
494+ path : permissionMismatchGidMismatch ,
495+ mode : 0755 ,
496+ expectedPerms : 0755 ,
497+ expectedGidBit : true ,
498+ expectedError : nil ,
499+ },
500+ {
501+ desc : "only change the permission mode bits when gid is not set but mode bits have gid set" ,
502+ path : permissionMismatchPath ,
503+ mode : 02755 ,
504+ expectedPerms : 0755 ,
505+ expectedGidBit : false ,
506+ expectedError : nil ,
466507 },
467508 }
468509
@@ -473,6 +514,18 @@ func TestChmodIfPermissionMismatch(t *testing.T) {
473514 t .Errorf ("test[%s]: unexpected error: %v, expected error: %v" , test .desc , err , test .expectedError )
474515 }
475516 }
517+
518+ if test .expectedError == nil {
519+ info , _ := os .Lstat (test .path )
520+ if test .expectedError == nil && (info .Mode ()& os .ModePerm != test .expectedPerms ) {
521+ t .Errorf ("test[%s]: unexpected perms: %v, expected perms: %v, " , test .desc , info .Mode ()& os .ModePerm , test .expectedPerms )
522+ }
523+
524+ if (info .Mode ()& os .ModeSetgid != 0 ) != test .expectedGidBit {
525+ t .Errorf ("test[%s]: unexpected gid bit: %v, expected gid bit: %v" , test .desc , info .Mode ()& os .ModeSetgid != 0 , test .expectedGidBit )
526+ }
527+ }
528+
476529 }
477530}
478531
0 commit comments