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 org.junit.Assert.assertEquals; 20 import static org.junit.Assert.fail; 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.mock; 23 24 import android.annotation.NonNull; 25 import android.content.Context; 26 import android.os.Parcel; 27 28 import androidx.test.filters.SmallTest; 29 import androidx.test.runner.AndroidJUnit4; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 35 import java.util.Collections; 36 import java.util.Set; 37 38 @RunWith(AndroidJUnit4.class) 39 @SmallTest 40 public class VcnConfigTest { 41 private static final String TEST_PACKAGE_NAME = VcnConfigTest.class.getPackage().getName(); 42 private static final Set<VcnGatewayConnectionConfig> GATEWAY_CONNECTION_CONFIGS = 43 Collections.singleton(VcnGatewayConnectionConfigTest.buildTestConfig()); 44 45 private final Context mContext = mock(Context.class); 46 47 // Public visibility for VcnManagementServiceTest buildTestConfig(@onNull Context context)48 public static VcnConfig buildTestConfig(@NonNull Context context) { 49 VcnConfig.Builder builder = new VcnConfig.Builder(context); 50 51 for (VcnGatewayConnectionConfig gatewayConnectionConfig : GATEWAY_CONNECTION_CONFIGS) { 52 builder.addGatewayConnectionConfig(gatewayConnectionConfig); 53 } 54 55 return builder.build(); 56 } 57 58 @Before setUp()59 public void setUp() throws Exception { 60 doReturn(TEST_PACKAGE_NAME).when(mContext).getOpPackageName(); 61 } 62 63 @Test testBuilderConstructorRequiresContext()64 public void testBuilderConstructorRequiresContext() { 65 try { 66 new VcnConfig.Builder(null); 67 fail("Expected exception due to null context"); 68 } catch (NullPointerException e) { 69 } 70 } 71 72 @Test testBuilderRequiresGatewayConnectionConfig()73 public void testBuilderRequiresGatewayConnectionConfig() { 74 try { 75 new VcnConfig.Builder(mContext).build(); 76 fail("Expected exception due to no VcnGatewayConnectionConfigs provided"); 77 } catch (IllegalArgumentException e) { 78 } 79 } 80 81 @Test testBuilderRequiresUniqueGatewayConnectionNames()82 public void testBuilderRequiresUniqueGatewayConnectionNames() { 83 final VcnGatewayConnectionConfig config = VcnGatewayConnectionConfigTest.buildTestConfig(); 84 try { 85 new VcnConfig.Builder(mContext) 86 .addGatewayConnectionConfig(config) 87 .addGatewayConnectionConfig(config); 88 fail("Expected exception due to duplicate gateway connection name"); 89 } catch (IllegalArgumentException e) { 90 } 91 } 92 93 @Test testBuilderAndGetters()94 public void testBuilderAndGetters() { 95 final VcnConfig config = buildTestConfig(mContext); 96 97 assertEquals(TEST_PACKAGE_NAME, config.getProvisioningPackageName()); 98 assertEquals(GATEWAY_CONNECTION_CONFIGS, config.getGatewayConnectionConfigs()); 99 } 100 101 @Test testPersistableBundle()102 public void testPersistableBundle() { 103 final VcnConfig config = buildTestConfig(mContext); 104 105 assertEquals(config, new VcnConfig(config.toPersistableBundle())); 106 } 107 108 @Test testParceling()109 public void testParceling() { 110 final VcnConfig config = buildTestConfig(mContext); 111 112 Parcel parcel = Parcel.obtain(); 113 config.writeToParcel(parcel, 0); 114 parcel.setDataPosition(0); 115 116 assertEquals(config, VcnConfig.CREATOR.createFromParcel(parcel)); 117 } 118 } 119