Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

FSJava7Test.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (C) 2012-2013, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.util;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertTrue;
  47. import static org.junit.Assume.assumeFalse;
  48. import static org.junit.Assume.assumeNotNull;
  49. import static org.junit.Assume.assumeTrue;
  50. import java.io.BufferedReader;
  51. import java.io.File;
  52. import java.io.IOException;
  53. import java.io.InputStreamReader;
  54. import java.nio.charset.Charset;
  55. import java.nio.file.Files;
  56. import java.nio.file.attribute.PosixFileAttributeView;
  57. import java.nio.file.attribute.PosixFilePermission;
  58. import java.util.Set;
  59. import org.eclipse.jgit.junit.RepositoryTestCase;
  60. import org.junit.After;
  61. import org.junit.Before;
  62. import org.junit.Test;
  63. public class FSJava7Test {
  64. private File trash;
  65. @Before
  66. public void setUp() throws Exception {
  67. trash = File.createTempFile("tmp_", "");
  68. trash.delete();
  69. assertTrue("mkdir " + trash, trash.mkdir());
  70. }
  71. @After
  72. public void tearDown() throws Exception {
  73. FileUtils.delete(trash, FileUtils.RECURSIVE | FileUtils.RETRY);
  74. }
  75. /**
  76. * The old File methods traverse symbolic links and look at the targets.
  77. * With symbolic links we usually want to modify/look at the link. For some
  78. * reason the executable attribute seems to always look at the target, but
  79. * for the other attributes like lastModified, hidden and exists we must
  80. * differ between the link and the target.
  81. *
  82. * @throws IOException
  83. * @throws InterruptedException
  84. */
  85. @Test
  86. public void testSymlinkAttributes() throws IOException, InterruptedException {
  87. FS fs = FS.DETECTED;
  88. File link = new File(trash, "ä");
  89. File target = new File(trash, "å");
  90. fs.createSymLink(link, "å");
  91. assertTrue(fs.exists(link));
  92. String targetName = fs.readSymLink(link);
  93. assertEquals("å", targetName);
  94. assertTrue(fs.lastModified(link) > 0);
  95. assertTrue(fs.exists(link));
  96. assertFalse(fs.canExecute(link));
  97. assertEquals(2, fs.length(link));
  98. assertFalse(fs.exists(target));
  99. assertFalse(fs.isFile(target));
  100. assertFalse(fs.isDirectory(target));
  101. assertFalse(fs.canExecute(target));
  102. RepositoryTestCase.fsTick(link);
  103. // Now create the link target
  104. FileUtils.createNewFile(target);
  105. assertTrue(fs.exists(link));
  106. assertTrue(fs.lastModified(link) > 0);
  107. assertTrue(fs.lastModified(target) > fs.lastModified(link));
  108. assertFalse(fs.canExecute(link));
  109. fs.setExecute(target, true);
  110. assertFalse(fs.canExecute(link));
  111. assumeTrue(fs.supportsExecute());
  112. assertTrue(fs.canExecute(target));
  113. }
  114. @Test
  115. public void testExecutableAttributes() throws Exception {
  116. FS fs = FS.DETECTED;
  117. // If this assumption fails the test is halted and ignored.
  118. assumeTrue(fs instanceof FS_POSIX);
  119. File f = new File(trash, "bla");
  120. assertTrue(f.createNewFile());
  121. assertFalse(fs.canExecute(f));
  122. String umask = readUmask();
  123. assumeNotNull(umask);
  124. char others = umask.charAt(umask.length() - 1);
  125. boolean badUmask;
  126. if (others != '0' && others != '2' && others != '4' && others != '6') {
  127. // umask is set in the way that "others" can not "execute" => git
  128. // CLI will not set "execute" attribute for "others", so we also
  129. // don't care
  130. badUmask = true;
  131. } else {
  132. badUmask = false;
  133. }
  134. Set<PosixFilePermission> permissions = readPermissions(f);
  135. assertTrue(!permissions.contains(PosixFilePermission.OTHERS_EXECUTE));
  136. assertTrue(!permissions.contains(PosixFilePermission.GROUP_EXECUTE));
  137. assertTrue(!permissions.contains(PosixFilePermission.OWNER_EXECUTE));
  138. fs.setExecute(f, true);
  139. permissions = readPermissions(f);
  140. assertTrue("'owner' execute permission not set",
  141. permissions.contains(PosixFilePermission.OWNER_EXECUTE));
  142. assertTrue("'group' execute permission not set",
  143. permissions.contains(PosixFilePermission.GROUP_EXECUTE));
  144. if (badUmask) {
  145. assertFalse("'others' execute permission set",
  146. permissions.contains(PosixFilePermission.OTHERS_EXECUTE));
  147. System.err.println("WARNING: your system's umask: \"" + umask
  148. + "\" doesn't allow FSJava7Test to test if setting posix"
  149. + " permissions for \"others\" works properly");
  150. assumeFalse(badUmask);
  151. } else {
  152. assertTrue("'others' execute permission not set",
  153. permissions.contains(PosixFilePermission.OTHERS_EXECUTE));
  154. }
  155. }
  156. private String readUmask() throws Exception {
  157. Process p = Runtime.getRuntime().exec(
  158. new String[] { "sh", "-c", "umask" }, null, null);
  159. final BufferedReader lineRead = new BufferedReader(
  160. new InputStreamReader(p.getInputStream(), Charset
  161. .defaultCharset().name()));
  162. p.waitFor();
  163. return lineRead.readLine();
  164. }
  165. private Set<PosixFilePermission> readPermissions(File f) throws IOException {
  166. return Files
  167. .getFileAttributeView(f.toPath(), PosixFileAttributeView.class)
  168. .readAttributes().permissions();
  169. }
  170. }