blob: 7a56b3aafee7a6f626c5e3337ce7e66512d7a9b4 [file] [log] [blame]
Remi NGUYEN VAN231b52b2019-01-29 15:38:52 +09001/*
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
17package android.net.ip;
18
19import static org.junit.Assert.assertArrayEquals;
20import static org.junit.Assert.assertEquals;
21import static org.mockito.ArgumentMatchers.any;
22import static org.mockito.Mockito.doNothing;
23import static org.mockito.Mockito.times;
24import static org.mockito.Mockito.verify;
25
26import android.net.INetd;
27import android.net.InetAddresses;
28import android.net.InterfaceConfigurationParcel;
29import android.net.LinkAddress;
30import android.net.util.SharedLog;
Brett Chabot1ae2aa62019-03-04 14:14:56 -080031
32import androidx.test.filters.SmallTest;
33import androidx.test.runner.AndroidJUnit4;
Remi NGUYEN VAN231b52b2019-01-29 15:38:52 +090034
35import org.junit.Before;
36import org.junit.Test;
37import org.junit.runner.RunWith;
38import org.mockito.ArgumentCaptor;
39import org.mockito.Captor;
40import org.mockito.Mock;
41import org.mockito.MockitoAnnotations;
42
43@RunWith(AndroidJUnit4.class)
44@SmallTest
45public 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}