1 /* 2 * Copyright (C) 2019 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 20 import static org.junit.Assert.assertEquals; 21 import static org.junit.Assert.assertNotNull; 22 23 import androidx.test.filters.SmallTest; 24 25 import org.junit.Test; 26 27 import java.io.ByteArrayInputStream; 28 import java.io.ByteArrayOutputStream; 29 import java.io.DataOutputStream; 30 import java.io.IOException; 31 import java.io.InputStream; 32 33 /** 34 * Unit tests for {@link android.net.wifi.SoftApConfToXmlMigrationUtilTest}. 35 */ 36 @SmallTest 37 public class SoftApConfToXmlMigrationUtilTest { 38 private static final String TEST_SSID = "SSID"; 39 private static final String TEST_PASSPHRASE = "TestPassphrase"; 40 private static final int TEST_CHANNEL = 0; 41 private static final boolean TEST_HIDDEN = false; 42 private static final int TEST_BAND = SoftApConfiguration.BAND_5GHZ; 43 private static final int TEST_SECURITY = SoftApConfiguration.SECURITY_TYPE_WPA2_PSK; 44 45 private static final String TEST_EXPECTED_XML_STRING = 46 "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n" 47 + "<WifiConfigStoreData>\n" 48 + "<int name=\"Version\" value=\"3\" />\n" 49 + "<SoftAp>\n" 50 + "<string name=\"SSID\">" + TEST_SSID + "</string>\n" 51 + "<int name=\"ApBand\" value=\"" + TEST_BAND + "\" />\n" 52 + "<int name=\"Channel\" value=\"" + TEST_CHANNEL + "\" />\n" 53 + "<boolean name=\"HiddenSSID\" value=\"" + TEST_HIDDEN + "\" />\n" 54 + "<int name=\"SecurityType\" value=\"" + TEST_SECURITY + "\" />\n" 55 + "<string name=\"Passphrase\">" + TEST_PASSPHRASE + "</string>\n" 56 + "<int name=\"MaxNumberOfClients\" value=\"0\" />\n" 57 + "<boolean name=\"ClientControlByUser\" value=\"false\" />\n" 58 + "<boolean name=\"AutoShutdownEnabled\" value=\"true\" />\n" 59 + "<long name=\"ShutdownTimeoutMillis\" value=\"-1\" />\n" 60 + "<BlockedClientList />\n" 61 + "<AllowedClientList />\n" 62 + "</SoftAp>\n" 63 + "</WifiConfigStoreData>\n"; 64 createLegacyApConfFile(WifiConfiguration config)65 private byte[] createLegacyApConfFile(WifiConfiguration config) throws Exception { 66 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 67 DataOutputStream out = new DataOutputStream(outputStream); 68 out.writeInt(3); 69 out.writeUTF(config.SSID); 70 out.writeInt(config.apBand); 71 out.writeInt(config.apChannel); 72 out.writeBoolean(config.hiddenSSID); 73 int authType = config.getAuthType(); 74 out.writeInt(authType); 75 if (authType != WifiConfiguration.KeyMgmt.NONE) { 76 out.writeUTF(config.preSharedKey); 77 } 78 out.close(); 79 return outputStream.toByteArray(); 80 } 81 82 /** 83 * Generate a SoftApConfiguration based on the specified parameters. 84 */ setupApConfig( String ssid, String preSharedKey, int keyManagement, int band, int channel, boolean hiddenSSID)85 private SoftApConfiguration setupApConfig( 86 String ssid, String preSharedKey, int keyManagement, int band, int channel, 87 boolean hiddenSSID) { 88 SoftApConfiguration.Builder configBuilder = new SoftApConfiguration.Builder(); 89 configBuilder.setSsid(ssid); 90 configBuilder.setPassphrase(preSharedKey, SoftApConfiguration.SECURITY_TYPE_WPA2_PSK); 91 if (channel == 0) { 92 configBuilder.setBand(band); 93 } else { 94 configBuilder.setChannel(channel, band); 95 } 96 configBuilder.setHiddenSsid(hiddenSSID); 97 return configBuilder.build(); 98 } 99 100 /** 101 * Generate a WifiConfiguration based on the specified parameters. 102 */ setupWifiConfigurationApConfig( String ssid, String preSharedKey, int keyManagement, int band, int channel, boolean hiddenSSID)103 private WifiConfiguration setupWifiConfigurationApConfig( 104 String ssid, String preSharedKey, int keyManagement, int band, int channel, 105 boolean hiddenSSID) { 106 WifiConfiguration config = new WifiConfiguration(); 107 config.SSID = ssid; 108 config.preSharedKey = preSharedKey; 109 config.allowedKeyManagement.set(keyManagement); 110 config.apBand = band; 111 config.apChannel = channel; 112 config.hiddenSSID = hiddenSSID; 113 return config; 114 } 115 116 /** 117 * Asserts that the WifiConfigurations equal to SoftApConfiguration. 118 * This only compares the elements saved 119 * for softAp used. 120 */ assertWifiConfigurationEqualSoftApConfiguration( WifiConfiguration backup, SoftApConfiguration restore)121 public static void assertWifiConfigurationEqualSoftApConfiguration( 122 WifiConfiguration backup, SoftApConfiguration restore) { 123 assertEquals(backup.SSID, restore.getSsid()); 124 assertEquals(backup.BSSID, restore.getBssid()); 125 assertEquals(SoftApConfToXmlMigrationUtil.convertWifiConfigBandToSoftApConfigBand( 126 backup.apBand), 127 restore.getBand()); 128 assertEquals(backup.apChannel, restore.getChannel()); 129 assertEquals(backup.preSharedKey, restore.getPassphrase()); 130 if (backup.getAuthType() == WifiConfiguration.KeyMgmt.WPA2_PSK) { 131 assertEquals(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK, restore.getSecurityType()); 132 } else { 133 assertEquals(SoftApConfiguration.SECURITY_TYPE_OPEN, restore.getSecurityType()); 134 } 135 assertEquals(backup.hiddenSSID, restore.isHiddenSsid()); 136 } 137 138 /** 139 * Note: This is a copy of {@link AtomicFile#readFully()} modified to use the passed in 140 * {@link InputStream} which was returned using {@link AtomicFile#openRead()}. 141 */ readFully(InputStream stream)142 private static byte[] readFully(InputStream stream) throws IOException { 143 try { 144 int pos = 0; 145 int avail = stream.available(); 146 byte[] data = new byte[avail]; 147 while (true) { 148 int amt = stream.read(data, pos, data.length - pos); 149 if (amt <= 0) { 150 return data; 151 } 152 pos += amt; 153 avail = stream.available(); 154 if (avail > data.length - pos) { 155 byte[] newData = new byte[pos + avail]; 156 System.arraycopy(data, 0, newData, 0, pos); 157 data = newData; 158 } 159 } 160 } finally { 161 stream.close(); 162 } 163 } 164 165 /** 166 * Tests conversion from legacy .conf file to XML file format. 167 */ 168 @Test testConversion()169 public void testConversion() throws Exception { 170 WifiConfiguration backupConfig = setupWifiConfigurationApConfig( 171 TEST_SSID, /* SSID */ 172 TEST_PASSPHRASE, /* preshared key */ 173 WifiConfiguration.KeyMgmt.WPA2_PSK, /* key management */ 174 1, /* AP band (5GHz) */ 175 TEST_CHANNEL, /* AP channel */ 176 TEST_HIDDEN /* Hidden SSID */); 177 SoftApConfiguration expectedConfig = setupApConfig( 178 TEST_SSID, /* SSID */ 179 TEST_PASSPHRASE, /* preshared key */ 180 SoftApConfiguration.SECURITY_TYPE_WPA2_PSK, /* security type */ 181 SoftApConfiguration.BAND_5GHZ, /* AP band (5GHz) */ 182 TEST_CHANNEL, /* AP channel */ 183 TEST_HIDDEN /* Hidden SSID */); 184 185 assertWifiConfigurationEqualSoftApConfiguration(backupConfig, expectedConfig); 186 187 byte[] confBytes = createLegacyApConfFile(backupConfig); 188 assertNotNull(confBytes); 189 190 InputStream xmlStream = SoftApConfToXmlMigrationUtil.convert( 191 new ByteArrayInputStream(confBytes)); 192 193 byte[] xmlBytes = readFully(xmlStream); 194 assertNotNull(xmlBytes); 195 196 assertEquals(TEST_EXPECTED_XML_STRING, new String(xmlBytes)); 197 } 198 199 } 200