OldURLClassLoaderTest.java revision 4557728efb66c455a52b7669a8eefef7a9e54854
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package libcore.java.net; 19 20import dalvik.annotation.SideEffect; 21import dalvik.annotation.TestLevel; 22import dalvik.annotation.TestTargetClass; 23import dalvik.annotation.TestTargetNew; 24import java.io.File; 25import java.io.FileOutputStream; 26import java.io.IOException; 27import java.io.InputStream; 28import java.net.MalformedURLException; 29import java.net.URL; 30import java.net.URLClassLoader; 31import java.security.CodeSource; 32import java.security.Permission; 33import java.security.PermissionCollection; 34import java.security.cert.Certificate; 35import java.util.ArrayList; 36import java.util.Enumeration; 37import java.util.List; 38import java.util.jar.Manifest; 39import org.apache.harmony.security.tests.support.TestCertUtils; 40import tests.support.Support_Configuration; 41import tests.support.Support_PortManager; 42import tests.support.Support_TestWebData; 43import tests.support.Support_TestWebServer; 44import tests.support.resource.Support_Resources; 45 46@TestTargetClass( 47 value = URLClassLoader.class, 48 untestedMethods = { 49 @TestTargetNew( 50 level = TestLevel.NOT_NECESSARY, 51 notes = "findClass uses defineClass which is not implemented", 52 method = "findClass", 53 args = {java.lang.String.class} 54 ) 55 } 56) 57public class OldURLClassLoaderTest extends junit.framework.TestCase { 58 59 URLClassLoader ucl; 60 SecurityManager sm = new SecurityManager() { 61 62 public void checkPermission(Permission perm) { 63 } 64 65 public void checkCreateClassLoader() { 66 throw new SecurityException(); 67 } 68 }; 69 70 /** 71 * @tests java.net.URLClassLoader#URLClassLoader(java.net.URL[]) 72 */ 73 public void test_Constructor$Ljava_net_URL() throws MalformedURLException { 74 URL[] u = new URL[0]; 75 ucl = new URLClassLoader(u); 76 assertTrue("Failed to set parent", ucl != null 77 && ucl.getParent() == URLClassLoader.getSystemClassLoader()); 78 79 80 URL [] urls = {new URL("http://foo.com/foo"), 81 new URL("jar:file://foo.jar!/foo.c"), 82 new URL("ftp://foo1/foo2/foo.c")}; 83 84 URLClassLoader ucl1 = new URLClassLoader(urls); 85 assertTrue(urls.length == ucl1.getURLs().length); 86 87 try { 88 Class.forName("test", false, ucl); 89 fail("Should throw ClassNotFoundException"); 90 } catch (ClassNotFoundException e) { 91 // expected 92 } 93 94 SecurityManager oldSm = System.getSecurityManager(); 95 System.setSecurityManager(sm); 96 try { 97 new URLClassLoader(u); 98 fail("SecurityException should be thrown."); 99 } catch (SecurityException e) { 100 // expected 101 } finally { 102 System.setSecurityManager(oldSm); 103 } 104 105 try { 106 new URLClassLoader(new URL[] { null }); 107 } catch(Exception e) { 108 fail("Unexpected exception was thrown: " + e.getMessage()); 109 } 110 } 111 112 /** 113 * @tests java.net.URLClassLoader#findResources(java.lang.String) 114 */ 115 public void test_findResourcesLjava_lang_String() throws Exception { 116 Enumeration<URL> res = null; 117 String[] resValues = { "This is a test resource file.", 118 "This is a resource from a subdir"}; 119 120 String tmp = System.getProperty("java.io.tmpdir") + "/"; 121 122 File tmpDir = new File(tmp); 123 File test1 = new File(tmp + "test0"); 124 test1.deleteOnExit(); 125 FileOutputStream out = new FileOutputStream(test1); 126 out.write(resValues[0].getBytes()); 127 out.flush(); 128 out.close(); 129 130 File subDir = new File(tmp + "subdir/"); 131 subDir.mkdir(); 132 File test2 = new File(tmp + "subdir/test0"); 133 test2.deleteOnExit(); 134 out = new FileOutputStream(test2); 135 out.write(resValues[1].getBytes()); 136 out.flush(); 137 out.close(); 138 139 URL[] urls = new URL[2]; 140 urls[0] = new URL("file://" + tmpDir.getAbsolutePath() + "/"); 141 urls[1] = new URL("file://" + subDir.getAbsolutePath() + "/"); 142 143 ucl = new URLClassLoader(urls); 144 res = ucl.findResources("test0"); 145 assertNotNull("Failed to locate resources", res); 146 147 int i = 0; 148 while (res.hasMoreElements()) { 149 StringBuffer sb = getResContent(res.nextElement()); 150 assertEquals("Returned incorrect resource/or in wrong order", 151 resValues[i++], sb.toString()); 152 } 153 assertEquals("Incorrect number of resources returned", 2, i); 154 } 155 156 @TestTargetNew( 157 level = TestLevel.COMPLETE, 158 notes = "", 159 method = "addURL", 160 args = { URL.class } 161 ) 162 public void test_addURLLjava_net_URL() throws MalformedURLException { 163 URL[] u = new URL[0]; 164 165 URL [] urls = {new URL("http://foo.com/foo"), 166 new URL("jar:file://foo.jar!/foo.c"), 167 new URL("ftp://foo1/foo2/foo.c"), null}; 168 169 TestURLClassLoader tucl = new TestURLClassLoader(u); 170 171 for(int i = 0; i < urls.length;) { 172 tucl.addURL(urls[i]); 173 i++; 174 URL [] result = tucl.getURLs(); 175 assertEquals("Result array length is incorrect: " + i, 176 i, result.length); 177 for(int j = 0; j < result.length; j++) { 178 assertEquals("Result array item is incorrect: " + j, 179 urls[j], result[j]); 180 } 181 } 182 } 183 184 public void test_getPermissions() throws MalformedURLException { 185 URL url = new URL("http://" + Support_Configuration.SpecialInetTestAddress); 186 Certificate[] chain = TestCertUtils.getCertChain(); 187 CodeSource cs = new CodeSource(url, chain); 188 TestURLClassLoader cl = new TestURLClassLoader(new URL[] {url}); 189 PermissionCollection permCol = cl.getPermissions(cs); 190 assertNotNull(permCol); 191 192 URL url1 = new URL("file://foo/foo.c"); 193 TestURLClassLoader cl1 = new TestURLClassLoader(new URL[] {url}); 194 CodeSource cs1 = new CodeSource(url1, chain); 195 PermissionCollection permCol1 = cl1.getPermissions(cs1); 196 assertNotNull(permCol1); 197 } 198 199 public void test_definePackage() throws MalformedURLException { 200 Manifest manifest = new Manifest(); 201 URL[] u = new URL[0]; 202 TestURLClassLoader tucl = new TestURLClassLoader(u); 203 204 URL [] urls = {new URL("http://foo.com/foo"), 205 new URL("jar:file://foo.jar!/foo.c"), 206 new URL("ftp://foo1/foo2/foo.c"), 207 new URL("file://new/package/name/"), 208 null}; 209 210 String packageName = "new.package.name"; 211 212 for(int i = 0; i < urls.length; i++) { 213 Package pack = tucl.definePackage(packageName + i, manifest, urls[i]); 214 assertEquals(packageName + i, pack.getName()); 215 assertNull("Implementation Title is not null", 216 pack.getImplementationTitle()); 217 assertNull("Implementation Vendor is not null", 218 pack.getImplementationVendor()); 219 assertNull("Implementation Version is not null.", 220 pack.getImplementationVersion()); 221 } 222 223 try { 224 tucl.definePackage(packageName + "0", manifest, null); 225 fail("IllegalArgumentException was not thrown."); 226 } catch(IllegalArgumentException iae) { 227 //expected 228 } 229 } 230 231 class TestURLClassLoader extends URLClassLoader { 232 public TestURLClassLoader(URL[] urls) { 233 super(urls); 234 } 235 236 public void addURL(URL url) { 237 super.addURL(url); 238 } 239 240 public Package definePackage(String name, 241 Manifest man, 242 URL url) 243 throws IllegalArgumentException { 244 return super.definePackage(name, man, url); 245 } 246 247 public Class<?> findClass(String name) 248 throws ClassNotFoundException { 249 return super.findClass(name); 250 } 251 252 protected PermissionCollection getPermissions(CodeSource codesource) { 253 return super.getPermissions(codesource); 254 } 255 } 256 257 /** 258 * @tests java.net.URLClassLoader#findResource(java.lang.String) 259 */ 260 @TestTargetNew( 261 level = TestLevel.PARTIAL_COMPLETE, 262 notes = "", 263 method = "findResource", 264 args = {java.lang.String.class} 265 ) 266 @SideEffect("Support_TestWebServer requires isolation.") 267 public void test_findResourceLjava_lang_String() throws Exception { 268 int port = Support_PortManager.getNextPort(); 269 File tmp = File.createTempFile("test", ".txt"); 270 271 Support_TestWebServer server = new Support_TestWebServer(); 272 try { 273 274 server.initServer(port, tmp.getAbsolutePath(), "text/html"); 275 276 URL[] urls = { new URL("http://localhost:" + port + "/") }; 277 ucl = new URLClassLoader(urls); 278 URL res = ucl.findResource("test1"); 279 assertNotNull("Failed to locate resource", res); 280 281 StringBuffer sb = getResContent(res); 282 assertEquals("Returned incorrect resource", new String(Support_TestWebData.test1), 283 sb.toString()); 284 } finally { 285 server.close(); 286 } 287 } 288 289 /** 290 * Regression for Harmony-2237 291 */ 292 @TestTargetNew( 293 level = TestLevel.PARTIAL_COMPLETE, 294 notes = "Regression test", 295 method = "findResource", 296 args = {java.lang.String.class} 297 ) 298 @SideEffect("Support_TestWebServer requires isolation.") 299 public void test_findResource_String() throws Exception { 300 File tempFile1 = File.createTempFile("textFile", ".txt"); 301 tempFile1.createNewFile(); 302 tempFile1.deleteOnExit(); 303 File tempFile2 = File.createTempFile("jarFile", ".jar"); 304 tempFile2.delete(); 305 tempFile2.deleteOnExit(); 306 307 Support_TestWebServer server = new Support_TestWebServer(); 308 int port = Support_PortManager.getNextPort(); 309 try { 310 server.initServer(port, false); 311 312 String tempPath1 = tempFile1.getParentFile().getAbsolutePath() + "/"; 313 InputStream is = getClass().getResourceAsStream( 314 "/tests/resources/hyts_patch.jar"); 315 Support_Resources.copyLocalFileto(tempFile2, is); 316 String tempPath2 = tempFile2.getAbsolutePath(); 317 String tempPath3 = "http://localhost:" + port + "/"; 318 URLClassLoader urlLoader = getURLClassLoader(tempPath1, tempPath2); 319 assertNull("Found inexistant resource", 320 urlLoader.findResource("XXX")); 321 assertNotNull("Couldn't find resource from directory", 322 urlLoader.findResource(tempFile1.getName())); 323 assertNotNull("Couldn't find resource from jar", 324 urlLoader.findResource("Blah.txt")); 325 urlLoader = getURLClassLoader(tempPath1, tempPath2, tempPath3); 326 assertNotNull("Couldn't find resource from web", 327 urlLoader.findResource("test1")); 328 assertNull("Found inexistant resource from web", 329 urlLoader.findResource("test3")); 330 } finally { 331 server.close(); 332 } 333 } 334 335 private static URLClassLoader getURLClassLoader(String... classPath) 336 throws MalformedURLException { 337 List<URL> urlList = new ArrayList<URL>(); 338 for (String path : classPath) { 339 String url; 340 File f = new File(path); 341 if (f.isDirectory()) { 342 url = "file:" + path; 343 } else if (path.startsWith("http")) { 344 url = path; 345 } else { 346 url = "jar:file:" + path + "!/"; 347 } 348 urlList.add(new URL(url)); 349 } 350 return new URLClassLoader(urlList.toArray(new URL[urlList.size()])); 351 } 352 353 private StringBuffer getResContent(URL res) throws IOException { 354 StringBuffer sb = new StringBuffer(); 355 InputStream is = res.openStream(); 356 357 int c; 358 while ((c = is.read()) != -1) { 359 sb.append((char) c); 360 } 361 is.close(); 362 return sb; 363 } 364} 365