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.Bundle; 19 import android.os.Parcel; 20 import android.test.AndroidTestCase; 21 import android.test.MoreAsserts; 22 import android.test.suitebuilder.annotation.SmallTest; 23 24 import com.android.managedprovisioning.model.WifiInfo; 25 26 import java.lang.Exception; 27 28 /** Tests for {@link WifiInfo} */ 29 public class WifiInfoTest extends AndroidTestCase { 30 private static final String TEST_SSID = "TestWifi"; 31 private static final boolean TEST_HIDDEN = true; 32 private static final String TEST_HIDDEN_STR = Boolean.toString(TEST_HIDDEN); 33 private static final String TEST_SECURITY_TYPE = "WPA2"; 34 private static final String TEST_PASSWORD = "TestPassword"; 35 private static final String TEST_PROXY_HOST = "proxy.example.com"; 36 private static final int TEST_PROXY_PORT = 7689; 37 private static final String TEST_PROXY_PORT_STR = Integer.toString(TEST_PROXY_PORT); 38 private static final String TEST_PROXY_BYPASS_HOSTS = "http://host1.com;https://host2.com"; 39 private static final String TEST_PAC_URL = "pac.example.com"; 40 41 @SmallTest 42 public void testBuilderWriteAndReadBack() { 43 // WHEN a WifiInfo object is constructed by a set of parameters. 44 WifiInfo wifiInfo = WifiInfo.Builder.builder() 45 .setSsid(TEST_SSID) 46 .setHidden(TEST_HIDDEN) 47 .setSecurityType(TEST_SECURITY_TYPE) 48 .setPassword(TEST_PASSWORD) 49 .setProxyHost(TEST_PROXY_HOST) 50 .setProxyPort(TEST_PROXY_PORT) 51 .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS) 52 .setPacUrl(TEST_PAC_URL) 53 .build(); 54 // THEN the same set of values are set to the object. 55 assertEquals(TEST_SSID, wifiInfo.ssid); 56 assertEquals(TEST_HIDDEN, wifiInfo.hidden); 57 assertEquals(TEST_SECURITY_TYPE, wifiInfo.securityType); 58 assertEquals(TEST_PASSWORD, wifiInfo.password); 59 assertEquals(TEST_PROXY_HOST, wifiInfo.proxyHost); 60 assertEquals(TEST_PROXY_PORT, wifiInfo.proxyPort); 61 assertEquals(TEST_PROXY_BYPASS_HOSTS, wifiInfo.proxyBypassHosts); 62 assertEquals(TEST_PAC_URL, wifiInfo.pacUrl); 63 } 64 65 @SmallTest 66 public void testFailToConstructWifiInfoWithoutSsid() { 67 try { 68 // WHEN a WifiInfo object is constructed without Ssid. 69 WifiInfo wifiInfo = WifiInfo.Builder.builder() 70 .setHidden(TEST_HIDDEN) 71 .setSecurityType(TEST_SECURITY_TYPE) 72 .setPassword(TEST_PASSWORD) 73 .setProxyHost(TEST_PROXY_HOST) 74 .setProxyPort(TEST_PROXY_PORT) 75 .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS) 76 .setPacUrl(TEST_PAC_URL) 77 .build(); 78 fail("Ssid is mandatory."); 79 } catch (IllegalArgumentException e) { 80 // THEN the WifiInfo object fails to construct due to missing ssid. 81 } 82 } 83 84 @SmallTest 85 public void testEquals() { 86 // GIVEN 2 WifiInfo objects are constructed with the same set of parameters. 87 WifiInfo wifiInfo1 = WifiInfo.Builder.builder() 88 .setSsid(TEST_SSID) 89 .setHidden(TEST_HIDDEN) 90 .setSecurityType(TEST_SECURITY_TYPE) 91 .setPassword(TEST_PASSWORD) 92 .setProxyHost(TEST_PROXY_HOST) 93 .setProxyPort(TEST_PROXY_PORT) 94 .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS) 95 .setPacUrl(TEST_PAC_URL) 96 .build(); 97 WifiInfo wifiInfo2 = WifiInfo.Builder.builder() 98 .setSsid(TEST_SSID) 99 .setHidden(TEST_HIDDEN) 100 .setSecurityType(TEST_SECURITY_TYPE) 101 .setPassword(TEST_PASSWORD) 102 .setProxyHost(TEST_PROXY_HOST) 103 .setProxyPort(TEST_PROXY_PORT) 104 .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS) 105 .setPacUrl(TEST_PAC_URL) 106 .build(); 107 // WHEN comparing these two objects. 108 // THEN they are equal. 109 assertEquals(wifiInfo1, wifiInfo2); 110 } 111 112 @SmallTest 113 public void testNotEquals() { 114 // GIVEN 2 WifiInfo objects are constructed with the same set of parameters. 115 WifiInfo wifiInfo1 = WifiInfo.Builder.builder() 116 .setSsid(TEST_SSID) 117 .setHidden(TEST_HIDDEN) 118 .setSecurityType(TEST_SECURITY_TYPE) 119 .setPassword(TEST_PASSWORD) 120 .setProxyHost(TEST_PROXY_HOST) 121 .setProxyPort(TEST_PROXY_PORT) 122 .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS) 123 .setPacUrl(TEST_PAC_URL) 124 .build(); 125 WifiInfo wifiInfo2 = WifiInfo.Builder.builder() 126 .setSsid("TestWifi2") 127 .setHidden(TEST_HIDDEN) 128 .setSecurityType(TEST_SECURITY_TYPE) 129 .setPassword(TEST_PASSWORD) 130 .setProxyHost(TEST_PROXY_HOST) 131 .setProxyPort(TEST_PROXY_PORT) 132 .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS) 133 .setPacUrl(TEST_PAC_URL) 134 .build(); 135 // WHEN comparing these two objects. 136 // THEN they are not equal. 137 MoreAsserts.assertNotEqual(wifiInfo1, wifiInfo2); 138 } 139 140 @SmallTest 141 public void testParceable() { 142 // GIVEN a WifiInfo object. 143 WifiInfo expectedWifiInfo = WifiInfo.Builder.builder() 144 .setSsid(TEST_SSID) 145 .setHidden(TEST_HIDDEN) 146 .setSecurityType(TEST_SECURITY_TYPE) 147 .setPassword(TEST_PASSWORD) 148 .setProxyHost(TEST_PROXY_HOST) 149 .setProxyPort(TEST_PROXY_PORT) 150 .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS) 151 .setPacUrl(TEST_PAC_URL) 152 .build(); 153 154 // WHEN the WifiInfo is written to parcel and then read back. 155 Parcel parcel = Parcel.obtain(); 156 expectedWifiInfo.writeToParcel(parcel, 0); 157 parcel.setDataPosition(0); 158 WifiInfo actualWifiInfo = WifiInfo.CREATOR.createFromParcel(parcel); 159 160 // THEN the same WifiInfo is obtained. 161 assertEquals(expectedWifiInfo, actualWifiInfo); 162 } 163 } 164