1 /* 2 * Copyright (C) 2019 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.shared; 18 19 import static android.net.InetAddresses.parseNumericAddress; 20 21 import static com.android.testutils.MiscAsserts.assertFieldCountEquals; 22 23 import static org.junit.Assert.assertEquals; 24 import static org.junit.Assert.assertNotEquals; 25 26 import android.net.IpPrefix; 27 import android.net.LinkAddress; 28 29 import androidx.test.filters.SmallTest; 30 import androidx.test.runner.AndroidJUnit4; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 import java.util.Arrays; 37 import java.util.function.Consumer; 38 39 /** 40 * Tests for {@link InitialConfiguration} 41 */ 42 @RunWith(AndroidJUnit4.class) 43 @SmallTest 44 public class InitialConfigurationTest { 45 private InitialConfiguration mConfig; 46 47 @Before setUp()48 public void setUp() { 49 mConfig = new InitialConfiguration(); 50 mConfig.ipAddresses.addAll(Arrays.asList( 51 new LinkAddress(parseNumericAddress("192.168.45.45"), 16), 52 new LinkAddress(parseNumericAddress("2001:db8::45"), 33))); 53 mConfig.directlyConnectedRoutes.addAll(Arrays.asList( 54 new IpPrefix(parseNumericAddress("192.168.46.46"), 17), 55 new IpPrefix(parseNumericAddress("2001:db8::46"), 34))); 56 mConfig.dnsServers.addAll(Arrays.asList( 57 parseNumericAddress("192.168.47.47"), 58 parseNumericAddress("2001:db8::47"))); 59 // Any added InitialConfiguration field must be included in equals() to be tested properly 60 assertFieldCountEquals(3, InitialConfiguration.class); 61 } 62 63 @Test testParcelUnparcelInitialConfiguration()64 public void testParcelUnparcelInitialConfiguration() { 65 final InitialConfiguration unparceled = 66 InitialConfiguration.fromStableParcelable(mConfig.toStableParcelable()); 67 assertEquals(mConfig, unparceled); 68 } 69 70 @Test testEquals()71 public void testEquals() { 72 assertEquals(mConfig, InitialConfiguration.copy(mConfig)); 73 74 assertNotEqualsAfterChange(c -> c.ipAddresses.add( 75 new LinkAddress(parseNumericAddress("192.168.47.47"), 24))); 76 assertNotEqualsAfterChange(c -> c.directlyConnectedRoutes.add( 77 new IpPrefix(parseNumericAddress("192.168.46.46"), 32))); 78 assertNotEqualsAfterChange(c -> c.dnsServers.add(parseNumericAddress("2001:db8::49"))); 79 assertFieldCountEquals(3, InitialConfiguration.class); 80 } 81 assertNotEqualsAfterChange(Consumer<InitialConfiguration> mutator)82 private void assertNotEqualsAfterChange(Consumer<InitialConfiguration> mutator) { 83 final InitialConfiguration newConfig = InitialConfiguration.copy(mConfig); 84 mutator.accept(newConfig); 85 assertNotEquals(mConfig, newConfig); 86 } 87 } 88