• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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 package com.android.managedprovisioning.model;
17 
18 import android.os.Parcel;
19 import android.test.AndroidTestCase;
20 import android.test.MoreAsserts;
21 import android.test.suitebuilder.annotation.SmallTest;
22 
23 /** Tests for {@link WifiInfo} */
24 public class WifiInfoTest extends AndroidTestCase {
25     private static final String TEST_SSID = "TestWifi";
26     private static final boolean TEST_HIDDEN = true;
27     private static final String TEST_HIDDEN_STR = Boolean.toString(TEST_HIDDEN);
28     private static final String TEST_SECURITY_TYPE = "WPA2";
29     private static final String TEST_PASSWORD = "TestPassword";
30     private static final String TEST_PROXY_HOST = "proxy.example.com";
31     private static final int TEST_PROXY_PORT = 7689;
32     private static final String TEST_PROXY_PORT_STR = Integer.toString(TEST_PROXY_PORT);
33     private static final String TEST_PROXY_BYPASS_HOSTS = "http://host1.com;https://host2.com";
34     private static final String TEST_PAC_URL = "pac.example.com";
35     private static final String TEST_EAP_METHOD = "TTLS";
36     private static final String TEST_PHASE2_AUTH = "PAP";
37     private static final String TEST_CA_CERT = "certificate";
38     private static final String TEST_USER_CERT = "certificate";
39     private static final String TEST_IDENTITY = "TestUser";
40     private static final String TEST_ANONYMOUS_IDENTITY = "TestAUser";
41     private static final String TEST_DOMAIN = "google.com";
42 
43     @SmallTest
testBuilderWriteAndReadBack()44     public void testBuilderWriteAndReadBack() {
45         // WHEN a WifiInfo object is constructed by a set of parameters.
46         WifiInfo wifiInfo = WifiInfo.Builder.builder()
47                 .setSsid(TEST_SSID)
48                 .setHidden(TEST_HIDDEN)
49                 .setSecurityType(TEST_SECURITY_TYPE)
50                 .setPassword(TEST_PASSWORD)
51                 .setEapMethod(TEST_EAP_METHOD)
52                 .setPhase2Auth(TEST_PHASE2_AUTH)
53                 .setCaCertificate(TEST_CA_CERT)
54                 .setUserCertificate(TEST_USER_CERT)
55                 .setIdentity(TEST_IDENTITY)
56                 .setAnonymousIdentity(TEST_ANONYMOUS_IDENTITY)
57                 .setDomain(TEST_DOMAIN)
58                 .setProxyHost(TEST_PROXY_HOST)
59                 .setProxyPort(TEST_PROXY_PORT)
60                 .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
61                 .setPacUrl(TEST_PAC_URL)
62                 .build();
63         // THEN the same set of values are set to the object.
64         assertEquals(TEST_SSID, wifiInfo.ssid);
65         assertEquals(TEST_HIDDEN, wifiInfo.hidden);
66         assertEquals(TEST_SECURITY_TYPE, wifiInfo.securityType);
67         assertEquals(TEST_PASSWORD, wifiInfo.password);
68         assertEquals(TEST_EAP_METHOD, wifiInfo.eapMethod);
69         assertEquals(TEST_PHASE2_AUTH, wifiInfo.phase2Auth);
70         assertEquals(TEST_CA_CERT, wifiInfo.caCertificate);
71         assertEquals(TEST_USER_CERT, wifiInfo.userCertificate);
72         assertEquals(TEST_IDENTITY, wifiInfo.identity);
73         assertEquals(TEST_ANONYMOUS_IDENTITY, wifiInfo.anonymousIdentity);
74         assertEquals(TEST_DOMAIN, wifiInfo.domain);
75         assertEquals(TEST_PROXY_HOST, wifiInfo.proxyHost);
76         assertEquals(TEST_PROXY_PORT, wifiInfo.proxyPort);
77         assertEquals(TEST_PROXY_BYPASS_HOSTS, wifiInfo.proxyBypassHosts);
78         assertEquals(TEST_PAC_URL, wifiInfo.pacUrl);
79     }
80 
81     @SmallTest
testFailToConstructWifiInfoWithoutSsid()82     public void testFailToConstructWifiInfoWithoutSsid() {
83         try {
84             // WHEN a WifiInfo object is constructed without Ssid.
85             WifiInfo wifiInfo = WifiInfo.Builder.builder()
86                     .setHidden(TEST_HIDDEN)
87                     .setSecurityType(TEST_SECURITY_TYPE)
88                     .setPassword(TEST_PASSWORD)
89                     .setEapMethod(TEST_EAP_METHOD)
90                     .setPhase2Auth(TEST_PHASE2_AUTH)
91                     .setCaCertificate(TEST_CA_CERT)
92                     .setUserCertificate(TEST_USER_CERT)
93                     .setIdentity(TEST_IDENTITY)
94                     .setAnonymousIdentity(TEST_ANONYMOUS_IDENTITY)
95                     .setDomain(TEST_DOMAIN)
96                     .setProxyHost(TEST_PROXY_HOST)
97                     .setProxyPort(TEST_PROXY_PORT)
98                     .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
99                     .setPacUrl(TEST_PAC_URL)
100                     .build();
101             fail("Ssid is mandatory.");
102         } catch (IllegalArgumentException e) {
103             // THEN the WifiInfo object fails to construct due to missing ssid.
104         }
105     }
106 
107     @SmallTest
testEquals()108     public void testEquals() {
109         // GIVEN 2 WifiInfo objects are constructed with the same set of parameters.
110         WifiInfo wifiInfo1 = WifiInfo.Builder.builder()
111                 .setSsid(TEST_SSID)
112                 .setHidden(TEST_HIDDEN)
113                 .setSecurityType(TEST_SECURITY_TYPE)
114                 .setPassword(TEST_PASSWORD)
115                 .setEapMethod(TEST_EAP_METHOD)
116                 .setPhase2Auth(TEST_PHASE2_AUTH)
117                 .setCaCertificate(TEST_CA_CERT)
118                 .setUserCertificate(TEST_USER_CERT)
119                 .setIdentity(TEST_IDENTITY)
120                 .setAnonymousIdentity(TEST_ANONYMOUS_IDENTITY)
121                 .setDomain(TEST_DOMAIN)
122                 .setProxyHost(TEST_PROXY_HOST)
123                 .setProxyPort(TEST_PROXY_PORT)
124                 .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
125                 .setPacUrl(TEST_PAC_URL)
126                 .build();
127         WifiInfo wifiInfo2 = WifiInfo.Builder.builder()
128                 .setSsid(TEST_SSID)
129                 .setHidden(TEST_HIDDEN)
130                 .setSecurityType(TEST_SECURITY_TYPE)
131                 .setPassword(TEST_PASSWORD)
132                 .setEapMethod(TEST_EAP_METHOD)
133                 .setPhase2Auth(TEST_PHASE2_AUTH)
134                 .setCaCertificate(TEST_CA_CERT)
135                 .setUserCertificate(TEST_USER_CERT)
136                 .setIdentity(TEST_IDENTITY)
137                 .setAnonymousIdentity(TEST_ANONYMOUS_IDENTITY)
138                 .setDomain(TEST_DOMAIN)
139                 .setProxyHost(TEST_PROXY_HOST)
140                 .setProxyPort(TEST_PROXY_PORT)
141                 .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
142                 .setPacUrl(TEST_PAC_URL)
143                 .build();
144         // WHEN comparing these two objects.
145         // THEN they are equal.
146         assertEquals(wifiInfo1, wifiInfo2);
147     }
148 
149     @SmallTest
testNotEquals()150     public void testNotEquals() {
151         // GIVEN 2 WifiInfo objects are constructed with the same set of parameters.
152         WifiInfo wifiInfo1 = WifiInfo.Builder.builder()
153                 .setSsid(TEST_SSID)
154                 .setHidden(TEST_HIDDEN)
155                 .setSecurityType(TEST_SECURITY_TYPE)
156                 .setPassword(TEST_PASSWORD)
157                 .setEapMethod(TEST_EAP_METHOD)
158                 .setPhase2Auth(TEST_PHASE2_AUTH)
159                 .setCaCertificate(TEST_CA_CERT)
160                 .setUserCertificate(TEST_USER_CERT)
161                 .setIdentity(TEST_IDENTITY)
162                 .setAnonymousIdentity(TEST_ANONYMOUS_IDENTITY)
163                 .setDomain(TEST_DOMAIN)
164                 .setProxyHost(TEST_PROXY_HOST)
165                 .setProxyPort(TEST_PROXY_PORT)
166                 .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
167                 .setPacUrl(TEST_PAC_URL)
168                 .build();
169         WifiInfo wifiInfo2 = WifiInfo.Builder.builder()
170                 .setSsid("TestWifi2")
171                 .setHidden(TEST_HIDDEN)
172                 .setSecurityType(TEST_SECURITY_TYPE)
173                 .setPassword(TEST_PASSWORD)
174                 .setEapMethod(TEST_EAP_METHOD)
175                 .setPhase2Auth(TEST_PHASE2_AUTH)
176                 .setCaCertificate(TEST_CA_CERT)
177                 .setUserCertificate(TEST_USER_CERT)
178                 .setIdentity(TEST_IDENTITY)
179                 .setAnonymousIdentity(TEST_ANONYMOUS_IDENTITY)
180                 .setDomain(TEST_DOMAIN)
181                 .setProxyHost(TEST_PROXY_HOST)
182                 .setProxyPort(TEST_PROXY_PORT)
183                 .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
184                 .setPacUrl(TEST_PAC_URL)
185                 .build();
186         // WHEN comparing these two objects.
187         // THEN they are not equal.
188         MoreAsserts.assertNotEqual(wifiInfo1, wifiInfo2);
189     }
190 
191     @SmallTest
testParceable()192     public void testParceable() {
193         // GIVEN a WifiInfo object.
194         WifiInfo expectedWifiInfo = WifiInfo.Builder.builder()
195                 .setSsid(TEST_SSID)
196                 .setHidden(TEST_HIDDEN)
197                 .setSecurityType(TEST_SECURITY_TYPE)
198                 .setPassword(TEST_PASSWORD)
199                 .setEapMethod(TEST_EAP_METHOD)
200                 .setPhase2Auth(TEST_PHASE2_AUTH)
201                 .setCaCertificate(TEST_CA_CERT)
202                 .setUserCertificate(TEST_USER_CERT)
203                 .setIdentity(TEST_IDENTITY)
204                 .setAnonymousIdentity(TEST_ANONYMOUS_IDENTITY)
205                 .setDomain(TEST_DOMAIN)
206                 .setProxyHost(TEST_PROXY_HOST)
207                 .setProxyPort(TEST_PROXY_PORT)
208                 .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
209                 .setPacUrl(TEST_PAC_URL)
210                 .build();
211 
212         // WHEN the WifiInfo is written to parcel and then read back.
213         Parcel parcel = Parcel.obtain();
214         expectedWifiInfo.writeToParcel(parcel, 0);
215         parcel.setDataPosition(0);
216         WifiInfo actualWifiInfo = WifiInfo.CREATOR.createFromParcel(parcel);
217 
218         // THEN the same WifiInfo is obtained.
219         assertEquals(expectedWifiInfo, actualWifiInfo);
220     }
221 }
222