1 /* 2 * Copyright (C) 2021 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.net.module.util.netlink; 18 19 import static com.android.net.module.util.netlink.RtNetlinkAddressMessage.IFA_FLAGS; 20 import static com.android.net.module.util.netlink.RtNetlinkLinkMessage.IFLA_ADDRESS; 21 import static com.android.net.module.util.netlink.RtNetlinkLinkMessage.IFLA_IFNAME; 22 23 import static org.junit.Assert.assertEquals; 24 import static org.junit.Assert.assertNull; 25 26 import android.net.MacAddress; 27 28 import androidx.test.filters.SmallTest; 29 import androidx.test.runner.AndroidJUnit4; 30 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 34 @RunWith(AndroidJUnit4.class) 35 @SmallTest 36 public class StructNlAttrTest { 37 private static final MacAddress TEST_MAC_ADDRESS = MacAddress.fromString("00:11:22:33:44:55"); 38 private static final String TEST_INTERFACE_NAME = "wlan0"; 39 private static final int TEST_ADDR_FLAGS = 0x80; 40 41 @Test testGetValueAsMacAddress()42 public void testGetValueAsMacAddress() { 43 final StructNlAttr attr1 = new StructNlAttr(IFLA_ADDRESS, TEST_MAC_ADDRESS); 44 final MacAddress address1 = attr1.getValueAsMacAddress(); 45 assertEquals(address1, TEST_MAC_ADDRESS); 46 47 // Invalid mac address byte array. 48 final byte[] array = new byte[] { 49 (byte) 0x00, (byte) 0x11, (byte) 0x22, (byte) 0x33, 50 (byte) 0x44, (byte) 0x55, (byte) 0x66, 51 }; 52 final StructNlAttr attr2 = new StructNlAttr(IFLA_ADDRESS, array); 53 final MacAddress address2 = attr2.getValueAsMacAddress(); 54 assertNull(address2); 55 } 56 57 @Test testGetValueAsString()58 public void testGetValueAsString() { 59 final StructNlAttr attr1 = new StructNlAttr(IFLA_IFNAME, TEST_INTERFACE_NAME); 60 final String str1 = attr1.getValueAsString(); 61 assertEquals(str1, TEST_INTERFACE_NAME); 62 63 final byte[] array = new byte[] { 64 (byte) 0x77, (byte) 0x6c, (byte) 0x61, (byte) 0x6E, (byte) 0x30, (byte) 0x00, 65 }; 66 final StructNlAttr attr2 = new StructNlAttr(IFLA_IFNAME, array); 67 final String str2 = attr2.getValueAsString(); 68 assertEquals(str2, TEST_INTERFACE_NAME); 69 } 70 71 @Test testGetValueAsInteger()72 public void testGetValueAsInteger() { 73 final StructNlAttr attr1 = new StructNlAttr(IFA_FLAGS, TEST_ADDR_FLAGS); 74 final Integer integer1 = attr1.getValueAsInteger(); 75 final int int1 = attr1.getValueAsInt(0x08 /* default value */); 76 assertEquals(integer1, Integer.valueOf(TEST_ADDR_FLAGS)); 77 assertEquals(int1, TEST_ADDR_FLAGS); 78 79 // Malformed attribute. 80 final byte[] malformed_int = new byte[] { (byte) 0x0, (byte) 0x0, (byte) 0x80, }; 81 final StructNlAttr attr2 = new StructNlAttr(IFA_FLAGS, malformed_int); 82 final Integer integer2 = attr2.getValueAsInteger(); 83 final int int2 = attr2.getValueAsInt(0x08 /* default value */); 84 assertNull(integer2); 85 assertEquals(int2, 0x08 /* default value */); 86 87 // Null attribute value. 88 final byte[] null_int = null; 89 final StructNlAttr attr3 = new StructNlAttr(IFA_FLAGS, null_int); 90 final Integer integer3 = attr3.getValueAsInteger(); 91 final int int3 = attr3.getValueAsInt(0x08 /* default value */); 92 assertNull(integer3); 93 assertEquals(int3, 0x08 /* default value */); 94 } 95 96 @Test testGetValueAsLong()97 public void testGetValueAsLong() { 98 final Long input = 1234567L; 99 // Not a real netlink attribute, just for testing 100 final StructNlAttr attr = new StructNlAttr(IFA_FLAGS, input); 101 102 final Long output = attr.getValueAsLong(); 103 104 assertEquals(input, output); 105 } 106 107 @Test testGetValueAsLong_malformed()108 public void testGetValueAsLong_malformed() { 109 final int input = 1234567; 110 // Not a real netlink attribute, just for testing 111 final StructNlAttr attr = new StructNlAttr(IFA_FLAGS, input); 112 113 final Long output = attr.getValueAsLong(); 114 115 assertNull(output); 116 } 117 } 118