1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
// Tags: JDK1.2
// Uses: getResourceBase
// Copyright (C) 2002, 2006 Free Software Foundation, Inc.
// Written by Mark Wielaard (mark@klomp.org)
// This file is part of Mauve.
// Mauve is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
// Mauve is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Mauve; see the file COPYING. If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA. */
package gnu.testlet.java.net.URLClassLoader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import gnu.testlet.TestHarness;
public class getResource extends getResourceBase
{
private File tmpdir, tmpfile, subtmpdir, subtmpfile;
private String jarPath;
private void setup() throws IOException
{
// Setup
String tmp = harness.getTempDirectory();
tmpdir = new File(tmp + File.separator + "mauve-testdir");
if (!tmpdir.mkdir() && !tmpdir.exists())
throw new IOException("Could not create: " + tmpdir);
tmpfile = new File(tmpdir, "testfile");
if (!tmpfile.delete() && tmpfile.exists())
throw new IOException("Could not remove (old): " + tmpfile);
tmpfile.createNewFile();
subtmpdir = new File(tmpdir, "testdir");
if (!subtmpdir.mkdir() && !subtmpdir.exists())
throw new IOException("Could not create: " + subtmpdir);
subtmpfile = new File(subtmpdir, "test");
if (!subtmpfile.delete() && subtmpfile.exists())
throw new IOException("Could not remove (old): " + subtmpfile);
subtmpfile.createNewFile();
jarPath = tmp + File.separator + "m.jar";
FileOutputStream fos = new FileOutputStream(jarPath);
JarOutputStream jos = new JarOutputStream(fos);
JarEntry je = new JarEntry("jresource");
jos.putNextEntry(je);
jos.write(new byte[256]);
je = new JarEntry("path/in/jar/");
jos.putNextEntry(je);
je = new JarEntry("path/in/jar/file");
jos.putNextEntry(je);
jos.write(new byte[256]);
jos.close();
fos.close();
}
private void tearDown()
{
if (tmpdir != null && tmpdir.exists())
{
if (tmpfile != null && tmpfile.exists())
tmpfile.delete();
if (subtmpdir != null && subtmpdir.exists())
{
if (subtmpfile != null && subtmpfile.exists())
subtmpfile.delete();
subtmpdir.delete();
}
tmpdir.delete();
}
if (jarPath != null)
new File(jarPath).delete();
}
public void test (TestHarness h)
{
harness = h;
try
{
setup();
URL[] urls = new URL[2];
urls[0] = tmpdir.toURL();
urls[1] = new URL("file://" + jarPath);
ucl = URLClassLoader.newInstance(urls);
// Check that the root resource can be found
// It is valid as a directory
URL u = ucl.getResource(".");
harness.debug(u != null ? u.toString() : null);
harness.check(u != null, "Checking .");
// Check that the parent directory can not be found
// It is invalid as one shouldn't be able to escape the "url root"
u = ucl.getResource("..");
harness.debug(u != null ? u.toString() : null);
harness.check(u == null, "Checking ..");
// Check that the current directory can not be found
// It is invalid because at one point, we're escaping the "url root"
u = ucl.getResource("../mauve-testdir");
harness.debug(u != null ? u.toString() : null);
harness.check(u == null, "Checking ../mauve-testdir");
// Check that the current directory can not be found
// It is invalid because at one point, we're walking into an invalid directory
u = ucl.getResource("mauve-testdir/..");
harness.debug(u != null ? u.toString() : null);
harness.check(u == null, "Checking mauve-testdir/..");
// Check that the current directory can not be found
// It is invalid because at one point, we're walking into an invalid directory
u = ucl.getResource("mauve-testdir/../");
harness.debug(u != null ? u.toString() : null);
harness.check(u == null, "Checking mauve-testdir/../");
// Check that the current directory can be found
// It is valid because we're walking into an valid directory and back
u = ucl.getResource("testdir/..");
harness.debug(u != null ? u.toString() : null);
harness.check(u != null, "Checking testdir/..");
// Check that the current directory can be found
// It is valid because we're walking into an valid directory and back
u = ucl.getResource("testdir/../");
harness.debug(u != null ? u.toString() : null);
harness.check(u != null, "Checking testdir/../");
// Check that the testdir subdirectory can not be found
// It is invalid because at one point, we're walking into an invalid directory
u = ucl.getResource("mauve-testdir/../testdir");
harness.debug(u != null ? u.toString() : null);
harness.check(u == null, "Checking mauve-testdir/../testdir");
// Check that the testdir subdirectory can be found
// It is valid because we're walking into an valid directory and back
// and into a valid directory again
u = ucl.getResource("testdir/../testdir");
harness.debug(u != null ? u.toString() : null);
harness.check(u != null, "Checking testdir/../testdir");
check("testfile", "mauve-testdir", true);
check("testdir", "mauve-testdir", true);
check("testdir/test", "mauve-testdir", true);
check("jresource", "m.jar", false);
check("path/in/jar/file", "m.jar", false);
check("path/in/jar", "m.jar", false);
}
catch(IOException ioe)
{
harness.fail("Unexpected exception: " + ioe);
harness.debug(ioe);
}
finally
{
tearDown();
}
}
}
|