From b28740ec8921118af2afa7afd2ccd9bf9409711e Mon Sep 17 00:00:00 2001 From: markchien Date: Mon, 5 Jul 2021 19:08:38 +0800 Subject: [PATCH] Load ncm regexs config to tetherableUsbRegexs if isUsingNcm=true If config_tether_ncm_regexs is configured, load it to tetherableUsbRegexs if ncm is used for TETHERING_USB. Load it to tetherableNcmRegexs if ncm is used for TETHERING_NCM. Bug: 185649441 Test: atest TetheringTests Change-Id: I0c542560bd04e8c0a6a78d632da5a00a34d9a3fa --- .../tethering/TetheringConfiguration.java | 12 ++++- .../tethering/TetheringConfigurationTest.java | 45 +++++++++++++++++++ .../networkstack/tethering/TetheringTest.java | 6 --- 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/Tethering/src/com/android/networkstack/tethering/TetheringConfiguration.java b/Tethering/src/com/android/networkstack/tethering/TetheringConfiguration.java index d2f44d3a95..b6240c4561 100644 --- a/Tethering/src/com/android/networkstack/tethering/TetheringConfiguration.java +++ b/Tethering/src/com/android/networkstack/tethering/TetheringConfiguration.java @@ -170,8 +170,16 @@ public class TetheringConfiguration { mUsbTetheringFunction = getUsbTetheringFunction(res); - tetherableUsbRegexs = getResourceStringArray(res, R.array.config_tether_usb_regexs); - tetherableNcmRegexs = getResourceStringArray(res, R.array.config_tether_ncm_regexs); + final String[] ncmRegexs = getResourceStringArray(res, R.array.config_tether_ncm_regexs); + // If usb tethering use NCM and config_tether_ncm_regexs is not empty, use + // config_tether_ncm_regexs for tetherableUsbRegexs. + if (isUsingNcm() && (ncmRegexs.length != 0)) { + tetherableUsbRegexs = ncmRegexs; + tetherableNcmRegexs = EMPTY_STRING_ARRAY; + } else { + tetherableUsbRegexs = getResourceStringArray(res, R.array.config_tether_usb_regexs); + tetherableNcmRegexs = ncmRegexs; + } // TODO: Evaluate deleting this altogether now that Wi-Fi always passes // us an interface name. Careful consideration needs to be given to // implications for Settings and for provisioning checks. diff --git a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringConfigurationTest.java b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringConfigurationTest.java index 0f940d8ca1..c0c2ab9a9d 100644 --- a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringConfigurationTest.java +++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringConfigurationTest.java @@ -30,6 +30,7 @@ import static com.android.networkstack.tethering.TetheringConfiguration.TETHER_F import static com.android.networkstack.tethering.TetheringConfiguration.TETHER_USB_NCM_FUNCTION; import static com.android.networkstack.tethering.TetheringConfiguration.TETHER_USB_RNDIS_FUNCTION; +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -600,4 +601,48 @@ public class TetheringConfigurationTest { private void setTetherForceUsbFunctions(final int value) { setTetherForceUsbFunctions(Integer.toString(value)); } + + @Test + public void testNcmRegexs() throws Exception { + final String[] rndisRegexs = {"test_rndis\\d"}; + final String[] ncmRegexs = {"test_ncm\\d"}; + final String[] rndisNcmRegexs = {"test_rndis\\d", "test_ncm\\d"}; + + // cfg.isUsingNcm = false. + when(mResources.getInteger(R.integer.config_tether_usb_functions)).thenReturn( + TETHER_USB_RNDIS_FUNCTION); + setUsbAndNcmRegexs(rndisRegexs, ncmRegexs); + assertUsbAndNcmRegexs(rndisRegexs, ncmRegexs); + + setUsbAndNcmRegexs(rndisNcmRegexs, new String[0]); + assertUsbAndNcmRegexs(rndisNcmRegexs, new String[0]); + + // cfg.isUsingNcm = true. + when(mResources.getInteger(R.integer.config_tether_usb_functions)).thenReturn( + TETHER_USB_NCM_FUNCTION); + setUsbAndNcmRegexs(rndisRegexs, ncmRegexs); + assertUsbAndNcmRegexs(ncmRegexs, new String[0]); + + setUsbAndNcmRegexs(rndisNcmRegexs, new String[0]); + assertUsbAndNcmRegexs(rndisNcmRegexs, new String[0]); + + // Check USB regex is not overwritten by the NCM regex after force to use rndis from + // Settings. + setUsbAndNcmRegexs(rndisRegexs, ncmRegexs); + setTetherForceUsbFunctions(TETHER_USB_RNDIS_FUNCTION); + assertUsbAndNcmRegexs(rndisRegexs, ncmRegexs); + } + + private void setUsbAndNcmRegexs(final String[] usbRegexs, final String[] ncmRegexs) { + when(mResources.getStringArray(R.array.config_tether_usb_regexs)).thenReturn(usbRegexs); + when(mResources.getStringArray(R.array.config_tether_ncm_regexs)).thenReturn(ncmRegexs); + } + + private void assertUsbAndNcmRegexs(final String[] usbRegexs, final String[] ncmRegexs) { + final TetheringConfiguration cfg = + new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID); + assertArrayEquals(usbRegexs, cfg.tetherableUsbRegexs); + assertArrayEquals(ncmRegexs, cfg.tetherableNcmRegexs); + } + } diff --git a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java index 5aebca06d4..f999dfa892 100644 --- a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java +++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java @@ -2665,12 +2665,6 @@ public class TetheringTest { forceUsbTetheringUse(TETHER_USB_NCM_FUNCTION); verifyUsbTetheringStopDueToSettingChange(TEST_NCM_IFNAME); - // TODO: move this into setup after allowing configure TEST_NCM_REGEX into - // config_tether_usb_regexs and config_tether_ncm_regexs at the same time. - when(mResources.getStringArray(R.array.config_tether_usb_regexs)) - .thenReturn(new String[] {TEST_RNDIS_REGEX, TEST_NCM_REGEX}); - sendConfigurationChanged(); - // If TETHERING_USB is forced to use ncm function, TETHERING_NCM would no longer be // available. final ResultListener ncmResult = new ResultListener(TETHER_ERROR_SERVICE_UNAVAIL);