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 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright 2011 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 com.gitblit.utils.JGitUtils;
  18. import com.gitblit.utils.JnaUtils;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import static org.junit.Assert.assertEquals;
  22. import static org.junit.Assert.assertNotNull;
  23. import static org.junit.Assert.assertTrue;
  24. import org.apache.commons.io.FileUtils;
  25. import org.eclipse.jgit.lib.Repository;
  26. import org.eclipse.jgit.lib.RepositoryCache;
  27. import org.eclipse.jgit.lib.RepositoryCache.FileKey;
  28. import org.eclipse.jgit.util.FS;
  29. import org.junit.Test;
  30. /**
  31. *
  32. * @author Florian Zschocke
  33. */
  34. public class JnaUtilsTest {
  35. @Test
  36. public void testGetgid() {
  37. if (JnaUtils.isWindows()) {
  38. try {
  39. JnaUtils.getFilemode(GitBlitSuite.REPOSITORIES);
  40. } catch(UnsupportedOperationException e) {}
  41. }
  42. else {
  43. int gid = JnaUtils.getgid();
  44. assertTrue(gid >= 0);
  45. int egid = JnaUtils.getegid();
  46. assertTrue(egid >= 0);
  47. assertTrue("Really? You're running unit tests as root?!", gid > 0);
  48. System.out.println("gid: " + gid + " egid: " + egid);
  49. }
  50. }
  51. @Test
  52. public void testGetFilemode() throws IOException {
  53. if (JnaUtils.isWindows()) {
  54. try {
  55. JnaUtils.getFilemode(GitBlitSuite.REPOSITORIES);
  56. } catch(UnsupportedOperationException e) {}
  57. }
  58. else {
  59. String repositoryName = "NewJnaTestRepository.git";
  60. Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES, repositoryName);
  61. File folder = FileKey.resolve(new File(GitBlitSuite.REPOSITORIES, repositoryName), FS.DETECTED);
  62. assertTrue(folder.exists());
  63. int mode = JnaUtils.getFilemode(folder);
  64. assertTrue(mode > 0);
  65. assertEquals(JnaUtils.S_IFDIR, (mode & JnaUtils.S_IFMT)); // directory
  66. assertEquals(JnaUtils.S_IRUSR | JnaUtils.S_IWUSR | JnaUtils.S_IXUSR, (mode & JnaUtils.S_IRWXU)); // owner full access
  67. mode = JnaUtils.getFilemode(folder.getAbsolutePath() + "/config");
  68. assertTrue(mode > 0);
  69. assertEquals(JnaUtils.S_IFREG, (mode & JnaUtils.S_IFMT)); // directory
  70. assertEquals(JnaUtils.S_IRUSR | JnaUtils.S_IWUSR, (mode & JnaUtils.S_IRWXU)); // owner full access
  71. repository.close();
  72. RepositoryCache.close(repository);
  73. FileUtils.deleteDirectory(repository.getDirectory());
  74. }
  75. }
  76. @Test
  77. public void testSetFilemode() throws IOException {
  78. if (JnaUtils.isWindows()) {
  79. try {
  80. JnaUtils.getFilemode(GitBlitSuite.REPOSITORIES);
  81. } catch(UnsupportedOperationException e) {}
  82. }
  83. else {
  84. String repositoryName = "NewJnaTestRepository.git";
  85. Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES, repositoryName);
  86. File folder = FileKey.resolve(new File(GitBlitSuite.REPOSITORIES, repositoryName), FS.DETECTED);
  87. assertTrue(folder.exists());
  88. File path = new File(folder, "refs");
  89. int mode = JnaUtils.getFilemode(path);
  90. assertTrue(mode > 0);
  91. assertEquals(JnaUtils.S_IFDIR, (mode & JnaUtils.S_IFMT)); // directory
  92. assertEquals(JnaUtils.S_IRUSR | JnaUtils.S_IWUSR | JnaUtils.S_IXUSR, (mode & JnaUtils.S_IRWXU)); // owner full access
  93. mode |= JnaUtils.S_ISGID;
  94. mode |= JnaUtils.S_IRWXG;
  95. int ret = JnaUtils.setFilemode(path, mode);
  96. assertEquals(0, ret);
  97. mode = JnaUtils.getFilemode(path);
  98. assertTrue(mode > 0);
  99. assertEquals(JnaUtils.S_ISGID, (mode & JnaUtils.S_ISGID)); // set-gid-bit set
  100. assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP | JnaUtils.S_IXGRP, (mode & JnaUtils.S_IRWXG)); // group full access
  101. path = new File(folder, "config");
  102. mode = JnaUtils.getFilemode(path.getAbsolutePath());
  103. assertTrue(mode > 0);
  104. assertEquals(JnaUtils.S_IFREG, (mode & JnaUtils.S_IFMT)); // directory
  105. assertEquals(JnaUtils.S_IRUSR | JnaUtils.S_IWUSR, (mode & JnaUtils.S_IRWXU)); // owner full access
  106. mode |= (JnaUtils.S_IRGRP | JnaUtils.S_IWGRP);
  107. ret = JnaUtils.setFilemode(path.getAbsolutePath(), mode);
  108. assertEquals(0, ret);
  109. mode = JnaUtils.getFilemode(path.getAbsolutePath());
  110. assertTrue(mode > 0);
  111. assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP, (mode & JnaUtils.S_IRWXG)); // group full access
  112. repository.close();
  113. RepositoryCache.close(repository);
  114. FileUtils.deleteDirectory(repository.getDirectory());
  115. }
  116. }
  117. @Test
  118. public void testGetFilestat() {
  119. if (JnaUtils.isWindows()) {
  120. try {
  121. JnaUtils.getFilemode(GitBlitSuite.REPOSITORIES);
  122. } catch(UnsupportedOperationException e) {}
  123. }
  124. else {
  125. JnaUtils.Filestat stat = JnaUtils.getFilestat(GitBlitSuite.REPOSITORIES);
  126. assertNotNull(stat);
  127. assertTrue(stat.mode > 0);
  128. assertTrue(stat.uid > 0);
  129. assertTrue(stat.gid > 0);
  130. }
  131. }
  132. }