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.apf; 18 19 import static com.android.testutils.ParcelUtils.assertParcelSane; 20 21 import static org.junit.Assert.assertArrayEquals; 22 import static org.junit.Assert.assertEquals; 23 import static org.junit.Assert.assertFalse; 24 import static org.junit.Assert.assertNotEquals; 25 import static org.junit.Assert.assertNotNull; 26 import static org.junit.Assert.assertTrue; 27 28 import android.content.Context; 29 import android.os.Build; 30 31 import androidx.test.InstrumentationRegistry; 32 import androidx.test.filters.SmallTest; 33 import androidx.test.runner.AndroidJUnit4; 34 35 import com.android.testutils.DevSdkIgnoreRule; 36 import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo; 37 38 import org.junit.Before; 39 import org.junit.Rule; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 43 import java.util.Arrays; 44 45 @RunWith(AndroidJUnit4.class) 46 @SmallTest 47 public class ApfCapabilitiesTest { 48 @Rule 49 public final DevSdkIgnoreRule mIgnoreRule = new DevSdkIgnoreRule(); 50 51 private Context mContext; 52 53 @Before setUp()54 public void setUp() { 55 mContext = InstrumentationRegistry.getContext(); 56 } 57 58 @Test testConstructAndParcel()59 public void testConstructAndParcel() { 60 final ApfCapabilities caps = new ApfCapabilities(123, 456, 789); 61 assertEquals(123, caps.apfVersionSupported); 62 assertEquals(456, caps.maximumApfProgramSize); 63 assertEquals(789, caps.apfPacketFormat); 64 65 assertParcelSane(caps, 3); 66 } 67 68 @Test testEquals()69 public void testEquals() { 70 assertEquals(new ApfCapabilities(1, 2, 3), new ApfCapabilities(1, 2, 3)); 71 assertNotEquals(new ApfCapabilities(2, 2, 3), new ApfCapabilities(1, 2, 3)); 72 assertNotEquals(new ApfCapabilities(1, 3, 3), new ApfCapabilities(1, 2, 3)); 73 assertNotEquals(new ApfCapabilities(1, 2, 4), new ApfCapabilities(1, 2, 3)); 74 } 75 76 @Test testHasDataAccess()77 public void testHasDataAccess() { 78 //hasDataAccess is only supported starting at apf version 4. 79 ApfCapabilities caps = new ApfCapabilities(1 /* apfVersionSupported */, 2, 3); 80 assertFalse(caps.hasDataAccess()); 81 82 caps = new ApfCapabilities(4 /* apfVersionSupported */, 5, 6); 83 assertTrue(caps.hasDataAccess()); 84 } 85 86 @Test testGetApfDrop8023Frames()87 public void testGetApfDrop8023Frames() { 88 // Get com.android.internal.R.bool.config_apfDrop802_3Frames. The test cannot directly 89 // use R.bool.config_apfDrop802_3Frames because that is not a stable resource ID. 90 final int resId = mContext.getResources().getIdentifier("config_apfDrop802_3Frames", 91 "bool", "android"); 92 final boolean shouldDrop8023Frames = mContext.getResources().getBoolean(resId); 93 final boolean actual = ApfCapabilities.getApfDrop8023Frames(); 94 assertEquals(shouldDrop8023Frames, actual); 95 } 96 97 @Test @IgnoreUpTo(Build.VERSION_CODES.R) testGetApfDrop8023Frames_S()98 public void testGetApfDrop8023Frames_S() { 99 // IpClient does not call getApfDrop8023Frames() since S, so any customization of the return 100 // value on S+ is a configuration error as it will not be used by IpClient. 101 assertTrue("android.R.bool.config_apfDrop802_3Frames has been modified to false, but " 102 + "starting from S its value is not used by IpClient. If the modification is " 103 + "intentional, use a runtime resource overlay for the NetworkStack package to " 104 + "overlay com.android.networkstack.R.bool.config_apfDrop802_3Frames instead.", 105 ApfCapabilities.getApfDrop8023Frames()); 106 } 107 108 @Test testGetApfEtherTypeBlackList()109 public void testGetApfEtherTypeBlackList() { 110 // Get com.android.internal.R.array.config_apfEthTypeBlackList. The test cannot directly 111 // use R.array.config_apfEthTypeBlackList because that is not a stable resource ID. 112 final int resId = mContext.getResources().getIdentifier("config_apfEthTypeBlackList", 113 "array", "android"); 114 final int[] blacklistedEtherTypeArray = mContext.getResources().getIntArray(resId); 115 final int[] actual = ApfCapabilities.getApfEtherTypeBlackList(); 116 assertNotNull(actual); 117 assertTrue(Arrays.equals(blacklistedEtherTypeArray, actual)); 118 } 119 120 @Test @IgnoreUpTo(Build.VERSION_CODES.R) testGetApfEtherTypeBlackList_S()121 public void testGetApfEtherTypeBlackList_S() { 122 // IpClient does not call getApfEtherTypeBlackList() since S, so any customization of the 123 // return value on S+ is a configuration error as it will not be used by IpClient. 124 assertArrayEquals("android.R.array.config_apfEthTypeBlackList has been modified, but " 125 + "starting from S its value is not used by IpClient. If the modification " 126 + "is intentional, use a runtime resource overlay for the NetworkStack " 127 + "package to overlay " 128 + "com.android.networkstack.R.array.config_apfEthTypeDenyList instead.", 129 new int[] { 0x88a2, 0x88a4, 0x88b8, 0x88cd, 0x88e3 }, 130 ApfCapabilities.getApfEtherTypeBlackList()); 131 } 132 } 133