1 /* 2 * Copyright (C) 2020 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; 18 19 import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; 20 import static android.net.NetworkCapabilities.TRANSPORT_TEST; 21 import static android.net.NetworkCapabilities.TRANSPORT_WIFI; 22 23 import static org.junit.Assert.assertEquals; 24 import static org.junit.Assert.assertFalse; 25 import static org.junit.Assert.assertNotEquals; 26 import static org.junit.Assert.fail; 27 import static org.mockito.Mockito.doReturn; 28 import static org.mockito.Mockito.mock; 29 30 import android.annotation.NonNull; 31 import android.content.Context; 32 import android.os.Parcel; 33 import android.util.ArraySet; 34 35 import androidx.test.filters.SmallTest; 36 import androidx.test.runner.AndroidJUnit4; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 42 import java.util.Collections; 43 import java.util.Set; 44 45 @RunWith(AndroidJUnit4.class) 46 @SmallTest 47 public class VcnConfigTest { 48 private static final String TEST_PACKAGE_NAME = VcnConfigTest.class.getPackage().getName(); 49 private static final Set<VcnGatewayConnectionConfig> GATEWAY_CONNECTION_CONFIGS = 50 Collections.singleton(VcnGatewayConnectionConfigTest.buildTestConfig()); 51 52 private static final Set<Integer> RESTRICTED_TRANSPORTS = new ArraySet<>(); 53 54 static { 55 RESTRICTED_TRANSPORTS.add(TRANSPORT_WIFI); 56 RESTRICTED_TRANSPORTS.add(TRANSPORT_CELLULAR); 57 } 58 59 private final Context mContext = mock(Context.class); 60 61 // Public visibility for VcnManagementServiceTest buildTestConfig( @onNull Context context, Set<Integer> restrictedTransports)62 public static VcnConfig buildTestConfig( 63 @NonNull Context context, Set<Integer> restrictedTransports) { 64 VcnConfig.Builder builder = new VcnConfig.Builder(context); 65 66 for (VcnGatewayConnectionConfig gatewayConnectionConfig : GATEWAY_CONNECTION_CONFIGS) { 67 builder.addGatewayConnectionConfig(gatewayConnectionConfig); 68 } 69 70 if (restrictedTransports != null) { 71 builder.setRestrictedUnderlyingNetworkTransports(restrictedTransports); 72 } 73 74 return builder.build(); 75 } 76 77 // Public visibility for VcnManagementServiceTest buildTestConfig(@onNull Context context)78 public static VcnConfig buildTestConfig(@NonNull Context context) { 79 return buildTestConfig(context, null); 80 } 81 82 @Before setUp()83 public void setUp() throws Exception { 84 doReturn(TEST_PACKAGE_NAME).when(mContext).getOpPackageName(); 85 } 86 87 @Test testBuilderConstructorRequiresContext()88 public void testBuilderConstructorRequiresContext() { 89 try { 90 new VcnConfig.Builder(null); 91 fail("Expected exception due to null context"); 92 } catch (NullPointerException e) { 93 } 94 } 95 96 @Test testBuilderRequiresGatewayConnectionConfig()97 public void testBuilderRequiresGatewayConnectionConfig() { 98 try { 99 new VcnConfig.Builder(mContext).build(); 100 fail("Expected exception due to no VcnGatewayConnectionConfigs provided"); 101 } catch (IllegalArgumentException e) { 102 } 103 } 104 105 @Test testBuilderRequiresUniqueGatewayConnectionNames()106 public void testBuilderRequiresUniqueGatewayConnectionNames() { 107 final VcnGatewayConnectionConfig config = VcnGatewayConnectionConfigTest.buildTestConfig(); 108 try { 109 new VcnConfig.Builder(mContext) 110 .addGatewayConnectionConfig(config) 111 .addGatewayConnectionConfig(config); 112 fail("Expected exception due to duplicate gateway connection name"); 113 } catch (IllegalArgumentException e) { 114 } 115 } 116 117 @Test testBuilderAndGettersDefaultValues()118 public void testBuilderAndGettersDefaultValues() { 119 final VcnConfig config = buildTestConfig(mContext); 120 121 assertEquals(TEST_PACKAGE_NAME, config.getProvisioningPackageName()); 122 assertEquals(GATEWAY_CONNECTION_CONFIGS, config.getGatewayConnectionConfigs()); 123 assertFalse(config.isTestModeProfile()); 124 assertEquals( 125 Collections.singleton(TRANSPORT_WIFI), 126 config.getRestrictedUnderlyingNetworkTransports()); 127 } 128 129 @Test testBuilderAndGettersConfigRestrictedTransports()130 public void testBuilderAndGettersConfigRestrictedTransports() { 131 final VcnConfig config = buildTestConfig(mContext, RESTRICTED_TRANSPORTS); 132 133 assertEquals(TEST_PACKAGE_NAME, config.getProvisioningPackageName()); 134 assertEquals(GATEWAY_CONNECTION_CONFIGS, config.getGatewayConnectionConfigs()); 135 assertFalse(config.isTestModeProfile()); 136 assertEquals(RESTRICTED_TRANSPORTS, config.getRestrictedUnderlyingNetworkTransports()); 137 } 138 139 @Test testPersistableBundle()140 public void testPersistableBundle() { 141 final VcnConfig config = buildTestConfig(mContext); 142 143 assertEquals(config, new VcnConfig(config.toPersistableBundle())); 144 } 145 146 @Test testPersistableBundleWithRestrictedTransports()147 public void testPersistableBundleWithRestrictedTransports() { 148 final VcnConfig config = buildTestConfig(mContext, RESTRICTED_TRANSPORTS); 149 150 assertEquals(config, new VcnConfig(config.toPersistableBundle())); 151 } 152 153 @Test testEqualityWithRestrictedTransports()154 public void testEqualityWithRestrictedTransports() { 155 final VcnConfig config = buildTestConfig(mContext, RESTRICTED_TRANSPORTS); 156 final VcnConfig configEqual = buildTestConfig(mContext, RESTRICTED_TRANSPORTS); 157 final VcnConfig configNotEqual = 158 buildTestConfig(mContext, Collections.singleton(TRANSPORT_WIFI)); 159 160 assertEquals(config, configEqual); 161 assertNotEquals(config, configNotEqual); 162 } 163 buildConfigRestrictTransportTest(boolean isTestMode)164 private VcnConfig buildConfigRestrictTransportTest(boolean isTestMode) throws Exception { 165 VcnConfig.Builder builder = 166 new VcnConfig.Builder(mContext) 167 .setRestrictedUnderlyingNetworkTransports(Set.of(TRANSPORT_TEST)); 168 if (isTestMode) { 169 builder.setIsTestModeProfile(); 170 } 171 172 for (VcnGatewayConnectionConfig gatewayConnectionConfig : GATEWAY_CONNECTION_CONFIGS) { 173 builder.addGatewayConnectionConfig(gatewayConnectionConfig); 174 } 175 176 return builder.build(); 177 } 178 179 @Test testRestrictTransportTestInTestModeProfile()180 public void testRestrictTransportTestInTestModeProfile() throws Exception { 181 final VcnConfig config = buildConfigRestrictTransportTest(true /* isTestMode */); 182 assertEquals(Set.of(TRANSPORT_TEST), config.getRestrictedUnderlyingNetworkTransports()); 183 } 184 185 @Test testRestrictTransportTestInNonTestModeProfile()186 public void testRestrictTransportTestInNonTestModeProfile() throws Exception { 187 try { 188 buildConfigRestrictTransportTest(false /* isTestMode */); 189 fail("Expected exception because the config is not a test mode profile"); 190 } catch (Exception expected) { 191 192 } 193 } 194 195 @Test testParceling()196 public void testParceling() { 197 final VcnConfig config = buildTestConfig(mContext); 198 199 Parcel parcel = Parcel.obtain(); 200 config.writeToParcel(parcel, 0); 201 parcel.setDataPosition(0); 202 203 assertEquals(config, VcnConfig.CREATOR.createFromParcel(parcel)); 204 } 205 } 206