Do not count defaultOrConfiguredNetwork in #hashCode or #equals

defaultOrConfiguredNetwork should not be counted in @hashCode()
or @equals() because it only reflects the device status at the
IkeSessionParams creation time. The real default network may change
after IkeSessionParams is created.

Also defaultOrConfiguredNetwork is only resolved if caller constructs
IkeSessionParams with a Context or a ConnectivityManager. If we
create two IkeSessionParams with same parameters but one with a
Context, the other without a Context or ConnectivityManager,
counting defaultOrConfiguredNetwork in @hashCode() and #equals()
will make these two objects not equal, which does not make sense.

Bug: 163604823
Test: FrameworksIkeTests(new tests added)
Change-Id: I1e6846e52b45ed5079df411d673e94e1fd8efc80
diff --git a/src/java/android/net/ipsec/ike/IkeSessionParams.java b/src/java/android/net/ipsec/ike/IkeSessionParams.java
index 05dc97c..b2b473c 100644
--- a/src/java/android/net/ipsec/ike/IkeSessionParams.java
+++ b/src/java/android/net/ipsec/ike/IkeSessionParams.java
@@ -228,7 +228,13 @@
 
     // @see #getNetwork for reasons of changing the annotation from @NonNull to @Nullable in Android
     // S and why it is safe.
-    @Nullable private final Network mNetwork;
+    // Do not include mDefaultOrConfiguredNetwork in #hashCode or #equal because when it represents
+    // configured network, it always has the same value as mCallerConfiguredNetwork. When it
+    // represents a default network it can only reflects the device status at the IkeSessionParams
+    // creation time. Since the actually default network may change after IkeSessionParams is
+    // constructed, depending on mDefaultOrConfiguredNetwork in #hashCode and #equal to decide
+    // if this object equals to another object does not make sense.
+    @Nullable private final Network mDefaultOrConfiguredNetwork;
 
     @Nullable private final Network mCallerConfiguredNetwork;
 
@@ -259,7 +265,7 @@
 
     private IkeSessionParams(
             @NonNull String serverHostname,
-            @NonNull Network network,
+            @NonNull Network defaultOrConfiguredNetwork,
             @NonNull Network callerConfiguredNetwork,
             @NonNull IkeSaProposal[] proposals,
             @NonNull IkeIdentification localIdentification,
@@ -276,7 +282,7 @@
             int nattKeepaliveDelaySec,
             boolean isIkeFragmentationSupported) {
         mServerHostname = serverHostname;
-        mNetwork = network;
+        mDefaultOrConfiguredNetwork = defaultOrConfiguredNetwork;
         mCallerConfiguredNetwork = callerConfiguredNetwork;
 
         mSaProposals = proposals;
@@ -465,7 +471,7 @@
     @NonNull
     // TODO: b/163604823 Make it @Nullable
     public Network getNetwork() {
-        return mNetwork;
+        return mDefaultOrConfiguredNetwork;
     }
 
     /** Retrieves all IkeSaProposals configured */
@@ -598,7 +604,6 @@
         return Objects.hash(
                 mServerHostname,
                 mCallerConfiguredNetwork,
-                mNetwork,
                 Arrays.hashCode(mSaProposals),
                 mLocalIdentification,
                 mRemoteIdentification,
@@ -625,7 +630,6 @@
 
         return mServerHostname.equals(other.mServerHostname)
                 && Objects.equals(mCallerConfiguredNetwork, other.mCallerConfiguredNetwork)
-                && Objects.equals(mNetwork, other.mNetwork)
                 && Arrays.equals(mSaProposals, other.mSaProposals)
                 && mLocalIdentification.equals(other.mLocalIdentification)
                 && mRemoteIdentification.equals(other.mRemoteIdentification)
diff --git a/tests/iketests/src/java/android/net/ipsec/ike/IkeSessionParamsTest.java b/tests/iketests/src/java/android/net/ipsec/ike/IkeSessionParamsTest.java
index 01dc9db..00fc7fd 100644
--- a/tests/iketests/src/java/android/net/ipsec/ike/IkeSessionParamsTest.java
+++ b/tests/iketests/src/java/android/net/ipsec/ike/IkeSessionParamsTest.java
@@ -825,4 +825,26 @@
                 new IkeSessionParams.Builder(sessionParams).setConfiguredNetwork(null).build();
         assertNull(result.getConfiguredNetwork());
     }
+
+    @Test
+    public void testCreateWithAndWithoutConnectivityMgr() throws Exception {
+        IkeSessionParams withConnectivityMgr =
+                new IkeSessionParams.Builder(mMockConnectManager)
+                        .setServerHostname(REMOTE_IPV4_HOST_ADDRESS)
+                        .addSaProposal(mIkeSaProposal)
+                        .setLocalIdentification(mLocalIdentification)
+                        .setRemoteIdentification(mRemoteIdentification)
+                        .setAuthPsk(PSK)
+                        .build();
+
+        IkeSessionParams withoutConnectivityMgr =
+                new IkeSessionParams.Builder()
+                        .setServerHostname(REMOTE_IPV4_HOST_ADDRESS)
+                        .addSaProposal(mIkeSaProposal)
+                        .setLocalIdentification(mLocalIdentification)
+                        .setRemoteIdentification(mRemoteIdentification)
+                        .setAuthPsk(PSK)
+                        .build();
+        assertEquals(withConnectivityMgr, withoutConnectivityMgr);
+    }
 }