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 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertNotEquals; 22 import static org.junit.Assert.assertThrows; 23 import static org.junit.Assert.assertTrue; 24 import static org.junit.Assume.assumeTrue; 25 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 public class MscsParamsTest { 34 private static final int TEST_FRAME_CLASSIFIER_FIELDS = 35 MscsParams.FRAME_CLASSIFIER_IP_VERSION | MscsParams.FRAME_CLASSIFIER_SRC_PORT; 36 private static final int TEST_USER_PRIORITY_BITMAP = 1 << 7; 37 private static final int TEST_USER_PRIORITY_LIMIT = 5; 38 private static final int TEST_STREAM_TIMEOUT_US = 5550; 39 40 @Before setUp()41 public void setUp() { 42 assumeTrue(SdkLevel.isAtLeastV()); 43 } 44 45 /** Create an MscsParams object with all fields set to the test values. */ createTestMscsParams()46 private MscsParams createTestMscsParams() { 47 return new MscsParams.Builder() 48 .setFrameClassifierFields(TEST_FRAME_CLASSIFIER_FIELDS) 49 .setUserPriorityBitmap(TEST_USER_PRIORITY_BITMAP) 50 .setUserPriorityLimit(TEST_USER_PRIORITY_LIMIT) 51 .setStreamTimeoutUs(TEST_STREAM_TIMEOUT_US) 52 .build(); 53 } 54 55 /** Test that an exception is thrown when invalid values are provided to the builder. */ 56 @Test testBuilderInvalid()57 public void testBuilderInvalid() { 58 MscsParams.Builder builder = new MscsParams.Builder(); 59 60 // Bitmap arguments can only use bits 0 - 7. 61 assertThrows(IllegalArgumentException.class, 62 () -> builder.setFrameClassifierFields(1 << 8)); 63 assertThrows(IllegalArgumentException.class, 64 () -> builder.setUserPriorityBitmap(1 << 8)); 65 66 // User priority limit must be between 0 - 7 (inclusive) 67 assertThrows(IllegalArgumentException.class, 68 () -> builder.setUserPriorityLimit(-1)); 69 assertThrows(IllegalArgumentException.class, 70 () -> builder.setUserPriorityLimit(8)); 71 72 // Stream timeout value must be between 0 - 60 seconds. 73 assertThrows(IllegalArgumentException.class, 74 () -> builder.setStreamTimeoutUs(-1)); 75 assertThrows(IllegalArgumentException.class, 76 () -> builder.setStreamTimeoutUs(MscsParams.MAX_STREAM_TIMEOUT_US + 1)); 77 } 78 79 /** 80 * Tests that the builder works as expected when provided valid values. 81 * Fields that are unset should be assigned their default value. 82 */ 83 @Test testBuilderValid()84 public void testBuilderValid() { 85 MscsParams params = new MscsParams.Builder() 86 .setFrameClassifierFields(TEST_FRAME_CLASSIFIER_FIELDS) 87 .setUserPriorityLimit(TEST_USER_PRIORITY_LIMIT) 88 .build(); 89 assertEquals(TEST_FRAME_CLASSIFIER_FIELDS, params.getFrameClassifierFields()); 90 assertEquals(TEST_USER_PRIORITY_LIMIT, params.getUserPriorityLimit()); 91 92 // Fields that were not explicitly assigned should be given a default value. 93 assertEquals(MscsParams.DEFAULT_USER_PRIORITY_BITMAP, params.getUserPriorityBitmap()); 94 assertEquals(MscsParams.MAX_STREAM_TIMEOUT_US, params.getStreamTimeoutUs()); 95 } 96 97 /** 98 * Tests that all fields are assigned a default value if they are not explicitly 99 * assigned in the builder. 100 */ 101 @Test testBuilderDefaultValues()102 public void testBuilderDefaultValues() { 103 MscsParams params = new MscsParams.Builder().build(); 104 assertEquals(MscsParams.DEFAULT_FRAME_CLASSIFIER_FIELDS, params.getFrameClassifierFields()); 105 assertEquals(MscsParams.DEFAULT_USER_PRIORITY_BITMAP, params.getUserPriorityBitmap()); 106 assertEquals(MscsParams.DEFAULT_USER_PRIORITY_LIMIT, params.getUserPriorityLimit()); 107 assertEquals(MscsParams.MAX_STREAM_TIMEOUT_US, params.getStreamTimeoutUs()); 108 } 109 110 /** Tests that this class can be properly parceled and unparceled. */ 111 @Test testParcelReadWrite()112 public void testParcelReadWrite() { 113 MscsParams params = createTestMscsParams(); 114 Parcel parcel = Parcel.obtain(); 115 params.writeToParcel(parcel, 0); 116 parcel.setDataPosition(0); // Rewind data position back to the beginning for read. 117 MscsParams unparceledParams = MscsParams.CREATOR.createFromParcel(parcel); 118 assertTrue(unparceledParams.equals(params)); 119 } 120 121 /** Tests the equality and hashcode operations on equivalent instances. */ 122 @Test testSameObjectComparison()123 public void testSameObjectComparison() { 124 MscsParams params1 = createTestMscsParams(); 125 MscsParams params2 = createTestMscsParams(); 126 assertTrue(params1.equals(params2)); 127 assertEquals(params1.hashCode(), params2.hashCode()); 128 } 129 130 /** Tests the equality and hashcode operations on different instances. */ 131 @Test testDifferentObjectComparison()132 public void testDifferentObjectComparison() { 133 MscsParams testParams = createTestMscsParams(); 134 MscsParams defaultParams = new MscsParams.Builder().build(); 135 assertFalse(testParams.equals(defaultParams)); 136 assertNotEquals(testParams.hashCode(), defaultParams.hashCode()); 137 } 138 } 139