1 /* 2 * Copyright (C) 2023 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.wifi; 18 19 20 import static org.junit.Assert.assertEquals; 21 import static org.junit.Assert.assertThrows; 22 import static org.junit.Assert.assertTrue; 23 24 import android.net.DscpPolicy; 25 import android.os.Parcel; 26 27 import com.android.modules.utils.build.SdkLevel; 28 29 import org.junit.Test; 30 31 import java.net.InetAddress; 32 33 /** 34 * Unit tests for {@link QosPolicyParams}. 35 */ 36 public class QosPolicyParamsTest { 37 private static final int TEST_POLICY_ID = 127; 38 private static final int TEST_DIRECTION = QosPolicyParams.DIRECTION_DOWNLINK; 39 private static final int TEST_IP_VERSION = QosPolicyParams.IP_VERSION_4; 40 private static final int TEST_DSCP = 7; 41 private static final int TEST_USER_PRIORITY = QosPolicyParams.USER_PRIORITY_VIDEO_LOW; 42 private static final int TEST_SOURCE_PORT = 15; 43 private static final int TEST_PROTOCOL = QosPolicyParams.PROTOCOL_TCP; 44 private static final int TEST_DESTINATION_PORT = 17; 45 private static final String TEST_SOURCE_ADDRESS = "127.0.0.1"; 46 private static final String TEST_DESTINATION_ADDRESS = "127.0.0.2"; 47 getInetAddress(String addr)48 private InetAddress getInetAddress(String addr) { 49 try { 50 return InetAddress.getByName(addr); 51 } catch (Exception e) { 52 // Should not occur. 53 return null; 54 } 55 } 56 57 /** 58 * Creates a QosCharacteristics object with all fields assigned to a default value. 59 */ createTestQosCharacteristics()60 private static QosCharacteristics createTestQosCharacteristics() { 61 int minServiceIntervalMicros = 2000; 62 int maxServiceIntervalMicros = 5000; 63 int minDataRateKbps = 500; 64 int delayBoundMicros = 200; 65 return new QosCharacteristics.Builder( 66 minServiceIntervalMicros, maxServiceIntervalMicros, 67 minDataRateKbps, delayBoundMicros).build(); 68 } 69 70 /** 71 * Creates a QosPolicyParams object with all fields assigned to a default test value. 72 */ createTestQosPolicyParams()73 private QosPolicyParams createTestQosPolicyParams() { 74 QosPolicyParams.Builder builder = 75 new QosPolicyParams.Builder(TEST_POLICY_ID, TEST_DIRECTION) 76 .setUserPriority(TEST_USER_PRIORITY) 77 .setIpVersion(TEST_IP_VERSION) 78 .setDscp(TEST_DSCP) 79 .setSourcePort(TEST_SOURCE_PORT) 80 .setProtocol(TEST_PROTOCOL) 81 .setDestinationPort(TEST_DESTINATION_PORT) 82 .setSourceAddress(getInetAddress(TEST_SOURCE_ADDRESS)) 83 .setDestinationAddress(getInetAddress(TEST_DESTINATION_ADDRESS)); 84 if (SdkLevel.isAtLeastV()) { 85 builder.setQosCharacteristics(createTestQosCharacteristics()); 86 } 87 return builder.build(); 88 } 89 90 /** 91 * Check that all fields in the provided QosPolicyParams object match the default test values. 92 */ verifyTestQosPolicyParams(QosPolicyParams params)93 private void verifyTestQosPolicyParams(QosPolicyParams params) { 94 assertEquals(TEST_POLICY_ID, params.getPolicyId()); 95 assertEquals(TEST_DIRECTION, params.getDirection()); 96 assertEquals(TEST_IP_VERSION, params.getIpVersion()); 97 assertEquals(TEST_USER_PRIORITY, params.getUserPriority()); 98 assertEquals(TEST_DSCP, params.getDscp()); 99 assertEquals(TEST_SOURCE_PORT, params.getSourcePort()); 100 assertEquals(TEST_PROTOCOL, params.getProtocol()); 101 assertEquals(TEST_DESTINATION_PORT, params.getDestinationPort()); 102 assertTrue(getInetAddress(TEST_SOURCE_ADDRESS).equals(params.getSourceAddress())); 103 assertTrue(getInetAddress(TEST_DESTINATION_ADDRESS).equals(params.getDestinationAddress())); 104 if (SdkLevel.isAtLeastV()) { 105 QosCharacteristics testQosCharacteristics = createTestQosCharacteristics(); 106 assertEquals(testQosCharacteristics, params.getQosCharacteristics()); 107 } 108 } 109 110 /** 111 * Tests that the default parameters are set if they are not assigned by the user. 112 */ 113 @Test testDefaultParamsSet()114 public void testDefaultParamsSet() { 115 QosPolicyParams params = 116 new QosPolicyParams.Builder(TEST_POLICY_ID, QosPolicyParams.DIRECTION_DOWNLINK) 117 .setUserPriority(TEST_USER_PRIORITY) 118 .setIpVersion(TEST_IP_VERSION) 119 .build(); 120 assertEquals(QosPolicyParams.DSCP_ANY, params.getDscp()); 121 assertEquals(QosPolicyParams.PROTOCOL_ANY, params.getProtocol()); 122 assertEquals(DscpPolicy.SOURCE_PORT_ANY, params.getSourcePort()); 123 } 124 125 /** 126 * Test that if we set all the parameters in the Builder, the resulting QosPolicyParams 127 * object contains the expected values. 128 */ 129 @Test testSetAllParams()130 public void testSetAllParams() { 131 QosPolicyParams params = createTestQosPolicyParams(); 132 verifyTestQosPolicyParams(params); 133 } 134 135 /** 136 * Tests that the Builder throws an exception if an invalid parameter is set. 137 */ 138 @Test testBuilderWithInvalidParam()139 public void testBuilderWithInvalidParam() { 140 assertThrows(IllegalArgumentException.class, () -> 141 new QosPolicyParams.Builder(TEST_POLICY_ID, QosPolicyParams.DIRECTION_DOWNLINK) 142 .setUserPriority(TEST_USER_PRIORITY) 143 .setDscp(120) // DSCP should be <= 63 144 .build()); 145 } 146 147 /** 148 * Tests that the Builder throws an exception if a direction-specific error is found. 149 */ 150 @Test testBuilderWithDirectionSpecificError()151 public void testBuilderWithDirectionSpecificError() { 152 assertThrows(IllegalArgumentException.class, () -> 153 // Policies for downlink are required to have a User Priority and IP Version. 154 new QosPolicyParams.Builder(TEST_POLICY_ID, QosPolicyParams.DIRECTION_DOWNLINK) 155 .build()); 156 assertThrows(IllegalArgumentException.class, () -> 157 // Policies for uplink are required to have QoS characteristics. 158 new QosPolicyParams.Builder(TEST_POLICY_ID, QosPolicyParams.DIRECTION_UPLINK) 159 .build()); 160 } 161 162 /** 163 * Tests that the parceling logic can properly read and write from a Parcel. 164 */ 165 @Test testParcelReadWrite()166 public void testParcelReadWrite() { 167 QosPolicyParams params = createTestQosPolicyParams(); 168 Parcel parcel = Parcel.obtain(); 169 params.writeToParcel(parcel, 0); 170 parcel.setDataPosition(0); // Rewind data position back to the beginning for read. 171 QosPolicyParams unparceledParams = QosPolicyParams.CREATOR.createFromParcel(parcel); 172 verifyTestQosPolicyParams(unparceledParams); 173 } 174 175 /** 176 * Tests that the overridden equality and hashCode operators properly compare two objects. 177 */ 178 @Test testObjectComparison()179 public void testObjectComparison() { 180 QosPolicyParams params1 = createTestQosPolicyParams(); 181 QosPolicyParams params2 = createTestQosPolicyParams(); 182 assertTrue(params1.equals(params2)); 183 assertEquals(params1.hashCode(), params2.hashCode()); 184 } 185 } 186