• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.server.wifi;
18 
19 import static org.junit.Assert.*;
20 import static org.mockito.Mockito.*;
21 
22 import android.util.Xml;
23 
24 import androidx.test.filters.SmallTest;
25 
26 import com.android.internal.util.FastXmlSerializer;
27 
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.xmlpull.v1.XmlPullParser;
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.HashMap;
37 import java.util.Map;
38 
39 /**
40  * Unit tests for {@link com.android.server.wifi.RandomizedMacStoreData}.
41  */
42 @SmallTest
43 public class RandomizedMacStoreDataTest {
44     private static final String TEST_MAC_ADDRESS_1 = "da:a1:19:0:0:0";
45     private static final String TEST_MAC_ADDRESS_2 = "ff:ff:ff:0:0:0";
46     private static final String TEST_CONFIG_KEY_1 = "TP-LINK_B6C1_5GWPA_PSK";
47     private static final String TEST_CONFIG_KEY_2 = "GoogleGuest-LegacyNONE";
48     private RandomizedMacStoreData mRandomizedMacStoreData;
49 
50     @Before
setUp()51     public void setUp() throws Exception {
52         mRandomizedMacStoreData = new RandomizedMacStoreData();
53     }
54 
55     /**
56      * Helper function for serializing data to a XML block.
57      *
58      * @return byte[] of the XML data
59      * @throws Exception
60      */
serializeData()61     private byte[] serializeData() throws Exception {
62         final XmlSerializer out = new FastXmlSerializer();
63         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
64         out.setOutput(outputStream, StandardCharsets.UTF_8.name());
65         mRandomizedMacStoreData.serializeData(out);
66         out.flush();
67         return outputStream.toByteArray();
68     }
69 
70     /**
71      * Helper function for parsing data from a XML block.
72      *
73      * @param data XML data to parse from
74      * @return Map from configKey to MAC address
75      * @throws Exception
76      */
deserializeData(byte[] data)77     private Map<String, String> deserializeData(byte[] data) throws Exception {
78         final XmlPullParser in = Xml.newPullParser();
79         final ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
80         in.setInput(inputStream, StandardCharsets.UTF_8.name());
81         mRandomizedMacStoreData.deserializeData(in, in.getDepth());
82         return mRandomizedMacStoreData.getMacMapping();
83     }
84 
85     /**
86      * Verify that serializing empty Map causes no crash and no data should be serialized.
87      *
88      * @throws Exception
89      */
90     @Test
serializeEmptyMap()91     public void serializeEmptyMap() throws Exception {
92         assertEquals(0, serializeData().length);
93     }
94 
95     /**
96      * Verify that parsing an empty data doesn't cause any crash and no configuration should
97      * be deserialized.
98      *
99      * @throws Exception
100      */
101     @Test
deserializeEmptyData()102     public void deserializeEmptyData() throws Exception {
103         assertTrue(deserializeData(new byte[0]).isEmpty());
104     }
105 
106     /**
107      * Verify that RandomizedMacStoreData is written to
108      * {@link WifiConfigStore#STORE_FILE_SHARED_GENERAL}.
109      *
110      * @throws Exception
111      */
112     @Test
getSharedStoreFileId()113     public void getSharedStoreFileId() throws Exception {
114         assertEquals(WifiConfigStore.STORE_FILE_SHARED_GENERAL,
115                 mRandomizedMacStoreData.getStoreFileId());
116     }
117 
118     /**
119      * Verify that MAC address mapping data is serialized and deserialized correctly.
120      * @throws Exception
121      */
122     @Test
testSerializeDeserialize()123     public void testSerializeDeserialize() throws Exception {
124         Map<String, String> macMap = new HashMap<>();
125         macMap.put(TEST_CONFIG_KEY_1, TEST_MAC_ADDRESS_1);
126         macMap.put(TEST_CONFIG_KEY_2, TEST_MAC_ADDRESS_2);
127         mRandomizedMacStoreData.setMacMapping(macMap);
128         byte[] data = serializeData();
129         Map<String, String> deserializedMap = deserializeData(data);
130         assertEquals(macMap, deserializedMap);
131     }
132 }
133