1 /* 2 * Copyright (C) 2017 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 com.android.server.wifi; 18 19 import static org.junit.Assert.*; 20 import static org.mockito.Mockito.*; 21 22 import android.support.test.filters.SmallTest; 23 import android.util.Xml; 24 25 import com.android.internal.util.FastXmlSerializer; 26 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.xmlpull.v1.XmlPullParser; 30 import org.xmlpull.v1.XmlPullParserException; 31 import org.xmlpull.v1.XmlSerializer; 32 33 import java.io.ByteArrayInputStream; 34 import java.io.ByteArrayOutputStream; 35 import java.nio.charset.StandardCharsets; 36 import java.util.Arrays; 37 import java.util.HashSet; 38 import java.util.Set; 39 40 /** 41 * Unit tests for {@link com.android.server.wifi.DeletedEphemeralSsidsStoreData}. 42 */ 43 @SmallTest 44 public class DeletedEphemeralSsidsStoreDataTest { 45 private static final String TEST_SSID1 = "SSID 1"; 46 private static final String TEST_SSID2 = "SSID 2"; 47 private static final String TEST_SSID_LIST_XML_STRING = 48 "<set name=\"SSIDList\">\n" 49 + "<string>" + TEST_SSID1 + "</string>\n" 50 + "<string>" + TEST_SSID2 + "</string>\n" 51 + "</set>\n"; 52 private static final byte[] TEST_SSID_LIST_XML_BYTES = 53 TEST_SSID_LIST_XML_STRING.getBytes(StandardCharsets.UTF_8); 54 private DeletedEphemeralSsidsStoreData mDeletedEphemeralSsidsStoreData; 55 56 @Before 57 public void setUp() throws Exception { 58 mDeletedEphemeralSsidsStoreData = new DeletedEphemeralSsidsStoreData(); 59 } 60 61 /** 62 * Helper function for serializing configuration data to a XML block. 63 * 64 * @param shared Flag indicating serializing shared or user configurations 65 * @return byte[] of the XML data 66 * @throws Exception 67 */ 68 private byte[] serializeData(boolean shared) throws Exception { 69 final XmlSerializer out = new FastXmlSerializer(); 70 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 71 out.setOutput(outputStream, StandardCharsets.UTF_8.name()); 72 mDeletedEphemeralSsidsStoreData.serializeData(out, shared); 73 out.flush(); 74 return outputStream.toByteArray(); 75 } 76 77 /** 78 * Helper function for parsing configuration data from a XML block. 79 * 80 * @param data XML data to parse from 81 * @param shared Flag indicating parsing of shared or user configurations 82 * @return SSID list 83 * @throws Exception 84 */ 85 private Set<String> deserializeData(byte[] data, boolean shared) throws Exception { 86 final XmlPullParser in = Xml.newPullParser(); 87 final ByteArrayInputStream inputStream = new ByteArrayInputStream(data); 88 in.setInput(inputStream, StandardCharsets.UTF_8.name()); 89 mDeletedEphemeralSsidsStoreData.deserializeData(in, in.getDepth(), shared); 90 return mDeletedEphemeralSsidsStoreData.getSsidList(); 91 } 92 93 /** 94 * Verify that a XmlPullParserException will be thrown when attempting to serialize SSID list 95 * to the share store, since the deleted ephemeral SSID list should never be persist 96 * to the share store. 97 * 98 * @throws Exception 99 */ 100 @Test(expected = XmlPullParserException.class) 101 public void serializeShareData() throws Exception { 102 serializeData(true /* shared */); 103 } 104 105 /** 106 * Verify that a XmlPullParserException will be thrown when attempting to parse SSID list 107 * from the share store, since the deleted ephemeral SSID list should never be persist 108 * to the share store. 109 * 110 * @throws Exception 111 */ 112 @Test(expected = XmlPullParserException.class) 113 public void deserializeShareData() throws Exception { 114 deserializeData(new byte[0], true /* shared */); 115 } 116 117 /** 118 * Verify that serializing the user store data without any configuration doesn't cause any 119 * crash and no data should be serialized. 120 * 121 * @throws Exception 122 */ 123 @Test 124 public void serializeEmptyConfigs() throws Exception { 125 assertEquals(0, serializeData(false /* shared */).length); 126 } 127 128 /** 129 * Verify that parsing an empty data doesn't cause any crash and no configuration should 130 * be deserialized. 131 * 132 * @throws Exception 133 */ 134 @Test 135 public void deserializeEmptyData() throws Exception { 136 assertTrue(deserializeData(new byte[0], false /* shared */).isEmpty()); 137 } 138 139 /** 140 * Verify that DeletedEphemeralSsidsStoreData does not support share data. 141 * 142 * @throws Exception 143 */ 144 @Test 145 public void supportShareData() throws Exception { 146 assertFalse(mDeletedEphemeralSsidsStoreData.supportShareData()); 147 } 148 149 /** 150 * Verify that user store SSID list is serialized correctly, matches the predefined test 151 * XML data. 152 * 153 * @throws Exception 154 */ 155 @Test 156 public void serializeSsidList() throws Exception { 157 Set<String> ssidList = new HashSet<>(); 158 ssidList.add(TEST_SSID1); 159 ssidList.add(TEST_SSID2); 160 mDeletedEphemeralSsidsStoreData.setSsidList(ssidList); 161 byte[] actualData = serializeData(false /* shared */); 162 assertTrue(Arrays.equals(TEST_SSID_LIST_XML_BYTES, actualData)); 163 } 164 165 /** 166 * Verify that user store SSID list is deserialized correctly using the predefined test XML 167 * data. 168 * 169 * @throws Exception 170 */ 171 @Test 172 public void deserializeSsidList() throws Exception { 173 Set<String> ssidList = new HashSet<>(); 174 ssidList.add(TEST_SSID1); 175 ssidList.add(TEST_SSID2); 176 assertEquals(ssidList, deserializeData(TEST_SSID_LIST_XML_BYTES, false /* shared */)); 177 } 178 } 179