diff options
author | Peter Qiu <zqiu@google.com> | 2016-11-30 11:38:03 -0800 |
---|---|---|
committer | Peter Qiu <zqiu@google.com> | 2016-12-06 09:32:45 -0800 |
commit | f1b7517b04fedc6fd81f34a8cb84ce583b8ea63e (patch) | |
tree | 417a756a884c53e6a21e86497427be71797f0e33 /tests | |
parent | b40ba9e6ef82ac6c82869d1b562701483b8f1fc2 (diff) |
hotspot2: simplify ANQP cache management
This makes the AnqpCache just a simple data cache, all the logic
related to the ANQP query (e.g. pending query, backoff timeout) will
be handled elsewhere (e.g. PasspointManager).
Also simplify the cache entry expiration timeout (entries with ANQP
domain ID vs entries without ANQP domain ID) by using one timeout
for all entries, since this is already factored in the ANQP entry key.
Entries without ANQP domain ID will be keyed per AP (SSID + BSSID)
and entries with ANQP domain ID will be keyed per ESS (either SSID or
HESSID).
TODO: update unit tests once the cleanup for ANQP elements are completed,
so that we can easily construct an ANQPElement objects (without constructing
a raw byte buffer).
Bug: 31348912
Test: frameworks/opt/net/wifi/tests/wifitests/runtests.sh
Change-Id: I6d85f69181755afa7845f9f742c2dfb762c3194c
Diffstat (limited to 'tests')
3 files changed, 203 insertions, 191 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/ANQPDataTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/ANQPDataTest.java new file mode 100644 index 000000000..b4e667b6a --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/ANQPDataTest.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wifi.hotspot2; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +import android.test.suitebuilder.annotation.SmallTest; + +import com.android.server.wifi.Clock; +import com.android.server.wifi.hotspot2.anqp.ANQPElement; +import com.android.server.wifi.hotspot2.anqp.Constants; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; + +import java.util.Map; + +/** + * Unit tests for {@link com.android.server.wifi.hotspot2.ANQPData}. + * + * TODO(b/33000864): add more test once the ANQP elements cleanup are completed, which will + * allow easy construction of ANQP elements for testing. + */ +@SmallTest +public class ANQPDataTest { + @Mock Clock mClock; + + /** + * Sets up test. + */ + @Before + public void setUp() throws Exception { + initMocks(this); + // Returning the initial timestamp. + when(mClock.getElapsedSinceBootMillis()).thenReturn(0L); + } + + /** + * Verify creation of ANQPData with null elements. + * + * @throws Exception + */ + @Test + public void createWithNullElements() throws Exception { + ANQPData data = new ANQPData(mClock, null); + Map<Constants.ANQPElementType, ANQPElement> elements = data.getElements(); + assertTrue(elements.isEmpty()); + } + + /** + * Verify the data expiration behavior. + * + * @throws Exception + */ + @Test + public void verifyExpiration() throws Exception { + ANQPData data = new ANQPData(mClock, null); + assertFalse(data.expired(ANQPData.DATA_LIFETIME_MILLISECONDS - 1)); + assertTrue(data.expired(ANQPData.DATA_LIFETIME_MILLISECONDS)); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/ANQPNetworkKeyTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/ANQPNetworkKeyTest.java new file mode 100644 index 000000000..1586d76a3 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/ANQPNetworkKeyTest.java @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wifi.hotspot2; + +import static org.junit.Assert.assertEquals; + +import android.test.suitebuilder.annotation.SmallTest; + +import org.junit.Test; + +/** + * Unit tests for {@link com.android.server.wifi.hotspot2.ANQPNetworkKey}. + */ +@SmallTest +public class ANQPNetworkKeyTest { + private static final String SSID = "TestSSID"; + private static final long BSSID = 0x123456L; + private static final long HESSID = 0x789012L; + private static final int ANQP_DOMAIN_ID = 1; + + /** + * Verify that building a SSID based key works as expected. + * + * @throws Exception + */ + @Test + public void buildStandardESSKey() throws Exception { + ANQPNetworkKey expectedKey = new ANQPNetworkKey(SSID, 0, 0, ANQP_DOMAIN_ID); + ANQPNetworkKey actualKey = ANQPNetworkKey.buildKey(SSID, BSSID, 0, ANQP_DOMAIN_ID); + assertEquals(expectedKey, actualKey); + } + + /** + * Verify that building a HESSID based key works as expected. + * + * @throws Exception + */ + @Test + public void buildHessidKey() throws Exception { + ANQPNetworkKey expectedKey = new ANQPNetworkKey(null, 0, HESSID, ANQP_DOMAIN_ID); + ANQPNetworkKey actualKey = ANQPNetworkKey.buildKey(SSID, BSSID, HESSID, ANQP_DOMAIN_ID); + assertEquals(expectedKey, actualKey); + } + + /** + * Verify that building a key based on an AP (SSID + BSSID) works as expected. + * + * @throws Exception + */ + @Test + public void buildAPKey() throws Exception { + ANQPNetworkKey expectedKey = new ANQPNetworkKey(SSID, BSSID, 0, 0); + ANQPNetworkKey actualKey = ANQPNetworkKey.buildKey(SSID, BSSID, HESSID, 0); + assertEquals(expectedKey, actualKey); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/AnqpCacheTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/AnqpCacheTest.java index b46c05c36..8c0d982f8 100644 --- a/tests/wifitests/src/com/android/server/wifi/hotspot2/AnqpCacheTest.java +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/AnqpCacheTest.java @@ -16,224 +16,87 @@ package com.android.server.wifi.hotspot2; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; -import android.net.wifi.ScanResult; -import android.net.wifi.WifiSsid; import android.test.suitebuilder.annotation.SmallTest; -import android.util.Log; import com.android.server.wifi.Clock; -import com.android.server.wifi.hotspot2.anqp.ANQPElement; -import com.android.server.wifi.hotspot2.anqp.Constants; import com.android.server.wifi.hotspot2.ANQPData; import com.android.server.wifi.hotspot2.AnqpCache; -import com.android.server.wifi.hotspot2.NetworkDetail; -import com.android.server.wifi.ScanDetail; -import com.android.server.wifi.ScanResults; +import org.junit.Before; import org.junit.Test; import org.mockito.Mock; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - - /** * Unit tests for {@link com.android.server.wifi.hotspot2.AnqpCache}. + * + * TODO(b/33000864): add more test once the ANQP elements cleanup are completed, which will + * allow easy construction of ANQP elements for testing. */ @SmallTest public class AnqpCacheTest { + private static final ANQPNetworkKey ENTRY_KEY = new ANQPNetworkKey("test", 0L, 0L, 1); - private static final String TAG = "AnqpCacheTest"; - - private static class NetworkDescription { - ScanDetail[] mScanDetails; - static int[] sChannels = new int[]{2412, 2437, 2462, 5180, 5220, 5745, 5825}; - static int[] sRSSIs = new int[]{ -50, -80, -60, -80, -55, -90, -75}; - - NetworkDescription(String ssid, String bssidPrefix) { - WifiSsid wifiSsid = WifiSsid.createFromAsciiEncoded(ssid); - mScanDetails = new ScanDetail[sChannels.length]; - for (int i = 0; i < sChannels.length; i++) { - String bssid = String.format("%s:%02x", bssidPrefix, i); - ScanResult.InformationElement[] ie = new ScanResult.InformationElement[1]; - ie[0] = ScanResults.generateSsidIe(ssid); - List<String> anqpLines = new ArrayList<String>(); - NetworkDetail nd = new NetworkDetail(bssid, ie, - new ArrayList<String>(), sChannels[i]); - mScanDetails[i] = new ScanDetail(nd, wifiSsid, - bssid, "", sRSSIs[i], sChannels[i], Long.MAX_VALUE, ie, anqpLines); - } - } - } - - private static final String ATT_SSID = "att_wifi"; - private static final String ATT_BSSID_PREFIX = "aa:44:bb:55:cc"; - private static final String TWC_SSID = "TWCWIFI"; - private static final String TWC_BSSID_PREFIX = "11:aa:22:bb:33"; - - private static ScanDetail[] getAttWifiNetworkDescription() { - NetworkDescription network = new NetworkDescription(ATT_SSID, ATT_BSSID_PREFIX); - return network.mScanDetails; - } - - private static ScanDetail[] getTwcWifiNetworkDescription() { - NetworkDescription network = new NetworkDescription(TWC_SSID, TWC_BSSID_PREFIX); - return network.mScanDetails; - } + @Mock Clock mClock; + AnqpCache mCache; - private static List<Constants.ANQPElementType> buildQueryList() { - List<Constants.ANQPElementType> list = Arrays.asList( - Constants.ANQPElementType.class.getEnumConstants()); - return list; + /** + * Sets up test. + */ + @Before + public void setUp() throws Exception { + initMocks(this); + // Returning the initial timestamp. + when(mClock.getElapsedSinceBootMillis()).thenReturn(0L); + mCache = new AnqpCache(mClock); } - private static Map<Constants.ANQPElementType, ANQPElement> buildAnqpResult() { - Map<Constants.ANQPElementType, ANQPElement> elements = new HashMap<>(); - List<Constants.ANQPElementType> list = Arrays.asList( - Constants.ANQPElementType.class.getEnumConstants()); - for (final Constants.ANQPElementType type : list) { - ANQPElement element = new ANQPElement(type) { - @Override - public Constants.ANQPElementType getID() { - return super.getID(); - } - }; - elements.put(type, element); - } - - return elements; + /** + * Verify expectation for addEntry and getEntry. + * + * @throws Exception + */ + @Test + public void addAndGetEntry() throws Exception { + mCache.addEntry(ENTRY_KEY, null); + ANQPData data = mCache.getEntry(ENTRY_KEY); + assertNotNull(data); + assertTrue(data.getElements().isEmpty()); } - private void advanceTimeAndTrimCache(long howManyMillis) { - mCurrentTimeMillis += howManyMillis; - Log.d(TAG, "Time set to " + mCurrentTimeMillis); - when(mClock.getWallClockMillis()).thenReturn(mCurrentTimeMillis); - mCache.clear(false, true); + /** + * Verify that getting a non-existing entry will return a null. + * + * @throws Exception + */ + @Test + public void getNonExistEntry() throws Exception { + assertNull(mCache.getEntry(ENTRY_KEY)); } - public AnqpCacheTest() {} - - private static final long SECOND_MS = 1000; - private static final long MINUTE_MS = 60 * SECOND_MS; - - @Mock Clock mClock; - long mCurrentTimeMillis = 1000000000; - AnqpCache mCache; - - /** verify that ANQP data is cached per the (rather abstract) spec */ + /** + * Verify the expectation for the sweep function (expired entries will be removed). + * + * @throws Exception + */ @Test - public void basicAddQueryAndExpiry() { - initMocks(this); - - AnqpCache cache = mCache = new AnqpCache(mClock); - advanceTimeAndTrimCache(0); - - List<Constants.ANQPElementType> queryList = buildQueryList(); - - ScanDetail[] attScanDetails = getAttWifiNetworkDescription(); - ScanDetail[] twcScanDetails = getTwcWifiNetworkDescription(); - - /* query att network at time 0 */ - for (ScanDetail scanDetail : attScanDetails) { - cache.initiate(scanDetail.getNetworkDetail(), queryList); - } - - /* verify that no data can be returned */ - for (ScanDetail scanDetail : attScanDetails) { - ANQPData data = cache.getEntry(scanDetail.getNetworkDetail()); - assertNull(data); - } - - /* update ANQP results after 1 min */ - advanceTimeAndTrimCache(1 * MINUTE_MS); - - Map<Constants.ANQPElementType, ANQPElement> anqpResults = buildAnqpResult(); - - for (ScanDetail scanDetail : attScanDetails) { - cache.update(scanDetail.getNetworkDetail(), anqpResults); - } - - /* check ANQP results after another 1 min */ - advanceTimeAndTrimCache(1 * MINUTE_MS); - - for (ScanDetail scanDetail : attScanDetails) { - ANQPData data = cache.getEntry(scanDetail.getNetworkDetail()); - assertNotNull(data); - NetworkDetail nd = data.getNetwork(); - Map<Constants.ANQPElementType, ANQPElement> anqp = data.getANQPElements(); - assertEquals(scanDetail.getBSSIDString(), nd.getBSSIDString()); - assertEquals(anqpResults.size(), anqp.size()); - } - - /* query ANQP results for twcwifi after another 10 min */ - advanceTimeAndTrimCache(10 * MINUTE_MS); - - for (ScanDetail scanDetail : twcScanDetails) { - cache.initiate(scanDetail.getNetworkDetail(), queryList); - } - - /* update ANQP results for twcwifi after another 10 min */ - advanceTimeAndTrimCache(1 * MINUTE_MS); - - for (ScanDetail scanDetail : twcScanDetails) { - cache.update(scanDetail.getNetworkDetail(), anqpResults); - } - - /* check all results after 1 minute */ - advanceTimeAndTrimCache(1 * MINUTE_MS); - - for (ScanDetail scanDetail : attScanDetails) { - ANQPData data = cache.getEntry(scanDetail.getNetworkDetail()); - assertNull(data); - } - - for (ScanDetail scanDetail : twcScanDetails) { - ANQPData data = cache.getEntry(scanDetail.getNetworkDetail()); - assertNotNull(data); - NetworkDetail nd = data.getNetwork(); - Map<Constants.ANQPElementType, ANQPElement> anqp = data.getANQPElements(); - assertEquals(scanDetail.getBSSIDString(), nd.getBSSIDString()); - assertEquals(anqpResults.size(), anqp.size()); - } + public void sweepRemoveExpiredEntry() throws Exception { + mCache.addEntry(ENTRY_KEY, null); + + // Sweep the cache when the entry is not expired. + when(mClock.getElapsedSinceBootMillis()) + .thenReturn(AnqpCache.CACHE_SWEEP_INTERVAL_MILLISECONDS); + mCache.sweep(); + assertNotNull(mCache.getEntry(ENTRY_KEY)); + + // Sweep the cache when the entry is expired. + when(mClock.getElapsedSinceBootMillis()).thenReturn(ANQPData.DATA_LIFETIME_MILLISECONDS); + mCache.sweep(); + assertNull(mCache.getEntry(ENTRY_KEY)); } } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |