PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/test/java/io/File/SetAccess.java

https://github.com/ikeji/openjdk7-jdk
Java | 186 lines | 150 code | 7 blank | 29 comment | 83 complexity | c16d2f473decd04af126fd775af72cd0 MD5 | raw file
  1. /*
  2. * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This code is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * version 2 for more details (a copy is included in the LICENSE file that
  13. * accompanied this code).
  14. *
  15. * You should have received a copy of the GNU General Public License version
  16. * 2 along with this work; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20. * or visit www.oracle.com if you need additional information or have any
  21. * questions.
  22. */
  23. /* @test
  24. @bug 4167472 5097703 6216563 6284003 6728842 6464744
  25. @summary Basic test for setWritable/Readable/Executable methods
  26. */
  27. import java.io.*;
  28. import java.nio.file.*;
  29. import java.nio.file.attribute.*;
  30. public class SetAccess {
  31. public static void main(String[] args) throws Exception {
  32. File d = new File(System.getProperty("test.dir", "."));
  33. File f = new File(d, "x.SetAccessPermission");
  34. if (f.exists() && !f.delete())
  35. throw new Exception("Can't delete test file: " + f);
  36. OutputStream o = new FileOutputStream(f);
  37. o.write('x');
  38. o.close();
  39. doTest(f);
  40. f = new File(d, "x.SetAccessPermission.dir");
  41. if (f.exists() && !f.delete())
  42. throw new Exception("Can't delete test dir: " + f);
  43. if (!f.mkdir())
  44. throw new Exception(f + ": Cannot create directory");
  45. doTest(f);
  46. }
  47. public static void doTest(File f) throws Exception {
  48. if (!System.getProperty("os.name").startsWith("Windows")) {
  49. if (!f.setReadOnly())
  50. throw new Exception(f + ": setReadOnly Failed");
  51. if (!f.setWritable(true, true) ||
  52. !f.canWrite() ||
  53. permission(f).charAt(2) != 'w')
  54. throw new Exception(f + ": setWritable(true, ture) Failed");
  55. if (!f.setWritable(false, true) ||
  56. f.canWrite() ||
  57. permission(f).charAt(2) != '-')
  58. throw new Exception(f + ": setWritable(false, true) Failed");
  59. if (!f.setWritable(true, false) ||
  60. !f.canWrite() ||
  61. !permission(f).matches(".(.w.){3}"))
  62. throw new Exception(f + ": setWritable(true, false) Failed");
  63. if (!f.setWritable(false, false) ||
  64. f.canWrite() ||
  65. !permission(f).matches(".(.-.){3}"))
  66. throw new Exception(f + ": setWritable(false, true) Failed");
  67. if (!f.setWritable(true) || !f.canWrite() ||
  68. permission(f).charAt(2) != 'w')
  69. throw new Exception(f + ": setWritable(true, ture) Failed");
  70. if (!f.setWritable(false) || f.canWrite() ||
  71. permission(f).charAt(2) != '-')
  72. throw new Exception(f + ": setWritable(false, true) Failed");
  73. if (!f.setExecutable(true, true) ||
  74. !f.canExecute() ||
  75. permission(f).charAt(3) != 'x')
  76. throw new Exception(f + ": setExecutable(true, true) Failed");
  77. if (!f.setExecutable(false, true) ||
  78. f.canExecute() ||
  79. permission(f).charAt(3) != '-')
  80. throw new Exception(f + ": setExecutable(false, true) Failed");
  81. if (!f.setExecutable(true, false) ||
  82. !f.canExecute() ||
  83. !permission(f).matches(".(..x){3}"))
  84. throw new Exception(f + ": setExecutable(true, false) Failed");
  85. if (!f.setExecutable(false, false) ||
  86. f.canExecute() ||
  87. !permission(f).matches(".(..-){3}"))
  88. throw new Exception(f + ": setExecutable(false, false) Failed");
  89. if (!f.setExecutable(true) || !f.canExecute() ||
  90. permission(f).charAt(3) != 'x')
  91. throw new Exception(f + ": setExecutable(true, true) Failed");
  92. if (!f.setExecutable(false) || f.canExecute() ||
  93. permission(f).charAt(3) != '-')
  94. throw new Exception(f + ": setExecutable(false, true) Failed");
  95. if (!f.setReadable(true, true) ||
  96. !f.canRead() ||
  97. permission(f).charAt(1) != 'r')
  98. throw new Exception(f + ": setReadable(true, true) Failed");
  99. if (!f.setReadable(false, true) ||
  100. f.canRead() ||
  101. permission(f).charAt(1) != '-')
  102. throw new Exception(f + ": setReadable(false, true) Failed");
  103. if (!f.setReadable(true, false) ||
  104. !f.canRead() ||
  105. !permission(f).matches(".(r..){3}"))
  106. throw new Exception(f + ": setReadable(true, false) Failed");
  107. if (!f.setReadable(false, false) ||
  108. f.canRead() ||
  109. !permission(f).matches(".(-..){3}"))
  110. throw new Exception(f + ": setReadable(false, false) Failed");
  111. if (!f.setReadable(true) || !f.canRead() ||
  112. permission(f).charAt(1) != 'r')
  113. throw new Exception(f + ": setReadable(true, true) Failed");
  114. if (!f.setReadable(false) || f.canRead() ||
  115. permission(f).charAt(1) != '-')
  116. throw new Exception(f + ": setReadable(false, true) Failed");
  117. } else {
  118. //Windows platform
  119. if (f.isFile()) {
  120. if (!f.setReadOnly())
  121. throw new Exception(f + ": setReadOnly Failed");
  122. if (!f.setWritable(true, true) || !f.canWrite())
  123. throw new Exception(f + ": setWritable(true, ture) Failed");
  124. if (!f.setWritable(true, false) || !f.canWrite())
  125. throw new Exception(f + ": setWritable(true, false) Failed");
  126. if (!f.setWritable(true) || !f.canWrite())
  127. throw new Exception(f + ": setWritable(true, ture) Failed");
  128. if (!f.setExecutable(true, true) || !f.canExecute())
  129. throw new Exception(f + ": setExecutable(true, true) Failed");
  130. if (!f.setExecutable(true, false) || !f.canExecute())
  131. throw new Exception(f + ": setExecutable(true, false) Failed");
  132. if (!f.setExecutable(true) || !f.canExecute())
  133. throw new Exception(f + ": setExecutable(true, true) Failed");
  134. if (!f.setReadable(true, true) || !f.canRead())
  135. throw new Exception(f + ": setReadable(true, true) Failed");
  136. if (!f.setReadable(true, false) || !f.canRead())
  137. throw new Exception(f + ": setReadable(true, false) Failed");
  138. if (!f.setReadable(true) || !f.canRead())
  139. throw new Exception(f + ": setReadable(true, true) Failed");
  140. }
  141. if (f.isDirectory()) {
  142. // setWritable should fail on directories because the DOS readonly
  143. // attribute prevents a directory from being deleted.
  144. if (f.setWritable(false, true))
  145. throw new Exception(f + ": setWritable(false, true) Succeeded");
  146. if (f.setWritable(false, false))
  147. throw new Exception(f + ": setWritable(false, false) Succeeded");
  148. if (f.setWritable(false))
  149. throw new Exception(f + ": setWritable(false) Succeeded");
  150. } else {
  151. if (!f.setWritable(false, true) || f.canWrite())
  152. throw new Exception(f + ": setWritable(false, true) Failed");
  153. if (!f.setWritable(false, false) || f.canWrite())
  154. throw new Exception(f + ": setWritable(false, false) Failed");
  155. if (!f.setWritable(false) || f.canWrite())
  156. throw new Exception(f + ": setWritable(false) Failed");
  157. }
  158. if (f.setExecutable(false, true))
  159. throw new Exception(f + ": setExecutable(false, true) Failed");
  160. if (f.setExecutable(false, false))
  161. throw new Exception(f + ": setExecutable(false, false) Failed");
  162. if (f.setExecutable(false))
  163. throw new Exception(f + ": setExecutable(false, true) Failed");
  164. if (f.setReadable(false, true))
  165. throw new Exception(f + ": setReadable(false, true) Failed");
  166. if (f.setReadable(false, false))
  167. throw new Exception(f + ": setReadable(false, false) Failed");
  168. if (f.setReadable(false))
  169. throw new Exception(f + ": setReadable(false, true) Failed");
  170. }
  171. if (f.exists() && !f.delete())
  172. throw new Exception("Can't delete test dir: " + f);
  173. }
  174. private static String permission(File f) throws Exception {
  175. PosixFileAttributes attrs = Files.readAttributes(f.toPath(), PosixFileAttributes.class);
  176. String type = attrs.isDirectory() ? "d" : " ";
  177. return type + PosixFilePermissions.toString(attrs.permissions());
  178. }
  179. }