From b724448b589d60a9a7dda60cf30741048c98e199 Mon Sep 17 00:00:00 2001 From: Florian Zschocke Date: Wed, 21 Aug 2013 10:39:43 +0200 Subject: Fix set-gid bit clearing under Linux when effective gid is different from file gid. --- src/test/java/com/gitblit/tests/JGitUtilsTest.java | 64 ++++++++++++++++++++++ src/test/java/com/gitblit/tests/JnaUtilsTest.java | 38 +++++++++++++ 2 files changed, 102 insertions(+) (limited to 'src/test') diff --git a/src/test/java/com/gitblit/tests/JGitUtilsTest.java b/src/test/java/com/gitblit/tests/JGitUtilsTest.java index cdf698d6..463c0a84 100644 --- a/src/test/java/com/gitblit/tests/JGitUtilsTest.java +++ b/src/test/java/com/gitblit/tests/JGitUtilsTest.java @@ -218,6 +218,70 @@ public class JGitUtilsTest { } } + @Test + public void testCreateRepositorySharedSgidParent() throws Exception { + if (! JnaUtils.isWindows()) { + String repositoryAll = "NewTestRepositoryAll.git"; + String repositoryUmask = "NewTestRepositoryUmask.git"; + String sgidParent = "sgid"; + + File parent = new File(GitBlitSuite.REPOSITORIES, sgidParent); + File folder = null; + boolean parentExisted = parent.exists(); + try { + if (!parentExisted) { + assertTrue("Could not create SGID parent folder.", parent.mkdir()); + } + int mode = JnaUtils.getFilemode(parent); + assertTrue(mode > 0); + assertEquals(0, JnaUtils.setFilemode(parent, mode | JnaUtils.S_ISGID | JnaUtils.S_IWGRP)); + + Repository repository = JGitUtils.createRepository(parent, repositoryAll, "all"); + folder = FileKey.resolve(new File(parent, repositoryAll), FS.DETECTED); + assertNotNull(repository); + + assertEquals("2", repository.getConfig().getString("core", null, "sharedRepository")); + + assertTrue(folder.exists()); + mode = JnaUtils.getFilemode(folder); + assertEquals(JnaUtils.S_ISGID, mode & JnaUtils.S_ISGID); + + mode = JnaUtils.getFilemode(folder.getAbsolutePath() + "/HEAD"); + assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP, mode & JnaUtils.S_IRWXG); + assertEquals(JnaUtils.S_IROTH, mode & JnaUtils.S_IRWXO); + + mode = JnaUtils.getFilemode(folder.getAbsolutePath() + "/config"); + assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP, mode & JnaUtils.S_IRWXG); + assertEquals(JnaUtils.S_IROTH, mode & JnaUtils.S_IRWXO); + + repository.close(); + RepositoryCache.close(repository); + + + + repository = JGitUtils.createRepository(parent, repositoryUmask, "umask"); + folder = FileKey.resolve(new File(parent, repositoryUmask), FS.DETECTED); + assertNotNull(repository); + + assertEquals(null, repository.getConfig().getString("core", null, "sharedRepository")); + + assertTrue(folder.exists()); + mode = JnaUtils.getFilemode(folder); + assertEquals(JnaUtils.S_ISGID, mode & JnaUtils.S_ISGID); + + repository.close(); + RepositoryCache.close(repository); + } + finally { + FileUtils.delete(new File(parent, repositoryAll), FileUtils.RECURSIVE | FileUtils.IGNORE_ERRORS); + FileUtils.delete(new File(parent, repositoryUmask), FileUtils.RECURSIVE | FileUtils.IGNORE_ERRORS); + if (!parentExisted) { + FileUtils.delete(parent, FileUtils.RECURSIVE | FileUtils.IGNORE_ERRORS); + } + } + } + } + @Test public void testRefs() throws Exception { Repository repository = GitBlitSuite.getJGitRepository(); diff --git a/src/test/java/com/gitblit/tests/JnaUtilsTest.java b/src/test/java/com/gitblit/tests/JnaUtilsTest.java index 2430b6ea..25d1ccf5 100644 --- a/src/test/java/com/gitblit/tests/JnaUtilsTest.java +++ b/src/test/java/com/gitblit/tests/JnaUtilsTest.java @@ -20,6 +20,7 @@ import com.gitblit.utils.JnaUtils; import java.io.File; import java.io.IOException; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import org.apache.commons.io.FileUtils; @@ -35,6 +36,24 @@ import org.junit.Test; */ public class JnaUtilsTest { + @Test + public void testGetgid() { + if (JnaUtils.isWindows()) { + try { + JnaUtils.getFilemode(GitBlitSuite.REPOSITORIES); + } catch(UnsupportedOperationException e) {} + } + else { + int gid = JnaUtils.getgid(); + assertTrue(gid >= 0); + int egid = JnaUtils.getegid(); + assertTrue(egid >= 0); + assertTrue("Really? You're running unit tests as root?!", gid > 0); + System.out.println("gid: " + gid + " egid: " + egid); + } + } + + @Test public void testGetFilemode() throws IOException { if (JnaUtils.isWindows()) { @@ -111,4 +130,23 @@ public class JnaUtilsTest { FileUtils.deleteDirectory(repository.getDirectory()); } } + + + @Test + public void testGetFilestat() { + if (JnaUtils.isWindows()) { + try { + JnaUtils.getFilemode(GitBlitSuite.REPOSITORIES); + } catch(UnsupportedOperationException e) {} + } + else { + JnaUtils.Filestat stat = JnaUtils.getFilestat(GitBlitSuite.REPOSITORIES); + assertNotNull(stat); + assertTrue(stat.mode > 0); + assertTrue(stat.uid > 0); + assertTrue(stat.gid > 0); + } + } + + } -- cgit v1.2.3