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 libcore.junit.junit3.TestCaseWithRules;
29 import libcore.junit.util.SwitchTargetSdkVersionRule;
30 import libcore.junit.util.SwitchTargetSdkVersionRule.TargetSdkVersion;
31 import org.junit.Rule;
32 import org.junit.rules.TestRule;
33 import tests.support.resource.Support_Resources;
34 import java.lang.reflect.Method;
35 import java.lang.reflect.InvocationTargetException;
36 
37 public class OldRuntimeTest extends TestCaseWithRules {
38 
39     @Rule
40     public TestRule switchTargetSdkVersionRule = SwitchTargetSdkVersionRule.getInstance();
41 
42     Runtime r = Runtime.getRuntime();
43 
44     InputStream is;
45 
test_getRuntime()46     public void test_getRuntime() {
47         // Test for method java.lang.Runtime java.lang.Runtime.getRuntime()
48         assertNotNull(Runtime.getRuntime());
49     }
50 
test_addShutdownHook()51     public void test_addShutdownHook() {
52         Thread thrException = new Thread () {
53             public void run() {
54                 try {
55                     Runtime.getRuntime().addShutdownHook(this);
56                     fail("IllegalStateException was not thrown.");
57                 } catch(IllegalStateException ise) {
58                     //expected
59                 }
60             }
61         };
62 
63         try {
64             Runtime.getRuntime().addShutdownHook(thrException);
65         } catch (Throwable t) {
66             fail(t.getMessage());
67         }
68 
69         try {
70             Runtime.getRuntime().addShutdownHook(thrException);
71             fail("IllegalArgumentException was not thrown.");
72         } catch(IllegalArgumentException  iae) {
73             // expected
74         }
75 
76         SecurityManager sm = new SecurityManager() {
77 
78             public void checkPermission(Permission perm) {
79                 if (perm.getName().equals("shutdownHooks")) {
80                     throw new SecurityException();
81                 }
82             }
83         };
84 
85         // remove previously added hook so we're not depending on the priority
86         // of the Exceptions to be thrown.
87         Runtime.getRuntime().removeShutdownHook(thrException);
88 
89         try {
90             Thread.currentThread().sleep(1000);
91         } catch (InterruptedException ie) {
92         }
93     }
94 
test_availableProcessors()95     public void test_availableProcessors() {
96         assertTrue(Runtime.getRuntime().availableProcessors() > 0);
97     }
98 
99 
test_execLjava_lang_StringLjava_lang_StringArray()100     public void test_execLjava_lang_StringLjava_lang_StringArray() {
101         String [] envp =  getEnv();
102 
103         checkExec(0, envp, null);
104         checkExec(0, null, null);
105 
106         try {
107             Runtime.getRuntime().exec((String)null, null);
108             fail("NullPointerException should be thrown.");
109         } catch(IOException ioe) {
110             fail("IOException was thrown.");
111         } catch(NullPointerException npe) {
112             //expected
113         }
114 
115         SecurityManager sm = new SecurityManager() {
116 
117             public void checkPermission(Permission perm) {
118                 if (perm.getName().equals("checkExec")) {
119                     throw new SecurityException();
120                 }
121             }
122 
123             public void checkExec(String cmd) {
124                 throw new SecurityException();
125             }
126         };
127 
128         try {
129             Runtime.getRuntime().exec("", envp);
130             fail("IllegalArgumentException should be thrown.");
131         } catch(IllegalArgumentException iae) {
132             //expected
133         } catch (IOException e) {
134             fail("IOException was thrown.");
135         }
136     }
137 
test_execLjava_lang_StringArrayLjava_lang_StringArray()138     public void test_execLjava_lang_StringArrayLjava_lang_StringArray() {
139         String [] envp =  getEnv();
140 
141         checkExec(4, envp, null);
142         checkExec(4, null, null);
143 
144         try {
145             Runtime.getRuntime().exec((String[])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         try {
154             Runtime.getRuntime().exec(new String[]{"ls", null}, null);
155             fail("NullPointerException should be thrown.");
156         } catch(IOException ioe) {
157             fail("IOException was thrown.");
158         } catch(NullPointerException npe) {
159             //expected
160         }
161 
162         SecurityManager sm = new SecurityManager() {
163 
164             public void checkPermission(Permission perm) {
165                 if (perm.getName().equals("checkExec")) {
166                     throw new SecurityException();
167                 }
168             }
169 
170             public void checkExec(String cmd) {
171                 throw new SecurityException();
172             }
173         };
174 
175         try {
176             Runtime.getRuntime().exec(new String[]{}, envp);
177             fail("IndexOutOfBoundsException should be thrown.");
178         } catch(IndexOutOfBoundsException ioob) {
179             //expected
180         } catch (IOException e) {
181             fail("IOException was thrown.");
182         }
183 
184         try {
185             Runtime.getRuntime().exec(new String[]{""}, envp);
186             fail("IOException should be thrown.");
187         } catch (IOException e) { /* expected */ }
188     }
189 
test_execLjava_lang_StringLjava_lang_StringArrayLjava_io_File()190     public void test_execLjava_lang_StringLjava_lang_StringArrayLjava_io_File() {
191 
192         String [] envp =  getEnv();
193 
194         File workFolder = Support_Resources.createTempFolder();
195 
196         checkExec(2, envp, workFolder);
197         checkExec(2, null, null);
198 
199         try {
200             Runtime.getRuntime().exec((String)null, null, workFolder);
201             fail("NullPointerException should be thrown.");
202         } catch(IOException ioe) {
203             fail("IOException was thrown.");
204         } catch(NullPointerException npe) {
205             //expected
206         }
207 
208         SecurityManager sm = new SecurityManager() {
209 
210             public void checkPermission(Permission perm) {
211                 if (perm.getName().equals("checkExec")) {
212                     throw new SecurityException();
213                 }
214             }
215 
216             public void checkExec(String cmd) {
217                 throw new SecurityException();
218             }
219         };
220 
221         try {
222             Runtime.getRuntime().exec("",  envp, workFolder);
223             fail("SecurityException should be thrown.");
224         } catch(IllegalArgumentException iae) {
225             //expected
226         } catch (IOException e) {
227             fail("IOException was thrown.");
228         }
229     }
230 
test_execLjava_lang_StringArrayLjava_lang_StringArrayLjava_io_File()231     public void test_execLjava_lang_StringArrayLjava_lang_StringArrayLjava_io_File() {
232         String [] envp =  getEnv();
233 
234         File workFolder = Support_Resources.createTempFolder();
235 
236         checkExec(5, envp, workFolder);
237         checkExec(5, null, null);
238 
239         try {
240             Runtime.getRuntime().exec((String[])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         try {
249             Runtime.getRuntime().exec(new String[]{"ls", null}, null, workFolder);
250             fail("NullPointerException should be thrown.");
251         } catch(IOException ioe) {
252             fail("IOException was thrown.");
253         } catch(NullPointerException npe) {
254             //expected
255         }
256 
257         SecurityManager sm = new SecurityManager() {
258 
259             public void checkPermission(Permission perm) {
260                 if (perm.getName().equals("checkExec")) {
261                     throw new SecurityException();
262                 }
263             }
264 
265             public void checkExec(String cmd) {
266                 throw new SecurityException();
267             }
268         };
269 
270         try {
271             Runtime.getRuntime().exec(new String[]{""}, envp, workFolder);
272             fail("IOException should be thrown.");
273         } catch (IOException e) {
274             //expected
275         }
276     }
277 
getEnv()278     String [] getEnv() {
279         Object [] valueSet = System.getenv().values().toArray();
280         Object [] keySet = System.getenv().keySet().toArray();
281         String [] envp = new String[valueSet.length];
282         for(int i = 0; i < envp.length; i++) {
283             envp[i] = keySet[i] + "=" + valueSet[i];
284         }
285         return envp;
286     }
287 
checkExec(int testCase, String [] envp, File file)288     void checkExec(int testCase, String [] envp, File file) {
289         String dirName = "Test_Directory";
290         String dirParentName = "Parent_Directory";
291         File resources = Support_Resources.createTempFolder();
292         String folder = resources.getAbsolutePath() + "/" + dirName;
293         String folderWithParent = resources.getAbsolutePath() + "/"  +
294                                     dirParentName + "/" + dirName;
295         String command = "mkdir " + folder;
296         String [] commandArguments = {"mkdir", folder};
297         try {
298             Process proc = null;
299             switch(testCase) {
300                 case 0:
301                     proc = Runtime.getRuntime().exec(command, envp);
302                     break;
303                 case 1:
304                     proc = Runtime.getRuntime().exec(command);
305                     break;
306                 case 2:
307                     proc = Runtime.getRuntime().exec(command, envp, file);
308                     break;
309                 case 3:
310                     proc = Runtime.getRuntime().exec(commandArguments);
311                     break;
312                 case 4:
313                     proc = Runtime.getRuntime().exec(commandArguments, envp);
314                     break;
315                 case 5:
316                     proc = Runtime.getRuntime().exec(commandArguments, envp, file);
317                     break;
318             }
319             assertNotNull(proc);
320             try {
321                 Thread.sleep(3000);
322             } catch(InterruptedException ie) {
323                 fail("InterruptedException was thrown.");
324             }
325             File f = new File(folder);
326             assertTrue(f.exists());
327             if(f.exists()) {
328                 f.delete();
329             }
330         } catch(IOException io) {
331             fail("IOException was thrown.");
332         }
333     }
334 
test_execLjava_lang_String()335     public void test_execLjava_lang_String() {
336         checkExec(1, null, null);
337 
338         try {
339             Runtime.getRuntime().exec((String) null);
340             fail("NullPointerException was not thrown.");
341         } catch(NullPointerException npe) {
342             //expected
343         } catch (IOException e) {
344             fail("IOException was thrown.");
345         }
346 
347         try {
348             Runtime.getRuntime().exec("");
349             fail("IllegalArgumentException was not thrown.");
350         } catch(IllegalArgumentException iae) {
351             //expected
352         } catch (IOException e) {
353             fail("IOException was thrown.");
354         }
355     }
356 
test_execLjava_lang_StringArray()357     public void test_execLjava_lang_StringArray() {
358 
359         checkExec(3, null, null);
360 
361         try {
362             Runtime.getRuntime().exec((String[]) 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[]{"ls", null});
372             fail("NullPointerException was not thrown.");
373         } catch(NullPointerException npe) {
374             //expected
375         } catch (IOException e) {
376             fail("IOException was thrown.");
377         }
378 
379         try {
380             Runtime.getRuntime().exec(new String[]{});
381             fail("IndexOutOfBoundsException was not thrown.");
382         } catch(IndexOutOfBoundsException iobe) {
383             //expected
384         } catch (IOException e) {
385             fail("IOException was thrown.");
386         }
387 
388         try {
389             Runtime.getRuntime().exec(new String[]{""});
390             fail("IOException should be thrown.");
391         } catch (IOException e) {
392             //expected
393         }
394     }
395 
test_runFinalizersOnExit()396     public void test_runFinalizersOnExit() {
397         Runtime.getRuntime().runFinalizersOnExit(true);
398     }
399 
test_removeShutdownHookLjava_lang_Thread()400     public void test_removeShutdownHookLjava_lang_Thread() {
401         Thread thr1 = new Thread () {
402             public void run() {
403                 try {
404                     Runtime.getRuntime().addShutdownHook(this);
405                 } catch(IllegalStateException ise) {
406                     fail("IllegalStateException shouldn't be thrown.");
407                 }
408             }
409         };
410 
411         try {
412             Runtime.getRuntime().addShutdownHook(thr1);
413             Runtime.getRuntime().removeShutdownHook(thr1);
414         } catch (Throwable t) {
415             fail(t.getMessage());
416         }
417 
418         Thread thr2 = new Thread () {
419             public void run() {
420                 try {
421                     Runtime.getRuntime().removeShutdownHook(this);
422                     fail("IllegalStateException wasn't thrown.");
423                 } catch(IllegalStateException ise) {
424                     //expected
425                 }
426             }
427         };
428 
429         try {
430             Runtime.getRuntime().addShutdownHook(thr2);
431         } catch (Throwable t) {
432             fail(t.getMessage());
433         }
434 
435         try {
436             Thread.currentThread().sleep(1000);
437         } catch (InterruptedException ie) {
438         }
439     }
440 
test_traceInstructions()441     public void test_traceInstructions() {
442         Runtime.getRuntime().traceInstructions(false);
443         Runtime.getRuntime().traceInstructions(true);
444         Runtime.getRuntime().traceInstructions(false);
445     }
446 
test_traceMethodCalls()447     public void test_traceMethodCalls() {
448         Runtime.getRuntime().traceMethodCalls(false);
449         try {
450             Runtime.getRuntime().traceMethodCalls(true);
451             fail();
452         } catch (UnsupportedOperationException expected) {}
453     }
454 
455     @SuppressWarnings("deprecation")
test_getLocalizedInputStream()456     public void test_getLocalizedInputStream() throws Exception {
457         String simpleString = "Heart \u2f3c";
458         byte[] expected = simpleString.getBytes("UTF-8");
459         byte[] returned = new byte[expected.length];
460 
461         ByteArrayInputStream bais = new ByteArrayInputStream(
462                 simpleString.getBytes("UTF-8"));
463 
464         InputStream lcIn =
465                 Runtime.getRuntime().getLocalizedInputStream(bais);
466         lcIn.read(returned);
467 
468         assertTrue("wrong result for String: " + simpleString,
469                 Arrays.equals(expected, returned));
470     }
471 
472     @SuppressWarnings("deprecation")
test_getLocalizedOutputStream()473     public void test_getLocalizedOutputStream() throws IOException {
474         String simpleString = "Heart \u2f3c";
475         byte[] expected = simpleString.getBytes("UTF-8");
476 
477         ByteArrayOutputStream out = new ByteArrayOutputStream();
478 
479         OutputStream lcOut =
480                 Runtime.getRuntime().getLocalizedOutputStream(out);
481         lcOut.write(simpleString.getBytes("UTF-8"));
482         lcOut.flush();
483         lcOut.close();
484 
485         byte[] returned = out.toByteArray();
486 
487         assertTrue("wrong result for String: " + returned.toString() +
488                 " expected string: " + expected.toString(),
489                 Arrays.equals(expected, returned));
490     }
491 
492 
test_load()493     public void test_load() {
494         try {
495             Runtime.getRuntime().load("nonExistentLibrary");
496             fail("UnsatisfiedLinkError was not thrown.");
497         } catch(UnsatisfiedLinkError ule) {
498             //expected
499         }
500 
501         try {
502             Runtime.getRuntime().load(null);
503             fail("NullPointerException was not thrown.");
504         } catch(NullPointerException npe) {
505             //expected
506         }
507     }
508 
test_loadLibrary()509     public void test_loadLibrary() {
510         try {
511             Runtime.getRuntime().loadLibrary("nonExistentLibrary");
512             fail("UnsatisfiedLinkError was not thrown.");
513         } catch(UnsatisfiedLinkError ule) {
514             //expected
515         }
516 
517         try {
518             Runtime.getRuntime().loadLibrary(null);
519             fail("NullPointerException was not thrown.");
520         } catch(NullPointerException npe) {
521             //expected
522         }
523     }
524 
525     // b/25859957
526     @TargetSdkVersion(24)
test_loadDeprecated_targetSdkVersion_24()527     public void test_loadDeprecated_targetSdkVersion_24() throws Exception {
528         try {
529             // Call Runtime#load(String, ClassLoader) at API level 24 (N). It will fail
530             // with an UnsatisfiedLinkError because requested library doesn't exist.
531             Method loadMethod =
532                     Runtime.class.getDeclaredMethod("load", String.class, ClassLoader.class);
533             loadMethod.setAccessible(true);
534             loadMethod.invoke(Runtime.getRuntime(), "nonExistentLibrary", null);
535             fail();
536         } catch(InvocationTargetException expected) {
537             assertTrue(expected.getCause() instanceof UnsatisfiedLinkError);
538         }
539     }
540 
541     // b/25859957
542     @TargetSdkVersion(25)
test_loadDeprecated_targetSdkVersion_25()543     public void test_loadDeprecated_targetSdkVersion_25() throws Exception {
544         try {
545             // Call Runtime#load(String, ClassLoader) at API level 25. It will fail
546             // with an IllegalStateException because it's deprecated.
547             Method loadMethod =
548                     Runtime.class.getDeclaredMethod("load", String.class, ClassLoader.class);
549             loadMethod.setAccessible(true);
550             loadMethod.invoke(Runtime.getRuntime(), "nonExistentLibrary", null);
551             fail();
552         } catch(InvocationTargetException expected) {
553             assertTrue(expected.getCause() instanceof UnsupportedOperationException);
554         }
555     }
556 
557     // b/25859957
558     @TargetSdkVersion(24)
test_loadLibraryDeprecated_targetSdkVersion_24()559     public void test_loadLibraryDeprecated_targetSdkVersion_24() throws Exception {
560         try {
561             // Call Runtime#loadLibrary(String, ClassLoader) at API level 24 (N). It will fail
562             // with a UnsatisfiedLinkError because requested library doesn't exits.
563             Method loadMethod =
564                     Runtime.class.getDeclaredMethod("loadLibrary", String.class, ClassLoader.class);
565             loadMethod.setAccessible(true);
566             loadMethod.invoke(Runtime.getRuntime(), "nonExistentLibrary", null);
567             fail();
568         } catch(InvocationTargetException expected) {
569             assertTrue(expected.getCause() instanceof UnsatisfiedLinkError);
570         }
571     }
572 
573     // b/25859957
574     @TargetSdkVersion(25)
test_loadLibraryDeprecated_targetSdkVersion_25()575     public void test_loadLibraryDeprecated_targetSdkVersion_25() throws Exception {
576         try {
577             // Call Runtime#load(String, ClassLoader) at API level 25. It will fail
578             // with a IllegalStateException because it's deprecated.
579             Method loadMethod =
580                     Runtime.class.getDeclaredMethod("loadLibrary", String.class, ClassLoader.class);
581             loadMethod.setAccessible(true);
582             loadMethod.invoke(Runtime.getRuntime(), "nonExistentLibrary", null);
583             fail();
584         } catch(InvocationTargetException expected) {
585             assertTrue(expected.getCause() instanceof UnsupportedOperationException);
586         }
587     }
588 }
589