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