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.InetAddresses;
22 import android.net.ipsec.ike.IkeTrafficSelector;
23 import android.os.PersistableBundle;
24 
25 import androidx.test.filters.SmallTest;
26 import androidx.test.runner.AndroidJUnit4;
27 
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 
31 import java.net.InetAddress;
32 
33 @RunWith(AndroidJUnit4.class)
34 @SmallTest
35 public class IkeTrafficSelectorUtilsTest {
36     private static final int START_PORT = 16;
37     private static final int END_PORT = 65520;
38 
39     private static final InetAddress IPV4_START_ADDRESS =
40             InetAddresses.parseNumericAddress("192.0.2.100");
41     private static final InetAddress IPV4_END_ADDRESS =
42             InetAddresses.parseNumericAddress("192.0.2.101");
43 
44     private static final InetAddress IPV6_START_ADDRESS =
45             InetAddresses.parseNumericAddress("2001:db8:2::100");
46     private static final InetAddress IPV6_END_ADDRESS =
47             InetAddresses.parseNumericAddress("2001:db8:2::101");
48 
verifyPersistableBundleEncodeDecodeIsLossless(IkeTrafficSelector ts)49     private static void verifyPersistableBundleEncodeDecodeIsLossless(IkeTrafficSelector ts) {
50         final PersistableBundle bundle = IkeTrafficSelectorUtils.toPersistableBundle(ts);
51         final IkeTrafficSelector resultTs = IkeTrafficSelectorUtils.fromPersistableBundle(bundle);
52         assertEquals(ts, resultTs);
53     }
54 
55     @Test
testPersistableBundleEncodeDecodeIsLosslessIpv4Ts()56     public void testPersistableBundleEncodeDecodeIsLosslessIpv4Ts() throws Exception {
57         verifyPersistableBundleEncodeDecodeIsLossless(
58                 new IkeTrafficSelector(START_PORT, END_PORT, IPV4_START_ADDRESS, IPV4_END_ADDRESS));
59     }
60 
61     @Test
testPersistableBundleEncodeDecodeIsLosslessIpv6Ts()62     public void testPersistableBundleEncodeDecodeIsLosslessIpv6Ts() throws Exception {
63         verifyPersistableBundleEncodeDecodeIsLossless(
64                 new IkeTrafficSelector(START_PORT, END_PORT, IPV6_START_ADDRESS, IPV6_END_ADDRESS));
65     }
66 }
67