1 /*
2  * Copyright (C) 2016 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;
18 
19 import static junit.framework.Assert.assertTrue;
20 
21 import android.net.wifi.RttManager;
22 import android.net.wifi.RttManager.ParcelableRttParams;
23 import android.net.wifi.RttManager.RttParams;
24 import android.os.Parcel;
25 
26 import androidx.test.filters.SmallTest;
27 
28 import org.junit.Test;
29 
30 /**
31  * Unit test for {@link RttManager}
32  */
33 @SmallTest
34 public class RttManagerTest {
35 
36     // Verify ParcelableRttParams are the same after writing and reading from parcel.
verifyReadWriteParcelForRttParams(ParcelableRttParams params)37     private void verifyReadWriteParcelForRttParams(ParcelableRttParams params) {
38         Parcel parcel = Parcel.obtain();
39         params.writeToParcel(parcel, 0);
40         parcel.setDataPosition(0);
41         ParcelableRttParams paramsFromParcel = ParcelableRttParams.CREATOR.createFromParcel(parcel);
42         assertTrue(verifyEquals(params, paramsFromParcel));
43     }
44 
45     // Check if two ParcelableRttParams equals.
verifyEquals(ParcelableRttParams params, ParcelableRttParams params2)46     private boolean verifyEquals(ParcelableRttParams params, ParcelableRttParams params2) {
47         if (params.mParams == params2.mParams) {
48             return true;
49         }
50         if (params == null || params2.mParams == null) {
51             return false;
52         }
53         RttParams[] paramsArray = params.mParams;
54         RttParams[] paramsArray2 = params2.mParams;
55         if (paramsArray.length != paramsArray2.length) {
56             return false;
57         }
58         for (int i = 0; i < paramsArray.length; i++) {
59             if (!rttParamsEquals(paramsArray[i], paramsArray2[i])) {
60                 return false;
61             }
62         }
63         return true;
64     }
65 
66     // Check if two RttParams equals. Note only a subset of fields are checked.
rttParamsEquals(RttParams params1, RttParams params2)67     private boolean rttParamsEquals(RttParams params1, RttParams params2) {
68         return params1.bssid.equals(params2.bssid)
69                 && params1.secure == params2.secure
70                 && params1.frequency == params2.frequency;
71     }
72 
73     /**
74      * Test writing and reading {@link RttParams} from Parcel.
75      */
76     @Test
testRttParamsReadWriteParcel()77     public void testRttParamsReadWriteParcel() throws Exception {
78         RttParams params = new RttParams();
79         params.bssid = "12-34-56-78-9A-BC";
80         params.secure = true;
81         params.frequency = 5240;
82 
83         RttParams params2 = new RttParams();
84         params2.bssid = "12-34-56-78-9B-CD";
85         params2.secure = false;
86         params2.frequency = 5220;
87 
88         ParcelableRttParams parcelableParams = new ParcelableRttParams(new RttParams[] {
89                 params, params2
90         });
91         verifyReadWriteParcelForRttParams(parcelableParams);
92         // Make sure writing/reading parcel doesn't change value for empty RttParams.
93         verifyReadWriteParcelForRttParams(new ParcelableRttParams(new RttParams[0]));
94     }
95 
96 }
97