1 /*
2  * Copyright (C) 2022 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 com.android.server.net;
18 
19 import static org.junit.Assert.assertArrayEquals;
20 import static org.junit.Assert.assertEquals;
21 
22 import androidx.test.ext.junit.runners.AndroidJUnit4;
23 
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 
27 @RunWith(AndroidJUnit4.class)
28 public class InterfaceMapValueTest {
29     private static final String IF_NAME = "wlan0";
30     private static final byte[] IF_NAME_BYTE = new byte[]{'w', 'l', 'a', 'n', '0'};
31     private static final byte[] IF_NAME_BYTE_WITH_PADDING =
32             new byte[]{'w', 'l', 'a', 'n', '0', 0, 0, 0,
33                     0, 0, 0, 0, 0, 0, 0, 0}; // IF_NAME_BYTE_WITH_PADDING.length = 16
34     private static final byte[] IF_NAME_BYTE_LONG =
35             new byte[]{'w', 'l', 'a', 'n', '0', 0, 0, 0,
36                     0, 0, 0, 0, 0, 0, 0, 0,
37                     0, 0, 0, 0, 0, 0, 0, 0}; // IF_NAME_BYTE_LONG.length = 24
38 
39     @Test
testInterfaceMapValueFromString()40     public void testInterfaceMapValueFromString() {
41         final InterfaceMapValue value = new InterfaceMapValue(IF_NAME);
42         assertArrayEquals(IF_NAME_BYTE_WITH_PADDING, value.interfaceName);
43     }
44 
45     @Test
testInterfaceMapValueFromByte()46     public void testInterfaceMapValueFromByte() {
47         final InterfaceMapValue value = new InterfaceMapValue(IF_NAME_BYTE_WITH_PADDING);
48         assertArrayEquals(IF_NAME_BYTE_WITH_PADDING, value.interfaceName);
49     }
50 
51     @Test
testInterfaceMapValueFromByteShort()52     public void testInterfaceMapValueFromByteShort() {
53         final InterfaceMapValue value = new InterfaceMapValue(IF_NAME_BYTE);
54         assertArrayEquals(IF_NAME_BYTE_WITH_PADDING, value.interfaceName);
55     }
56 
57     @Test
testInterfaceMapValueFromByteLong()58     public void testInterfaceMapValueFromByteLong() {
59         final InterfaceMapValue value = new InterfaceMapValue(IF_NAME_BYTE_LONG);
60         assertArrayEquals(IF_NAME_BYTE_WITH_PADDING, value.interfaceName);
61     }
62 
63     @Test
testGetInterfaceNameString()64     public void testGetInterfaceNameString() {
65         final InterfaceMapValue value = new InterfaceMapValue(IF_NAME_BYTE_WITH_PADDING);
66         assertEquals(IF_NAME, value.getInterfaceNameString());
67     }
68 }
69