You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JnaUtilsTest.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright 2013 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.tests;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import org.apache.commons.io.FileUtils;
  20. import org.eclipse.jgit.lib.Repository;
  21. import org.eclipse.jgit.lib.RepositoryCache;
  22. import org.eclipse.jgit.lib.RepositoryCache.FileKey;
  23. import org.eclipse.jgit.util.FS;
  24. import org.junit.Test;
  25. import com.gitblit.utils.JGitUtils;
  26. import com.gitblit.utils.JnaUtils;
  27. /**
  28. *
  29. * @author Florian Zschocke
  30. */
  31. public class JnaUtilsTest extends GitblitUnitTest {
  32. @Test
  33. public void testGetgid() {
  34. if (JnaUtils.isWindows()) {
  35. try {
  36. JnaUtils.getFilemode(GitBlitSuite.REPOSITORIES);
  37. } catch(UnsupportedOperationException e) {}
  38. }
  39. else {
  40. int gid = JnaUtils.getgid();
  41. assertTrue(gid >= 0);
  42. int egid = JnaUtils.getegid();
  43. assertTrue(egid >= 0);
  44. assertTrue("Really? You're running unit tests as root?!", gid > 0);
  45. System.out.println("gid: " + gid + " egid: " + egid);
  46. }
  47. }
  48. @Test
  49. public void testGetFilemode() throws IOException {
  50. if (JnaUtils.isWindows()) {
  51. try {
  52. JnaUtils.getFilemode(GitBlitSuite.REPOSITORIES);
  53. } catch(UnsupportedOperationException e) {}
  54. }
  55. else {
  56. String repositoryName = "NewJnaTestRepository.git";
  57. Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES, repositoryName);
  58. File folder = FileKey.resolve(new File(GitBlitSuite.REPOSITORIES, repositoryName), FS.DETECTED);
  59. assertTrue(folder.exists());
  60. int mode = JnaUtils.getFilemode(folder);
  61. assertTrue(mode > 0);
  62. assertEquals(JnaUtils.S_IFDIR, (mode & JnaUtils.S_IFMT)); // directory
  63. assertEquals(JnaUtils.S_IRUSR | JnaUtils.S_IWUSR | JnaUtils.S_IXUSR, (mode & JnaUtils.S_IRWXU)); // owner full access
  64. mode = JnaUtils.getFilemode(folder.getAbsolutePath() + "/config");
  65. assertTrue(mode > 0);
  66. assertEquals(JnaUtils.S_IFREG, (mode & JnaUtils.S_IFMT)); // directory
  67. assertEquals(JnaUtils.S_IRUSR | JnaUtils.S_IWUSR, (mode & JnaUtils.S_IRWXU)); // owner full access
  68. repository.close();
  69. RepositoryCache.close(repository);
  70. FileUtils.deleteDirectory(repository.getDirectory());
  71. }
  72. }
  73. @Test
  74. public void testSetFilemode() throws IOException {
  75. if (JnaUtils.isWindows()) {
  76. try {
  77. JnaUtils.getFilemode(GitBlitSuite.REPOSITORIES);
  78. } catch(UnsupportedOperationException e) {}
  79. }
  80. else {
  81. String repositoryName = "NewJnaTestRepository.git";
  82. Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES, repositoryName);
  83. File folder = FileKey.resolve(new File(GitBlitSuite.REPOSITORIES, repositoryName), FS.DETECTED);
  84. assertTrue(folder.exists());
  85. File path = new File(folder, "refs");
  86. int mode = JnaUtils.getFilemode(path);
  87. assertTrue(mode > 0);
  88. assertEquals(JnaUtils.S_IFDIR, (mode & JnaUtils.S_IFMT)); // directory
  89. assertEquals(JnaUtils.S_IRUSR | JnaUtils.S_IWUSR | JnaUtils.S_IXUSR, (mode & JnaUtils.S_IRWXU)); // owner full access
  90. mode |= JnaUtils.S_ISGID;
  91. mode |= JnaUtils.S_IRWXG;
  92. int ret = JnaUtils.setFilemode(path, mode);
  93. assertEquals(0, ret);
  94. mode = JnaUtils.getFilemode(path);
  95. assertTrue(mode > 0);
  96. assertEquals(JnaUtils.S_ISGID, (mode & JnaUtils.S_ISGID)); // set-gid-bit set
  97. assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP | JnaUtils.S_IXGRP, (mode & JnaUtils.S_IRWXG)); // group full access
  98. path = new File(folder, "config");
  99. mode = JnaUtils.getFilemode(path.getAbsolutePath());
  100. assertTrue(mode > 0);
  101. assertEquals(JnaUtils.S_IFREG, (mode & JnaUtils.S_IFMT)); // directory
  102. assertEquals(JnaUtils.S_IRUSR | JnaUtils.S_IWUSR, (mode & JnaUtils.S_IRWXU)); // owner full access
  103. mode |= (JnaUtils.S_IRGRP | JnaUtils.S_IWGRP);
  104. ret = JnaUtils.setFilemode(path.getAbsolutePath(), mode);
  105. assertEquals(0, ret);
  106. mode = JnaUtils.getFilemode(path.getAbsolutePath());
  107. assertTrue(mode > 0);
  108. assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP, (mode & JnaUtils.S_IRWXG)); // group full access
  109. repository.close();
  110. RepositoryCache.close(repository);
  111. FileUtils.deleteDirectory(repository.getDirectory());
  112. }
  113. }
  114. @Test
  115. public void testGetFilestat() {
  116. if (JnaUtils.isWindows()) {
  117. try {
  118. JnaUtils.getFilemode(GitBlitSuite.REPOSITORIES);
  119. } catch(UnsupportedOperationException e) {}
  120. }
  121. else {
  122. JnaUtils.Filestat stat = JnaUtils.getFilestat(GitBlitSuite.REPOSITORIES);
  123. assertNotNull(stat);
  124. assertTrue(stat.mode > 0);
  125. assertTrue(stat.uid > 0);
  126. assertTrue(stat.gid > 0);
  127. }
  128. }
  129. }