1 /* 2 * Copyright (C) 2020 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 com.android.cts.useprocess; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.ServiceConnection; 23 import android.os.IBinder; 24 import android.os.Parcel; 25 import android.os.RemoteException; 26 import android.os.SystemClock; 27 28 import androidx.test.InstrumentationRegistry; 29 import androidx.test.runner.AndroidJUnit4; 30 31 import org.junit.Assert; 32 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 /** 37 * Tests that network can / can not be accessed in the appropriate processes. 38 */ 39 @RunWith(AndroidJUnit4.class) 40 public class AccessNetworkTest { getContext()41 private static Context getContext() { 42 return InstrumentationRegistry.getTargetContext(); 43 } 44 45 final class MyConnection implements ServiceConnection { 46 boolean mDone; 47 String mMsg; 48 String mStackTrace; 49 waitForResult()50 public String waitForResult() { 51 long waitUntil = SystemClock.uptimeMillis() + 5000; 52 synchronized (this) { 53 long now = SystemClock.uptimeMillis(); 54 while (!mDone) { 55 if (now >= waitUntil) { 56 return "Timed out"; 57 } 58 try { 59 wait(waitUntil - now); 60 } catch (InterruptedException e) { 61 } 62 } 63 return mMsg; 64 } 65 } 66 getStackTrace()67 public String getStackTrace() { 68 return mStackTrace; 69 } 70 71 @Override onServiceConnected(ComponentName name, IBinder service)72 public void onServiceConnected(ComponentName name, IBinder service) { 73 Parcel cmd = Parcel.obtain(); 74 Parcel reply = Parcel.obtain(); 75 boolean called = false; 76 try { 77 service.transact(BaseNetworkService.TRANSACT_TEST, cmd, reply, 0); 78 called = true; 79 } catch (RemoteException e) { 80 Assert.fail("Remote service died: " + e.toString()); 81 } finally { 82 synchronized (this) { 83 mDone = true; 84 if (called) { 85 mMsg = BaseNetworkService.readResult(reply); 86 mStackTrace = mMsg != null 87 ? BaseNetworkService.readResultCallstack(reply) : null; 88 } else { 89 mMsg = "Transaction failed for some reason"; 90 } 91 notifyAll(); 92 } 93 94 reply.recycle(); 95 cmd.recycle(); 96 } 97 } 98 99 @Override onServiceDisconnected(ComponentName name)100 public void onServiceDisconnected(ComponentName name) { 101 } 102 } 103 104 /** 105 * Test that the main process has network access. 106 */ 107 @Test testMainHasNetwork()108 public void testMainHasNetwork() { 109 doNetworkTest(ServiceWithNetwork1.class); 110 } 111 112 /** 113 * Test that the first with-network process has network access. 114 */ 115 @Test testWithNet2HasNetwork()116 public void testWithNet2HasNetwork() { 117 doNetworkTest(ServiceWithNetwork2.class); 118 } 119 120 /** 121 * Test that the second with-network process has network access. 122 */ 123 @Test testWithNet3HasNetwork()124 public void testWithNet3HasNetwork() { 125 doNetworkTest(ServiceWithNetwork3.class); 126 } 127 128 /** 129 * Test that the first without-network process doesn't have network access. 130 */ 131 @Test testWithoutNet1NoNetwork()132 public void testWithoutNet1NoNetwork() { 133 doNetworkTest(ServiceWithoutNetwork1.class); 134 } 135 136 /** 137 * Test that the first without-network process doesn't have network access. 138 */ 139 @Test testWithoutNet2NoNetwork()140 public void testWithoutNet2NoNetwork() { 141 doNetworkTest(ServiceWithoutNetwork2.class); 142 } 143 doNetworkTest(Class serviceClass)144 private void doNetworkTest(Class serviceClass) { 145 final MyConnection conn = new MyConnection(); 146 final Context context = getContext(); 147 Intent intent = new Intent(context, serviceClass); 148 if (!context.bindService(intent, conn, Context.BIND_AUTO_CREATE)) { 149 Assert.fail("Failed binding to service " + intent.getComponent()); 150 } 151 String result = conn.waitForResult(); 152 if (result != null) { 153 Assert.fail(result + ", stack trace:\n" + conn.getStackTrace()); 154 } 155 } 156 } 157