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