From fae988bc1a3ff26b83e8d52a4893364ccd9898d5 Mon Sep 17 00:00:00 2001 From: xshu Date: Tue, 21 May 2019 17:34:56 -0700 Subject: Trigger bugreport for abnormally long connections This is an effort investigate why certain connections take really long to finish. Users running user-debug builds will be shown an notification, which they may tap on to file a bugreport. Added GService flags (that is disabled by default) which may be configured on the server side to enable this feature for a select group of users. Also added a flag to fine tune the threshold at which bugreports get triggered. Bug: 132648941 Test: Unit tests Test: mannually tested with a smaller threshold to verify bugreport is triggering properly. Test: Tested setting Gservices value with adb command "am broadcast -a com.google.gservices.intent.action.GSERVICES_OVERRIDE -e android.wifi.abnormal_connection_duration_ms 300" Change-Id: I2f5f9c9c08874f6f356a081b0a2575dd3851c241 --- .../server/wifi/WifiLastResortWatchdogTest.java | 115 ++++++++++++++++++++- 1 file changed, 113 insertions(+), 2 deletions(-) (limited to 'tests/wifitests/src/com/android/server') diff --git a/tests/wifitests/src/com/android/server/wifi/WifiLastResortWatchdogTest.java b/tests/wifitests/src/com/android/server/wifi/WifiLastResortWatchdogTest.java index 9ae382640..1117339c6 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiLastResortWatchdogTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiLastResortWatchdogTest.java @@ -21,16 +21,24 @@ import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.*; import static org.mockito.MockitoAnnotations.*; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.IntentFilter; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiInfo; import android.net.wifi.WifiSsid; +import android.os.Handler; import android.os.test.TestLooper; import android.util.Pair; import androidx.test.filters.SmallTest; +import com.google.android.gsf.Gservices; + import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.ArgumentMatcher; import org.mockito.Mock; import java.util.ArrayList; @@ -42,6 +50,8 @@ import java.util.List; */ @SmallTest public class WifiLastResortWatchdogTest { + final ArgumentCaptor mBroadcastReceiverCaptor = + ArgumentCaptor.forClass(BroadcastReceiver.class); WifiLastResortWatchdog mLastResortWatchdog; @Mock WifiInjector mWifiInjector; @Mock WifiMetrics mWifiMetrics; @@ -49,6 +59,8 @@ public class WifiLastResortWatchdogTest { @Mock ClientModeImpl mClientModeImpl; @Mock Clock mClock; @Mock WifiInfo mWifiInfo; + @Mock Context mContext; + @Mock GservicesFacade mGservicesFacade; private String[] mSsids = {"\"test1\"", "\"test2\"", "\"test3\"", "\"test4\""}; private String[] mBssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4", "de:ad:ba:b1:e5:55", @@ -61,17 +73,22 @@ public class WifiLastResortWatchdogTest { private boolean[] mHasEverConnected = {false, false, false, false}; private TestLooper mLooper; private static final String TEST_NETWORK_SSID = "\"test_ssid\""; + private static final int DEFAULT_ABNORMAL_CONNECTION_DURATION_MS = 30000; @Before public void setUp() throws Exception { initMocks(this); mLooper = new TestLooper(); when(mWifiInjector.getSelfRecovery()).thenReturn(mSelfRecovery); - mLastResortWatchdog = new WifiLastResortWatchdog(mWifiInjector, mClock, mWifiMetrics, - mClientModeImpl, mLooper.getLooper()); + when(mGservicesFacade.isAbnormalConnectionBugreportEnabled()).thenReturn(true); + when(mGservicesFacade.getAbnormalConnectionDurationMs()).thenReturn( + DEFAULT_ABNORMAL_CONNECTION_DURATION_MS); + mLastResortWatchdog = new WifiLastResortWatchdog(mWifiInjector, mContext, mClock, + mWifiMetrics, mClientModeImpl, mLooper.getLooper(), mGservicesFacade); mLastResortWatchdog.setBugReportProbability(1); when(mClientModeImpl.getWifiInfo()).thenReturn(mWifiInfo); when(mWifiInfo.getSSID()).thenReturn(TEST_NETWORK_SSID); + when(mWifiInjector.getClientModeImplHandler()).thenReturn(mLastResortWatchdog.getHandler()); } private List> createFilteredQnsCandidates(String[] ssids, @@ -2152,4 +2169,98 @@ public class WifiLastResortWatchdogTest { verify(mWifiMetrics, times(1)).incrementNumLastResortWatchdogSuccesses(); } + /** + * Verifies that when a connection takes too long (time difference between + * StaEvent.TYPE_CMD_START_CONNECT and StaEvent.TYPE_NETWORK_CONNECTION_EVENT) a bugreport is + * taken. + */ + @Test + public void testAbnormalConnectionTimeTriggersBugreport() throws Exception { + // first verifies that bugreports are not taken when connection takes less than + // DEFAULT_ABNORMAL_CONNECTION_DURATION_MS + when(mClock.getElapsedSinceBootMillis()).thenReturn(1L); + mLastResortWatchdog.noteStartConnectTime(); + when(mClock.getElapsedSinceBootMillis()).thenReturn( + (long) DEFAULT_ABNORMAL_CONNECTION_DURATION_MS); + Handler handler = mLastResortWatchdog.getHandler(); + handler.sendMessage( + handler.obtainMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, null)); + mLooper.dispatchAll(); + verify(mClientModeImpl, never()).takeBugReport(anyString(), anyString()); + + // Now verify that bugreport is taken + mLastResortWatchdog.noteStartConnectTime(); + when(mClock.getElapsedSinceBootMillis()).thenReturn( + 2L * DEFAULT_ABNORMAL_CONNECTION_DURATION_MS + 1); + handler.sendMessage( + handler.obtainMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, null)); + mLooper.dispatchAll(); + verify(mClientModeImpl).takeBugReport(anyString(), anyString()); + + // Verify additional connections (without more TYPE_CMD_START_CONNECT) don't trigger more + // bugreports. + when(mClock.getElapsedSinceBootMillis()).thenReturn( + 4L * DEFAULT_ABNORMAL_CONNECTION_DURATION_MS); + handler.sendMessage( + handler.obtainMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, null)); + mLooper.dispatchAll(); + verify(mClientModeImpl).takeBugReport(anyString(), anyString()); + } + + /** + * Changes |mAbnormalConnectionDurationMs| to a new value, and then verify that a bugreport is + * taken for a connection that takes longer than the new threshold. + * @throws Exception + */ + @Test + public void testGServicesSetDuration() throws Exception { + final int testDurationMs = 10 * 1000; // 10 seconds + // changes the abnormal connection duration to |testDurationMs|. + when(mGservicesFacade.getAbnormalConnectionDurationMs()).thenReturn(testDurationMs); + verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), + (IntentFilter) argThat(new IntentFilterMatcher())); + mBroadcastReceiverCaptor.getValue().onReceive(mContext, null); + + // verifies that bugreport is taken for connections that take longer than |testDurationMs|. + when(mClock.getElapsedSinceBootMillis()).thenReturn(1L); + mLastResortWatchdog.noteStartConnectTime(); + when(mClock.getElapsedSinceBootMillis()).thenReturn((long) testDurationMs + 2); + Handler handler = mLastResortWatchdog.getHandler(); + handler.sendMessage( + handler.obtainMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, null)); + mLooper.dispatchAll(); + verify(mClientModeImpl).takeBugReport(anyString(), anyString()); + } + + /** + * Verifies that bugreports are not triggered even when conditions are met after the + * |mAbnormalConnectionBugreportEnabled| flag is changed to false. + * @throws Exception + */ + @Test + public void testGServicesFlagDisable() throws Exception { + // changes |mAbnormalConnectionBugreportEnabled| to false. + when(mGservicesFacade.isAbnormalConnectionBugreportEnabled()).thenReturn(false); + verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), + (IntentFilter) argThat(new IntentFilterMatcher())); + mBroadcastReceiverCaptor.getValue().onReceive(mContext, null); + + // verifies that bugreports are not taken. + when(mClock.getElapsedSinceBootMillis()).thenReturn(1L); + mLastResortWatchdog.noteStartConnectTime(); + when(mClock.getElapsedSinceBootMillis()).thenReturn( + (long) DEFAULT_ABNORMAL_CONNECTION_DURATION_MS + 2); + Handler handler = mLastResortWatchdog.getHandler(); + handler.sendMessage( + handler.obtainMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, null)); + mLooper.dispatchAll(); + verify(mClientModeImpl, never()).takeBugReport(anyString(), anyString()); + } + + private class IntentFilterMatcher implements ArgumentMatcher { + @Override + public boolean matches(IntentFilter filter) { + return filter.hasAction(Gservices.CHANGED_ACTION); + } + } } -- cgit v1.2.3