• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package libcore.java.lang;
18 
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.util.HashMap;
24 import java.util.Map;
25 import libcore.java.util.AbstractResourceLeakageDetectorTestCase;
26 import static tests.support.Support_Exec.execAndCheckOutput;
27 
28 public class ProcessBuilderTest extends AbstractResourceLeakageDetectorTestCase {
29 
shell()30     private static String shell() {
31         String deviceSh = System.getenv("ANDROID_ROOT") + "/bin/sh";
32         String desktopSh = "/bin/sh";
33         return new File(deviceSh).exists() ? deviceSh : desktopSh;
34     }
35 
assertRedirectErrorStream(boolean doRedirect, String expectedOut, String expectedErr)36     private static void assertRedirectErrorStream(boolean doRedirect,
37             String expectedOut, String expectedErr) throws Exception {
38         ProcessBuilder pb = new ProcessBuilder(shell(), "-c", "echo out; echo err 1>&2");
39         pb.redirectErrorStream(doRedirect);
40         execAndCheckOutput(pb, expectedOut, expectedErr);
41     }
42 
test_redirectErrorStream_true()43     public void test_redirectErrorStream_true() throws Exception {
44         assertRedirectErrorStream(true, "out\nerr\n", "");
45     }
46 
test_redirectErrorStream_false()47     public void test_redirectErrorStream_false() throws Exception {
48         assertRedirectErrorStream(false, "out\n", "err\n");
49     }
50 
testEnvironment()51     public void testEnvironment() throws Exception {
52         ProcessBuilder pb = new ProcessBuilder(shell(), "-c", "echo $A");
53         pb.environment().put("A", "android");
54         execAndCheckOutput(pb, "android\n", "");
55     }
56 
testDestroyClosesEverything()57     public void testDestroyClosesEverything() throws IOException {
58         Process process = new ProcessBuilder(shell(), "-c", "echo out; echo err 1>&2").start();
59         InputStream in = process.getInputStream();
60         InputStream err = process.getErrorStream();
61         OutputStream out = process.getOutputStream();
62         process.destroy();
63 
64         try {
65             in.read();
66             fail();
67         } catch (IOException expected) {
68         }
69         try {
70             err.read();
71             fail();
72         } catch (IOException expected) {
73         }
74         try {
75             /*
76              * We test write+flush because the RI returns a wrapped stream, but
77              * only bothers to close the underlying stream.
78              */
79             out.write(1);
80             out.flush();
81             fail();
82         } catch (IOException expected) {
83         }
84     }
85 
testDestroyDoesNotLeak()86     public void testDestroyDoesNotLeak() throws IOException {
87         Process process = new ProcessBuilder(shell(), "-c", "echo out; echo err 1>&2").start();
88         process.destroy();
89     }
90 
testEnvironmentMapForbidsNulls()91     public void testEnvironmentMapForbidsNulls() throws Exception {
92         ProcessBuilder pb = new ProcessBuilder(shell(), "-c", "echo $A");
93         Map<String, String> environment = pb.environment();
94         Map<String, String> before = new HashMap<String, String>(environment);
95         try {
96             environment.put("A", null);
97             fail();
98         } catch (NullPointerException expected) {
99         }
100         try {
101             environment.put(null, "android");
102             fail();
103         } catch (NullPointerException expected) {
104         }
105         assertEquals(before, environment);
106     }
107 }
108