J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 6203047 |
| 27 | * @summary Inconsistency in FilePermission |
| 28 | */ |
| 29 | |
| 30 | import java.io.*; |
| 31 | |
| 32 | public class CanonPath { |
| 33 | |
| 34 | private static boolean windows; |
| 35 | |
| 36 | private static final String WIN_FOOBAR = "\\foo\\bar\\"; |
| 37 | private static final String UNIX_FOOBAR = "/foo/bar/"; |
| 38 | |
| 39 | private static final String WIN_FOO = "\\foo.txt"; |
| 40 | private static final String UNIX_FOO = "/foo.txt"; |
| 41 | |
| 42 | private static final String WIN_BAR = "bar\\bar.txt"; |
| 43 | private static final String UNIX_BAR = "bar/bar.txt"; |
| 44 | |
| 45 | private static final String WIN_SLASH = "\\"; |
| 46 | private static final String UNIX_SLASH = "/"; |
| 47 | |
| 48 | private static void printCanonPath(String label, String path) |
| 49 | throws Exception { |
| 50 | |
| 51 | File f = new File(path); |
| 52 | System.out.println(label + " path = " + f.getCanonicalPath()); |
| 53 | } |
| 54 | |
| 55 | public static void main(String[] args) throws Exception { |
| 56 | |
| 57 | if (System.getProperty("os.name").startsWith("Windows")) { |
| 58 | windows = true; |
| 59 | System.out.println("Testing on Windows"); |
| 60 | } else { |
| 61 | System.out.println("Testing on Unix"); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | System.out.println(); |
| 66 | System.out.println("\\\\foo\\\\bar\\\\- versus /foo/bar/-"); |
| 67 | FilePermission w = new FilePermission(WIN_FOOBAR + "-", "read"); |
| 68 | FilePermission u = new FilePermission(UNIX_FOOBAR + "-", "read"); |
| 69 | printCanonPath("WIN_FOOBAR", WIN_FOOBAR); |
| 70 | printCanonPath("UNIX_FOOBAR", UNIX_FOOBAR); |
| 71 | if (windows) { |
| 72 | if (!w.implies(u) || !u.implies(w)) { |
| 73 | throw new Exception("FOOBAR test failed"); |
| 74 | } |
| 75 | } else { |
| 76 | if (w.implies(u) || u.implies(w)) { |
| 77 | throw new Exception("FOOBAR test failed"); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | |
| 82 | |
| 83 | System.out.println(); |
| 84 | System.out.println("\\\\foo.txt versus /foo.txt"); |
| 85 | w = new FilePermission(WIN_FOO, "read"); |
| 86 | u = new FilePermission(UNIX_FOO, "read"); |
| 87 | printCanonPath("WIN_FOO", WIN_FOO); |
| 88 | printCanonPath("UNIX_FOO", UNIX_FOO); |
| 89 | if (windows) { |
| 90 | if (!w.implies(u) || !u.implies(w)) { |
| 91 | throw new Exception("FOO test failed"); |
| 92 | } |
| 93 | } else { |
| 94 | if (w.implies(u) || u.implies(w)) { |
| 95 | throw new Exception("FOO test failed"); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
| 100 | |
| 101 | System.out.println(); |
| 102 | System.out.println("bar\\\\bar.txt versus bar/bar.txt"); |
| 103 | w = new FilePermission(WIN_BAR, "read"); |
| 104 | u = new FilePermission(UNIX_BAR, "read"); |
| 105 | printCanonPath("WIN_BAR", WIN_BAR); |
| 106 | printCanonPath("UNIX_BAR", UNIX_BAR); |
| 107 | if (windows) { |
| 108 | if (!w.implies(u) || !u.implies(w)) { |
| 109 | throw new Exception("BAR test failed"); |
| 110 | } |
| 111 | } else { |
| 112 | if (w.implies(u) || u.implies(w)) { |
| 113 | throw new Exception("BAR test failed"); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | |
| 118 | |
| 119 | System.out.println(); |
| 120 | System.out.println("\\\\ versus /"); |
| 121 | w = new FilePermission(WIN_SLASH, "read"); |
| 122 | u = new FilePermission(UNIX_SLASH, "read"); |
| 123 | printCanonPath("WIN_SLASH", WIN_SLASH); |
| 124 | printCanonPath("UNIX_SLASH", UNIX_SLASH); |
| 125 | if (windows) { |
| 126 | if (!w.implies(u) || !u.implies(w)) { |
| 127 | throw new Exception("SLASH test failed"); |
| 128 | } |
| 129 | } else { |
| 130 | if (w.implies(u) || u.implies(w)) { |
| 131 | throw new Exception("SLASH test failed"); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | |
| 136 | |
| 137 | System.out.println(); |
| 138 | System.out.println("\\\\- versus /-"); |
| 139 | w = new FilePermission(WIN_SLASH + "-", "read"); |
| 140 | u = new FilePermission(UNIX_SLASH + "-", "read"); |
| 141 | printCanonPath("WIN_SLASH", WIN_SLASH); |
| 142 | printCanonPath("UNIX_SLASH", UNIX_SLASH); |
| 143 | if (windows) { |
| 144 | if (!w.implies(u) || !u.implies(w)) { |
| 145 | throw new Exception("SLASH/- test failed"); |
| 146 | } |
| 147 | } else { |
| 148 | |
| 149 | // XXX |
| 150 | // |
| 151 | // on unix, /- implies everything |
| 152 | |
| 153 | if (w.implies(u) || !u.implies(w)) { |
| 154 | throw new Exception("SLASH/- test failed"); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | |
| 159 | |
| 160 | System.out.println(); |
| 161 | System.out.println("- versus -"); |
| 162 | w = new FilePermission("-", "read"); |
| 163 | u = new FilePermission("-", "read"); |
| 164 | printCanonPath("WIN_DASH", ""); |
| 165 | printCanonPath("UNIX_DASH", ""); |
| 166 | if (windows) { |
| 167 | if (!w.implies(u) || !u.implies(w)) { |
| 168 | throw new Exception("- test failed"); |
| 169 | } |
| 170 | } else { |
| 171 | if (!w.implies(u) || !u.implies(w)) { |
| 172 | throw new Exception("- test failed"); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | |
| 177 | |
| 178 | System.out.println(); |
| 179 | System.out.println("- versus *"); |
| 180 | w = new FilePermission("-", "read"); |
| 181 | u = new FilePermission("*", "read"); |
| 182 | printCanonPath("WIN_DASH", ""); |
| 183 | printCanonPath("UNIX_STAR", ""); |
| 184 | if (windows) { |
| 185 | |
| 186 | // XXX |
| 187 | // |
| 188 | // - implies *, but not the other way around |
| 189 | |
| 190 | if (!w.implies(u) || u.implies(w)) { |
| 191 | throw new Exception("- test failed"); |
| 192 | } |
| 193 | } else { |
| 194 | |
| 195 | // XXX |
| 196 | // |
| 197 | // - implies *, but not the other way around |
| 198 | |
| 199 | if (!w.implies(u) || u.implies(w)) { |
| 200 | throw new Exception("- test failed"); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | |
| 205 | |
| 206 | System.out.println(); |
| 207 | System.out.println("\\\\* versus /*"); |
| 208 | w = new FilePermission(WIN_SLASH + "*", "read"); |
| 209 | u = new FilePermission(UNIX_SLASH + "*", "read"); |
| 210 | printCanonPath("WIN_SLASH", WIN_SLASH); |
| 211 | printCanonPath("UNIX_SLASH", UNIX_SLASH); |
| 212 | if (windows) { |
| 213 | if (!w.implies(u) || !u.implies(w)) { |
| 214 | throw new Exception("SLASH/* test failed"); |
| 215 | } |
| 216 | } else { |
| 217 | if (w.implies(u) || u.implies(w)) { |
| 218 | throw new Exception("SLASH/* test failed"); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | |
| 223 | |
| 224 | System.out.println(); |
| 225 | System.out.println("\\\\foo\\\\bar\\\\- versus /foo/bar/foobar/w.txt"); |
| 226 | w = new FilePermission(WIN_FOOBAR + "-", "read"); |
| 227 | u = new FilePermission("/foo/bar/foobar/w.txt", "read"); |
| 228 | printCanonPath("FOOBAR", WIN_FOOBAR); |
| 229 | printCanonPath("W.TXT", "/foo/bar/foobar/w.txt"); |
| 230 | if (windows) { |
| 231 | if (!w.implies(u) || u.implies(w)) { |
| 232 | throw new Exception("w.txt (-) test failed"); |
| 233 | } |
| 234 | } else { |
| 235 | if (w.implies(u) || u.implies(w)) { |
| 236 | throw new Exception("w.txt (-) test failed"); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | |
| 241 | |
| 242 | System.out.println(); |
| 243 | System.out.println("\\\\foo\\\\bar\\\\* versus /foo/bar/w.txt"); |
| 244 | w = new FilePermission(WIN_FOOBAR + "*", "read"); |
| 245 | u = new FilePermission("/foo/bar/w.txt", "read"); |
| 246 | printCanonPath("FOOBAR", WIN_FOOBAR); |
| 247 | printCanonPath("W.TXT", "/foo/bar/w.txt"); |
| 248 | if (windows) { |
| 249 | if (!w.implies(u) || u.implies(w)) { |
| 250 | throw new Exception("w.txt (*) test failed"); |
| 251 | } |
| 252 | } else { |
| 253 | if (w.implies(u) || u.implies(w)) { |
| 254 | throw new Exception("w.txt (*) test failed"); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | |
| 259 | // make sure "/" does not imply "/-" nor "/*" |
| 260 | |
| 261 | System.out.println(); |
| 262 | System.out.println("/ versus /- and /*"); |
| 263 | File file = new File(UNIX_SLASH); |
| 264 | FilePermission recursive = new FilePermission |
| 265 | (file.getCanonicalPath() + |
| 266 | File.separatorChar + |
| 267 | "-", |
| 268 | "read"); |
| 269 | FilePermission wild = new FilePermission |
| 270 | (file.getCanonicalPath() + |
| 271 | File.separatorChar + |
| 272 | "*", |
| 273 | "read"); |
| 274 | FilePermission standard = new FilePermission |
| 275 | (file.getCanonicalPath(), |
| 276 | "read"); |
| 277 | if (standard.implies(recursive) || standard.implies(wild)) { |
| 278 | throw new Exception("standard vs directory test failed"); |
| 279 | } |
| 280 | } |
| 281 | } |