1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.net.vcn.persistablebundleutils;
18 
19 import static org.junit.Assert.assertEquals;
20 
21 import android.net.ipsec.ike.ChildSaProposal;
22 import android.net.ipsec.ike.IkeSaProposal;
23 import android.net.ipsec.ike.SaProposal;
24 import android.os.PersistableBundle;
25 
26 import androidx.test.filters.SmallTest;
27 import androidx.test.runner.AndroidJUnit4;
28 
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 
32 @RunWith(AndroidJUnit4.class)
33 @SmallTest
34 public class SaProposalUtilsTest {
35     /** Package private so that IkeSessionParamsUtilsTest can use it */
buildTestIkeSaProposal()36     static IkeSaProposal buildTestIkeSaProposal() {
37         return new IkeSaProposal.Builder()
38                 .addEncryptionAlgorithm(
39                         SaProposal.ENCRYPTION_ALGORITHM_3DES, SaProposal.KEY_LEN_UNUSED)
40                 .addEncryptionAlgorithm(
41                         SaProposal.ENCRYPTION_ALGORITHM_AES_CBC, SaProposal.KEY_LEN_AES_128)
42                 .addIntegrityAlgorithm(SaProposal.INTEGRITY_ALGORITHM_HMAC_SHA1_96)
43                 .addIntegrityAlgorithm(SaProposal.INTEGRITY_ALGORITHM_HMAC_SHA2_256_128)
44                 .addPseudorandomFunction(SaProposal.PSEUDORANDOM_FUNCTION_AES128_XCBC)
45                 .addPseudorandomFunction(SaProposal.PSEUDORANDOM_FUNCTION_SHA2_256)
46                 .addDhGroup(SaProposal.DH_GROUP_1024_BIT_MODP)
47                 .addDhGroup(SaProposal.DH_GROUP_3072_BIT_MODP)
48                 .build();
49     }
50 
51     @Test
testPersistableBundleEncodeDecodeIsLosslessIkeProposal()52     public void testPersistableBundleEncodeDecodeIsLosslessIkeProposal() throws Exception {
53         final IkeSaProposal proposal = buildTestIkeSaProposal();
54 
55         final PersistableBundle bundle = IkeSaProposalUtils.toPersistableBundle(proposal);
56         final SaProposal resultProposal = IkeSaProposalUtils.fromPersistableBundle(bundle);
57 
58         assertEquals(proposal, resultProposal);
59     }
60 
61     /** Package private so that TunnelModeChildSessionParamsUtilsTest can use it */
buildTestChildSaProposal()62     static ChildSaProposal buildTestChildSaProposal() {
63         return new ChildSaProposal.Builder()
64                 .addEncryptionAlgorithm(
65                         SaProposal.ENCRYPTION_ALGORITHM_AES_GCM_12, SaProposal.KEY_LEN_AES_128)
66                 .addEncryptionAlgorithm(
67                         SaProposal.ENCRYPTION_ALGORITHM_AES_GCM_12, SaProposal.KEY_LEN_AES_192)
68                 .addDhGroup(SaProposal.DH_GROUP_1024_BIT_MODP)
69                 .addDhGroup(SaProposal.DH_GROUP_4096_BIT_MODP)
70                 .build();
71     }
72 
73     @Test
testPersistableBundleEncodeDecodeIsLosslessChildProposal()74     public void testPersistableBundleEncodeDecodeIsLosslessChildProposal() throws Exception {
75         final ChildSaProposal proposal = buildTestChildSaProposal();
76 
77         final PersistableBundle bundle = ChildSaProposalUtils.toPersistableBundle(proposal);
78         final SaProposal resultProposal = ChildSaProposalUtils.fromPersistableBundle(bundle);
79 
80         assertEquals(proposal, resultProposal);
81     }
82 }
83