Permalink
Browse files

[bugfix] Make sure setUid, setGid and setSticky work for unsetting. A…

…dded further test cases to prove
  • Loading branch information...
1 parent e809dcd commit 59090814fc1310b7d4c68ad3f017f2cfcabef376 @adamretter adamretter committed Nov 12, 2013
@@ -237,7 +237,7 @@ public boolean isSetUid() {
@PermissionRequired(user = IS_DBA | IS_OWNER)
@Override
public void setSetUid(final boolean setUid) {
- this.vector = (((vector >>> 31) | (setUid ? 1 : 0)) << 31) | (vector & 2147483647);
+ this.vector = (((vector >>> 32) << 1 | (setUid ? 1 : 0)) << 31) | (vector & 2147483647);
}
@Override
@@ -248,7 +248,7 @@ public boolean isSetGid() {
@PermissionRequired(user = IS_DBA | IS_OWNER)
@Override
public void setSetGid(final boolean setGid) {
- this.vector = (((vector >>> 7) | (setGid ? 1 : 0)) << 7) | (vector & 127);
+ this.vector = (((vector >>> 8) << 1 | (setGid ? 1 : 0)) << 7) | (vector & 127);
}
@Override
@@ -259,7 +259,7 @@ public boolean isSticky() {
@PermissionRequired(user = IS_DBA | IS_OWNER)
@Override
public void setSticky(final boolean sticky) {
- this.vector = (((vector >>> 3) | (sticky ? 1 : 0)) << 3) | (vector & 7);
+ this.vector = (((vector >>> 4) << 1 | (sticky ? 1 : 0)) << 3) | (vector & 7);
}
/**
@@ -266,6 +266,9 @@ public void validate_cant_write_WhenNotOwner_and_NotInGroup_and_OtherWithoutWrit
@Test
public void permission_toString() {
+
+ //TODO need tests for toString() to produce 's' and 'S' where appropriate... big 'S' indicates not 'x'
+
final SecurityManager mockSecurityManager = EasyMock.createMock(SecurityManager.class);
final int ownerId = new Random().nextInt(SecurityManagerImpl.MAX_USER_ID);
@@ -340,10 +343,63 @@ public void permission_setFromModeString_existSymbolic() throws SyntaxException,
permission.setMode("user=-read,-write,-execute,group=-read,-write,-execute,other=-read,-write,-execute");
assertEquals(0, permission.getMode());
}
-
- //TODO need tests for toString() to produce 's' and 'S' where appropriate... big 'S' indicates not 'x'
- //TODO need tests for set then get uid/gid/sticky
+ @Test
+ public void setUid_roundtrip() throws PermissionDeniedException {
+ final SecurityManager mockSecurityManager = EasyMock.createMock(SecurityManager.class);
+ final int ownerId = new Random().nextInt(SecurityManagerImpl.MAX_USER_ID);
+ final int ownerGroupId = new Random().nextInt(SecurityManagerImpl.MAX_GROUP_ID);
+
+ Permission permission = new TestableUnixStylePermissionWithCurrentSubject(mockSecurityManager, ownerId, ownerGroupId, 0555);
+ assertFalse(permission.isSetUid());
+ permission.setSetUid(true);
+ assertTrue(permission.isSetUid());
+ assertEquals(04555, permission.getMode());
+
+ permission = new TestableUnixStylePermissionWithCurrentSubject(mockSecurityManager, ownerId, ownerGroupId, 04555);
+ assertTrue(permission.isSetUid());
+ permission.setSetUid(false);
+ assertFalse(permission.isSetUid());
+ assertEquals(0555, permission.getMode());
+ }
+
+ @Test
+ public void setGid_roundtrip() throws PermissionDeniedException {
+ final SecurityManager mockSecurityManager = EasyMock.createMock(SecurityManager.class);
+ final int ownerId = new Random().nextInt(SecurityManagerImpl.MAX_USER_ID);
+ final int ownerGroupId = new Random().nextInt(SecurityManagerImpl.MAX_GROUP_ID);
+
+ Permission permission = new TestableUnixStylePermissionWithCurrentSubject(mockSecurityManager, ownerId, ownerGroupId, 0555);
+ assertFalse(permission.isSetGid());
+ permission.setSetGid(true);
+ assertTrue(permission.isSetGid());
+ assertEquals(02555, permission.getMode());
+
+ permission = new TestableUnixStylePermissionWithCurrentSubject(mockSecurityManager, ownerId, ownerGroupId, 02555);
+ assertTrue(permission.isSetGid());
+ permission.setSetGid(false);
+ assertFalse(permission.isSetGid());
+ assertEquals(0555, permission.getMode());
+ }
+
+ @Test
+ public void setSticky_roundtrip() throws PermissionDeniedException {
+ final SecurityManager mockSecurityManager = EasyMock.createMock(SecurityManager.class);
+ final int ownerId = new Random().nextInt(SecurityManagerImpl.MAX_USER_ID);
+ final int ownerGroupId = new Random().nextInt(SecurityManagerImpl.MAX_GROUP_ID);
+
+ Permission permission = new TestableUnixStylePermissionWithCurrentSubject(mockSecurityManager, ownerId, ownerGroupId, 0555);
+ assertFalse(permission.isSticky());
+ permission.setSticky(true);
+ assertTrue(permission.isSticky());
+ assertEquals(01555, permission.getMode());
+
+ permission = new TestableUnixStylePermissionWithCurrentSubject(mockSecurityManager, ownerId, ownerGroupId, 01555);
+ assertTrue(permission.isSticky());
+ permission.setSticky(false);
+ assertFalse(permission.isSticky());
+ assertEquals(0555, permission.getMode());
+ }
@Test
public void permission_setFromModeString_unixSymbolic() throws SyntaxException, PermissionDeniedException {

0 comments on commit 5909081

Please sign in to comment.