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