Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 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.ip; |
| 18 | |
| 19 | import static org.junit.Assert.assertArrayEquals; |
| 20 | import static org.junit.Assert.assertEquals; |
| 21 | import static org.mockito.ArgumentMatchers.any; |
| 22 | import static org.mockito.Mockito.doNothing; |
| 23 | import static org.mockito.Mockito.times; |
| 24 | import static org.mockito.Mockito.verify; |
| 25 | |
| 26 | import android.net.INetd; |
| 27 | import android.net.InetAddresses; |
| 28 | import android.net.InterfaceConfigurationParcel; |
| 29 | import android.net.LinkAddress; |
| 30 | import android.net.util.SharedLog; |
Brett Chabot | 1ae2aa6 | 2019-03-04 14:14:56 -0800 | [diff] [blame] | 31 | |
| 32 | import androidx.test.filters.SmallTest; |
| 33 | import androidx.test.runner.AndroidJUnit4; |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 34 | |
| 35 | import org.junit.Before; |
| 36 | import org.junit.Test; |
| 37 | import org.junit.runner.RunWith; |
| 38 | import org.mockito.ArgumentCaptor; |
| 39 | import org.mockito.Captor; |
| 40 | import org.mockito.Mock; |
| 41 | import org.mockito.MockitoAnnotations; |
| 42 | |
| 43 | @RunWith(AndroidJUnit4.class) |
| 44 | @SmallTest |
| 45 | public class InterfaceControllerTest { |
| 46 | private static final String TEST_IFACE = "testif"; |
| 47 | private static final String TEST_IPV4_ADDR = "192.168.123.28"; |
| 48 | private static final int TEST_PREFIXLENGTH = 31; |
| 49 | |
| 50 | @Mock private INetd mNetd; |
| 51 | @Mock private SharedLog mLog; |
| 52 | @Captor private ArgumentCaptor<InterfaceConfigurationParcel> mConfigCaptor; |
| 53 | |
| 54 | private InterfaceController mController; |
| 55 | |
| 56 | @Before |
| 57 | public void setUp() throws Exception { |
| 58 | MockitoAnnotations.initMocks(this); |
| 59 | mController = new InterfaceController(TEST_IFACE, mNetd, mLog); |
| 60 | |
| 61 | doNothing().when(mNetd).interfaceSetCfg(mConfigCaptor.capture()); |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | public void testSetIPv4Address() throws Exception { |
| 66 | mController.setIPv4Address( |
| 67 | new LinkAddress(InetAddresses.parseNumericAddress(TEST_IPV4_ADDR), |
| 68 | TEST_PREFIXLENGTH)); |
| 69 | verify(mNetd, times(1)).interfaceSetCfg(any()); |
| 70 | final InterfaceConfigurationParcel parcel = mConfigCaptor.getValue(); |
| 71 | assertEquals(TEST_IFACE, parcel.ifName); |
| 72 | assertEquals(TEST_IPV4_ADDR, parcel.ipv4Addr); |
| 73 | assertEquals(TEST_PREFIXLENGTH, parcel.prefixLength); |
| 74 | assertEquals("", parcel.hwAddr); |
| 75 | assertArrayEquals(new String[0], parcel.flags); |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void testClearIPv4Address() throws Exception { |
| 80 | mController.clearIPv4Address(); |
| 81 | verify(mNetd, times(1)).interfaceSetCfg(any()); |
| 82 | final InterfaceConfigurationParcel parcel = mConfigCaptor.getValue(); |
| 83 | assertEquals(TEST_IFACE, parcel.ifName); |
| 84 | assertEquals("0.0.0.0", parcel.ipv4Addr); |
| 85 | assertEquals(0, parcel.prefixLength); |
| 86 | assertEquals("", parcel.hwAddr); |
| 87 | assertArrayEquals(new String[0], parcel.flags); |
| 88 | } |
| 89 | } |