@@ -23,6 +23,7 @@ import (
23
23
"fmt"
24
24
"os"
25
25
"reflect"
26
+ "runtime"
26
27
"sort"
27
28
"strings"
28
29
"testing"
@@ -1508,6 +1509,10 @@ func TestIsSupportedContainerNamePrefix(t *testing.T) {
1508
1509
}
1509
1510
1510
1511
func TestChmodIfPermissionMismatch (t * testing.T ) {
1512
+ if runtime .GOOS == "windows" {
1513
+ t .Skip ("Skipping test on Windows" )
1514
+ }
1515
+
1511
1516
permissionMatchingPath , _ := getWorkDirPath ("permissionMatchingPath" )
1512
1517
_ = makeDir (permissionMatchingPath )
1513
1518
defer os .RemoveAll (permissionMatchingPath )
@@ -1516,11 +1521,23 @@ func TestChmodIfPermissionMismatch(t *testing.T) {
1516
1521
_ = os .MkdirAll (permissionMismatchPath , os .FileMode (0721 ))
1517
1522
defer os .RemoveAll (permissionMismatchPath )
1518
1523
1524
+ permissionMatchGidMismatchPath , _ := getWorkDirPath ("permissionMatchGidMismatchPath" )
1525
+ _ = os .MkdirAll (permissionMatchGidMismatchPath , os .FileMode (0755 ))
1526
+ _ = os .Chmod (permissionMatchGidMismatchPath , 0755 | os .ModeSetgid ) // Setgid bit is set
1527
+ defer os .RemoveAll (permissionMatchGidMismatchPath )
1528
+
1529
+ permissionMismatchGidMismatch , _ := getWorkDirPath ("permissionMismatchGidMismatch" )
1530
+ _ = os .MkdirAll (permissionMismatchGidMismatch , os .FileMode (0721 ))
1531
+ _ = os .Chmod (permissionMismatchGidMismatch , 0721 | os .ModeSetgid ) // Setgid bit is set
1532
+ defer os .RemoveAll (permissionMismatchGidMismatch )
1533
+
1519
1534
tests := []struct {
1520
- desc string
1521
- path string
1522
- mode os.FileMode
1523
- expectedError error
1535
+ desc string
1536
+ path string
1537
+ mode os.FileMode
1538
+ expectedPerms os.FileMode
1539
+ expectedGidBit bool
1540
+ expectedError error
1524
1541
}{
1525
1542
{
1526
1543
desc : "Invalid path" ,
@@ -1529,16 +1546,52 @@ func TestChmodIfPermissionMismatch(t *testing.T) {
1529
1546
expectedError : fmt .Errorf ("CreateFile invalid-path: The system cannot find the file specified" ),
1530
1547
},
1531
1548
{
1532
- desc : "permission matching path" ,
1533
- path : permissionMatchingPath ,
1534
- mode : 0755 ,
1535
- expectedError : nil ,
1549
+ desc : "permission matching path" ,
1550
+ path : permissionMatchingPath ,
1551
+ mode : 0755 ,
1552
+ expectedPerms : 0755 ,
1553
+ expectedGidBit : false ,
1554
+ expectedError : nil ,
1536
1555
},
1537
1556
{
1538
- desc : "permission mismatch path" ,
1539
- path : permissionMismatchPath ,
1540
- mode : 0755 ,
1541
- expectedError : nil ,
1557
+ desc : "permission mismatch path" ,
1558
+ path : permissionMismatchPath ,
1559
+ mode : 0755 ,
1560
+ expectedPerms : 0755 ,
1561
+ expectedGidBit : false ,
1562
+ expectedError : nil ,
1563
+ },
1564
+ {
1565
+ desc : "permission mismatch path" ,
1566
+ path : permissionMismatchPath ,
1567
+ mode : 0755 ,
1568
+ expectedPerms : 0755 ,
1569
+ expectedGidBit : false ,
1570
+ expectedError : nil ,
1571
+ },
1572
+ {
1573
+ desc : "only match the permission mode bits" ,
1574
+ path : permissionMatchGidMismatchPath ,
1575
+ mode : 0755 ,
1576
+ expectedPerms : 0755 ,
1577
+ expectedGidBit : true ,
1578
+ expectedError : nil ,
1579
+ },
1580
+ {
1581
+ desc : "only change the permission mode bits when gid is set" ,
1582
+ path : permissionMismatchGidMismatch ,
1583
+ mode : 0755 ,
1584
+ expectedPerms : 0755 ,
1585
+ expectedGidBit : true ,
1586
+ expectedError : nil ,
1587
+ },
1588
+ {
1589
+ desc : "only change the permission mode bits when gid is not set but mode bits have gid set" ,
1590
+ path : permissionMismatchPath ,
1591
+ mode : 02755 ,
1592
+ expectedPerms : 0755 ,
1593
+ expectedGidBit : false ,
1594
+ expectedError : nil ,
1542
1595
},
1543
1596
}
1544
1597
@@ -1549,7 +1602,19 @@ func TestChmodIfPermissionMismatch(t *testing.T) {
1549
1602
t .Errorf ("test[%s]: unexpected error: %v, expected error: %v" , test .desc , err , test .expectedError )
1550
1603
}
1551
1604
}
1605
+
1606
+ if test .expectedError == nil {
1607
+ info , _ := os .Lstat (test .path )
1608
+ if test .expectedError == nil && (info .Mode ()& os .ModePerm != test .expectedPerms ) {
1609
+ t .Errorf ("test[%s]: unexpected perms: %v, expected perms: %v, " , test .desc , info .Mode ()& os .ModePerm , test .expectedPerms )
1610
+ }
1611
+
1612
+ if (info .Mode ()& os .ModeSetgid != 0 ) != test .expectedGidBit {
1613
+ t .Errorf ("test[%s]: unexpected gid bit: %v, expected gid bit: %v" , test .desc , info .Mode ()& os .ModeSetgid != 0 , test .expectedGidBit )
1614
+ }
1615
+ }
1552
1616
}
1617
+
1553
1618
}
1554
1619
1555
1620
// getWorkDirPath returns the path to the current working directory
0 commit comments