From 5303b9cb6acc2149b454e8aad58f33d8df88ba78 Mon Sep 17 00:00:00 2001 From: Rebecca Silberstein Date: Sat, 25 Mar 2017 17:02:03 -0700 Subject: WifiApConfigStore: create temp sharing AP config Create a temporary config for use by local only hotspot. These configs are generated by the WifiApConfigStore, but will not be stored. Bug: 36704784 Test: frameworks/opt/net/wifi/tests/wifitests/runtests.sh Change-Id: I3470708a274e97f6e4a752ea1863cfa86bfbf8b6 Merged-In: I3470708a274e97f6e4a752ea1863cfa86bfbf8b6 --- .../com/android/server/wifi/WifiApConfigStore.java | 18 ++++++++++++++++- .../android/server/wifi/WifiApConfigStoreTest.java | 23 +++++++++++++++++----- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/service/java/com/android/server/wifi/WifiApConfigStore.java b/service/java/com/android/server/wifi/WifiApConfigStore.java index 4233f5834..850c5aebe 100644 --- a/service/java/com/android/server/wifi/WifiApConfigStore.java +++ b/service/java/com/android/server/wifi/WifiApConfigStore.java @@ -203,8 +203,24 @@ public class WifiApConfigStore { return config; } - private int getRandomIntForDefaultSsid() { + private static int getRandomIntForDefaultSsid() { Random random = new Random(); return random.nextInt((RAND_SSID_INT_MAX - RAND_SSID_INT_MIN) + 1) + RAND_SSID_INT_MIN; } + + /** + * Generate a temporary WPA2 based configuration for use by the local only hotspot. + * This config is not persisted and will not be stored by the WifiApConfigStore. + */ + public static WifiConfiguration generateLocalOnlyHotspotConfig(Context context) { + WifiConfiguration config = new WifiConfiguration(); + config.SSID = context.getResources().getString( + R.string.wifi_localhotspot_configure_ssid_default) + "_" + + getRandomIntForDefaultSsid(); + config.allowedKeyManagement.set(KeyMgmt.WPA2_PSK); + String randomUUID = UUID.randomUUID().toString(); + // first 12 chars from xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx + config.preSharedKey = randomUUID.substring(0, 8) + randomUUID.substring(9, 13); + return config; + } } diff --git a/tests/wifitests/src/com/android/server/wifi/WifiApConfigStoreTest.java b/tests/wifitests/src/com/android/server/wifi/WifiApConfigStoreTest.java index 42161fa54..d4a3ff549 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiApConfigStoreTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiApConfigStoreTest.java @@ -49,6 +49,7 @@ public class WifiApConfigStoreTest { private static final String TEST_DEFAULT_2G_CHANNEL_LIST = "1,2,3,4,5,6"; private static final String TEST_DEFAULT_AP_SSID = "TestAP"; private static final String TEST_CONFIGURED_AP_SSID = "ConfiguredAP"; + private static final String TEST_DEFAULT_HOTSPOT_SSID = "TestShare"; private static final int RAND_SSID_INT_MIN = 1000; private static final int RAND_SSID_INT_MAX = 9999; @@ -69,6 +70,8 @@ public class WifiApConfigStoreTest { TEST_DEFAULT_2G_CHANNEL_LIST); resources.setString(R.string.wifi_tether_configure_ssid_default, TEST_DEFAULT_AP_SSID); + resources.setString(R.string.wifi_localhotspot_configure_ssid_default, + TEST_DEFAULT_HOTSPOT_SSID); when(mContext.getResources()).thenReturn(resources); } @@ -107,10 +110,10 @@ public class WifiApConfigStoreTest { assertEquals(config1.apChannel, config2.apChannel); } - private void verifyDefaultApConfig(WifiConfiguration config) { + private void verifyDefaultApConfig(WifiConfiguration config, String expectedSsid) { String[] splitSsid = config.SSID.split("_"); assertEquals(2, splitSsid.length); - assertEquals(TEST_DEFAULT_AP_SSID, splitSsid[0]); + assertEquals(expectedSsid, splitSsid[0]); int randomPortion = Integer.parseInt(splitSsid[1]); assertTrue(randomPortion >= RAND_SSID_INT_MIN && randomPortion <= RAND_SSID_INT_MAX); assertTrue(config.allowedKeyManagement.get(KeyMgmt.WPA2_PSK)); @@ -124,7 +127,7 @@ public class WifiApConfigStoreTest { public void initWithDefaultConfiguration() throws Exception { WifiApConfigStore store = new WifiApConfigStore( mContext, mBackupManagerProxy, mApConfigFile.getPath()); - verifyDefaultApConfig(store.getApConfiguration()); + verifyDefaultApConfig(store.getApConfiguration(), TEST_DEFAULT_AP_SSID); } /** @@ -165,7 +168,7 @@ public class WifiApConfigStoreTest { verifyApConfig(expectedConfig, store.getApConfiguration()); store.setApConfiguration(null); - verifyDefaultApConfig(store.getApConfiguration()); + verifyDefaultApConfig(store.getApConfiguration(), TEST_DEFAULT_AP_SSID); verify(mBackupManagerProxy).notifyDataChanged(); } @@ -177,7 +180,7 @@ public class WifiApConfigStoreTest { /* Initialize WifiApConfigStore with default configuration. */ WifiApConfigStore store = new WifiApConfigStore( mContext, mBackupManagerProxy, mApConfigFile.getPath()); - verifyDefaultApConfig(store.getApConfiguration()); + verifyDefaultApConfig(store.getApConfiguration(), TEST_DEFAULT_AP_SSID); /* Update with a valid configuration. */ WifiConfiguration expectedConfig = setupApConfig( @@ -190,4 +193,14 @@ public class WifiApConfigStoreTest { verifyApConfig(expectedConfig, store.getApConfiguration()); verify(mBackupManagerProxy).notifyDataChanged(); } + + /** + * Verify a proper local only hotspot config is generated when called properly with the valid + * context. + */ + @Test + public void generateLocalOnlyHotspotConfigIsValid() { + WifiConfiguration config = WifiApConfigStore.generateLocalOnlyHotspotConfig(mContext); + verifyDefaultApConfig(config, TEST_DEFAULT_HOTSPOT_SSID); + } } -- cgit v1.2.3