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 18 package libcore.java.lang; 19 20 import java.io.ByteArrayInputStream; 21 import java.io.ByteArrayOutputStream; 22 import java.io.File; 23 import java.io.IOException; 24 import java.io.InputStream; 25 import java.io.OutputStream; 26 import java.security.Permission; 27 import java.util.Arrays; 28 import java.util.Vector; 29 import tests.support.resource.Support_Resources; 30 31 public class OldRuntimeTest extends junit.framework.TestCase { 32 33 Runtime r = Runtime.getRuntime(); 34 35 InputStream is; 36 test_getRuntime()37 public void test_getRuntime() { 38 // Test for method java.lang.Runtime java.lang.Runtime.getRuntime() 39 assertNotNull(Runtime.getRuntime()); 40 } 41 test_addShutdownHook()42 public void test_addShutdownHook() { 43 Thread thrException = new Thread () { 44 public void run() { 45 try { 46 Runtime.getRuntime().addShutdownHook(this); 47 fail("IllegalStateException was not thrown."); 48 } catch(IllegalStateException ise) { 49 //expected 50 } 51 } 52 }; 53 54 try { 55 Runtime.getRuntime().addShutdownHook(thrException); 56 } catch (Throwable t) { 57 fail(t.getMessage()); 58 } 59 60 try { 61 Runtime.getRuntime().addShutdownHook(thrException); 62 fail("IllegalArgumentException was not thrown."); 63 } catch(IllegalArgumentException iae) { 64 // expected 65 } 66 67 SecurityManager sm = new SecurityManager() { 68 69 public void checkPermission(Permission perm) { 70 if (perm.getName().equals("shutdownHooks")) { 71 throw new SecurityException(); 72 } 73 } 74 }; 75 76 // remove previously added hook so we're not depending on the priority 77 // of the Exceptions to be thrown. 78 Runtime.getRuntime().removeShutdownHook(thrException); 79 80 try { 81 Thread.currentThread().sleep(1000); 82 } catch (InterruptedException ie) { 83 } 84 } 85 test_availableProcessors()86 public void test_availableProcessors() { 87 assertTrue(Runtime.getRuntime().availableProcessors() > 0); 88 } 89 90 test_execLjava_lang_StringLjava_lang_StringArray()91 public void test_execLjava_lang_StringLjava_lang_StringArray() { 92 String [] envp = getEnv(); 93 94 checkExec(0, envp, null); 95 checkExec(0, null, null); 96 97 try { 98 Runtime.getRuntime().exec((String)null, null); 99 fail("NullPointerException should be thrown."); 100 } catch(IOException ioe) { 101 fail("IOException was thrown."); 102 } catch(NullPointerException npe) { 103 //expected 104 } 105 106 SecurityManager sm = new SecurityManager() { 107 108 public void checkPermission(Permission perm) { 109 if (perm.getName().equals("checkExec")) { 110 throw new SecurityException(); 111 } 112 } 113 114 public void checkExec(String cmd) { 115 throw new SecurityException(); 116 } 117 }; 118 119 try { 120 Runtime.getRuntime().exec("", envp); 121 fail("IllegalArgumentException should be thrown."); 122 } catch(IllegalArgumentException iae) { 123 //expected 124 } catch (IOException e) { 125 fail("IOException was thrown."); 126 } 127 } 128 test_execLjava_lang_StringArrayLjava_lang_StringArray()129 public void test_execLjava_lang_StringArrayLjava_lang_StringArray() { 130 String [] envp = getEnv(); 131 132 checkExec(4, envp, null); 133 checkExec(4, null, null); 134 135 try { 136 Runtime.getRuntime().exec((String[])null, null); 137 fail("NullPointerException should be thrown."); 138 } catch(IOException ioe) { 139 fail("IOException was thrown."); 140 } catch(NullPointerException npe) { 141 //expected 142 } 143 144 try { 145 Runtime.getRuntime().exec(new String[]{"ls", null}, null); 146 fail("NullPointerException should be thrown."); 147 } catch(IOException ioe) { 148 fail("IOException was thrown."); 149 } catch(NullPointerException npe) { 150 //expected 151 } 152 153 SecurityManager sm = new SecurityManager() { 154 155 public void checkPermission(Permission perm) { 156 if (perm.getName().equals("checkExec")) { 157 throw new SecurityException(); 158 } 159 } 160 161 public void checkExec(String cmd) { 162 throw new SecurityException(); 163 } 164 }; 165 166 try { 167 Runtime.getRuntime().exec(new String[]{}, envp); 168 fail("IndexOutOfBoundsException should be thrown."); 169 } catch(IndexOutOfBoundsException ioob) { 170 //expected 171 } catch (IOException e) { 172 fail("IOException was thrown."); 173 } 174 175 try { 176 Runtime.getRuntime().exec(new String[]{""}, envp); 177 fail("IOException should be thrown."); 178 } catch (IOException e) { /* expected */ } 179 } 180 test_execLjava_lang_StringLjava_lang_StringArrayLjava_io_File()181 public void test_execLjava_lang_StringLjava_lang_StringArrayLjava_io_File() { 182 183 String [] envp = getEnv(); 184 185 File workFolder = Support_Resources.createTempFolder(); 186 187 checkExec(2, envp, workFolder); 188 checkExec(2, null, null); 189 190 try { 191 Runtime.getRuntime().exec((String)null, null, workFolder); 192 fail("NullPointerException should be thrown."); 193 } catch(IOException ioe) { 194 fail("IOException was thrown."); 195 } catch(NullPointerException npe) { 196 //expected 197 } 198 199 SecurityManager sm = new SecurityManager() { 200 201 public void checkPermission(Permission perm) { 202 if (perm.getName().equals("checkExec")) { 203 throw new SecurityException(); 204 } 205 } 206 207 public void checkExec(String cmd) { 208 throw new SecurityException(); 209 } 210 }; 211 212 try { 213 Runtime.getRuntime().exec("", envp, workFolder); 214 fail("SecurityException should be thrown."); 215 } catch(IllegalArgumentException iae) { 216 //expected 217 } catch (IOException e) { 218 fail("IOException was thrown."); 219 } 220 } 221 test_execLjava_lang_StringArrayLjava_lang_StringArrayLjava_io_File()222 public void test_execLjava_lang_StringArrayLjava_lang_StringArrayLjava_io_File() { 223 String [] envp = getEnv(); 224 225 File workFolder = Support_Resources.createTempFolder(); 226 227 checkExec(5, envp, workFolder); 228 checkExec(5, null, null); 229 230 try { 231 Runtime.getRuntime().exec((String[])null, null, workFolder); 232 fail("NullPointerException should be thrown."); 233 } catch(IOException ioe) { 234 fail("IOException was thrown."); 235 } catch(NullPointerException npe) { 236 //expected 237 } 238 239 try { 240 Runtime.getRuntime().exec(new String[]{"ls", null}, null, workFolder); 241 fail("NullPointerException should be thrown."); 242 } catch(IOException ioe) { 243 fail("IOException was thrown."); 244 } catch(NullPointerException npe) { 245 //expected 246 } 247 248 SecurityManager sm = new SecurityManager() { 249 250 public void checkPermission(Permission perm) { 251 if (perm.getName().equals("checkExec")) { 252 throw new SecurityException(); 253 } 254 } 255 256 public void checkExec(String cmd) { 257 throw new SecurityException(); 258 } 259 }; 260 261 try { 262 Runtime.getRuntime().exec(new String[]{""}, envp, workFolder); 263 fail("IOException should be thrown."); 264 } catch (IOException e) { 265 //expected 266 } 267 } 268 getEnv()269 String [] getEnv() { 270 Object [] valueSet = System.getenv().values().toArray(); 271 Object [] keySet = System.getenv().keySet().toArray(); 272 String [] envp = new String[valueSet.length]; 273 for(int i = 0; i < envp.length; i++) { 274 envp[i] = keySet[i] + "=" + valueSet[i]; 275 } 276 return envp; 277 } 278 checkExec(int testCase, String [] envp, File file)279 void checkExec(int testCase, String [] envp, File file) { 280 String dirName = "Test_Directory"; 281 String dirParentName = "Parent_Directory"; 282 File resources = Support_Resources.createTempFolder(); 283 String folder = resources.getAbsolutePath() + "/" + dirName; 284 String folderWithParent = resources.getAbsolutePath() + "/" + 285 dirParentName + "/" + dirName; 286 String command = "mkdir " + folder; 287 String [] commandArguments = {"mkdir", folder}; 288 try { 289 Process proc = null; 290 switch(testCase) { 291 case 0: 292 proc = Runtime.getRuntime().exec(command, envp); 293 break; 294 case 1: 295 proc = Runtime.getRuntime().exec(command); 296 break; 297 case 2: 298 proc = Runtime.getRuntime().exec(command, envp, file); 299 break; 300 case 3: 301 proc = Runtime.getRuntime().exec(commandArguments); 302 break; 303 case 4: 304 proc = Runtime.getRuntime().exec(commandArguments, envp); 305 break; 306 case 5: 307 proc = Runtime.getRuntime().exec(commandArguments, envp, file); 308 break; 309 } 310 assertNotNull(proc); 311 try { 312 Thread.sleep(3000); 313 } catch(InterruptedException ie) { 314 fail("InterruptedException was thrown."); 315 } 316 File f = new File(folder); 317 assertTrue(f.exists()); 318 if(f.exists()) { 319 f.delete(); 320 } 321 } catch(IOException io) { 322 fail("IOException was thrown."); 323 } 324 } 325 test_execLjava_lang_String()326 public void test_execLjava_lang_String() { 327 checkExec(1, null, null); 328 329 try { 330 Runtime.getRuntime().exec((String) null); 331 fail("NullPointerException was not thrown."); 332 } catch(NullPointerException npe) { 333 //expected 334 } catch (IOException e) { 335 fail("IOException was thrown."); 336 } 337 338 try { 339 Runtime.getRuntime().exec(""); 340 fail("IllegalArgumentException was not thrown."); 341 } catch(IllegalArgumentException iae) { 342 //expected 343 } catch (IOException e) { 344 fail("IOException was thrown."); 345 } 346 } 347 test_execLjava_lang_StringArray()348 public void test_execLjava_lang_StringArray() { 349 350 checkExec(3, null, null); 351 352 try { 353 Runtime.getRuntime().exec((String[]) null); 354 fail("NullPointerException was not thrown."); 355 } catch(NullPointerException npe) { 356 //expected 357 } catch (IOException e) { 358 fail("IOException was thrown."); 359 } 360 361 try { 362 Runtime.getRuntime().exec(new String[]{"ls", null}); 363 fail("NullPointerException was not thrown."); 364 } catch(NullPointerException npe) { 365 //expected 366 } catch (IOException e) { 367 fail("IOException was thrown."); 368 } 369 370 try { 371 Runtime.getRuntime().exec(new String[]{}); 372 fail("IndexOutOfBoundsException was not thrown."); 373 } catch(IndexOutOfBoundsException iobe) { 374 //expected 375 } catch (IOException e) { 376 fail("IOException was thrown."); 377 } 378 379 try { 380 Runtime.getRuntime().exec(new String[]{""}); 381 fail("IOException should be thrown."); 382 } catch (IOException e) { 383 //expected 384 } 385 } 386 test_runFinalizersOnExit()387 public void test_runFinalizersOnExit() { 388 Runtime.getRuntime().runFinalizersOnExit(true); 389 } 390 test_removeShutdownHookLjava_lang_Thread()391 public void test_removeShutdownHookLjava_lang_Thread() { 392 Thread thr1 = new Thread () { 393 public void run() { 394 try { 395 Runtime.getRuntime().addShutdownHook(this); 396 } catch(IllegalStateException ise) { 397 fail("IllegalStateException shouldn't be thrown."); 398 } 399 } 400 }; 401 402 try { 403 Runtime.getRuntime().addShutdownHook(thr1); 404 Runtime.getRuntime().removeShutdownHook(thr1); 405 } catch (Throwable t) { 406 fail(t.getMessage()); 407 } 408 409 Thread thr2 = new Thread () { 410 public void run() { 411 try { 412 Runtime.getRuntime().removeShutdownHook(this); 413 fail("IllegalStateException wasn't thrown."); 414 } catch(IllegalStateException ise) { 415 //expected 416 } 417 } 418 }; 419 420 try { 421 Runtime.getRuntime().addShutdownHook(thr2); 422 } catch (Throwable t) { 423 fail(t.getMessage()); 424 } 425 426 try { 427 Thread.currentThread().sleep(1000); 428 } catch (InterruptedException ie) { 429 } 430 } 431 test_traceInstructions()432 public void test_traceInstructions() { 433 Runtime.getRuntime().traceInstructions(false); 434 Runtime.getRuntime().traceInstructions(true); 435 Runtime.getRuntime().traceInstructions(false); 436 } 437 test_traceMethodCalls()438 public void test_traceMethodCalls() { 439 try { 440 Runtime.getRuntime().traceMethodCalls(false); 441 Runtime.getRuntime().traceMethodCalls(true); 442 Runtime.getRuntime().traceMethodCalls(false); 443 } catch (RuntimeException ex) { 444 // Slightly ugly: we default to the SD card, which may or may not 445 // be there. So we also accept the error case as a success, since 446 // it means we actually did enable tracing (or tried to). 447 if (!"file open failed".equals(ex.getMessage())) { 448 throw ex; 449 } 450 } 451 } 452 453 @SuppressWarnings("deprecation") test_getLocalizedInputStream()454 public void test_getLocalizedInputStream() throws Exception { 455 String simpleString = "Heart \u2f3c"; 456 byte[] expected = simpleString.getBytes("UTF-8"); 457 byte[] returned = new byte[expected.length]; 458 459 ByteArrayInputStream bais = new ByteArrayInputStream( 460 simpleString.getBytes("UTF-8")); 461 462 InputStream lcIn = 463 Runtime.getRuntime().getLocalizedInputStream(bais); 464 lcIn.read(returned); 465 466 assertTrue("wrong result for String: " + simpleString, 467 Arrays.equals(expected, returned)); 468 } 469 470 @SuppressWarnings("deprecation") test_getLocalizedOutputStream()471 public void test_getLocalizedOutputStream() throws IOException { 472 String simpleString = "Heart \u2f3c"; 473 byte[] expected = simpleString.getBytes("UTF-8"); 474 475 ByteArrayOutputStream out = new ByteArrayOutputStream(); 476 477 OutputStream lcOut = 478 Runtime.getRuntime().getLocalizedOutputStream(out); 479 lcOut.write(simpleString.getBytes("UTF-8")); 480 lcOut.flush(); 481 lcOut.close(); 482 483 byte[] returned = out.toByteArray(); 484 485 assertTrue("wrong result for String: " + returned.toString() + 486 " expected string: " + expected.toString(), 487 Arrays.equals(expected, returned)); 488 } 489 490 test_load()491 public void test_load() { 492 try { 493 Runtime.getRuntime().load("nonExistentLibrary"); 494 fail("UnsatisfiedLinkError was not thrown."); 495 } catch(UnsatisfiedLinkError ule) { 496 //expected 497 } 498 499 try { 500 Runtime.getRuntime().load(null); 501 fail("NullPointerException was not thrown."); 502 } catch(NullPointerException npe) { 503 //expected 504 } 505 } 506 test_loadLibrary()507 public void test_loadLibrary() { 508 try { 509 Runtime.getRuntime().loadLibrary("nonExistentLibrary"); 510 fail("UnsatisfiedLinkError was not thrown."); 511 } catch(UnsatisfiedLinkError ule) { 512 //expected 513 } 514 515 try { 516 Runtime.getRuntime().loadLibrary(null); 517 fail("NullPointerException was not thrown."); 518 } catch(NullPointerException npe) { 519 //expected 520 } 521 } 522 } 523