Construct a Builder with IkeSessionParams

This CL supports constructing a IkeSessionParams.Builder with
an IkeSessionParams object. It allows callers to create
an IkeSessionParams object copy with modifications.

This CL also allows caller to clear configured network by
passing null to setConfiguredNetwork

Bug: 163604823
Test: FrameworksIkeTests(new tests added), CtsIkeTestCases
Change-Id: Ia256200ae9f7184b63bb3ba71ced6a99274a432e
diff --git a/src/java/android/net/ipsec/ike/IkeSessionParams.java b/src/java/android/net/ipsec/ike/IkeSessionParams.java
index 8880b56..05dc97c 100644
--- a/src/java/android/net/ipsec/ike/IkeSessionParams.java
+++ b/src/java/android/net/ipsec/ike/IkeSessionParams.java
@@ -1204,6 +1204,42 @@
         }
 
         /**
+         * Construct Builder from the {@link IkeSessionParams} object.
+         *
+         * @param ikeSessionParams the object this Builder will be constructed with.
+         * @hide
+         */
+        public Builder(@NonNull IkeSessionParams ikeSessionParams) {
+            mSaProposalList.addAll(ikeSessionParams.getSaProposals());
+            mConfigRequestList.addAll(Arrays.asList(ikeSessionParams.mConfigRequests));
+
+            int[] retransmissionTimeouts = ikeSessionParams.getRetransmissionTimeoutsMillis();
+            mRetransTimeoutMsList =
+                    Arrays.copyOf(retransmissionTimeouts, retransmissionTimeouts.length);
+
+            mServerHostname = ikeSessionParams.getServerHostname();
+            mCallerConfiguredNetwork = ikeSessionParams.getConfiguredNetwork();
+            mLocalIdentification = ikeSessionParams.getLocalIdentification();
+            mRemoteIdentification = ikeSessionParams.getRemoteIdentification();
+            mLocalAuthConfig = ikeSessionParams.getLocalAuthConfig();
+            mRemoteAuthConfig = ikeSessionParams.getRemoteAuthConfig();
+
+            mIke3gppExtension = ikeSessionParams.getIke3gppExtension();
+
+            mHardLifetimeSec = ikeSessionParams.getHardLifetimeSeconds();
+            mSoftLifetimeSec = ikeSessionParams.getSoftLifetimeSeconds();
+            mDpdDelaySec = ikeSessionParams.getDpdDelaySeconds();
+            mNattKeepaliveDelaySec = ikeSessionParams.getNattKeepAliveDelaySeconds();
+
+            mIkeOptions = ikeSessionParams.mIkeOptions;
+
+            if (!ikeSessionParams.mIsIkeFragmentationSupported) {
+                throw new IllegalStateException(
+                        "mIsIkeFragmentationSupported should never be false");
+            }
+        }
+
+        /**
          * Sets the server hostname for the {@link IkeSessionParams} being built.
          *
          * @param serverHostname the hostname of the IKE server, such as "ike.android.com".
@@ -1223,15 +1259,13 @@
          * <p>If no {@link Network} is provided, the default Network (as per {@link
          * ConnectivityManager#getActiveNetwork()}) will be used.
          *
-         * @param network the {@link Network} that IKE Session will use.
+         * @param network the {@link Network} that IKE Session will use, or {@code null} to clear
+         *     the previously set {@link Network}
          * @return Builder this, to facilitate chaining.
          */
+        // TODO(b/163604823): Making @NonNull to @Nullable
         @NonNull
         public Builder setConfiguredNetwork(@NonNull Network network) {
-            if (network == null) {
-                throw new NullPointerException("Required argument not provided");
-            }
-
             mCallerConfiguredNetwork = network;
             return this;
         }
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 ebaa773..01dc9db 100644
--- a/tests/iketests/src/java/android/net/ipsec/ike/IkeSessionParamsTest.java
+++ b/tests/iketests/src/java/android/net/ipsec/ike/IkeSessionParamsTest.java
@@ -260,16 +260,18 @@
         assertFalse(sessionParams.hasIkeOption(IKE_OPTION_ACCEPT_ANY_REMOTE_ID));
     }
 
+    private IkeSessionParams.Builder createIkeParamsBuilderMinimum() {
+        return new IkeSessionParams.Builder()
+                .setServerHostname(REMOTE_IPV4_HOST_ADDRESS)
+                .addSaProposal(mIkeSaProposal)
+                .setLocalIdentification(mLocalIdentification)
+                .setRemoteIdentification(mRemoteIdentification)
+                .setAuthPsk(PSK);
+    }
+
     @Test
     public void testIkeSessionParamsEncodeDecodeIsLossLess() throws Exception {
-        IkeSessionParams sessionParams =
-                new IkeSessionParams.Builder()
-                        .setServerHostname(REMOTE_IPV4_HOST_ADDRESS)
-                        .addSaProposal(mIkeSaProposal)
-                        .setLocalIdentification(mLocalIdentification)
-                        .setRemoteIdentification(mRemoteIdentification)
-                        .setAuthPsk(PSK)
-                        .build();
+        IkeSessionParams sessionParams = createIkeParamsBuilderMinimum().build();
 
         PersistableBundle bundle = sessionParams.toPersistableBundle();
         IkeSessionParams result = IkeSessionParams.fromPersistableBundle(bundle);
@@ -803,4 +805,24 @@
                         .build();
         verifyPersistableBundleEncodeDecodeIsLossless(new IkeAuthEapConfig(eapSessionConfig));
     }
+
+    @Test
+    public void testConstructBuilderWithIkeSessionParams() throws Exception {
+        IkeSessionParams sessionParams = createIkeParamsBuilderMinimum().build();
+
+        IkeSessionParams result = new IkeSessionParams.Builder(sessionParams).build();
+        assertEquals(sessionParams, result);
+    }
+
+    @Test
+    public void testCreateCopyWithNetworkCleared() throws Exception {
+        IkeSessionParams sessionParams =
+                createIkeParamsBuilderMinimum()
+                        .setConfiguredNetwork(mMockUserConfigNetwork)
+                        .build();
+
+        IkeSessionParams result =
+                new IkeSessionParams.Builder(sessionParams).setConfiguredNetwork(null).build();
+        assertNull(result.getConfiguredNetwork());
+    }
 }