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 import android.os.PersistableBundle;
28 
29 import com.android.modules.utils.build.SdkLevel;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 
34 import java.util.Arrays;
35 import java.util.Objects;
36 
37 public class OuiKeyedDataTest {
38     private static final String INT_FIELD_KEY = "intField";
39     private static final String STRING_FIELD_KEY = "stringField";
40     private static final String ARRAY_FIELD_KEY = "arrayField";
41 
42     private final int mTestOui = 0x00112233;
43     private PersistableBundle mTestData;
44     private final int mIntField = 123;
45     private final String mStringField = "someString";
46     private final int[] mArrayField = new int[] {1, 2, 3};
47 
48     @Before
setUp()49     public void setUp() {
50         assumeTrue(SdkLevel.isAtLeastV());
51         mTestData = createTestBundle();
52     }
53 
createTestBundle()54     private PersistableBundle createTestBundle() {
55         PersistableBundle bundle = new PersistableBundle();
56         bundle.putInt(INT_FIELD_KEY, mIntField);
57         bundle.putString(STRING_FIELD_KEY, mStringField);
58         bundle.putIntArray(ARRAY_FIELD_KEY, mArrayField.clone());
59         return bundle;
60     }
61 
validateTestBundle(PersistableBundle bundle)62     private boolean validateTestBundle(PersistableBundle bundle) {
63         return (bundle != null)
64                 && Objects.equals(bundle.getInt(INT_FIELD_KEY), mIntField)
65                 && Objects.equals(bundle.getString(STRING_FIELD_KEY), mStringField)
66                 && Arrays.equals(bundle.getIntArray(ARRAY_FIELD_KEY), mArrayField);
67     }
68 
69     /** Tests that the builder throws an exception if given an invalid OUI. */
70     @Test
testBuilderInvalidOui()71     public void testBuilderInvalidOui() {
72         int invalidOui = 0;
73         final OuiKeyedData.Builder zeroOuiBuilder = new OuiKeyedData.Builder(invalidOui, mTestData);
74         assertThrows(IllegalArgumentException.class, () -> zeroOuiBuilder.build());
75 
76         invalidOui = 0x11000000; // larger than 24 bits
77         final OuiKeyedData.Builder invalidOuiBuilder =
78                 new OuiKeyedData.Builder(invalidOui, mTestData);
79         assertThrows(IllegalArgumentException.class, () -> invalidOuiBuilder.build());
80     }
81 
82     /** Tests that the builder throws an exception if given a null data value. */
83     @Test
testBuilderNullData()84     public void testBuilderNullData() {
85         final OuiKeyedData.Builder builder = new OuiKeyedData.Builder(mTestOui, null);
86         assertThrows(IllegalArgumentException.class, () -> builder.build());
87     }
88 
89     /** Tests that this class can be properly parceled and unparceled. */
90     @Test
testParcelReadWrite()91     public void testParcelReadWrite() {
92         OuiKeyedData data = new OuiKeyedData.Builder(mTestOui, mTestData).build();
93         Parcel parcel = Parcel.obtain();
94         data.writeToParcel(parcel, 0);
95         parcel.setDataPosition(0); // Rewind data position back to the beginning for read.
96         OuiKeyedData unparceledData = OuiKeyedData.CREATOR.createFromParcel(parcel);
97 
98         assertEquals(mTestOui, unparceledData.getOui());
99         assertTrue(validateTestBundle(unparceledData.getData()));
100     }
101 
102     /** Tests the equality and hashcode operations on equivalent instances. */
103     @Test
testSameObjectComparison()104     public void testSameObjectComparison() {
105         OuiKeyedData data1 = new OuiKeyedData.Builder(mTestOui, mTestData).build();
106         OuiKeyedData data2 = new OuiKeyedData.Builder(mTestOui, mTestData).build();
107         assertTrue(data1.equals(data2));
108         assertEquals(data1.hashCode(), data2.hashCode());
109     }
110 
111     /** Tests the equality and hashcode operations on different instances. */
112     @Test
testDifferentObjectComparison()113     public void testDifferentObjectComparison() {
114         PersistableBundle otherBundle = new PersistableBundle();
115         OuiKeyedData data1 = new OuiKeyedData.Builder(mTestOui, mTestData).build();
116         OuiKeyedData data2 = new OuiKeyedData.Builder(mTestOui, otherBundle).build();
117         assertFalse(data1.equals(data2));
118         assertNotEquals(data1.hashCode(), data2.hashCode());
119     }
120 }
121