1 /*
2  * Copyright (C) 2022 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 com.android.server.wifi.hal;
18 
19 import static junit.framework.Assert.assertEquals;
20 
21 import static org.mockito.Mockito.when;
22 
23 import android.hardware.wifi.common.OuiKeyedData;
24 import android.net.wifi.util.PersistableBundleUtils;
25 import android.os.PersistableBundle;
26 
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.function.Supplier;
30 
31 public class HalTestUtils {
32     private static final String BUNDLE_STRING_FIELD_KEY = "stringField";
33     private static final String BUNDLE_ARRAY_FIELD_KEY = "arrayField";
34 
35     private static final String BUNDLE_STRING_FIELD_VALUE = "someString";
36     private static final int[] BUNDLE_ARRAY_FIELD_VALUE = new int[] {1, 2, 3};
37 
38     /**
39      * Check that we get the expected return value when the specified method is called.
40      *
41      * @param calledMethod Method to call on mDut.
42      * @param mockedMethod Method called by mDut to retrieve the value.
43      * @param value Value that the mockedMethod should return.
44      */
verifyReturnValue(Supplier<T> calledMethod, T mockedMethod, T value)45     public static <T> void verifyReturnValue(Supplier<T> calledMethod, T mockedMethod, T value) {
46         when(mockedMethod).thenReturn(value);
47         T retrievedValue = calledMethod.get();
48         assertEquals(value, retrievedValue);
49     }
50 
51     /**
52      * Generate a single PersistableBundle containing several test fields.
53      */
createTestPersistableBundle()54     private static PersistableBundle createTestPersistableBundle() {
55         PersistableBundle bundle = new PersistableBundle();
56         bundle.putString(BUNDLE_STRING_FIELD_KEY, BUNDLE_STRING_FIELD_VALUE);
57         bundle.putIntArray(BUNDLE_ARRAY_FIELD_KEY, BUNDLE_ARRAY_FIELD_VALUE);
58         return bundle;
59     }
60 
61     /**
62      * Generate a list of HAL OuiKeyedData objects, each containing several test fields.
63      */
createHalOuiKeyedDataList(int size)64     public static OuiKeyedData[] createHalOuiKeyedDataList(int size) {
65         OuiKeyedData[] ouiKeyedDataList = new OuiKeyedData[size];
66         for (int i = 0; i < size; i++) {
67             OuiKeyedData ouiKeyedData = new OuiKeyedData();
68             ouiKeyedData.oui = i + 1;
69             ouiKeyedData.vendorData = createTestPersistableBundle();
70             ouiKeyedDataList[i] = ouiKeyedData;
71         }
72         return ouiKeyedDataList;
73     }
74 
75     /**
76      * Generate a list of framework OuiKeyedData objects, each containing several test fields.
77      */
createFrameworkOuiKeyedDataList(int size)78     public static List<android.net.wifi.OuiKeyedData> createFrameworkOuiKeyedDataList(int size) {
79         List<android.net.wifi.OuiKeyedData> ouiKeyedDataList = new ArrayList<>();
80         for (int i = 0; i < size; i++) {
81             android.net.wifi.OuiKeyedData ouiKeyedData = new android.net.wifi.OuiKeyedData.Builder(
82                     i + 1, createTestPersistableBundle()).build();
83             ouiKeyedDataList.add(ouiKeyedData);
84         }
85         return ouiKeyedDataList;
86     }
87 
88     /**
89      * Check whether a HAL and framework OuiKeyedData object are equivalent.
90      */
ouiKeyedDataEquals( OuiKeyedData halData, android.net.wifi.OuiKeyedData frameworkData)91     public static boolean ouiKeyedDataEquals(
92             OuiKeyedData halData, android.net.wifi.OuiKeyedData frameworkData) {
93         return halData.oui == frameworkData.getOui()
94                 && PersistableBundleUtils.isEqual(halData.vendorData, frameworkData.getData());
95     }
96 
97     /**
98      * Check whether a list of HAL and framework OuiKeyedData objects are equivalent.
99      */
ouiKeyedDataListEquals( OuiKeyedData[] halDataList, List<android.net.wifi.OuiKeyedData> frameworkDataList)100     public static boolean ouiKeyedDataListEquals(
101             OuiKeyedData[] halDataList, List<android.net.wifi.OuiKeyedData> frameworkDataList) {
102         if (halDataList.length != frameworkDataList.size()) {
103             return false;
104         }
105 
106         for (int i = 0; i < halDataList.length; i++) {
107             if (!ouiKeyedDataEquals(halDataList[i], frameworkDataList.get(i))) {
108                 return false;
109             }
110         }
111         return true;
112     }
113 }
114