• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.assertNull;
21 
22 import android.os.Parcel;
23 
24 import androidx.test.filters.SmallTest;
25 
26 import com.android.net.module.util.MacAddressUtils;
27 
28 import org.junit.Test;
29 
30 /** Unit tests for {@link android.net.wifi.UriParserResults}. */
31 @SmallTest
32 public class UriParserResultsTest {
33     private static final String TEST_PUBLIC_KEY = "testPublicKey";
34     private static final String TEST_INFORMATION = "testInformation";
35 
generateTestWifiConfig()36     WifiConfiguration generateTestWifiConfig() {
37         WifiConfiguration config = new WifiConfiguration();
38         final String mSsid = "\"TestAP\"";
39         final String bSsid = MacAddressUtils.createRandomUnicastAddress().toString();
40         config.SSID = mSsid;
41         config.BSSID = bSsid;
42         return config;
43     }
44 
testParcelOperation(UriParserResults testResult)45     private void testParcelOperation(UriParserResults testResult) throws Exception {
46         Parcel parcelW = Parcel.obtain();
47         testResult.writeToParcel(parcelW, 0);
48         byte[] bytes = parcelW.marshall();
49         parcelW.recycle();
50 
51         Parcel parcelR = Parcel.obtain();
52         parcelR.unmarshall(bytes, 0, bytes.length);
53         parcelR.setDataPosition(0);
54         UriParserResults fromParcel = UriParserResults.CREATOR.createFromParcel(parcelR);
55 
56         assertEquals(testResult, fromParcel);
57         assertEquals(testResult.hashCode(), fromParcel.hashCode());
58     }
59 
60     /** Verifies parcel serialization/deserialization. */
61     @Test
testParcelOperation()62     public void testParcelOperation() throws Exception {
63         final WifiConfiguration testWifiConfig = generateTestWifiConfig();
64         testParcelOperation(
65                 new UriParserResults(
66                         UriParserResults.URI_SCHEME_ZXING_WIFI_NETWORK_CONFIG,
67                         null,
68                         null,
69                         testWifiConfig));
70         testParcelOperation(
71                 new UriParserResults(
72                         UriParserResults.URI_SCHEME_DPP,
73                         TEST_PUBLIC_KEY,
74                         TEST_INFORMATION,
75                         null));
76     }
77 
78     @Test
testGetXXXMethods()79     public void testGetXXXMethods() throws Exception {
80         final WifiConfiguration testWifiConfig = generateTestWifiConfig();
81         UriParserResults testResultZxing =
82                 new UriParserResults(
83                         UriParserResults.URI_SCHEME_ZXING_WIFI_NETWORK_CONFIG,
84                         null,
85                         null,
86                         testWifiConfig);
87         assertEquals(
88                 testResultZxing.getUriScheme(),
89                 UriParserResults.URI_SCHEME_ZXING_WIFI_NETWORK_CONFIG);
90         assertNull(testResultZxing.getPublicKey());
91         assertNull(testResultZxing.getInformation());
92         assertEquals(testWifiConfig.toString(), testResultZxing.getWifiConfiguration().toString());
93 
94         UriParserResults testResultDpp =
95                 new UriParserResults(
96                         UriParserResults.URI_SCHEME_DPP,
97                         TEST_PUBLIC_KEY,
98                         TEST_INFORMATION,
99                         null);
100         assertEquals(testResultDpp.getUriScheme(), UriParserResults.URI_SCHEME_DPP);
101         assertEquals(testResultDpp.getPublicKey(), TEST_PUBLIC_KEY);
102         assertEquals(testResultDpp.getInformation(), TEST_INFORMATION);
103         assertNull(testResultDpp.getWifiConfiguration());
104     }
105 }
106