Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 14 | * limitations under the License. |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package com.android.server.am; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 20 | import static org.junit.Assert.assertTrue; |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 21 | |
| 22 | import android.app.ActivityManagerInternal; |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 23 | import android.os.SystemClock; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 24 | |
| 25 | import androidx.test.filters.MediumTest; |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 26 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
Sudheer Shanka | 51ab3ac | 2017-03-07 15:38:01 -0800 | [diff] [blame] | 29 | import org.mockito.Mock; |
| 30 | import org.mockito.MockitoAnnotations; |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Test class for {@link ActivityManagerInternal}. |
| 34 | * |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 35 | * Build/Install/Run: |
Tadashi G. Takaoka | d3eb645 | 2018-11-03 18:21:40 -0700 | [diff] [blame] | 36 | * atest FrameworksServicesTests:ActivityManagerInternalTest |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 37 | */ |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 38 | public class ActivityManagerInternalTest { |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 39 | private static final int TEST_UID1 = 111; |
| 40 | private static final int TEST_UID2 = 112; |
| 41 | |
| 42 | private static final long TEST_PROC_STATE_SEQ1 = 1111; |
| 43 | private static final long TEST_PROC_STATE_SEQ2 = 1112; |
| 44 | private static final long TEST_PROC_STATE_SEQ3 = 1113; |
| 45 | |
Sudheer Shanka | 51ab3ac | 2017-03-07 15:38:01 -0800 | [diff] [blame] | 46 | @Mock private ActivityManagerService.Injector mMockInjector; |
| 47 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 48 | private ActivityManagerService mAms; |
| 49 | private ActivityManagerInternal mAmi; |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 50 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 51 | @Before |
| 52 | public void setUp() { |
Sudheer Shanka | 51ab3ac | 2017-03-07 15:38:01 -0800 | [diff] [blame] | 53 | MockitoAnnotations.initMocks(this); |
| 54 | |
| 55 | mAms = new ActivityManagerService(mMockInjector); |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 56 | mAmi = mAms.new LocalService(); |
| 57 | } |
| 58 | |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 59 | @MediumTest |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 60 | @Test |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 61 | public void testNotifyNetworkPolicyRulesUpdated() throws Exception { |
| 62 | // Check there is no crash when there are no active uid records. |
| 63 | mAmi.notifyNetworkPolicyRulesUpdated(TEST_UID1, TEST_PROC_STATE_SEQ1); |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 64 | |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 65 | // Notify that network policy rules are updated for TEST_UID1 and verify that |
| 66 | // UidRecord.lastNetworkUpdateProcStateSeq is updated and any blocked threads are notified. |
| 67 | verifyNetworkUpdatedProcStateSeq( |
| 68 | TEST_PROC_STATE_SEQ2, // curProcStateSeq |
| 69 | TEST_PROC_STATE_SEQ1, // lastNetworkUpdateProcStateSeq |
| 70 | TEST_PROC_STATE_SEQ2, // procStateSeq to notify |
| 71 | true); // expectNotify |
| 72 | |
| 73 | // Notify that network policy rules are updated for TEST_UID1 with already handled |
| 74 | // procStateSeq and verify that there is no notify call. |
| 75 | verifyNetworkUpdatedProcStateSeq( |
| 76 | TEST_PROC_STATE_SEQ1, // curProcStateSeq |
| 77 | TEST_PROC_STATE_SEQ1, // lastNetworkUpdateProcStateSeq |
| 78 | TEST_PROC_STATE_SEQ1, // procStateSeq to notify |
| 79 | false); // expectNotify |
| 80 | |
| 81 | // Notify that network policy rules are updated for TEST_UID1 with procStateSeq older |
| 82 | // than it's UidRecord.curProcStateSeq and verify that there is no notify call. |
| 83 | verifyNetworkUpdatedProcStateSeq( |
| 84 | TEST_PROC_STATE_SEQ3, // curProcStateSeq |
| 85 | TEST_PROC_STATE_SEQ1, // lastNetworkUpdateProcStateSeq |
| 86 | TEST_PROC_STATE_SEQ2, // procStateSeq to notify |
| 87 | false); // expectNotify |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 90 | private void verifyNetworkUpdatedProcStateSeq(long curProcStateSeq, |
| 91 | long lastNetworkUpdatedProcStateSeq, long expectedProcStateSeq, boolean expectNotify) |
| 92 | throws Exception { |
| 93 | final UidRecord record1 = addActiveUidRecord(TEST_UID1, curProcStateSeq, |
| 94 | lastNetworkUpdatedProcStateSeq); |
| 95 | final UidRecord record2 = addActiveUidRecord(TEST_UID2, curProcStateSeq, |
| 96 | lastNetworkUpdatedProcStateSeq); |
| 97 | |
Sudheer Shanka | f4923ea | 2017-03-27 15:36:35 -0700 | [diff] [blame] | 98 | final CustomThread thread1 = new CustomThread(record1.networkStateLock); |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 99 | thread1.startAndWait("Unexpected state for " + record1); |
Sudheer Shanka | f4923ea | 2017-03-27 15:36:35 -0700 | [diff] [blame] | 100 | final CustomThread thread2 = new CustomThread(record2.networkStateLock); |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 101 | thread2.startAndWait("Unexpected state for " + record2); |
| 102 | |
| 103 | mAmi.notifyNetworkPolicyRulesUpdated(TEST_UID1, expectedProcStateSeq); |
| 104 | assertEquals(record1 + " should be updated", |
| 105 | expectedProcStateSeq, record1.lastNetworkUpdatedProcStateSeq); |
| 106 | assertEquals(record2 + " should not be updated", |
| 107 | lastNetworkUpdatedProcStateSeq, record2.lastNetworkUpdatedProcStateSeq); |
| 108 | |
| 109 | if (expectNotify) { |
| 110 | thread1.assertTerminated("Unexpected state for " + record1); |
| 111 | assertTrue("Threads waiting for network should be notified: " + record1, |
| 112 | thread1.mNotified); |
| 113 | } else { |
| 114 | thread1.assertWaiting("Unexpected state for " + record1); |
| 115 | thread1.interrupt(); |
| 116 | } |
| 117 | thread2.assertWaiting("Unexpected state for " + record2); |
| 118 | thread2.interrupt(); |
| 119 | |
Amith Yamasani | aa74644 | 2019-01-10 10:09:12 -0800 | [diff] [blame] | 120 | mAms.mProcessList.mActiveUids.clear(); |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | private UidRecord addActiveUidRecord(int uid, long curProcStateSeq, |
| 124 | long lastNetworkUpdatedProcStateSeq) { |
Riddle Hsu | d7088f8 | 2019-01-30 13:04:50 +0800 | [diff] [blame] | 125 | final UidRecord record = new UidRecord(uid); |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 126 | record.lastNetworkUpdatedProcStateSeq = lastNetworkUpdatedProcStateSeq; |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 127 | record.curProcStateSeq = curProcStateSeq; |
| 128 | record.waitingForNetwork = true; |
Amith Yamasani | aa74644 | 2019-01-10 10:09:12 -0800 | [diff] [blame] | 129 | mAms.mProcessList.mActiveUids.put(uid, record); |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 130 | return record; |
| 131 | } |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 132 | |
| 133 | static class CustomThread extends Thread { |
| 134 | private static final long WAIT_TIMEOUT_MS = 1000; |
| 135 | private static final long WAIT_INTERVAL_MS = 100; |
| 136 | |
| 137 | private final Object mLock; |
| 138 | private Runnable mRunnable; |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 139 | public boolean mNotified; |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 140 | |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 141 | CustomThread(Object lock) { |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 142 | mLock = lock; |
| 143 | } |
| 144 | |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 145 | CustomThread(Object lock, Runnable runnable) { |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 146 | super(runnable); |
| 147 | mLock = lock; |
| 148 | mRunnable = runnable; |
| 149 | } |
| 150 | |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 151 | @SuppressWarnings("WaitNotInLoop") |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 152 | @Override |
| 153 | public void run() { |
| 154 | if (mRunnable != null) { |
| 155 | mRunnable.run(); |
| 156 | } else { |
| 157 | synchronized (mLock) { |
| 158 | try { |
| 159 | mLock.wait(); |
| 160 | } catch (InterruptedException e) { |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 161 | Thread.interrupted(); |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | } |
| 165 | mNotified = !Thread.interrupted(); |
| 166 | } |
| 167 | |
| 168 | public void startAndWait(String errMsg) throws Exception { |
| 169 | startAndWait(errMsg, false); |
| 170 | } |
| 171 | |
| 172 | public void startAndWait(String errMsg, boolean timedWaiting) throws Exception { |
| 173 | start(); |
| 174 | final long endTime = SystemClock.elapsedRealtime() + WAIT_TIMEOUT_MS; |
| 175 | final Thread.State stateToReach = timedWaiting |
| 176 | ? Thread.State.TIMED_WAITING : Thread.State.WAITING; |
| 177 | while (getState() != stateToReach |
| 178 | && SystemClock.elapsedRealtime() < endTime) { |
| 179 | Thread.sleep(WAIT_INTERVAL_MS); |
| 180 | } |
| 181 | if (timedWaiting) { |
| 182 | assertTimedWaiting(errMsg); |
| 183 | } else { |
| 184 | assertWaiting(errMsg); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | public void assertWaiting(String errMsg) { |
| 189 | assertEquals(errMsg, Thread.State.WAITING, getState()); |
| 190 | } |
| 191 | |
| 192 | public void assertTimedWaiting(String errMsg) { |
| 193 | assertEquals(errMsg, Thread.State.TIMED_WAITING, getState()); |
| 194 | } |
| 195 | |
| 196 | public void assertTerminated(String errMsg) throws Exception { |
| 197 | final long endTime = SystemClock.elapsedRealtime() + WAIT_TIMEOUT_MS; |
| 198 | while (getState() != Thread.State.TERMINATED |
| 199 | && SystemClock.elapsedRealtime() < endTime) { |
| 200 | Thread.sleep(WAIT_INTERVAL_MS); |
| 201 | } |
| 202 | assertEquals(errMsg, Thread.State.TERMINATED, getState()); |
| 203 | } |
| 204 | } |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 205 | } |