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.assertParcelingIsLossless; 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.IgnoreAfter; 37 import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo; 38 39 import org.junit.Before; 40 import org.junit.Rule; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 44 import java.util.Arrays; 45 46 @RunWith(AndroidJUnit4.class) 47 @SmallTest 48 public class ApfCapabilitiesTest { 49 @Rule 50 public final DevSdkIgnoreRule mIgnoreRule = new DevSdkIgnoreRule(); 51 52 private Context mContext; 53 54 @Before setUp()55 public void setUp() { 56 mContext = InstrumentationRegistry.getContext(); 57 } 58 59 @Test testConstructAndParcel()60 public void testConstructAndParcel() { 61 final ApfCapabilities caps = new ApfCapabilities(123, 456, 789); 62 assertEquals(123, caps.apfVersionSupported); 63 assertEquals(456, caps.maximumApfProgramSize); 64 assertEquals(789, caps.apfPacketFormat); 65 66 assertParcelingIsLossless(caps); 67 } 68 69 @Test testEquals()70 public void testEquals() { 71 assertEquals(new ApfCapabilities(1, 2, 3), new ApfCapabilities(1, 2, 3)); 72 assertNotEquals(new ApfCapabilities(2, 2, 3), new ApfCapabilities(1, 2, 3)); 73 assertNotEquals(new ApfCapabilities(1, 3, 3), new ApfCapabilities(1, 2, 3)); 74 assertNotEquals(new ApfCapabilities(1, 2, 4), new ApfCapabilities(1, 2, 3)); 75 } 76 77 @Test testHasDataAccess()78 public void testHasDataAccess() { 79 //hasDataAccess is only supported starting at apf version 4. 80 ApfCapabilities caps = new ApfCapabilities(1 /* apfVersionSupported */, 2, 3); 81 assertFalse(caps.hasDataAccess()); 82 83 caps = new ApfCapabilities(4 /* apfVersionSupported */, 5, 6); 84 assertTrue(caps.hasDataAccess()); 85 } 86 87 // the property is deleted in U, on S & T getApfDrop8023Frames() is mainline and 88 // hardcoded to return true, so this simply verifies the unused property is also true, 89 // as modifying it would not take effect. For S+ change NetworkStack's 90 // config_apfDrop802_3Frames instead. 91 @Test @IgnoreAfter(Build.VERSION_CODES.TIRAMISU) testGetApfDrop8023Frames()92 public void testGetApfDrop8023Frames() { 93 // Get com.android.internal.R.bool.config_apfDrop802_3Frames. The test cannot directly 94 // use R.bool.config_apfDrop802_3Frames because that is not a stable resource ID. 95 final int resId = mContext.getResources().getIdentifier("config_apfDrop802_3Frames", 96 "bool", "android"); 97 final boolean shouldDrop8023Frames = mContext.getResources().getBoolean(resId); 98 final boolean actual = ApfCapabilities.getApfDrop8023Frames(); 99 assertEquals(shouldDrop8023Frames, actual); 100 } 101 102 @Test @IgnoreUpTo(Build.VERSION_CODES.R) testGetApfDrop8023Frames_S()103 public void testGetApfDrop8023Frames_S() { 104 // IpClient does not call getApfDrop8023Frames() since S, so any customization of the return 105 // value on S+ is a configuration error as it will not be used by IpClient. 106 assertTrue("android.R.bool.config_apfDrop802_3Frames has been modified to false, but " 107 + "starting from S its value is not used by IpClient. If the modification is " 108 + "intentional, use a runtime resource overlay for the NetworkStack package to " 109 + "overlay com.android.networkstack.R.bool.config_apfDrop802_3Frames instead.", 110 ApfCapabilities.getApfDrop8023Frames()); 111 } 112 113 // the property is deleted in U, on S & T getApfEtherTypeBlackList() is mainline and 114 // hardcoded to return a specific default set of ethertypes, so this simply verifies 115 // that the unused property hasn't been changed away from the default, as it would 116 // not take effect. For S+ change NetworkStack's config_apfEthTypeDenyList instead. 117 @Test @IgnoreAfter(Build.VERSION_CODES.TIRAMISU) testGetApfEtherTypeBlackList()118 public void testGetApfEtherTypeBlackList() { 119 // Get com.android.internal.R.array.config_apfEthTypeBlackList. The test cannot directly 120 // use R.array.config_apfEthTypeBlackList because that is not a stable resource ID. 121 final int resId = mContext.getResources().getIdentifier("config_apfEthTypeBlackList", 122 "array", "android"); 123 final int[] blacklistedEtherTypeArray = mContext.getResources().getIntArray(resId); 124 final int[] actual = ApfCapabilities.getApfEtherTypeBlackList(); 125 assertNotNull(actual); 126 assertTrue(Arrays.equals(blacklistedEtherTypeArray, actual)); 127 } 128 129 @Test @IgnoreUpTo(Build.VERSION_CODES.R) testGetApfEtherTypeBlackList_S()130 public void testGetApfEtherTypeBlackList_S() { 131 // IpClient does not call getApfEtherTypeBlackList() since S, so any customization of the 132 // return value on S+ is a configuration error as it will not be used by IpClient. 133 assertArrayEquals("android.R.array.config_apfEthTypeBlackList has been modified, but " 134 + "starting from S its value is not used by IpClient. If the modification " 135 + "is intentional, use a runtime resource overlay for the NetworkStack " 136 + "package to overlay " 137 + "com.android.networkstack.R.array.config_apfEthTypeDenyList instead.", 138 new int[] { 0x88a2, 0x88a4, 0x88b8, 0x88cd, 0x88e3 }, 139 ApfCapabilities.getApfEtherTypeBlackList()); 140 } 141 } 142