OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.os.RemoteException; | 8 import android.os.RemoteException; |
9 import android.test.InstrumentationTestCase; | 9 import android.test.InstrumentationTestCase; |
10 import android.test.suitebuilder.annotation.MediumTest; | 10 import android.test.suitebuilder.annotation.MediumTest; |
11 | 11 |
12 import org.chromium.base.BaseSwitches; | 12 import org.chromium.base.BaseSwitches; |
13 import org.chromium.base.library_loader.LibraryLoader; | 13 import org.chromium.base.library_loader.LibraryLoader; |
14 import org.chromium.base.library_loader.LibraryProcessType; | 14 import org.chromium.base.library_loader.LibraryProcessType; |
15 import org.chromium.base.test.util.CommandLineFlags; | |
15 import org.chromium.base.test.util.Feature; | 16 import org.chromium.base.test.util.Feature; |
16 import org.chromium.content.browser.test.util.Criteria; | 17 import org.chromium.content.browser.test.util.Criteria; |
17 import org.chromium.content.browser.test.util.CriteriaHelper; | 18 import org.chromium.content.browser.test.util.CriteriaHelper; |
18 | 19 |
19 import java.util.concurrent.Callable; | 20 import java.util.concurrent.Callable; |
20 | 21 |
21 /** | 22 /** |
22 * Instrumentation tests for ChildProcessLauncher. | 23 * Instrumentation tests for ChildProcessLauncher. |
24 * TODO(hanxi): Add tests for assigning {@ChildConnectionAllocator} for differen t package names | |
25 * when render processes can be run in WebAPKs. | |
23 */ | 26 */ |
24 public class ChildProcessLauncherTest extends InstrumentationTestCase { | 27 public class ChildProcessLauncherTest extends InstrumentationTestCase { |
25 // Pseudo command line arguments to instruct the child process to wait until being killed. | 28 // Pseudo command line arguments to instruct the child process to wait until being killed. |
26 // Allowing the process to continue would lead to a crash when attempting to initialize IPC | 29 // Allowing the process to continue would lead to a crash when attempting to initialize IPC |
27 // channels that are not being set up in this test. | 30 // channels that are not being set up in this test. |
28 private static final String[] sProcessWaitArguments = { | 31 private static final String[] sProcessWaitArguments = { |
29 "_", "--" + BaseSwitches.RENDERER_WAIT_FOR_JAVA_DEBUGGER }; | 32 "_", "--" + BaseSwitches.RENDERER_WAIT_FOR_JAVA_DEBUGGER }; |
30 | 33 |
31 /** | 34 /** |
32 * Tests cleanup for a connection that fails to connect in the first place. | 35 * Tests cleanup for a connection that fails to connect in the first place. |
33 */ | 36 */ |
34 @MediumTest | 37 @MediumTest |
35 @Feature({"ProcessManagement"}) | 38 @Feature({"ProcessManagement"}) |
39 @CommandLineFlags.Add(ChildProcessLauncher.SWITCH_NUM_SANDBOXED_SERVICES_FOR _TESTING + "=4") | |
Maria
2016/06/07 17:20:17
Why do we need the new flag here?
Also doesn't th
Xi Han
2016/06/07 18:50:00
This is because ChildProcessLauncher#getNumberOfSe
| |
36 public void testServiceFailedToBind() throws InterruptedException, RemoteExc eption { | 40 public void testServiceFailedToBind() throws InterruptedException, RemoteExc eption { |
37 final Context appContext = getInstrumentation().getTargetContext(); | 41 assertEquals(0, allocatedChromeSandboxedConnectionsCount()); |
38 assertEquals(0, ChildProcessLauncher.allocatedConnectionsCountForTesting (appContext)); | |
39 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting()) ; | 42 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting()) ; |
40 | 43 |
41 // Try to allocate a connection to service class in incorrect package. W e can do that by | 44 // Try to allocate a connection to service class in incorrect package. W e can do that by |
42 // using the instrumentation context (getContext()) instead of the app c ontext | 45 // using the instrumentation context (getContext()) instead of the app c ontext |
43 // (getTargetContext()). | 46 // (getTargetContext()). |
44 Context context = getInstrumentation().getContext(); | 47 Context context = getInstrumentation().getContext(); |
45 ChildProcessLauncher.allocateBoundConnectionForTesting(context); | 48 ChildProcessLauncher.allocateBoundConnectionForTesting( |
49 context, getDefaultChildProcessCreationParams(context.getPackage Name())); | |
46 | 50 |
47 // Verify that the connection is not considered as allocated. | 51 // Verify that the connection is not considered as allocated. |
48 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable <Integer>() { | 52 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable <Integer>() { |
49 @Override | 53 @Override |
50 public Integer call() { | 54 public Integer call() { |
51 return ChildProcessLauncher.allocatedConnectionsCountForTesting( | 55 return allocatedChromeSandboxedConnectionsCount(); |
52 appContext); | |
53 } | 56 } |
54 })); | 57 })); |
55 | 58 |
56 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable <Integer>() { | 59 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable <Integer>() { |
57 @Override | 60 @Override |
58 public Integer call() { | 61 public Integer call() { |
59 return ChildProcessLauncher.connectedServicesCountForTesting(); | 62 return ChildProcessLauncher.connectedServicesCountForTesting(); |
60 } | 63 } |
61 })); | 64 })); |
62 } | 65 } |
63 | 66 |
64 /** | 67 /** |
65 * Tests cleanup for a connection that terminates before setup. | 68 * Tests cleanup for a connection that terminates before setup. |
66 */ | 69 */ |
67 @MediumTest | 70 @MediumTest |
68 @Feature({"ProcessManagement"}) | 71 @Feature({"ProcessManagement"}) |
69 public void testServiceCrashedBeforeSetup() throws InterruptedException, Rem oteException { | 72 public void testServiceCrashedBeforeSetup() throws InterruptedException, Rem oteException { |
70 final Context appContext = getInstrumentation().getTargetContext(); | 73 assertEquals(0, allocatedChromeSandboxedConnectionsCount()); |
71 assertEquals(0, ChildProcessLauncher.allocatedConnectionsCountForTesting (appContext)); | |
72 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting()) ; | 74 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting()) ; |
73 | 75 |
74 // Start and connect to a new service. | 76 // Start and connect to a new service. |
75 final ChildProcessConnectionImpl connection = startConnection(); | 77 final ChildProcessConnectionImpl connection = startConnection(); |
76 assertEquals(1, ChildProcessLauncher.allocatedConnectionsCountForTesting (appContext)); | 78 assertEquals(1, allocatedChromeSandboxedConnectionsCount()); |
77 | 79 |
78 // Verify that the service is not yet set up. | 80 // Verify that the service is not yet set up. |
79 assertEquals(0, connection.getPid()); | 81 assertEquals(0, connection.getPid()); |
80 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting()) ; | 82 assertEquals(0, ChildProcessLauncher.connectedServicesCountForTesting()) ; |
81 | 83 |
82 // Crash the service. | 84 // Crash the service. |
83 assertTrue(connection.crashServiceForTesting()); | 85 assertTrue(connection.crashServiceForTesting()); |
84 | 86 |
85 // Verify that the connection gets cleaned-up. | 87 // Verify that the connection gets cleaned-up. |
86 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable <Integer>() { | 88 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable <Integer>() { |
87 @Override | 89 @Override |
88 public Integer call() { | 90 public Integer call() { |
89 return ChildProcessLauncher.allocatedConnectionsCountForTesting( | 91 return allocatedChromeSandboxedConnectionsCount(); |
90 appContext); | |
91 } | 92 } |
92 })); | 93 })); |
93 | 94 |
94 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable <Integer>() { | 95 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable <Integer>() { |
95 @Override | 96 @Override |
96 public Integer call() { | 97 public Integer call() { |
97 return ChildProcessLauncher.connectedServicesCountForTesting(); | 98 return ChildProcessLauncher.connectedServicesCountForTesting(); |
98 } | 99 } |
99 })); | 100 })); |
100 } | 101 } |
101 | 102 |
102 /** | 103 /** |
103 * Tests cleanup for a connection that terminates after setup. | 104 * Tests cleanup for a connection that terminates after setup. |
104 */ | 105 */ |
105 @MediumTest | 106 @MediumTest |
106 @Feature({"ProcessManagement"}) | 107 @Feature({"ProcessManagement"}) |
107 public void testServiceCrashedAfterSetup() throws InterruptedException, Remo teException { | 108 public void testServiceCrashedAfterSetup() throws InterruptedException, Remo teException { |
108 final Context appContext = getInstrumentation().getTargetContext(); | 109 assertEquals(0, allocatedChromeSandboxedConnectionsCount()); |
109 assertEquals(0, ChildProcessLauncher.allocatedConnectionsCountForTesting (appContext)); | |
110 | 110 |
111 // Start and connect to a new service. | 111 // Start and connect to a new service. |
112 final ChildProcessConnectionImpl connection = startConnection(); | 112 final ChildProcessConnectionImpl connection = startConnection(); |
113 assertEquals(1, ChildProcessLauncher.allocatedConnectionsCountForTesting (appContext)); | 113 assertEquals(1, allocatedChromeSandboxedConnectionsCount()); |
114 | 114 |
115 // Initiate the connection setup. | 115 // Initiate the connection setup. |
116 triggerConnectionSetup(connection); | 116 triggerConnectionSetup(connection); |
117 | 117 |
118 // Verify that the connection completes the setup. | 118 // Verify that the connection completes the setup. |
119 CriteriaHelper.pollInstrumentationThread(Criteria.equals(1, new Callable <Integer>() { | 119 CriteriaHelper.pollInstrumentationThread(Criteria.equals(1, new Callable <Integer>() { |
120 @Override | 120 @Override |
121 public Integer call() { | 121 public Integer call() { |
122 return ChildProcessLauncher.connectedServicesCountForTesting(); | 122 return ChildProcessLauncher.connectedServicesCountForTesting(); |
123 } | 123 } |
124 })); | 124 })); |
125 | 125 |
126 CriteriaHelper.pollInstrumentationThread( | 126 CriteriaHelper.pollInstrumentationThread( |
127 new Criteria("The connection failed to get a pid in setup.") { | 127 new Criteria("The connection failed to get a pid in setup.") { |
128 @Override | 128 @Override |
129 public boolean isSatisfied() { | 129 public boolean isSatisfied() { |
130 return connection.getPid() != 0; | 130 return connection.getPid() != 0; |
131 } | 131 } |
132 }); | 132 }); |
133 | 133 |
134 // Crash the service. | 134 // Crash the service. |
135 assertTrue(connection.crashServiceForTesting()); | 135 assertTrue(connection.crashServiceForTesting()); |
136 | 136 |
137 // Verify that the connection gets cleaned-up. | 137 // Verify that the connection gets cleaned-up. |
138 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable <Integer>() { | 138 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable <Integer>() { |
139 @Override | 139 @Override |
140 public Integer call() { | 140 public Integer call() { |
141 return ChildProcessLauncher.allocatedConnectionsCountForTesting( | 141 return allocatedChromeSandboxedConnectionsCount(); |
142 appContext); | |
143 } | 142 } |
144 })); | 143 })); |
145 | 144 |
146 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable <Integer>() { | 145 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable <Integer>() { |
147 @Override | 146 @Override |
148 public Integer call() { | 147 public Integer call() { |
149 return ChildProcessLauncher.connectedServicesCountForTesting(); | 148 return ChildProcessLauncher.connectedServicesCountForTesting(); |
150 } | 149 } |
151 })); | 150 })); |
152 | 151 |
153 // Verify that the connection pid remains set after termination. | 152 // Verify that the connection pid remains set after termination. |
154 assertTrue(connection.getPid() != 0); | 153 assertTrue(connection.getPid() != 0); |
155 } | 154 } |
156 | 155 |
157 /** | 156 /** |
158 * Tests spawning a pending process from queue. | 157 * Tests spawning a pending process from queue. |
159 */ | 158 */ |
160 @MediumTest | 159 @MediumTest |
161 @Feature({"ProcessManagement"}) | 160 @Feature({"ProcessManagement"}) |
162 public void testPendingSpawnQueue() throws InterruptedException, RemoteExcep tion { | 161 public void testPendingSpawnQueue() throws InterruptedException, RemoteExcep tion { |
163 final Context appContext = getInstrumentation().getTargetContext(); | 162 final Context appContext = getInstrumentation().getTargetContext(); |
164 assertEquals(0, ChildProcessLauncher.allocatedConnectionsCountForTesting (appContext)); | 163 assertEquals(0, allocatedChromeSandboxedConnectionsCount()); |
165 | 164 |
166 // Start and connect to a new service. | 165 // Start and connect to a new service. |
167 final ChildProcessConnectionImpl connection = startConnection(); | 166 final ChildProcessConnectionImpl connection = startConnection(); |
168 assertEquals(1, ChildProcessLauncher.allocatedConnectionsCountForTesting (appContext)); | 167 assertEquals(1, allocatedChromeSandboxedConnectionsCount()); |
169 | 168 |
170 // Queue up a new spawn request. There is no way to kill the pending con nection, leak it | 169 // Queue up a new spawn request. There is no way to kill the pending con nection, leak it |
171 // until the browser restart. | 170 // until the browser restart. |
172 ChildProcessLauncher.enqueuePendingSpawnForTesting(appContext, sProcessW aitArguments); | 171 final String packageName = appContext.getPackageName(); |
173 assertEquals(1, ChildProcessLauncher.pendingSpawnsCountForTesting()); | 172 final boolean inSandbox = true; |
173 ChildProcessLauncher.enqueuePendingSpawnForTesting(appContext, sProcessW aitArguments, | |
174 getDefaultChildProcessCreationParams(packageName), inSandbox); | |
175 assertEquals(1, ChildProcessLauncher.pendingSpawnsCountForTesting(appCon text, packageName, | |
176 inSandbox)); | |
174 | 177 |
175 // Initiate the connection setup. | 178 // Initiate the connection setup. |
176 triggerConnectionSetup(connection); | 179 triggerConnectionSetup(connection); |
177 | 180 |
178 // Verify that the connection completes the setup. | 181 // Verify that the connection completes the setup. |
179 CriteriaHelper.pollInstrumentationThread( | 182 CriteriaHelper.pollInstrumentationThread( |
180 Criteria.equals(1, new Callable<Integer>() { | 183 Criteria.equals(1, new Callable<Integer>() { |
181 @Override | 184 @Override |
182 public Integer call() { | 185 public Integer call() { |
183 return ChildProcessLauncher.connectedServicesCountForTes ting(); | 186 return ChildProcessLauncher.connectedServicesCountForTes ting(); |
184 } | 187 } |
185 })); | 188 })); |
186 | 189 |
187 CriteriaHelper.pollInstrumentationThread( | 190 CriteriaHelper.pollInstrumentationThread( |
188 new Criteria("The connection failed to get a pid in setup.") { | 191 new Criteria("The connection failed to get a pid in setup.") { |
189 @Override | 192 @Override |
190 public boolean isSatisfied() { | 193 public boolean isSatisfied() { |
191 return connection.getPid() != 0; | 194 return connection.getPid() != 0; |
192 } | 195 } |
193 }); | 196 }); |
194 | 197 |
195 // Crash the service. | 198 // Crash the service. |
196 assertTrue(connection.crashServiceForTesting()); | 199 assertTrue(connection.crashServiceForTesting()); |
197 | 200 |
198 // Verify that a new service is started for the pending spawn. | 201 // Verify that a new service is started for the pending spawn. |
199 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable <Integer>() { | 202 CriteriaHelper.pollInstrumentationThread(Criteria.equals(0, new Callable <Integer>() { |
200 @Override | 203 @Override |
201 public Integer call() { | 204 public Integer call() { |
202 return ChildProcessLauncher.pendingSpawnsCountForTesting(); | 205 return ChildProcessLauncher.pendingSpawnsCountForTesting(appCont ext, packageName, |
206 inSandbox); | |
203 } | 207 } |
204 })); | 208 })); |
205 | 209 |
206 CriteriaHelper.pollInstrumentationThread( | 210 CriteriaHelper.pollInstrumentationThread( |
207 Criteria.equals(1, new Callable<Integer>() { | 211 Criteria.equals(1, new Callable<Integer>() { |
208 @Override | 212 @Override |
209 public Integer call() { | 213 public Integer call() { |
210 return ChildProcessLauncher.allocatedConnectionsCountFor Testing( | 214 return allocatedChromeSandboxedConnectionsCount(); |
211 appContext); | |
212 } | 215 } |
213 })); | 216 })); |
214 | 217 |
215 // Verify that the connection completes the setup for the pending spawn. | 218 // Verify that the connection completes the setup for the pending spawn. |
216 CriteriaHelper.pollInstrumentationThread(Criteria.equals(1, new Callable <Integer>() { | 219 CriteriaHelper.pollInstrumentationThread(Criteria.equals(1, new Callable <Integer>() { |
217 @Override | 220 @Override |
218 public Integer call() { | 221 public Integer call() { |
219 return ChildProcessLauncher.connectedServicesCountForTesting(); | 222 return ChildProcessLauncher.connectedServicesCountForTesting(); |
220 } | 223 } |
221 })); | 224 })); |
222 } | 225 } |
223 | 226 |
224 private ChildProcessConnectionImpl startConnection() throws InterruptedExcep tion { | 227 private ChildProcessConnectionImpl startConnection() throws InterruptedExcep tion { |
225 // Allocate a new connection. | 228 // Allocate a new connection. |
226 Context context = getInstrumentation().getTargetContext(); | 229 Context context = getInstrumentation().getTargetContext(); |
227 final ChildProcessConnectionImpl connection = (ChildProcessConnectionImp l) | 230 final ChildProcessConnectionImpl connection = |
228 ChildProcessLauncher.allocateBoundConnectionForTesting(context); | 231 (ChildProcessConnectionImpl) ChildProcessLauncher.allocateBoundC onnectionForTesting( |
232 context, getDefaultChildProcessCreationParams(context.ge tPackageName())); | |
229 | 233 |
230 // Wait for the service to connect. | 234 // Wait for the service to connect. |
231 CriteriaHelper.pollInstrumentationThread( | 235 CriteriaHelper.pollInstrumentationThread( |
232 new Criteria("The connection wasn't established.") { | 236 new Criteria("The connection wasn't established.") { |
233 @Override | 237 @Override |
234 public boolean isSatisfied() { | 238 public boolean isSatisfied() { |
235 return connection.isConnected(); | 239 return connection.isConnected(); |
236 } | 240 } |
237 }); | 241 }); |
238 return connection; | 242 return connection; |
239 } | 243 } |
240 | 244 |
245 /** | |
246 * Returns the number of Chrome's sandboxed connections. | |
247 */ | |
248 private int allocatedChromeSandboxedConnectionsCount() { | |
249 Context context = getInstrumentation().getTargetContext(); | |
250 return ChildProcessLauncher.allocatedSandboxedConnectionsCountForTesting ( | |
251 context, context.getPackageName()); | |
252 } | |
253 | |
254 private ChildProcessCreationParams getDefaultChildProcessCreationParams(Stri ng packageName) { | |
255 return new ChildProcessCreationParams(packageName, 0, | |
256 LibraryProcessType.PROCESS_CHILD); | |
257 } | |
258 | |
241 private void triggerConnectionSetup(ChildProcessConnectionImpl connection) { | 259 private void triggerConnectionSetup(ChildProcessConnectionImpl connection) { |
242 ChildProcessLauncher.triggerConnectionSetup(connection, sProcessWaitArgu ments, 1, | 260 ChildProcessLauncher.triggerConnectionSetup(connection, sProcessWaitArgu ments, 1, |
243 new FileDescriptorInfo[0], ChildProcessLauncher.CALLBACK_FOR_REN DERER_PROCESS, 0); | 261 new FileDescriptorInfo[0], ChildProcessLauncher.CALLBACK_FOR_REN DERER_PROCESS, 0); |
244 } | 262 } |
245 | 263 |
246 @Override | 264 @Override |
247 protected void setUp() throws Exception { | 265 protected void setUp() throws Exception { |
248 super.setUp(); | 266 super.setUp(); |
249 LibraryLoader.get(LibraryProcessType.PROCESS_CHILD) | 267 LibraryLoader.get(LibraryProcessType.PROCESS_CHILD) |
250 .ensureInitialized(getInstrumentation().getTargetContext()); | 268 .ensureInitialized(getInstrumentation().getTargetContext()); |
251 } | 269 } |
252 } | 270 } |
OLD | NEW |