diff options
author | David Su <dysu@google.com> | 2019-04-05 13:24:56 -0700 |
---|---|---|
committer | David Su <dysu@google.com> | 2019-04-08 23:11:15 +0000 |
commit | e3511011bde47c051c31bce8c0569e097a19c421 (patch) | |
tree | 5bdf136e9a19c7ea543c02c55df9b2c4dab95b3b | |
parent | 2fe92b76c85f9abc3126116b0be8ff337cfbfdb1 (diff) |
Fix hashCode() method for wificond Parcelables
Parcelables in wificond incorrectly used Objects.hash()
directly on primitive array fields. Fixed to use
Arrays.hashCode() instead.
Bug: 130037227
Test: frameworks/opt/net/wifi/tests/wifitests/runtests.sh
Test: frameworks/base/wifi/tests/runtests.sh
Test: frameworks/opt/net/wifi/tests/wifitests/coverage.sh ~/temp/cov
Change-Id: I1a6726e9af2c6b2bb04cdf6c175777ec28a6645e
5 files changed, 115 insertions, 43 deletions
diff --git a/service/java/com/android/server/wifi/wificond/HiddenNetwork.java b/service/java/com/android/server/wifi/wificond/HiddenNetwork.java index 3daf7781b..1a96f2557 100644 --- a/service/java/com/android/server/wifi/wificond/HiddenNetwork.java +++ b/service/java/com/android/server/wifi/wificond/HiddenNetwork.java @@ -19,7 +19,7 @@ package com.android.server.wifi.wificond; import android.os.Parcel; import android.os.Parcelable; -import java.util.Objects; +import java.util.Arrays; /** * HiddenNetwork for wificond @@ -42,13 +42,13 @@ public class HiddenNetwork implements Parcelable { return false; } HiddenNetwork network = (HiddenNetwork) rhs; - return java.util.Arrays.equals(ssid, network.ssid); + return Arrays.equals(ssid, network.ssid); } /** override hash code */ @Override public int hashCode() { - return Objects.hash(ssid); + return Arrays.hashCode(ssid); } /** implement Parcelable interface */ diff --git a/service/java/com/android/server/wifi/wificond/PnoNetwork.java b/service/java/com/android/server/wifi/wificond/PnoNetwork.java index 159e53a47..6bcad06ce 100644 --- a/service/java/com/android/server/wifi/wificond/PnoNetwork.java +++ b/service/java/com/android/server/wifi/wificond/PnoNetwork.java @@ -52,7 +52,10 @@ public class PnoNetwork implements Parcelable { /** override hash code */ @Override public int hashCode() { - return Objects.hash(isHidden, ssid, frequencies); + return Objects.hash( + isHidden, + Arrays.hashCode(ssid), + Arrays.hashCode(frequencies)); } /** implement Parcelable interface */ diff --git a/tests/wifitests/src/com/android/server/wifi/wificond/NativeScanResultTest.java b/tests/wifitests/src/com/android/server/wifi/wificond/NativeScanResultTest.java index 6020794d3..11be8315f 100644 --- a/tests/wifitests/src/com/android/server/wifi/wificond/NativeScanResultTest.java +++ b/tests/wifitests/src/com/android/server/wifi/wificond/NativeScanResultTest.java @@ -56,7 +56,7 @@ public class NativeScanResultTest { * values unchanged. */ @Test - public void canSerializeAndDeserialize() throws Exception { + public void canSerializeAndDeserialize() { NativeScanResult scanResult = new NativeScanResult(); scanResult.ssid = TEST_SSID; scanResult.bssid = TEST_BSSID; @@ -66,9 +66,9 @@ public class NativeScanResultTest { scanResult.tsf = TEST_TSF; scanResult.capability = TEST_CAPABILITY; scanResult.associated = TEST_ASSOCIATED; - scanResult.radioChainInfos = new ArrayList( - Arrays.asList(new RadioChainInfo(RADIO_CHAIN_IDS[0], RADIO_CHAIN_LEVELS[0]), - new RadioChainInfo(RADIO_CHAIN_IDS[1], RADIO_CHAIN_LEVELS[1]))); + scanResult.radioChainInfos = new ArrayList<>(Arrays.asList( + new RadioChainInfo(RADIO_CHAIN_IDS[0], RADIO_CHAIN_LEVELS[0]), + new RadioChainInfo(RADIO_CHAIN_IDS[1], RADIO_CHAIN_LEVELS[1]))); Parcel parcel = Parcel.obtain(); scanResult.writeToParcel(parcel, 0); // Rewind the pointer to the head of the parcel. @@ -81,7 +81,7 @@ public class NativeScanResultTest { assertEquals(scanResult.frequency, scanResultDeserialized.frequency); assertEquals(scanResult.signalMbm, scanResultDeserialized.signalMbm); assertEquals(scanResult.tsf, scanResultDeserialized.tsf); - assertTrue(scanResult.capability.equals(scanResultDeserialized.capability)); + assertEquals(scanResult.capability, scanResultDeserialized.capability); assertEquals(scanResult.associated, scanResultDeserialized.associated); assertTrue(scanResult.radioChainInfos.containsAll(scanResultDeserialized.radioChainInfos)); } diff --git a/tests/wifitests/src/com/android/server/wifi/wificond/PnoSettingsTest.java b/tests/wifitests/src/com/android/server/wifi/wificond/PnoSettingsTest.java index d47fff822..b484665c3 100644 --- a/tests/wifitests/src/com/android/server/wifi/wificond/PnoSettingsTest.java +++ b/tests/wifitests/src/com/android/server/wifi/wificond/PnoSettingsTest.java @@ -17,16 +17,17 @@ package com.android.server.wifi.wificond; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; import android.os.Parcel; import androidx.test.filters.SmallTest; +import org.junit.Before; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; /** * Unit tests for {@link com.android.server.wifi.wificond.PnoSettingsResult}. @@ -43,40 +44,69 @@ public class PnoSettingsTest { private static final int TEST_INTERVAL_MS = 30000; private static final int TEST_MIN_2G_RSSI = -60; private static final int TEST_MIN_5G_RSSI = -65; + private static final int TEST_VALUE = 42; + + private PnoNetwork mPnoNetwork1; + private PnoNetwork mPnoNetwork2; + + @Before + public void setUp() { + mPnoNetwork1 = new PnoNetwork(); + mPnoNetwork1.ssid = TEST_SSID_1; + mPnoNetwork1.isHidden = true; + mPnoNetwork1.frequencies = TEST_FREQUENCIES_1; + + mPnoNetwork2 = new PnoNetwork(); + mPnoNetwork2.ssid = TEST_SSID_2; + mPnoNetwork2.isHidden = false; + mPnoNetwork2.frequencies = TEST_FREQUENCIES_2; + } /** * PnoSettings object can be serialized and deserialized, while keeping the * values unchanged. */ @Test - public void canSerializeAndDeserialize() throws Exception { - + public void canSerializeAndDeserialize() { PnoSettings pnoSettings = new PnoSettings(); - - PnoNetwork pnoNetwork1 = new PnoNetwork(); - pnoNetwork1.ssid = TEST_SSID_1; - pnoNetwork1.isHidden = true; - pnoNetwork1.frequencies = TEST_FREQUENCIES_1; - - PnoNetwork pnoNetwork2 = new PnoNetwork(); - pnoNetwork2.ssid = TEST_SSID_2; - pnoNetwork2.isHidden = false; - pnoNetwork2.frequencies = TEST_FREQUENCIES_2; - - pnoSettings.pnoNetworks = new ArrayList(Arrays.asList(pnoNetwork1, pnoNetwork2)); - pnoSettings.intervalMs = TEST_INTERVAL_MS; pnoSettings.min2gRssi = TEST_MIN_2G_RSSI; pnoSettings.min5gRssi = TEST_MIN_5G_RSSI; + pnoSettings.pnoNetworks = new ArrayList<>(Arrays.asList(mPnoNetwork1, mPnoNetwork2)); Parcel parcel = Parcel.obtain(); pnoSettings.writeToParcel(parcel, 0); // Rewind the pointer to the head of the parcel. parcel.setDataPosition(0); - PnoSettings pnoSettingsDeserialized = - pnoSettings.CREATOR.createFromParcel(parcel); + PnoSettings pnoSettingsDeserialized = PnoSettings.CREATOR.createFromParcel(parcel); - assertNotNull(pnoSettingsDeserialized); assertEquals(pnoSettings, pnoSettingsDeserialized); + assertEquals(pnoSettings.hashCode(), pnoSettingsDeserialized.hashCode()); + } + + /** + * Tests usage of {@link PnoSettings} as a HashMap key type. + */ + @Test + public void testAsHashMapKey() { + PnoSettings pnoSettings1 = new PnoSettings(); + pnoSettings1.intervalMs = TEST_INTERVAL_MS; + pnoSettings1.min2gRssi = TEST_MIN_2G_RSSI; + pnoSettings1.min5gRssi = TEST_MIN_5G_RSSI; + pnoSettings1.pnoNetworks = new ArrayList<>(Arrays.asList(mPnoNetwork1, mPnoNetwork2)); + + PnoSettings pnoSettings2 = new PnoSettings(); + pnoSettings2.intervalMs = TEST_INTERVAL_MS; + pnoSettings2.min2gRssi = TEST_MIN_2G_RSSI; + pnoSettings2.min5gRssi = TEST_MIN_5G_RSSI; + pnoSettings2.pnoNetworks = new ArrayList<>(Arrays.asList(mPnoNetwork1, mPnoNetwork2)); + + assertEquals(pnoSettings1, pnoSettings2); + assertEquals(pnoSettings1.hashCode(), pnoSettings2.hashCode()); + + HashMap<PnoSettings, Integer> map = new HashMap<>(); + map.put(pnoSettings1, TEST_VALUE); + + assertEquals(TEST_VALUE, map.get(pnoSettings2).intValue()); } } diff --git a/tests/wifitests/src/com/android/server/wifi/wificond/SingleScanSettingsTest.java b/tests/wifitests/src/com/android/server/wifi/wificond/SingleScanSettingsTest.java index fc430c734..5b424b768 100644 --- a/tests/wifitests/src/com/android/server/wifi/wificond/SingleScanSettingsTest.java +++ b/tests/wifitests/src/com/android/server/wifi/wificond/SingleScanSettingsTest.java @@ -17,17 +17,18 @@ package com.android.server.wifi.wificond; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; import android.net.wifi.IWifiScannerImpl; import android.os.Parcel; import androidx.test.filters.SmallTest; +import org.junit.Before; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; /** * Unit tests for {@link com.android.server.wifi.wificond.SingleScanSettingsResult}. @@ -41,29 +42,39 @@ public class SingleScanSettingsTest { new byte[] {'A', 'n', 'd', 'r', 'o', 'i', 'd', 'T', 'e', 's', 't'}; private static final int TEST_FREQUENCY_1 = 2456; private static final int TEST_FREQUENCY_2 = 5215; + private static final int TEST_VALUE = 42; + + private ChannelSettings mChannelSettings1; + private ChannelSettings mChannelSettings2; + private HiddenNetwork mHiddenNetwork1; + private HiddenNetwork mHiddenNetwork2; + + @Before + public void setUp() { + mChannelSettings1 = new ChannelSettings(); + mChannelSettings1.frequency = TEST_FREQUENCY_1; + mChannelSettings2 = new ChannelSettings(); + mChannelSettings2.frequency = TEST_FREQUENCY_2; + + mHiddenNetwork1 = new HiddenNetwork(); + mHiddenNetwork1.ssid = TEST_SSID_1; + mHiddenNetwork2 = new HiddenNetwork(); + mHiddenNetwork2.ssid = TEST_SSID_2; + } /** * SingleScanSettings object can be serialized and deserialized, while keeping the * values unchanged. */ @Test - public void canSerializeAndDeserialize() throws Exception { - ChannelSettings channelSettings1 = new ChannelSettings(); - channelSettings1.frequency = TEST_FREQUENCY_1; - ChannelSettings channelSettings2 = new ChannelSettings(); - channelSettings2.frequency = TEST_FREQUENCY_2; - - HiddenNetwork hiddenNetwork1 = new HiddenNetwork(); - hiddenNetwork1.ssid = TEST_SSID_1; - HiddenNetwork hiddenNetwork2 = new HiddenNetwork(); - hiddenNetwork2.ssid = TEST_SSID_2; - + public void canSerializeAndDeserialize() { SingleScanSettings scanSettings = new SingleScanSettings(); scanSettings.scanType = IWifiScannerImpl.SCAN_TYPE_HIGH_ACCURACY; scanSettings.channelSettings = - new ArrayList(Arrays.asList(channelSettings1, channelSettings2)); - scanSettings.hiddenNetworks = new ArrayList(Arrays.asList(hiddenNetwork1, hiddenNetwork2)); + new ArrayList<>(Arrays.asList(mChannelSettings1, mChannelSettings2)); + scanSettings.hiddenNetworks = + new ArrayList<>(Arrays.asList(mHiddenNetwork1, mHiddenNetwork2)); Parcel parcel = Parcel.obtain(); scanSettings.writeToParcel(parcel, 0); @@ -72,7 +83,35 @@ public class SingleScanSettingsTest { SingleScanSettings scanSettingsDeserialized = SingleScanSettings.CREATOR.createFromParcel(parcel); - assertNotNull(scanSettingsDeserialized); assertEquals(scanSettings, scanSettingsDeserialized); + assertEquals(scanSettings.hashCode(), scanSettingsDeserialized.hashCode()); + } + + /** + * Tests usage of {@link SingleScanSettings} as a HashMap key type. + */ + @Test + public void testAsHashMapKey() { + SingleScanSettings scanSettings1 = new SingleScanSettings(); + scanSettings1.scanType = IWifiScannerImpl.SCAN_TYPE_HIGH_ACCURACY; + scanSettings1.channelSettings = + new ArrayList<>(Arrays.asList(mChannelSettings1, mChannelSettings2)); + scanSettings1.hiddenNetworks = + new ArrayList<>(Arrays.asList(mHiddenNetwork1, mHiddenNetwork2)); + + SingleScanSettings scanSettings2 = new SingleScanSettings(); + scanSettings2.scanType = IWifiScannerImpl.SCAN_TYPE_HIGH_ACCURACY; + scanSettings2.channelSettings = + new ArrayList<>(Arrays.asList(mChannelSettings1, mChannelSettings2)); + scanSettings2.hiddenNetworks = + new ArrayList<>(Arrays.asList(mHiddenNetwork1, mHiddenNetwork2)); + + assertEquals(scanSettings1, scanSettings2); + assertEquals(scanSettings1.hashCode(), scanSettings2.hashCode()); + + HashMap<SingleScanSettings, Integer> map = new HashMap<>(); + map.put(scanSettings1, TEST_VALUE); + + assertEquals(TEST_VALUE, map.get(scanSettings2).intValue()); } } |