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